forked from flashinfer-ai/flashinfer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmla_sm120.cu
More file actions
1845 lines (1670 loc) · 73.2 KB
/
Copy pathmla_sm120.cu
File metadata and controls
1845 lines (1670 loc) · 73.2 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
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights
* reserved. SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "defines.h"
#include "mha.h"
#if IS_MLA
#include "barriers.cuh"
#include "mhaUtils.cuh"
#include "mha_components.cuh"
#include "mha_stdheaders.cuh"
#include "mla_sm120.cuh"
#include "mma.cuh"
#include "tma.h"
#include "utils.cuh"
#include "utils.h"
#ifndef GENERATE_CUBIN
#include <cuda_runtime.h>
#include "hostUtils.h"
#include "tensorMap.h"
#endif
#define USE_REG_Q 1
__constant__ constexpr XQAKernelType kernelType = XQAKernelType::kSM120_MLA;
inline constexpr bool allowMultipleInputTokens = true;
inline constexpr uint32_t partElemsK = 64; // @fixme: change this to 128 to save L2 traffic
inline constexpr uint32_t nbKParts = exactDiv(validElemsPerKHead, partElemsK);
inline constexpr uint32_t nbQParts = nbKParts;
inline constexpr uint32_t tokensPerTile = 64;
inline constexpr uint32_t partElemsV = 128;
inline constexpr uint32_t nbVSplit = 2;
inline constexpr uint32_t gemm1V = exactDiv(validElemsPerVHead, nbVSplit);
inline constexpr uint32_t nbProducerCtasPerCga = nbVSplit;
inline constexpr uint32_t multiBlockMinNbTilesPerCta = 2;
inline constexpr uint32_t multiBlockMinNbTiles = multiBlockMinNbTilesPerCta * 2;
using MathElem = CacheElem;
inline constexpr uint32_t mathElemBytes = sizeof(MathElem);
inline constexpr uint32_t grainsPerPartK = exactDiv(partElemsK * mathElemBytes, grainBytes);
inline constexpr uint32_t grainElems = exactDiv(grainBytes, mathElemBytes);
inline constexpr float xScale = 1.f / kE4M3_MAX;
__constant__ constexpr float rcpXScale = kE4M3_MAX;
inline constexpr uint32_t nbRegsForIOWarps = 32;
inline constexpr uint32_t nbRegsForMathWarps = 232;
inline constexpr bool computeRowSumFromF8 = true;
struct KVTilePartLoader {
static_assert(tokensPerPage % tokensPerTile == 0 || tokensPerTile % tokensPerPage == 0);
static inline constexpr uint32_t nbPagesPerTile =
tokensPerTile >= tokensPerPage ? exactDiv(tokensPerTile, tokensPerPage) : 1;
static inline constexpr uint32_t const nbKHeads = 1;
KVCacheList<usePagedKVCache> const& cacheList;
uint32_t const idxReq;
static inline constexpr uint32_t const idxHeadGrp = 0;
CUtensorMap const& tensorMap;
// if greater than 1, then we need unrolling for the loading loop. Seems 1 is fine for latency.
static inline constexpr uint32_t nbPageBuffers = 1;
uint32_t const nbPages; // for bound check
Vec<KVCachePageIndex, nbPagesPerTile> pageBuffers[nbPageBuffers];
uint32_t idxTileRef = ~0U; // idxTile used to load the pages
uint32_t const baseOffset;
__device__ KVTilePartLoader(KVCacheList<usePagedKVCache> const& cacheList, uint32_t idxReq,
CUtensorMap const& tensorMap, uint32_t nbPages);
// tensorMap is for one whole page ([nbKHeads*tokensPerPage][headElems]) or whole cache
template <uint32_t nbTokens, uint32_t grainsPerPart, bool alignedForSwizzle>
__device__ void loadData(Array2D<LdGrain, nbTokens, grainsPerPart, alignedForSwizzle>& dst,
uint32_t idxTile, uint32_t idxElemBeg, CtaBarrier& bar,
uint32_t idxPageBuf);
__device__ void loadPages(uint32_t idxTile, uint32_t idxPageBuf);
};
__device__ inline KVTilePartLoader::KVTilePartLoader(KVCacheList<usePagedKVCache> const& cacheList,
uint32_t idxReq, CUtensorMap const& tensorMap,
uint32_t nbPages)
: cacheList{cacheList},
idxReq{idxReq},
tensorMap{tensorMap},
nbPages{nbPages},
baseOffset{idxReq * cacheList.maxNbPagesPerSeq} {
#pragma unroll
for (auto& pageBuffer : pageBuffers) {
pageBuffer.fill(kBAD_PAGE_INDEX);
}
}
// tensorMap is for one whole page ([nbKHeads*tokensPerPage][headElems]) or whole cache
template <uint32_t nbTokens, uint32_t grainsPerPart, bool alignedForSwizzle>
__device__ inline void KVTilePartLoader::loadData(
Array2D<LdGrain, nbTokens, grainsPerPart, alignedForSwizzle>& dst, uint32_t idxTile,
uint32_t idxElemBeg, CtaBarrier& bar, uint32_t idxPageBuf) {
static_assert(nbTokens == tokensPerTile);
assert(idxTile == idxTileRef);
auto const& pages = pageBuffers[idxPageBuf];
if constexpr (nbTokens < tokensPerPage) {
assert(nbPagesPerTile == 1);
uint32_t const offset = nbTokens * (idxTile % exactDiv(tokensPerPage, nbTokens));
if (warpElectSync()) {
tma::loadAsync(&dst, tensorMap, DimsLE<4>{idxElemBeg, idxHeadGrp, offset, (uint32_t)pages[0]},
bar);
}
} else {
#pragma unroll
for (uint32_t i = 0; i < nbPagesPerTile; i++) {
if (warpElectSync()) {
tma::loadAsync(&dst(tokensPerPage * i, 0), tensorMap,
DimsLE<4>{idxElemBeg, idxHeadGrp, 0, (uint32_t)pages[i]}, bar);
}
}
}
}
__device__ inline void KVTilePartLoader::loadPages(uint32_t idxTile, uint32_t idxPageBuf) {
uint32_t const idxPageBeg = tokensPerTile >= tokensPerPage
? nbPagesPerTile * idxTile
: idxTile / exactDiv(tokensPerPage, tokensPerTile);
auto& pages = pageBuffers[idxPageBuf];
#pragma unroll
for (uint32_t i = 0; i < nbPagesPerTile; i++) {
uint32_t const idxPage = idxPageBeg + i;
pages[i] =
idxPage < nbPages ? cacheList.kvCachePageList[baseOffset + idxPage] : kBAD_PAGE_INDEX;
}
idxTileRef = idxTile;
}
using Mat16x32 = Vec<uint32_t, 4>;
template <uint32_t srcRows, uint32_t srcCols>
class Mat16x32Loader {
public:
using Src = Array2D<LdGrain, srcRows, srcCols>;
// default r and c are for mat A.
__device__ inline Mat16x32Loader(Src const& src, uint32_t baseRow, uint32_t idxInstK,
uint32_t r = laneId() % 16, uint32_t c = laneId() / 16)
: src{src}, baseRow{baseRow}, idxInstK{idxInstK}, r{r}, c{c}, basePtr{getPtrRef(0)} {
static_assert((grainBytes * srcCols * qmmaShape.m) % 1024 == 0);
}
__device__ inline Mat16x32 load(uint32_t idxInstM) const {
return ldmatrix<false, 4>(getPtr(idxInstM));
}
template <uint32_t tileM>
__device__ inline Vec<Mat16x32, exactDiv(tileM, qmmaShape.m)> loadWholeCol() const {
uint32_t const nbInstM = exactDiv(tileM, qmmaShape.m);
Vec<Mat16x32, nbInstM> ret;
#pragma unroll
for (uint32_t i = 0; i < nbInstM; i++) {
ret[i] = load(i);
}
return ret;
}
__device__ inline LdGrain const* getPtr(uint32_t idxInstM) const {
return checkedVal(basePtr + idxInstM * qmmaShape.m * srcCols, getPtrRef(idxInstM));
}
private:
__device__ inline LdGrain const* getPtrRef(uint32_t idxInstM) const {
return &src.template at<true>(baseRow + idxInstM * qmmaShape.m + r,
idxInstK * exactDiv(qmmaShape.k, grainElems) + c);
}
Src const& src;
uint32_t const baseRow;
uint32_t const idxInstK;
uint32_t const r;
uint32_t const c;
LdGrain const* const basePtr;
};
using InstAcc = Array2D<float, 2, 2>;
using XBuffer = Array2D<LdGrain, headGrpSize, exactDiv(mathElemBytes* tokensPerTile, grainBytes)>;
struct CgaXBuffer {
XBuffer x;
Vec<float, headGrpSize> rowSum;
Vec<float, headGrpSize> rowMaxLog2e;
};
struct PingPongMutex {
using ShmStorage = CtaBarrier[2];
ShmStorage& barriers;
uint32_t const idxGrp;
bool skipWait = false;
static __device__ inline void initStorage(ShmStorage& barriers, uint32_t thrdsPerGrp) {
new (&barriers[0]) CtaBarrier(thrdsPerGrp);
new (&barriers[1]) CtaBarrier(thrdsPerGrp);
barriers[0].arrive(thrdsPerGrp);
}
__device__ inline PingPongMutex(ShmStorage& shmStorage, uint32_t idxGrp)
: barriers{shmStorage}, idxGrp{idxGrp} {}
__device__ inline void test_lock(uint32_t iter) {
skipWait = barriers[idxGrp].test_wait_parity(toParity<1>(iter));
}
__device__ inline void lock(uint32_t iter) {
if (!skipWait) {
barriers[idxGrp].wait_parity(toParity<1>(iter));
}
}
__device__ inline void unlock() {
barriers[idxGrp ^ 1U].arrive();
skipWait = false;
}
};
struct PartialResult {
static constexpr uint32_t nbChunks = 4;
static constexpr uint32_t nbRowsPerChunk = exactDiv(headGrpSize, nbChunks);
struct Chunk {
Vec<OutputHead, nbRowsPerChunk> data;
Vec<float, nbRowsPerChunk> rowSum;
Vec<float, nbRowsPerChunk> rowMaxLog2e;
};
Chunk chunks[nbChunks];
};
constexpr uint32_t nbMathWarpsA = 8;
constexpr uint32_t nbComputeWarpsB = 8;
constexpr uint32_t nbMathGrpsA = 2;
constexpr uint32_t nbMathWarpsB = 8;
constexpr uint32_t nbMultiBlockBufs = 2;
constexpr uint32_t multiBlockMathWarps = 8;
constexpr bool useRegQ = USE_REG_Q;
struct SharedMemA {
static inline constexpr uint32_t nbKBufs = 12;
static inline constexpr uint32_t regQParts = (useRegQ ? 4 : 0);
static inline constexpr uint32_t shmQParts = nbQParts - regQParts;
using ShmQPart = Array2D<LdGrain, headGrpSize, grainsPerPartK>;
using ShmKPart = Array2D<LdGrain, tokensPerTile, grainsPerPartK>;
Vec<ShmQPart, shmQParts> q;
ShmKPart k[nbKBufs];
// single buffer reused by two groups. sendX() warp will arbitrate the order of x buffer access
// via two xBars.
CgaXBuffer x;
// scaled by log2e. Write by last CGA iteration (from the other producer CTA) and read by current
// producer CTA.
Vec<float, headGrpSize> rowMaxLog2e;
// sync rowMaxLog2e between two producer CTAs and .consumed means the buffer for next iteration
// (in next producer) is ready. The 4 groups from 2 producers CTAs form a ring
CgaBarrier rowMaxLog2eBar[nbMathGrpsA];
PingPongMutex::ShmStorage tensorCoreMutex;
CtaBarrierPair kBars[nbKBufs];
static inline constexpr uint32_t nbXBars = nbMathGrpsA;
CtaBarrierPair xBars[nbXBars];
#if USE_REG_Q
CtaBarrierPair regQBar;
#endif
CtaBarrier shmQBar;
CgaBarrier cgaXBufConsumed; // for X
CtaBarrierPair multiBlockBars[nbMultiBlockBufs];
__device__ inline void invalidateBarriers(uint32_t thrdIdx) {
constexpr uint32_t nbBars = (useRegQ ? 12 : 10) + 2 * (nbKBufs + nbXBars);
#ifndef __CUDACC_RTC__
constexpr uint32_t nbBarsRef =
exactDiv(offsetof(SharedMemA, qkScaleLog2e) - offsetof(SharedMemA, rowMaxLog2eBar), 8);
static_assert(nbBars == nbBarsRef);
#endif
if (thrdIdx < nbBars) {
reinterpret_cast<CtaBarrier*>(&rowMaxLog2eBar[0])[thrdIdx].~CtaBarrier();
}
}
__device__ inline Vec<PartialResult::Chunk, nbMultiBlockBufs>& getMultiBlockBufs() {
#ifndef __CUDACC_RTC__
assert(sizeof(Vec<PartialResult::Chunk, nbMultiBlockBufs>) <
offsetof(SharedMemA, rowMaxLog2eBar));
#endif
return *reinterpret_cast<Vec<PartialResult::Chunk, nbMultiBlockBufs>*>(this);
}
float qkScaleLog2e;
bool isLastSubSeq;
};
struct SharedMemB {
static inline constexpr uint32_t nbXVBufs = 2;
static inline constexpr uint32_t nbXBufs = nbXVBufs;
static inline constexpr uint32_t nbVBufs = nbXVBufs;
using VBuffer = Vec<Array2D<LdGrain, tokensPerTile, exactDiv(partElemsV, grainElems)>,
exactDiv(gemm1V, partElemsV)>;
// x and v are using gemmK=128 per iteration. If we see high pressure on shared memory capacity,
// we can change to 64 in the future.
struct XVBuffer {
VBuffer v;
CgaXBuffer x;
uint8_t
pad[headGrpSize * 128 * 2 - sizeof(VBuffer) - sizeof(CgaXBuffer)]; // for output swizzling
};
XVBuffer xv[nbXVBufs];
__device__ inline XBuffer& x(uint32_t idx) { return xv[idx].x.x; }
__device__ inline VBuffer& v(uint32_t idx) { return xv[idx].v; }
__device__ inline Vec<float, headGrpSize>& xRowSum(uint32_t idx) { return xv[idx].x.rowSum; }
__device__ inline Vec<float, headGrpSize>& xRowMaxLog2e(uint32_t idx) {
return xv[idx].x.rowMaxLog2e;
}
static inline constexpr uint32_t nbAccRowMaxSumCopies = 2;
Vec<float, headGrpSize> accRowMaxLog2e[nbAccRowMaxSumCopies];
Vec<float, headGrpSize> accRowSum[nbAccRowMaxSumCopies];
CtaBarrierPair xBars[nbXBufs];
CtaBarrierPair vBars[nbVBufs];
CgaBarrier cgaXBufProduced[nbProducerCtasPerCga];
CtaBarrier mathWarpsBar;
CtaBarrierPair multiBlockBars[nbMultiBlockBufs];
__device__ inline void invalidateBarriers(uint32_t thrdIdx) {
constexpr uint32_t nbBars = 15;
#ifndef __CUDACC_RTC__
constexpr uint32_t nbBarsRef =
exactDiv(offsetof(SharedMemB, isLastSubSeq) - offsetof(SharedMemB, xBars), 8);
static_assert(nbBars == nbBarsRef);
#endif
if (thrdIdx < nbBars) {
reinterpret_cast<CtaBarrier*>(&xBars[0])[thrdIdx].~CtaBarrier();
}
}
__device__ inline Vec<PartialResult::Chunk, nbMultiBlockBufs>& getMultiBlockBufs() {
#ifndef __CUDACC_RTC__
static_assert(sizeof(Vec<PartialResult::Chunk, nbMultiBlockBufs>) <
offsetof(SharedMemB, xBars));
#endif
return *reinterpret_cast<Vec<PartialResult::Chunk, nbMultiBlockBufs>*>(this);
}
bool isLastSubSeq;
};
__device__ void mergePartialOutputs(uint32_t& semaphore,
Vec<OutputHead, PartialResult::nbRowsPerChunk>& dst,
PartialResult const* reqPartialResults, uint32_t nbSubSeq,
uint32_t ctaRank, uint32_t warpRank, uint2 warpIdx,
void* sharedMem);
struct KernelArgs {
CUtensorMap const& tensorMapQ; // MhaIOHead[nbQHeads * totalNbInputTokens]
CUtensorMap const& tensorMapK;
CUtensorMap const& tensorMapV;
float const& qScale;
OutputHead* __restrict__ const& output; // [totalNbIntputTokens][nbQHeads]
KVCacheList<usePagedKVCache> const& cacheList;
uint32_t const& batchSize;
float kvCacheScale; // Same scale for K and V cache. Used only for int8/fp8 KV cache.
Vec<CgaXBuffer, nbProducerCtasPerCga>* __restrict__ const&
cgaXBuf; // [totalNbInputTokens][maxNbSubSeq]
uint32_t* __restrict__ const& semaphores; // [totalNbInputTokens]
PartialResult* __restrict__ const& partialResults; // [totalNbInputTokens][maxNbSubSeq]
};
struct Producer {
static inline constexpr uint32_t nbMathGrps = nbMathGrpsA;
static inline constexpr uint32_t nbMathWarps = nbMathWarpsA;
static inline constexpr uint32_t nbMathThrds = nbMathWarps * warp_size;
static inline constexpr uint32_t warpsPerGrp = exactDiv(nbMathWarps, nbMathGrps);
static inline constexpr uint32_t thrdsPerGrp = warpsPerGrp * warp_size;
static inline constexpr uint2 warpTile = {tokensPerTile, exactDiv(headGrpSize, warpsPerGrp)};
using WarpAcc = WarpAccT<warpTile.y, warpTile.x>;
using ThrdRegRowMax = ThrdRegRowMaxT<warpTile.y>;
using QuadRegRowMax = QuadRegRowMaxT<warpTile.y>;
KernelArgs const& args;
SharedMemA& smem;
uint32_t const maxNbSubSeq;
uint32_t const idxReq;
uint32_t const idxInputTokenGlobal;
uint32_t const nbSubSeq;
uint32_t const idxSubSeq;
uint32_t const seqLen;
uint32_t const ctaRank;
uint32_t const warpRank;
uint2 const warpIdx;
__device__ inline Producer(KernelArgs const& args, SharedMemA& smem, uint32_t const maxNbSubSeq,
uint32_t const idxReq, uint32_t idxInputTokenGlobal,
uint32_t const seqLen, uint32_t const nbSubSeq,
uint32_t const idxSubSeq, uint32_t ctaRank, uint32_t const warpRank,
uint2 const warpIdx)
: args(args),
smem(smem),
maxNbSubSeq(maxNbSubSeq),
idxReq(idxReq),
idxInputTokenGlobal(idxInputTokenGlobal),
seqLen(seqLen),
nbSubSeq(nbSubSeq),
idxSubSeq(idxSubSeq),
ctaRank(ctaRank),
warpRank(warpRank),
warpIdx(warpIdx) {
#ifndef NDEBUG
if (threadIdx.x == 0) {
asm("st.bulk.weak [%0], %1, 0;\n" ::"l"(&smem), "n"(sizeof(SharedMemA)) : "memory");
}
__syncthreads();
#endif
if (threadIdx.x == 0) {
smem.qkScaleLog2e = args.qScale * args.kvCacheScale * log2e;
}
if (threadIdx.x < headGrpSize) {
smem.rowMaxLog2e[threadIdx.x] = safeInitRowMax;
}
if (warpElectSync()) {
if (warpRank < SharedMemA::nbKBufs) {
auto& b = smem.kBars[warpRank];
b.initialize(1, thrdsPerGrp);
b.consumed.arrive<Scope::CTA, ArriveOrder::RELAXED>(thrdsPerGrp);
}
if (warpRank < SharedMemA::nbXBars) {
auto& b = smem.xBars[warpRank];
b.initialize(thrdsPerGrp, 1);
}
#if USE_REG_Q
if (warpRank == 0) {
smem.regQBar.initialize(1, nbMathThrds);
smem.regQBar.consumed.arrive<Scope::CTA, ArriveOrder::RELAXED>(nbMathThrds);
}
#endif
if (warpRank < nbMathGrpsA) {
auto& b = smem.rowMaxLog2eBar[warpRank];
init(&b, thrdsPerGrp);
}
if (ctaRank == 0 && warpRank == 0) {
smem.rowMaxLog2eBar[0].arrive<Scope::CTA, ArriveOrder::RELAXED>(thrdsPerGrp);
}
if (warpRank == 0) {
init(&smem.shmQBar, 1);
init(&smem.cgaXBufConsumed, 1 * nbVSplit);
smem.cgaXBufConsumed.arrive<Scope::CTA, ArriveOrder::RELAXED>(1 * nbVSplit);
PingPongMutex::initStorage(smem.tensorCoreMutex, thrdsPerGrp);
}
if (nbSubSeq > 1 && warpRank < nbMultiBlockBufs) {
auto& b = smem.multiBlockBars[warpRank];
b.initialize(1, warp_size * multiBlockMathWarps);
b.consumed.arrive<Scope::CTA, ArriveOrder::RELAXED>(warp_size * multiBlockMathWarps);
}
}
clusterBarArrive();
clusterBarWait();
}
__device__ inline ~Producer() {
clusterBarArrive();
clusterBarWait();
smem.invalidateBarriers(threadIdx.x);
}
__device__ inline void run() {
if (warpIdx.y == 2) { // IO warps
asm volatile("setmaxnreg.dec.sync.aligned.u32 %0;\n" ::"n"(nbRegsForIOWarps));
if (warpIdx.x == 0) { // q
loadQ();
} else if (warpIdx.x == 1) { // k
loadK();
} else if (warpIdx.x == 2) { // x
sendX();
}
} else { // Compute warps
asm volatile("setmaxnreg.inc.sync.aligned.u32 %0;\n" ::"n"(nbRegsForMathWarps));
compute();
}
if (nbSubSeq > 1) {
mergePartialOutputs(args.semaphores[idxInputTokenGlobal],
reinterpret_cast<Vec<OutputHead, PartialResult::nbRowsPerChunk>&>(
args.output[headGrpSize * idxInputTokenGlobal +
PartialResult::nbRowsPerChunk * ctaRank]),
args.partialResults + maxNbSubSeq * idxInputTokenGlobal, nbSubSeq,
ctaRank, warpRank, warpIdx, &smem);
}
}
private:
__device__ inline uint32_t iterStride() const { return nbSubSeq * nbProducerCtasPerCga; }
__device__ inline uint32_t idxTileBeg() const {
return nbProducerCtasPerCga * idxSubSeq + ctaRank;
}
__device__ inline uint32_t nbTiles() const { return divUp(seqLen, tokensPerTile); }
__device__ inline SharedMemB& getConsumerShm(uint32_t const idxConsumer) {
return *mapa(reinterpret_cast<SharedMemB*>(&smem), nbProducerCtasPerCga + idxConsumer);
};
static constexpr uint32_t regQPartShmBeg = SharedMemA::shmQParts - SharedMemA::regQParts;
__device__ inline void loadQ() {
#if USE_REG_Q
static_assert(SharedMemA::regQParts <= SharedMemA::shmQParts);
smem.regQBar.consumed.wait_parity(toParity<1>(0));
#pragma unroll 1
for (uint32_t i = 0; i < SharedMemA::regQParts; i++) {
if (warpElectSync()) {
tma::loadAsync(&smem.q[regQPartShmBeg + i], args.tensorMapQ,
DimsLE<2>{partElemsK * i, headGrpSize * idxInputTokenGlobal},
smem.regQBar.produced);
}
}
if (warpElectSync()) {
smem.regQBar.produced.arrive_tx(sizeof(SharedMemA::ShmQPart) * SharedMemA::regQParts);
}
#endif
#pragma unroll 1
for (uint32_t i = 0; i < SharedMemA::shmQParts; i++) {
uint32_t const idxPart = SharedMemA::regQParts + i;
#if USE_REG_Q
if (i == regQPartShmBeg) {
smem.regQBar.consumed.wait_parity(toParity<1>(1));
}
#endif
if (warpElectSync()) {
tma::loadAsync(&smem.q[i], args.tensorMapQ,
DimsLE<2>{partElemsK * idxPart, headGrpSize * idxInputTokenGlobal},
smem.shmQBar);
}
}
if (warpElectSync()) {
smem.shmQBar.arrive_tx(sizeof(SharedMemA::ShmQPart) * SharedMemA::shmQParts);
}
}
__device__ inline void loadK();
__device__ inline void sendX();
__device__ inline void compute() {
uint32_t const grpIdx = warpIdx.y;
uint32_t const tileBaseRow = warpTile.y * warpIdx.x;
PingPongMutex tensorCoreMutex{smem.tensorCoreMutex, grpIdx};
constexpr uint32_t partNbInstK = exactDiv(partElemsK, qmmaShape.k);
using AtomA = Vec<uint32_t, 4>; // for 16x32 data, working as mat A of QMMA.16832
using RegQPartCol = Vec<AtomA, exactDiv(warpTile.y, qmmaShape.m)>;
using RegQPart = Vec<RegQPartCol, partNbInstK>;
using RegQ = Vec<RegQPart, SharedMemA::regQParts>;
constexpr uint32_t tileNbAtomBx2 = exactDiv(tokensPerTile, qmmaShape.n * 2);
using AtomBx2 = Vec<uint32_t, 4>; // one AtomB is 8x32 and AtomBx2 is 16x32
using RegKPartCol = Vec<AtomBx2, tileNbAtomBx2>;
using RegKPart = Vec<RegKPartCol, partNbInstK>;
uint32_t const lane = laneId();
uint32_t const rA = lane % 16;
uint32_t const cA = lane / 16;
uint32_t const rB = (lane / 16) * 8 + lane % 8;
uint32_t const cB = (lane % 16) / 8;
auto loadRegQCol = [&](SharedMemA::ShmQPart const& q, uint32_t idxInstK) -> RegQPartCol {
Mat16x32Loader const loaderQ(q, tileBaseRow, idxInstK, rA, cA);
return loaderQ.loadWholeCol<warpTile.y>();
};
auto loadRegKCol = [&](SharedMemA::ShmKPart const& k, uint32_t idxInstK) -> RegKPartCol {
Mat16x32Loader const loaderK(k, 0, idxInstK, rB, cB);
return loaderK.loadWholeCol<warpTile.x>();
};
auto loadPart = [&](auto const& loadCol, auto const& shmPart) {
mha::conditional_t<mha::is_same_v<SharedMemA::ShmQPart, mha::decay_t<decltype(shmPart)>>,
RegQPart, RegKPart>
regPart;
#pragma unroll
for (uint32_t idxInstK = 0; idxInstK < partNbInstK; idxInstK++) {
regPart[idxInstK] = loadCol(shmPart, idxInstK);
}
return regPart;
};
#if USE_REG_Q
// load regQ
smem.regQBar.produced.wait_parity(toParity<1>(0));
RegQ regQ;
#pragma unroll
for (uint32_t idxPart = 0; idxPart < SharedMemA::regQParts; idxPart++) {
uint32_t const idxBuf = regQPartShmBeg + idxPart;
regQ[idxPart] = loadPart(loadRegQCol, smem.q[idxBuf]);
}
smem.regQBar.consumed.arrive();
#endif
// main loop
#pragma unroll 1
for (uint32_t grpIter = 0; true; grpIter++) {
uint32_t const ctaIter = grpIdx + grpIter * nbMathGrps;
uint32_t const idxTile = idxTileBeg() + iterStride() * ctaIter;
if (idxTile >= nbTiles()) {
break;
}
WarpAcc acc{};
// wait until it's our turn
tensorCoreMutex.lock(grpIter);
BarWaiter kBarWaiter(smem.kBars, ctaIter * nbKParts + 0);
kBarWaiter.testWait();
RegQPart regQBuf;
#if USE_REG_Q
static_assert(SharedMemA::regQParts > 0);
regQBuf[0] = regQ[0][0];
#else
regQBuf[0] = loadRegQCol(smem.q[0], 0);
#endif
kBarWaiter.wait();
RegKPart regKBuf;
regKBuf[0] = loadRegKCol(smem.k[kBarWaiter.idxBuf], 0);
auto shouldTestWait = [](uint32_t idxInstK, uint32_t idxAtomBx2) {
return idxInstK == partNbInstK - 1 && idxAtomBx2 == tileNbAtomBx2 - 2;
};
BarWaiter kBarWaiterNext = kBarWaiter.next();
#if USE_REG_Q
#pragma unroll
for (uint32_t idxPart = 0; idxPart < SharedMemA::regQParts; idxPart++) {
#pragma unroll
for (uint32_t idxInstK = 0; idxInstK < partNbInstK; idxInstK++) {
bool const prefetchNextPart = (idxInstK == partNbInstK - 1);
uint32_t const idxPartPrefetch = prefetchNextPart ? idxPart + 1 : idxPart;
uint32_t const idxInstKPrefetch = prefetchNextPart ? 0 : idxInstK + 1;
bool const prefetch = (!prefetchNextPart || (idxPart < nbKParts - 1));
if (prefetchNextPart) {
kBarWaiter = kBarWaiterNext;
kBarWaiterNext = kBarWaiter.next();
if (prefetch) {
kBarWaiter.wait();
}
}
Mat16x32Loader const loaderK(smem.k[kBarWaiter.idxBuf], 0, idxInstKPrefetch, rB, cB);
#pragma unroll
for (uint32_t idxAtomBx2 = 0; idxAtomBx2 < tileNbAtomBx2; idxAtomBx2++) {
if (idxAtomBx2 == 2 && prefetch) {
if (idxPartPrefetch < SharedMemA::regQParts) {
regQBuf[idxInstKPrefetch] = regQ[idxPartPrefetch][idxInstKPrefetch];
} else {
regQBuf[idxInstKPrefetch] =
loadRegQCol(smem.q[idxPartPrefetch - SharedMemA::regQParts], idxInstKPrefetch);
}
}
AtomBx2 const& atomBx2 = regKBuf[idxInstK][idxAtomBx2];
regKBuf[idxInstKPrefetch][idxAtomBx2] = loaderK.load(idxAtomBx2);
if (shouldTestWait(idxInstKPrefetch, idxAtomBx2) && prefetch) {
kBarWaiterNext.testWait();
}
#pragma unroll
for (uint32_t i = 0; i < WarpAcc::rows; i++) {
#pragma unroll
for (uint32_t j = 0; j < 2; j++) {
mma<__nv_fp8_e4m3>(reinterpret_cast<float(&)[2][2]>(acc(i, 2 * idxAtomBx2 + j)),
reinterpret_cast<uint32_t const(&)[2][2]>(regQBuf[idxInstK][i]),
reinterpret_cast<uint32_t const(&)[2][1]>(atomBx2[2 * j]));
}
}
if (prefetch) {
regKBuf[idxInstKPrefetch][idxAtomBx2] = loaderK.load(idxAtomBx2);
}
}
if (idxInstKPrefetch == partNbInstK - 1) {
assert(prefetch);
kBarWaiter.consumed();
}
}
}
#endif
if (ctaIter == 0) {
smem.shmQBar.wait_parity(false);
}
#pragma unroll
for (uint32_t idxPart = SharedMemA::regQParts; idxPart < nbQParts; idxPart++) {
#pragma unroll
for (uint32_t idxInstK = 0; idxInstK < partNbInstK; idxInstK++) {
bool const prefetchNextPart = (idxInstK == partNbInstK - 1);
uint32_t const idxPartPrefetch = prefetchNextPart ? idxPart + 1 : idxPart;
uint32_t const idxInstKPrefetch = prefetchNextPart ? 0 : idxInstK + 1;
bool const prefetch = (!prefetchNextPart || (idxPart < nbKParts - 1));
if (prefetchNextPart) {
kBarWaiter = kBarWaiterNext;
kBarWaiterNext = kBarWaiter.next();
if (prefetch) {
kBarWaiter.wait();
}
}
Mat16x32Loader const loaderK(smem.k[kBarWaiter.idxBuf], 0, idxInstKPrefetch, rB, cB);
#pragma unroll
for (uint32_t idxAtomBx2 = 0; idxAtomBx2 < tileNbAtomBx2; idxAtomBx2++) {
if (idxAtomBx2 == 2 && prefetch) {
regQBuf[idxInstKPrefetch] =
loadRegQCol(smem.q[idxPartPrefetch - SharedMemA::regQParts], idxInstKPrefetch);
}
AtomBx2 const& atomBx2 = regKBuf[idxInstK][idxAtomBx2];
if (shouldTestWait(idxInstKPrefetch, idxAtomBx2) && prefetch) {
kBarWaiterNext.testWait();
}
#pragma unroll
for (uint32_t i = 0; i < WarpAcc::rows; i++) {
#pragma unroll
for (uint32_t j = 0; j < 2; j++) {
mma<__nv_fp8_e4m3>(reinterpret_cast<float(&)[2][2]>(acc(i, 2 * idxAtomBx2 + j)),
reinterpret_cast<uint32_t const(&)[2][2]>(regQBuf[idxInstK][i]),
reinterpret_cast<uint32_t const(&)[2][1]>(atomBx2[2 * j]));
}
}
if (prefetch) {
regKBuf[idxInstKPrefetch][idxAtomBx2] = loaderK.load(idxAtomBx2);
}
}
if (idxInstKPrefetch == partNbInstK - 1) {
assert(prefetch);
kBarWaiter.consumed();
if (idxPartPrefetch == nbKParts - 1) {
tensorCoreMutex.unlock(); // let the other group to use tensor cores
}
}
}
}
uint32_t const validTokens = seqLen - tokensPerTile * idxTile;
if (validTokens < tokensPerTile) {
applyMask(this_warp(), acc, 0, validTokens);
}
ThrdRegRowMax rowMaxLog2e;
WarpAcc const xF32 = scaleAndSoftmax(rowMaxLog2e, acc, grpIdx, grpIter, tileBaseRow);
auto& xBar = smem.xBars[grpIdx];
bool const skipXBarWait = xBar.consumed.test_wait_parity(toParity<1>(grpIter));
// convert to fp8
WarpAcc const xF32Quant = xF32 * rcpXScale;
// 0, 1, 8, 9, 2, 3, 10, 11, 4, 5, 12, 13, 6, 7, 14, 15
Array2D<Array2D<uint32_t, 2, 1>, WarpAcc::rows, exactDiv(WarpAcc::cols, 2)> xF8;
#pragma unroll
for (uint32_t i = 0; i < WarpAcc::rows; i++) {
#pragma unroll
for (uint32_t m = 0; m < exactDiv(qmmaShape.m, 8); m++) {
#pragma unroll
for (uint32_t j = 0; j < WarpAcc::cols; j += 2) {
auto& dst = reinterpret_cast<__nv_fp8x2_e4m3(&)[2]>(xF8(i, j / 2)(m, 0));
dst[0] = __nv_fp8x2_e4m3(float2{xF32Quant(i, j)(m, 0), xF32Quant(i, j)(m, 1)});
dst[1] = __nv_fp8x2_e4m3(float2{xF32Quant(i, j + 1)(m, 0), xF32Quant(i, j + 1)(m, 1)});
}
}
}
// use tensor core to compute rowSum
ThrdRegRowMax const rowSum =
computeRowSumFromF8 ? computeRowSumF8<warpTile.y, warpTile.x>(this_warp(), xF8)
: computeRowSumF32<warpTile.y, warpTile.x>(this_warp(), xF32);
// store xF8 and rowSum into L2 scratch buffer
if (!skipXBarWait) {
xBar.consumed.wait_parity(toParity<1>(grpIter));
}
storeRowMax<warpTile.y>(smem.x.rowMaxLog2e, rowMaxLog2e, tileBaseRow, lane);
storeRowMax<warpTile.y>(smem.x.rowSum, rowSum, tileBaseRow, lane);
storeOrderedXToShm(smem.x.x, xF8, tileBaseRow, lane);
xBar.produced.arrive();
}
}
__device__ inline WarpAcc scaleAndSoftmax(ThrdRegRowMax& rowMaxLog2e, WarpAcc const& acc,
uint32_t grpIdx, uint32_t grpIter,
uint32_t tileBaseRow);
__device__ inline void storeOrderedXToShm(
XBuffer& dst,
Array2D<Array2D<uint32_t, 2, 1>, WarpAcc::rows, exactDiv(WarpAcc::cols, 2)> const& src,
uint32_t const tileBaseRow, uint32_t const lane = laneId());
};
__device__ inline void Producer::loadK() {
KVTilePartLoader loader{args.cacheList, idxReq, args.tensorMapK, divUp(seqLen, tokensPerPage)};
#pragma unroll 1
for (uint32_t iter = 0; true; iter++) {
uint32_t const idxTile = idxTileBeg() + iterStride() * iter;
if (idxTile >= nbTiles()) {
break;
}
uint32_t const idxPageBuf = iter % KVTilePartLoader::nbPageBuffers;
loader.loadPages(idxTile, idxPageBuf);
#pragma unroll 1
for (uint32_t idxPart = 0; idxPart < nbKParts; idxPart++) {
uint32_t const idxPartGlobal = iter * nbKParts + idxPart;
uint32_t const idxBuf = idxPartGlobal % SharedMemA::nbKBufs;
auto& bar = smem.kBars[idxBuf];
bar.consumed.wait_parity(toParity<SharedMemA::nbKBufs>(idxPartGlobal));
loader.loadData(smem.k[idxBuf], idxTile, partElemsK * idxPart, bar.produced, idxPageBuf);
if (warpElectSync()) {
bar.produced.arrive_tx(sizeof(SharedMemA::ShmKPart));
}
}
}
}
__device__ inline void Producer::sendX() {
// let group 0 to produce first.
if (warpElectSync()) {
smem.xBars[0].consumed.arrive();
}
for (uint32_t iter = 0; true; iter++) {
uint32_t const idxTile = idxTileBeg() + iterStride() * iter;
if (idxTile >= nbTiles()) {
break;
}
uint32_t const idxBar = iter % SharedMemA::nbXBars;
auto& xBar = smem.xBars[idxBar];
xBar.produced.wait_parity(toParity<SharedMemA::nbXBars>(iter));
smem.cgaXBufConsumed.wait_parity(toParity<1>(iter));
if (warpElectSync()) {
auto& dst = args.cgaXBuf[nbSubSeq * idxInputTokenGlobal + idxSubSeq][ctaRank];
tma::store1DAsync(&dst, &smem.x, sizeof(CgaXBuffer));
tma::commitGroup();
tma::waitGroup<0>();
// it's turn for the other math group to produce.
uint32_t const idxBarNext = (iter + 1) % SharedMemA::nbXBars;
auto& xBarNext = smem.xBars[idxBarNext];
xBarNext.consumed.arrive();
asm volatile("fence.release.cluster;\n");
#pragma unroll
for (uint32_t i = 0; i < nbVSplit; i++) {
auto& producedBar = getConsumerShm(i).cgaXBufProduced[ctaRank];
producedBar.arrive<Scope::CGA, ArriveOrder::RELAXED>();
}
}
}
}
__device__ inline Producer::WarpAcc Producer::scaleAndSoftmax(ThrdRegRowMax& rowMaxLog2e,
WarpAcc const& acc, uint32_t grpIdx,
uint32_t grpIter,
uint32_t tileBaseRow) {
uint32_t const ctaIter = grpIdx + grpIter * nbMathGrps;
uint32_t const cgaIter = ctaRank + ctaIter * nbProducerCtasPerCga;
auto const warp = this_warp();
uint32_t const lane = laneId();
uint32_t const idxProducer = ctaRank;
assert(ctaRank < nbProducerCtasPerCga);
float const qkScaleLog2e = smem.qkScaleLog2e;
bool const skipWaitLastShmRowMax =
smem.rowMaxLog2eBar[grpIdx].test_wait_parity(toParity<1>(grpIter));
QuadRegRowMax const tileRowMaxLog2e = computeRowMax<warpTile.y, warpTile.x>(acc) * qkScaleLog2e;
// get max with previous CTA's rowMax
if (!skipWaitLastShmRowMax) {
smem.rowMaxLog2eBar[grpIdx].wait_parity(toParity<1>(grpIter));
}
auto const lastRowMaxLog2e = loadShmRowMax<warpTile.y>(smem.rowMaxLog2e, tileBaseRow, lane);
auto const quadRowMaxLog2e = fmaxf(tileRowMaxLog2e, replicateForQuad(warp, lastRowMaxLog2e));
// transfer new row max to the other producer CTA for next iteration
SharedMemA& smemNext = mapa(smem, ctaRank ^ 1U);
CgaBarrier& nextRowMaxLog2eBar =
smemNext.rowMaxLog2eBar[(cgaIter + 1) % (nbMathGrps * nbProducerCtasPerCga) / nbMathGrps];
rowMaxLog2e = dedupFromQuad(warp, quadRowMaxLog2e);
storeRowMaxAsync<warpTile.y>(nextRowMaxLog2eBar, smemNext.rowMaxLog2e, rowMaxLog2e, tileBaseRow,
lane);
nextRowMaxLog2eBar.arrive_tx_relaxed(
sizeof(rowMaxLog2e)); // notify that the next CTA can read rowMax now.
WarpAcc x;
// apply softmax
#pragma unroll
for (uint32_t m = 0; m < acc.rows; m++) {
#pragma unroll
for (uint32_t i = 0; i < InstAcc::rows; i++) {
float const maxVal = quadRowMaxLog2e[m * InstAcc::rows + i];
#pragma unroll
for (uint32_t n = 0; n < acc.cols; n++) {
#pragma unroll
for (uint32_t j = 0; j < InstAcc::cols; j++) {
float elem = acc(m, n)(i, j);
assert(maxVal >= elem * qkScaleLog2e);
x(m, n)(i, j) = exp2f(elem * qkScaleLog2e - maxVal);
}
}
}
}
return x;
}
__device__ inline void Producer::storeOrderedXToShm(
XBuffer& dst,
Array2D<Array2D<uint32_t, 2, 1>, WarpAcc::rows, exactDiv(WarpAcc::cols, 2)> const& src,
uint32_t const tileBaseRow, uint32_t const lane) {
uint32_t const r = lane % 16;
uint32_t const c = lane / 16;
using Src = mha::decay_t<decltype(src)>;
LdGrain* ptrs[exactDiv(Src::cols, 2)][Src::rows];
#pragma unroll
for (uint32_t idxInstK = 0; idxInstK < exactDiv(Src::cols, 2); idxInstK++) {
Mat16x32Loader const loader(dst, tileBaseRow, idxInstK, r, c);
#pragma unroll
for (uint32_t idxInstM = 0; idxInstM < Src::rows; idxInstM++) {
auto const p = const_cast<LdGrain*>(loader.getPtr(idxInstM));
stmatrix<false, 4>(p, reinterpret_cast<LdGrain const&>(src(idxInstM, idxInstK * 2)));
ptrs[idxInstK][idxInstM] = p;
}
}
// reorder from 0, 1, 8, 9, 2, 3, 10, 11, 4, 5, 12, 13, 6, 7, 14, 15
// to 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
__syncwarp();
#pragma unroll
for (uint32_t idxInstK = 0; idxInstK < exactDiv(Src::cols, 2); idxInstK++) {
#pragma unroll
for (uint32_t idxInstM = 0; idxInstM < Src::rows; idxInstM++) {
auto const p = ptrs[idxInstK][idxInstM];
auto const i = *p;
LdGrain const o = {
prmt(i[0], i[1], PermuteOrder{0, 1, 4, 5}), prmt(i[2], i[3], PermuteOrder{0, 1, 4, 5}),
prmt(i[0], i[1], PermuteOrder{2, 3, 6, 7}), prmt(i[2], i[3], PermuteOrder{2, 3, 6, 7})};
*p = o;
}
}
}
struct Consumer {
static inline constexpr uint32_t nbMathWarps = nbMathWarpsB;
static inline constexpr uint32_t nbMathThrds = warp_size * nbMathWarps;
static inline constexpr uint2 ctaShape = {2, 4};
static_assert(SharedMemB::nbAccRowMaxSumCopies == ctaShape.x);
static_assert(ctaShape.x * ctaShape.y == nbMathWarps);
static inline constexpr uint2 warpTile = {exactDiv(gemm1V, ctaShape.x),
exactDiv(headGrpSize, ctaShape.y)};
static inline constexpr uint32_t nbWarpOutSwizzleBuf = nbMathWarps;
using WarpOutSwizzleBuf =
Array2D<LdGrain,
exactDiv(exactDiv(sizeof(SharedMemB::XVBuffer), sizeof(OutputElem) * warpTile.x),
nbMathWarps),
exactDiv(sizeof(OutputElem) * warpTile.x, grainBytes)>;
static_assert(WarpOutSwizzleBuf::rows % 8 == 0);
using WarpAcc = WarpAccT<warpTile.y, warpTile.x>;
using ThrdRegRowMax = ThrdRegRowMaxT<warpTile.y>;
using UniformNeedRescaleMask = Vec<uint32_t, divUp(warpTile.y, warp_size)>;
KernelArgs const& args;
SharedMemB& smem;
uint32_t const maxNbSubSeq;
uint32_t const idxReq;
uint32_t const idxInputTokenGlobal;
uint32_t const nbSubSeq;
uint32_t const idxSubSeq;
uint32_t const seqLen;
uint32_t const ctaRank;
uint32_t const warpRank;
uint2 const warpIdx;