attn prefill h256: the small-launch KV split for every GQA geometry

The KV split of the head-256 / GQA-6 prefill kernel (few workgroups -> split the KV walk into segments,
f32 partials, the split-KV merge with a cu span) now serves every head-256 prefill row: GQA 12
(4-token pages: fp8, bf16, fp8_v4, k6_v4), GQA 6 with 4-token pages (the same four), GQA 8, GQA 4 and
GQA 2 (fp8, bf16). The fewer KV heads a rank holds, the more it matters: at TP 2 Qwen4-Exp (GQA 12) and
Qwen3.5-35B-A3B (GQA 8) keep one KV head per rank, so a 53-row tail runs 2 workgroups on 32 WGPs.

The host side (the law, the scratch sweep, the scratch-capped count, R4D_PREFILL_SPLITS, the merge
launch) moves into r4d_attn_prefill_split.h; each unit passes its own geometry (query rows per workgroup
= 24 * 16 / gqa, tile 48). The GQA-6 unit is refactored onto it with the same law (Qwen3.8-27B in the
engine: greedy top-5 logprobs bit-identical before and after at 300 positions after 25k and 41k prompts).

Left unsplit: GQA-2 launches with a sliding window (the partition has no window floor; a 1024 window
walks at most ~26 tiles), the lossy e4m3 legs, and the head-128 / head-64 legs sharing the bodies.

Split count, one sequence, N query rows -> S: GQA 12 with 1 KV head: 1-32 -> 32, 53 -> 16, 160 -> 6,
1024 -> 1; GQA 8 with 1 KV head: 53 -> 16, 345 -> 4; GQA 2 with 4 KV heads: up to 160 -> 8. Scratch at
declare at most 25.4 MB per row. Split vs unsplit: at most 0.000488 (fp8 KV) and 0.0039 (bf16, fp8_v4,
k6_v4; about 1 bf16 ulp).

Tests: the split tests generalised over geometry for all 18 split rows (query lengths 1-640 over 8k-20k
keys, empty segments, ragged batches, pipelined pieces, poisoned scratch, oracle rows at each tile edge,
declare-time scratch): 1782 ok; the mixed-step case over 9 geometry x cache pairs: 54 ok. Every kernel
that existed before is byte-identical (311 of 311).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 committed 2026-10-26 09:25:35 +01:00
1 parent 22df1c7264
commit 96bc0465b3
8 files changed
+433 -226

No files matched your search

