-
Notifications
You must be signed in to change notification settings - Fork 182
/
block_all_reduce.cu
813 lines (753 loc) · 37.4 KB
/
block_all_reduce.cu
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <vector>
#include <algorithm>
#include <cuda_runtime.h>
#include <cuda_fp16.h>
#include <cuda_bf16.h>
#include <cuda_fp8.h>
#include <torch/types.h>
#include <torch/extension.h>
#define WARP_SIZE 32
#define INT4(value) (reinterpret_cast<int4*>(&(value))[0])
#define FLOAT4(value) (reinterpret_cast<float4*>(&(value))[0])
#define HALF2(value) (reinterpret_cast<half2*>(&(value))[0])
#define BFLOAT2(value) (reinterpret_cast<__nv_bfloat162*>(&(value))[0])
#define LDST128BITS(value) (reinterpret_cast<float4*>(&(value))[0])
// FP16/BF16 CUDA Cores/Tensor Cores:
// https://resources.nvidia.com/en-us-tensor-core
// Non MatMul FP16/BF16 -> CUDA Cores
// MatMul FP16/BF16 -> Tensor Cores
// Non MatMul FP8 -> Not supported
// MatMul FP8 -> Tensor Cores
// CUDA温故(0x00): 一步步学习block all reduce: 从FP32到FP16/BF16,再到FP8
// -------------------------------------- FP32 --------------------------------------
// Warp Reduce Sum
template<const int kWarpSize = WARP_SIZE>
__device__ __forceinline__ float warp_reduce_sum_f32(float val) {
#pragma unroll
for (int mask = kWarpSize >> 1; mask >= 1; mask >>= 1) {
val += __shfl_xor_sync(0xffffffff, val, mask);
}
return val;
}
// Block All Reduce Sum
// grid(N/256), block(256)
// a: Nx1, y=sum(a)
template<const int NUM_THREADS = 256>
__global__ void block_all_reduce_sum_f32_f32_kernel(float* a, float* y, int N) {
int tid = threadIdx.x;
int idx = blockIdx.x * NUM_THREADS + tid;
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ float reduce_smem[NUM_WARPS];
// keep the data in register is enougth for warp operaion.
float sum = (idx < N) ? a[idx] : 0.0f;
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
sum = warp_reduce_sum_f32<WARP_SIZE>(sum);
// warp leaders store the data to shared memory.
if (lane == 0) reduce_smem[warp] = sum;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
sum = (lane < NUM_WARPS) ? reduce_smem[lane] : 0.0f;
if (warp == 0) sum = warp_reduce_sum_f32<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, sum);
}
// Block All Reduce Sum + float4
// grid(N/256), block(256/4)
// a: Nx1, y=sum(a)
template<const int NUM_THREADS = 256/4>
__global__ void block_all_reduce_sum_f32x4_f32_kernel(float* a, float* y, int N) {
int tid = threadIdx.x;
int idx = (blockIdx.x * NUM_THREADS + tid) * 4;
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ float reduce_smem[NUM_WARPS];
float4 reg_a = FLOAT4(a[idx]);
// keep the data in register is enougth for warp operaion.
float sum = (idx < N) ? (reg_a.x + reg_a.y + reg_a.z + reg_a.w) : 0.0f;
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
sum = warp_reduce_sum_f32<WARP_SIZE>(sum);
// warp leaders store the data to shared memory.
if (lane == 0) reduce_smem[warp] = sum;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
sum = (lane < NUM_WARPS) ? reduce_smem[lane] : 0.0f;
if (warp == 0) sum = warp_reduce_sum_f32<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, sum);
}
// -------------------------------------- FP16 --------------------------------------
// Warp Reduce Sum: Half
template<const int kWarpSize = WARP_SIZE>
__device__ __forceinline__ half warp_reduce_sum_f16_f16(half val) {
#pragma unroll
for (int mask = kWarpSize >> 1; mask >= 1; mask >>= 1) {
val = __hadd(val, __shfl_xor_sync(0xffffffff, val, mask));
// val += __shfl_xor_sync(0xffffffff, val, mask);
}
return val;
}
template<const int kWarpSize = WARP_SIZE>
__device__ __forceinline__ float warp_reduce_sum_f16_f32(half val) {
float val_f32 = __half2float(val);
#pragma unroll
for (int mask = kWarpSize >> 1; mask >= 1; mask >>= 1) {
val_f32 += __shfl_xor_sync(0xffffffff, val_f32, mask);
}
return val_f32;
}
// Block All Reduce Sum: Half
// grid(N/256), block(256)
// a: Nx1, y=sum(a)
template<const int NUM_THREADS = 256>
__global__ void block_all_reduce_sum_f16_f16_kernel(half* a, float* y, int N) {
int tid = threadIdx.x;
int idx = blockIdx.x * NUM_THREADS + tid;
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ float reduce_smem[NUM_WARPS];
// keep the data in register is enougth for warp operaion.
half sum_f16 = (idx < N) ? a[idx] : __float2half(0.0f);
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
sum_f16 = warp_reduce_sum_f16_f16<WARP_SIZE>(sum_f16);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp32 inter warps.
if (lane == 0) reduce_smem[warp] = __half2float(sum_f16);
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
float sum = (lane < NUM_WARPS) ? reduce_smem[lane] : 0.0f;
if (warp == 0) sum = warp_reduce_sum_f32<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, sum);
}
template<const int NUM_THREADS = 256>
__global__ void block_all_reduce_sum_f16_f32_kernel(half* a, float* y, int N) {
int tid = threadIdx.x;
int idx = blockIdx.x * NUM_THREADS + tid;
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ float reduce_smem[NUM_WARPS];
// keep the data in register is enougth for warp operaion.
half sum_f16 = (idx < N) ? a[idx] : __float2half(0.0f);
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
float sum_f32 = warp_reduce_sum_f16_f32<WARP_SIZE>(sum_f16);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp32 inter warps.
if (lane == 0) reduce_smem[warp] = sum_f32;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
float sum = (lane < NUM_WARPS) ? reduce_smem[lane] : 0.0f;
if (warp == 0) sum = warp_reduce_sum_f32<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, sum);
}
template<const int NUM_THREADS = 256/2>
__global__ void block_all_reduce_sum_f16x2_f32_kernel(half* a, float* y, int N) {
int tid = threadIdx.x;
int idx = (blockIdx.x * NUM_THREADS + tid) * 2; // 2 half elements per thread
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ float reduce_smem[NUM_WARPS];
// keep the data in register is enougth for warp operaion.
half2 reg_a = HALF2(a[idx]);
half sum_f16 = (idx < N) ? __hadd(reg_a.x, reg_a.y) : __float2half(0.0f);
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
float sum_f32 = warp_reduce_sum_f16_f32<WARP_SIZE>(sum_f16);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp32 inter warps.
if (lane == 0) reduce_smem[warp] = sum_f32;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
float sum = (lane < NUM_WARPS) ? reduce_smem[lane] : 0.0f;
if (warp == 0) sum = warp_reduce_sum_f32<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, sum);
}
template<const int NUM_THREADS = 256/2>
__global__ void block_all_reduce_sum_f16x2_f16_kernel(half* a, float* y, int N) {
int tid = threadIdx.x;
int idx = (blockIdx.x * NUM_THREADS + tid) * 2; // 2 half elements per thread
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ float reduce_smem[NUM_WARPS];
// keep the data in register is enougth for warp operaion.
half2 reg_a = HALF2(a[idx]);
half sum_f16 = (idx < N) ? __hadd(reg_a.x, reg_a.y) : __float2half(0.0f);
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
sum_f16 = warp_reduce_sum_f16_f16<WARP_SIZE>(sum_f16);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp32 inter warps.
if (lane == 0) reduce_smem[warp] = __half2float(sum_f16);
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
float sum = (lane < NUM_WARPS) ? reduce_smem[lane] : 0.0f;
if (warp == 0) sum = warp_reduce_sum_f32<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, sum);
}
template<const int NUM_THREADS = 256/8>
__global__ void block_all_reduce_sum_f16x8_pack_f16_kernel(half* a, float* y, int N) {
int tid = threadIdx.x;
int idx = (blockIdx.x * NUM_THREADS + tid) * 8; // 8 half elements per thread
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ float reduce_smem[NUM_WARPS];
// temporary register(memory), .local space in ptx, addressable
half pack_a[8]; // 8x16 bits=128 bits.
// reinterpret as float4 and load 128 bits in 1 memory issue.
LDST128BITS(pack_a[0]) = LDST128BITS(a[idx]); // load 128 bits
const half z = __float2half(0.0f);
half sum_f16 = z;
#pragma unroll
for (int i = 0; i < 8; ++i) {
sum_f16 += (((idx + i ) < N) ? pack_a[i] : z);
}
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
sum_f16 = warp_reduce_sum_f16_f16<WARP_SIZE>(sum_f16);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp32 inter warps.
if (lane == 0) reduce_smem[warp] = __half2float(sum_f16);
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
float sum = (lane < NUM_WARPS) ? reduce_smem[lane] : 0.0f;
if (warp == 0) sum = warp_reduce_sum_f32<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, sum);
}
template<const int NUM_THREADS = 256/8>
__global__ void block_all_reduce_sum_f16x8_pack_f32_kernel(half* a, float* y, int N) {
int tid = threadIdx.x;
int idx = (blockIdx.x * NUM_THREADS + tid) * 8; // 8 half elements per thread
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ float reduce_smem[NUM_WARPS];
// temporary register(memory), .local space in ptx, addressable
half pack_a[8]; // 8x16 bits=128 bits.
// reinterpret as float4 and load 128 bits in 1 memory issue.
LDST128BITS(pack_a[0]) = LDST128BITS(a[idx]); // load 128 bits
float sum_f32 = 0.0f;
#pragma unroll
for (int i = 0; i < 8; ++i) {
sum_f32 += (((idx + i ) < N) ? __half2float(pack_a[i]) : 0.0f);
}
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
sum_f32 = warp_reduce_sum_f32<WARP_SIZE>(sum_f32);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp32 inter warps.
if (lane == 0) reduce_smem[warp] = sum_f32;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
float sum = (lane < NUM_WARPS) ? reduce_smem[lane] : 0.0f;
if (warp == 0) sum = warp_reduce_sum_f32<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, sum);
}
// -------------------------------------- BF16 --------------------------------------
// Warp Reduce Sum: Half
template<const int kWarpSize = WARP_SIZE>
__device__ __forceinline__ __nv_bfloat16 warp_reduce_sum_bf16_bf16(
__nv_bfloat16 val) {
#pragma unroll
for (int mask = kWarpSize >> 1; mask >= 1; mask >>= 1) {
val = __hadd(val, __shfl_xor_sync(0xffffffff, val, mask));
}
return val;
}
template<const int kWarpSize = WARP_SIZE>
__device__ __forceinline__ float warp_reduce_sum_bf16_f32(
__nv_bfloat16 val) {
float val_f32 = __bfloat162float(val);
#pragma unroll
for (int mask = kWarpSize >> 1; mask >= 1; mask >>= 1) {
val_f32 += __shfl_xor_sync(0xffffffff, val_f32, mask);
}
return val_f32;
}
// Block All Reduce Sum: BF16
// grid(N/256), block(256)
// a: Nx1, y=sum(a)
template<const int NUM_THREADS = 256>
__global__ void block_all_reduce_sum_bf16_bf16_kernel(
__nv_bfloat16* a, float* y, int N) {
int tid = threadIdx.x;
int idx = blockIdx.x * NUM_THREADS + tid;
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ __nv_bfloat16 reduce_smem[NUM_WARPS];
// keep the data in register is enougth for warp operaion.
__nv_bfloat16 sum_bf16 = (idx < N) ? a[idx] : __float2bfloat16(0.0f);
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
sum_bf16 = warp_reduce_sum_bf16_bf16<WARP_SIZE>(sum_bf16);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp32 inter warps.
if (lane == 0) reduce_smem[warp] = sum_bf16;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
__nv_bfloat16 sum = (lane < NUM_WARPS) ? reduce_smem[lane] : __float2bfloat16(0.0f);
if (warp == 0) sum = warp_reduce_sum_bf16_bf16<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, __bfloat162float(sum));
}
template<const int NUM_THREADS = 256>
__global__ void block_all_reduce_sum_bf16_f32_kernel(
__nv_bfloat16* a, float* y, int N) {
int tid = threadIdx.x;
int idx = blockIdx.x * NUM_THREADS + tid;
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ float reduce_smem[NUM_WARPS];
// keep the data in register is enougth for warp operaion.
__nv_bfloat16 sum_bf16 = (idx < N) ? a[idx] : __float2bfloat16(0.0f);
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
float sum_f32 = warp_reduce_sum_bf16_f32<WARP_SIZE>(sum_bf16);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp32 inter warps.
if (lane == 0) reduce_smem[warp] = sum_f32;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
float sum = (lane < NUM_WARPS) ? reduce_smem[lane] : 0.0f;
if (warp == 0) sum = warp_reduce_sum_f32<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, sum);
}
template<const int NUM_THREADS = 256/2>
__global__ void block_all_reduce_sum_bf16x2_bf16_kernel(
__nv_bfloat16* a, float* y, int N) {
int tid = threadIdx.x;
int idx = (blockIdx.x * NUM_THREADS + tid) * 2; // 2 bf16 elements per thread
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ __nv_bfloat16 reduce_smem[NUM_WARPS];
// keep the data in register is enougth for warp operaion.
__nv_bfloat162 reg_a = BFLOAT2(a[idx]);
__nv_bfloat16 sum_bf16 = (idx < N) ? __hadd(reg_a.x, reg_a.y) : __float2bfloat16(0.0f);
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
sum_bf16 = warp_reduce_sum_bf16_bf16<WARP_SIZE>(sum_bf16);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp32 inter warps.
if (lane == 0) reduce_smem[warp] = sum_bf16;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
__nv_bfloat16 sum = (lane < NUM_WARPS) ? reduce_smem[lane] : __float2bfloat16(0.0f);
if (warp == 0) sum = warp_reduce_sum_bf16_bf16<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, __bfloat162float(sum));
}
template<const int NUM_THREADS = 256/2>
__global__ void block_all_reduce_sum_bf16x2_f32_kernel(
__nv_bfloat16* a, float* y, int N) {
int tid = threadIdx.x;
int idx = (blockIdx.x * NUM_THREADS + tid) * 2; // 2 bf16 elements per thread
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ float reduce_smem[NUM_WARPS];
// keep the data in register is enougth for warp operaion.
__nv_bfloat162 reg_a = BFLOAT2(a[idx]);
__nv_bfloat16 sum_bf16 = (idx < N) ? __hadd(reg_a.x, reg_a.y) : __float2bfloat16(0.0f);
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
float sum_f32 = warp_reduce_sum_bf16_f32<WARP_SIZE>(sum_bf16);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp32 inter warps.
if (lane == 0) reduce_smem[warp] = sum_f32;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
float sum = (lane < NUM_WARPS) ? reduce_smem[lane] : 0.0f;
if (warp == 0) sum = warp_reduce_sum_f32<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, sum);
}
template<const int NUM_THREADS = 256/8>
__global__ void block_all_reduce_sum_bf16x8_pack_bf16_kernel(
__nv_bfloat16* a, float* y, int N) {
int tid = threadIdx.x;
int idx = (blockIdx.x * NUM_THREADS + tid) * 8; // 8 bf16 elements per thread
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ __nv_bfloat16 reduce_smem[NUM_WARPS];
// temporary register(memory), .local space in ptx, addressable
__nv_bfloat16 pack_a[8]; // 8x16 bits=128 bits.
// reinterpret as float4 and load 128 bits in 1 memory issue.
LDST128BITS(pack_a[0]) = LDST128BITS(a[idx]); // load 128 bits
const __nv_bfloat16 z = __float2bfloat16(0.0f);
__nv_bfloat16 sum_bf16 = z;
#pragma unroll
for (int i = 0; i < 8; ++i) {
sum_bf16 += (((idx + i ) < N) ? pack_a[i] : z);
}
// keep the data in register is enougth for warp operaion.
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
sum_bf16 = warp_reduce_sum_bf16_bf16<WARP_SIZE>(sum_bf16);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp32 inter warps.
if (lane == 0) reduce_smem[warp] = sum_bf16;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
__nv_bfloat16 sum = (lane < NUM_WARPS) ? reduce_smem[lane] : z;
if (warp == 0) sum = warp_reduce_sum_bf16_bf16<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, __bfloat162float(sum));
}
template<const int NUM_THREADS = 256/8>
__global__ void block_all_reduce_sum_bf16x8_pack_f32_kernel(
__nv_bfloat16* a, float* y, int N) {
int tid = threadIdx.x;
int idx = (blockIdx.x * NUM_THREADS + tid) * 8; // 8 bf16 elements per thread
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ float reduce_smem[NUM_WARPS];
// temporary register(memory), .local space in ptx, addressable
__nv_bfloat16 pack_a[8]; // 8x16 bits=128 bits.
// reinterpret as float4 and load 128 bits in 1 memory issue.
LDST128BITS(pack_a[0]) = LDST128BITS(a[idx]); // load 128 bits
const __nv_bfloat16 z = __float2bfloat16(0.0f);
__nv_bfloat16 sum_bf16 = z;
#pragma unroll
for (int i = 0; i < 8; ++i) {
sum_bf16 += (((idx + i ) < N) ? pack_a[i] : z);
}
// keep the data in register is enougth for warp operaion.
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
float sum_f32 = warp_reduce_sum_bf16_f32<WARP_SIZE>(sum_bf16);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp32 inter warps.
if (lane == 0) reduce_smem[warp] = sum_f32;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
float sum = (lane < NUM_WARPS) ? reduce_smem[lane] : 0.0f;
if (warp == 0) sum = warp_reduce_sum_f32<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, sum);
}
// -------------------------------------- FP8 --------------------------------------
template<const int kWarpSize = WARP_SIZE>
__device__ __forceinline__ half warp_reduce_sum_fp8_e4m3_f16(
__nv_fp8_storage_t val) {
// typedef unsigned char __nv_fp8_storage_t;
// __half &operator=(const __half_raw &hr);
half val_f16 = __nv_cvt_fp8_to_halfraw(val, __NV_E4M3);
#pragma unroll
for (int mask = kWarpSize >> 1; mask >= 1; mask >>= 1) {
val_f16 = __hadd(val_f16, __shfl_xor_sync(0xffffffff, val_f16, mask));
}
return val_f16;
}
template<const int kWarpSize = WARP_SIZE>
__device__ __forceinline__ half warp_reduce_sum_fp8_e5m2_f16(
__nv_fp8_storage_t val) {
// typedef unsigned char __nv_fp8_storage_t;
// __half &operator=(const __half_raw &hr);
half val_f16 = __nv_cvt_fp8_to_halfraw(val, __NV_E5M2);
#pragma unroll
for (int mask = kWarpSize >> 1; mask >= 1; mask >>= 1) {
val_f16 = __hadd(val_f16, __shfl_xor_sync(0xffffffff, val_f16, mask));
}
return val_f16;
}
template<const int NUM_THREADS = 256>
__global__ void block_all_reduce_sum_fp8_e4m3_f16_kernel(
__nv_fp8_storage_t* a, float* y, int N) {
int tid = threadIdx.x;
int idx = blockIdx.x * NUM_THREADS + tid;
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ half reduce_smem[NUM_WARPS];
// keep the data in register is enougth for warp operaion.
__nv_fp8_storage_t sum_f8 = (idx < N) ? a[idx] : __nv_cvt_float_to_fp8(
0.0f, __NV_SATFINITE, __NV_E4M3);
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
half sum_f16 = warp_reduce_sum_fp8_e4m3_f16<WARP_SIZE>(sum_f8);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp16 inter warps.
if (lane == 0) reduce_smem[warp] = sum_f16;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
half sum = (lane < NUM_WARPS) ? reduce_smem[lane] : __float2half(0.0f);
if (warp == 0) sum = warp_reduce_sum_f16_f16<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, __half2float(sum));
}
template<const int NUM_THREADS = 256>
__global__ void block_all_reduce_sum_fp8_e5m2_f16_kernel(
__nv_fp8_storage_t* a, float* y, int N) {
int tid = threadIdx.x;
int idx = blockIdx.x * NUM_THREADS + tid;
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ half reduce_smem[NUM_WARPS];
// keep the data in register is enougth for warp operaion.
__nv_fp8_storage_t sum_f8 = (idx < N) ? a[idx] : __nv_cvt_float_to_fp8(
0.0f, __NV_SATFINITE, __NV_E5M2);
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
half sum_f16 = warp_reduce_sum_fp8_e5m2_f16<WARP_SIZE>(sum_f8);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp16 inter warps.
if (lane == 0) reduce_smem[warp] = sum_f16;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
half sum = (lane < NUM_WARPS) ? reduce_smem[lane] : __float2half(0.0f);
if (warp == 0) sum = warp_reduce_sum_f16_f16<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, __half2float(sum));
}
template<const int NUM_THREADS = 256/16>
__global__ void block_all_reduce_sum_fp8_e4m3x16_pack_f16_kernel(
__nv_fp8_storage_t* a, float* y, int N) {
int tid = threadIdx.x;
int idx = (blockIdx.x * NUM_THREADS + tid) * 16;
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ half reduce_smem[NUM_WARPS];
__nv_fp8_storage_t pack_a[16]; // 16x8 bits=128 bits.
// reinterpret as float4 and load 128 bits in 1 memory issue.
LDST128BITS(pack_a[0]) = LDST128BITS(a[idx]); // load 128 bits
half sum_f16 = __float2half(0.0f);
#pragma unroll
for (int i = 0; i < 16; ++i) {
sum_f16 += __nv_cvt_fp8_to_halfraw(pack_a[i], __NV_E4M3);
}
// keep the data in register is enougth for warp operaion.
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
sum_f16 = warp_reduce_sum_f16_f16<WARP_SIZE>(sum_f16);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp16 inter warps.
if (lane == 0) reduce_smem[warp] = sum_f16;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
half sum = (lane < NUM_WARPS) ? reduce_smem[lane] : __float2half(0.0f);
if (warp == 0) sum = warp_reduce_sum_f16_f16<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, __half2float(sum));
}
template<const int NUM_THREADS = 256/16>
__global__ void block_all_reduce_sum_fp8_e5m2x16_pack_f16_kernel(
__nv_fp8_storage_t* a, float* y, int N) {
int tid = threadIdx.x;
int idx = (blockIdx.x * NUM_THREADS + tid) * 16;
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ half reduce_smem[NUM_WARPS];
__nv_fp8_storage_t pack_a[16]; // 16x8 bits=128 bits.
// reinterpret as float4 and load 128 bits in 1 memory issue.
LDST128BITS(pack_a[0]) = LDST128BITS(a[idx]); // load 128 bits
half sum_f16 = __float2half(0.0f);
#pragma unroll
for (int i = 0; i < 16; ++i) {
sum_f16 += __nv_cvt_fp8_to_halfraw(pack_a[i], __NV_E5M2);
}
// keep the data in register is enougth for warp operaion.
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
sum_f16 = warp_reduce_sum_f16_f16<WARP_SIZE>(sum_f16);
// warp leaders store the data to shared memory.
// use float to keep sum from each block and reduce
// with fp16 inter warps.
if (lane == 0) reduce_smem[warp] = sum_f16;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
half sum = (lane < NUM_WARPS) ? reduce_smem[lane] : __float2half(0.0f);
if (warp == 0) sum = warp_reduce_sum_f16_f16<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, __half2float(sum));
}
// -------------------------------------- INT8 --------------------------------------
template<const int kWarpSize = WARP_SIZE>
__device__ __forceinline__ int32_t warp_reduce_sum_i8_i32(int8_t val) {
int32_t val_i32 = static_cast<int32_t>(val);
#pragma unroll
for (int mask = kWarpSize >> 1; mask >= 1; mask >>= 1) {
val_i32 += __shfl_xor_sync(0xffffffff, val_i32, mask);
}
return val_i32;
}
template<const int kWarpSize = WARP_SIZE>
__device__ __forceinline__ int32_t warp_reduce_sum_i32_i32(int32_t val) {
#pragma unroll
for (int mask = kWarpSize >> 1; mask >= 1; mask >>= 1) {
val += __shfl_xor_sync(0xffffffff, val, mask);
}
return val;
}
template<const int NUM_THREADS = 256>
__global__ void block_all_reduce_sum_i8_i32_kernel(
int8_t* a, int32_t* y, int N) {
int tid = threadIdx.x;
int idx = blockIdx.x * NUM_THREADS + tid;
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ int32_t reduce_smem[NUM_WARPS];
// keep the data in register is enougth for warp operaion.
int8_t sum_i8 = (idx < N) ? a[idx] : 0;
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
int32_t sum_i32 = warp_reduce_sum_i8_i32<WARP_SIZE>(sum_i8);
if (lane == 0) reduce_smem[warp] = sum_i32;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
int32_t sum = (lane < NUM_WARPS) ? reduce_smem[lane] : 0;
if (warp == 0) sum = warp_reduce_sum_i32_i32<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, sum);
}
template<const int NUM_THREADS = 256/16>
__global__ void block_all_reduce_sum_i8x16_pack_i32_kernel(
int8_t* a, int32_t* y, int N) {
int tid = threadIdx.x;
int idx = (blockIdx.x * NUM_THREADS + tid) * 16;
constexpr int NUM_WARPS = (NUM_THREADS + WARP_SIZE - 1) / WARP_SIZE;
__shared__ int32_t reduce_smem[NUM_WARPS];
int8_t pack_a[16]; // 16x8=128 bits
// reinterpret as float4 and load 128 bits in 1 memory issue.
LDST128BITS(pack_a[0]) = LDST128BITS(a[idx]); // load 128 bits
// keep the data in register is enougth for warp operaion.
int32_t sum_i32 = 0;
#pragma unroll
for (int i = 0; i < 16; ++i) {
sum_i32 += (static_cast<int32_t>(pack_a[i]));
}
int warp = tid / WARP_SIZE;
int lane = tid % WARP_SIZE;
// perform warp sync reduce.
sum_i32 = warp_reduce_sum_i32_i32<WARP_SIZE>(sum_i32);
if (lane == 0) reduce_smem[warp] = sum_i32;
__syncthreads(); // make sure the data is in shared memory.
// the first warp compute the final sum.
int32_t sum = (lane < NUM_WARPS) ? reduce_smem[lane] : 0;
if (warp == 0) sum = warp_reduce_sum_i32_i32<NUM_WARPS>(sum);
if (tid == 0) atomicAdd(y, sum);
}
// --------------------- PyTorch bindings for custom kernel -----------------------
#define STRINGFY(str) #str
#define TORCH_BINDING_COMMON_EXTENSION(func) \
m.def(STRINGFY(func), &func, STRINGFY(func));
#define CHECK_TORCH_TENSOR_DTYPE(T, th_type) \
if(((T).options().dtype() != (th_type))) { \
std::cout << "Tensor Info:" << (T).options() << std::endl; \
throw std::runtime_error("values must be "#th_type); \
}
#define LANUCH_REDUCE_KERNEL(NT, packed_type, acc_type, element_type, out_type) \
block_all_reduce_sum_##packed_type##_##acc_type##_kernel<(NT)><<<grid, block>>>( \
reinterpret_cast<element_type*>(x.data_ptr()), \
reinterpret_cast<out_type*>(y.data_ptr()), N);
#define DISPATCH_REDUCE_KERNEL(K, packed_type, acc_type, element_type, n_elements, out_type) \
const int NT = (K)/(n_elements); \
dim3 block(NT); \
dim3 grid((S)); \
switch (NT) \
{ \
case 32: \
LANUCH_REDUCE_KERNEL(32, packed_type, acc_type, element_type, out_type) \
break; \
case 64: \
LANUCH_REDUCE_KERNEL(64, packed_type, acc_type, element_type, out_type) \
break; \
case 128: \
LANUCH_REDUCE_KERNEL(128, packed_type, acc_type, element_type, out_type) \
break; \
case 256: \
LANUCH_REDUCE_KERNEL(256, packed_type, acc_type, element_type, out_type) \
break; \
case 512: \
LANUCH_REDUCE_KERNEL(512, packed_type, acc_type, element_type, out_type) \
break; \
case 1024: \
LANUCH_REDUCE_KERNEL(1024, packed_type, acc_type, element_type, out_type) \
break; \
default: \
throw std::runtime_error( \
"only support (K)/(n_elements): 32/64/128/256/512/1024"); \
break; \
}
#define TORCH_BINDING_REDUCE(packed_type, acc_type, th_type, element_type, n_elements, out_type) \
torch::Tensor block_all_reduce_sum_##packed_type##_##acc_type(torch::Tensor x) { \
CHECK_TORCH_TENSOR_DTYPE(x, (th_type)) \
auto y_th_type = (th_type) == torch::kInt8 ? torch::kInt32 : torch::kFloat32; \
auto options = torch::TensorOptions().dtype(y_th_type).device(torch::kCUDA, 0); \
auto y = torch::zeros({1}, options); \
const int ndim = x.dim(); \
if (ndim != 2) { \
int N = 1; \
for (int i = 0; i < ndim; ++i) { N *= x.size(i); } \
dim3 block(1024 / (n_elements)); \
dim3 grid((N + 1024 - 1) / 1024); \
block_all_reduce_sum_##packed_type##_##acc_type##_kernel< \
1024 / (n_elements)><<<grid, block>>>( \
reinterpret_cast<element_type*>(x.data_ptr()), \
reinterpret_cast<out_type*>(y.data_ptr()), N); \
} else { \
const int S = x.size(0); \
const int K = x.size(1); \
const int N = S * K; \
if ((K/(n_elements)) <= 1024) { \
DISPATCH_REDUCE_KERNEL(K, packed_type, acc_type, element_type, n_elements, out_type) \
} else { \
int N = 1; \
for (int i = 0; i < ndim; ++i) { N *= x.size(i); } \
dim3 block(1024 / (n_elements)); \
dim3 grid((N + 1024 - 1) / 1024); \
block_all_reduce_sum_##packed_type##_##acc_type##_kernel< \
1024 / (n_elements)><<<grid, block>>>( \
reinterpret_cast<element_type*>(x.data_ptr()), \
reinterpret_cast<out_type*>(y.data_ptr()), N); \
} \
} \
return y; \
}
// packed_type, acc_type, th_type, element_type, n_elements_per_pack, out_type
TORCH_BINDING_REDUCE(f32, f32, torch::kFloat32, float, 1, float)
TORCH_BINDING_REDUCE(f32x4, f32, torch::kFloat32, float, 4, float)
TORCH_BINDING_REDUCE(f16, f16, torch::kHalf, half, 1, float)
TORCH_BINDING_REDUCE(f16, f32, torch::kHalf, half, 1, float)
TORCH_BINDING_REDUCE(f16x2, f16, torch::kHalf, half, 2, float)
TORCH_BINDING_REDUCE(f16x2, f32, torch::kHalf, half, 2, float)
TORCH_BINDING_REDUCE(f16x8_pack, f16, torch::kHalf, half, 8, float)
TORCH_BINDING_REDUCE(f16x8_pack, f32, torch::kHalf, half, 8, float)
TORCH_BINDING_REDUCE(bf16, bf16, torch::kBFloat16, __nv_bfloat16, 1, float)
TORCH_BINDING_REDUCE(bf16, f32, torch::kBFloat16, __nv_bfloat16, 1, float)
TORCH_BINDING_REDUCE(bf16x2, bf16, torch::kBFloat16, __nv_bfloat16, 2, float)
TORCH_BINDING_REDUCE(bf16x2, f32, torch::kBFloat16, __nv_bfloat16, 2, float)
TORCH_BINDING_REDUCE(bf16x8_pack, bf16, torch::kBFloat16, __nv_bfloat16, 8, float)
TORCH_BINDING_REDUCE(bf16x8_pack, f32, torch::kBFloat16, __nv_bfloat16, 8, float)
TORCH_BINDING_REDUCE(fp8_e4m3, f16, torch::kFloat8_e4m3fn, __nv_fp8_storage_t, 1, float)
TORCH_BINDING_REDUCE(fp8_e4m3x16_pack, f16, torch::kFloat8_e4m3fn, __nv_fp8_storage_t, 16, float)
TORCH_BINDING_REDUCE(fp8_e5m2, f16, torch::kFloat8_e5m2, __nv_fp8_storage_t, 1, float)
TORCH_BINDING_REDUCE(fp8_e5m2x16_pack, f16, torch::kFloat8_e5m2, __nv_fp8_storage_t, 16, float)
TORCH_BINDING_REDUCE(i8, i32, torch::kInt8, int8_t, 1, int32_t)
TORCH_BINDING_REDUCE(i8x16_pack, i32, torch::kInt8, int8_t, 16, int32_t)
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_f32_f32)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_f32x4_f32)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_f16_f16)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_f16_f32)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_f16x2_f16)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_f16x2_f32)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_f16x8_pack_f16)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_f16x8_pack_f32)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_bf16_bf16)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_bf16_f32)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_bf16x2_bf16)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_bf16x2_f32)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_bf16x8_pack_bf16)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_bf16x8_pack_f32)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_fp8_e4m3_f16)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_fp8_e4m3x16_pack_f16)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_fp8_e5m2_f16)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_fp8_e5m2x16_pack_f16)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_i8_i32)
TORCH_BINDING_COMMON_EXTENSION(block_all_reduce_sum_i8x16_pack_i32)
}