From f3e4d11872ab1fa84b24c7a3c42d7ff2ebdd4c9d Mon Sep 17 00:00:00 2001 From: behrooz Date: Fri, 30 Oct 2026 07:12:05 +0000 Subject: [PATCH] libr4d: gqa 7 at head_dim 128 Nanbeige4.3-33B is 56 query heads over 8 KV heads at head_dim 128: seven queries a KV head, the same ratio at one rank and at two. The gqa-8 unit cannot express it -- its decode body holds 64 rows of q_len * gqa, and 64 is not a multiple of 7 -- so instantiate the shared body at 21 warps: 336 rows, the same 48 query positions a block as gqa 8, and a 63-row decode cap (nine query positions). Four rows: prefill and decode over bf16 and fp8-e4m3 KV. The gqa-8 defaults stay as they were. G8_MAX_DECODE_ROWS, G8_P_NWARPS and G8_P_TILE become #ifndef guards the including unit can win; nothing changes for gqa 8, which is the only other unit including this file today. The rows state the ratio and both bounds, so a verify wider than nine query positions takes the prefill kernel. Without this no checkpoint with that geometry serves at all: declare names no row for it, and the server stops in startup with nothing selected for attn_paged. Tested against the model's own releases, which need no change to the engine. llama.cpp 0.9.0-dev on the same two Radeon AI PRO R9700 measures 1192 tok/s prefill and 23.1 tok/s decode: official FP8, TP=2 34 GiB 4014 tok/s prefill 33.3 tok/s decode official INT4, TP=2 20 GiB 3677 tok/s prefill 53.2 tok/s decode official INT4, TP=1 20 GiB 2355 tok/s prefill 29.6 tok/s decode On the FP8 release (compressed-tensors FP8_BLOCK, imported as it ships), 17 * 23 -> 391, 9.11 against 9.8 -> 9.8, 100 - 9 -> 91, every one ending on eos, matching llama.cpp. The INT4 release does not import as it stands: it carries u4*bf16[1x32]-u4[1x32] while these linears read u4*f16[1x128]-u4[1x128], so a recipe re-blocks it onto the served grid, which measures 9.5e-02 relative Frobenius (both grids are four bits, so the bytes are the same and only the scales move). The answers stay right at that error, and moving about half the bytes a token it decodes faster on the coarser codes than fp8 does. At 20 GiB it also fits one card. A Q5_K_M GGUF of this model is no use as a way in: it wants an arch plugin and a conversion of values that are already quantised, and even then the same questions come out 7 * 3, 9.1 against 9.1 and 10 - 9. The two releases above are the way in; nothing here depends on the GGUF. Also: the gqa 7 rows against libref on the real 33B weights, 216/216 cases, and rad-kbench -m with every fusion byte-identical (worst rel-L2 2.08e-3). --- libr4d/CMakeLists.txt | 3 ++ libr4d/r4d_attn_paged_gqa8.hip | 8 +++- libr4d/r4d_attn_paged_h128_gqa7.hip | 23 +++++++++++ libr4d/r4d_plugin.h | 7 ++++ libr4d/r4d_rows.cpp | 63 +++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 libr4d/r4d_attn_paged_h128_gqa7.hip diff --git a/libr4d/CMakeLists.txt b/libr4d/CMakeLists.txt index 5eb1d250..1fe12533 100644 --- a/libr4d/CMakeLists.txt +++ b/libr4d/CMakeLists.txt @@ -70,6 +70,9 @@ set(R4D_UNITS "r4d_attn_paged_h128_gqa2|" # Sixteen queries per KV head at head_dim 128 (Nemotron-H's 32 over 2 at one or two ranks). "r4d_attn_paged_h128_gqa16|" + # Seven queries per KV head at head_dim 128: Nanbeige4.3-33B (56 over 8). Seven divides neither + # 384 nor 64, so the unit runs 21 warps and caps a decode workgroup at 63 rows; see its header. + "r4d_attn_paged_h128_gqa7|" "r4d_attn_vit_h72_bf16|" # An encoder's dense attention (bidirectional, GQA in the workgroup, a two-sided window) and # the small ops around it: act_mul, seg_mean, qk_norm_rope with a v norm, the f32 widening. diff --git a/libr4d/r4d_attn_paged_gqa8.hip b/libr4d/r4d_attn_paged_gqa8.hip index 34d6efbc..6aaf7a61 100644 --- a/libr4d/r4d_attn_paged_gqa8.hip +++ b/libr4d/r4d_attn_paged_gqa8.hip @@ -73,13 +73,19 @@ using std::max; #define G8_BLOCK_SIZE 16 #endif /* q_len * gqa a single decode workgroup can hold: q_len <= 8 here. An including unit may raise it - * to 128 (eight waves): gqa 16 then verifies 7 drafts in the decode kernel, as gqa 8 does. */ + * to 128 (eight waves): gqa 16 then verifies 7 drafts in the decode kernel, as gqa 8 does. A unit + * may also lower it (see the gqa7 unit: 63 rows and 21 warps at gqa 7), so each bound below is a + * guard the including unit can win; the defaults are the numbers the row table states for gqa 8. */ #ifndef G8_MAX_DECODE_ROWS #define G8_MAX_DECODE_ROWS 64 #endif +#ifndef G8_P_NWARPS #define G8_P_NWARPS 24 +#endif +#ifndef G8_P_TILE #define G8_P_TILE 48 +#endif // K prefetch 2 at head_dim 256, as in the h256/gqa6 prefill (P_OPT there): 4 spills the m-tile // loop. The head_dim-128 set keeps prefetch 4, which fits. #define G8_P_OPT_256 3954187 diff --git a/libr4d/r4d_attn_paged_h128_gqa7.hip b/libr4d/r4d_attn_paged_h128_gqa7.hip new file mode 100644 index 00000000..95bb6261 --- /dev/null +++ b/libr4d/r4d_attn_paged_h128_gqa7.hip @@ -0,0 +1,23 @@ +// R4D attention at seven queries per KV head, head_dim 128: Nanbeige4.3-33B (56 query heads over +// 8 KV heads). Geometry only; r4d_attn_paged_gqa8.hip is the body. The ratio changes two bounds, +// both stated in the row table: +// +// decode verify width 64 / gqa = 9 query positions (63 rows of q_len * gqa) +// prefill block width 336 / gqa = 48 query positions +// +// The gqa-8 unit runs 24 warps because ROWS = NWARPS * 16 is both the tile's query rows and a +// multiple of the ratio. Seven divides neither 384 (24 warps) nor 64, so this unit takes 21 warps +// -- 336 rows, the same 48 query positions a block as the gqa-8 unit, at seven eighths of the +// rows -- and a 63-row decode cap, which is nine query positions rather than eight. Both numbers +// are compile-time geometry (BLOCK_Q = ROWS / GQA in the prefill kernel); a 24-warp instantiation +// would leave six rows mapping outside the tile's query positions, so they are not tuning knobs. +// +// No head_dim 256 leg: Nanbeige is 128 throughout, and a second instantiation would widen the +// module for a geometry nothing serves. +#define R4D_ATTN_GQA 7 +#define R4D_ATTN_H128 1 +#define R4D_ATTN_H256 0 +#define G8_MAX_DECODE_ROWS 63 +#define G8_P_NWARPS 21 + +#include "r4d_attn_paged_gqa8.hip" diff --git a/libr4d/r4d_plugin.h b/libr4d/r4d_plugin.h index ca1ca60f..d25635a1 100644 --- a/libr4d/r4d_plugin.h +++ b/libr4d/r4d_plugin.h @@ -370,6 +370,13 @@ int r4d_rad_attn_prefill_h128_gqa8_bf16(const RadArgs*, RadStream); int r4d_rad_attn_decode_h128_gqa8_fp8 (const RadArgs*, RadStream); int r4d_rad_attn_decode_h128_gqa8_bf16 (const RadArgs*, RadStream); int64_t r4d_rad_attn_decode_h128_gqa8_scratch(const RadArgs*); +/* SEVEN queries a KV head, head_dim 128 -- Nanbeige4.3-33B (56 over 8). Same body at 21 warps and + * a 63-row decode cap (r4d_attn_paged_h128_gqa7.hip). */ +int r4d_rad_attn_prefill_h128_gqa7_fp8 (const RadArgs*, RadStream); +int r4d_rad_attn_prefill_h128_gqa7_bf16(const RadArgs*, RadStream); +int r4d_rad_attn_decode_h128_gqa7_fp8 (const RadArgs*, RadStream); +int r4d_rad_attn_decode_h128_gqa7_bf16 (const RadArgs*, RadStream); +int64_t r4d_rad_attn_decode_h128_gqa7_scratch(const RadArgs*); /* ONE query a KV head, head_dim 128 -- plain MHA, the Unlimited-OCR decoder (10 over 10). Both * legs from the generic body instantiated at ratio 1 (r4d_attn_paged_h128_gqa1.hip). */ int r4d_rad_attn_prefill_h128_gqa1_fp8 (const RadArgs*, RadStream); diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index fdd53f82..cbe9c273 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -4593,6 +4593,28 @@ static const RadConstraint cAttnDecodeH128G8Bf16[] = { C_GE("causal", 0), C_LE("q_len", 8), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"), }; +/* Seven queries per kv head at head_dim 128 (Nanbeige4.3-33B: 56 query heads over 8), from + * r4d_attn_paged_h128_gqa7.hip. Same body and the same 48 query positions a prefill block as the + * gqa-8 pair above, at 21 warps rather than 24 because 7 does not divide 384 rows. The decode leg + * holds 63 rows of q_len * gqa, so nine query positions where gqa 8 stops at eight; a wider verify + * takes the prefill kernel. */ +static const RadConstraint cAttnPrefillH128G7Fp8[] = { + C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 7), C_EQ("block_size", 16), + C_EQ("causal", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"), +}; +static const RadConstraint cAttnPrefillH128G7Bf16[] = { + C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 7), C_EQ("block_size", 16), + C_EQ("causal", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"), +}; +static const RadConstraint cAttnDecodeH128G7Fp8[] = { + C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 7), C_EQ("block_size", 16), + C_GE("causal", 0), C_LE("q_len", 9), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"), +}; +static const RadConstraint cAttnDecodeH128G7Bf16[] = { + C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 7), C_EQ("block_size", 16), + C_GE("causal", 0), C_LE("q_len", 9), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"), +}; + /* head_dim 128 at ratio ONE: plain MHA (the Unlimited-OCR decoder, 10 query heads over 10), from * r4d_attn_paged_h128_gqa1.hip. The decode leg caps at 64 query rows, which at this ratio is a * plain q_len <= 64; the prefill leg has no query-row cap. */ @@ -7948,6 +7970,47 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8) nullptr, nullptr, r4d_shape_attn_paged }, +/* Seven queries per kv head at head_dim 128, from r4d_attn_paged_h128_gqa7.hip: Nanbeige4.3-33B, + * 56 query heads over 8, the same ratio at one or two ranks. 48 query positions a prefill block + * (21 warps), and a decode workgroup of 63 rows -- nine query positions. */ +{ "attn_prefill_h128_gqa7_fp8kv", "attn_paged", "attn", + "paged causal attention, query-tiled (prefill / chunked prefill)", + "head_dim 128, 7 queries per kv head, paged block 16, varlen", + "bf16 query, fp8-e4m3 kv cache with per-(seq,head) descales, bf16 out", + RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL, + ROW(cAttnPrefillH128G7Fp8), NOTUNE, + nullptr, nullptr, r4d_rad_attn_prefill_h128_gqa7_fp8, nullptr, nullptr, nullptr, + r4d_shape_attn_paged }, + +{ "attn_prefill_h128_gqa7_bf16kv", "attn_paged", "attn", + "paged causal attention, query-tiled (prefill / chunked prefill)", + "head_dim 128, 7 queries per kv head, paged block 16, varlen", + "bf16 query, bf16 kv cache, bf16 out", + RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL, + ROW(cAttnPrefillH128G7Bf16), NOTUNE, + nullptr, nullptr, r4d_rad_attn_prefill_h128_gqa7_bf16, nullptr, nullptr, nullptr, + r4d_shape_attn_paged }, + +{ "attn_decode_h128_gqa7_fp8kv", "attn_paged", "attn", + "paged attention, causal or not, split-KV (decode / speculative verify / draft block)", + "head_dim 128, 7 queries per kv head, paged block 16, q_len*gqa <= 63, no window", + "bf16 query, fp8-e4m3 kv cache with per-(seq,head) descales, bf16 out, f16 partials", + RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_DECODE, + ROW(cAttnDecodeH128G7Fp8), ROW(tAttnSplit), nullptr, + nullptr, nullptr, r4d_rad_attn_decode_h128_gqa7_fp8, r4d_rad_attn_decode_h128_gqa7_scratch, + nullptr, nullptr, + r4d_shape_attn_paged }, + +{ "attn_decode_h128_gqa7_bf16kv", "attn_paged", "attn", + "paged attention, causal or not, split-KV (decode / speculative verify / draft block)", + "head_dim 128, 7 queries per kv head, paged block 16, q_len*gqa <= 63, no window", + "bf16 query, bf16 kv cache, bf16 out, f16 partials", + RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_DECODE, + ROW(cAttnDecodeH128G7Bf16), ROW(tAttnSplit), nullptr, + nullptr, nullptr, r4d_rad_attn_decode_h128_gqa7_bf16, r4d_rad_attn_decode_h128_gqa7_scratch, + nullptr, nullptr, + r4d_shape_attn_paged }, + { "attn_prefill_h128_gqa2_fp8kv", "attn_paged", "attn", "paged causal attention, query-tiled (prefill / chunked prefill)", "head_dim 128, 2 queries per kv head, paged block 16, varlen",