Repository navigation
Expand file tree
/
Copy pathkernel.cu
More file actions
959 lines (856 loc) · 40.8 KB
/
Copy pathkernel.cu
File metadata and controls
959 lines (856 loc) · 40.8 KB
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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
/**
* Fused single-kernel decode for Qwen3.5-0.8B (hybrid DeltaNet + Full Attention).
* ALL BF16: weights bf16, activations bf16, accumulation f32.
* DeltaNet state: f32 (recurrence needs precision).
*
* Optimized for: NVIDIA RTX 3090 (sm_86, 82 SMs)
* Model: Qwen/Qwen3.5-0.8B (bf16 weights)
*/
#include <cuda_bf16.h>
#include <cuda_runtime.h>
// =============================================================================
// Model constants
// =============================================================================
constexpr int WARP_SIZE = 32;
constexpr int HIDDEN_SIZE = 1024;
constexpr int INTERMEDIATE_SIZE = 3584;
constexpr int NUM_LAYERS = 24;
constexpr float RMS_EPS = 1e-6f;
constexpr int VOCAB_SIZE = 248320;
// Full Attention
constexpr int FA_NUM_Q_HEADS = 8;
constexpr int FA_NUM_KV_HEADS = 2;
constexpr int FA_HEAD_DIM = 256;
constexpr int FA_GQA_RATIO = FA_NUM_Q_HEADS / FA_NUM_KV_HEADS;
constexpr int FA_Q_SIZE = FA_NUM_Q_HEADS * FA_HEAD_DIM;
constexpr int FA_GATE_SIZE = FA_Q_SIZE;
constexpr int FA_QPROJ_SIZE = FA_Q_SIZE + FA_GATE_SIZE;
constexpr int FA_KV_SIZE = FA_NUM_KV_HEADS * FA_HEAD_DIM;
constexpr int FA_ROTARY_DIM = 64;
constexpr float FA_ROPE_THETA = 10000000.0f;
// DeltaNet
constexpr int DN_NUM_HEADS = 16;
constexpr int DN_KEY_DIM = 128;
constexpr int DN_VALUE_DIM = 128;
constexpr int DN_CONV_KERNEL = 4;
constexpr int DN_QK_SIZE = DN_NUM_HEADS * DN_KEY_DIM;
constexpr int DN_V_SIZE = DN_NUM_HEADS * DN_VALUE_DIM;
constexpr int DN_CONV_CHANNELS = DN_QK_SIZE + DN_QK_SIZE + DN_V_SIZE;
constexpr int MAX_ACT_DIM = (HIDDEN_SIZE > INTERMEDIATE_SIZE) ? HIDDEN_SIZE : INTERMEDIATE_SIZE;
#ifndef NUM_BLOCKS
#define NUM_BLOCKS 82
#endif
#ifndef BLOCK_SIZE
#define BLOCK_SIZE 512
#endif
constexpr int NUM_WARPS = BLOCK_SIZE / WARP_SIZE;
#ifndef LM_NUM_BLOCKS
#define LM_NUM_BLOCKS 512
#endif
#ifndef LM_BLOCK_SIZE
#define LM_BLOCK_SIZE 256
#endif
__device__ __constant__ int LAYER_TYPE[NUM_LAYERS] = {
0,0,0,1, 0,0,0,1, 0,0,0,1, 0,0,0,1, 0,0,0,1, 0,0,0,1
};
// =============================================================================
// Weight structs — ALL BF16
// =============================================================================
struct FullAttnWeights {
const __nv_bfloat16 *input_layernorm_weight; // [1024]
const __nv_bfloat16 *q_proj_weight; // [4096, 1024]
const __nv_bfloat16 *k_proj_weight; // [512, 1024]
const __nv_bfloat16 *v_proj_weight; // [512, 1024]
const __nv_bfloat16 *q_norm_weight; // [256]
const __nv_bfloat16 *k_norm_weight; // [256]
const __nv_bfloat16 *o_proj_weight; // [1024, 2048]
const __nv_bfloat16 *post_attn_layernorm_weight;
const __nv_bfloat16 *gate_proj_weight; // [3584, 1024]
const __nv_bfloat16 *up_proj_weight; // [3584, 1024]
const __nv_bfloat16 *down_proj_weight; // [1024, 3584]
};
struct DeltaNetWeights {
const __nv_bfloat16 *input_layernorm_weight;
const __nv_bfloat16 *qkv_proj_weight; // [6144, 1024]
const __nv_bfloat16 *z_proj_weight; // [2048, 1024]
const __nv_bfloat16 *beta_proj_weight; // [16, 1024]
const __nv_bfloat16 *alpha_proj_weight; // [16, 1024]
const __nv_bfloat16 *conv1d_weight; // [6144, 1, 4]
const __nv_bfloat16 *a_log; // [16]
const __nv_bfloat16 *dt_bias; // [16]
const __nv_bfloat16 *norm_weight; // [128]
const __nv_bfloat16 *out_proj_weight; // [1024, 2048]
const __nv_bfloat16 *post_attn_layernorm_weight;
const __nv_bfloat16 *gate_proj_weight;
const __nv_bfloat16 *up_proj_weight;
const __nv_bfloat16 *down_proj_weight;
};
struct LayerWeights {
int layer_type;
int _pad[3];
union {
DeltaNetWeights dn;
FullAttnWeights fa;
};
};
// =============================================================================
// Atomic barrier
// =============================================================================
struct AtomicGridSync {
unsigned int *counter;
unsigned int *generation;
unsigned int nblocks;
unsigned int local_gen;
__device__ void sync() {
__syncthreads();
if (threadIdx.x == 0) {
unsigned int my_gen = local_gen;
asm volatile("fence.acq_rel.gpu;" ::: "memory");
unsigned int arrived = atomicAdd(counter, 1);
if (arrived == nblocks - 1) {
*counter = 0;
asm volatile("fence.acq_rel.gpu;" ::: "memory");
atomicAdd(generation, 1);
} else {
volatile unsigned int *vgen = (volatile unsigned int *)generation;
while (*vgen <= my_gen) {}
}
local_gen = my_gen + 1;
}
__syncthreads();
}
};
// =============================================================================
// Helpers
// =============================================================================
__device__ __forceinline__ float warp_reduce_sum(float val) {
for (int offset = WARP_SIZE / 2; offset > 0; offset /= 2)
val += __shfl_down_sync(0xffffffff, val, offset);
return val;
}
__device__ __forceinline__ float fast_exp(float x) {
float y; asm volatile("ex2.approx.ftz.f32 %0, %1;" : "=f"(y) : "f"(x * 1.44269504088896340736f)); return y;
}
__device__ __forceinline__ float fast_sigmoid(float x) {
float y; asm volatile("rcp.approx.ftz.f32 %0, %1;" : "=f"(y) : "f"(1.0f + fast_exp(-x))); return y;
}
__device__ __forceinline__ float fast_silu(float x) { return x * fast_sigmoid(x); }
__device__ __forceinline__ uint4 load_128bit(const uint4 *ptr) {
uint4 out;
asm volatile("ld.global.L1::no_allocate.v4.b32 {%0, %1, %2, %3}, [%4];"
: "=r"(out.x), "=r"(out.y), "=r"(out.z), "=r"(out.w) : "l"(ptr));
return out;
}
// BF16 dot product: 8 bf16 weights × 8 bf16 activations → f32
__device__ __forceinline__ float dot8_bf16(const uint4 &w_u4, const __nv_bfloat16 *act) {
const __nv_bfloat16 *w = reinterpret_cast<const __nv_bfloat16 *>(&w_u4);
float sum = 0.0f;
#pragma unroll
for (int i = 0; i < 8; i++)
sum += __bfloat162float(w[i]) * __bfloat162float(act[i]);
return sum;
}
// =============================================================================
// RMSNorm — reads bf16 input, writes bf16 output
// =============================================================================
__device__ void rmsnorm_redundant(
const __nv_bfloat16 *__restrict__ input,
const __nv_bfloat16 *__restrict__ weight,
__nv_bfloat16 *__restrict__ s_out, // shared memory bf16
__nv_bfloat16 *__restrict__ g_residual) // global bf16
{
int block_id = blockIdx.x;
int warp_id = threadIdx.x / WARP_SIZE;
int lane_id = threadIdx.x % WARP_SIZE;
__shared__ float smem_reduce[NUM_WARPS];
float local_sum_sq = 0.0f;
for (int i = threadIdx.x; i < HIDDEN_SIZE; i += BLOCK_SIZE) {
float v = __bfloat162float(__ldg(input + i));
s_out[i] = __float2bfloat16(v);
local_sum_sq += v * v;
}
if (block_id == 0) {
for (int i = threadIdx.x; i < HIDDEN_SIZE; i += BLOCK_SIZE)
g_residual[i] = s_out[i];
}
local_sum_sq = warp_reduce_sum(local_sum_sq);
if (lane_id == 0) smem_reduce[warp_id] = local_sum_sq;
__syncthreads();
if (warp_id == 0) {
float sum = (lane_id < NUM_WARPS) ? smem_reduce[lane_id] : 0.0f;
sum = warp_reduce_sum(sum);
if (lane_id == 0)
smem_reduce[0] = rsqrtf(sum / float(HIDDEN_SIZE) + RMS_EPS);
}
__syncthreads();
float rstd = smem_reduce[0];
for (int i = threadIdx.x; i < HIDDEN_SIZE; i += BLOCK_SIZE) {
float w = __bfloat162float(__ldg(weight + i));
float v = __bfloat162float(s_out[i]);
s_out[i] = __float2bfloat16(v * rstd * (1.0f + w));
}
__syncthreads();
}
// RMSNorm from bf16 buffer (for post-attn norm)
__device__ void rmsnorm_from_bf16(
const __nv_bfloat16 *__restrict__ input,
const __nv_bfloat16 *__restrict__ weight,
__nv_bfloat16 *__restrict__ s_out,
__nv_bfloat16 *__restrict__ g_residual)
{
int block_id = blockIdx.x;
int warp_id = threadIdx.x / WARP_SIZE;
int lane_id = threadIdx.x % WARP_SIZE;
__shared__ float smem_reduce[NUM_WARPS];
float local_sum_sq = 0.0f;
for (int i = threadIdx.x; i < HIDDEN_SIZE; i += BLOCK_SIZE) {
float v = __bfloat162float(input[i]);
s_out[i] = __float2bfloat16(v);
local_sum_sq += v * v;
}
if (block_id == 0) {
for (int i = threadIdx.x; i < HIDDEN_SIZE; i += BLOCK_SIZE)
g_residual[i] = s_out[i];
}
local_sum_sq = warp_reduce_sum(local_sum_sq);
if (lane_id == 0) smem_reduce[warp_id] = local_sum_sq;
__syncthreads();
if (warp_id == 0) {
float sum = (lane_id < NUM_WARPS) ? smem_reduce[lane_id] : 0.0f;
sum = warp_reduce_sum(sum);
if (lane_id == 0)
smem_reduce[0] = rsqrtf(sum / float(HIDDEN_SIZE) + RMS_EPS);
}
__syncthreads();
float rstd = smem_reduce[0];
for (int i = threadIdx.x; i < HIDDEN_SIZE; i += BLOCK_SIZE) {
float w = __bfloat162float(__ldg(weight + i));
float v = __bfloat162float(s_out[i]);
s_out[i] = __float2bfloat16(v * rstd * (1.0f + w));
}
__syncthreads();
}
// =============================================================================
// BF16 Matvec: warp-per-row, activations in shared memory (bf16)
// =============================================================================
__device__ void matvec_bf16(
const __nv_bfloat16 *__restrict__ s_input, // shared memory bf16 [in_dim]
const __nv_bfloat16 *__restrict__ weight, // [out_dim, in_dim] bf16
float *__restrict__ output, // [out_dim] f32 (accumulate in f32)
int in_dim, int out_dim, int num_blocks)
{
int block_id = blockIdx.x;
int warp_id = threadIdx.x / WARP_SIZE;
int lane_id = threadIdx.x % WARP_SIZE;
int rows_per_block = (out_dim + num_blocks - 1) / num_blocks;
int row_start = block_id * rows_per_block;
int row_end = min(row_start + rows_per_block, out_dim);
for (int m_base = row_start; m_base < row_end; m_base += NUM_WARPS) {
int m = m_base + warp_id;
if (m < row_end) {
const __nv_bfloat16 *w_row = weight + m * in_dim;
float sum = 0.0f;
#pragma unroll 4
for (int k = lane_id * 8; k < in_dim; k += WARP_SIZE * 8) {
uint4 w_u4 = load_128bit(reinterpret_cast<const uint4 *>(w_row + k));
sum += dot8_bf16(w_u4, s_input + k);
}
sum = warp_reduce_sum(sum);
if (lane_id == 0) output[m] = sum;
}
}
}
// Fused gate+up+SiLU matvec (bf16 weights, bf16 activations)
__device__ void matvec_gate_up_silu_bf16(
const __nv_bfloat16 *__restrict__ s_input,
const __nv_bfloat16 *__restrict__ gate_weight,
const __nv_bfloat16 *__restrict__ up_weight,
float *__restrict__ output,
int in_dim, int out_dim, int num_blocks)
{
int block_id = blockIdx.x;
int warp_id = threadIdx.x / WARP_SIZE;
int lane_id = threadIdx.x % WARP_SIZE;
int rows_per_block = (out_dim + num_blocks - 1) / num_blocks;
int row_start = block_id * rows_per_block;
int row_end = min(row_start + rows_per_block, out_dim);
for (int m_base = row_start; m_base < row_end; m_base += NUM_WARPS) {
int m = m_base + warp_id;
if (m < row_end) {
const __nv_bfloat16 *g_row = gate_weight + m * in_dim;
const __nv_bfloat16 *u_row = up_weight + m * in_dim;
float gate_sum = 0.0f, up_sum = 0.0f;
#pragma unroll 4
for (int k = lane_id * 8; k < in_dim; k += WARP_SIZE * 8) {
uint4 g_u4 = load_128bit(reinterpret_cast<const uint4 *>(g_row + k));
uint4 u_u4 = load_128bit(reinterpret_cast<const uint4 *>(u_row + k));
gate_sum += dot8_bf16(g_u4, s_input + k);
up_sum += dot8_bf16(u_u4, s_input + k);
}
gate_sum = warp_reduce_sum(gate_sum);
up_sum = warp_reduce_sum(up_sum);
if (lane_id == 0)
output[m] = fast_silu(gate_sum) * up_sum;
}
}
}
// Down projection + residual → bf16 hidden
__device__ void matvec_down_residual_bf16(
const float *__restrict__ s_input, // shared [INTER] f32
const __nv_bfloat16 *__restrict__ weight, // [HIDDEN, INTER] bf16
const __nv_bfloat16 *__restrict__ residual, // [HIDDEN] bf16
__nv_bfloat16 *__restrict__ hidden_out, // [HIDDEN] bf16
int in_dim, int out_dim, int num_blocks)
{
// This needs f32 input (MLP intermediate is f32). Convert on the fly.
int block_id = blockIdx.x;
int warp_id = threadIdx.x / WARP_SIZE;
int lane_id = threadIdx.x % WARP_SIZE;
int rows_per_block = (out_dim + num_blocks - 1) / num_blocks;
int row_start = block_id * rows_per_block;
int row_end = min(row_start + rows_per_block, out_dim);
for (int m_base = row_start; m_base < row_end; m_base += NUM_WARPS) {
int m = m_base + warp_id;
if (m < row_end) {
const __nv_bfloat16 *w_row = weight + m * in_dim;
float sum = 0.0f;
// Weight is bf16, input is f32 — convert input to bf16 on the fly
for (int k = lane_id * 8; k < in_dim; k += WARP_SIZE * 8) {
uint4 w_u4 = load_128bit(reinterpret_cast<const uint4 *>(w_row + k));
const __nv_bfloat16 *w = reinterpret_cast<const __nv_bfloat16 *>(&w_u4);
#pragma unroll
for (int i = 0; i < 8; i++)
sum += __bfloat162float(w[i]) * s_input[k + i];
}
sum = warp_reduce_sum(sum);
if (lane_id == 0)
hidden_out[m] = __float2bfloat16(sum + __bfloat162float(residual[m]));
}
}
}
// O projection + residual → bf16
__device__ void matvec_o_residual_bf16(
const float *__restrict__ s_input, // shared [Q_SIZE] f32
const __nv_bfloat16 *__restrict__ weight,
const __nv_bfloat16 *__restrict__ residual,
__nv_bfloat16 *__restrict__ hidden_out,
int in_dim, int out_dim, int num_blocks)
{
int block_id = blockIdx.x;
int warp_id = threadIdx.x / WARP_SIZE;
int lane_id = threadIdx.x % WARP_SIZE;
int rows_per_block = (out_dim + num_blocks - 1) / num_blocks;
int row_start = block_id * rows_per_block;
int row_end = min(row_start + rows_per_block, out_dim);
for (int m_base = row_start; m_base < row_end; m_base += NUM_WARPS) {
int m = m_base + warp_id;
if (m < row_end) {
const __nv_bfloat16 *w_row = weight + m * in_dim;
float sum = 0.0f;
for (int k = lane_id * 8; k < in_dim; k += WARP_SIZE * 8) {
uint4 w_u4 = load_128bit(reinterpret_cast<const uint4 *>(w_row + k));
const __nv_bfloat16 *w = reinterpret_cast<const __nv_bfloat16 *>(&w_u4);
#pragma unroll
for (int i = 0; i < 8; i++)
sum += __bfloat162float(w[i]) * s_input[k + i];
}
sum = warp_reduce_sum(sum);
if (lane_id == 0)
hidden_out[m] = __float2bfloat16(sum + __bfloat162float(residual[m]));
}
}
}
// =============================================================================
// Full Attention layer (bf16)
// =============================================================================
__device__ void full_attention_layer(
AtomicGridSync &grid,
const FullAttnWeights &w,
const __nv_bfloat16 *__restrict__ input,
__nv_bfloat16 *__restrict__ k_cache,
__nv_bfloat16 *__restrict__ v_cache,
__nv_bfloat16 *__restrict__ g_residual, // [HIDDEN] bf16
float *__restrict__ g_activations, // scratch f32
float *__restrict__ g_q, // [FA_QPROJ_SIZE] f32
float *__restrict__ g_kv, // [FA_KV_SIZE*2] f32
float *__restrict__ g_attn_out, // [FA_Q_SIZE] f32
float *__restrict__ g_mlp_inter, // [INTER] f32
__nv_bfloat16 *__restrict__ hidden_out, // [HIDDEN] bf16
int position, int max_seq_len,
__nv_bfloat16 *__restrict__ shmem)
{
int block_id = blockIdx.x;
int num_blocks = gridDim.x;
int warp_id = threadIdx.x / WARP_SIZE;
int lane_id = threadIdx.x % WARP_SIZE;
// Phase 1: RMSNorm + QKV projection
__nv_bfloat16 *s_norm = shmem;
rmsnorm_redundant(input, w.input_layernorm_weight, s_norm, g_residual);
matvec_bf16(s_norm, w.q_proj_weight, g_q, HIDDEN_SIZE, FA_QPROJ_SIZE, num_blocks);
matvec_bf16(s_norm, w.k_proj_weight, g_kv, HIDDEN_SIZE, FA_KV_SIZE, num_blocks);
matvec_bf16(s_norm, w.v_proj_weight, g_kv + FA_KV_SIZE, HIDDEN_SIZE, FA_KV_SIZE, num_blocks);
grid.sync();
// Phase 2: QK norm + partial RoPE + KV cache write
if (block_id == 0) {
float *k_buf = g_kv, *v_buf = g_kv + FA_KV_SIZE;
for (int h = warp_id; h < FA_NUM_KV_HEADS; h += NUM_WARPS) {
float *kh = k_buf + h * FA_HEAD_DIM, *vh = v_buf + h * FA_HEAD_DIM;
__nv_bfloat16 *kc = k_cache + h * max_seq_len * FA_HEAD_DIM + position * FA_HEAD_DIM;
__nv_bfloat16 *vc = v_cache + h * max_seq_len * FA_HEAD_DIM + position * FA_HEAD_DIM;
float ss = 0; for (int i = lane_id; i < FA_HEAD_DIM; i += WARP_SIZE) ss += kh[i]*kh[i];
ss = warp_reduce_sum(ss); float sc = rsqrtf(ss / float(FA_HEAD_DIM) + RMS_EPS);
sc = __shfl_sync(0xffffffff, sc, 0);
for (int i = lane_id; i < FA_HEAD_DIM; i += WARP_SIZE) {
float normed = kh[i] * sc * (1.0f + __bfloat162float(__ldg(w.k_norm_weight + i)));
if (i < FA_ROTARY_DIM) {
float fe = float(2*(i%(FA_ROTARY_DIM/2))) / float(FA_ROTARY_DIM);
float freq = float(position) / powf(FA_ROPE_THETA, fe);
float cv = cosf(freq), sv = sinf(freq);
int p = (i < FA_ROTARY_DIM/2) ? i+FA_ROTARY_DIM/2 : i-FA_ROTARY_DIM/2;
float pv = kh[p]*sc*(1.0f+__bfloat162float(__ldg(w.k_norm_weight+p)));
float rotated = (i < FA_ROTARY_DIM/2) ? (normed*cv - pv*sv) : (pv*sv + normed*cv);
kc[i] = __float2bfloat16(rotated);
} else { kc[i] = __float2bfloat16(normed); }
vc[i] = __float2bfloat16(vh[i]);
}
}
}
// Q norm + RoPE (all blocks)
{
int hpb = (FA_NUM_Q_HEADS + num_blocks - 1) / num_blocks;
int hs = block_id * hpb, he = min(hs + hpb, FA_NUM_Q_HEADS);
for (int qh = hs; qh < he; qh++) {
float *qh_ptr = g_q + qh * FA_HEAD_DIM * 2;
if (warp_id == 0) {
float ss = 0; for (int i = lane_id; i < FA_HEAD_DIM; i += WARP_SIZE) ss += qh_ptr[i]*qh_ptr[i];
ss = warp_reduce_sum(ss); float sc = rsqrtf(ss / float(FA_HEAD_DIM) + RMS_EPS);
sc = __shfl_sync(0xffffffff, sc, 0);
for (int i = lane_id; i < FA_HEAD_DIM; i += WARP_SIZE) {
float normed = qh_ptr[i]*sc*(1.0f+__bfloat162float(__ldg(w.q_norm_weight+i)));
if (i < FA_ROTARY_DIM) {
float fe = float(2*(i%(FA_ROTARY_DIM/2))) / float(FA_ROTARY_DIM);
float freq = float(position) / powf(FA_ROPE_THETA, fe);
float cv = cosf(freq), sv = sinf(freq);
int p = (i < FA_ROTARY_DIM/2) ? i+FA_ROTARY_DIM/2 : i-FA_ROTARY_DIM/2;
float pv = qh_ptr[p]*sc*(1.0f+__bfloat162float(__ldg(w.q_norm_weight+p)));
qh_ptr[i] = (i < FA_ROTARY_DIM/2) ? (normed*cv-pv*sv) : (pv*sv+normed*cv);
} else { qh_ptr[i] = normed; }
}
}
}
}
grid.sync();
// Phase 3: Attention decode (online softmax + sigmoid gate)
{
int cache_len = position + 1;
float attn_scale = 1.0f / sqrtf(float(FA_HEAD_DIM));
int hpb = (FA_NUM_Q_HEADS + num_blocks - 1) / num_blocks;
int hs = block_id * hpb, he = min(hs + hpb, FA_NUM_Q_HEADS);
__shared__ float s_max_score[NUM_WARPS];
__shared__ float s_sum_exp[NUM_WARPS];
constexpr int EPL = FA_HEAD_DIM / WARP_SIZE;
for (int qh = hs; qh < he; qh++) {
int kvh = qh / FA_GQA_RATIO;
float *q_head = g_q + qh * FA_HEAD_DIM * 2;
float *out_head = g_attn_out + qh * FA_HEAD_DIM;
float max_score = -INFINITY, sum_exp = 0;
float out_acc[EPL], q_local[EPL];
for (int e = 0; e < EPL; e++) { out_acc[e] = 0; q_local[e] = q_head[lane_id*EPL+e]; }
for (int pos = warp_id; pos < cache_len; pos += NUM_WARPS) {
const __nv_bfloat16 *k_pos = k_cache + kvh*max_seq_len*FA_HEAD_DIM + pos*FA_HEAD_DIM;
const __nv_bfloat16 *v_pos = v_cache + kvh*max_seq_len*FA_HEAD_DIM + pos*FA_HEAD_DIM;
float score = 0;
for (int e = 0; e < EPL; e++) score += q_local[e] * __bfloat162float(__ldg(k_pos + lane_id*EPL+e));
score = warp_reduce_sum(score) * attn_scale;
score = __shfl_sync(0xffffffff, score, 0);
float old_max = max_score; max_score = fmaxf(max_score, score);
float exp_diff = fast_exp(old_max - max_score);
sum_exp = sum_exp * exp_diff + fast_exp(score - max_score);
float wt = fast_exp(score - max_score);
for (int e = 0; e < EPL; e++)
out_acc[e] = out_acc[e]*exp_diff + wt*__bfloat162float(__ldg(v_pos + lane_id*EPL+e));
}
if (lane_id == 0) { s_max_score[warp_id] = max_score; s_sum_exp[warp_id] = sum_exp; }
for (int e = 0; e < EPL; e++) g_activations[warp_id*FA_HEAD_DIM + lane_id*EPL+e] = out_acc[e];
__syncthreads();
if (warp_id == 0) {
float gm = -INFINITY; for (int ww = 0; ww < NUM_WARPS; ww++) if (s_max_score[ww] > -INFINITY) gm = fmaxf(gm, s_max_score[ww]);
float ts = 0; float fo[EPL]; for (int e = 0; e < EPL; e++) fo[e] = 0;
for (int ww = 0; ww < NUM_WARPS; ww++) {
if (s_max_score[ww] > -INFINITY) {
float s = fast_exp(s_max_score[ww]-gm); ts += s_sum_exp[ww]*s;
for (int e = 0; e < EPL; e++) fo[e] += g_activations[ww*FA_HEAD_DIM+lane_id*EPL+e]*s;
}
}
float *gate_ptr = q_head + FA_HEAD_DIM;
float rcp = 1.0f / ts;
for (int e = 0; e < EPL; e++) {
int idx = lane_id*EPL+e;
out_head[idx] = fo[e]*rcp * fast_sigmoid(gate_ptr[idx]);
}
}
__syncthreads();
}
}
grid.sync();
// Phase 4: O projection + residual → bf16
{
float *s_attn = reinterpret_cast<float *>(shmem);
for (int i = threadIdx.x; i < FA_Q_SIZE; i += BLOCK_SIZE) s_attn[i] = g_attn_out[i];
__syncthreads();
matvec_o_residual_bf16(s_attn, w.o_proj_weight, g_residual, hidden_out, FA_Q_SIZE, HIDDEN_SIZE, num_blocks);
}
grid.sync();
// Phase 5: Post-attn norm + MLP
__nv_bfloat16 *s_act = shmem;
rmsnorm_from_bf16(hidden_out, w.post_attn_layernorm_weight, s_act, g_residual);
matvec_gate_up_silu_bf16(s_act, w.gate_proj_weight, w.up_proj_weight,
g_mlp_inter, HIDDEN_SIZE, INTERMEDIATE_SIZE, num_blocks);
grid.sync();
// Load MLP intermediate to shared (f32)
float *s_mlp = reinterpret_cast<float *>(shmem);
for (int i = threadIdx.x; i < INTERMEDIATE_SIZE; i += BLOCK_SIZE) s_mlp[i] = g_mlp_inter[i];
__syncthreads();
matvec_down_residual_bf16(s_mlp, w.down_proj_weight, g_residual, hidden_out,
INTERMEDIATE_SIZE, HIDDEN_SIZE, num_blocks);
grid.sync();
}
// =============================================================================
// DeltaNet layer (bf16) — warp-cooperative state-in-registers recurrence
// =============================================================================
__device__ void deltanet_layer(
AtomicGridSync &grid,
const DeltaNetWeights &w,
const __nv_bfloat16 *__restrict__ input,
__nv_bfloat16 *__restrict__ g_residual,
float *__restrict__ g_activations,
float *__restrict__ g_qkv,
float *__restrict__ g_z,
float *__restrict__ g_beta,
float *__restrict__ g_alpha,
float *__restrict__ g_dn_out,
float *__restrict__ g_mlp_inter,
float *__restrict__ dn_state, // [DN_NUM_HEADS, DN_KEY, DN_VAL] f32
float *__restrict__ conv_buf, // [DN_CONV_CH, DN_CONV_K] f32
__nv_bfloat16 *__restrict__ hidden_out,
int dn_layer_idx,
__nv_bfloat16 *__restrict__ shmem)
{
int block_id = blockIdx.x;
int num_blocks = gridDim.x;
int warp_id = threadIdx.x / WARP_SIZE;
int lane_id = threadIdx.x % WARP_SIZE;
// Phase 1: RMSNorm + projections
__nv_bfloat16 *s_norm = shmem;
rmsnorm_redundant(input, w.input_layernorm_weight, s_norm, g_residual);
matvec_bf16(s_norm, w.qkv_proj_weight, g_qkv, HIDDEN_SIZE, DN_CONV_CHANNELS, num_blocks);
matvec_bf16(s_norm, w.z_proj_weight, g_z, HIDDEN_SIZE, DN_V_SIZE, num_blocks);
matvec_bf16(s_norm, w.beta_proj_weight, g_beta, HIDDEN_SIZE, DN_NUM_HEADS, num_blocks);
matvec_bf16(s_norm, w.alpha_proj_weight, g_alpha, HIDDEN_SIZE, DN_NUM_HEADS, num_blocks);
grid.sync();
// Phase 2+3: Conv1d + recurrence (blocks 0-15 only)
if (block_id < DN_NUM_HEADS) {
int h = block_id;
float *layer_conv = conv_buf + dn_layer_idx * DN_CONV_CHANNELS * DN_CONV_KERNEL;
// Conv1d + SiLU
__shared__ float s_q[DN_KEY_DIM], s_k[DN_KEY_DIM], s_v[DN_VALUE_DIM];
int head_ch[3] = {h*DN_KEY_DIM, DN_QK_SIZE+h*DN_KEY_DIM, 2*DN_QK_SIZE+h*DN_VALUE_DIM};
for (int region = 0; region < 3; region++) {
int ch_base = head_ch[region], ch_count = (region < 2) ? DN_KEY_DIM : DN_VALUE_DIM;
float *dst = (region == 0) ? s_q : (region == 1) ? s_k : s_v;
for (int c = threadIdx.x; c < ch_count; c += BLOCK_SIZE) {
int ch = ch_base + c;
float h0=layer_conv[ch*DN_CONV_KERNEL+1], h1=layer_conv[ch*DN_CONV_KERNEL+2], h2=layer_conv[ch*DN_CONV_KERNEL+3];
layer_conv[ch*DN_CONV_KERNEL]=h0; layer_conv[ch*DN_CONV_KERNEL+1]=h1;
layer_conv[ch*DN_CONV_KERNEL+2]=h2; layer_conv[ch*DN_CONV_KERNEL+3]=g_qkv[ch];
float co = 0;
for (int t = 0; t < DN_CONV_KERNEL; t++)
co += layer_conv[ch*DN_CONV_KERNEL+t] * __bfloat162float(__ldg(w.conv1d_weight + ch*DN_CONV_KERNEL+t));
dst[c] = fast_silu(co);
}
}
// Beta/alpha activations
if (threadIdx.x == 0) {
g_beta[h] = fast_sigmoid(g_beta[h]);
float a_log_val = __bfloat162float(__ldg(w.a_log + h));
float dt_b = __bfloat162float(__ldg(w.dt_bias + h));
float x = g_alpha[h] + dt_b;
float sp = (x > 20.0f) ? x : logf(1.0f + fast_exp(x));
g_alpha[h] = fast_exp(-fast_exp(a_log_val) * sp);
}
__syncthreads();
// L2 normalize Q, K
constexpr float Q_SCALE = 1.0f / 11.313708498984761f;
if (warp_id == 0) {
float sq = 0; for (int i = lane_id; i < DN_KEY_DIM; i += WARP_SIZE) sq += s_q[i]*s_q[i];
sq = warp_reduce_sum(sq); float n = rsqrtf(sq+1e-6f)*Q_SCALE;
n = __shfl_sync(0xffffffff,n,0); for (int i = lane_id; i < DN_KEY_DIM; i += WARP_SIZE) s_q[i] *= n;
}
if (warp_id == 1) {
float sq = 0; for (int i = lane_id; i < DN_KEY_DIM; i += WARP_SIZE) sq += s_k[i]*s_k[i];
sq = warp_reduce_sum(sq); float n = rsqrtf(sq+1e-6f);
n = __shfl_sync(0xffffffff,n,0); for (int i = lane_id; i < DN_KEY_DIM; i += WARP_SIZE) s_k[i] *= n;
}
__syncthreads();
float decay = g_alpha[h], beta = g_beta[h];
// k·q dot
__shared__ float s_kq;
if (warp_id == 0) {
float kq = 0; for (int i = lane_id; i < DN_KEY_DIM; i += WARP_SIZE) kq += s_k[i]*s_q[i];
kq = warp_reduce_sum(kq); if (lane_id == 0) s_kq = kq;
}
__syncthreads();
float kq = s_kq;
// Warp-cooperative recurrence (state in global memory — decode is 1 token, fine)
float *state = dn_state + h * DN_KEY_DIM * DN_VALUE_DIM;
float *out_head = g_dn_out + h * DN_VALUE_DIM;
constexpr int J_PER_WARP = DN_VALUE_DIM / NUM_WARPS;
constexpr int I_PER_LANE = DN_KEY_DIM / WARP_SIZE;
#pragma unroll
for (int jj = 0; jj < J_PER_WARP; jj++) {
int j = warp_id * J_PER_WARP + jj;
float s_regs[I_PER_LANE], stk = 0, sqv = 0;
#pragma unroll
for (int ii = 0; ii < I_PER_LANE; ii++) {
int i = lane_id + ii * WARP_SIZE;
float sv = state[j*DN_KEY_DIM+i]; s_regs[ii] = sv;
stk += sv * s_k[i]; sqv += sv * s_q[i];
}
stk = warp_reduce_sum(stk); sqv = warp_reduce_sum(sqv);
stk = __shfl_sync(0xffffffff,stk,0); sqv = __shfl_sync(0xffffffff,sqv,0);
float error_j = (s_v[j] - stk) * beta;
float o_j = decay * sqv + error_j * kq;
if (lane_id == 0) out_head[j] = o_j;
#pragma unroll
for (int ii = 0; ii < I_PER_LANE; ii++) {
int i = lane_id + ii * WARP_SIZE;
state[j*DN_KEY_DIM+i] = s_regs[ii] * decay + s_k[i] * error_j;
}
}
// Gated RMSNorm
__syncthreads();
{
__shared__ float smem_gnorm[NUM_WARPS];
float sq = 0; for (int i = threadIdx.x; i < DN_VALUE_DIM; i += BLOCK_SIZE) sq += out_head[i]*out_head[i];
sq = warp_reduce_sum(sq); if (lane_id == 0) smem_gnorm[warp_id] = sq; __syncthreads();
if (warp_id == 0) { float v = (lane_id < NUM_WARPS) ? smem_gnorm[lane_id] : 0; v = warp_reduce_sum(v); if (lane_id == 0) smem_gnorm[0] = rsqrtf(v/DN_VALUE_DIM + RMS_EPS); }
__syncthreads(); float rstd = smem_gnorm[0];
for (int i = threadIdx.x; i < DN_VALUE_DIM; i += BLOCK_SIZE) {
float normed = out_head[i] * rstd * __bfloat162float(__ldg(w.norm_weight + i));
float gate = fast_silu(g_z[h*DN_VALUE_DIM+i]);
out_head[i] = normed * gate;
}
}
} else {
// Idle blocks: could prefetch weights
}
grid.sync();
// Phase 4: Out projection + residual → bf16
{
float *s_dn = reinterpret_cast<float *>(shmem);
for (int i = threadIdx.x; i < DN_V_SIZE; i += BLOCK_SIZE) s_dn[i] = g_dn_out[i];
__syncthreads();
matvec_o_residual_bf16(s_dn, w.out_proj_weight, g_residual, hidden_out, DN_V_SIZE, HIDDEN_SIZE, num_blocks);
}
grid.sync();
// Phase 5: Post-attn norm + MLP
__nv_bfloat16 *s_act = shmem;
rmsnorm_from_bf16(hidden_out, w.post_attn_layernorm_weight, s_act, g_residual);
matvec_gate_up_silu_bf16(s_act, w.gate_proj_weight, w.up_proj_weight,
g_mlp_inter, HIDDEN_SIZE, INTERMEDIATE_SIZE, num_blocks);
grid.sync();
float *s_mlp = reinterpret_cast<float *>(shmem);
for (int i = threadIdx.x; i < INTERMEDIATE_SIZE; i += BLOCK_SIZE) s_mlp[i] = g_mlp_inter[i];
__syncthreads();
matvec_down_residual_bf16(s_mlp, w.down_proj_weight, g_residual, hidden_out,
INTERMEDIATE_SIZE, HIDDEN_SIZE, num_blocks);
grid.sync();
}
// =============================================================================
// LM Head: vocab projection + argmax
// =============================================================================
__global__ void lm_head_kernel(
const float *__restrict__ hidden,
const __nv_bfloat16 *__restrict__ weight, // [VOCAB, HIDDEN] bf16
float *__restrict__ block_max_vals,
int *__restrict__ block_max_idxs,
int *__restrict__ output_token,
unsigned int *__restrict__ sync_counter)
{
__shared__ float s_hidden[HIDDEN_SIZE];
for (int i = threadIdx.x; i < HIDDEN_SIZE; i += LM_BLOCK_SIZE) s_hidden[i] = hidden[i];
__syncthreads();
int warp_id = threadIdx.x / WARP_SIZE, lane_id = threadIdx.x % WARP_SIZE;
int num_warps = LM_BLOCK_SIZE / WARP_SIZE;
int rpb = (VOCAB_SIZE + gridDim.x - 1) / gridDim.x;
int rs = blockIdx.x * rpb, re = min(rs + rpb, VOCAB_SIZE);
float local_max = -INFINITY; int local_max_idx = -1;
for (int m = rs + warp_id; m < re; m += num_warps) {
const __nv_bfloat16 *w_row = weight + m * HIDDEN_SIZE;
float sum = 0;
#pragma unroll 4
for (int k = lane_id * 8; k < HIDDEN_SIZE; k += WARP_SIZE * 8) {
uint4 w_u4 = load_128bit(reinterpret_cast<const uint4 *>(w_row + k));
const __nv_bfloat16 *wp = reinterpret_cast<const __nv_bfloat16 *>(&w_u4);
for (int i = 0; i < 8; i++) sum += __bfloat162float(wp[i]) * s_hidden[k+i];
}
sum = warp_reduce_sum(sum);
if (lane_id == 0 && sum > local_max) { local_max = sum; local_max_idx = m; }
}
local_max = __shfl_sync(0xffffffff, local_max, 0);
local_max_idx = __shfl_sync(0xffffffff, local_max_idx, 0);
__shared__ float wm[32]; __shared__ int wi[32];
if (lane_id == 0) { wm[warp_id] = local_max; wi[warp_id] = local_max_idx; }
__syncthreads();
if (warp_id == 0) {
float mv = (lane_id < num_warps) ? wm[lane_id] : -INFINITY;
int mi = (lane_id < num_warps) ? wi[lane_id] : -1;
for (int o = WARP_SIZE/2; o > 0; o /= 2) {
float ov = __shfl_down_sync(0xffffffff, mv, o);
int oi = __shfl_down_sync(0xffffffff, mi, o);
if (ov > mv) { mv = ov; mi = oi; }
}
if (lane_id == 0) { block_max_vals[blockIdx.x] = mv; block_max_idxs[blockIdx.x] = mi; }
}
__syncthreads();
if (threadIdx.x == 0) { __threadfence(); atomicAdd(sync_counter, 1); }
if (blockIdx.x == 0) {
if (threadIdx.x == 0) { volatile unsigned int *vc = (volatile unsigned int *)sync_counter; while (*vc < (unsigned int)gridDim.x) {} __threadfence(); }
__syncthreads();
int tid = threadIdx.x; float bv = -INFINITY; int bi = -1;
for (int i = tid; i < gridDim.x; i += LM_BLOCK_SIZE) { float v = block_max_vals[i]; if (v > bv) { bv = v; bi = block_max_idxs[i]; } }
__shared__ float sv[256]; __shared__ int si[256];
sv[tid] = bv; si[tid] = bi; __syncthreads();
for (int s = LM_BLOCK_SIZE/2; s > 0; s >>= 1) { if (tid < s && sv[tid+s] > sv[tid]) { sv[tid] = sv[tid+s]; si[tid] = si[tid+s]; } __syncthreads(); }
if (tid == 0) *output_token = si[0];
}
}
// =============================================================================
// Main decode kernel
// =============================================================================
__global__ void __launch_bounds__(BLOCK_SIZE, 1)
decode_kernel(
const __nv_bfloat16 *__restrict__ embed_weight,
const __nv_bfloat16 *__restrict__ final_norm_weight,
const __nv_bfloat16 *__restrict__ lm_head_weight,
const LayerWeights *__restrict__ layer_weights,
__nv_bfloat16 *__restrict__ fa_k_cache,
__nv_bfloat16 *__restrict__ fa_v_cache,
float *__restrict__ dn_states,
float *__restrict__ conv_bufs,
__nv_bfloat16 *__restrict__ hidden_buffer,
float *__restrict__ g_activations,
__nv_bfloat16 *__restrict__ g_residual,
float *__restrict__ g_qkv_scratch,
float *__restrict__ g_kv_scratch,
float *__restrict__ g_attn_out,
float *__restrict__ g_mlp_inter,
float *__restrict__ g_z_scratch,
float *__restrict__ g_beta_scratch,
float *__restrict__ g_alpha_scratch,
float *__restrict__ g_normalized,
unsigned int *__restrict__ barrier_counter,
unsigned int *__restrict__ barrier_generation,
int input_token_id, int position, int max_seq_len)
{
int block_id = blockIdx.x;
int num_blocks = gridDim.x;
// Initialize barrier
if (block_id == 0 && threadIdx.x == 0) { *barrier_counter = 0; *barrier_generation = 0; }
__syncthreads();
if (threadIdx.x == 0) {
asm volatile("fence.acq_rel.gpu;" ::: "memory");
unsigned int arrived = atomicAdd(barrier_counter, 1);
if (arrived == (unsigned int)num_blocks - 1) { *barrier_counter = 0; asm volatile("fence.acq_rel.gpu;" ::: "memory"); atomicAdd(barrier_generation, 1); }
else { volatile unsigned int *vg = (volatile unsigned int *)barrier_generation; while (*vg == 0) {} }
asm volatile("fence.acq_rel.gpu;" ::: "memory");
}
__syncthreads();
AtomicGridSync grid{barrier_counter, barrier_generation, (unsigned int)num_blocks, 1};
// Shared memory: large enough for max(HIDDEN_SIZE bf16, INTERMEDIATE_SIZE f32)
__shared__ __align__(16) char shmem_raw[MAX_ACT_DIM * sizeof(float)];
__nv_bfloat16 *shmem_bf16 = reinterpret_cast<__nv_bfloat16 *>(shmem_raw);
const __nv_bfloat16 *embed_row = embed_weight + input_token_id * HIDDEN_SIZE;
int fa_kv_stride = FA_NUM_KV_HEADS * max_seq_len * FA_HEAD_DIM;
int dn_state_stride = DN_NUM_HEADS * DN_KEY_DIM * DN_VALUE_DIM;
int dn_layer_idx = 0, fa_layer_idx = 0;
for (int layer = 0; layer < NUM_LAYERS; layer++) {
const __nv_bfloat16 *layer_input = (layer == 0) ? embed_row : hidden_buffer;
if (LAYER_TYPE[layer] == 0) {
deltanet_layer(
grid, layer_weights[layer].dn, layer_input,
g_residual, g_activations, g_qkv_scratch, g_z_scratch,
g_beta_scratch, g_alpha_scratch, g_attn_out, g_mlp_inter,
dn_states + dn_layer_idx * dn_state_stride,
conv_bufs, hidden_buffer, dn_layer_idx, shmem_bf16);
dn_layer_idx++;
} else {
full_attention_layer(
grid, layer_weights[layer].fa, layer_input,
fa_k_cache + fa_layer_idx * fa_kv_stride,
fa_v_cache + fa_layer_idx * fa_kv_stride,
g_residual, g_activations, g_qkv_scratch, g_kv_scratch,
g_attn_out, g_mlp_inter, hidden_buffer,
position, max_seq_len, shmem_bf16);
fa_layer_idx++;
}
}
// Final RMSNorm (block 0 only)
if (block_id == 0) {
__shared__ float smem_reduce[NUM_WARPS];
int warp_id = threadIdx.x / WARP_SIZE, lane_id = threadIdx.x % WARP_SIZE;
float local_sum_sq = 0;
for (int i = threadIdx.x; i < HIDDEN_SIZE; i += BLOCK_SIZE) {
float v = __bfloat162float(hidden_buffer[i]); g_activations[i] = v; local_sum_sq += v*v;
}
local_sum_sq = warp_reduce_sum(local_sum_sq);
if (lane_id == 0) smem_reduce[warp_id] = local_sum_sq; __syncthreads();
if (warp_id == 0) { float sum = (lane_id < NUM_WARPS) ? smem_reduce[lane_id] : 0; sum = warp_reduce_sum(sum); if (lane_id == 0) smem_reduce[0] = rsqrtf(sum/HIDDEN_SIZE + RMS_EPS); }
__syncthreads(); float rstd = smem_reduce[0];
for (int i = threadIdx.x; i < HIDDEN_SIZE; i += BLOCK_SIZE) {
float wt = __bfloat162float(__ldg(final_norm_weight + i));
g_normalized[i] = g_activations[i] * rstd * (1.0f + wt);
}
}
}
// =============================================================================
// C entry point
// =============================================================================
extern "C" void launch_decode(
int input_token_id, int *output_token_id,
const void *embed_weight, const LayerWeights *layer_weights,
const void *final_norm_weight,
const void *lm_head_weight,
void *fa_k_cache, void *fa_v_cache,
void *dn_states, void *conv_bufs,
void *hidden_buffer, void *g_activations, void *g_residual,
void *g_qkv_scratch, void *g_kv_scratch, void *g_attn_out,
void *g_mlp_inter, void *g_z_scratch, void *g_beta_scratch,
void *g_alpha_scratch, void *g_normalized,
unsigned int *barrier_counter, unsigned int *barrier_generation,
float *block_max_vals, int *block_max_idxs,
unsigned int *lm_sync_counter,
int position, int max_seq_len, cudaStream_t stream)
{
decode_kernel<<<NUM_BLOCKS, BLOCK_SIZE, 0, stream>>>(
(const __nv_bfloat16 *)embed_weight,
(const __nv_bfloat16 *)final_norm_weight,
(const __nv_bfloat16 *)lm_head_weight,
layer_weights,
(__nv_bfloat16 *)fa_k_cache, (__nv_bfloat16 *)fa_v_cache,
(float *)dn_states, (float *)conv_bufs,
(__nv_bfloat16 *)hidden_buffer,
(float *)g_activations, (__nv_bfloat16 *)g_residual,
(float *)g_qkv_scratch, (float *)g_kv_scratch,
(float *)g_attn_out, (float *)g_mlp_inter,
(float *)g_z_scratch, (float *)g_beta_scratch,
(float *)g_alpha_scratch, (float *)g_normalized,
barrier_counter, barrier_generation,
input_token_id, position, max_seq_len);
cudaMemsetAsync(lm_sync_counter, 0, sizeof(unsigned int), stream);
lm_head_kernel<<<LM_NUM_BLOCKS, LM_BLOCK_SIZE, 0, stream>>>(
(const float *)g_normalized,
(const __nv_bfloat16 *)lm_head_weight,
block_max_vals, block_max_idxs,
output_token_id, lm_sync_counter);
}