okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-q4-geometry (fc1c4e429): the model shape is fixed at build time (-DSTRATA_GEOMETRY / -DSTRATA_GEOMETRY_FILE); the default Flash-Next build is unchanged (CUDA SASS 3573/3573, HIP gfx1201 3920/3920, gfx1151 path-string offsets only, SYCL 22/22 checksums, Metal 10/10 tokens, CUDA 130/130 tokens); Qwen4 test shapes q4_lite/q4_mid/q4_big pass logits gate 2 on CUDA (Qwen4 prep)4dad59ab3

main
42.2 KiB684 linesraw
1// src/prefill/gdn_tc.cuh - the chunked DeltaNet recurrence's scan on the tensor cores (STRATA_GDN_TC=1), sm_80+.
2//
3// Included inside the anonymous namespace of src/prefill/kernels.cu and of src/prefill/gdn_rec_parity.cu, after
4// gdn_chunk_scan_kernel (the harness runs the engine's kernels verbatim: one text, included twice).  It uses S, HK, HV,
5// C, VPK, GCH, GSB, GTP, gdn_cp16 / gdn_cp_commit / gdn_cp_wait_all from there and reads no environment.
6//
7// gdn_chunk_scan_kernel's chunk algebra (kernels.cu, "STRATA_GDN_CHUNKED=1"), every product transposed so that a
8// warp's slice of the state is the mma A operand and never leaves its registers: a block per value head, warp w owns
9// value columns 16w..16w+15 of S^T (16 columns x 128 rows = 16 m16n8 f32 accumulator tiles, 64 registers a lane) and
10// all 32 tokens of a chunk, so not one product couples two warps:
11//   X^T  (16c x 32i) = S0^T . K^T        f16x3, k = state row (8 k16-steps), n = token (4 tiles)
12//   QS^T (16c x 32t) = S0^T . Q^T        the same A fragments
13//   Y^T  (16c x 32i) = beta_i (V^T - e_i X^T)                                     elementwise on the accumulators
14//   D^T  (16c x 32i) = Y^T . T^T         f16x3, k = token i' (2 steps), n = token i: T is zero above its diagonal,
15//                                        so 6 of 8 tiles (as P below)
16//   o^T  (16c x 32t) = e_t QS^T + D^T . P^T   f16x3, k = i (2 steps), n = t: P zero above its diagonal (6 of 8 tiles)
17//   Dd^T (16c x 32i) = f_i D^T,  f_i = exp(gL - gamma_i)
18//   S^T  (16c x 128r) = eL S0^T + Dd^T . K    f16x3, k = token (2 steps), n = state row (16 tiles)
19// 324 mma a warp a chunk; the only shared data are the staged inputs (k, q, T and P as f16 hi / lo images, v, the
20// per-token scalars), one barrier a chunk.
21//
22// Numerics (the harness holds the result within 1e-4 of gdn_rec_kh_kernel, as the FP32 chunked pair): an f16 operand
23// is split x = hi + lo, hi = f16(x), lo = f16(x - hi), and a product takes hi.hi + hi.lo + lo.hi, dropping lo.lo: with
24// both parts normal that is within 3 x 2^-22 |a||b| of the exact product, the 3xTF32 bound, at half the tensor time.
25// For the low part to be normal the operand's largest entry must sit in [2^14, 2^15), which a power of two does
26// exactly: k and q (unit vectors after gdn_l2_kernel) carry 2^8, P (|P| <= 1) 2^8, the state 2^es from a warp max
27// per chunk, Y^T 2^ey and D^T 2^ed likewise, T = (I + A)^-1 (no a-priori bound) 2^et from the prep's warp max; the
28// powers are taken out of the per-token scalars and of the accumulator before and after the mma (one FMUL an
29// element, exact).  (D^T = Y^T T^T on tf32x3 with T split at use, the form before this one, cost 30 mma, 32 shuffles
30// and 48 tf32 conversions a warp a chunk against 18 mma now; measured in the commit that changed it.)
31#if !defined(__HIPCC__)
32#if !defined(__CUDA_ARCH__) || __CUDA_ARCH__ >= 800
33#define STRATA_GDN_TC_SM80 1
34#else
35#define STRATA_GDN_TC_SM80 0   // device code for older cards: the wrappers trap (gdn_rec_tc never launches there)
36#endif
37
38constexpr int GTC_KP = S + 8;     // f16 k / q rows: a 272-B stride puts the 8 rows of a ldmatrix 8x8 on 8 distinct 16-B lanes
39constexpr int GTC_VP = S + 4;     // f32 v rows: lane (gid, tig) reads word 264 tig + gid, 32 distinct banks
40constexpr int GTC_PP = GCH + 8;   // f16 T / P rows: an 80-B stride, 8 rows on distinct 16-B lanes
41constexpr int GTC_CW = S / 16;    // consumer warps a block: 16 value columns each
42// dynamic shared memory a block, two buffers each: k / q hi / lo, v, (z), T hi / lo, P hi / lo, the token scalars
43// (beta, exp(gamma), exp(gL - gamma), gamma) as float4, (the norm's o^2 partials), T's scale 2^-et: 124,944 B
44// without the fused norm, 160,784 with it (the 166,912 opt-in of GA100)
45template <bool NORM>
46constexpr size_t kTcScanSmem = 2 * (4 * GCH * GTC_KP * 2 + GCH * GTC_VP * 4 + (NORM ? GCH * GTC_VP * 4 : 0) +
47                                    4 * GCH * GTC_PP * 2 + GCH * 16 + (NORM ? GCH * GTC_CW * 4 : 0)) + 16;
48static_assert(kTcScanSmem<false> == 124944 && kTcScanSmem<true> == 160784, "gdn_tc.cuh: the shared-memory plan");
49// SPLIT (the 99-KB cards: sm_86 / sm_89 / sm_120): only k / q hi / lo and the token scalars stay double-buffered; v, T and P
50// (one buffer) are staged for the next chunk after this chunk's last read of them, so that load overlaps the state update:
51// 97,808 B against the 101,376 B opt-in of those cards (the full plan needs 124,944)
52constexpr size_t kTcScanSmemSplit = 2 * (4 * GCH * GTC_KP * 2 + GCH * 16) + GCH * GTC_VP * 4 + 4 * GCH * GTC_PP * 2 + 16;
53static_assert(kTcScanSmemSplit == 97808, "gdn_tc.cuh: the split shared-memory plan");
54// + the fused output norm's partial sums of squares [GTC_CW warps][GCH tokens] (SPLIT only): 98,832 B
55constexpr size_t kTcScanSmemSplitNorm = kTcScanSmemSplit + GTC_CW * GCH * 4;
56static_assert(kTcScanSmemSplitNorm == 98832, "gdn_tc.cuh: the split + norm shared-memory plan");
57// the prep's scratch for a super-block (gdn_tc_scratch, one a device, 60.3 MB): the T images x 2^et as
58// [chunk][vh][2: hi, lo][GCH][GCH] f16 and their scales 2^-et as [chunk][vh], the token scalars as float4
59// [chunk][vh][GCH], the k and q images x 2^8 as [chunk][qh][4: k hi, k lo, q hi, q lo][GCH][S] and the P images x 2^8
60// as [chunk][vh][2: hi, lo][GCH][GCH] - every row a multiple of 16 B, so the scan stages all of it by cp.async
61constexpr int GTC_NB = GSB / GCH;
62struct GtcScratch {
63    uint16_t* ti;
64    float* te;
65    float4* sc;
66    uint16_t* kq;
67    uint16_t* pi;
68};
69constexpr size_t kTcTiBytes = (size_t) GTC_NB * HV * 2 * GCH * GCH * 2, kTcTeBytes = (size_t) GTC_NB * HV * 4,
70                 kTcScBytes = (size_t) GTC_NB * HV * GCH * 16, kTcKqBytes = (size_t) GTC_NB * HK * 4 * GCH * S * 2,
71                 kTcPiBytes = (size_t) GTC_NB * HV * 2 * GCH * GCH * 2;
72// the frozen total is Flash-Next's (HK 16, HV 48); any other shape is sized by the formulas above
73static_assert(HV != 48 || HK != 16 || kTcTiBytes + kTcTeBytes + kTcScBytes + kTcKqBytes + kTcPiBytes == 60305408, "gdn_tc.cuh: the scratch plan");
74// allocated on first use and kept, as gdn_chunk_scratch (kernels.cu); ti == nullptr when the card had no room
75GtcScratch gdn_tc_scratch() {
76    static unsigned char* bufs[64] = {};
77    GtcScratch r = {nullptr, nullptr, nullptr, nullptr, nullptr};
78    int dev = 0;
79    if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) { cudaGetLastError(); return r; }
80    if (bufs[dev] == nullptr && cudaMalloc((void**) &bufs[dev], kTcTiBytes + kTcTeBytes + kTcScBytes + kTcKqBytes + kTcPiBytes) != cudaSuccess) {
81        cudaGetLastError();
82        bufs[dev] = nullptr;
83        return r;
84    }
85    unsigned char* p = bufs[dev];
86    r.ti = reinterpret_cast<uint16_t*>(p);
87    r.te = reinterpret_cast<float*>(p + kTcTiBytes);
88    r.sc = reinterpret_cast<float4*>(p + kTcTiBytes + kTcTeBytes);
89    r.kq = reinterpret_cast<uint16_t*>(p + kTcTiBytes + kTcTeBytes + kTcScBytes);
90    r.pi = reinterpret_cast<uint16_t*>(p + kTcTiBytes + kTcTeBytes + kTcScBytes + kTcKqBytes);
91    return r;
92}
93
94// the warp's largest of non-negative floats: their bits order as unsigned integers, so one redux.sync (sm_80).  Under
95// the full mask: __activemask() does not reconverge the warp (CUDA guide), and the first call of the scan follows a
96// lane-divergent branch (the L2 prefetch of tokens past the end, in the chunk two before the end whenever
97// 0 < T - f_t0 < 32), so with it the lanes could reduce under a partial mask and disagree on es.  The harness build
98// happened to close that branch (BSYNC) before the REDUX.MAX; the engine's build is a separate compile that nothing
99// checks.  The kernel has no early return, so the full mask costs no reconvergence code.
100__device__ __forceinline__ float gtc_warp_max(float v) {
101#if STRATA_GDN_TC_SM80
102    return __uint_as_float(__reduce_max_sync(0xffffffffu, __float_as_uint(v)));
103#else
104#pragma unroll
105    for (int o = 16; o > 0; o >>= 1) v = fmaxf(v, __shfl_xor_sync(0xffffffffu, v, o));
106    return v;
107#endif
108}
109__device__ __forceinline__ float gtc_pow2(int e) { return __int_as_float((e + 127) << 23); }   // 2^e, |e| < 127
110// the power of two that puts m = max |x| >= 0 in [2^14, 2^15): f16 hi never overflows (65504) and the low part of
111// every entry within 2^-17 of the max stays normal; clamped so that the compensating powers stay far from f32's range
112__device__ __forceinline__ int gtc_scale_exp(float m) {
113    if (m == 0.0f) return 0;
114    const int e = 14 - ((__float_as_int(m) >> 23) - 127);
115    return e < -40 ? -40 : (e > 40 ? 40 : e);
116}
117
118// ---- the tensor-core wrappers (PTX ISA "Matrix Fragments": lane = 4 gid + tig)
119//   m16n8k16 f16: A a0 = (row gid, cols 2tig, 2tig+1) a1 = (gid+8, ..) a2 = (gid, 2tig+8..) a3 = (gid+8, 2tig+8..);
120//                 B b0 = (k 2tig, 2tig+1; n gid) b1 = (k 2tig+8..; n gid); C/D c0 = (gid, 2tig) c1 = (gid, 2tig+1)
121//                 c2 = (gid+8, 2tig) c3 = (gid+8, 2tig+1) - so two adjacent C tiles ARE the next product's A fragment
122//   ldmatrix m8n8.x4.b16: lanes 8m..8m+7 give the row addresses of matrix m; a lane gets (row gid, cols 2tig, 2tig+1)
123//                 of each matrix (the lower column in the low half), or with .trans (rows 2tig, 2tig+1; col gid)
124__device__ __forceinline__ void gtc_ldsm_x4(uint32_t (&r)[4], const void* smem) {
125#if STRATA_GDN_TC_SM80
126    asm volatile("ldmatrix.sync.aligned.m8n8.x4.shared.b16 {%0,%1,%2,%3}, [%4];"
127                 : "=r"(r[0]), "=r"(r[1]), "=r"(r[2]), "=r"(r[3])
128                 : "r"((unsigned) __cvta_generic_to_shared(smem)));
129#else
130    r[0] = r[1] = r[2] = r[3] = 0u; (void) smem; __trap();
131#endif
132}
133__device__ __forceinline__ void gtc_ldsm_x4_trans(uint32_t (&r)[4], const void* smem) {
134#if STRATA_GDN_TC_SM80
135    asm volatile("ldmatrix.sync.aligned.m8n8.x4.trans.shared.b16 {%0,%1,%2,%3}, [%4];"
136                 : "=r"(r[0]), "=r"(r[1]), "=r"(r[2]), "=r"(r[3])
137                 : "r"((unsigned) __cvta_generic_to_shared(smem)));
138#else
139    r[0] = r[1] = r[2] = r[3] = 0u; (void) smem; __trap();
140#endif
141}
142__device__ __forceinline__ void gtc_mma_f16(float (&c)[4], const uint32_t (&a)[4], uint32_t b0, uint32_t b1) {
143#if STRATA_GDN_TC_SM80
144    asm("mma.sync.aligned.m16n8k16.row.col.f32.f16.f16.f32 {%0,%1,%2,%3}, {%4,%5,%6,%7}, {%8,%9}, {%0,%1,%2,%3};"
145        : "+f"(c[0]), "+f"(c[1]), "+f"(c[2]), "+f"(c[3])
146        : "r"(a[0]), "r"(a[1]), "r"(a[2]), "r"(a[3]), "r"(b0), "r"(b1));
147#else
148    (void) c; (void) a; (void) b0; (void) b1; __trap();
149#endif
150}
151// the three passes of a split product, small terms first (as qsa_select.cu): lo.hi, hi.lo, hi.hi
152__device__ __forceinline__ void gtc_mma3_f16(float (&c)[4], const uint32_t (&ah)[4], const uint32_t (&al)[4], uint32_t bh0,
153                                             uint32_t bh1, uint32_t bl0, uint32_t bl1) {
154    gtc_mma_f16(c, al, bh0, bh1);
155    gtc_mma_f16(c, ah, bl0, bl1);
156    gtc_mma_f16(c, ah, bh0, bh1);
157}
158// NP (experiment, STRATA_GDN_TC split kernels only): 3 = every product on the three passes above (the default); 2 = only the products
159// that reach the OUTPUT alone (q.S and D.P) on one pass, the state's products (k.S, Y.T, the update) on three; 1 = every product on one pass
160// (hi.hi, plain f16 operands as the gfx11 WMMA path).  A single-pass product does not read its B operand's lo image.
161template <int NP>
162__device__ __forceinline__ void gtc_mmaN(float (&c)[4], const uint32_t (&ah)[4], const uint32_t (&al)[4], uint32_t bh0,
163                                         uint32_t bh1, uint32_t bl0, uint32_t bl1) {
164    if constexpr (NP == 3) {
165        gtc_mma_f16(c, al, bh0, bh1);
166        gtc_mma_f16(c, ah, bl0, bl1);
167    } else if constexpr (NP == 2) {
168        gtc_mma_f16(c, al, bh0, bh1);
169    }
170    gtc_mma_f16(c, ah, bh0, bh1);
171}
172// x0, x1 -> packed f16 hi (x0 in the low half) and lo = f16(x - hi): x - hi is exact in f32 (hi carries x's exponent)
173__device__ __forceinline__ void gtc_split_f16x2(float x0, float x1, uint32_t& hi, uint32_t& lo) {
174    union { __half2 h; uint32_t u; } cv;
175    cv.h = __floats2half2_rn(x0, x1);
176    hi = cv.u;
177    const float2 r = __half22float2(cv.h);
178    cv.h = __floats2half2_rn(x0 - r.x, x1 - r.y);
179    lo = cv.u;
180}
181// four consecutive elements of an f16 hi / lo row from a float4 x scale (an 8-byte store each)
182__device__ __forceinline__ void gtc_split_store4(uint16_t* hi, uint16_t* lo, float4 v, float scale) {
183    uint32_t h0, l0, h1, l1;
184    gtc_split_f16x2(v.x * scale, v.y * scale, h0, l0);
185    gtc_split_f16x2(v.z * scale, v.w * scale, h1, l1);
186    *reinterpret_cast<uint2*>(hi) = make_uint2(h0, h1);
187    *reinterpret_cast<uint2*>(lo) = make_uint2(l0, l1);
188}
189// a float4 from shared memory the compiler does not keep: the per-token scalars are read in three phases of a chunk,
190// a component each, and kept across them they cost 24 registers a thread
191__device__ __forceinline__ float4 gtc_lds128(const float4* p) {
192    float4 v;
193#if STRATA_GDN_TC_SM80
194    asm volatile("ld.shared.v4.f32 {%0,%1,%2,%3}, [%4];"
195                 : "=f"(v.x), "=f"(v.y), "=f"(v.z), "=f"(v.w)
196                 : "r"((unsigned) __cvta_generic_to_shared(p)));
197#else
198    v = *p;
199#endif
200    return v;
201}
202// ---- the products of a chunk, a warp each (the self-test of gdn_rec_parity --selftest-frag runs them one by one)
203// X^T = S^T K^T and QS^T = S^T Q^T: A = the state fragments x sA (= 2^es) split in f16 hi / lo just in time per k16-step
204// (tiles 2kk and 2kk + 1 of S^T are that step's A fragment), B = the f16 hi / lo images of k and q ([GCH][GTC_KP] in
205// shared memory, rows = tokens) by ldmatrix: matrices (n-tile 2hf + (lane >> 4), k-half (lane >> 3) & 1), a lane's
206// row is token 8 n-tile + (lane & 7).  The accumulators come out as 2^(8 + es) times the products.  2 x 96 mma.
207template <int NPK = 3, int NPQ = 3>
208__device__ __forceinline__ void gtc_prod_state(const float (&s)[16][4], float sA, const uint16_t* kh, const uint16_t* kl,
209                                               const uint16_t* qh, const uint16_t* ql, float (&xt)[4][4], float (&qs)[4][4]) {
210    const int lane = threadIdx.x & 31;
211    const int off = (8 * (lane >> 4) + (lane & 7)) * GTC_KP + 8 * ((lane >> 3) & 1);
212#pragma unroll
213    for (int kk = 0; kk < S / 16; ++kk) {
214        uint32_t ah[4], al[4];
215        gtc_split_f16x2(s[2 * kk][0] * sA, s[2 * kk][1] * sA, ah[0], al[0]);
216        gtc_split_f16x2(s[2 * kk][2] * sA, s[2 * kk][3] * sA, ah[1], al[1]);
217        gtc_split_f16x2(s[2 * kk + 1][0] * sA, s[2 * kk + 1][1] * sA, ah[2], al[2]);
218        gtc_split_f16x2(s[2 * kk + 1][2] * sA, s[2 * kk + 1][3] * sA, ah[3], al[3]);
219#pragma unroll
220        for (int hf = 0; hf < 2; ++hf) {
221            const int o = off + 16 * hf * GTC_KP + 16 * kk;
222            uint32_t bh[4], bl[4] = {0u, 0u, 0u, 0u};
223            gtc_ldsm_x4(bh, kh + o);
224            if constexpr (NPK == 3) gtc_ldsm_x4(bl, kl + o);
225            gtc_mmaN<NPK>(xt[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]);
226            gtc_mmaN<NPK>(xt[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]);
227            gtc_ldsm_x4(bh, qh + o);
228            if constexpr (NPQ == 3) gtc_ldsm_x4(bl, ql + o);
229            gtc_mmaN<NPQ>(qs[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]);
230            gtc_mmaN<NPQ>(qs[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]);
231        }
232    }
233}
234// o^T += (sD D^T) (2^8 P^T) on f16x3: A = the D^T tiles x sD (= 2^ed) split per k16-step, B = the f16 hi / lo images
235// of P x 2^8 ([GCH][GTC_PP], rows = tokens t) by ldmatrix as in gtc_prod_state.  SKIP leaves out the tiles above P's
236// diagonal: tokens t < 16 (n-tiles 0, 1) see no i >= 16 (k-step 1).  18 (SKIP) or 24 mma.  D^T += (sY Y^T) (2^et T^T)
237// is the same product on the T images (T is zero above its diagonal too), the accumulator x 2^(ey + et).
238template <bool SKIP, int NP = 3>
239__device__ __forceinline__ void gtc_prod_dt(const float (&dt)[4][4], float sD, const uint16_t* ph, const uint16_t* pl,
240                                            float (&ot)[4][4]) {
241    const int lane = threadIdx.x & 31;
242    const int off = (8 * (lane >> 4) + (lane & 7)) * GTC_PP + 8 * ((lane >> 3) & 1);
243#pragma unroll
244    for (int ks = 0; ks < GCH / 16; ++ks) {
245        uint32_t ah[4], al[4];
246        gtc_split_f16x2(dt[2 * ks][0] * sD, dt[2 * ks][1] * sD, ah[0], al[0]);
247        gtc_split_f16x2(dt[2 * ks][2] * sD, dt[2 * ks][3] * sD, ah[1], al[1]);
248        gtc_split_f16x2(dt[2 * ks + 1][0] * sD, dt[2 * ks + 1][1] * sD, ah[2], al[2]);
249        gtc_split_f16x2(dt[2 * ks + 1][2] * sD, dt[2 * ks + 1][3] * sD, ah[3], al[3]);
250#pragma unroll
251        for (int hf = 0; hf < 2; ++hf) {
252            if (SKIP && ks == 1 && hf == 0) continue;
253            const int o = off + 16 * hf * GTC_PP + 16 * ks;
254            uint32_t bh[4], bl[4] = {0u, 0u, 0u, 0u};
255            gtc_ldsm_x4(bh, ph + o);
256            if constexpr (NP == 3) gtc_ldsm_x4(bl, pl + o);
257            gtc_mmaN<NP>(ot[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]);
258            gtc_mmaN<NP>(ot[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]);
259        }
260    }
261}
262// S^T += Dd^T (2^8 K) on f16x3: A = the (already scaled) Dd^T tiles split per k16-step, B = the same k images read the
263// other way by ldmatrix.trans: matrices (n-tile 2hf + (lane >> 4), k-half (lane >> 3) & 1), a lane's row is token
264// 16 ks + 8 k-half + (lane & 7), its 16-B piece state rows 8 n-tile..  96 mma.
265template <int NP = 3>
266__device__ __forceinline__ void gtc_update_state(float (&s)[16][4], const float (&dd)[4][4], const uint16_t* kh,
267                                                 const uint16_t* kl) {
268    const int lane = threadIdx.x & 31;
269    const int off = (8 * ((lane >> 3) & 1) + (lane & 7)) * GTC_KP + 8 * (lane >> 4);
270#pragma unroll
271    for (int ks = 0; ks < GCH / 16; ++ks) {
272        uint32_t ah[4], al[4];
273        gtc_split_f16x2(dd[2 * ks][0], dd[2 * ks][1], ah[0], al[0]);
274        gtc_split_f16x2(dd[2 * ks][2], dd[2 * ks][3], ah[1], al[1]);
275        gtc_split_f16x2(dd[2 * ks + 1][0], dd[2 * ks + 1][1], ah[2], al[2]);
276        gtc_split_f16x2(dd[2 * ks + 1][2], dd[2 * ks + 1][3], ah[3], al[3]);
277#pragma unroll
278        for (int hf = 0; hf < S / 16; ++hf) {
279            const int o = off + 16 * ks * GTC_KP + 16 * hf;
280            uint32_t bh[4], bl[4] = {0u, 0u, 0u, 0u};
281            gtc_ldsm_x4_trans(bh, kh + o);
282            if constexpr (NP == 3) gtc_ldsm_x4_trans(bl, kl + o);
283            gtc_mmaN<NP>(s[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]);
284            gtc_mmaN<NP>(s[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]);
285        }
286    }
287}
288
289// ---- the prep: gdn_chunk_prep_kernel (kernels.cu) for the tensor-core scan, a block per (chunk, key head): the same
290// A, solve, T and P, but the Gram products k_t.k_i and q_t.k_i on the tensor cores (f16x3 from the k and q hi / lo
291// images x 2^8, which the block builds first in shared memory straight from the float4 registers the rows arrive in,
292// rows past the chunk's end zero: 48 mma a warp against ~1,150 LDS a thread for the FFMA form, which was LDS-bound at
293// 233 us a 2048-token super-block), and it writes what the scan stages by cp.async with no conversion of its own: the
294// same k / q images, P as f16 hi / lo images x 2^8 (the split the scan did, element by element) and the token scalars
295// (beta, exp(gamma), exp(gL - gamma), gamma) as a float4 a token, T as f16 hi / lo images x 2^et, et from the warp's
296// largest |T| (2^-et beside them in te).  The Gram entries are within 3 x 2^-22 of exact (the scan's bound), so T and
297// P differ from gdn_chunk_prep_kernel's by that.
298constexpr size_t kTcPrepSmem = (size_t) 4 * GCH * GTC_KP * 2 + 2 * GCH * GTP * 4;   // the images, then k_t.k_i and q_t.k_i
299static_assert(VPK * GCH * GTP * 4 <= 4 * GCH * GTC_KP * 2, "gdn_tc_prep_kernel: A fits where the images were");
300__global__ void __launch_bounds__(256) gdn_tc_prep_kernel(const float* __restrict__ h, const float* __restrict__ gate,
301                                                          const float* __restrict__ beta, uint16_t* __restrict__ ti,
302                                                          float* __restrict__ te, float4* __restrict__ sc,
303                                                          uint16_t* __restrict__ kq, uint16_t* __restrict__ pi, int64_t t0,
304                                                          int64_t T) {
305    extern __shared__ __align__(16) float gsm[];
306    uint16_t* im = reinterpret_cast<uint16_t*>(gsm);   // [4][GCH][GTC_KP]  k hi, k lo, q hi, q lo (x 2^8)
307    float* skk = gsm + 4 * GCH * GTC_KP / 2;           // [GCH][GTP]  k_t.k_i
308    float* sqk = skk + GCH * GTP;                      // [GCH][GTP]  q_t.k_i
309    float* sa = gsm;                                   // [VPK][GCH][GTP]  A, then T below the diagonal (over the
310                                                       // images: read by then)
311    const int qh = blockIdx.y, ch = blockIdx.x, tid = threadIdx.x, lane = tid & 31, warp = tid >> 5;
312    const int64_t tb = t0 + (int64_t) ch * GCH;
313    const int n = (int) min((int64_t) GCH, T - tb);
314    // after the Gram: warps j and VPK + j take value head j (lane i: token i), the first A, the solve and T, the
315    // second P and the token scalars; their beta and gate (past the end: gate 0, beta 0, no effect on S) are loaded
316    // now so that they are in by then
317    const int j = warp % VPK, vh = qh + j * HK, i = lane;
318    const bool pv = warp < 2 * VPK && i < n;
319    const float b = pv ? beta[(tb + i) * HV + vh] : 0.0f;
320    float gam = pv ? gate[(tb + i) * HV + vh] : 0.0f;
321    {   // k and q as float4, every load issued before the first store; the images to shared and to global
322        constexpr int NP = GCH * S / 4 / 256;   // float4 a thread, each of k and q
323        float4 kv[NP], qv[NP];
324#pragma unroll
325        for (int m = 0; m < NP; ++m) {
326            const int p = tid + 256 * m, t = p / (S / 4), r = (p % (S / 4)) * 4;
327            kv[m] = t < n ? *reinterpret_cast<const float4*>(h + (tb + t) * C + HK * S + qh * S + r) : make_float4(0.f, 0.f, 0.f, 0.f);
328            qv[m] = t < n ? *reinterpret_cast<const float4*>(h + (tb + t) * C + qh * S + r) : make_float4(0.f, 0.f, 0.f, 0.f);
329        }
330        uint16_t* img = kq + ((size_t) ch * HK + qh) * 4 * GCH * S;
331#pragma unroll
332        for (int m = 0; m < NP; ++m) {
333            const int p = tid + 256 * m, t = p / (S / 4), r = (p % (S / 4)) * 4;
334            const int o = t * GTC_KP + r;
335            gtc_split_store4(im + o, im + GCH * GTC_KP + o, kv[m], 256.0f);
336            gtc_split_store4(im + 2 * GCH * GTC_KP + o, im + 3 * GCH * GTC_KP + o, qv[m], 256.0f);
337            gtc_split_store4(img + t * S + r, img + GCH * S + t * S + r, kv[m], 256.0f);
338            gtc_split_store4(img + 2 * GCH * S + t * S + r, img + 3 * GCH * S + t * S + r, qv[m], 256.0f);
339        }
340    }
341    __syncthreads();
342    {   // the Gram products: warp w takes product w >> 2 (k_t.k_i, q_t.k_i), rows 16 ((w >> 1) & 1).. (the A
343        // tile, by ldmatrix as a row-major [t][r] 16 x 16), columns 16 (w & 1).. (two n-tiles: B = k^T, stored
344        // [n][k] = the k rows, by ldmatrix as the scan's gtc_prod_state); the accumulators come out x 2^16
345        const int pr = warp >> 2, mt = (warp >> 1) & 1, np = warp & 1;
346        const uint16_t* ah_ = im + 2 * pr * GCH * GTC_KP + (16 * mt + (lane & 7) + 8 * ((lane >> 3) & 1)) * GTC_KP + 8 * (lane >> 4);
347        const uint16_t* bh_ = im + (16 * np + 8 * (lane >> 4) + (lane & 7)) * GTC_KP + 8 * ((lane >> 3) & 1);
348        float c[2][4] = {};
349#pragma unroll
350        for (int kk = 0; kk < S / 16; ++kk) {
351            uint32_t ah[4], al[4], bh[4], bl[4];
352            gtc_ldsm_x4(ah, ah_ + 16 * kk);
353            gtc_ldsm_x4(al, ah_ + GCH * GTC_KP + 16 * kk);
354            gtc_ldsm_x4(bh, bh_ + 16 * kk);
355            gtc_ldsm_x4(bl, bh_ + GCH * GTC_KP + 16 * kk);
356            gtc_mma3_f16(c[0], ah, al, bh[0], bh[1], bl[0], bl[1]);
357            gtc_mma3_f16(c[1], ah, al, bh[2], bh[3], bl[2], bl[3]);
358        }
359        float* g = pr ? sqk : skk;
360        const int gid = lane >> 2, tig = lane & 3;
361#pragma unroll
362        for (int j = 0; j < 2; ++j)
363#pragma unroll
364            for (int q = 0; q < 4; ++q) {
365                const int t = 16 * mt + gid + 8 * (q >> 1), i = 16 * np + 8 * j + 2 * tig + (q & 1);
366                g[t * GTP + i] = i <= t ? c[j][q] * gtc_pow2(-16) : 0.0f;
367            }
368    }
369    __syncthreads();
370    if (warp >= 2 * VPK) return;
371#pragma unroll
372    for (int o = 1; o < 32; o <<= 1) {
373        const float u = __shfl_up_sync(0xffffffffu, gam, o);
374        if (lane >= o) gam += u;
375    }
376    if (warp >= VPK) {   // P = exp(gamma_t - gamma_i) q_t.k_i (i <= t) x 2^8 as f16 hi / lo; the scalars
377        uint16_t* Ph = pi + ((int64_t) ch * HV + vh) * 2 * GCH * GCH;
378        uint16_t* Pl = Ph + GCH * GCH;
379#pragma unroll
380        for (int t = 0; t < GCH; ++t) {
381            const float gt = __shfl_sync(0xffffffffu, gam, t);
382            const float p = (i <= t ? expf(gt - gam) * sqk[t * GTP + i] : 0.0f) * 256.0f;
383            const __half hi = __float2half_rn(p);
384            Ph[t * GCH + i] = __half_as_ushort(hi);
385            Pl[t * GCH + i] = __half_as_ushort(__float2half_rn(p - __half2float(hi)));
386        }
387        const float gL = __shfl_sync(0xffffffffu, gam, GCH - 1);
388        sc[((int64_t) ch * HV + vh) * GCH + i] = make_float4(b, expf(gam), expf(gL - gam), gam);
389        return;
390    }
391    float* a = sa + j * GCH * GTP;
392#pragma unroll
393    for (int t = 0; t < GCH; ++t) {
394        const float gt = __shfl_sync(0xffffffffu, gam, t), bt = __shfl_sync(0xffffffffu, b, t);
395        a[t * GTP + i] = i < t ? bt * expf(gt - gam) * skk[t * GTP + i] : (i == t ? 1.0f : 0.0f);
396    }
397    __syncwarp();
398    // T = (I + A)^-1 by forward substitution, row by row: T[t][i] = -sum_{k < t} A[t][k] T[k][i] over the finished
399    // rows (diagonal 1, zeros above it: the terms k < i add an exact 0, so the same bits as summing from k = i).
400    // Lane i keeps its column T[.][i] in registers, so a row needs only A's row t (a broadcast load a term) and A
401    // is never written: no store and no __syncwarp a row
402    float tv[GCH];
403    tv[0] = i == 0 ? 1.0f : 0.0f;
404#pragma unroll
405    for (int t = 1; t < GCH; ++t) {
406        float v = 0.0f;
407#pragma unroll
408        for (int k = 0; k < t; ++k) v = fmaf(-a[t * GTP + k], tv[k], v);
409        tv[t] = i < t ? v : (i == t ? 1.0f : 0.0f);
410    }
411    // T's images x 2^et (its largest entry in [2^14, 2^15), as the scan scales its own operands)
412    float mt4[4] = {};
413#pragma unroll
414    for (int t = 0; t < GCH; ++t) mt4[t & 3] = fmaxf(mt4[t & 3], fabsf(tv[t]));
415    const int et = gtc_scale_exp(gtc_warp_max(fmaxf(fmaxf(mt4[0], mt4[1]), fmaxf(mt4[2], mt4[3]))));
416    const float sT = gtc_pow2(et);
417    uint16_t* Th = ti + ((int64_t) ch * HV + vh) * 2 * GCH * GCH;
418    uint16_t* Tl = Th + GCH * GCH;
419#pragma unroll
420    for (int t = 0; t < GCH; ++t) {
421        const float x = tv[t] * sT;
422        const __half hi = __float2half_rn(x);
423        Th[t * GCH + i] = __half_as_ushort(hi);
424        Tl[t * GCH + i] = __half_as_ushort(__float2half_rn(x - __half2float(hi)));
425    }
426    if (i == 0) te[(int64_t) ch * HV + vh] = gtc_pow2(-et);
427}
428
429// ---- the scan: a block per value head (48 blocks, one wave on 48 of the CMP 170HX's 70 SMs), 8 consumer warps of 16
430// value columns each walking every chunk of the super-block with their state slice in registers (written back at the
431// end as gdn_chunk_scan_kernel does), the prep's scratch from gdn_tc_prep_kernel.  Everything a chunk needs arrives by
432// cp.async, issued for the next chunk right after the one __syncthreads a chunk (which publishes the buffer just
433// landed and ends every read of the other one): the k / q and P f16 images, T and the token scalars as the prep
434// wrote them, v from h (its rows past the chunk's end zero-filled).  The 256 threads do no conversion of their own.
435// Earlier forms, measured on this card: the scan splitting k / q itself after the state update (0.282 us a token;
436// ~300 split instructions a warp a chunk and the staging exposed at the chunk's end); two stager warps with named
437// barriers (6 % slower: registers are allocated per SM sub-partition, 16K each, and a block of more than 8 warps puts
438// 3 on a sub-partition, which caps the kernel at 168 registers a thread; the consumers need about 200 and spilled.
439// 8 warps, 2 a sub-partition, may have 255).
440// NORM (step 5: the output norm fused, the FP16 output written here) is not implemented yet; oc_out takes the FP32
441// output as gdn_chunk_scan_kernel writes it.
442template <bool NORM, bool SPLIT = false, int NP = 3, int NTL = 1>
443__global__ void __launch_bounds__(GTC_CW * 32, 1)
444gdn_tc_scan_kernel(float* __restrict__ state, const float* __restrict__ h, const uint16_t* __restrict__ tig_,
445                   const float* __restrict__ teg, const float4* __restrict__ scg, const uint16_t* __restrict__ kqg,
446                   const uint16_t* __restrict__ pig, float* __restrict__ oc_out, int64_t t0, int nch, int64_t T,
447                   const float* __restrict__ zg = nullptr, const float* __restrict__ gamma = nullptr, float eps = 0.0f,
448                   uint16_t* __restrict__ y16 = nullptr, int64_t ld16 = 0) {
449    // NORM (SPLIT only): the output norm of gdn_out_norm_kernel fused: the scan writes the FP16 y16 = o rsqrt(mean(o^2) + eps) gamma
450    // sigmoid(z) itself (the sums of squares across the block's 8 warps through shared memory), and not the FP32 o: one pass less
451    // over the 24 KB-a-token output.  Another summation order than the kernel's 4 partials (not bitwise).
452    static_assert(!NORM || SPLIT, "gdn_tc_scan_kernel: the fused output norm is implemented for the split plan");
453    // NTL value tiles a head (a launch of HV x NTL blocks of GTC_CW / NTL warps): block (vh, tile) owns the state columns
454    // [tile S / NTL, (tile + 1) S / NTL), so a card with more than 48 SMs keeps more of them busy.  The k / q / T / P images are staged
455    // by every tile of the head; v only for its columns.  The fused output norm needs the whole head in one block: NORM => NTL == 1.
456    static_assert(!NORM || NTL == 1, "gdn_tc_scan_kernel: the fused output norm needs a whole head in a block");
457    static_assert(GTC_CW % NTL == 0, "gdn_tc_scan_kernel: tiles of whole warps");
458    constexpr int NT = (GTC_CW / NTL) * 32;
459    extern __shared__ __align__(16) unsigned char gtc_sm[];
460    uint16_t* kh = reinterpret_cast<uint16_t*>(gtc_sm);                 // [2][GCH][GTC_KP]  f16 of k x 2^8 (hi)
461    uint16_t* kl = kh + 2 * GCH * GTC_KP;                               // [2][GCH][GTC_KP]  what hi left out (lo)
462    uint16_t* qh = kl + 2 * GCH * GTC_KP;                               // q likewise
463    uint16_t* ql = qh + 2 * GCH * GTC_KP;
464    constexpr int NV = SPLIT ? 1 : 2;                                   // buffers of v, T and P
465    float* sv = reinterpret_cast<float*>(ql + 2 * GCH * GTC_KP);        // [NV][GCH][GTC_VP]  v
466    uint16_t* th = reinterpret_cast<uint16_t*>(sv + NV * GCH * GTC_VP); // [NV][GCH][GTC_PP]  T x 2^et hi / lo
467    uint16_t* tl = th + NV * GCH * GTC_PP;
468    uint16_t* ph = tl + NV * GCH * GTC_PP;                              // [NV][GCH][GTC_PP]  P x 2^8 hi / lo
469    uint16_t* pl = ph + NV * GCH * GTC_PP;
470    float4* sc = reinterpret_cast<float4*>(pl + NV * GCH * GTC_PP);     // [2][GCH]  (beta, exp(gamma), exp(gL - gamma), gamma)
471    float* ste = reinterpret_cast<float*>(sc + 2 * GCH);                // [2]  2^-et
472    float* rss = ste + 2;                                               // NORM: [GTC_CW][GCH]  partial sums of squares
473    const int vh = blockIdx.x / NTL, qhd = vh % HK, cb = (blockIdx.x % NTL) * (S / NTL);   // the tile's first column
474    const int tid = threadIdx.x, warp = tid >> 5, lane = tid & 31, gid = lane >> 2, tig = lane & 3;
475    const size_t rs = (size_t) HV * S;
476    // chunk ch into buffer b, this thread's 16-B pieces (one cp.async group): the four k / q images (4 x GCH rows of
477    // 16 pieces: 8 a thread), v (4 a thread), the two T images (1), the two P images (1), the scalars (warp 0, 1),
478    // T's scale (thread 0, 4 B)
479    // parts: bit 0 = the k / q images, the scalars and T's scale (buffer b); bit 1 = v, T and P (buffer bv: b, or 0 in SPLIT)
480    auto stage = [&](int ch, int b, int parts) {
481        const int64_t c_t0 = t0 + (int64_t) ch * GCH;
482        const int n = (int) min((int64_t) GCH, T - c_t0);
483        const int bv = SPLIT ? 0 : b;
484        if (parts & 1) {
485            const uint16_t* img = kqg + ((size_t) ch * HK + qhd) * 4 * GCH * S;
486#pragma unroll
487            for (int m = 0; m < 4 * GCH * S / 8 / NT; ++m) {
488                const int pc = tid + NT * m, im = pc / (GCH * S / 8), t = (pc % (GCH * S / 8)) / (S / 8), r = (pc % (S / 8)) * 8;
489                uint16_t* dst = (im == 0 ? kh : im == 1 ? kl : im == 2 ? qh : ql) + (b * GCH + t) * GTC_KP + r;
490                gdn_cp16(reinterpret_cast<float*>(dst), reinterpret_cast<const float*>(img + (im * GCH + t) * S + r));
491            }
492            if (tid < GCH) gdn_cp16(reinterpret_cast<float*>(sc + b * GCH + tid), reinterpret_cast<const float*>(scg + ((int64_t) ch * HV + vh) * GCH + tid));
493            if (tid == 0) gdn_cp4(ste + b, teg + (int64_t) ch * HV + vh);
494        }
495        if (parts & 2) {
496#pragma unroll
497            for (int m = 0; m < GCH * (S / NTL) / 4 / NT; ++m) {
498                const int p = tid + NT * m, t = p / (S / NTL / 4), r = (p % (S / NTL / 4)) * 4;
499                float* dst = sv + (bv * GCH + t) * GTC_VP + r;
500                if (t < n) gdn_cp16(dst, h + (c_t0 + t) * C + 2 * HK * S + vh * S + cb + r);
501                else *reinterpret_cast<float4*>(dst) = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
502            }
503#pragma unroll
504            for (int m = 0; m < 2 * GCH * GCH / 8 / NT; ++m) {
505                const int pc = tid + NT * m, im = pc / (GCH * GCH / 8), t = (pc % (GCH * GCH / 8)) / (GCH / 8), i = (pc % (GCH / 8)) * 8;
506                const int64_t src = ((((int64_t) ch * HV + vh) * 2 + im) * GCH + t) * GCH + i;
507                const int dst = (bv * GCH + t) * GTC_PP + i;
508                gdn_cp16(reinterpret_cast<float*>((im ? tl : th) + dst), reinterpret_cast<const float*>(tig_ + src));
509                gdn_cp16(reinterpret_cast<float*>((im ? pl : ph) + dst), reinterpret_cast<const float*>(pig + src));
510            }
511        }
512        gdn_cp_commit();
513    };
514    const int c0 = 16 * warp;     // the warp's value columns c0 + gid and c0 + gid + 8 of the tile (v in shared memory)
515    const int ca = cb + c0;       // the same in the head (state, output)
516    // the state slice as S^T fragments: tile j = rows 8j..8j+7; s[j][0] = S[8j + 2tig][c0 + gid], [1] the row after,
517    // [2] column + 8, [3] both (64 scalar loads a lane, 32-B runs, once a launch)
518    float s[16][4];
519#pragma unroll
520    for (int j = 0; j < 16; ++j) {
521        const float* p = state + (size_t) (8 * j + 2 * tig) * rs + (size_t) vh * S + ca + gid;
522        s[j][0] = p[0]; s[j][1] = p[rs]; s[j][2] = p[8]; s[j][3] = p[rs + 8];
523    }
524    stage(0, 0, 3);
525    const float out_scale = rsqrtf((float) S);
526    for (int ch = 0; ch < nch; ++ch) {
527        const int b = ch & 1;
528        const int64_t c_t0 = t0 + (int64_t) ch * GCH;
529        const int n = (int) min((int64_t) GCH, T - c_t0);
530        gdn_cp_wait_all();   // this thread's pieces of chunk ch
531        __syncthreads();     // every thread's: chunk ch's buffer complete; every read of the other one done
532        if (ch + 1 < nch) stage(ch + 1, b ^ 1, SPLIT ? 1 : 3);
533        const uint16_t* bkh = kh + b * GCH * GTC_KP;
534        const uint16_t* bkl = kl + b * GCH * GTC_KP;
535        const uint16_t* bqh = qh + b * GCH * GTC_KP;
536        const uint16_t* bql = ql + b * GCH * GTC_KP;
537        const int bv = SPLIT ? 0 : b;
538        const uint16_t* bth = th + bv * GCH * GTC_PP;
539        const uint16_t* btl = tl + bv * GCH * GTC_PP;
540        const uint16_t* bph = ph + bv * GCH * GTC_PP;
541        const uint16_t* bpl = pl + bv * GCH * GTC_PP;
542        const float* bsv = sv + bv * GCH * GTC_VP;
543        const float4* bsc = sc + b * GCH;
544        // (a) X^T = S0^T K^T and QS^T = S0^T Q^T, the state x 2^es (its largest entry in [2^14, 2^15)).  The lane's
545        // maximum over eight chains (nvcc keeps the order written: one chain was 64 dependent FMNMX before the first mma)
546        float m8[8] = {};
547#pragma unroll
548        for (int j = 0; j < 16; ++j)
549#pragma unroll
550            for (int q = 0; q < 4; ++q) m8[(2 * j + (q >> 1)) & 7] = fmaxf(m8[(2 * j + (q >> 1)) & 7], fabsf(s[j][q]));
551        const float m = fmaxf(fmaxf(fmaxf(m8[0], m8[1]), fmaxf(m8[2], m8[3])), fmaxf(fmaxf(m8[4], m8[5]), fmaxf(m8[6], m8[7])));
552        const int es = gtc_scale_exp(gtc_warp_max(m));
553        float xt[4][4] = {}, qs[4][4] = {};
554        gtc_prod_state<(NP == 1 ? 1 : 3), (NP == 3 ? 3 : 1)>(s, gtc_pow2(es), bkh, bkl, bqh, bql, xt, qs);
555        // (b) Y^T = beta_i (V^T - e'_i X^T), e'_i = exp(gamma_i) 2^-(8 + es): lane (gid, tig) holds tokens
556        // 8nt + 2tig and + 1, columns c0 + gid and + 8
557        const float eS = gtc_pow2(-(8 + es));
558        float yt[4][4];
559#pragma unroll
560        for (int nt = 0; nt < 4; ++nt) {
561            const float4 s0 = gtc_lds128(bsc + 8 * nt + 2 * tig), s1 = gtc_lds128(bsc + 8 * nt + 2 * tig + 1);
562            const float* v = bsv + (8 * nt + 2 * tig) * GTC_VP + c0 + gid;
563            const float e0 = s0.y * eS, e1 = s1.y * eS;
564            yt[nt][0] = s0.x * fmaf(-e0, xt[nt][0], v[0]);
565            yt[nt][1] = s1.x * fmaf(-e1, xt[nt][1], v[GTC_VP]);
566            yt[nt][2] = s0.x * fmaf(-e0, xt[nt][2], v[8]);
567            yt[nt][3] = s1.x * fmaf(-e1, xt[nt][3], v[GTC_VP + 8]);
568        }
569        // (c) D^T = Y^T T^T on f16x3: Y^T x 2^ey (its largest entry in [2^14, 2^15)), T's images x 2^et from the prep;
570        // the accumulator comes out x 2^(ey + et) (T above its diagonal skipped)
571        float my4[4] = {};
572#pragma unroll
573        for (int nt = 0; nt < 4; ++nt)
574#pragma unroll
575            for (int q = 0; q < 4; ++q) my4[q] = fmaxf(my4[q], fabsf(yt[nt][q]));
576        const int ey = gtc_scale_exp(gtc_warp_max(fmaxf(fmaxf(my4[0], my4[1]), fmaxf(my4[2], my4[3]))));
577        float dt[4][4] = {};
578        gtc_prod_dt<true, (NP == 1 ? 1 : 3)>(yt, gtc_pow2(ey), bth, btl, dt);
579        const float sYT = gtc_pow2(-ey) * ste[b];
580#pragma unroll
581        for (int nt = 0; nt < 4; ++nt)
582#pragma unroll
583            for (int q = 0; q < 4; ++q) dt[nt][q] *= sYT;
584        // (d) o^T = e_t QS^T + D^T P^T: the accumulator starts as 2^(ed + 8) e_t QS^T (QS^T came out x 2^(8 + es)),
585        // D^T goes in x 2^ed, P x 2^8; the output is the accumulator x 2^-(ed + 8), then rsqrt(S) as gdn_chunk_scan_kernel
586        float md4[4] = {};
587#pragma unroll
588        for (int nt = 0; nt < 4; ++nt)
589#pragma unroll
590            for (int q = 0; q < 4; ++q) md4[q] = fmaxf(md4[q], fabsf(dt[nt][q]));
591        const int ed = gtc_scale_exp(gtc_warp_max(fmaxf(fmaxf(md4[0], md4[1]), fmaxf(md4[2], md4[3]))));
592        const float sD = gtc_pow2(ed), sO = gtc_pow2(ed + 8), sOi = gtc_pow2(-(ed + 8)), eO = gtc_pow2(ed - es);
593        float ot[4][4];
594#pragma unroll
595        for (int nt = 0; nt < 4; ++nt) {
596            const float e0 = gtc_lds128(bsc + 8 * nt + 2 * tig).y * eO, e1 = gtc_lds128(bsc + 8 * nt + 2 * tig + 1).y * eO;
597            ot[nt][0] = qs[nt][0] * e0; ot[nt][1] = qs[nt][1] * e1; ot[nt][2] = qs[nt][2] * e0; ot[nt][3] = qs[nt][3] * e1;
598        }
599        gtc_prod_dt<true, (NP == 3 ? 3 : 1)>(dt, sD, bph, bpl, ot);
600        float zr[4][4] = {}, gm2[2] = {};
601        if constexpr (NORM) {
602            // the z values and gamma of this lane's elements are loaded now, so that their latency is spent in the barrier below
603#pragma unroll
604            for (int nt = 0; nt < 4; ++nt)
605#pragma unroll
606                for (int q = 0; q < 4; ++q) {
607                    const int t = 8 * nt + 2 * tig + (q & 1);
608                    if (t < n) zr[nt][q] = zg[(c_t0 + t) * rs + (size_t) vh * S + c0 + gid + 8 * (q >> 1)];
609                }
610            gm2[0] = gamma[c0 + gid]; gm2[1] = gamma[c0 + gid + 8];
611            // this lane's two columns of each of its 8 tokens -> partial sums over the warp's 16 columns (the 8 gid lanes), 8 warps in rss
612#pragma unroll
613            for (int nt = 0; nt < 4; ++nt)
614#pragma unroll
615                for (int q = 0; q < 4; ++q) ot[nt][q] = (ot[nt][q] * sOi) * out_scale;
616#pragma unroll
617            for (int nt = 0; nt < 4; ++nt)
618#pragma unroll
619                for (int e = 0; e < 2; ++e) {
620                    float ps = ot[nt][e] * ot[nt][e] + ot[nt][e + 2] * ot[nt][e + 2];
621                    ps += __shfl_xor_sync(0xffffffffu, ps, 4);
622                    ps += __shfl_xor_sync(0xffffffffu, ps, 8);
623                    ps += __shfl_xor_sync(0xffffffffu, ps, 16);
624                    if (gid == 0) rss[warp * GCH + 8 * nt + 2 * tig + e] = ps;
625                }
626        } else {
627#pragma unroll
628            for (int nt = 0; nt < 4; ++nt)
629#pragma unroll
630                for (int q = 0; q < 4; ++q) {
631                    const int t = 8 * nt + 2 * tig + (q & 1);
632                    if (t < n) oc_out[(c_t0 + t) * rs + (size_t) vh * S + ca + gid + 8 * (q >> 1)] = (ot[nt][q] * sOi) * out_scale;
633                }
634        }
635        if (SPLIT) {   // v, T and P of this chunk are dead: every warp is past (d); the next chunk's land during (e)
636            __syncthreads();
637            if (ch + 1 < nch) stage(ch + 1, 0, 2);
638        }
639        if constexpr (NORM) {
640#pragma unroll
641            for (int nt = 0; nt < 4; ++nt)
642#pragma unroll
643                for (int e = 0; e < 2; ++e) {
644                    const int t = 8 * nt + 2 * tig + e;
645                    if (t >= n) continue;
646                    float ss = 0.0f;
647#pragma unroll
648                    for (int w = 0; w < GTC_CW; ++w) ss += rss[w * GCH + t];
649                    const float rn = rsqrtf(ss / (float) S + eps);
650#pragma unroll
651                    for (int hc = 0; hc < 2; ++hc) {
652                        const int col = c0 + gid + 8 * hc;
653                        const float zz = zr[nt][e + 2 * hc];
654                        const float v = ot[nt][e + 2 * hc] * rn * gm2[hc] * (1.0f / (1.0f + __expf(-zz)));
655                        y16[(c_t0 + t) * ld16 + (size_t) vh * S + col] = __half_as_ushort(__float2half_rn(v));
656                    }
657                }
658        }
659        // (e) S^T = eL S0^T + Dd^T K, Dd^T = f_i D^T: the accumulator x (eL 2^(ed + 8)) before (one rounding, as the
660        // s *= gL of gdn_chunk_scan_kernel), Dd^T in x 2^ed, K x 2^8, the accumulator x 2^-(ed + 8) after
661        const float sPre = gtc_lds128(bsc + GCH - 1).y * sO;   // exp(gL)
662#pragma unroll
663        for (int j = 0; j < 16; ++j)
664#pragma unroll
665            for (int q = 0; q < 4; ++q) s[j][q] *= sPre;
666        float dd[4][4];
667#pragma unroll
668        for (int nt = 0; nt < 4; ++nt) {
669            const float f0 = gtc_lds128(bsc + 8 * nt + 2 * tig).z * sD, f1 = gtc_lds128(bsc + 8 * nt + 2 * tig + 1).z * sD;
670            dd[nt][0] = dt[nt][0] * f0; dd[nt][1] = dt[nt][1] * f1; dd[nt][2] = dt[nt][2] * f0; dd[nt][3] = dt[nt][3] * f1;
671        }
672        gtc_update_state<(NP == 1 ? 1 : 3)>(s, dd, bkh, bkl);
673#pragma unroll
674        for (int j = 0; j < 16; ++j)
675#pragma unroll
676            for (int q = 0; q < 4; ++q) s[j][q] *= sOi;
677    }
678#pragma unroll
679    for (int j = 0; j < 16; ++j) {
680        float* p = state + (size_t) (8 * j + 2 * tig) * rs + (size_t) vh * S + ca + gid;
681        p[0] = s[j][0]; p[rs] = s[j][1]; p[8] = s[j][2]; p[rs + 8] = s[j][3];
682    }
683}
684#endif   // !__HIPCC__