test_layernorm.cu 11.4 KB
Newer Older
Przemek Tredak's avatar
Przemek Tredak committed
1
/*************************************************************************
2
 * Copyright (c) 2022-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
Przemek Tredak's avatar
Przemek Tredak committed
3
4
5
6
 *
 * See LICENSE for license information.
 ************************************************************************/

Tim Moon's avatar
Tim Moon committed
7
8
#include <cmath>
#include <cstring>
Przemek Tredak's avatar
Przemek Tredak committed
9
10
#include <memory>
#include <iomanip>
Tim Moon's avatar
Tim Moon committed
11
#include <iostream>
Przemek Tredak's avatar
Przemek Tredak committed
12
#include <random>
Tim Moon's avatar
Tim Moon committed
13
14
15
16
17
18
19

#include <cuda_bf16.h>
#include <cuda_runtime.h>
#include <gtest/gtest.h>

#include <transformer_engine/layer_norm.h>
#include <transformer_engine/transformer_engine.h>
Przemek Tredak's avatar
Przemek Tredak committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "../test_common.h"

using namespace transformer_engine;
using namespace test;

namespace {

template <typename InputType>
void compute_ref_stats(const InputType *data, float *mu, float *rsigma,
                       const size_t N, const size_t H, const double epsilon) {
  using compute_t = float;
  for (size_t i = 0 ; i < N; ++i) {
    compute_t sum = 0;
    for (size_t j = 0; j < H; ++j) {
      compute_t current = static_cast<compute_t>(data[i * H + j]);
      sum += current;
    }
    mu[i] = sum / H;
    compute_t m = mu[i];
    sum = 0;
    for (size_t j = 0; j < H; ++j) {
      compute_t current = static_cast<compute_t>(data[i * H + j]);
      sum += (current - m) * (current - m);
    }
    sum = sum / H;
    compute_t rs = rsqrtf(sum + epsilon);
    rsigma[i] = rs;
  }
}

template <typename InputType, typename OutputType>
void compute_ref_output(const InputType *data, const InputType *gamma, const InputType *beta,
                 OutputType *output, const float *mu, const float *rsigma,
                 const size_t N, const size_t H,
54
                 float *amax, float scale, const bool zero_centered_gamma) {
Przemek Tredak's avatar
Przemek Tredak committed
55
56
57
58
59
  using compute_t = float;
  compute_t current_max = -1e100;
  for (size_t i = 0 ; i < N; ++i) {
    for (size_t j = 0; j < H; ++j) {
      compute_t current = static_cast<compute_t>(data[i * H + j]);
60
61
62
63
64
      compute_t g = static_cast<compute_t>(gamma[j]);
      if (zero_centered_gamma) {
        g += 1;
      }
      compute_t tmp = (current - mu[i]) * rsigma[i] * g + static_cast<compute_t>(beta[j]);
Przemek Tredak's avatar
Przemek Tredak committed
65
66
67
68
69
70
71
72
73
74
75
76
77
      output[i * H + j] = static_cast<OutputType>(tmp * scale);
      current_max = fmaxf(current_max, fabsf(tmp));
    }
  }
  *amax = current_max;
}

template <typename InputType, typename OutputType>
void compute_ref_backward(const OutputType *output_grad, const InputType *data,
                          const float *mu, const float *rsigma,
                          const InputType *gamma,
                          InputType *data_grad,
                          InputType *gamma_grad, InputType *beta_grad,
78
79
                          const size_t N, const size_t H,
                          const bool zero_centered_gamma) {
Przemek Tredak's avatar
Przemek Tredak committed
80
81
82
83
84
85
86
87
88
89
  using compute_t = float;
  std::vector<compute_t> dgamma(H, 0.f);
  std::vector<compute_t> dbeta(H, 0.f);

  for (size_t i = 0 ; i < N; ++i) {
    // Reductions
    compute_t mdy = 0, mdyy = 0;
    for (size_t j = 0; j < H; ++j) {
      const compute_t x = static_cast<compute_t>(data[i * H + j]);
      const compute_t y = (x - mu[i]) * rsigma[i];
90
91
92
93
      compute_t g = static_cast<compute_t>(gamma[j]);
      if (zero_centered_gamma) {
        g += 1;
      }
Przemek Tredak's avatar
Przemek Tredak committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
      const compute_t dz = static_cast<compute_t>(output_grad[i * H + j]);
      const compute_t dy = g * dz;
      dgamma[j] += y * dz;
      dbeta[j] += dz;
      mdy += dy;
      mdyy += dy * y;
    }
    mdy /= H;
    mdyy /= H;

    // Input grads
    for (size_t j = 0; j < H; ++j) {
      const compute_t x = static_cast<compute_t>(data[i * H + j]);
      const compute_t y = (x - mu[i]) * rsigma[i];
108
109
110
111
      compute_t g = static_cast<compute_t>(gamma[j]);
      if (zero_centered_gamma) {
        g += 1;
      }
Przemek Tredak's avatar
Przemek Tredak committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
      const compute_t dz = static_cast<compute_t>(output_grad[i * H + j]);
      const compute_t dy = g * dz;
      const compute_t dx = rsigma[i] * (dy - mdyy * y - mdy);
      data_grad[i * H + j] = static_cast<InputType>(dx);
    }
  }

  // Weight grads
  for (size_t j = 0; j < H; ++j) {
    gamma_grad[j] = static_cast<InputType>(dgamma[j]);
    beta_grad[j] = static_cast<InputType>(dbeta[j]);
  }
}

template <typename InputType, typename OutputType>
127
void performTest(const size_t N, const size_t H, const bool zero_centered_gamma) {
Przemek Tredak's avatar
Przemek Tredak committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  if (sizeof(InputType) < sizeof(OutputType)) {
    GTEST_SKIP() << "LN kernel does not support OutputType > InputType";
    return;
  }
  using WeightType = InputType;
  DType itype = TypeInfo<InputType>::dtype;
  DType wtype = TypeInfo<WeightType>::dtype;
  DType otype = TypeInfo<OutputType>::dtype;

  if ((itype == DType::kBFloat16 && otype == DType::kFloat16) ||
      (itype == DType::kFloat16 && otype == DType::kBFloat16)) {
    GTEST_SKIP() << "LN kernel does not support mixing Float16 and BFloat16";
    return;
  }

  Tensor input({ N, H }, itype);
  Tensor z({ N, H }, otype);
  Tensor gamma({ H }, wtype);
  Tensor beta({ H }, wtype);
  Tensor mu({ N }, DType::kFloat32);
  Tensor rsigma({ N }, DType::kFloat32);
  Tensor dz({ N, H }, wtype);
  Tensor dx({ N, H }, itype);
  Tensor dgamma({ H }, wtype);
  Tensor dbeta({ H }, wtype);
  Tensor workspace, barrier, dgamma_part, dbeta_part;

155
156
157
158
159
  fillUniform(&input);
  fillUniform(&gamma);
  fillUniform(&beta);
  setRandomScale(&z);
  fillUniform(&dz);
Przemek Tredak's avatar
Przemek Tredak committed
160
161
162
163
164
165
166
167
168
169
170
171
172

  std::unique_ptr<OutputType[]> ref_output = std::make_unique<OutputType[]>(N * H);
  std::unique_ptr<float[]> ref_mu = std::make_unique<float[]>(N);
  std::unique_ptr<float[]> ref_rsigma = std::make_unique<float[]>(N);
  std::unique_ptr<InputType[]> ref_dx = std::make_unique<InputType[]>(N * H);
  std::unique_ptr<WeightType[]> ref_dgamma = std::make_unique<InputType[]>(H);
  std::unique_ptr<WeightType[]> ref_dbeta = std::make_unique<InputType[]>(H);

  cudaDeviceProp prop;
  cudaGetDeviceProperties(&prop, 0);

  // Forward kernel
  float epsilon = 1e-5;
173
174
175
176
  auto fwd_function = zero_centered_gamma ? nvte_layernorm1p_fwd : nvte_layernorm_fwd;
  fwd_function(input.data(), gamma.data(), beta.data(), epsilon,
               z.data(), mu.data(), rsigma.data(), 0, prop.multiProcessorCount,
               workspace.data(), barrier.data());
Przemek Tredak's avatar
Przemek Tredak committed
177
178
  workspace = Tensor(workspace.shape(), workspace.dtype());
  barrier = Tensor(barrier.shape(), barrier.dtype());
179
180
181
  fwd_function(input.data(), gamma.data(), beta.data(), epsilon,
               z.data(), mu.data(), rsigma.data(), 0, prop.multiProcessorCount,
               workspace.data(), barrier.data());
Przemek Tredak's avatar
Przemek Tredak committed
182
183

  // Backward kernel
184
185
186
187
188
189
190
  auto bwd_function = zero_centered_gamma ? nvte_layernorm1p_bwd : nvte_layernorm_bwd;
  bwd_function(dz.data(), input.data(),
               mu.data(), rsigma.data(), gamma.data(),
               dx.data(), dgamma.data(), dbeta.data(),
               dgamma_part.data(), dbeta_part.data(),
               0, prop.multiProcessorCount,
               workspace.data(), barrier.data());
Przemek Tredak's avatar
Przemek Tredak committed
191
192
193
194
  workspace = Tensor(workspace.shape(), workspace.dtype());
  barrier = Tensor(barrier.shape(), barrier.dtype());
  dgamma_part = Tensor(dgamma_part.shape(), dgamma_part.dtype());
  dbeta_part = Tensor(dbeta_part.shape(), dbeta_part.dtype());
195
196
197
198
199
200
  bwd_function(dz.data(), input.data(),
               mu.data(), rsigma.data(), gamma.data(),
               dx.data(), dgamma.data(), dbeta.data(),
               dgamma_part.data(), dbeta_part.data(),
               0, prop.multiProcessorCount,
               workspace.data(), barrier.data());
Przemek Tredak's avatar
Przemek Tredak committed
201
202
203
204
205
206
207
208

  // Reference implementations
  // use the GPU stats to tighten the tolerances
  mu.to_cpu();
  rsigma.to_cpu();
  float ref_amax;
  compute_ref_stats(input.cpu_dptr<InputType>(), ref_mu.get(),
                    ref_rsigma.get(), N, H, epsilon);
209
  float ref_scale = isFp8Type(otype) ? z.scale() : 1.f;
Przemek Tredak's avatar
Przemek Tredak committed
210
211
212
213
214
215
216
217
  compute_ref_output(input.cpu_dptr<InputType>(),
                     gamma.cpu_dptr<WeightType>(),
                     beta.cpu_dptr<WeightType>(),
                     ref_output.get(),
                     mu.cpu_dptr<float>(),
                     rsigma.cpu_dptr<float>(),
                     N, H,
                     &ref_amax,
218
219
                     ref_scale,
                     zero_centered_gamma);
Przemek Tredak's avatar
Przemek Tredak committed
220
221
222
223
  compute_ref_backward(dz.cpu_dptr<WeightType>(), input.cpu_dptr<InputType>(),
                       mu.cpu_dptr<float>(), rsigma.cpu_dptr<float>(),
                       gamma.cpu_dptr<WeightType>(),
                       ref_dx.get(), ref_dgamma.get(), ref_dbeta.get(),
224
                       N, H, zero_centered_gamma);
Przemek Tredak's avatar
Przemek Tredak committed
225
226
227
228
229
230

  cudaDeviceSynchronize();
  auto err = cudaGetLastError();
  ASSERT_EQ(err, cudaSuccess) << cudaGetErrorString(err);

  auto [atol_amax, rtol_amax] = getTolerances(DType::kFloat32);
231
  if (isFp8Type(otype)) {
232
    compareResults("amax", z.amax(), ref_amax, atol_amax, rtol_amax);
233
  }
Przemek Tredak's avatar
Przemek Tredak committed
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

  auto [atol_stats, rtol_stats] = getTolerances(DType::kFloat32);
  rtol_stats = 5e-5;
  compareResults("mu", mu, ref_mu.get(), atol_stats, rtol_stats);
  compareResults("rsigma", rsigma, ref_rsigma.get(), atol_stats, rtol_stats);

  auto [atol, rtol] = getTolerances(otype);
  if (otype == DType::kFloat32) {
    atol = 5e-7;
  }
  compareResults("output", z, ref_output.get(), atol, rtol);

  double atol_bwd = 1e-4;
  double rtol_bwd = 1e-4;
  compareResults("dx", dx, ref_dx.get(), atol_bwd, rtol_bwd);
  compareResults("dgamma", dgamma, ref_dgamma.get(), atol_bwd, rtol_bwd);
  compareResults("dbeta", dbeta, ref_dbeta.get(), atol_bwd, rtol_bwd);
}

std::vector<std::pair<size_t, size_t>> test_cases = {{2048, 12288},
                                                     {768, 1024},
                                                     {256, 65536},
                                                     {128, 6144},
                                                     {64, 2304},
                                                     {229, 541},   // Primes 50, 100
                                                     {71, 3571},   // Primes 20, 500
                                                     {29, 17389}}; // Primes 10, 2000

}  // namespace

