libr4d, arch: the 31B and DFlash2 with fewer launches a step, same bytes

Attention reads q and the gqa-8 drafter's KV in place (attn_paged_qrow); the 31B's attention
prologue stores k and v itself (qkv_norm_rope_kv_store); DFlash2's norm/rope/cache-store chains
and add-then-norm pairs run as one launch each. Carries the launch-cliff padding until that PR
merges.

Squashed from:

- libr4d: attn_paged_qrow on the split-KV decode kernel -- h256/gqa2, and h128/gqa8 by two groups a kv head
- arch: the 31B's sliding layers and its drafter read through attn_paged_qrow where a card serves it
- libr4d: step past the full-card launch cliff in qkv_norm_rope and the row norms
- libr4d: qkv_norm_rope describes its operands, so rad-kbench and rad-tune can time it
- libr4d, arch/gemma4_bf16: qkv_norm_rope_kv_store -- the 31B's prologue stores k and v itself
- libr4d, arch/common: qkv_norm_rope takes v_norm; DFlash2's query pass folds its q/k chain
- libr4d, arch/common: qkv_norm_rope takes n_head 0; DFlash2's context pass folds its key chain
- libr4d, arch/common: add_rmsnorm; DFlash2 issues each residual add with the norm after it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
AshlynandClaude Opus 5.5 authored and StillDeadcode committed 2026-10-21 09:48:20 +02:00
1 parent 04e5480387
commit 015cba8837
20 files changed
+1332 -164

No files matched your search

