forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeightNormKernel.cpp
449 lines (407 loc) · 14 KB
/
WeightNormKernel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#define TORCH_ASSERT_NO_OPERATORS
#include <ATen/core/TensorBase.h>
#include <ATen/Dispatch.h>
#include <ATen/EmptyTensor.h>
#include <ATen/Parallel.h>
#include <ATen/OpMathType.h>
#include <ATen/native/cpu/WeightNormKernel.h>
#include <ATen/cpu/vec/functional.h>
#include <ATen/cpu/vec/vec.h>
#include <c10/util/irange.h>
namespace at::native {
namespace {
template <typename scalar_t, typename accscalar_t>
void weight_norm_first_dim_kernel(
TensorBase& w,
TensorBase& norm,
const TensorBase& v,
const TensorBase& g,
int64_t M, int64_t N) {
const auto v_data = v.data_ptr<scalar_t>();
const auto g_data = g.data_ptr<scalar_t>();
auto w_data = w.data_ptr<scalar_t>();
auto norm_data = norm.data_ptr<accscalar_t>();
using Vec = vec::Vectorized<accscalar_t>;
at::parallel_for(0, M, 1, [&](int64_t begin, int64_t end) {
for (const auto i : c10::irange(begin, end)) {
accscalar_t norm_val = vec::map_reduce_all<scalar_t>(
[](Vec x) { return x * x; },
[](Vec x, Vec y) { return x + y; },
v_data + i * N,
N);
norm_val = std::sqrt(norm_val);
norm_data[i] = norm_val;
accscalar_t a = g_data[i] / norm_val;
vec::map(
[a](Vec x) { return x * Vec(a); },
w_data + i * N,
v_data + i * N,
N);
}
});
}
template <typename scalar_t>
inline void sum_norm_per_row(
scalar_t* out_ptr,
const scalar_t* v_ptr,
int64_t size) {
using Vec = vec::Vectorized<scalar_t>;
vec::map2(
[](Vec out, Vec v) { return out + v * v; },
out_ptr,
out_ptr,
v_ptr,
size);
}
inline void sum_norm_per_row(
float* out_ptr,
const BFloat16* v_ptr,
int64_t size) {
using bVec = vec::Vectorized<BFloat16>;
using fVec = vec::Vectorized<float>;
int64_t d = 0;
for (; d < size - (size % bVec::size()); d += bVec::size()) {
bVec v_bvec = bVec::loadu(v_ptr + d);
fVec v_fvec0, v_fvec1;
std::tie(v_fvec0, v_fvec1) = convert_bfloat16_float(v_bvec);
fVec out_fvec0 = fVec::loadu(out_ptr + d) + v_fvec0 * v_fvec0;
fVec out_fvec1 = fVec::loadu(out_ptr + d + fVec::size()) + v_fvec1 * v_fvec1;
out_fvec0.store(out_ptr + d);
out_fvec1.store(out_ptr + d + fVec::size());
}
for(; d < size; ++d) {
float v_val = float(v_ptr[d]);
out_ptr[d] += v_val * v_val;
}
}
template <typename scalar_t>
inline void apply_norm_per_row(
scalar_t* w_ptr,
const scalar_t* v_ptr,
const scalar_t* a_ptr,
int64_t size) {
using Vec = vec::Vectorized<scalar_t>;
vec::map2(
[](Vec v, Vec a) { return v * a; },
w_ptr,
v_ptr,
a_ptr,
size);
}
inline void apply_norm_per_row(
BFloat16* w_ptr,
const BFloat16* v_ptr,
const float* a_ptr,
int64_t size) {
using bVec = vec::Vectorized<BFloat16>;
using fVec = vec::Vectorized<float>;
int64_t d = 0;
for (; d < size - (size % bVec::size()); d += bVec::size()) {
bVec v_bvec = bVec::loadu(v_ptr + d);
fVec v_fvec0, v_fvec1;
std::tie(v_fvec0, v_fvec1) = convert_bfloat16_float(v_bvec);
fVec w_fvec0 = fVec::loadu(a_ptr + d) * v_fvec0;
fVec w_fvec1 = fVec::loadu(a_ptr + d + fVec::size()) * v_fvec1;
bVec w_bvec = convert_float_bfloat16(w_fvec0, w_fvec1);
w_bvec.store(w_ptr + d);
}
for(; d < size; ++d) {
w_ptr[d] = float(v_ptr[d]) * a_ptr[d];
}
}
template <typename scalar_t, typename accscalar_t>
void weight_norm_last_dim_kernel(
TensorBase& w,
TensorBase& norm,
const TensorBase& v,
const TensorBase& g,
int64_t M, int64_t N) {
const auto v_data = v.data_ptr<scalar_t>();
const auto g_data = g.data_ptr<scalar_t>();
auto w_data = w.data_ptr<scalar_t>();
auto norm_data = norm.data_ptr<accscalar_t>();
int num_threads = at::get_num_threads();
TensorBase buffer = at::detail::empty_cpu({num_threads, N}, norm.options()).zero_();
auto buffer_data = buffer.data_ptr<accscalar_t>();
// vertical parallel reduction
at::parallel_for(0, M, 1, [&](int64_t begin, int64_t end) {
int tid = at::get_thread_num();
TORCH_CHECK(tid < num_threads, "expect thread id smaller than ", num_threads, ", got thread id ", tid);
auto buffer_ptr = buffer_data + tid * N;
for (const auto i : c10::irange(begin, end)) {
sum_norm_per_row(buffer_ptr, v_data + i * N, N);
}
});
for (const auto j : c10::irange(N)) {
accscalar_t sum = 0;
for (const auto t : c10::irange(num_threads)) {
sum += buffer_data[t * N + j];
}
norm_data[j] = std::sqrt(sum);
}
// reuse the first row of buffer to store g / norm
vec::convert(g_data, buffer_data, N);
using Vec = vec::Vectorized<accscalar_t>;
vec::map2(
[](Vec g, Vec norm) { return g / norm; },
buffer_data,
buffer_data,
norm_data,
N);
// apply w = v * (g/norm)
at::parallel_for(0, M, 1, [&](int64_t begin, int64_t end) {
for (const auto i : c10::irange(begin, end)) {
apply_norm_per_row(w_data + i * N, v_data + i * N, buffer_data, N);
}
});
}
template <typename scalar_t, typename accscalar_t>
void weight_norm_backward_first_dim_kernel(
TensorBase& grad_v,
TensorBase& grad_g,
const TensorBase& grad_w,
const TensorBase& saved_v,
const TensorBase& saved_g,
const TensorBase& saved_norm,
int64_t M, int64_t N) {
const auto grad_w_data = grad_w.data_ptr<scalar_t>();
const auto saved_v_data = saved_v.data_ptr<scalar_t>();
const auto saved_g_data = saved_g.data_ptr<scalar_t>();
const auto saved_norm_data = saved_norm.data_ptr<accscalar_t>();
auto grad_v_data = grad_v.data_ptr<scalar_t>();
auto grad_g_data = grad_g.data_ptr<scalar_t>();
using Vec = vec::Vectorized<accscalar_t>;
at::parallel_for(0, M, 1, [&](int64_t begin, int64_t end) {
for (const auto i : c10::irange(begin, end)) {
accscalar_t per_dim_sum_val = vec::map2_reduce_all<scalar_t>(
[](Vec grad_w, Vec saved_v) { return grad_w * saved_v; },
[](Vec x, Vec y) { return x + y; },
grad_w_data + i * N,
saved_v_data + i * N,
N);
accscalar_t saved_norm_val = saved_norm_data[i];
accscalar_t saved_g_val = accscalar_t(saved_g_data[i]);
accscalar_t grad_g_val = per_dim_sum_val / saved_norm_val;
// grad_g = sum / norm
// grad_v = (g / norm) * (grad_w - v * (sum / norm^2))
// let a = g /norm
// b = a * grad_g / norm
// grad_v = a * grad_w - b * v
grad_g_data[i] = scalar_t(grad_g_val);
accscalar_t a = saved_g_val / saved_norm_val;
accscalar_t b = a * grad_g_val / saved_norm_val;
vec::map2(
[a, b](Vec grad_w, Vec v) { return Vec(a) * grad_w - Vec(b) * v; },
grad_v_data + i * N,
grad_w_data + i * N,
saved_v_data + i * N,
N);
}
});
}
template <typename scalar_t>
inline void sum_product_per_row(
scalar_t* out_ptr,
const scalar_t* grad_w_ptr,
const scalar_t* v_ptr,
int64_t size) {
using Vec = vec::Vectorized<scalar_t>;
vec::map3(
[](Vec out, Vec grad_w, Vec v) { return out + grad_w * v; },
out_ptr,
out_ptr,
grad_w_ptr,
v_ptr,
size);
}
inline void sum_product_per_row(
float* out_ptr,
const BFloat16* grad_w_ptr,
const BFloat16* v_ptr,
int64_t size) {
using bVec = vec::Vectorized<BFloat16>;
using fVec = vec::Vectorized<float>;
int64_t d = 0;
for (; d < size - (size % bVec::size()); d += bVec::size()) {
bVec grad_w_bvec = bVec::loadu(grad_w_ptr + d);
fVec grad_w_fvec0, grad_w_fvec1;
std::tie(grad_w_fvec0, grad_w_fvec1) = convert_bfloat16_float(grad_w_bvec);
bVec v_bvec = bVec::loadu(v_ptr + d);
fVec v_fvec0, v_fvec1;
std::tie(v_fvec0, v_fvec1) = convert_bfloat16_float(v_bvec);
fVec out_fvec0 = fVec::loadu(out_ptr + d) + grad_w_fvec0 * v_fvec0;
fVec out_fvec1 = fVec::loadu(out_ptr + d + fVec::size()) + grad_w_fvec1 * v_fvec1;
out_fvec0.store(out_ptr + d);
out_fvec1.store(out_ptr + d + fVec::size());
}
for(; d < size; ++d) {
float grad_w_val = float(grad_w_ptr[d]);
float v_val = float(v_ptr[d]);
out_ptr[d] += grad_w_val * v_val;
}
}
template <typename scalar_t>
inline void apply_per_row_backward(
scalar_t* grad_v_ptr,
const scalar_t* grad_w_ptr,
const scalar_t* v_ptr,
const scalar_t* a_ptr,
const scalar_t* b_ptr,
int64_t size) {
using Vec = vec::Vectorized<scalar_t>;
vec::map4(
[](Vec grad_w, Vec v, Vec a, Vec b) { return a * grad_w - b * v; },
grad_v_ptr,
grad_w_ptr,
v_ptr,
a_ptr,
b_ptr,
size);
}
inline void apply_per_row_backward(
BFloat16* grad_v_ptr,
const BFloat16* grad_w_ptr,
const BFloat16* v_ptr,
const float* a_ptr,
const float* b_ptr,
int64_t size) {
using bVec = vec::Vectorized<BFloat16>;
using fVec = vec::Vectorized<float>;
int64_t d = 0;
for (; d < size - (size % bVec::size()); d += bVec::size()) {
bVec grad_w_bvec = bVec::loadu(grad_w_ptr + d);
fVec grad_w_fvec0, grad_w_fvec1;
std::tie(grad_w_fvec0, grad_w_fvec1) = convert_bfloat16_float(grad_w_bvec);
bVec v_bvec = bVec::loadu(v_ptr + d);
fVec v_fvec0, v_fvec1;
std::tie(v_fvec0, v_fvec1) = convert_bfloat16_float(v_bvec);
fVec grad_v_fvec0 = fVec::loadu(a_ptr + d) * grad_w_fvec0 - fVec::loadu(b_ptr + d) * v_fvec0;
fVec grad_v_fvec1 = fVec::loadu(a_ptr + d + fVec::size()) * grad_w_fvec1
- fVec::loadu(b_ptr + d + fVec::size()) * v_fvec1;
bVec grad_v_bvec = convert_float_bfloat16(grad_v_fvec0, grad_v_fvec1);
grad_v_bvec.store(grad_v_ptr + d);
}
for(; d < size; ++d) {
grad_v_ptr[d] = float(grad_w_ptr[d]) * a_ptr[d] - float(v_ptr[d]) * b_ptr[d];
}
}
template <typename scalar_t, typename accscalar_t>
void weight_norm_backward_last_dim_kernel(
TensorBase& grad_v,
TensorBase& grad_g,
const TensorBase& grad_w,
const TensorBase& saved_v,
const TensorBase& saved_g,
const TensorBase& saved_norm,
int64_t M, int64_t N) {
const auto grad_w_data = grad_w.data_ptr<scalar_t>();
const auto saved_v_data = saved_v.data_ptr<scalar_t>();
const auto saved_g_data = saved_g.data_ptr<scalar_t>();
const auto saved_norm_data = saved_norm.data_ptr<accscalar_t>();
auto grad_v_data = grad_v.data_ptr<scalar_t>();
auto grad_g_data = grad_g.data_ptr<scalar_t>();
// the temp buffer will be used twice:
// 1. vertical reduction from [M, N] to [T, N]
// 2. store the intermediate data of `sum`, `a` and `b`,
// so need to make sure it has at least 3 rows
//
int num_threads = at::get_num_threads();
int K = std::max(3, num_threads);
TensorBase buffer = at::detail::empty_cpu({K, N}, saved_norm.options()).zero_();
auto buffer_data = buffer.data_ptr<accscalar_t>();
// vertical parallel reduction
at::parallel_for(0, M, 1, [&](int64_t begin, int64_t end) {
int tid = at::get_thread_num();
TORCH_CHECK(tid < num_threads, "expect thread id smaller than ", num_threads, ", got thread id ", tid);
auto buffer_ptr = buffer_data + tid * N;
for (const auto i : c10::irange(begin, end)) {
sum_product_per_row(buffer_ptr, grad_w_data + i * N, saved_v_data + i * N, N);
}
});
// store result on the first row of buffer
for (const auto j : c10::irange(N)) {
accscalar_t sum = 0;
for (const auto t : c10::irange(num_threads)) {
sum += buffer_data[t * N + j];
}
buffer_data[j] = sum;
}
// reuse the 1st row of buffer to store the sum
// 2nd row to store coefficient a
// 3rd row to store coefficient b
accscalar_t* per_dim_sum = buffer_data;
accscalar_t* a = buffer_data + N;
accscalar_t* b = buffer_data + 2 * N;
// a = g /norm
// b = a * grad_g / norm
for (const auto j : c10::irange(N)) {
accscalar_t saved_norm_val = saved_norm_data[j];
accscalar_t saved_g_val = accscalar_t(saved_g_data[j]);
accscalar_t grad_g_val = per_dim_sum[j] / saved_norm_val;
grad_g_data[j] = scalar_t(grad_g_val);
a[j] = saved_g_val / saved_norm_val;
b[j] = a[j] * grad_g_val / saved_norm_val;
}
// apply grad_v = a * grad_w - b * v
at::parallel_for(0, M, 1, [&](int64_t begin, int64_t end) {
for (const auto i : c10::irange(begin, end)) {
apply_per_row_backward(
grad_v_data + i * N,
grad_w_data + i * N,
saved_v_data + i * N,
a,
b,
N);
}
});
}
void weight_norm_kernel(
TensorBase& w,
TensorBase& norm,
const TensorBase& v,
const TensorBase& g,
int64_t dim) {
TORCH_INTERNAL_ASSERT(dim == 0 || dim == v.dim() - 1,
"fused kernels can only be applied for first or last dim");
AT_DISPATCH_FLOATING_TYPES_AND(ScalarType::BFloat16, v.scalar_type(),
"weight_norm_kernel", [&]() {
using accscalar_t = at::opmath_type<scalar_t>;
if (dim == 0) {
int64_t M = v.size(0);
int64_t N = v.numel() / M;
weight_norm_first_dim_kernel<scalar_t, accscalar_t>(w, norm, v, g, M, N);
} else {
int64_t N = v.size(-1);
int64_t M = v.numel() / N;
weight_norm_last_dim_kernel<scalar_t, accscalar_t>(w, norm, v, g, M, N);
}
});
}
void weight_norm_backward_kernel(
TensorBase& grad_v,
TensorBase& grad_g,
const TensorBase& grad_w,
const TensorBase& saved_v,
const TensorBase& saved_g,
const TensorBase& saved_norm,
int64_t dim) {
TORCH_INTERNAL_ASSERT(dim == 0 || dim == saved_v.dim() - 1,
"fused kernels can only be applied for first or last dim");
AT_DISPATCH_FLOATING_TYPES_AND(ScalarType::BFloat16, saved_v.scalar_type(),
"weight_norm_backward_kernel", [&]() {
using accscalar_t = at::opmath_type<scalar_t>;
if (dim == 0) {
int64_t M = saved_v.size(0);
int64_t N = saved_v.numel() / M;
weight_norm_backward_first_dim_kernel<scalar_t, accscalar_t>(grad_v, grad_g, grad_w, saved_v, saved_g, saved_norm, M, N);
} else {
int64_t N = saved_v.size(-1);
int64_t M = saved_v.numel() / N;
weight_norm_backward_last_dim_kernel<scalar_t, accscalar_t>(grad_v, grad_g, grad_w, saved_v, saved_g, saved_norm, M, N);
}
});
}
} // anonymous namespace
REGISTER_DISPATCH(weight_norm_stub, &weight_norm_kernel);
REGISTER_DISPATCH(weight_norm_backward_stub, &weight_norm_backward_kernel);
} // at::native