class LNTestSuite : public ::testing::TestWithParam<std::tuple<transformer_engine::DType,
                                                               transformer_engine::DType,
266
267
                                                               std::pair<size_t, size_t>,
                                                               bool>> {};
Przemek Tredak's avatar
Przemek Tredak committed
268
269
270
271
272
273
274
275

TEST_P(LNTestSuite, TestLN) {
    using namespace transformer_engine;
    using namespace test;

    const DType input_type = std::get<0>(GetParam());
    const DType output_type = std::get<1>(GetParam());
    const auto size = std::get<2>(GetParam());
276
    const bool zero_centered_gamma = std::get<3>(GetParam());
Przemek Tredak's avatar
Przemek Tredak committed
277
278
279

    TRANSFORMER_ENGINE_TYPE_SWITCH_ALL(input_type, InputType,
      TRANSFORMER_ENGINE_TYPE_SWITCH_ALL(output_type, OutputType,
280
        performTest<InputType, OutputType>(size.first, size.second, zero_centered_gamma);
Przemek Tredak's avatar
Przemek Tredak committed
281
282
283
284
285
286
287
288
289
290
      );
    );
}

INSTANTIATE_TEST_SUITE_P(
    OperatorTest,
    LNTestSuite,
    ::testing::Combine(
        ::testing::Values(DType::kFloat32, DType::kBFloat16, DType::kFloat16),
        ::testing::Values(DType::kFloat32, DType::kBFloat16, DType::kFloat16, DType::kFloat8E4M3),
291
292
        ::testing::ValuesIn(test_cases),
        ::testing::Values(false, true)),
Przemek Tredak's avatar
Przemek Tredak committed
293
294
295
296
    [](const testing::TestParamInfo<LNTestSuite::ParamType>& info) {
      std::string name = test::typeName(std::get<0>(info.param)) + "X" +
                         test::typeName(std::get<1>(info.param)) + "X" +
                         std::to_string(std::get<2>(info.param).first) + "X" +
297
298
                         std::to_string(std::get<2>(info.param).second) + "X" +
                         std::to_string(std::get<3>(info.param));
Przemek Tredak's avatar
Przemek Tredak committed
299
300
      return name;
    });