From 44490c859abdf04296f41d0a9d6fe6a266f594d9 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Sat, 31 Oct 2026 10:46:28 +0000 Subject: [PATCH] libr4d: paged attention at head_dim 128 and gqa 16 Nemotron-H's attention layers have 32 query heads over 2 KV heads, gqa 16 at one and two ranks. r4d_attn_paged_h128_gqa16.hip is the gqa8 body at that ratio: prefill blocks of 24 query positions, decode q_len <= 4. Selftest cases at one rank. --- libr4d/CMakeLists.txt | 2 + libr4d/r4d_attn_paged_h128_gqa16.hip | 13 +++++++ libr4d/r4d_plugin.h | 7 ++++ libr4d/r4d_rows.cpp | 55 ++++++++++++++++++++++++++++ libr4d/r4d_selftest.cpp | 10 +++++ 5 files changed, 87 insertions(+) create mode 100644 libr4d/r4d_attn_paged_h128_gqa16.hip diff --git a/libr4d/CMakeLists.txt b/libr4d/CMakeLists.txt index e6fb2d0f..84f1d42c 100644 --- a/libr4d/CMakeLists.txt +++ b/libr4d/CMakeLists.txt @@ -68,6 +68,8 @@ set(R4D_UNITS # Two queries per KV head at head_dim 128 only (Qwen3-1.7B's 16 over 8, Qwen3-ASR's decoder): # the gqa8 body with the ratio set. "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|" "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_h128_gqa16.hip b/libr4d/r4d_attn_paged_h128_gqa16.hip new file mode 100644 index 00000000..65fbdf97 --- /dev/null +++ b/libr4d/r4d_attn_paged_h128_gqa16.hip @@ -0,0 +1,13 @@ +// R4D attention at sixteen queries per KV head, head_dim 128: Nemotron-H's attention layers (32 +// query heads over 2 KV heads) at one and two ranks. 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 = 4 (a workgroup holds 64 (query, head) rows). +// prefill block width 384 / gqa = 24 query positions. +// +// No head_dim 256 leg (R4D_ATTN_H256 0): nothing in scope is gqa 16 at 256. Page size 16. +#define R4D_ATTN_GQA 16 +#define R4D_ATTN_H128 1 +#define R4D_ATTN_H256 0 + +#include "r4d_attn_paged_gqa8.hip" diff --git a/libr4d/r4d_plugin.h b/libr4d/r4d_plugin.h index 90023f34..67c44a93 100644 --- a/libr4d/r4d_plugin.h +++ b/libr4d/r4d_plugin.h @@ -385,6 +385,13 @@ int r4d_rad_attn_prefill_h128_gqa2_bf16(const RadArgs*, RadStream); int r4d_rad_attn_decode_h128_gqa2_fp8 (const RadArgs*, RadStream); int r4d_rad_attn_decode_h128_gqa2_bf16 (const RadArgs*, RadStream); int64_t r4d_rad_attn_decode_h128_gqa2_scratch(const RadArgs*); +/* Sixteen queries per KV head at head_dim 128 (Nemotron-H): the gqa8 body at gqa 16 + * (r4d_attn_paged_h128_gqa16.hip). */ +int r4d_rad_attn_prefill_h128_gqa16_fp8 (const RadArgs*, RadStream); +int r4d_rad_attn_prefill_h128_gqa16_bf16(const RadArgs*, RadStream); +int r4d_rad_attn_decode_h128_gqa16_fp8 (const RadArgs*, RadStream); +int r4d_rad_attn_decode_h128_gqa16_bf16 (const RadArgs*, RadStream); +int64_t r4d_rad_attn_decode_h128_gqa16_scratch(const RadArgs*); /* Twelve queries per KV head, head_dim 256 only: Qwen3.8-Flash-Next's QSA layers. Same body, * another instantiation (r4d_attn_paged_h256_gqa12.hip). */ int r4d_rad_attn_prefill_h256_gqa12_fp8 (const RadArgs*, RadStream); diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index b273c4ec..bea221e4 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -4521,6 +4521,23 @@ static const RadConstraint cAttnDecodeH128G2Bf16[] = { C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 2), C_EQ("block_size", 16), C_GE("causal", 0), C_LE("q_len", 32), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"), }; +/* Sixteen queries per KV head at head_dim 128 (Nemotron-H's 32 over 2): q_len <= 4 at decode. */ +static const RadConstraint cAttnPrefillH128G16Fp8[] = { + C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 16), C_EQ("block_size", 16), + C_EQ("causal", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"), +}; +static const RadConstraint cAttnPrefillH128G16Bf16[] = { + C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 16), C_EQ("block_size", 16), + C_EQ("causal", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"), +}; +static const RadConstraint cAttnDecodeH128G16Fp8[] = { + C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 16), C_EQ("block_size", 16), + C_GE("causal", 0), C_LE("q_len", 4), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"), +}; +static const RadConstraint cAttnDecodeH128G16Bf16[] = { + C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 16), C_EQ("block_size", 16), + C_GE("causal", 0), C_LE("q_len", 4), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"), +}; /* Key rename: libr4d writes C_IN("q_dtype","bf16") and C_IN("kv_dtype","bf16"); docs/OPS.md's * `attn_dense` has one `dtype`. The predicate is unchanged. * @@ -7759,6 +7776,44 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8) nullptr, nullptr, r4d_shape_attn_paged }, +{ "attn_prefill_h128_gqa16_fp8kv", "attn_paged", "attn", + "paged causal attention, query-tiled (prefill / chunked prefill)", + "head_dim 128, 16 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(cAttnPrefillH128G16Fp8), NOTUNE, + nullptr, nullptr, r4d_rad_attn_prefill_h128_gqa16_fp8, nullptr, nullptr, nullptr, + r4d_shape_attn_paged }, + +{ "attn_prefill_h128_gqa16_bf16kv", "attn_paged", "attn", + "paged causal attention, query-tiled (prefill / chunked prefill)", + "head_dim 128, 16 queries per kv head, paged block 16, varlen", + "bf16 query, bf16 kv cache, bf16 out", + RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL, + ROW(cAttnPrefillH128G16Bf16), NOTUNE, + nullptr, nullptr, r4d_rad_attn_prefill_h128_gqa16_bf16, nullptr, nullptr, nullptr, + r4d_shape_attn_paged }, + +{ "attn_decode_h128_gqa16_fp8kv", "attn_paged", "attn", + "paged attention, causal or not, split-KV (decode / speculative verify)", + "head_dim 128, 16 queries per kv head, paged block 16, q_len*gqa <= 64, 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(cAttnDecodeH128G16Fp8), ROW(tAttnSplit), nullptr, + nullptr, nullptr, r4d_rad_attn_decode_h128_gqa16_fp8, r4d_rad_attn_decode_h128_gqa16_scratch, + nullptr, nullptr, + r4d_shape_attn_paged }, + +{ "attn_decode_h128_gqa16_bf16kv", "attn_paged", "attn", + "paged attention, causal or not, split-KV (decode / speculative verify)", + "head_dim 128, 16 queries per kv head, paged block 16, q_len*gqa <= 64, no window", + "bf16 query, bf16 kv cache, bf16 out, f16 partials", + RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_DECODE, + ROW(cAttnDecodeH128G16Bf16), ROW(tAttnSplit), nullptr, + nullptr, nullptr, r4d_rad_attn_decode_h128_gqa16_bf16, r4d_rad_attn_decode_h128_gqa16_scratch, + nullptr, nullptr, + r4d_shape_attn_paged }, + { "attn_vit_h72_bf16", "attn_dense", "attn", "dense non-causal vision-encoder attention, varlen over images / windows", "head_dim 72 native (no padding to 128), multi-head (no GQA), contiguous q/k/v/o, " diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 0b7e2a18..839df4ed 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -30860,6 +30860,16 @@ static void device_phase(const Plugin& pl) { attn_numeric_case(pl, "attn_decode_h128_gqa2_fp8kv", 128, 2, 1, "fp8_e4m3", 0, 256); attn_numeric_case(pl, "attn_decode_h128_gqa2_bf16kv", 128, 2, 1, "bf16", 0, 256); attn_numeric_case(pl, "attn_decode_h128_gqa2_bf16kv", 128, 2, 24, "bf16", 0, 256); + /* Nemotron-H's 32 query heads over 2 at one rank. */ + attn_case(pl, "attn_prefill_h128_gqa16_fp8kv", 128, 16, 32, "fp8_e4m3", 1); + attn_case(pl, "attn_prefill_h128_gqa16_bf16kv", 128, 16, 32, "bf16", 1); + attn_case(pl, "attn_decode_h128_gqa16_fp8kv", 128, 16, 1, "fp8_e4m3", 0); + attn_case(pl, "attn_decode_h128_gqa16_bf16kv", 128, 16, 1, "bf16", 0); + attn_numeric_case(pl, "attn_prefill_h128_gqa16_fp8kv", 128, 16, 64, "fp8_e4m3", 0, 256); + attn_numeric_case(pl, "attn_prefill_h128_gqa16_bf16kv", 128, 16, 64, "bf16", 0, 256); + attn_numeric_case(pl, "attn_decode_h128_gqa16_fp8kv", 128, 16, 1, "fp8_e4m3", 0, 256); + attn_numeric_case(pl, "attn_decode_h128_gqa16_bf16kv", 128, 16, 1, "bf16", 0, 256); + attn_numeric_case(pl, "attn_decode_h128_gqa16_bf16kv", 128, 16, 4, "bf16", 0, 256); attn_gq_fold_case(pl, 3, false); attn_gq_fold_case(pl, 24, false); /* And over the fp8_v4 and k6_v4 caches (Flash-Next's --kv-cache-dtype fp8_v4 / k6_v4). */