kernels: paged attention at h512/gqa8 and h256/gqa2, windowed prefill band

head 512 (gqa 8) — a head width the library had not compiled before — for the
global layers, head 256 (gqa 2) for sliding-window decode, plus the >=32-token
windowed prefill fix. Numeric selftests vs an inline port of ref_attn_paged
with negative control; env-gated attn-verify probes (R4D_VERIFY_ATTN, default
off) at the decode entries.
This commit is contained in:
sfjes committed 2026-10-10 00:36:08 -07:00
1 parent 8b825a941c
commit dd3310849a
6 files changed
+493 -14

No files matched your search

+45 -8
View File
@@ -28,6 +28,10 @@ using std::max;
#include "r4d_attn_prefill_h256_gqa6.hip"
#include "r4d_attn_decode_h256_gqa6.hip"
/* DEBUG (env-gated, R4D_VERIFY_OFF): defined in r4d_model_bf16.hip, same .so. Recomputes the
* attention on the host from the same q / paged kv the kernel read and reports the max deviation. */
void verify_attn_paged(const R4DArgs* r, long stream, int kv_is_fp8, int is_decode);
#define CHK() do { hipError_t e_ = hipGetLastError(); if (e_ != hipSuccess) return -100 - (int)e_; } while (0)
// The geometry every kernel in this translation unit is compiled for. HEAD_DIM and GQA appear in
@@ -251,6 +255,15 @@ extern "C" int r4d_rad_attn_prefill_h256_bf16(const RadArgs* a, RadStream s) {
* step off its pothole doubles the splits at one count and not at the next. A step may carry any
* count up to the bound, so the region is the largest over all of them. The laws are closed forms
* and this runs at declare, so the sweep costs nothing a step. */
// One hook per geometry but one shared (seqs x heads x rows) partial layout, so the worst-over-
// the-batch sweep below asks each geometry its own bytes in one place instead of triplicating the
// sweep. which: 0 = h256/gqa6, 1 = h128/gqa4, 2 = h256/gqa2 (the Gemma 4 windowed leg).
static int64_t decode_scratch_bytes_of(const R4DArgs* r, int which) {
if (which == 1) return (int64_t)r4d_attn_decode_h128_gqa4_scratch_bytes(r);
if (which == 2) return (int64_t)r4d_attn_decode_h256_gqa2_scratch_bytes(r);
return (int64_t)r4d_attn_decode_h256_gqa6_scratch_bytes(r);
}
static int64_t decode_scratch(const RadArgs* a, int head_dim, int which) {
const int sp = split_of(a);
if (sp < 0) return R4D_NO(RAD_E_INVAL);
@@ -275,8 +288,7 @@ static int64_t decode_scratch(const RadArgs* a, int head_dim, int which) {
int64_t worst = 0;
for (long long n = 1; n <= max_seqs; ++n) {
r.num_seqs = (int)n;
const int64_t need = which == 0 ? r4d_attn_decode_h256_gqa6_scratch_bytes(&r)
: r4d_attn_decode_h128_gqa4_scratch_bytes(&r);
const int64_t need = decode_scratch_bytes_of(&r, which);
if (need > worst) worst = need;
}
return worst;
@@ -285,8 +297,7 @@ static int64_t decode_scratch(const RadArgs* a, int head_dim, int which) {
int rc = fill_paged(a, &r, head_dim);
if (rc != RAD_OK) return rc;
r.splits = sp;
return which == 0 ? r4d_attn_decode_h256_gqa6_scratch_bytes(&r)
: r4d_attn_decode_h128_gqa4_scratch_bytes(&r);
return decode_scratch_bytes_of(&r, which);
}
extern "C" int64_t r4d_rad_attn_decode_h256_scratch(const RadArgs* a) {
@@ -303,8 +314,7 @@ static int decode_common(const RadArgs* a, R4DArgs* r, int head_dim, int which)
int rc = fill_paged(a, r, head_dim);
if (rc != RAD_OK) return rc;
r->splits = sp;
const int64_t need = (which == 0) ? r4d_attn_decode_h256_gqa6_scratch_bytes(r)
: r4d_attn_decode_h128_gqa4_scratch_bytes(r);
const int64_t need = decode_scratch_bytes_of(r, which);
if (!a->scratch || a->scratch_bytes < need) return R4D_NO(RAD_E_SCRATCH);
return RAD_OK;
}
@@ -315,7 +325,9 @@ extern "C" int r4d_rad_attn_decode_h256_fp8(const RadArgs* a, RadStream s) {
int rc = decode_common(a, &r, 256, 0);
if (rc != RAD_OK) return rc;
/* Absent descales mean 1.0; see the prefill twin above. */
return r4d_attn_decode_h256_gqa6_fp8kv(&r, (hipStream_t)s);
int rc2 = r4d_attn_decode_h256_gqa6_fp8kv(&r, (hipStream_t)s);
verify_attn_paged(&r, (long)s, 1, 1);
return rc2;
}
extern "C" int r4d_rad_attn_decode_h256_bf16(const RadArgs* a, RadStream s) {
@@ -323,7 +335,9 @@ extern "C" int r4d_rad_attn_decode_h256_bf16(const RadArgs* a, RadStream s) {
R4DArgs r;
int rc = decode_common(a, &r, 256, 0);
if (rc != RAD_OK) return rc;
return r4d_attn_decode_h256_gqa6_bf16kv(&r, (hipStream_t)s);
int rc2 = r4d_attn_decode_h256_gqa6_bf16kv(&r, (hipStream_t)s);
verify_attn_paged(&r, (long)s, 0, 1);
return rc2;
}
extern "C" int r4d_rad_attn_decode_h128_fp8(const RadArgs* a, RadStream s) {
@@ -340,3 +354,26 @@ extern "C" int r4d_rad_attn_decode_h128_bf16(const RadArgs* a, RadStream s) {
if (rc != RAD_OK) return rc;
return r4d_attn_decode_h128_gqa4_bf16kv(&r, (hipStream_t)s);
}
/* Gemma 4 dense sliding window -- head_dim 256, 2 queries per KV head, window 1024. Decode only;
* a prefill step does not match the row's q_len band and takes the caller's (windowed) fallback.
* Same shape as the drafter leg above (h128): one `which` routing to the gqa2 scratch law and the
* gqa2 decode launch, no check_dims_once because this is not the geometry that reports dims. */
extern "C" int64_t r4d_rad_attn_decode_h256_gqa2_scratch(const RadArgs* a) {
return decode_scratch(a, 256, 2);
}
extern "C" int r4d_rad_attn_decode_h256_gqa2_fp8(const RadArgs* a, RadStream s) {
R4DArgs r;
int rc = decode_common(a, &r, 256, 2);
if (rc != RAD_OK) return rc;
/* Absent descales mean 1.0; see the prefill twin above. */
return r4d_attn_decode_h256_gqa2_fp8kv(&r, (hipStream_t)s);
}
extern "C" int r4d_rad_attn_decode_h256_gqa2_bf16(const RadArgs* a, RadStream s) {
R4DArgs r;
int rc = decode_common(a, &r, 256, 2);
if (rc != RAD_OK) return rc;
return r4d_attn_decode_h256_gqa2_bf16kv(&r, (hipStream_t)s);
}