-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathflash_fwd_mla_sm100.py
More file actions
3160 lines (2890 loc) · 134 KB
/
Copy pathflash_fwd_mla_sm100.py
File metadata and controls
3160 lines (2890 loc) · 134 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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2026, Colfax International.
import math
from functools import partial
from typing import Callable, Optional
import cuda.bindings.driver as cuda
import cutlass
import cutlass.cute as cute
from cutlass import Float32, Int64, Int32, Uint32, Boolean, const_expr
from cutlass.cute import FastDivmodDivisor
import cutlass.pipeline as pipeline
from cutlass.cute.nvgpu import cpasync, tcgen05
import cutlass.utils.blackwell_helpers as sm100_utils
from cutlass.utils import ClcDynamicPersistentTileScheduler
from quack import copy_utils
from flash_attn.cute.pack_gqa import pack_gqa_layout, make_packgqa_tiled_tma_atom
from flash_attn.cute.paged_kv import PagedKVManager
from flash_attn.cute import utils as fa_utils
from flash_attn.cute.seqlen_info import SeqlenInfoQK
from flash_attn.cute.block_info import BlockInfo
from flash_attn.cute.mask import AttentionMask
import flash_attn.cute.blackwell_helpers as fa_sm100_utils
from flash_attn.cute.softmax import SoftmaxSm100
from flash_attn.cute.tile_scheduler import (
SchedulerState,
SchedulingMode,
TileSchedulerArguments,
TileSchedulerProtocol,
SingleTileScheduler,
SingleTileLPTScheduler,
SingleTileVarlenScheduler,
ParamsBase,
)
from flash_attn.cute.fa_logging import fa_log, fa_printf
from flash_attn.cute.utils import smid, get_batch_from_cu_tensor
from flash_attn.cute.topk_gather_kv import CpasyncGatherKVManager
from flash_attn.cute.named_barrier import NamedBarrierFwdSm100_MLA2CTA
class FlashAttentionMLAForwardSm100:
def __init__(
self,
is_causal: bool = False,
use_cpasync_load_KV: bool = False,
topk_length: int = 2048,
is_topk_gather: bool = True,
pack_gqa: bool = False,
qhead_per_kvhead: int = 1,
nheads_kv: int = 1,
hdim: int = 64,
hdimv: int = 512,
has_seqused_q: bool = False,
has_cu_seqlens_q: bool = False,
disable_bitmask: bool = False,
use_clc_scheduler: bool = True,
has_qk: bool = True,
):
self.is_causal = is_causal
self.is_local = False
self.pack_gqa = pack_gqa
self.qhead_per_kvhead = qhead_per_kvhead
assert qhead_per_kvhead <= 128
self.nheads_kv = nheads_kv
self.use_tma_O = True
self.use_cpasync_load_KV = use_cpasync_load_KV
self.use_tma_KV = not use_cpasync_load_KV
self.topk_length = topk_length
self.is_topk_gather = is_topk_gather
if is_topk_gather:
assert pack_gqa
assert qhead_per_kvhead == 128, "require MQA 128 for DSA path"
assert use_cpasync_load_KV
# user-provided option if topk indices guaranteed in bounds
self.disable_bitmask = disable_bitmask
self.has_qk = has_qk
# ==== tile scheduler ====
self.is_persistent = False
self.use_clc_scheduler = use_clc_scheduler
self.sched_stages = 1
self.scheduling_mode = (
SchedulingMode.CLC if self.use_clc_scheduler else SchedulingMode.STATIC
)
self.is_varlen_q = has_seqused_q or has_cu_seqlens_q
self.use_packed_varlen_sched = has_cu_seqlens_q and qhead_per_kvhead == 128 and pack_gqa
self.use_varlen_scheduler = self.is_varlen_q and not self.use_packed_varlen_sched
if const_expr(self.use_varlen_scheduler):
self.TileScheduler = SingleTileVarlenScheduler
elif self.use_clc_scheduler:
self.TileScheduler = SingleTileLPTScheduler
else:
self.TileScheduler = SingleTileScheduler
fa_log(
1,
f"TileScheduler={self.TileScheduler.__name__}, scheduling_mode={self.scheduling_mode.name}",
)
# ==== thread info ====
self.num_softmax_threads = 128
self.num_epilogue_threads = 128
self.num_load_threads = 32
self.num_mma_threads = 32
self.num_empty_threads = 32 if use_cpasync_load_KV else 64
self.num_relay_threads = 32 if use_cpasync_load_KV else 0
self.num_cpasync_load_threads = 128 if use_cpasync_load_KV else 0
self.num_threads = (
self.num_softmax_threads
+ self.num_epilogue_threads
+ self.num_load_threads
+ self.num_mma_threads
+ self.num_empty_threads
+ self.num_relay_threads
+ self.num_cpasync_load_threads
)
self.num_warps = self.num_threads // 32
assert self.num_warps == 12 or self.num_warps == 16
self.softmax_warp_indices = (0, 1, 2, 3)
self.epilogue_warp_indices = (4, 5, 6, 7)
self.load_warp_id = 8
self.mma_warp_id = 9
self.clc_scheduler_warp_id = 10
self.relay_warp_id = 11
self.empty_warp_ids = tuple(
w
for w, active in [
(self.relay_warp_id, not use_cpasync_load_KV),
(self.clc_scheduler_warp_id, not self.use_clc_scheduler),
]
if active
)
self.cpasync_load_warp_indices = (12, 13, 14, 15)
# ==== register usage ====
if self.num_warps == 16:
self.num_regs_load = 112
self.num_regs_mma = 112
self.num_regs_softmax = 192
self.num_regs_epilogue = 128
self.num_regs_cpasync = 80 if self.use_cpasync_load_KV else 0
self.num_regs_other = 48
else:
self.num_regs_load = 168 - 40
self.num_regs_mma = 168 - 40
self.num_regs_softmax = 168 + 80
self.num_regs_epilogue = 168 - 40
self.num_regs_cpasync = 0
self.num_regs_other = 48
self.num_regs_per_thread = 168 if self.num_warps == 12 else 128
self.num_regs_total = 504 if self.num_warps == 12 else 512
assert (
self.num_regs_mma
+ self.num_regs_softmax
+ self.num_regs_epilogue
+ self.num_regs_cpasync
<= self.num_regs_total
)
# ==== 2cta info ====
self.use_2cta_instrs = True
self.cta_group = tcgen05.CtaGroup.TWO
self.cta_group_size = 2
self.cluster_shape_mn = (2, 1)
self.cluster_shape_mnk = (2, 1, 1)
# ==== problem shape info ====
self.hdim = hdim
self.hdimv = hdimv
self.cta_tile_m = 64
self.cluster_tile_m = self.cta_group_size * self.cta_tile_m
self.tile_n = 128
assert (
pack_gqa is False
or self.cluster_tile_m % qhead_per_kvhead == 0
or qhead_per_kvhead % self.cluster_tile_m == 0
)
self.num_hdimv_splits = 2 # split hdimv in half for our Qv @ V^T and P @ V mmas.
assert hdimv % 32 == 0
assert self.topk_length % self.tile_n == 0 or not self.is_topk_gather
self.epi_tile = (self.cta_tile_m, self.hdimv // self.num_hdimv_splits)
self.tile_P = (self.cta_tile_m, self.tile_n)
# ==== MMA info ====
self.mma_tiler_QK = (
self.cluster_tile_m,
self.tile_n,
self.hdim,
)
self.mma_tiler_QvV = (
self.cluster_tile_m,
self.tile_n,
self.hdimv // self.num_hdimv_splits,
)
self.mma_tiler_PVt = (
self.cluster_tile_m,
self.hdimv // self.num_hdimv_splits,
self.tile_n,
)
self.major_mode_Q = tcgen05.OperandMajorMode.K
self.major_mode_Qvi = tcgen05.OperandMajorMode.K
self.major_mode_K = tcgen05.OperandMajorMode.K
self.major_mode_Vi = tcgen05.OperandMajorMode.K
self.major_mode_Vti = tcgen05.OperandMajorMode.MN
self.major_mode_P = tcgen05.OperandMajorMode.K
self.operand_source_Q = tcgen05.OperandSource.SMEM
self.operand_source_Qvi = tcgen05.OperandSource.SMEM
self.operand_source_P = tcgen05.OperandSource.SMEM
# ==== pipeline info ====
self.num_stages_Q = 1
self.num_stages_K = 1
self.num_stages_Qv = 2
self.num_stages_V = 4
self.num_stages_S = 2
# self.num_stages_P = 1 if has_qk else 2
self.num_stages_P = 1
self.num_stages_Oi = 1
self.num_stages_sm_stats = 2
self.num_stages_bitmask = 2
assert self.num_stages_S == 2, "mainloops expect 2 stages for S"
# ==== dtype info ====
self.dtype_acc = Float32
# ==== TMEM info ====
SM100_TMEM_CAPACITY_COLUMNS = 512
self.tmem_alloc_cols = SM100_TMEM_CAPACITY_COLUMNS
self.tmem_cols_S = self.tile_n // self.cta_group_size
self.tmem_cols_Oi = (self.hdimv // self.num_hdimv_splits) // self.cta_group_size
self.tmem_offset_S = [
self.tmem_cols_S * stage for stage in range(self.num_stages_S)
] # allocate 64 TMEM columns for each stage of S
self.tmem_offset_O0 = self.tmem_cols_S * self.num_stages_S
self.tmem_offset_O1 = self.tmem_offset_O0 + self.tmem_cols_Oi
self.tmem_offsets_O = [self.tmem_offset_O0, self.tmem_offset_O1]
self.total_tmem = self.tmem_offset_O1 + self.tmem_cols_Oi
assert self.total_tmem <= self.tmem_alloc_cols, (
f"Total TMEM columns allocated {self.total_tmem} exceeds capacity {self.tmem_alloc_cols}"
)
def _get_shared_storage_cls(self):
self.buffer_align_bytes = 1024
def smem_struct_align(dtype, staged_layout, disabled=False):
if disabled:
return cute.struct.MemRange[dtype, 0]
return cute.struct.Align[
cute.struct.MemRange[dtype, cute.cosize(staged_layout)],
self.buffer_align_bytes,
]
def mbar_struct(num_stages):
return cute.struct.MemRange[Int64, 2 * num_stages]
(sQ_struct, sK_struct, sQv_struct, sV_struct, sP_struct) = (
smem_struct_align(dtype, layout, disabled)
for dtype, layout, disabled in [
(self.dtype_Q, self.sQ_layout_staged, not self.has_qk),
(self.dtype_K, self.sK_layout_staged, not self.has_qk),
(self.dtype_Qv, self.sQv_layout_staged, False),
(self.dtype_V, self.sV_layout_staged, False),
(self.dtype_P, self.sP_layout_staged, False),
]
)
sStats_struct = cute.struct.MemRange[Float32, cute.cosize(self.sStats_layout)]
sScale_struct = cute.struct.MemRange[Float32, cute.cosize(self.sScale_layout)]
sBitmask_struct = cute.struct.MemRange[Uint32, cute.cosize(self.sBitmask_layout)]
(
mbar_ptr_Q_struct,
mbar_ptr_K_struct,
mbar_ptr_Qv_struct,
mbar_ptr_V_struct,
mbar_ptr_S_struct,
mbar_ptr_P_struct,
mbar_ptr_O0_struct,
mbar_ptr_O1_struct,
mbar_sm_stats_struct,
mbar_bitmask_struct,
) = (
mbar_struct(n)
for n in [
self.num_stages_Q,
self.num_stages_K,
self.num_stages_Qv,
self.num_stages_V,
self.num_stages_S,
self.num_stages_P,
self.num_stages_Oi,
self.num_stages_Oi,
self.num_stages_sm_stats,
self.num_stages_bitmask,
]
)
tmem_dealloc_mbar_struct = Int64
tmem_holding_buf_struct = Int32
self.sched_stages = 1
clc_response_size = self.sched_stages * 4 if self.use_clc_scheduler else 0
clc_mbar_size = self.sched_stages * 2 if self.use_clc_scheduler else 0
@cute.struct
class SharedStorage:
mbar_ptr_Q: mbar_ptr_Q_struct
mbar_ptr_K: mbar_ptr_K_struct
mbar_ptr_Qv: mbar_ptr_Qv_struct
mbar_ptr_V: mbar_ptr_V_struct
mbar_ptr_S: mbar_ptr_S_struct
mbar_ptr_P: mbar_ptr_P_struct
mbar_ptr_O0: mbar_ptr_O0_struct
mbar_ptr_O1: mbar_ptr_O1_struct
mbar_ptr_K_cpasync: mbar_ptr_K_struct
mbar_ptr_V_cpasync: mbar_ptr_V_struct
mbar_ptr_sm_stats: mbar_sm_stats_struct
mbar_ptr_bitmask: mbar_bitmask_struct
tmem_dealloc_mbar: tmem_dealloc_mbar_struct
tmem_holding_buf: tmem_holding_buf_struct
clc_mbar_ptr: cute.struct.MemRange[cutlass.Int64, clc_mbar_size]
clc_response: cute.struct.MemRange[Int32, clc_response_size]
sO_empty_mbar_ptr: cutlass.Int64
sRowMax: sStats_struct
sRowSum: sStats_struct
sScale: sScale_struct
sBitmask: sBitmask_struct
sQv: sQv_struct
sQ: sQ_struct
sK: sK_struct
sV: sV_struct
sP: sP_struct
# print("smem bytes = ", SharedStorage.size_in_bytes())
return SharedStorage
# fmt: off
@cute.jit
def __call__(
self,
mQ: Optional[cute.Tensor], # (b, s_q, h, d) or (total_q, h, d) if there is cu_seqlens_q
mQv: cute.Tensor, # (b, s_q, h, dv) or (total_q, h, d) if there is cu_seqlens_q
mK: Optional[cute.Tensor], # (b, s_k, h_k, d) or (total_k, h_k, d) if there is cu_seqlens_k or (num_pages, page_size, h_k, d) if there is page_table
mV: cute.Tensor, # (b, s_k, h_k, dv) or (total_k, h_k, dv) if there is cu_seqlens_k or (num_pages, page_size, h_k, dv) if there is page_table
mO: cute.Tensor, # (b, s_q, h, dv) or (total_q, h, dv) if there is cu_seqlens_q
mLSE: Optional[cute.Tensor], # (b, s_q, h) or (total_q, h) if there is cu_seqlens_q
softmax_scale: Float32,
mP: Optional[cute.Tensor] = None, # (b, s_q, h, topk) or (total_q, h, topk) if there is cu_seqlens_q
mRowMax: Optional[cute.Tensor] = None, # (b, s_q, topk // tile_n, h) or (total_q, topk // tile_n, h) if there is cu_seqlens_q
mCuSeqlensQ: Optional[cute.Tensor] = None, # (b + 1)
mCuSeqlensK: Optional[cute.Tensor] = None, # (b + 1)
mSeqUsedQ: Optional[cute.Tensor] = None, # (b)
mSeqUsedK: Optional[cute.Tensor] = None, # (b)
mIndexTopk: Optional[cute.Tensor] = None, # (b, s_q, topk) or (total_q, topk) if there is cu_seqlens_q
mPageTable: Optional[cute.Tensor] = None,
window_size_left: Int32 | int | None = None,
window_size_right: Int32 | int | None = None,
# Always keep stream as the last parameter (EnvStream: obtained implicitly via TVM FFI).
stream: cuda.CUstream = None,
):
# fmt: on
self.store_P = mP is not None
self.store_row_max = mRowMax is not None
if const_expr(self.has_qk):
assert mQ is not None and mK is not None, "has_qk requires mQ and mK"
else:
assert mQ is None and mK is None, "not has_qk disallows mQ and mK"
# ==== dtype info ====
self.dtype_Q = mQ.element_type if self.has_qk else cutlass.BFloat16
self.dtype_K = mK.element_type if self.has_qk else cutlass.BFloat16
self.dtype_Qv = mQv.element_type
self.dtype_V = mV.element_type
self.dtype_P = mV.element_type
self.dtype_O = mO.element_type
if const_expr(self.store_P):
assert mP.element_type == self.dtype_P
# ==== Prepare Tensors ====
new_stride = lambda mX: (
*(cute.assume(s, divby=128 // mX.element_type.width) for s in mX.stride[:-1]),
mX.stride[-1],
)
mQ, mQv, mK, mV, mO, mP = [
cute.make_tensor(mX.iterator, cute.make_layout(mX.shape, stride=new_stride(mX)))
if mX is not None
else None
for mX in (mQ, mQv, mK, mV, mO, mP)
]
# (b, s, h, d) -> (s, d, h, b) or
# (total, h, d) -> (total, d, h) or
# (num_pages, page_size, h_k, d) -> (page_size, d, h_k, num_pages)
QO_layout_transpose = [1, 3, 2, 0] if const_expr(mCuSeqlensQ is None) else [0, 2, 1]
KV_layout_transpose = [1, 3, 2, 0] if const_expr(mCuSeqlensK is None) else [0, 2, 1]
mQ, mQv, mO, mP = [
cute.make_tensor(mX.iterator, cute.select(mX.layout, mode=QO_layout_transpose))
if mX is not None
else None
for mX in (mQ, mQv, mO, mP)
]
mK, mV = [
cute.make_tensor(mX.iterator, cute.select(mX.layout, mode=KV_layout_transpose))
if mX is not None
else None
for mX in (mK, mV)
]
# (s_k, dv, h_k, b) -> (dv, s_k, h_k, b) or
# (total_k, dv, h_k) -> (dv, total_k, h_k)
V_layout_transpose = [1, 0, 2, 3] if const_expr(mCuSeqlensK is None) else [1, 0, 2]
mVt = cute.make_tensor(mV.iterator, cute.select(mV.layout, mode=V_layout_transpose))
# (b, s_q, topk) -> (topk, s_q, b) or (total_q, topk) -> (topk, total_q)
topk_layout_transpose = [2, 1, 0] if const_expr(mCuSeqlensQ is None) else [1, 0]
mIndexTopk = (
cute.make_tensor(
mIndexTopk.iterator, cute.select(mIndexTopk.layout, mode=topk_layout_transpose)
)
if mIndexTopk is not None
else None
)
# (b, s_q, h) -> (s_q, h, b) or (total_q, h) -> (total_q, h)
LSE_layout_transpose = [1, 2, 0] if const_expr(mCuSeqlensQ is None) else [0, 1]
mLSE = (
cute.make_tensor(mLSE.iterator, cute.select(mLSE.layout, mode=LSE_layout_transpose))
if mLSE is not None
else None
)
# (b, s, topk//128, h) => (s, topk//128, h, b) or
# (total, topk//128, h) == (total, topk//128, h)
rowmax_layout_transpose = [1, 2, 3, 0] if const_expr(mCuSeqlensQ is None) else [0, 1, 2]
if const_expr(mRowMax is not None):
mRowMax = cute.make_tensor(
mRowMax.iterator, cute.select(mRowMax.layout, mode=rowmax_layout_transpose)
)
topk_length_dynamic = mIndexTopk.shape[0] if mIndexTopk is not None else None
self.o_layout = cutlass.utils.LayoutEnum.from_tensor(mO)
self.p_layout = cutlass.utils.LayoutEnum.ROW_MAJOR
if const_expr(self.store_P):
assert cutlass.utils.LayoutEnum.from_tensor(mP) == self.p_layout
mO_og = mO
mP_og = mP
if const_expr(self.pack_gqa):
mQ, mQv, mO, mP, mRowMax = [
pack_gqa_layout(mX, self.qhead_per_kvhead, self.nheads_kv, head_idx=2)
if mX is not None
else None
for mX in (mQ, mQv, mO, mP, mRowMax)
]
if const_expr(mLSE is not None):
mLSE = pack_gqa_layout(mLSE, self.qhead_per_kvhead, self.nheads_kv, head_idx=1)
# ==== Prepare MMAs ====
# (local_var, dtype_a, major_a, major_b, mma_tiler, operand_source_a)
# fmt: off
_mma_specs = [
("tiled_mma_QK", self.dtype_Q, self.major_mode_Q, self.major_mode_K, self.mma_tiler_QK, self.operand_source_Q),
("tiled_mma_QvV", self.dtype_Qv, self.major_mode_Qvi, self.major_mode_Vi, self.mma_tiler_QvV, self.operand_source_Qvi),
("tiled_mma_PVt", self.dtype_P, self.major_mode_P, self.major_mode_Vti, self.mma_tiler_PVt, self.operand_source_P),
]
tiled_mma_QK, tiled_mma_QvV, tiled_mma_PVt = (
sm100_utils.make_trivial_tiled_mma(
dtype_a, major_a, major_b, self.dtype_acc, self.cta_group, mma_tiler[:2], operand_source_a,
)
for _, dtype_a, major_a, major_b, mma_tiler, operand_source_a in _mma_specs
)
# fmt: on
# ==== Prepare SMEM layouts and TMAs ====
# (attr, make_fn, tiled_mma, mma_tiler, dtype, num_stages)
# fmt: off
_smem_layout_specs = [
("sQ_layout", sm100_utils.make_smem_layout_a, tiled_mma_QK, self.mma_tiler_QK, self.dtype_Q, self.num_stages_Q),
("sK_layout", sm100_utils.make_smem_layout_b, tiled_mma_QK, self.mma_tiler_QK, self.dtype_K, self.num_stages_K),
("sP_layout", sm100_utils.make_smem_layout_a, tiled_mma_PVt, self.mma_tiler_PVt, self.dtype_P, self.num_stages_P),
("sQv_layout", sm100_utils.make_smem_layout_a, tiled_mma_QvV, self.mma_tiler_QvV, self.dtype_Qv, self.num_stages_Qv),
("sV_layout", sm100_utils.make_smem_layout_b, tiled_mma_QvV, self.mma_tiler_QvV, self.dtype_V, self.num_stages_V),
("sVt_layout", sm100_utils.make_smem_layout_b, tiled_mma_PVt, self.mma_tiler_PVt, self.dtype_V, self.num_stages_V),
]
for attr, make_fn, tiled_mma, mma_tiler, dtype, num_stages in _smem_layout_specs:
ab_kwarg = "a_dtype" if make_fn is sm100_utils.make_smem_layout_a else "b_dtype"
staged = make_fn(
tiled_mma=tiled_mma,
mma_tiler_mnk=mma_tiler,
num_stages=num_stages,
**{ab_kwarg: dtype},
)
setattr(self, f"{attr}_staged", staged)
setattr(self, attr, cute.select(staged, mode=[0, 1, 2]))
# fmt: on
self.sStats_layout = cute.make_layout((self.cta_tile_m, self.cta_group_size))
self.sScale_layout = cute.make_layout((self.cta_tile_m, self.num_stages_sm_stats))
self.sBitmask_layout = cute.make_layout((self.tile_n // 32, self.num_stages_bitmask))
# fmt: off
for attr, dtype, layout in [
("tma_copy_bytes_Q", self.dtype_Q, self.sQ_layout),
("tma_copy_bytes_K", self.dtype_K, self.sK_layout),
("tma_copy_bytes_Qvi", self.dtype_Qv, self.sQv_layout),
("tma_copy_bytes_Vi", self.dtype_V, self.sV_layout),
]:
setattr(self, attr, cute.size_in_bytes(dtype, layout) * self.cta_group_size)
# fmt: on
tma_load_op = cpasync.CopyBulkTensorTileG2SOp(self.cta_group)
cta_layout_vmnk = cute.tiled_divide(
cute.make_layout(self.cluster_shape_mnk), (tiled_mma_QK.thr_id.shape,)
)
cta_shape = cta_layout_vmnk.shape
def make_tma(make_fn, mX, smem_layout, mma_tiler, tiled_mma):
return make_fn(tma_load_op, mX, smem_layout, mma_tiler, tiled_mma, cta_shape)
A, B = cute.nvgpu.make_tiled_tma_atom_A, cute.nvgpu.make_tiled_tma_atom_B
# (atom_name, tensor_name, make_fn, m, smem_layout, mma_tiler, tiled_mma, kv_only)
# fmt: off
_tma_specs = [
("tma_atom_Q", "tma_tensor_Q", A, mQ, self.sQ_layout, self.mma_tiler_QK, tiled_mma_QK, False),
("tma_atom_Qv", "tma_tensor_Qv", A, mQv, self.sQv_layout, self.mma_tiler_QvV, tiled_mma_QvV, False),
("tma_atom_K", "tma_tensor_K", B, mK, self.sK_layout, self.mma_tiler_QK, tiled_mma_QK, True),
("tma_atom_V", "tma_tensor_V", B, mV, self.sV_layout, self.mma_tiler_QvV, tiled_mma_QvV, True),
("tma_atom_Vt", "tma_tensor_Vt", B, mVt, self.sVt_layout, self.mma_tiler_PVt, tiled_mma_PVt, True),
]
_tmas = {}
for atom_name, tensor_name, make_fn, m, smem_layout, mma_tiler, tiled_mma, kv_only in _tma_specs:
_tmas[atom_name], _tmas[tensor_name] = (
make_tma(make_fn, m, smem_layout, mma_tiler, tiled_mma)
if const_expr((not kv_only or self.use_tma_KV) and m is not None)
else (None, None)
)
(tma_atom_Q, tma_tensor_Q,
tma_atom_Qv, tma_tensor_Qv,
tma_atom_K, tma_tensor_K,
tma_atom_V, tma_tensor_V,
tma_atom_Vt, tma_tensor_Vt) = _tmas.values()
# fmt: on
tma_store_op = cpasync.CopyBulkTensorTileS2GOp()
self.ragged_tma_O = (
self.use_tma_O
and self.is_varlen_q
and self.pack_gqa
and self.cta_tile_m % self.qhead_per_kvhead == 0
)
make_tiled_tma_atom_fn = (
partial(make_packgqa_tiled_tma_atom, qhead_per_kvhead=self.qhead_per_kvhead, head_idx=2)
if const_expr(self.ragged_tma_O)
else cpasync.make_tiled_tma_atom
)
# ==== Set up P smem -> gmem tma store ====
# S<3,4,3> o 0 o ((8,8),(64,2),(1,1)):((64,512),(1,4096),(0,0))
sP_layout_out = sm100_utils.make_smem_layout_epi(
self.dtype_P, self.p_layout, self.tile_P, self.num_stages_P
)
if const_expr(self.store_P):
# TODO: add asserts
mP_tma = mP_og if const_expr(self.ragged_tma_O) else mP
if const_expr(self.ragged_tma_O):
mP_tma = copy_utils.create_ragged_tensor_for_tma(
mP_tma, ragged_dim=0, ptr_shift=True
)
tma_atom_P, tma_tensor_P = make_tiled_tma_atom_fn(
tma_store_op, mP_tma, cute.select(sP_layout_out, mode=[0, 1]), self.tile_P
)
else:
tma_atom_P = None
tma_tensor_P = None
# ==== Set up Oi smem -> gmem tma store ====
self.overlap_sO_sV = True
if const_expr(self.overlap_sO_sV):
num_stages_sO = self.num_stages_V
else:
num_stages_sO = self.num_hdimv_splits
sO_layout = sm100_utils.make_smem_layout_epi(
self.dtype_O, self.o_layout, self.epi_tile, num_stages_sO
)
if const_expr(self.use_tma_O):
mO_tma = mO_og if const_expr(self.ragged_tma_O) else mO
if const_expr(self.ragged_tma_O):
mO_tma = copy_utils.create_ragged_tensor_for_tma(
mO_tma, ragged_dim=0, ptr_shift=True
)
tma_atom_O, tma_tensor_O = make_tiled_tma_atom_fn(
tma_store_op, mO_tma, cute.select(sO_layout, mode=[0, 1]), self.epi_tile
)
else:
tma_atom_O = None
tma_tensor_O = None
# ==== Set up Oi rmem -> gmem copy ====
universal_copy_bits = 128
atom_universal_copy = cute.make_copy_atom(
cute.nvgpu.CopyUniversalOp(),
self.dtype_O,
num_bits_per_copy=universal_copy_bits,
)
thread_layout_O_r2g = cute.make_layout((64, 2), stride=(1, 64))
value_layout_O_r2g = cute.make_layout(
(1, self.hdimv // self.num_hdimv_splits // self.cta_group_size)
)
tiled_copy_O_r2g = cute.make_tiled_copy_tv(
atom=atom_universal_copy,
thr_layout=thread_layout_O_r2g,
val_layout=value_layout_O_r2g,
)
# ==== Allocate shared memory ====
SharedStorage = self._get_shared_storage_cls()
# ==== Tile scheduler ====
TileScheduler = self.TileScheduler
batch_size_for_sched = (
cute.size(mQv.shape[3]) if const_expr(mCuSeqlensQ is None)
else cute.size(mCuSeqlensQ.shape[0] - 1) if self.use_varlen_scheduler
else 1
)
tile_sched_args = TileSchedulerArguments(
num_block=cute.ceil_div(cute.size(mQv.shape[0]), self.cluster_tile_m),
num_head=cute.size(mQv.shape[2]),
num_batch=batch_size_for_sched,
num_splits=1, # todo: split_kv
seqlen_k=cute.size(mV.shape[0])
if const_expr(mPageTable is None)
else cute.size(mV.shape[0]) * cute.size(mPageTable.shape[1]),
headdim=self.hdim,
headdim_v=self.hdimv,
total_q=cute.size(mQv.shape[0])
if const_expr(mCuSeqlensQ is not None)
else cute.size(mQv.shape[0]) * cute.size(mQv.shape[3]),
tile_shape_mn=(self.cta_tile_m, self.tile_n),
mCuSeqlensQ=mCuSeqlensQ,
mSeqUsedQ=mSeqUsedQ,
qhead_per_kvhead_packgqa=self.qhead_per_kvhead if const_expr(self.pack_gqa) else 1,
element_size=self.dtype_K.width // 8,
is_persistent=self.is_persistent,
# lpt=self.is_causal or self.is_local,
lpt=False,
is_split_kv=False,
cluster_shape_mn=self.cluster_shape_mn,
use_cluster_idx=True,
)
tile_sched_params = TileScheduler.to_underlying_arguments(
tile_sched_args, scheduling_mode=self.scheduling_mode
)
self.tile_scheduler_cls = TileScheduler
grid_dim = TileScheduler.get_grid_shape(tile_sched_params)
fa_printf(1, "grid = {}", grid_dim)
# ==== Named Barrier ====
self.cpasync_barrier = cutlass.pipeline.NamedBarrier(
barrier_id=int(NamedBarrierFwdSm100_MLA2CTA.Cpasync),
num_threads=self.num_cpasync_load_threads,
)
self.softmax_barrier = cutlass.pipeline.NamedBarrier(
barrier_id=int(NamedBarrierFwdSm100_MLA2CTA.Softmax),
num_threads=self.num_softmax_threads,
)
self.epi_barrier = cutlass.pipeline.NamedBarrier(
barrier_id=int(NamedBarrierFwdSm100_MLA2CTA.Epilogue),
num_threads=self.num_epilogue_threads,
)
# softmax -> correction
self.sm_stats_barrier_full = cutlass.pipeline.NamedBarrier(
barrier_id=int(NamedBarrierFwdSm100_MLA2CTA.SoftmaxStatsFull),
num_threads=self.num_softmax_threads + self.num_epilogue_threads,
)
self.sm_stats_barrier_empty = cutlass.pipeline.NamedBarrier(
barrier_id=int(NamedBarrierFwdSm100_MLA2CTA.SoftmaxStatsEmpty),
num_threads=self.num_softmax_threads + self.num_epilogue_threads,
)
LOG2_E = math.log2(math.e)
softmax_scale_log2 = softmax_scale * LOG2_E
# ==== Launch kernel ====
self.kernel(
tma_tensor_Q,
tma_tensor_Qv,
tma_tensor_K if self.use_tma_KV else mK,
tma_tensor_V if self.use_tma_KV else mV,
tma_tensor_Vt if self.use_tma_KV else mVt,
tma_tensor_O if self.use_tma_O else mO,
tma_tensor_P,
mLSE,
mRowMax,
mCuSeqlensQ,
mCuSeqlensK,
mSeqUsedQ,
mSeqUsedK,
mIndexTopk,
mPageTable,
tma_atom_Q,
tma_atom_Qv,
tma_atom_K,
tma_atom_V,
tma_atom_Vt,
tma_atom_O,
tma_atom_P,
tiled_copy_O_r2g,
self.sQ_layout_staged,
self.sK_layout_staged,
self.sQv_layout_staged,
self.sV_layout_staged,
self.sVt_layout_staged,
self.sP_layout_staged,
self.sStats_layout,
self.sScale_layout,
self.sBitmask_layout,
sO_layout,
sP_layout_out,
tiled_mma_QK,
tiled_mma_QvV,
tiled_mma_PVt,
softmax_scale,
softmax_scale_log2,
topk_length_dynamic,
tile_sched_params,
SharedStorage,
).launch(
grid=grid_dim,
block=(
self.num_threads,
1,
1,
),
cluster=self.cluster_shape_mnk,
smem=SharedStorage.size_in_bytes(),
stream=stream,
)
@cute.kernel
def kernel(
self,
mQ: Optional[cute.Tensor],
mQv: cute.Tensor,
mK: Optional[cute.Tensor],
mV: cute.Tensor,
mVt: cute.Tensor,
mO: cute.Tensor,
mP: Optional[cute.Tensor],
mLSE: Optional[cute.Tensor],
mRowMax: Optional[cute.Tensor],
mCuSeqlensQ: Optional[cute.Tensor],
mCuSeqlensK: Optional[cute.Tensor],
mSeqUsedQ: Optional[cute.Tensor],
mSeqUsedK: Optional[cute.Tensor],
mIndexTopk: Optional[cute.Tensor],
mPageTable: Optional[cute.Tensor],
tma_atom_Q: cute.CopyAtom,
tma_atom_Qv: cute.CopyAtom,
tma_atom_K: Optional[cute.CopyAtom],
tma_atom_V: Optional[cute.CopyAtom],
tma_atom_Vt: Optional[cute.CopyAtom],
tma_atom_O: Optional[cute.CopyAtom],
tma_atom_P: Optional[cute.CopyAtom],
tiled_copy_O_r2g: cute.TiledCopy,
sQ_layout_staged: cute.ComposedLayout,
sK_layout_staged: cute.ComposedLayout,
sQv_layout_staged: cute.ComposedLayout,
sV_layout_staged: cute.ComposedLayout,
sVt_layout_staged: cute.ComposedLayout,
sP_layout_staged: cute.ComposedLayout,
sStats_layout: cute.Layout,
sScale_layout: cute.Layout,
sBitmask_layout: cute.Layout,
sO_layout: cute.ComposedLayout,
sP_layout_out: cute.ComposedLayout,
tiled_mma_QK: cute.TiledMma,
tiled_mma_QvV: cute.TiledMma,
tiled_mma_PVt: cute.TiledMma,
softmax_scale: Float32,
softmax_scale_log2: Float32,
topk_length_dynamic: Optional[Int32],
tile_sched_params: ParamsBase,
SharedStorage: cutlass.Constexpr[Callable],
):
warp_idx = cute.arch.make_warp_uniform(cute.arch.warp_idx())
cta_layout_vmnk = cute.tiled_divide(
cute.make_layout(self.cluster_shape_mnk), (tiled_mma_QvV.thr_id.shape,)
)
cta_rank_in_cluster = cute.arch.make_warp_uniform(cute.arch.block_idx_in_cluster())
mma_tile_coord_v = cta_rank_in_cluster % cute.size(tiled_mma_QvV.thr_id.shape)
is_leader_cta = mma_tile_coord_v == 0
# ==== Allocate SMEM ====
smem = cutlass.utils.SmemAllocator()
storage = smem.allocate(SharedStorage)
# ==== TMEM stuff ====
tmem_alloc_barrier = pipeline.NamedBarrier(
barrier_id=int(NamedBarrierFwdSm100_MLA2CTA.TmemPtr),
num_threads=self.num_mma_threads + self.num_softmax_threads + self.num_epilogue_threads,
)
tmem = cutlass.utils.TmemAllocator(
storage.tmem_holding_buf.ptr,
barrier_for_retrieve=tmem_alloc_barrier,
allocator_warp_id=self.mma_warp_id,
is_two_cta=self.use_2cta_instrs,
two_cta_tmem_dealloc_mbar_ptr=storage.tmem_dealloc_mbar.ptr,
)
# ==== Prefetch TMA descriptors ====
if warp_idx == self.load_warp_id:
if const_expr(self.has_qk):
cpasync.prefetch_descriptor(tma_atom_Q)
cpasync.prefetch_descriptor(tma_atom_Qv)
if const_expr(self.use_tma_KV):
if const_expr(self.has_qk):
cpasync.prefetch_descriptor(tma_atom_K)
cpasync.prefetch_descriptor(tma_atom_V)
cpasync.prefetch_descriptor(tma_atom_Vt)
if const_expr(self.use_tma_O):
cpasync.prefetch_descriptor(tma_atom_O)
# ==== Construct pipelines ====
tma_warp = pipeline.CooperativeGroup(pipeline.Agent.Thread, 1)
mma_warp = pipeline.CooperativeGroup(pipeline.Agent.Thread, 1)
sm_threads = pipeline.CooperativeGroup(pipeline.Agent.Thread, self.num_softmax_threads)
epi_threads = pipeline.CooperativeGroup(pipeline.Agent.Thread, self.num_epilogue_threads)
sm_threads_cluster = pipeline.CooperativeGroup(
pipeline.Agent.Thread, self.num_softmax_threads * self.cta_group_size
)
epi_threads_cluster = pipeline.CooperativeGroup(
pipeline.Agent.Thread, self.num_epilogue_threads * self.cta_group_size
)
TmaUmma = pipeline.PipelineTmaUmma
AsyncUmma = pipeline.PipelineAsyncUmma
UmmaAsync = pipeline.PipelineUmmaAsync
Async = pipeline.PipelineAsync
def make_pipeline(cls, mbar_ptr, num_stages, producer, consumer, tx_count=None):
return cls.create(
barrier_storage=mbar_ptr.data_ptr(),
num_stages=num_stages,
producer_group=producer,
consumer_group=consumer,
defer_sync=True,
**({"cta_layout_vmnk": cta_layout_vmnk} if cls is not Async else {}),
**({"tx_count": tx_count} if tx_count is not None else {}),
)
# Unconditional pipelines
# fmt: off
pipeline_Q = None
if const_expr(self.has_qk):
pipeline_Q = make_pipeline(TmaUmma, storage.mbar_ptr_Q, self.num_stages_Q, tma_warp, mma_warp, self.tma_copy_bytes_Q)
pipeline_Qv = make_pipeline(TmaUmma, storage.mbar_ptr_Qv, self.num_stages_Qv, tma_warp, mma_warp, self.tma_copy_bytes_Qvi)
pipeline_S = make_pipeline(UmmaAsync, storage.mbar_ptr_S, self.num_stages_S, mma_warp, sm_threads_cluster)
pipeline_P = make_pipeline(AsyncUmma, storage.mbar_ptr_P, self.num_stages_P, sm_threads_cluster, mma_warp)
pipeline_O0 = make_pipeline(UmmaAsync, storage.mbar_ptr_O0, self.num_stages_Oi, mma_warp, epi_threads_cluster)
pipeline_O1 = make_pipeline(UmmaAsync, storage.mbar_ptr_O1, self.num_stages_Oi, mma_warp, epi_threads_cluster)
pipeline_sm_stats = make_pipeline(Async, storage.mbar_ptr_sm_stats, self.num_stages_sm_stats, sm_threads, epi_threads)
# K/V pipelines: type and producer depend on use_tma_KV
if const_expr(self.use_tma_KV):
pipeline_K = None
if const_expr(self.has_qk):
pipeline_K = make_pipeline(TmaUmma, storage.mbar_ptr_K, self.num_stages_K, tma_warp, mma_warp, self.tma_copy_bytes_K)
pipeline_V = make_pipeline(TmaUmma, storage.mbar_ptr_V, self.num_stages_V, tma_warp, mma_warp, self.tma_copy_bytes_Vi)
pipeline_K_cpasync = pipeline_V_cpasync = pipeline_bitmask = None
else:
cpasync_load_threads = pipeline.CooperativeGroup(pipeline.Agent.Thread, self.num_cpasync_load_threads)
relay_warps_cluster = pipeline.CooperativeGroup(pipeline.Agent.Thread, self.cta_group_size)
relay_threads = pipeline.CooperativeGroup(pipeline.Agent.Thread, self.num_relay_threads)
pipeline_K = pipeline_K_cpasync = None
if const_expr(self.has_qk):
pipeline_K = make_pipeline(AsyncUmma, storage.mbar_ptr_K, self.num_stages_K, relay_warps_cluster, mma_warp)
pipeline_K_cpasync = make_pipeline(Async, storage.mbar_ptr_K_cpasync, self.num_stages_K, cpasync_load_threads, relay_threads)
pipeline_V = make_pipeline(AsyncUmma, storage.mbar_ptr_V, self.num_stages_V, relay_warps_cluster, mma_warp)
pipeline_V_cpasync = make_pipeline(Async, storage.mbar_ptr_V_cpasync, self.num_stages_V, cpasync_load_threads, relay_threads)
pipeline_bitmask = (
make_pipeline(Async, storage.mbar_ptr_bitmask, self.num_stages_bitmask, cpasync_load_threads, sm_threads)
if const_expr(self.is_topk_gather and not self.disable_bitmask) else None
)
# fmt: on
sO_empty_mbar_ptr = None
if const_expr(self.use_tma_O and self.overlap_sO_sV):
sO_empty_mbar_ptr = storage.sO_empty_mbar_ptr
if warp_idx == 0:
cute.arch.mbarrier_init(sO_empty_mbar_ptr, 1)
pipeline.pipeline_init_arrive(cluster_shape_mn=cta_layout_vmnk, is_relaxed=True)
# ==== Get SMEM tensors ====
# fmt: off
sQ, sK, sQv, sV, sVt, sP, sP_out = (
store.get_tensor(layout.outer, swizzle=layout.inner)
if const_expr(store._size > 0) else None
for store, layout in [
(storage.sQ, sQ_layout_staged),
(storage.sK, sK_layout_staged),
(storage.sQv, sQv_layout_staged),
(storage.sV, sV_layout_staged),
(storage.sV, sVt_layout_staged), # sVt reuses sV storage
(storage.sP, sP_layout_staged),
(storage.sP, sP_layout_out),
]
)
# fmt: on
sRowMax = storage.sRowMax.get_tensor(sStats_layout)
sRowSum = storage.sRowSum.get_tensor(sStats_layout)
sScale = storage.sScale.get_tensor(sScale_layout)
sBitmask = None
if const_expr(self.is_topk_gather):
sBitmask = storage.sBitmask.get_tensor(sBitmask_layout)
if const_expr(self.overlap_sO_sV):
sO_iterator = sV.iterator
assert cute.cosize(sO_layout) <= cute.cosize(sV_layout_staged)
else:
sO_iterator = sQv.iterator
assert cute.cosize(sO_layout) <= cute.cosize(sQv_layout_staged)
sO = cute.make_tensor(
cute.recast_ptr(sO_iterator, sO_layout.inner, self.dtype_O), sO_layout.outer
)
# ==== Get thread MMAs and accumulator fragments ====
thr_mma_QK = tiled_mma_QK.get_slice(mma_tile_coord_v)
thr_mma_QvV = tiled_mma_QvV.get_slice(mma_tile_coord_v)
thr_mma_PVt = tiled_mma_PVt.get_slice(mma_tile_coord_v)
acc_shape_S = thr_mma_QvV.partition_shape_C(self.mma_tiler_QvV[:2])
tStS_fake = thr_mma_QvV.make_fragment_C(cute.append(acc_shape_S, self.num_stages_S))
acc_shape_Oi = thr_mma_PVt.partition_shape_C(self.mma_tiler_PVt[:2])
tOtO0_fake = thr_mma_PVt.make_fragment_C(acc_shape_Oi)
tOtO1_fake = thr_mma_PVt.make_fragment_C(acc_shape_Oi)
block_info = BlockInfo(
self.cta_tile_m * self.cta_group_size,
self.tile_n,
is_causal=self.is_causal,
qhead_per_kvhead_packgqa=self.qhead_per_kvhead if const_expr(self.pack_gqa) else 1,
)
SeqlenInfoCls = partial(
SeqlenInfoQK.create,
seqlen_q_static=mQv.shape[0] if const_expr(not self.pack_gqa) else mQv.shape[0][1],
seqlen_k_static=mV.shape[0]
if const_expr(mPageTable is None)
else mV.shape[0] * mPageTable.shape[1],
tile_m=self.cta_tile_m,
tile_n=self.tile_n,
mCuSeqlensQ=mCuSeqlensQ,
mCuSeqlensK=mCuSeqlensK,
mSeqUsedQ=mSeqUsedQ,
mSeqUsedK=mSeqUsedK,
)
AttentionMaskCls = partial(
AttentionMask,
self.cta_tile_m * self.cta_group_size,
self.tile_n,
qhead_per_kvhead_packgqa=self.qhead_per_kvhead if const_expr(self.pack_gqa) else 1,
)
if const_expr(self.use_clc_scheduler):
clc_response_ptr = storage.clc_response.data_ptr()
clc_mbar_ptr = storage.clc_mbar_ptr.data_ptr()
clc_pipeline_producer_group = pipeline.CooperativeGroup(pipeline.Agent.Thread)
num_clc_consumer_warps_per_cta = self.num_threads // cute.arch.WARP_SIZE
num_clc_consumer_warps = num_clc_consumer_warps_per_cta * self.cta_group_size
clc_pipeline_consumer_group = pipeline.CooperativeGroup(
pipeline.Agent.Thread, cute.arch.WARP_SIZE * num_clc_consumer_warps
)
clc = SchedulerState.create_clc(
hw_scheduler=ClcDynamicPersistentTileScheduler.create(
self.tile_scheduler_cls.clc_problem_shape(tile_sched_params),
cute.arch.block_idx(),
cute.arch.grid_dim(),
clc_response_ptr,