attn prefill h256 gqa6: split the KV walk when the launch has too few workgroups

The prefill attention kernel gives each workgroup 64 query rows of one KV head and walks the whole
causal context serially. A launch with few query rows therefore has few workgroups: the tail chunk
of a prompt (53 rows) or a follow-up turn of a few hundred tokens runs 2-10 workgroups on 32 WGPs,
each walking up to 188k keys (17.7 ms per layer at 188k).

When kv_heads * qtiles * seqs is below the WGP count, the launch now splits the KV range into S
segments (blockIdx.x = qtile * S + segment), writes unnormalised f32 partials (O, m, l) in the
decode partial layout and merges them with the existing split-KV combine. S is the smaller of one or
two rounds of workgroups, capped by the context's tiles and the declared scratch. A segment past a
workgroup's causal end still writes an empty partial; the merge reads every segment it counts.
The merges take a SPAN parameter so a launch over part of a step (pipelined pieces) only rewrites its
own rows; the decode instances are unchanged (identical ISA).

R4D_PREFILL_SPLITS: unset/0 = the law, 1 = off, N = force N (debug/tuning).

Measured on 2x Radeon AI PRO R9700, Qwen3.8-27B MXFP4, TP 2, k6_v4 KV:
- r4d_selftest --perfpaged, off -> on: 53 rows over 40k 3.71 -> 0.47 ms, 80 rows 3.64 -> 0.68 ms,
  345 rows 3.80 -> 1.79 ms, 1024 rows 4.29 -> 4.29 ms (no split).
- Engine, 188k prompt, time to first token: 62.78 -> 62.54 s (the tail chunk).
- KLD vs the bf16 reference: chunk 64 0.040384 -> 0.040337, chunk 2048 0.040237 -> 0.040220;
  top-1 agreement 93.985 -> 93.998 %. Split vs unsplit outputs differ by at most 1 bf16 ulp.
- With R4D_PREFILL_SPLITS=1 the engine is bit-identical to before (top-5 logprobs, 100 positions).

Tests: attn_split_case / attn_split_scratch_case for k6_v4, fp8_v4, fp8 and bf16 KV (q 1-345 over
8k-20k keys, empty segments, ragged batches, pipelined pieces), scratch poisoned; 410/410 ok.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 authored and StillDeadcode committed 2026-10-26 09:20:22 +01:00
1 parent 64c652f01e
commit 6a7824a837
6 files changed
+553 -16

No files matched your search

+17 -8
View File
@@ -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<int HEAD_DIM, int CHAINS, int PF16, int JPB, int WIN, int GQ>
//
// 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<int HEAD_DIM, int CHAINS, int PF16, int JPB, int WIN, int GQ, int SPAN = 0>
__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<int HEAD_DIM, int CHAINS, int PF16, int JPB, int WIN = 0>
template<int HEAD_DIM, int CHAINS, int PF16, int JPB, int WIN = 0, int SPAN = 0>
__global__ __launch_bounds__(32 * JPB)
void r4d_attn_splitkv_combine_vec_kernel(const R4DArgs a, int splits, int tile)
{
splitkv_combine_vec_body<HEAD_DIM, CHAINS, PF16, JPB, WIN, 0>(a, splits, tile,
R4DGateQuant{});
splitkv_combine_vec_body<HEAD_DIM, CHAINS, PF16, JPB, WIN, 0, SPAN>(a, splits, tile,
R4DGateQuant{});
}
template<int HEAD_DIM, int CHAINS, int PF16, int JPB, int WIN = 0>
__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<int HEAD_DIM, int CHAINS, int PF16, int WIN, int GQ>
// rounding never depends on workgroup scheduling. SPAN as in the vectorised form (the return is
// block-uniform, ahead of every barrier).
template<int HEAD_DIM, int CHAINS, int PF16, int WIN, int GQ, int SPAN = 0>
__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<int HEAD_DIM, int CHAINS, int PF16, int WIN = 0>
template<int HEAD_DIM, int CHAINS, int PF16, int WIN = 0, int SPAN = 0>
__global__ __launch_bounds__(HEAD_DIM)
void r4d_attn_splitkv_combine_kernel(const R4DArgs a, int splits, int tile)
{
splitkv_combine_body<HEAD_DIM, CHAINS, PF16, WIN, 0>(a, splits, tile, R4DGateQuant{});
splitkv_combine_body<HEAD_DIM, CHAINS, PF16, WIN, 0, SPAN>(a, splits, tile, R4DGateQuant{});
}
template<int HEAD_DIM, int CHAINS, int PF16, int WIN = 0>
__global__ __launch_bounds__(HEAD_DIM)