libr4d: windowed attention at head_dim 512 with 16 queries per kv head

Gemma 4 12B's full layers at one rank: 16 query heads over one kv head,
in the trunk and in its assistant head. A row pair, its constraints and
emulator cases (scalar, split decode, verify, WMMA prefill, fp8).
This commit is contained in:
keptn42 committed 2026-11-04 07:20:29 +01:00
1 parent b88d370722
commit 326443ca89
5 files changed
+35 -8

No files matched your search

+6 -4
View File
@@ -1,5 +1,6 @@
/* r4d_attn_paged_window.hip -- paged attention at head_dim 128, 256 and 512, 1/2/4/8 queries per
* KV head, any sliding window, causal or not, bf16 or fp8-e4m3 cache, q at any row pitch.
* KV head (16 at 512), any sliding window, causal or not, bf16 or fp8-e4m3 cache, q at any row
* pitch.
*
* head_dim 128 is Unlimited-OCR's decoder (10 query heads over 10 KV heads): the plain MHA case,
* served here so its q slice of the fused q|k|v plane is read in place (attn_paged_qrow) instead
@@ -7,8 +8,9 @@
*
* Gemma 4's text layers: sliding layers are 16 query heads over 8 KV heads at head_dim 256 with a
* 1024-token window (gqa 2 at every TP); full layers are 16 over 2 at head_dim 512 (gqa 8 at
* TP1/TP2, 4 at TP4, 2 at TP8). Where this overlaps the library's other h256 legs, the row table
* decides (r4d_rows.cpp).
* TP1/TP2, 4 at TP4, 2 at TP8). The 12B's full layers are 16 over 1 (gqa 16 at TP1; its one KV
* head is replicated at TP2/TP4, gqa 8/4). Where this overlaps the library's other h256 legs, the
* row table decides (r4d_rows.cpp).
*
* Two kernels behind one entry point:
* - Scalar, for decode and verify: a workgroup owns one (sequence, KV head, slice of BQ query
@@ -1260,7 +1262,7 @@ extern "C" {
}
R4D_WIN_DEF(128, 1)
R4D_WIN_DEF(256, 1) R4D_WIN_DEF(256, 2) R4D_WIN_DEF(256, 4) R4D_WIN_DEF(256, 8)
R4D_WIN_DEF(512, 1) R4D_WIN_DEF(512, 2) R4D_WIN_DEF(512, 4) R4D_WIN_DEF(512, 8)
R4D_WIN_DEF(512, 1) R4D_WIN_DEF(512, 2) R4D_WIN_DEF(512, 4) R4D_WIN_DEF(512, 8) R4D_WIN_DEF(512, 16)
#undef R4D_WIN_DEF
/* attn_paged_qrow_tree: the scalar kernel with a tree verify's mask (TREE). */
+2
View File
@@ -185,6 +185,8 @@ int r4d_rad_attn_window_h512_gqa4_bf16(const RadArgs*, RadStream);
int r4d_rad_attn_window_h512_gqa4_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_window_h512_gqa8_bf16(const RadArgs*, RadStream);
int r4d_rad_attn_window_h512_gqa8_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_window_h512_gqa16_bf16(const RadArgs*, RadStream);
int r4d_rad_attn_window_h512_gqa16_fp8 (const RadArgs*, RadStream);
int64_t r4d_rad_attn_window_h128_scratch(const RadArgs*);
int64_t r4d_rad_attn_window_h256_scratch(const RadArgs*);
int64_t r4d_rad_attn_window_h512_scratch(const RadArgs*);
+6 -4
View File
@@ -4208,10 +4208,10 @@ static const RadConstraint cAttnPrefillH64G4Bf16[] = {
C_EQ("causal", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"),
};
/* The windowed kernels (r4d_attn_paged_window.hip): head_dim 128, 256 and 512, 1/2/4/8 queries per
* KV head, any window, causal or not. Gemma 4's sliding layers are h256/gqa2 with a 1024-token
* window, its full layers h512 at gqa 8 (TP1/TP2), 4 (TP4) or 2 (TP8); h128/gqa1 is Unlimited-OCR's
* MHA decoder. One kernel serves every q_len (splitting the key range when there are too few tiles
* to fill the card), so `q_len` cuts no band. `causal` and `window` are runtime fields; block_size
* KV head (16 at 512), any window, causal or not. Gemma 4's sliding layers are h256/gqa2 with a
* 1024-token window, its full layers h512 at gqa 8 (TP1/TP2), 4 (TP4) or 2 (TP8), the 12B's at
* gqa 16 (TP1); h128/gqa1 is Unlimited-OCR's MHA decoder. One kernel serves every q_len (splitting
* the key range when there are too few tiles to fill the card), so `q_len` cuts no band. `causal` and `window` are runtime fields; block_size
* is derived: 16.
*
* h256 at gqa 4 and 8 take a window only (C_GE 1), leaving the window-0 band to the gqa4 / gqa8
@@ -4241,6 +4241,7 @@ R4D_WIN_C(cAnchorH512G1Bf16, 512, 1, "bf16", 1) R4D_WIN_C(cAnchorH512G1Fp8, 512,
R4D_WIN_C(cAnchorH512G2Bf16, 512, 2, "bf16", 1) R4D_WIN_C(cAnchorH512G2Fp8, 512, 2, "fp8_e4m3", 1)
R4D_WIN_C(cAnchorH512G4Bf16, 512, 4, "bf16", 1) R4D_WIN_C(cAnchorH512G4Fp8, 512, 4, "fp8_e4m3", 1)
R4D_WIN_C(cAnchorH512G8Bf16, 512, 8, "bf16", 1) R4D_WIN_C(cAnchorH512G8Fp8, 512, 8, "fp8_e4m3", 1)
R4D_WIN_C(cWinH512G16Bf16, 512, 16, "bf16", 0) R4D_WIN_C(cWinH512G16Fp8, 512, 16, "fp8_e4m3", 0)
#undef R4D_WIN_C
/* Twelve queries per KV head, head_dim 256 (r4d_attn_paged_h256_gqa12.hip): Qwen3.8-Flash-Next's
* QSA layers, 24 query heads over 2. `q_len <= 5` is the same `q_len * gqa <= 64` bound, tighter
@@ -7232,6 +7233,7 @@ static const RadKernelInfo kKernels[] = {
R4D_WIN_PAIR(128, 1)
R4D_WIN_PAIR(256, 1) R4D_WIN_PAIR(256, 2) R4D_WIN_PAIR(256, 4) R4D_WIN_PAIR(256, 8)
R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, 8)
R4D_WIN_PAIR(512, 16)
#undef R4D_WIN_PAIR
#undef R4D_WIN_ROW
+11
View File
@@ -30052,6 +30052,17 @@ static void device_phase(const Plugin& pl) {
attn_numeric_case(pl, "attn_window_h512_gqa8_bf16kv", 512, 8, 32, "bf16", 0);
attn_numeric_case(pl, "attn_window_h512_gqa8_fp8kv", 512, 8, 1, "fp8_e4m3", 0);
attn_numeric_case(pl, "attn_window_h512_gqa8_bf16kv", 512, 8, 1, "bf16", 0);
/* Gemma 4 12B global at one rank: 16 query heads over its one kv head, in the trunk and in the
* assistant (one row a sequence, a verify of sixteen). Launches, then numeric checks. */
attn_case(pl, "attn_window_h512_gqa16_fp8kv", 512, 16, 32, "fp8_e4m3", 1);
attn_case(pl, "attn_window_h512_gqa16_bf16kv", 512, 16, 32, "bf16", 1);
attn_case(pl, "attn_window_h512_gqa16_fp8kv", 512, 16, 1, "fp8_e4m3", 0);
attn_case(pl, "attn_window_h512_gqa16_bf16kv", 512, 16, 1, "bf16", 0);
attn_numeric_case(pl, "attn_window_h512_gqa16_fp8kv", 512, 16, 32, "fp8_e4m3", 0);
attn_numeric_case(pl, "attn_window_h512_gqa16_bf16kv", 512, 16, 32, "bf16", 0);
attn_numeric_case(pl, "attn_window_h512_gqa16_fp8kv", 512, 16, 1, "fp8_e4m3", 0, 700);
attn_numeric_case(pl, "attn_window_h512_gqa16_bf16kv", 512, 16, 1, "bf16", 0, 700);
attn_numeric_case(pl, "attn_window_h512_gqa16_bf16kv", 512, 16, 16, "bf16", 0, 300);
/* The h256/gqa2 prefill numeric check with ctx 2048 > window 1024, so the window applies: for
* query row 0, pos = 2048-64 = 1984 and the visible keys are [961..1984], so wlo0 = 961 and
* tstart = 961/48 = 20. This exercises the window mask and the below-window tile skip (tiles
+10
View File
@@ -43,6 +43,7 @@ using Fn = int (*)(const RadArgs*, RadStream);
static Fn pick(int hd, int gqa, bool fp8) {
#define P(H, G) if (hd == H && gqa == G) return fp8 ? r4d_rad_attn_window_h##H##_gqa##G##_fp8 : r4d_rad_attn_window_h##H##_gqa##G##_bf16;
P(128, 1) P(256, 1) P(256, 2) P(256, 4) P(256, 8) P(512, 1) P(512, 2) P(512, 4) P(512, 8)
P(512, 16)
#undef P
return nullptr;
}
@@ -197,6 +198,13 @@ int main(int argc, char** argv) {
{"h512 gqa4 full decode, 64 splits, short ctx", 512, 4, 1, false, true, false, false, 0, {4}, {37}, -1, 262144, s512},
{"h256 gqa2 swa w1024 verify, ragged, splits", 256, 2, 2, true, true, true, true, 1024, {3, 5}, {20, 900}, -1, 262144, s256},
{"h512 gqa2 full ragged", 512, 2, 2, false, true, true, false, 0, {3, 1, 9}, {50, 64, 9}},
/* Gemma 4 12B full at TP1: 16 q heads over its one kv head, one position a tile; the
* assistant's full layer is the same geometry at one row a sequence */
{"h512 gqa16 full ragged bf16", 512, 16, 1, false, true, true, false, 0, {3, 1, 9}, {50, 64, 9}},
{"h512 gqa16 full decode split fp8", 512, 16, 1, true, true, false, true, 0, {1}, {700}, -1, 4096, s512},
{"h512 gqa16 full verify16 split", 512, 16, 1, false, true, false, false, 0, {16}, {300}, -1, 1024, s512},
{"h512 gqa16 full decode, 3 seqs, 64 splits", 512, 16, 1, false, true, true, false, 0, {1, 1, 1}, {40, 90, 300}, -1, 262144, s512},
{"h512 gqa16 kvh2 noncausal missing-block", 512, 16, 2, false, false, true, false, 0, {4, 2}, {37, 20}, 1},
/* the coverage the rows claim beyond Gemma: non-causal, two-sided window, a missing block */
{"h256 gqa4 noncausal w20 missing-block", 256, 4, 1, false, false, true, false, 20, {6, 2}, {70, 30}, 2},
{"h512 gqa1 noncausal full", 512, 1, 1, false, false, false, false, 0, {4}, {37}},
@@ -208,6 +216,8 @@ int main(int argc, char** argv) {
{"wmma h512 gqa8 prefill fp8+descale", 512, 8, 1, true, true, false, true, 0, {130}, {130}, -1, 0, 0, 0, 1},
{"wmma h512 gqa2 kvh2 ragged + decode rows", 512, 2, 2, false, true, true, false, 0, {150, 1, 70}, {300, 50, 70}, -1, 0, 0, 0, 1},
{"wmma h512 gqa1 noncausal", 512, 1, 1, false, false, false, false, 0, {64}, {100}, -1, 0, 0, 0, 1},
{"wmma h512 gqa16 prefill 200 of 900", 512, 16, 1, false, true, false, false, 0, {200}, {900}, -1, 262144, s512, 0, 1},
{"wmma h512 gqa16 ragged + decode rows fp8+descale", 512, 16, 1, true, true, true, true, 0, {150, 1, 70}, {300, 50, 70}, -1, 0, 0, 0, 1},
{"wmma h256 gqa2 swa w1024 prefill 300 of 1500", 256, 2, 2, false, true, false, false, 1024, {300}, {1500}, -1, 0, 0, 0, 1},
{"wmma h256 gqa2 swa w64 ragged fp8+descale", 256, 2, 2, true, true, true, true, 64, {100, 90}, {250, 90}, -1, 0, 0, 0, 1},
{"wmma h256 gqa4 noncausal w20 missing-block", 256, 4, 1, false, false, true, false, 20, {80, 70}, {200, 70}, 3, 0, 0, 0, 1},