+57 -5
View File
@@ -219,7 +219,11 @@ static int64_t decode_scratch_bytes_of(const R4DArgs* r, int which) {
return (int64_t)r4d_attn_decode_h256_gqa6_scratch_bytes(r);
}
static int64_t decode_scratch(const RadArgs* a, int head_dim, int which, long long row = 0) {
/* `row`: a token's bytes in a (block, head) page (fill_paged_row), 0 for 2 * head_dim; `rep`: the
* launch maps each kv head onto `rep` query groups (R4DArgs::kv_rep), so the split law counts
* groups, gqa / rep of them a kv head; `q_pitched`: q may be a slice (attn_paged_qrow). */
static int64_t decode_scratch(const RadArgs* a, int head_dim, int which, long long row = 0,
int rep = 1, bool q_pitched = false) {
const int sp = split_of(a);
if (sp < 0) return R4D_NO(RAD_E_INVAL);
@@ -231,7 +235,8 @@ static int64_t decode_scratch(const RadArgs* a, int head_dim, int which, long lo
r.head_dim = head_dim;
r.q_heads = (int)n_head;
const long long gqa = r4d_geti_or(a, "gqa", 1);
r.kv_heads = (int)(gqa > 0 ? n_head / gqa : n_head);
r.kv_heads = (int)(gqa > 0 ? n_head / gqa : n_head) * rep;
r.kv_rep = rep;
r.num_seqs = (int)max_seqs;
r.q_len = (int)r4d_geti_or(a, "q_len", 1);
r.max_ctx = (int)r4d_geti_or(a, "max_ctx", 0);
@@ -249,8 +254,10 @@ static int64_t decode_scratch(const RadArgs* a, int head_dim, int which, long lo
return worst;
}
int rc = fill_paged_row(a, &r, head_dim, row > 0 ? row : 2LL * head_dim);
int rc = fill_paged_row(a, &r, head_dim, row > 0 ? row : 2LL * head_dim, q_pitched);
if (rc != RAD_OK) return rc;
r.kv_heads *= rep;
r.kv_rep = rep;
r.splits = sp;
return decode_scratch_bytes_of(&r, which);
}
@@ -263,11 +270,17 @@ extern "C" int64_t r4d_rad_attn_decode_h128_scratch(const RadArgs* a) {
return decode_scratch(a, 128, 1);
}
static int decode_common(const RadArgs* a, R4DArgs* r, int head_dim, int which, long long row = 0) {
static int decode_common(const RadArgs* a, R4DArgs* r, int head_dim, int which, long long row = 0,
int rep = 1, bool q_pitched = false) {
const int sp = split_of(a);
if (sp < 0) return R4D_NO(RAD_E_INVAL);
int rc = fill_paged_row(a, r, head_dim, row > 0 ? row : 2LL * head_dim);
int rc = fill_paged_row(a, r, head_dim, row > 0 ? row : 2LL * head_dim, q_pitched);
if (rc != RAD_OK) return rc;
/* a QLD instance reads q_ld whatever q is: dense is the pitch q_heads * head_dim */
if (q_pitched && r->q_ld == 0) r->q_ld = (long)r->q_heads * head_dim;
/* each cache head serves `rep` query groups: the kernel's grid and its gqa check count groups */
r->kv_heads *= rep;
r->kv_rep = rep;
r->splits = sp;
const int64_t need = decode_scratch_bytes_of(r, which);
if (!a->scratch || a->scratch_bytes < need) return R4D_NO(RAD_E_SCRATCH);
@@ -389,3 +402,42 @@ extern "C" int r4d_rad_attn_decode_h128_gqa2w_bf16(const RadArgs* a, RadStream s
if (rc != RAD_OK) return rc;
return r4d_attn_decode_h128_gqa2w_bf16kv(&r, (hipStream_t)s);
}
/* The same decode under attn_paged_qrow: q read at its own row pitch (R4DArgs::q_ld), the sliding
* layers' q where Gemma 4 31B's fused q|k|v projection left it. Same kernel, same arithmetic. */
extern "C" int64_t r4d_rad_attn_qrow_decode_h256_gqa2_scratch(const RadArgs* a) {
return decode_scratch(a, 256, 2, 0, 1, true);
}
extern "C" int r4d_rad_attn_qrow_decode_h256_gqa2_fp8(const RadArgs* a, RadStream s) {
R4DArgs r;
int rc = decode_common(a, &r, 256, 2, 0, 1, true);
if (rc != RAD_OK) return rc;
return r4d_attn_decode_h256_gqa2_qld_fp8kv(&r, (hipStream_t)s);
}
extern "C" int r4d_rad_attn_qrow_decode_h256_gqa2_bf16(const RadArgs* a, RadStream s) {
R4DArgs r;
int rc = decode_common(a, &r, 256, 2, 0, 1, true);
if (rc != RAD_OK) return rc;
return r4d_attn_decode_h256_gqa2_qld_bf16kv(&r, (hipStream_t)s);
}
/* Head_dim 128 at 8 queries per kv head, any window, causal or not, q_len up to 16, under
* attn_paged_qrow: the gqa-4 kernel with each kv head mapped onto two query groups
* (R4DArgs::kv_rep = 2), the 31B's DFlash drafter's attention over a cache that holds each kv head
* once. The workgroup holds q_len * 4 rows, so a block of 16 fits where gqa 8 (128 rows) does not;
* the two groups of a kv head read its blocks twice, as the copies they replace did. */
extern "C" int64_t r4d_rad_attn_qrow_h128_gqa8_scratch(const RadArgs* a) {
return decode_scratch(a, 128, 1, 0, 2, true);
}
extern "C" int r4d_rad_attn_qrow_h128_gqa8_fp8(const RadArgs* a, RadStream s) {
R4DArgs r;
int rc = decode_common(a, &r, 128, 1, 0, 2, true);
if (rc != RAD_OK) return rc;
return r4d_attn_decode_h128_gqa4_rep_fp8kv(&r, (hipStream_t)s);
}
extern "C" int r4d_rad_attn_qrow_h128_gqa8_bf16(const RadArgs* a, RadStream s) {
R4DArgs r;
int rc = decode_common(a, &r, 128, 1, 0, 2, true);
if (rc != RAD_OK) return rc;
return r4d_attn_decode_h128_gqa4_rep_bf16kv(&r, (hipStream_t)s);
}