+52 -2
View File
@@ -28,6 +28,7 @@ using std::max;
// The decode kernel uses helpers defined in the prefill unit, so prefill is included first.
#include "r4d_attn_prefill_h256_gqa6.hip"
#include "r4d_attn_decode_h256_gqa6.hip"
#include "r4d_attn_prefill_split.h"
#define CHK8() do { hipError_t e_ = hipGetLastError(); if (e_ != hipSuccess) return -100 - (int)e_; } while (0)
@@ -86,12 +87,30 @@ using std::max;
#define G8_D_VECMIN 256
// ---- prefill -----------------------------------------------------------------------------------
// Query positions a workgroup: 384 / gqa.
#define G8_P_BQ ((G8_P_NWARPS * 16) / G8_GQA)
// a->splits > 1 with a->scratch set is the KV split (r4d_attn_prefill_split.h): segments in the
// scratch, then the merge. Only the head_dim 256 entries set it (their rows declare the scratch);
// the 128 and 64 legs have no split instance and always run the plain form, unchanged.
template <int HEAD_DIM, int OPT, int KVP>
static int g8_prefill_launch(const R4DArgs* a, hipStream_t s) {
if (a->head_dim != HEAD_DIM || a->block_size != G8_BLOCK_SIZE) return -1;
if (a->q_heads / a->kv_heads != G8_GQA) return -2;
constexpr int BQ = (G8_P_NWARPS * 16) / G8_GQA;
dim3 grid((a->q_len + BQ - 1) / BQ, a->kv_heads, a->num_seqs);
constexpr int BQ = G8_P_BQ;
constexpr bool CAN_SPLIT = HEAD_DIM == 256;
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<G8_P_NWARPS, G8_P_TILE, HEAD_DIM, G8_GQA,
G8_BLOCK_SIZE, KVP, OPT | O_PSPLIT>),
grid, dim3(G8_P_NWARPS * 32), 0, s, *a);
CHK8();
return r4d_prefill_combine<HEAD_DIM, G8_D_JPB, G8_D_VECMIN>(a, splits, G8_P_TILE, s);
}
}
hipLaunchKernelGGL((r4d_attn_prefill_kernel<G8_P_NWARPS, G8_P_TILE, HEAD_DIM, G8_GQA,
G8_BLOCK_SIZE, KVP, OPT>),
grid, dim3(G8_P_NWARPS * 32), 0, s, *a);
@@ -189,10 +208,36 @@ static int g8_decode_launch(const R4DArgs* a, RadStream stream,
// ============================================================== the plugin ABI entry points
#if R4D_ATTN_H256
/* The head_dim 256 prefill's KV split at this ratio's workgroup height (r4d_attn_prefill_split.h).
* Test hooks (r4d_selftest), not plugin entries, named by ratio and page since gqa 6 exists at 16-
* and 4-token pages in different units: the law at this card's WGP count, and the split count this
* unit's last prefill launch used. */
#define G8_HOOK_(g, b, suf) r4d_attn_prefill_h256_gqa##g##_bs##b##suf
#define G8_HOOK(g, b, suf) G8_HOOK_(g, b, suf)
extern "C" int G8_HOOK(R4D_ATTN_GQA, G8_BLOCK_SIZE, _splits)(int q_len, int total_q, int num_seqs,
int kv_heads, int max_ctx) {
return r4d_prefill_splits_law(G8_P_BQ, G8_P_TILE, q_len, total_q, num_seqs, kv_heads, max_ctx,
r4d_prefill_wgps());
}
extern "C" int G8_HOOK(R4D_ATTN_GQA, G8_BLOCK_SIZE, _last_splits)(void) {
return g_prefill_last_splits;
}
#undef G8_HOOK
#undef G8_HOOK_
/* The split's f32 partials at declare, by the cache's token row (r4d_prefill_scratch). */
static int64_t g8_prefill_scratch(const RadArgs* a, long long row) {
return r4d_prefill_scratch(a, 256, row, G8_P_BQ, G8_P_TILE);
}
extern "C" int64_t GQ_SYM(r4d_rad_attn_prefill_h256_gqa, _scratch)(const RadArgs* a) {
return g8_prefill_scratch(a, 512);
}
extern "C" int GQ_SYM(r4d_rad_attn_prefill_h256_gqa, _fp8)(const RadArgs* a, RadStream s) {
R4DArgs r;
const int rc = fill_paged(a, &r, 256);
if (rc != RAD_OK) return rc;
r.splits = r4d_prefill_splits(a, &r, G8_P_BQ, G8_P_TILE);
/* Absent descales mean 1.0, as on the gqa6 leg. */
return g8_prefill_launch<256, G8_P_OPT_256, 0>(&r, (hipStream_t)s);
}
@@ -200,6 +245,7 @@ extern "C" int GQ_SYM(r4d_rad_attn_prefill_h256_gqa, _bf16)(const RadArgs* a, Ra
R4DArgs r;
const int rc = fill_paged(a, &r, 256);
if (rc != RAD_OK) return rc;
r.splits = r4d_prefill_splits(a, &r, G8_P_BQ, G8_P_TILE);
return g8_prefill_launch<256, G8_P_OPT_256, 1>(&r, (hipStream_t)s);
}
#endif
@@ -397,10 +443,14 @@ extern "C" int GQ_SYM(r4d_rad_attn_decode_h128_gqa, _bf16)(const RadArgs* a, Rad
* the k6_v4 cache (352 bytes: int6 K, int4 V), prefill, decode and decode with the gate and
* quantiser in the merge. */
#define G8_V4_ENTRIES(TAG, ROWB, XOPT) \
extern "C" int64_t GQ_SYM(r4d_rad_attn_prefill_h256_gqa, _##TAG##_scratch)(const RadArgs* a) { \
return g8_prefill_scratch(a, ROWB); \
} \
extern "C" int GQ_SYM(r4d_rad_attn_prefill_h256_gqa, _##TAG)(const RadArgs* a, RadStream s) { \
R4DArgs r; \
const int rc = fill_paged_row(a, &r, 256, ROWB); \
if (rc != RAD_OK) return rc; \
r.splits = r4d_prefill_splits(a, &r, G8_P_BQ, G8_P_TILE); \
return g8_prefill_launch<256, G8_P_OPT_256 | (XOPT), 0>(&r, (hipStream_t)s); \
} \
extern "C" int64_t GQ_SYM(r4d_rad_attn_decode_h256_gqa, _##TAG##_scratch)(const RadArgs* a) { \