libr4d: windowed attention at head_dim 128 with 4 queries per kv head

LFM3's attention: 16 query heads over 4 kv heads at head_dim 128, in full
layers and in sliding layers with a 256-token window. The decode leg
(r4d_attn_paged_h128_gqa4.hip) serves q_len <= 16 and ranks above; every
longer q_len had no row at this geometry. A row pair (bf16 and int8
cache), its constraints, selftest launches and emulator cases (split
verify, ragged window, int8 cache, non-causal with a missing block, WMMA
prefill with and without the window).
This commit is contained in:
DokterSmol authored and StillDeadcode committed 2026-11-17 18:12:32 +01:00
1 parent 38c182a2ba
commit 660d410196
5 files changed
+40 -6

No files matched your search

+5 -3
View File
@@ -1,10 +1,12 @@
/* r4d_attn_paged_window.hip -- paged attention at head_dim 128, 256 and 512, 1/2/4/8 queries per
* KV head (16 at 512), any sliding window, causal or not, bf16 or int8 cache, q at any row
* KV head (1, 4 or 16 at 128, 16 at 512), any sliding window, causal or not, bf16 or int8 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
* of being lifted into a dense buffer.
* of being lifted into a dense buffer. head_dim 128 at gqa 4 is LFM3's attention (16 query heads
* over 4 KV heads): full layers, and sliding layers with a 256-token window; its decode steps stay
* on r4d_attn_paged_h128_gqa4.hip and every longer q_len lands here.
*
* 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
@@ -1372,7 +1374,7 @@ extern "C" {
}
/* h128 gqa16: Muse Glimmer's sliding layers, 32 query heads over 2 KV heads; the same
* launch_attn the sink variant below already runs at this geometry. */
R4D_WIN_DEF(128, 1) R4D_WIN_DEF(128, 16)
R4D_WIN_DEF(128, 1) R4D_WIN_DEF(128, 4) R4D_WIN_DEF(128, 16)
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, 16)
#undef R4D_WIN_DEF
+2
View File
@@ -161,6 +161,8 @@ int64_t r4d_rad_attn_qrow_h128_gqa8_scratch(const RadArgs*);
* row pitch, so they serve both attn_paged and attn_paged_qrow. */
int r4d_rad_attn_window_h128_gqa1_bf16(const RadArgs*, RadStream);
int r4d_rad_attn_window_h128_gqa1_i8 (const RadArgs*, RadStream);
int r4d_rad_attn_window_h128_gqa4_bf16(const RadArgs*, RadStream);
int r4d_rad_attn_window_h128_gqa4_i8 (const RadArgs*, RadStream);
/* h128 gqa16: Muse Glimmer's 32 query heads over 2 KV heads. */
int r4d_rad_attn_window_h128_gqa16_bf16(const RadArgs*, RadStream);
int r4d_rad_attn_window_h128_gqa16_i8 (const RadArgs*, RadStream);
+6 -2
View File
@@ -4368,7 +4368,9 @@ static const RadConstraint cAttnPrefillH64G4Bf16[] = {
/* The windowed kernels (r4d_attn_paged_window.hip): head_dim 128, 256 and 512, 1/2/4/8 queries per
* 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
* gqa 16 (TP1); h128/gqa1 is Unlimited-OCR's MHA decoder; h128/gqa4 is LFM3 (full layers, and
* sliding ones with a 256-token window), whose decode band (q_len <= 16) stays on the h128/gqa4
* decode leg, which ranks above these rows. 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.
*
@@ -4381,6 +4383,7 @@ static const RadConstraint cAttnPrefillH64G4Bf16[] = {
C_GE("window", WMIN), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", KV), \
};
R4D_WIN_C(cWinH128G1Bf16, 128, 1, "bf16", 0) R4D_WIN_C(cWinH128G1I8, 128, 1, "i8", 0)
R4D_WIN_C(cWinH128G4Bf16, 128, 4, "bf16", 0) R4D_WIN_C(cWinH128G4I8, 128, 4, "i8", 0)
R4D_WIN_C(cWinH128G8Bf16, 128, 8, "bf16", 0) R4D_WIN_C(cWinH128G8I8, 128, 8, "i8", 0)
R4D_WIN_C(cWinH128G16Bf16, 128, 16, "bf16", 0) R4D_WIN_C(cWinH128G16I8, 128, 16, "i8", 0)
R4D_WIN_C(cWinH256G1Bf16, 256, 1, "bf16", 0) R4D_WIN_C(cWinH256G1I8, 256, 1, "i8", 0)
@@ -7436,7 +7439,8 @@ static const RadKernelInfo kKernels[] = {
#define R4D_WIN_PAIR(HD, GQ) \
R4D_WIN_ROW(HD, GQ, bf16kv, bf16, cWinH##HD##G##GQ##Bf16, "bf16") \
R4D_WIN_ROW(HD, GQ, i8kv, i8, cWinH##HD##G##GQ##I8, "int8 (an f16 scale of K and of V per token and head)")
R4D_WIN_PAIR(128, 1) R4D_WIN_PAIR(128, 16) /* Muse Glimmer's 32/2-head sliding layers */
R4D_WIN_PAIR(128, 1) R4D_WIN_PAIR(128, 4)
R4D_WIN_PAIR(128, 16) /* Muse Glimmer's 32/2-head sliding layers */
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)
+11
View File
@@ -30681,6 +30681,17 @@ static void device_phase(const Plugin& pl) {
attn_numeric_case(pl, "attn_window_h512_gqa16_i8kv", 512, 16, 1, "i8", 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);
/* LFM3: 16 query heads over 4 kv heads at head_dim 128, full and with its 256-token window; the
* rows take what the decode leg's band (q_len <= 16) leaves. Launches, then numeric checks, the
* last two with a context past the window. The int8 row has numeric cases only: attn_case
* builds a bf16 or an fp8 cache. */
attn_case(pl, "attn_window_h128_gqa4_bf16kv", 128, 4, 32, "bf16", 1);
attn_case(pl, "attn_window_h128_gqa4_bf16kv", 128, 4, 32, "bf16", 1, 256);
attn_numeric_case(pl, "attn_window_h128_gqa4_i8kv", 128, 4, 32, "i8", 0);
attn_numeric_case(pl, "attn_window_h128_gqa4_bf16kv", 128, 4, 32, "bf16", 0);
attn_numeric_case(pl, "attn_window_h128_gqa4_bf16kv", 128, 4, 17, "bf16", 0, 300);
attn_numeric_case(pl, "attn_window_h128_gqa4_bf16kv", 128, 4, 64, "bf16", 256, 700);
attn_numeric_case(pl, "attn_window_h128_gqa4_i8kv", 128, 4, 64, "i8", 256, 700);
/* 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
+16 -1
View File
@@ -51,7 +51,7 @@ static Fn pick(int hd, int gqa, bool i8, bool ahead) {
return nullptr;
}
#define P(H, G) if (hd == H && gqa == G) return i8 ? r4d_rad_attn_window_h##H##_gqa##G##_i8 : 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(128, 1) P(128, 4) 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;
@@ -237,6 +237,21 @@ int main(int argc, char** argv) {
0, 0, 512, 1},
{"wmma h128 gqa1 swa w1024 ragged", 128, 1, 1, false, true, true, false, 1024, {150, 70},
{300, 70}, -1, 0, 0, 0, 1},
/* LFM3: h128, 16 query heads over 4 KV heads; full layers and a 256-token sliding window.
* Prefill and chunked prefill past the decode leg's band, ragged steps with decode rows, a
* query whose window starts inside the context, int8 cache */
{"h128 gqa4 kvh4 full verify17 split", 128, 4, 4, false, true, false, false, 0, {17}, {300}, -1, 262144, s128},
{"h128 gqa4 kvh4 swa w256 ragged", 128, 4, 4, false, true, true, false, 256, {1, 5, 40}, {600, 77, 300}},
{"h128 gqa4 kvh2 swa w256 i8+descale split", 128, 4, 2, true, true, false, true, 256, {20}, {700}, -1, 4096, s128},
{"h128 gqa4 kvh4 noncausal missing-block", 128, 4, 4, false, false, true, false, 0, {4, 2}, {37, 20}, 1},
{"wmma h128 gqa4 kvh4 full prefill 200 of 900", 128, 4, 4, false, true, false, false, 0, {200}, {900},
-1, 0, 0, 0, 1},
{"wmma h128 gqa4 kvh4 swa w256 prefill 300 of 900", 128, 4, 4, false, true, false, false, 256, {300},
{900}, -1, 0, 0, 0, 1},
{"wmma h128 gqa4 kvh4 swa w256 ragged + decode rows", 128, 4, 4, false, true, true, false, 256,
{150, 1, 70}, {500, 50, 70}, -1, 0, 0, 0, 1},
{"wmma h128 gqa4 kvh2 swa w256 i8+descale", 128, 4, 2, true, true, true, true, 256, {100, 90},
{400, 90}, -1, 0, 0, 0, 1},
/* Gemma 4 sliding: h256 gqa2, causal, windowed; a ragged mixed step */
{"h256 gqa2 swa w64 ragged bf16", 256, 2, 2, false, true, true, false, 64, {1, 5, 40}, {200, 77, 140}},
{"h256 gqa2 swa w64 ragged i8+descale", 256, 2, 2, true, true, true, true, 64, {1, 5, 40}, {200, 77, 140}},