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",