diff --git a/libr4d/r4d_attn_decode_h256_gqa6.hip b/libr4d/r4d_attn_decode_h256_gqa6.hip index 3371b952..611c6101 100644 --- a/libr4d/r4d_attn_decode_h256_gqa6.hip +++ b/libr4d/r4d_attn_decode_h256_gqa6.hip @@ -650,7 +650,13 @@ void r4dchain_attn_decode(const R4DArgs a, int splits) // to splitkv_combine_body below; only the thread map differs. One wave owns one output row and // each lane HEAD_DIM/32 dims, so a segment is one 16-byte load per lane; a block carries JPB rows. // No barrier and no LDS, so rows in one block may have different contexts and segment counts. -template +// +// SPAN: merge only rows [cu_q[0], cu_q[num_seqs]). A launch over part of a step (the prefill +// kernel's split under ArPieces, or a band-split step's prefill half) gets q and out whole with +// cu_q at its own first row; every other row belongs to another launch, and r4d_q_seq would map it +// onto this launch's first or last sequence and overwrite it. The decode launchers never offset +// cu_q and do not instantiate it. +template __device__ __forceinline__ void splitkv_combine_vec_body(const R4DArgs& a, int splits, int tile, const R4DGateQuant& gq) { @@ -659,6 +665,7 @@ void splitkv_combine_vec_body(const R4DArgs& a, int splits, int tile, const R4DG const int job = blockIdx.x * JPB + (int)(threadIdx.x >> 5); if (job >= r4d_q_total(a) * a.q_heads) return; const int tok = job / a.q_heads; + if (SPAN && a.cu_q && (tok < a.cu_q[0] || tok >= a.cu_q[a.num_seqs])) return; const int seq = r4d_q_seq(a.cu_q, a.num_seqs, a.q_len, tok); const int ctx = a.seqused_k[seq]; uint16_t* outp = (uint16_t*)a.out + (size_t)job * HEAD_DIM + lane * DPL; @@ -808,12 +815,12 @@ void splitkv_combine_vec_body(const R4DArgs& a, int splits, int tile, const R4DG finish(ob); } -template +template __global__ __launch_bounds__(32 * JPB) void r4d_attn_splitkv_combine_vec_kernel(const R4DArgs a, int splits, int tile) { - splitkv_combine_vec_body(a, splits, tile, - R4DGateQuant{}); + splitkv_combine_vec_body(a, splits, tile, + R4DGateQuant{}); } template __global__ __launch_bounds__(32 * JPB) @@ -834,8 +841,9 @@ void r4d_attn_splitkv_combine_vec_gq_kernel(const R4DArgs a, int splits, int til // Merge. PF16=1 reads normalised f16 partials and folds l into the weight; PF16=0 reads // unnormalised f32 partials with l summed separately. Both walk segments in fixed index order so -// rounding never depends on workgroup scheduling. -template +// rounding never depends on workgroup scheduling. SPAN as in the vectorised form (the return is +// block-uniform, ahead of every barrier). +template __device__ __forceinline__ void splitkv_combine_body(const R4DArgs& a, int splits, int tile, const R4DGateQuant& gq) { @@ -843,6 +851,7 @@ void splitkv_combine_body(const R4DArgs& a, int splits, int tile, const R4DGateQ __shared__ float red[HEAD_DIM]; const int job = blockIdx.x, d = threadIdx.x; const int tok = job / a.q_heads; + if (SPAN && a.cu_q && (tok < a.cu_q[0] || tok >= a.cu_q[a.num_seqs])) return; const int seq = r4d_q_seq(a.cu_q, a.num_seqs, a.q_len, tok); const int ctx = a.seqused_k[seq]; @@ -952,11 +961,11 @@ void splitkv_combine_body(const R4DArgs& a, int splits, int tile, const R4DGateQ finish(f32_to_bf16(tot > 0.0f ? av * vd / tot : 0.0f)); } -template +template __global__ __launch_bounds__(HEAD_DIM) void r4d_attn_splitkv_combine_kernel(const R4DArgs a, int splits, int tile) { - splitkv_combine_body(a, splits, tile, R4DGateQuant{}); + splitkv_combine_body(a, splits, tile, R4DGateQuant{}); } template __global__ __launch_bounds__(HEAD_DIM) diff --git a/libr4d/r4d_attn_paged_h256_gqa6.hip b/libr4d/r4d_attn_paged_h256_gqa6.hip index 42326c18..4c4a70f8 100644 --- a/libr4d/r4d_attn_paged_h256_gqa6.hip +++ b/libr4d/r4d_attn_paged_h256_gqa6.hip @@ -51,13 +51,93 @@ extern "C" void r4d_attn_dims(int* head_dim, int* gqa, int* block_size, int* max // (36-48 bytes per lane, ~4% of the prefill). At 2 the spill is 20 bytes, outside the WMMA groups. #define P_OPT 3954187 +// ---- the prefill KV split ---------------------------------------------------------------------- +// The query-tiled grid is ceil(q_len / 64) x kv_heads x num_seqs, one 768-thread workgroup a WGP, +// and each workgroup walks its whole causal range serially. So below one workgroup per WGP a launch +// costs one full walk of the context whatever its row count: Qwen3.8-27B at TP2 (kv_heads 2) runs a +// 53-row tail over 188k keys as 2 workgroups on 32 WGPs, ~17.7 ms a layer, against ~21 ms a +// workgroup for a 1024-row piece filling all 32 (audits 9a/9b; profiled tails, attention: 63 ms +// at 41k / 80 rows, 152 at 97k / 345, 283 at 188k / 53). Such launches split the KV (O_PSPLIT) +// and finish in the split-KV merge. +// +// Workgroups the card runs at once: one a WGP. HIP's multiProcessorCount counts WGPs on RDNA (32 on +// the R9700, see want_wg in r4d_hc_bf16.hip); 32 if it cannot be read. +static int prefill_wgps() { + static const int v = [] { + int dev = 0; + hipDeviceProp_t pr{}; + if (hipGetDevice(&dev) != hipSuccess || hipGetDeviceProperties(&pr, dev) != hipSuccess) + return 32; + return pr.multiProcessorCount > 0 ? pr.multiProcessorCount : 32; + }(); + return v; +} + +// The split law. `units` counts the workgroups that do work: a ragged batch's grid is sized from +// its longest sequence (q_len), but a shorter sequence's query tiles past its end exit at once, so +// the count is bounded by sum ceil(q_s / 64) <= (total_q + 63 n) / 64 as well (exact for one +// sequence), so a ragged launch's longest sequence does not make its others look like full tiles. +// One S applies to every sequence of the launch; a mixed step's decode rows reach the split-KV +// decode row instead, by the arch's per-band issue (AttnGatedFP8::step). A launch over part of a +// step (q and out whole, cu past row 0) counts the step's rows in total_q, so the bound is loose +// there and may keep it unsplit. +// At `wgps` units or more the card is full: no split. Otherwise S rounds of equal workgroups cost +// rounds / S walks, so take one round (S1 = wgps / units) or two (S2 = 2 wgps / units), whichever +// is faster, the smaller S on a tie (fewer partials): 53 rows (2 units) -> 16 splits, 80 -> 8, +// 345 (12 units) -> 5 (60 workgroups, 0.4 walks against 0.5 at 2), 576 -> 3, 1024 and up -> 1. +// Each split keeps at least 8 tiles of max_ctx, the only context the host sees (the engine passes +// the model's bound, so in serving this caps nothing; a short context's splits past its tiles exit +// at once). At most 64, the vectorised merge's limit. +static inline int r4d_prefill_splits_law(int q_len, int total_q, int num_seqs, int kv_heads, + int max_ctx, int wgps) { + constexpr int BQ = (P_NWARPS * 16) / A_GQA; + const long n = std::max(1, num_seqs); + const long qt = std::min(n * ((std::max(1, q_len) + BQ - 1) / BQ), + (std::max(1, total_q) + (BQ - 1) * n) / BQ); + const long units = (long)std::max(1, kv_heads) * std::max(1L, qt); + if (units >= wgps) return 1; + const int s1 = (int)(wgps / units), s2 = (int)(2L * wgps / units); + int sp = s2 > 2 * s1 ? s2 : s1; + const int cap = ((max_ctx > 0 ? max_ctx : (1 << 30) - 1) + P_TILE - 1) / P_TILE / 8; + sp = std::min(sp, std::max(1, cap)); + return std::max(1, std::min(sp, 64)); +} + +// The law at this card's WGP count. A test hook (r4d_selftest), not a plugin entry. +extern "C" int r4d_attn_prefill_h256_gqa6_splits(int q_len, int total_q, int num_seqs, + int kv_heads, int max_ctx) { + return r4d_prefill_splits_law(q_len, total_q, num_seqs, kv_heads, max_ctx, prefill_wgps()); +} +// The split count the last prefill launch in this process used, 1 unsplit. A test hook: lets +// r4d_selftest tell a split launch from an unsplit one whose bytes happen to agree. Not atomic. +static int g_prefill_last_splits = 0; +extern "C" int r4d_attn_prefill_h256_gqa6_last_splits(void) { return g_prefill_last_splits; } + +static int prefill_combine(const R4DArgs* a, int splits, hipStream_t s); + // KVP: 0 = an 8-bit cache (XOPT names fp8_v4 or k6_v4 and the e4m3 legs), 1 = bf16 cache. +// a->splits > 1 with a->scratch set (the plugin entries below, prefill_splits) is the split form: +// segments in the scratch, then the merge. Anything else is the plain form, unchanged. template static int prefill_launch(const R4DArgs* a, hipStream_t s) { if (a->head_dim != A_HEAD_DIM || a->block_size != A_BLOCK_SIZE) return -1; if (a->q_heads / a->kv_heads != A_GQA) return -2; constexpr int BQ = (P_NWARPS * 16) / A_GQA; - dim3 grid((a->q_len + BQ - 1) / BQ, a->kv_heads, a->num_seqs); + // The split form serves the f16 operand legs; the lossy e4m3 legs (O_QK8, O_PV8 rows) stay unsplit + // rather than double their instances. + constexpr bool CAN_SPLIT = !(XOPT & (O_QK8 | O_PV8)); + const int splits = (CAN_SPLIT && a->scratch && a->splits > 1) ? a->splits : 1; + g_prefill_last_splits = splits; + dim3 grid((a->q_len + BQ - 1) / BQ * splits, a->kv_heads, a->num_seqs); + if constexpr (CAN_SPLIT) { + if (splits > 1) { + hipLaunchKernelGGL((r4d_attn_prefill_kernel), + grid, dim3(P_NWARPS * 32), 0, s, *a); + CHK(); + return prefill_combine(a, splits, s); + } + } hipLaunchKernelGGL((r4d_attn_prefill_kernel), grid, dim3(P_NWARPS * 32), 0, s, *a); @@ -102,6 +182,23 @@ static int prefill_legs_launch(const R4DArgs* a, hipStream_t s, int legs) { // 288 it wins by 1.2-20%. The cut is four rows per CU. #define D_VECMIN 256 +// The prefill split's merge: the decode merge over f32 partials (PF16 0), at the prefill tile, so +// it recomputes the kernel's partition; vectorised on the decode launcher's terms. SPAN: the launch +// may cover part of a step's rows (ArPieces' pieces, a band-split step's prefill half: cu_q starts +// past row 0, or ends before total_q), and the rest are another launch's. +static int prefill_combine(const R4DArgs* a, int splits, hipStream_t s) { + const int jobs3 = r4d_q_total(*a) * a->q_heads; + if (splits <= 64 && jobs3 >= D_VECMIN) + hipLaunchKernelGGL((r4d_attn_splitkv_combine_vec_kernel), + dim3((jobs3 + D_JPB - 1) / D_JPB), dim3(32 * D_JPB), 0, s, *a, splits, + P_TILE); + else + hipLaunchKernelGGL((r4d_attn_splitkv_combine_kernel), dim3(jobs3), + dim3(256), splits * sizeof(float), s, *a, splits, P_TILE); + CHK(); + return 0; +} + // Split law: r4d_decode_splits (r4d_attn_decode_h256_gqa6.hip). extern "C" long r4d_attn_decode_h256_gqa6_scratch_bytes(const R4DArgs* a) { return r4d_decode_scratch(a, r4d_decode_splits(a, D_TILE), D_OPT); @@ -171,11 +268,96 @@ extern "C" int r4d_attn_decode_h256_gqa6_k6v4kv(const R4DArgs* a, hipStream_t s) // the .so carries an undefined symbol that only r4d_selftest catches. /* ------------------------------------------------------------------ paged prefill */ +/* The KV split's count before the scratch bound: the case's `splits` parameter when it names one + * (r4d_selftest; the prefill rows declare no such axis, so the engine passes none), else + * R4D_PREFILL_SPLITS, else the law. R4D_PREFILL_SPLITS is a tuning and A/B knob, not + * configuration: 0 or unset = the law, 1 = never split, N = N splits for every launch (at most 64, + * and at most what the scratch holds, see prefill_splits). Read once. */ +static int prefill_splits_want(const RadArgs* a, const R4DArgs* r) { + const int sp = split_of(a); + if (sp > 0) return std::min(sp, 64); + static const int env = [] { + const char* e = getenv("R4D_PREFILL_SPLITS"); + return e && *e ? atoi(e) : 0; + }(); + if (env > 0) return std::min(env, 64); + return r4d_prefill_splits_law(r->q_len, r4d_q_total(*r), r->num_seqs, r->kv_heads, r->max_ctx, + prefill_wgps()); +} + +/* The launch's split count, 1 for the plain form: what prefill_splits_want asks, cut to the + * segments the caller's scratch holds, so a forced count past the declared worst case runs with as + * many as fit rather than writing past the region. No scratch (a row without the hook, a direct + * caller of the r4d_attn_prefill_* entry points) is the plain form. */ +static int prefill_splits(const RadArgs* a, const R4DArgs* r) { + if (!a->scratch || a->scratch_bytes <= 0) return 1; + int sp = prefill_splits_want(a, r); + const long per = r4d_decode_scratch(r, 1, 0); /* one segment of every row, f32 */ + if (sp > 1 && per > 0 && a->scratch_bytes / per < sp) sp = (int)(a->scratch_bytes / per); + return std::max(1, sp); +} + +/* Bytes of the split's f32 partials, at declare the most any launch in the band can take. + * + * Declare passes the band's upper bound as q_len and no tensors, so the worst case is swept over + * every sequence count up to max_seqs and every row count up to the band, in both the uniform form + * (q_len = rows / n) and the ragged one (q_len = min(band, rows), as fill_paged takes it). Only + * launches with fewer than one workgroup a WGP split, and the units of a launch of `t` rows are at + * least kv_heads * ceil(t / 64), so the sweep stops there. The law keeps units * S <= 2 wgps, so + * rows * S <= 64 * gqa * 2 wgps: 24576 rows of 1032 bytes, 25.4 MB, for any kv_heads (23.8 MB is + * reached, at 384 rows x 5 and 640 x 3). Not monotone in the rows, hence the sweep. A launch over + * part of a step's rows counts the whole step's in its partial layout (total_q), more than this + * covers when it is long; prefill_splits then cuts S to what fits, down to the plain form. + * + * Never an error: a refusal would unseat the prefill row, which ran without scratch before the + * split. A shape it cannot read gets none, and its launches run the plain form. */ +static int64_t prefill_scratch(const RadArgs* a, long long row) { + R4DArgs r; + const long long n_head = r4d_geti_or(a, "n_head", 0); + const long long max_seqs = r4d_geti_or(a, "max_seqs", 0); + if (n_head > 0 && max_seqs > 0) { + const long long gqa = r4d_geti_or(a, "gqa", 1); + const int kv = (int)std::max(1LL, gqa > 0 ? n_head / gqa : n_head); + const int band = (int)std::max(1LL, r4d_geti_or(a, "q_len", 1)); + const int mctx = (int)r4d_geti_or(a, "max_ctx", 0); + const int w = prefill_wgps(); + constexpr int BQ = (P_NWARPS * 16) / A_GQA; + const int64_t per_row = (int64_t)n_head * (A_HEAD_DIM + 2) * 4; + int64_t worst = 0; + for (long long n = 1; n <= max_seqs; ++n) + for (long long t = n; t <= band; ++t) { + if ((long long)kv * ((t + BQ - 1) / BQ) >= w) break; + int sp = r4d_prefill_splits_law((int)std::min(band, t), (int)t, (int)n, + kv, mctx, w); + if (t % n == 0) + sp = std::max(sp, r4d_prefill_splits_law((int)(t / n), (int)t, (int)n, kv, + mctx, w)); + if (sp > 1) worst = std::max(worst, (int64_t)t * sp * per_row); + } + return worst; + } + if (fill_paged_row(a, &r, 256, row) != RAD_OK) return 0; + const int sp = prefill_splits_want(a, &r); + return sp > 1 ? (int64_t)r4d_decode_scratch(&r, sp, 0) : 0; +} + +/* By the cache's token row: fp8 and bf16 (2 * head_dim), fp8_v4 (400 bytes), k6_v4 (352). */ +extern "C" int64_t r4d_rad_attn_prefill_h256_scratch(const RadArgs* a) { + return prefill_scratch(a, 512); +} +extern "C" int64_t r4d_rad_attn_prefill_h256_v4_scratch(const RadArgs* a) { + return prefill_scratch(a, 400); +} +extern "C" int64_t r4d_rad_attn_prefill_h256_k6_scratch(const RadArgs* a) { + return prefill_scratch(a, 352); +} + extern "C" int r4d_rad_attn_prefill_h256_fp8(const RadArgs* a, RadStream s) { if (check_dims_once() != RAD_OK) return R4D_NO(RAD_E_SHAPE); R4DArgs r; int rc = fill_paged(a, &r, 256); if (rc != RAD_OK) return rc; + r.splits = prefill_splits(a, &r); /* Absent descales mean 1.0, not an error: the kernels read them that way, vLLM defaults both * scales to 1.0, and kv_store_fp8 at its default scale needs no descale plane. The operands * are optional in the schema and the kernel, so they are optional here too. A calibrated pair @@ -227,6 +409,7 @@ extern "C" int r4d_rad_attn_prefill_h256_bf16(const RadArgs* a, RadStream s) { R4DArgs r; int rc = fill_paged(a, &r, 256); if (rc != RAD_OK) return rc; + r.splits = prefill_splits(a, &r); return r4d_attn_prefill_h256_gqa6_bf16kv(&r, (hipStream_t)s); } @@ -335,6 +518,7 @@ extern "C" int r4d_rad_attn_prefill_h256_fp8v4(const RadArgs* a, RadStream s) { R4DArgs r; int rc = fill_paged_row(a, &r, 256, 400); if (rc != RAD_OK) return rc; + r.splits = prefill_splits(a, &r); return r4d_attn_prefill_h256_gqa6_fp8v4kv(&r, (hipStream_t)s); } @@ -356,6 +540,7 @@ extern "C" int r4d_rad_attn_prefill_h256_k6v4(const RadArgs* a, RadStream s) { R4DArgs r; int rc = fill_paged_row(a, &r, 256, 352); if (rc != RAD_OK) return rc; + r.splits = prefill_splits(a, &r); return r4d_attn_prefill_h256_gqa6_k6v4kv(&r, (hipStream_t)s); } diff --git a/libr4d/r4d_attn_prefill_h256_gqa6.hip b/libr4d/r4d_attn_prefill_h256_gqa6.hip index b4b41590..9d3b69f0 100644 --- a/libr4d/r4d_attn_prefill_h256_gqa6.hip +++ b/libr4d/r4d_attn_prefill_h256_gqa6.hip @@ -69,6 +69,13 @@ // O_V4: the fp8_v4 cache, O_V4 | O_K6 the k6_v4 cache (see r4d_attn_decode_h256_gqa6.hip). #define O_V4 33554432 #define O_K6 67108864 +// O_PSPLIT: a KV split for launches with too few workgroups to fill the card (prefill_splits in +// r4d_attn_paged_h256_gqa6.hip). blockIdx.x is qtile * a.splits + split, and the workgroup walks +// only its segment of the sequence's tiles, cut as r4d_attn_tps cuts them (the partition the +// split-KV merge recomputes from ctx), clipped at its causal end. Instead of bf16 out it stores the +// unnormalised f32 O, m_ref and l in the decode kernel's partial layout (r4d_attn_decode_body, +// !PF16); r4d_attn_splitkv_combine* finishes the rows, so only the summation order changes. +#define O_PSPLIT 536870912 typedef int v2i32_r4d __attribute__((ext_vector_type(2))); // one fp8 WMMA operand (8 e4m3) // pack 4 floats -> 4 e4m3 bytes in element order (RNE, saturating), 2 instructions @@ -97,6 +104,24 @@ __device__ __forceinline__ uint32_t r4d_qcvt(uint32_t w, float s) { __builtin_bit_cast(float, w & 0xffff0000u) * s); } +// O_PSPLIT's store: segment `sp` of row (tok, qhead) in the decode kernel's f32 partial layout, +// O at [idx][HEAD_DIM] then m[nrow] and l[nrow], idx = (tok * q_heads + qhead) * splits + sp. Lane +// (c, h) holds dims 16 dt + 8 h .. + 7 of its row, as at the bf16 store. An empty segment stores +// O = 0, m = -3e38 (at the merge's zero-weight cut) and l = 0. +template +__device__ __forceinline__ void r4d_prefill_store_partial(const R4DArgs& a, int tok, int qhead, + int sp, const v8f* acc, float m, float l, + int h) { + const size_t idx = (size_t)(tok * a.q_heads + qhead) * a.splits + sp; + const size_t nrow = (size_t)r4d_q_total(a) * a.q_heads * a.splits; + float* so = (float*)a.scratch; + float* op = so + idx * HEAD_DIM; + #pragma unroll + for (int dt = 0; dt < NKS; ++dt) + *(v8f*)(op + dt * 16 + 8 * h) = acc[dt]; + if (h == 0) { so[nrow * HEAD_DIM + idx] = m; so[nrow * HEAD_DIM + nrow + idx] = l; } +} + // WIN enables a sliding window (a.window positions ending at each row's causal limit), as in the // decode kernel. Keys below the window are masked, and the KV walk starts at the tile holding the // workgroup's earliest window token. window <= 0 (or WIN off) is full attention. @@ -126,6 +151,8 @@ void r4d_attn_prefill_kernel(const R4DArgs a) constexpr int PV8 = ((OPT & O_PV8) && !KVP) ? 1 : 0; static_assert(!(OPT & (O_QK8 | O_PV8)) || !KVP, "the e4m3 legs need an 8-bit cache, or they compile to the exact form"); + constexpr int SPLIT = (OPT & O_PSPLIT) ? 1 : 0; + static_assert(!SPLIT || !WIN, "the split partition has no window floor (the merge's WIN has)"); static_assert(!QK8 || KWIDE, "QK8 raw-byte staging is written for the wide K fetch"); typedef DT16 D; typedef typename D::frag frag16; @@ -159,7 +186,10 @@ void r4d_attn_prefill_kernel(const R4DArgs a) const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5; const int c = lane & 15, h = lane >> 4; - const int qb = blockIdx.x, kvh = blockIdx.y, seq = blockIdx.z; + // SPLIT: blockIdx.x = qtile * splits + segment; wave-uniform, so the divide is scalar. + const int qb = SPLIT ? (int)blockIdx.x / a.splits : (int)blockIdx.x; + const int sp = SPLIT ? (int)blockIdx.x - qb * a.splits : 0; + const int kvh = blockIdx.y, seq = blockIdx.z; const int ctx = a.seqused_k[seq]; if (ctx <= 0) return; @@ -178,6 +208,19 @@ void r4d_attn_prefill_kernel(const R4DArgs a) const int* __restrict__ bt = a.block_table + (size_t)seq * a.max_blocks; + // SPLIT: tps tiles a segment, as r4d_attn_tps (decode unit, included after this one) cuts + // tiles(ctx). A segment at or past the merge's segment count u = ceil(tiles / tps) is never + // read: exit. One the merge reads but that lies wholly past this workgroup's causal end (an + // early query tile of a multi-tile chunk) must still be written: the merge reads all u segments + // of every row. It is, by the ordinary path: its walk below is empty, so the epilogue stores + // the empty partial (O = 0, m_ref = -3e38, l = 0); the prologue's prefetch of tile tstart reads + // inside the sequence (tstart < tiles) and is dropped. A separate early store measured worse: + // its extra live range spilled k6_v4's next-tile fetch addresses inside the tile loop. + const int sp_tps = SPLIT ? ((ctx + TILE - 1) / TILE + a.splits - 1) / a.splits : 0; + if constexpr (SPLIT) { + if (sp * sp_tps >= (ctx + TILE - 1) / TILE) return; + } + const float kdesc = a.k_descale ? a.k_descale[seq * a.kv_heads + kvh] : 1.0f; const float vdesc = a.v_descale ? a.v_descale[seq * a.kv_heads + kvh] : 1.0f; const float mul = a.scale * kdesc * 1.44269504089f; @@ -223,12 +266,15 @@ void r4d_attn_prefill_kernel(const R4DArgs a) const int qmax_cta = min(qb * BLOCK_Q + BLOCK_Q - 1, qlen - 1); const int klimit_cta = ctx - qlen + qmax_cta; - const int ntiles = (min(klimit_cta + 1, ctx) + TILE - 1) / TILE; + const int ctiles = (min(klimit_cta + 1, ctx) + TILE - 1) / TILE; + // SPLIT: the segment's tiles, clipped at the causal end (empty when it starts past it). + const int ntiles = SPLIT ? min((sp + 1) * sp_tps, ctiles) : ctiles; // First window tile, taken at this workgroup's first query row (the one reaching furthest // back), not the chunk's row 0, so fully masked tiles are never walked. Starts at 0 without a // window. const int wlo0 = (WIN && a.window > 0) ? (ctx - qlen + qb * BLOCK_Q - a.window + 1) : 0; - const int tstart = (WIN && a.window > 0 && wlo0 > 0) ? (wlo0 / TILE) : 0; + const int tstart = SPLIT ? sp * sp_tps + : (WIN && a.window > 0 && wlo0 > 0) ? (wlo0 / TILE) : 0; // ---- staging, split into a global fetch (registers) and an LDS store ------------------------ // The split lets GPREV move the fetch out from between the barriers. @@ -615,6 +661,11 @@ void r4d_attn_prefill_kernel(const R4DArgs a) if (!live) return; l_i += swap16(l_i); + if constexpr (SPLIT) { + // Unnormalised: the merge weighs segments by 2^(m - max m) and applies v_descale itself. + r4d_prefill_store_partial(a, qoff + qpos, qhead, sp, acc, m_ref, l_i, h); + return; + } const float inv = (l_i > 0.0f) ? (vdesc / l_i) : 0.0f; uint16_t* op = (uint16_t*)a.out + ((size_t)(qoff + qpos) * a.q_heads + qhead) * HEAD_DIM; diff --git a/libr4d/r4d_plugin.h b/libr4d/r4d_plugin.h index 73d6bc3c..c8e93c4d 100644 --- a/libr4d/r4d_plugin.h +++ b/libr4d/r4d_plugin.h @@ -121,6 +121,11 @@ int r4d_rad_attn_decode_h256_bf16 (const RadArgs*, RadStream); int r4d_rad_attn_decode_h128_fp8 (const RadArgs*, RadStream); int r4d_rad_attn_decode_h128_bf16 (const RadArgs*, RadStream); int64_t r4d_rad_attn_decode_h256_scratch(const RadArgs*); +/* The prefill rows' KV-split partials (r4d_attn_paged_h256_gqa6.hip, prefill_scratch), by cache + * row: fp8 and bf16, fp8_v4, k6_v4. */ +int64_t r4d_rad_attn_prefill_h256_scratch(const RadArgs*); +int64_t r4d_rad_attn_prefill_h256_v4_scratch(const RadArgs*); +int64_t r4d_rad_attn_prefill_h256_k6_scratch(const RadArgs*); int64_t r4d_rad_attn_decode_h128_scratch(const RadArgs*); /* Gemma 4 dense sliding window (r4d_attn_paged_h256_gqa2.hip): head_dim 256, 2 queries per KV * head. Both bands: decode (q_len*gqa <= 64, verify width 32 against the gqa6 leg's 10) and diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index a6fbb78d..dda19c07 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -6215,7 +6215,8 @@ static const RadKernelInfo kKernels[] = { "bf16 query, fp8-e4m3 kv cache with per-(seq,head) descales, bf16 out", RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL, ROW(cAttnPrefillFp8), NOTUNE, - nullptr, nullptr, r4d_rad_attn_prefill_h256_fp8, nullptr, nullptr, nullptr, + nullptr, nullptr, r4d_rad_attn_prefill_h256_fp8, r4d_rad_attn_prefill_h256_scratch, + nullptr, nullptr, r4d_shape_attn_paged }, /* Lossy: they match only a caller whose `prefill_fp8` accepts their legs. */ @@ -6255,7 +6256,8 @@ static const RadKernelInfo kKernels[] = { "bf16 query, fp8_v4 kv cache (E4M3 K, int4 V in groups of 32 with f16 scales), bf16 out", RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL, ROW(cAttnPrefillFp8v4), NOTUNE, - nullptr, nullptr, r4d_rad_attn_prefill_h256_fp8v4, nullptr, nullptr, nullptr, + nullptr, nullptr, r4d_rad_attn_prefill_h256_fp8v4, r4d_rad_attn_prefill_h256_v4_scratch, + nullptr, nullptr, nullptr }, { "attn_prefill_h256_gqa6_fp8v4_qk8", "attn_paged", "attn", @@ -6297,7 +6299,8 @@ static const RadKernelInfo kKernels[] = { "bf16 query, k6_v4 kv cache (int6 K, int4 V, each in groups of 32 with f16 scales), bf16 out", RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL, ROW(cAttnPrefillK6v4), NOTUNE, - nullptr, nullptr, r4d_rad_attn_prefill_h256_k6v4, nullptr, nullptr, nullptr, + nullptr, nullptr, r4d_rad_attn_prefill_h256_k6v4, r4d_rad_attn_prefill_h256_k6_scratch, + nullptr, nullptr, nullptr }, { "attn_prefill_h256_gqa6_k6v4_qk8", "attn_paged", "attn", @@ -6339,7 +6342,8 @@ static const RadKernelInfo kKernels[] = { "bf16 query, bf16 kv cache, bf16 out", RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL, ROW(cAttnPrefillBf16), NOTUNE, - nullptr, nullptr, r4d_rad_attn_prefill_h256_bf16, nullptr, nullptr, nullptr, + nullptr, nullptr, r4d_rad_attn_prefill_h256_bf16, r4d_rad_attn_prefill_h256_scratch, + nullptr, nullptr, r4d_shape_attn_paged }, { "attn_decode_h256_gqa6_fp8kv", "attn_paged", "attn", diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index e2980dc1..a568c26a 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -1281,6 +1281,256 @@ static void attn_numeric_case(const Plugin& pl, const char* name, int64_t head_d fmt("corrupted ref must fail: max_err=%.5g bad=%d", nmax, nbadc)); } +/* The prefill kernel's KV split (prefill_splits, r4d_attn_paged_h256_gqa6.hip): a launch with too + * few query tiles to fill the card walks the KV in segments, each into f32 partials, and finishes in + * the split-KV merge. `seqs` are {q_len, ctx} each, ragged (cu_seqlens, q_len at the band's bound + * 2048 as the engine issues it) when there are two or more. The row runs three times on the same + * operands: unsplit (`splits` 1), at `force` segments, and at the law's own count, which must be + * what the law hook says (and above one when `law_splits`). Both split runs are checked against the + * oracle on sampled rows (every head of each sequence's rows 0, 62, 63, 64, middle and last) at + * attn_numeric_case's tolerance, and against the unsplit run on every element; the largest + * difference is in the case's name. A chunk long against its context (345 of 345 or 1000 keys) + * gives its early query tiles segments past their causal end, which must be written empty. + * + * `lead` and `tail`: rows of q and out before and after the launch's own, as the arch issues a + * launch over part of a step (ArPieces' two pieces, the prefill half of a band-split mixed step): + * cu_seqlens starts at `lead` and q/out are the whole step. Those rows must come out untouched. */ +struct SplitSeq { int64_t q, ctx; }; +static void attn_split_case(const Plugin& pl, const char* name, const char* kv_dtype, + const std::vector& seqs, int force, bool law_splits, + int64_t lead = 0, int64_t tail = 0) { + if (!sel(name) && !sel("attn_prefill_split")) return; + const RadKernelInfo* r = find(pl, name); + if (!r) { ok(name, false, "row absent"); return; } + std::string tag = std::string(name) + " split"; + for (const SplitSeq& s : seqs) tag += fmt(" %lld/%lld", (long long)s.q, (long long)s.ctx); + tag += fmt(" x%d", force); + if (lead || tail) tag += fmt(" rows %lld+..+%lld", (long long)lead, (long long)tail); + using LawFn = int (*)(int, int, int, int, int); + using LastFn = int (*)(void); + const LawFn law = (LawFn)dlsym(pl.h, "r4d_attn_prefill_h256_gqa6_splits"); + const LastFn last = (LastFn)dlsym(pl.h, "r4d_attn_prefill_h256_gqa6_last_splits"); + if (!law || !last) { ok(tag.c_str(), false, "libr4d lacks the split's test hooks"); return; } + + const int64_t head_dim = 256, gqa = 6, kv_heads = 2, bs = 16, q_heads = kv_heads * gqa; + const int64_t ns = (int64_t)seqs.size(); + const int kv_is_fp8 = std::strcmp(kv_dtype, "bf16") != 0; + const int kvq = !std::strcmp(kv_dtype, "fp8_v4") ? 1 : !std::strcmp(kv_dtype, "k6_v4") ? 2 : 0; + const int v4 = kvq != 0; + const uint32_t kvdt = kv_is_fp8 ? RAD_F8E4M3 : RAD_BF16; + const int ew = kv_is_fp8 ? 1 : 2; + const int64_t rowe = kvq == 2 ? 22 * head_dim / 16 : v4 ? 25 * head_dim / 16 : 2 * head_dim; + const int64_t voff = kvq == 2 ? head_dim * 3 / 4 : head_dim; + const int64_t vsoff = kvq == 2 ? head_dim * 21 / 16 : head_dim * 3 / 2; + const float scale = 1.0f / std::sqrt((float)head_dim); + + int64_t mb = 0, qmax = 0; + std::vector cu((size_t)ns + 1, (int32_t)lead), used((size_t)ns); + for (int64_t s = 0; s < ns; ++s) { + cu[(size_t)s + 1] = cu[(size_t)s] + (int32_t)seqs[(size_t)s].q; + used[(size_t)s] = (int32_t)seqs[(size_t)s].ctx; + mb = std::max(mb, (seqs[(size_t)s].ctx + bs - 1) / bs); + qmax = std::max(qmax, seqs[(size_t)s].q); + } + const int64_t span0 = lead, span1 = cu[(size_t)ns]; /* the launch's own rows */ + const int64_t total_q = span1 + tail; + const bool ragged = ns > 1 || lead || tail; + /* one disjoint run of mb blocks per sequence */ + const std::vector bt = iota_i32(ns * mb); + + /* q and the cache drawn as attn_numeric_case draws them */ + std::vector qh((size_t)(total_q * q_heads * head_dim)); + for (size_t i = 0; i < qh.size(); ++i) qh[i] = st_rand_bf16(i); + const int64_t nelem = ns * mb * kv_heads * bs * rowe; + std::vector kv((size_t)nelem * ew); + for (int64_t e = 0; e < nelem; ++e) { + const size_t bo = (size_t)e * ew; + if (kv_is_fp8) kv[bo] = st_rand_e4m3(e); + else { const uint16_t h = st_rand_bf16(e); kv[bo] = (uint8_t)h; kv[bo + 1] = (uint8_t)(h >> 8); } + } + if (v4) + for (int64_t r0 = 0; r0 < nelem; r0 += rowe) { + uint8_t* row = &kv[(size_t)r0]; + for (int64_t b = 0; b < head_dim / 2; ++b) + row[voff + b] = (uint8_t)(st_rand_e4m3(r0 * 7 + b) * 37u + (uint32_t)b); + for (int64_t g = 0; g < head_dim / 32; ++g) { + const _Float16 s = (_Float16)(0.015625f * (float)(1 + (st_rand_e4m3(r0 + g) & 15))); + std::memcpy(row + vsoff + g * 2, &s, 2); + } + if (kvq == 2) { + for (int64_t b = 0; b < head_dim * 3 / 4; ++b) + row[b] = (uint8_t)(st_rand_e4m3(r0 * 5 + b + 11) * 29u + (uint32_t)(3 * b)); + for (int64_t g = 0; g < head_dim / 32; ++g) { + const _Float16 s = (_Float16)(0.00390625f * (float)(1 + (st_rand_e4m3(r0 + g + 7) & 15))); + std::memcpy(row + head_dim * 5 / 4 + g * 2, &s, 2); + } + } + } + + const size_t on = (size_t)(total_q * q_heads * head_dim); + void* d_q = dalloc(on * 2); + void* d_kv = dalloc(kv.size()); + void* d_o[3] = { dalloc(on * 2), dalloc(on * 2), dalloc(on * 2) }; + if (!d_q || !d_kv || !d_o[0] || !d_o[1] || !d_o[2]) { + ok(tag.c_str(), false, "hipMalloc failed"); + dfree_all(); + return; + } + st_memcpy(d_q, qh.data(), on * 2, hipMemcpyHostToDevice); + st_memcpy(d_kv, kv.data(), kv.size(), hipMemcpyHostToDevice); + + /* sp 0 = the law, else forced; returns the count the launch used */ + auto run = [&](int sp, void* out) { + st_memset(out, 0xff, on * 2); /* NaN: a row nobody writes shows */ + std::vector ts = { + T(d_q, RAD_BF16, { total_q, q_heads, head_dim }), + T(d_kv, kvdt, { ns * mb, kv_heads, bs, rowe }), + TI32({ ns, mb }, bt), + TI32({ ns }, used), + TF32({ ns, kv_heads }, 1.0f), + TF32({ ns, kv_heads }, 1.0f), + ragged ? TI32({ ns + 1 }, cu) : T(nullptr, RAD_I32, { ns + 1 }), + T(out, RAD_BF16, { total_q, q_heads, head_dim }), + }; + if (kvdt == RAD_BF16) { absent(ts[4]); absent(ts[5]); } + if (!ragged) absent(ts[6]); + std::vector ps = { + PI("q_len", ragged ? 2048 : qmax), PI("head_dim", head_dim), PI("gqa", gqa), + PI("block_size", bs), PI("causal", 1), PI("window", 0), PS("q_dtype", "bf16"), + PS("kv_dtype", kv_dtype), + }; + if (sp > 0) ps.push_back(PI("splits", sp)); + /* run_case, with the scratch poisoned: 0x7f7f7f7f is m = 3.4e38, so a segment the merge + * reads but nobody wrote outweighs every real one and the row comes out wrong. Fresh + * device memory is often zero, which the merge would weigh as nothing. */ + RadArgs a; + std::memset(&a, 0, sizeof(a)); + a.t = ts.data(); a.n_t = (int)ts.size(); + a.p = ps.data(); a.n_p = (int)ps.size(); + a.world_size = 1; + const int64_t need = r->scratch ? r->scratch(&a) : 0; + if (need > 0) { + a.scratch = dalloc((size_t)need); + a.scratch_bytes = need; + if (a.scratch) st_memset(a.scratch, 0x7f, (size_t)need); + } + const int rc = r->launch(&a, nullptr); + const hipError_t he = (hipError_t)st_stream_sync(nullptr); + ok(r->name, rc == RAD_OK && he == hipSuccess && need >= 0, + fmt("rc=%d need=%lld%s%s", rc, (long long)need, he != hipSuccess ? " hip=" : "", + he != hipSuccess ? hipGetErrorString(he) : "")); + return last(); + }; + const int s_un = run(1, d_o[0]); + const int s_fo = run(force, d_o[1]); + const int s_lw = run(0, d_o[2]); + const int want = law((int)(ragged ? std::min(2048, total_q) : qmax), (int)total_q, + (int)ns, (int)kv_heads, (int)(mb * bs)); + ok((tag + ": splits 1 / forced / law").c_str(), + s_un == 1 && s_fo == force && s_lw == want && (!law_splits || want > 1), + fmt("launched %d / %d / %d, the law says %d%s", s_un, s_fo, s_lw, want, + law_splits ? " and must split" : "")); + + std::vector o[3]; + for (int k = 0; k < 3; ++k) { + o[k].resize(on); + st_memcpy(o[k].data(), d_o[k], on * 2, hipMemcpyDeviceToHost); + } + const float abs_tol = 1e-3f, rel_tol = 2e-2f; + float dmax[2] = { 0.0f, 0.0f }; + int nbad = 0, nout = 0; + const size_t rowe_o = (size_t)(q_heads * head_dim); + for (size_t i = 0; i < on; ++i) { + if (i < (size_t)span0 * rowe_o || i >= (size_t)span1 * rowe_o) { + for (int k = 0; k < 3; ++k) nout += o[k][i] != 0xffffu; /* not the launch's row */ + continue; + } + const float u = h_bf16(o[0][i]); + for (int k = 1; k < 3; ++k) { + const float d = std::fabs(h_bf16(o[k][i]) - u); + if (!(d <= abs_tol + rel_tol * std::fabs(u))) ++nbad; /* NaN counts */ + if (d > dmax[k - 1]) dmax[k - 1] = d; + } + } + ok(fmt("%s: split against unsplit, max abs diff %.3g forced, %.3g law", tag.c_str(), + dmax[0], dmax[1]).c_str(), nbad == 0 && nout == 0, + fmt("bad=%d/%d, %d elements written outside the launch's rows", nbad, + (int)(2 * (span1 - span0) * rowe_o), nout)); + + float max_err = 0.0f; + int obad = 0, nchk = 0; + for (int64_t s = 0; s < ns; ++s) { + const int64_t q = seqs[(size_t)s].q; + std::vector rows = { 0, 62, 63, 64, q / 2, q - 1 }; + std::sort(rows.begin(), rows.end()); + rows.erase(std::unique(rows.begin(), rows.end()), rows.end()); + for (const int64_t i : rows) { + if (i < 0 || i >= q) continue; + const int64_t tok = cu[(size_t)s] + i; + for (int64_t h = 0; h < q_heads; ++h) { + float ref[256]; + attn_oracle_one(&qh[(size_t)((tok * q_heads + h) * head_dim)], kv.data(), kv_is_fp8, + head_dim, kv_heads, bs, h / gqa, &bt[(size_t)(s * mb)], mb, + (int)seqs[(size_t)s].ctx, 1.0f, 1.0f, 1, 0, scale, q, i, ref, kvq); + for (int64_t d = 0; d < head_dim; ++d) { + const size_t idx = (size_t)((tok * q_heads + h) * head_dim + d); + for (int k = 0; k < 3; ++k) { + const float diff = std::fabs(h_bf16(o[k][idx]) - ref[d]); + if (!(diff <= abs_tol + rel_tol * std::fabs(ref[d]))) ++obad; + if (diff > max_err) max_err = diff; + ++nchk; + } + } + } + } + } + ok((tag + ": all three runs against the oracle").c_str(), obad == 0, + fmt("max_err=%.5g bad=%d/%d", max_err, obad, nchk)); + dfree_all(); +} + +/* The split's scratch at declare (the band's bound 2048, Qwen3.8-27B's per-rank 12 heads over 2, + * the model's context, no tensors): the hook must cover the largest launch the law can split in the + * band, over every sequence count up to max_seqs, and stay near the 25.4 MB bound its comment + * derives. Sizes it in the name. */ +static void attn_split_scratch_case(const Plugin& pl, const char* name, const char* kv_dtype) { + if (!sel(name) && !sel("attn_prefill_split")) return; + const RadKernelInfo* r = find(pl, name); + if (!r) { ok(name, false, "row absent"); return; } + using LawFn = int (*)(int, int, int, int, int); + const LawFn law = (LawFn)dlsym(pl.h, "r4d_attn_prefill_h256_gqa6_splits"); + if (!r->scratch || !law) { + ok((std::string(name) + ": split scratch at declare").c_str(), false, + !r->scratch ? "the row has no scratch hook" : "libr4d lacks the law's test hook"); + return; + } + const int band = 2048, n_head = 12, kv = 2, mctx = 262144; + for (const int ms : { 1, 16, 64 }) { + std::vector ps = { + PI("q_len", band), PI("head_dim", 256), PI("gqa", 6), PI("n_head", n_head), + PI("max_seqs", ms), PI("max_ctx", mctx), PI("block_size", 16), PI("causal", 1), + PI("window", 0), PS("q_dtype", "bf16"), PS("kv_dtype", kv_dtype), + }; + RadArgs a; + std::memset(&a, 0, sizeof(a)); + a.p = ps.data(); a.n_p = (int)ps.size(); + a.world_size = 1; + const int64_t need = r->scratch(&a); + int64_t worst = 0; + for (int n = 1; n <= ms; ++n) + for (int t = n; t <= band; ++t) { + int sp = law(std::min(band, t), t, n, kv, mctx); + if (t % n == 0) sp = std::max(sp, law(t / n, t, n, kv, mctx)); + if (sp > 1) worst = std::max(worst, (int64_t)t * sp * n_head * (256 + 2) * 4); + } + ok(fmt("%s: split scratch at declare, %d sequences: %.2f MB (the law's worst %.2f MB)", name, + ms, need / 1e6, worst / 1e6).c_str(), + worst > 0 && need >= worst && need <= 24576LL * 1032, + fmt("need %lld worst %lld bound %lld", (long long)need, (long long)worst, + 24576LL * 1032)); + } +} + /* The h256/gqa6 prefill over an 8-bit cache with e4m3 products (`prefill_fp8` 1 Q.K^T, 2 P.V, 3 * both), against the inline oracle and against `exact`, the cache's exact row, on the same data. @@ -26116,6 +26366,39 @@ static void device_phase(const Plugin& pl) { attn_numeric_case(pl, "attn_prefill_h256_gqa6_k6v4", 256, 6, 100, "k6_v4", 0, 1024); attn_numeric_case(pl, "attn_decode_h256_gqa6_k6v4", 256, 6, 1, "k6_v4", 0, 1024); attn_numeric_case(pl, "attn_decode_h256_gqa6_k6v4", 256, 6, 4, "k6_v4", 0, 2048); + /* The prefill KV split at Qwen3.8-27B's per-rank geometry, every cache layout: the tails the + * audits measured (11 to 345 rows over 8k to 20k keys, where the law splits), one row through + * the prefill kernel, chunks long against their context (early query tiles own empty + * segments), and ragged batches of a chunk beside decode rows of other contexts. Then the + * declare-time scratch. */ + static const struct { const char* row; const char* kv; } split_rows[] = { + { "attn_prefill_h256_gqa6_k6v4", "k6_v4" }, { "attn_prefill_h256_gqa6_fp8v4", "fp8_v4" }, + { "attn_prefill_h256_gqa6_fp8kv", "fp8_e4m3" }, { "attn_prefill_h256_gqa6_bf16kv", "bf16" }, + }; + for (const auto& sr : split_rows) { + const char* n = sr.row; + const char* kv = sr.kv; + attn_split_case(pl, n, kv, { { 1, 20000 } }, 16, true); + attn_split_case(pl, n, kv, { { 11, 8192 } }, 7, true); + attn_split_case(pl, n, kv, { { 53, 18000 } }, 16, true); + attn_split_case(pl, n, kv, { { 64, 12000 } }, 13, true); + attn_split_case(pl, n, kv, { { 65, 9000 } }, 8, true); + attn_split_case(pl, n, kv, { { 345, 16000 } }, 16, true); + attn_split_case(pl, n, kv, { { 345, 345 } }, 16, false); + attn_split_case(pl, n, kv, { { 345, 1000 } }, 16, true); + attn_split_case(pl, n, kv, { { 640, 2000 } }, 5, true); + attn_split_case(pl, n, kv, { { 53, 12000 }, { 1, 20000 } }, 16, true); + attn_split_case(pl, n, kv, { { 11, 8192 }, { 64, 9000 }, { 1, 12000 } }, 6, true); + attn_split_case(pl, n, kv, { { 300, 600 }, { 1, 10000 } }, 16, true); + /* Launches over part of a step: ArPieces' first piece (rows after it) and second (rows + * before it), and a band-split mixed step's prefill half behind its decode rows. The law + * sees the step's rows (total_q), so it leaves the 512-row pieces unsplit. */ + attn_split_case(pl, n, kv, { { 512, 12000 } }, 4, false, 0, 512); + attn_split_case(pl, n, kv, { { 512, 12512 } }, 4, false, 512, 0); + attn_split_case(pl, n, kv, { { 53, 12000 } }, 16, true, 8, 0); + attn_split_case(pl, n, kv, { { 64, 9000 }, { 11, 8192 } }, 6, true, 3, 0); + attn_split_scratch_case(pl, n, kv); + } /* Flash-Next's QSA layers: twelve queries per KV head over 4-token pages, and six at * four ranks, each layout's prefill (a long history and a ragged last tile), decode, and the * widest verify (q_len 5 at gqa 12, 10 at gqa 6). */