Archived
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:
1 parent
b88d370722
commit
326443ca89
5 files changed
+35
-8
No files matched your search
@@ -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). */
|
||||
|
||||
@@ -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
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user