Archived
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:
1 parent
8b825a941c
commit
dd3310849a
6 files changed
+493
-14
No files matched your search
@@ -57,6 +57,13 @@ set(RAD_W4_GROUP 128 CACHE STRING "libr4d quantised-weight group size (must matc
|
||||
set(R4D_UNITS
|
||||
"r4d_attn_paged_h256_gqa6|"
|
||||
"r4d_attn_paged_h128_gqa4|"
|
||||
# Gemma 4 dense sliding window: head_dim 256, 2 queries per KV head, windowed decode only
|
||||
# (prefill falls to the caller's libref path). The windowed twin of the drafter leg above.
|
||||
"r4d_attn_paged_h256_gqa2|"
|
||||
# Gemma 4 dense global layers: head_dim 512, 8 queries per KV head, full attention (window 0).
|
||||
# The head_dim the library had not compiled before -- the prefill KV tile drops to 16 to fit 64
|
||||
# KiB of LDS, and the decode combine takes the head-generic merge (DPL 16 breaks the vector form).
|
||||
"r4d_attn_paged_h512_gqa8|"
|
||||
# Eight queries per KV head, at both head widths: the routed 35B-A3B is 16 over 2 at 256
|
||||
# and MiniCPM5-2B is 16 over 2 at 128. Same device code, different instantiations.
|
||||
"r4d_attn_paged_gqa8|"
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
// R4D attention dispatch for the Gemma 4 dense model's sliding-window geometry: head_dim 256,
|
||||
// 2 queries per KV head, paged block 16, and a SLIDING WINDOW (window 1024 in the model, 50 of its
|
||||
// 60 layers). It is the same device code as the 27B target's h256/gqa6 dispatch -- the kernels are
|
||||
// templates on head_dim and gqa, so this file is another set of instantiations, not a second
|
||||
// kernel -- with the window bit (template arg WIN) turned on, the way the DFlash2 drafter leg does
|
||||
// it at head_dim 128.
|
||||
//
|
||||
// Both bands, now. The decode band (q_len * gqa <= 64, i.e. q_len <= 32 at gqa 2) is the
|
||||
// windowed decode kernel above. The prefill band (a query long enough to want the query-tiled
|
||||
// prefill kernel) is the windowed prefill kernel below -- the same r4d_attn_prefill_kernel the
|
||||
// h256/gqa6 and h512/gqa8 dispatches instantiate, with the window bit (template arg WIN) turned on,
|
||||
// the way the decode leg does. (This leg used to be decode-only on the assumption that prefill
|
||||
// fell back to the portable libref paged attention; that was a misreading -- libref is an opt-in
|
||||
// fallback the engine refuses by default, spec §17, so a base-model prefill band with no device
|
||||
// kernel is fatal. The drafter leg it mirrored is decode-only because a drafter never prefills;
|
||||
// a base-model layer does.)
|
||||
#include "r4d.h"
|
||||
#include "r4d_attn_args.h"
|
||||
#include "r4d_common.h"
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
using std::max;
|
||||
// The decode and prefill kernels share their query conversion and fragment helpers, so the prefill
|
||||
// header comes first here for the same reason it does in the h256 dispatch. This translation unit
|
||||
// instantiates BOTH r4d_attn_prefill_kernel (below, WIN=1) and the windowed decode kernel.
|
||||
#include "r4d_attn_prefill_h256_gqa6.hip"
|
||||
#include "r4d_attn_decode_h256_gqa6.hip"
|
||||
|
||||
#define CHK4() do { hipError_t e_ = hipGetLastError(); if (e_ != hipSuccess) return -100 - (int)e_; } while (0)
|
||||
|
||||
#define B_HEAD_DIM 256
|
||||
#define B_GQA 2
|
||||
#define B_BLOCK_SIZE 16
|
||||
#define B_MAX_DECODE_ROWS 64 // q_len * gqa a single decode workgroup can hold
|
||||
#define B_TILE 16
|
||||
// The same option set the h256 decode kernel ships, K prefetch of 2 included. At head_dim 256 it
|
||||
// is the same register picture gqa6 has (256 VGPRs with a small spill), so this is a correctness
|
||||
// instantiation here -- the register count is a 3b tuning question, not a 3a one. The window bit
|
||||
// is already in the template; this file only turns it on.
|
||||
#define B_OPT 3429947
|
||||
#ifndef B_JPB
|
||||
#define B_JPB 2
|
||||
#endif
|
||||
#ifndef B_VECMIN
|
||||
#define B_VECMIN 256
|
||||
#endif
|
||||
|
||||
// Split law, the drafter leg's with the same window-aware term. It aims at 192 workgroups, snaps
|
||||
// DOWN to a power of two but never below 16, clamps to at least two tiles a segment, and then steps
|
||||
// UP off the exact grid size `splits * units == 256`, a pothole in this geometry's timing.
|
||||
//
|
||||
// THE WINDOW-AWARE TERM is the one thing it does that the h256 full-attention law does not: under a
|
||||
// sliding window the context the kernel actually walks is the window, not the sequence, so the tile
|
||||
// count that feeds the machine-fill target and the thin-segment clamp comes from the window.
|
||||
// Without that the law reads a 16K sequence and asks for segments two tiles deep off a 1024-token
|
||||
// walk.
|
||||
static int decode_splits_h256_gqa2(const R4DArgs* a) {
|
||||
if (a->splits > 0) return a->splits;
|
||||
const int span = (a->window > 0 && a->window + a->q_len < a->max_ctx)
|
||||
? a->window + a->q_len : a->max_ctx;
|
||||
const int tiles = (span + B_TILE - 1) / B_TILE;
|
||||
const int units = std::max(1, a->kv_heads * a->num_seqs);
|
||||
const int raw = 192 / units;
|
||||
int s = 128;
|
||||
while (s > 16 && s > raw) s >>= 1;
|
||||
const int cap = tiles / 2;
|
||||
while (s > 16 && s > cap) s >>= 1;
|
||||
if (s > tiles) s = tiles;
|
||||
if (s < 1) s = 1;
|
||||
if (s * units == 256 && s * 2 <= tiles) s <<= 1; // step off the pothole, see above
|
||||
return s;
|
||||
}
|
||||
|
||||
extern "C" long r4d_attn_decode_h256_gqa2_scratch_bytes(const R4DArgs* a) {
|
||||
const int splits = decode_splits_h256_gqa2(a);
|
||||
const long rows = (long)r4d_q_total(*a) * a->q_heads;
|
||||
return (B_OPT & 32) ? rows * splits * (long)(a->head_dim * 2 + 8)
|
||||
: rows * splits * (long)(a->head_dim + 2) * 4L;
|
||||
}
|
||||
|
||||
template <int KVP>
|
||||
static int decode_launch_h256_gqa2(const R4DArgs* a, hipStream_t s) {
|
||||
if (a->head_dim != B_HEAD_DIM || a->block_size != B_BLOCK_SIZE) return -1;
|
||||
if (a->q_heads / a->kv_heads != B_GQA) return -2;
|
||||
const int rows = a->q_len * B_GQA;
|
||||
if (rows > B_MAX_DECODE_ROWS) return -4;
|
||||
const int nw = (rows + 15) / 16;
|
||||
const int splits = decode_splits_h256_gqa2(a);
|
||||
|
||||
#define B_LAUNCH(NW) \
|
||||
hipLaunchKernelGGL((r4d_attn_decode_kernel<NW, B_TILE, B_HEAD_DIM, B_GQA, B_BLOCK_SIZE, \
|
||||
KVP, B_OPT, 1>), \
|
||||
dim3(splits, a->kv_heads, a->num_seqs), dim3(NW * 32), 0, s, *a, splits)
|
||||
if (nw == 1) B_LAUNCH(1);
|
||||
else if (nw == 2) B_LAUNCH(2);
|
||||
else if (nw == 3) B_LAUNCH(3);
|
||||
else B_LAUNCH(4);
|
||||
#undef B_LAUNCH
|
||||
CHK4();
|
||||
|
||||
// Same two merge forms, and the same rule for choosing between them, as the h256 dispatch:
|
||||
// a wave a row while there are enough rows to fill the part and the segment count fits a
|
||||
// wave, the general LDS-tree form otherwise. Both produce the same bytes, and both carry the
|
||||
// window bit here (the final template argument) for the same reason the decode kernel does.
|
||||
const int jobs3 = r4d_q_total(*a) * a->q_heads;
|
||||
if (splits <= 64 && jobs3 >= B_VECMIN) {
|
||||
const int blocks = (jobs3 + B_JPB - 1) / B_JPB;
|
||||
if (B_OPT & 32)
|
||||
hipLaunchKernelGGL((r4d_attn_splitkv_combine_vec_kernel<B_HEAD_DIM, 4, 1, B_JPB, 1>),
|
||||
dim3(blocks), dim3(32 * B_JPB), 0, s, *a, splits, B_TILE);
|
||||
else
|
||||
hipLaunchKernelGGL((r4d_attn_splitkv_combine_vec_kernel<B_HEAD_DIM, 4, 0, B_JPB, 1>),
|
||||
dim3(blocks), dim3(32 * B_JPB), 0, s, *a, splits, B_TILE);
|
||||
} else if (B_OPT & 32)
|
||||
hipLaunchKernelGGL((r4d_attn_splitkv_combine_kernel<B_HEAD_DIM, 4, 1, 1>), dim3(jobs3),
|
||||
dim3(B_HEAD_DIM), splits * sizeof(float), s, *a, splits, B_TILE);
|
||||
else
|
||||
hipLaunchKernelGGL((r4d_attn_splitkv_combine_kernel<B_HEAD_DIM, 4, 0, 1>), dim3(jobs3),
|
||||
dim3(B_HEAD_DIM), splits * sizeof(float), s, *a, splits, B_TILE);
|
||||
CHK4();
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern "C" int r4d_attn_decode_h256_gqa2_fp8kv(const R4DArgs* a, hipStream_t s) {
|
||||
return decode_launch_h256_gqa2<0>(a, s);
|
||||
}
|
||||
extern "C" int r4d_attn_decode_h256_gqa2_bf16kv(const R4DArgs* a, hipStream_t s) {
|
||||
return decode_launch_h256_gqa2<1>(a, s);
|
||||
}
|
||||
|
||||
// ---- prefill ---------------------------------------------------------------------------------
|
||||
// The h256/gqa6 prefill option set (P_OPT 3955211) is reused unchanged: at head_dim 256 the wide K
|
||||
// fetch (O_KWIDE) is valid -- it is written for 16 channels = head_dim 256 exactly -- so unlike the
|
||||
// h512 leg (which turns it off because 512 reads past the tile) this one keeps it. gqa 2 makes
|
||||
// BLOCK_Q (NWARPS*16/2 = 192) larger than gqa 6's 64, so a workgroup covers more query positions:
|
||||
// fewer grid.x blocks, the same KV tile. WIN is on (the last template arg); window <= 0 folds it
|
||||
// away to the full-attention form, so a windowless launch is safe.
|
||||
#define P_NWARPS 24
|
||||
#define P_TILE 48
|
||||
#define P_OPT 3955211
|
||||
|
||||
template <int KVP>
|
||||
static int prefill_launch_h256_gqa2(const R4DArgs* a, hipStream_t s) {
|
||||
if (a->head_dim != B_HEAD_DIM || a->block_size != B_BLOCK_SIZE) return -1;
|
||||
if (a->q_heads / a->kv_heads != B_GQA) return -2;
|
||||
constexpr int BQ = (P_NWARPS * 16) / B_GQA;
|
||||
dim3 grid((a->q_len + BQ - 1) / BQ, a->kv_heads, a->num_seqs);
|
||||
hipLaunchKernelGGL((r4d_attn_prefill_kernel<P_NWARPS, P_TILE, B_HEAD_DIM, B_GQA,
|
||||
B_BLOCK_SIZE, KVP, P_OPT, 1>),
|
||||
grid, dim3(P_NWARPS * 32), 0, s, *a);
|
||||
CHK4();
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern "C" int r4d_attn_prefill_h256_gqa2_fp8kv(const R4DArgs* a, hipStream_t s) {
|
||||
return prefill_launch_h256_gqa2<0>(a, s);
|
||||
}
|
||||
extern "C" int r4d_attn_prefill_h256_gqa2_bf16kv(const R4DArgs* a, hipStream_t s) {
|
||||
return prefill_launch_h256_gqa2<1>(a, s);
|
||||
}
|
||||
|
||||
/* The prefill RadArgs wrappers, defined here (not in the h256 dispatch) for the same reason the
|
||||
* h512 leg's live in theirs: an entry point appended to a textual inclusion compiles into nothing.
|
||||
* Absent descales mean 1.0 (fill_paged leaves them null and the kernel reads 1.0). */
|
||||
extern "C" int r4d_rad_attn_prefill_h256_gqa2_fp8(const RadArgs* a, RadStream s) {
|
||||
R4DArgs r;
|
||||
int rc = fill_paged(a, &r, B_HEAD_DIM);
|
||||
if (rc != RAD_OK) return rc;
|
||||
return r4d_attn_prefill_h256_gqa2_fp8kv(&r, (hipStream_t)s);
|
||||
}
|
||||
extern "C" int r4d_rad_attn_prefill_h256_gqa2_bf16(const RadArgs* a, RadStream s) {
|
||||
R4DArgs r;
|
||||
int rc = fill_paged(a, &r, B_HEAD_DIM);
|
||||
if (rc != RAD_OK) return rc;
|
||||
return r4d_attn_prefill_h256_gqa2_bf16kv(&r, (hipStream_t)s);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
// R4D attention dispatch for the Gemma 4 dense model's GLOBAL layers: head_dim 512, 8 queries per
|
||||
// KV head, full attention (window 0), paged block 16. This is the one head_dim the library has not
|
||||
// compiled before -- no h512 kernel existed -- so this file is a real new instantiation, not a
|
||||
// rename. It is the same device code as the h256/gqa6 and gqa8 dispatches (the kernels are
|
||||
// templates on head_dim and gqa), with the two changes head_dim 512 forces:
|
||||
//
|
||||
// PREFILL -- TILE 16, not 48. The 16-bit K+V tiles are sK[TILE*(512+8)] + sV16[512*(TILE+8)];
|
||||
// at TILE 48 that is 107264 B, past the 64 KiB of LDS the part has, and only TILE 16 clears it
|
||||
// (41216 B). The prefill warps tile the QUERY dimension (BLOCK_Q = NWARPS*16/gqa) and loop the
|
||||
// KV in TILE chunks, so shrinking the KV tile costs loop iterations, not a different tiling.
|
||||
// `O_KWIDE` is off -- its thread-to-(key,channel) map is written for 16 channels, head_dim 256
|
||||
// exactly, and at 512 it would read past the tile; the h128 prefill runs the same set minus it,
|
||||
// so the non-wide path is already a shipped, correct one.
|
||||
//
|
||||
// DECODE -- the split-KV combine is the HEAD-GENERIC plain form (one thread per channel, a block
|
||||
// of head_dim threads), not the vectorised form: that one pins a lane's width to eight or four
|
||||
// dwords, and head_dim 512 would give it sixteen and refuse to compile. The plain form is slower
|
||||
// but merges off the decode critical path; the trade is a 3b question.
|
||||
//
|
||||
// Register pressure at head_dim 512 is the one thing this file does not fix -- a thread carries
|
||||
// head_dim/16 query fragments plus head_dim/8 accumulator floats, ~384 VGPRs, which the part tops
|
||||
// out at, so both legs spill. That is correct and a tuning (3b) question, not a 3a one.
|
||||
#include "r4d.h"
|
||||
#include "r4d_attn_args.h"
|
||||
#include "r4d_common.h"
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
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 CHK5() do { hipError_t e_ = hipGetLastError(); if (e_ != hipSuccess) return -100 - (int)e_; } while (0)
|
||||
|
||||
#define H5_HEAD_DIM 512
|
||||
#define H5_GQA 8
|
||||
#define H5_BLOCK_SIZE 16
|
||||
#define H5_MAX_DECODE_ROWS 64 // q_len * gqa a single decode workgroup can hold: q_len <= 8
|
||||
// The h256 prefill option set (3955211) minus O_KWIDE -- the only head-512 option change; the h128
|
||||
// prefill ships this exact number.
|
||||
#define H5_P_NWARPS 24
|
||||
#define H5_P_TILE 16 // the KV tile that fits 64 KiB LDS at head_dim 512 (see above)
|
||||
#define H5_P_OPT 3430923
|
||||
#define H5_D_TILE 16
|
||||
#define H5_D_OPT 3429947 // the shared decode opt set; bit 32 (f16 partials) is set
|
||||
#define H5_D_JPB 2
|
||||
#define H5_D_VECMIN 256
|
||||
|
||||
// ---- prefill -----------------------------------------------------------------------------------
|
||||
template <int KVP>
|
||||
static int h5_prefill_launch(const R4DArgs* a, hipStream_t s) {
|
||||
if (a->head_dim != H5_HEAD_DIM || a->block_size != H5_BLOCK_SIZE) return -1;
|
||||
if (a->q_heads / a->kv_heads != H5_GQA) return -2;
|
||||
constexpr int BQ = (H5_P_NWARPS * 16) / H5_GQA;
|
||||
dim3 grid((a->q_len + BQ - 1) / BQ, a->kv_heads, a->num_seqs);
|
||||
hipLaunchKernelGGL((r4d_attn_prefill_kernel<H5_P_NWARPS, H5_P_TILE, H5_HEAD_DIM, H5_GQA,
|
||||
H5_BLOCK_SIZE, KVP, H5_P_OPT>),
|
||||
grid, dim3(H5_P_NWARPS * 32), 0, s, *a);
|
||||
CHK5();
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern "C" int r4d_attn_prefill_h512_gqa8_fp8kv(const R4DArgs* a, hipStream_t s) {
|
||||
return h5_prefill_launch<0>(a, s);
|
||||
}
|
||||
extern "C" int r4d_attn_prefill_h512_gqa8_bf16kv(const R4DArgs* a, hipStream_t s) {
|
||||
return h5_prefill_launch<1>(a, s);
|
||||
}
|
||||
|
||||
// ---- decode ------------------------------------------------------------------------------------
|
||||
// The h256 full-attention split law: as many workgroups as fit under 128, at least two tiles a
|
||||
// segment. It reads kv_heads, num_seqs and max_ctx and nothing about gqa or head_dim, so there is
|
||||
// nothing to re-derive for this geometry; a window does not exist at full attention.
|
||||
static int h5_decode_splits(const R4DArgs* a) {
|
||||
if (a->splits > 0) return a->splits;
|
||||
const int tiles = (a->max_ctx + H5_D_TILE - 1) / H5_D_TILE;
|
||||
const int units = max(1, a->kv_heads * a->num_seqs);
|
||||
int s = 128 / units;
|
||||
const int cap = tiles / 2;
|
||||
if (s > cap) s = cap;
|
||||
if (s > tiles) s = tiles;
|
||||
if (s < 1) s = 1;
|
||||
return s;
|
||||
}
|
||||
|
||||
extern "C" long r4d_attn_decode_h512_gqa8_scratch_bytes(const R4DArgs* a) {
|
||||
const int splits = h5_decode_splits(a);
|
||||
const long rows = (long)r4d_q_total(*a) * a->q_heads;
|
||||
return (H5_D_OPT & 32) ? rows * splits * (long)(a->head_dim * 2 + 8)
|
||||
: rows * splits * (long)(a->head_dim + 2) * 4L;
|
||||
}
|
||||
|
||||
template <int KVP>
|
||||
static int h5_decode_launch(const R4DArgs* a, hipStream_t s) {
|
||||
if (a->head_dim != H5_HEAD_DIM || a->block_size != H5_BLOCK_SIZE) return -1;
|
||||
if (a->q_heads / a->kv_heads != H5_GQA) return -2;
|
||||
const int rows = a->q_len * H5_GQA;
|
||||
if (rows > H5_MAX_DECODE_ROWS) return -4;
|
||||
const int nw = (rows + 15) / 16;
|
||||
const int splits = h5_decode_splits(a);
|
||||
|
||||
#define H5_LAUNCH(NW) \
|
||||
hipLaunchKernelGGL((r4d_attn_decode_kernel<NW, H5_D_TILE, H5_HEAD_DIM, H5_GQA, \
|
||||
H5_BLOCK_SIZE, KVP, H5_D_OPT>), \
|
||||
dim3(splits, a->kv_heads, a->num_seqs), dim3(NW * 32), 0, s, *a, splits)
|
||||
if (nw == 1) H5_LAUNCH(1);
|
||||
else if (nw == 2) H5_LAUNCH(2);
|
||||
else if (nw == 3) H5_LAUNCH(3);
|
||||
else H5_LAUNCH(4);
|
||||
#undef H5_LAUNCH
|
||||
CHK5();
|
||||
|
||||
// The head-generic plain combine, always: a single thread per channel, one block of head_dim
|
||||
// threads, no DPL to trip on at 512. H5_D_OPT carries bit 32 (f16 partials), so it reads them
|
||||
// as such; the f32 half is spelled for the same guard the other dispatches use, not expected.
|
||||
const int jobs3 = r4d_q_total(*a) * a->q_heads;
|
||||
if (H5_D_OPT & 32)
|
||||
hipLaunchKernelGGL((r4d_attn_splitkv_combine_kernel<H5_HEAD_DIM, 4, 1>), dim3(jobs3),
|
||||
dim3(H5_HEAD_DIM), splits * sizeof(float), s, *a, splits, H5_D_TILE);
|
||||
else
|
||||
hipLaunchKernelGGL((r4d_attn_splitkv_combine_kernel<H5_HEAD_DIM, 4, 0>), dim3(jobs3),
|
||||
dim3(H5_HEAD_DIM), splits * sizeof(float), s, *a, splits, H5_D_TILE);
|
||||
CHK5();
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern "C" int r4d_attn_decode_h512_gqa8_fp8kv(const R4DArgs* a, hipStream_t s) {
|
||||
return h5_decode_launch<0>(a, s);
|
||||
}
|
||||
extern "C" int r4d_attn_decode_h512_gqa8_bf16kv(const R4DArgs* a, hipStream_t s) {
|
||||
return h5_decode_launch<1>(a, s);
|
||||
}
|
||||
|
||||
// ============================================================== the plugin ABI entry points
|
||||
// Defined here, in this translation unit, for the same reason the h128 drafter's live in theirs:
|
||||
// the prefill and decode headers are textual inclusions, and an entry point appended to either
|
||||
// compiles into nothing, leaving the .so with an undefined symbol only r4d_selftest would catch.
|
||||
|
||||
extern "C" int r4d_rad_attn_prefill_h512_gqa8_fp8(const RadArgs* a, RadStream s) {
|
||||
R4DArgs r;
|
||||
int rc = fill_paged(a, &r, 512);
|
||||
if (rc != RAD_OK) return rc;
|
||||
/* Absent descales mean 1.0; see the prefill twins in the h256 dispatch. */
|
||||
return r4d_attn_prefill_h512_gqa8_fp8kv(&r, (hipStream_t)s);
|
||||
}
|
||||
extern "C" int r4d_rad_attn_prefill_h512_gqa8_bf16(const RadArgs* a, RadStream s) {
|
||||
R4DArgs r;
|
||||
int rc = fill_paged(a, &r, 512);
|
||||
if (rc != RAD_OK) return rc;
|
||||
return r4d_attn_prefill_h512_gqa8_bf16kv(&r, (hipStream_t)s);
|
||||
}
|
||||
|
||||
/* The split-KV partial buffer, sized at declare from the batch's worst step, the way the other
|
||||
* decode geometries do it. The h256 split law makes splits * num_seqs flat, but its clamp to
|
||||
* `>= 2 tiles a segment` breaks that at large batches, so the sweep over every count to the bound
|
||||
* is still the right size even at full attention. */
|
||||
extern "C" int64_t r4d_rad_attn_decode_h512_gqa8_scratch(const RadArgs* a) {
|
||||
const int sp = split_of(a);
|
||||
if (sp < 0) return R4D_NO(RAD_E_INVAL);
|
||||
|
||||
R4DArgs r;
|
||||
const long long n_head = r4d_geti_or(a, "n_head", 0);
|
||||
const long long max_seqs = r4d_geti_or(a, "max_seqs", 0);
|
||||
if (n_head > 0 && max_seqs > 0) {
|
||||
std::memset(&r, 0, sizeof(r));
|
||||
r.head_dim = H5_HEAD_DIM;
|
||||
r.q_heads = (int)n_head;
|
||||
r.kv_heads = (int)(n_head / H5_GQA);
|
||||
r.num_seqs = (int)max_seqs;
|
||||
r.q_len = (int)r4d_geti_or(a, "q_len", 1);
|
||||
r.max_ctx = (int)r4d_geti_or(a, "max_ctx", 0);
|
||||
r.block_size = (int)r4d_geti_or(a, "block_size", 16);
|
||||
r.splits = sp;
|
||||
if (r.max_ctx <= 0) return R4D_NO(RAD_E_INVAL);
|
||||
int64_t worst = 0;
|
||||
for (long long n = 1; n <= max_seqs; ++n) {
|
||||
r.num_seqs = (int)n;
|
||||
const int64_t need = r4d_attn_decode_h512_gqa8_scratch_bytes(&r);
|
||||
if (need > worst) worst = need;
|
||||
}
|
||||
return worst;
|
||||
}
|
||||
|
||||
int rc = fill_paged(a, &r, H5_HEAD_DIM);
|
||||
if (rc != RAD_OK) return rc;
|
||||
r.splits = sp;
|
||||
return r4d_attn_decode_h512_gqa8_scratch_bytes(&r);
|
||||
}
|
||||
|
||||
static int h5_decode_common(const RadArgs* a, R4DArgs* r, int which) {
|
||||
const int sp = split_of(a);
|
||||
if (sp < 0) return R4D_NO(RAD_E_INVAL);
|
||||
int rc = fill_paged(a, r, H5_HEAD_DIM);
|
||||
if (rc != RAD_OK) return rc;
|
||||
r->splits = sp;
|
||||
const int64_t need = r4d_attn_decode_h512_gqa8_scratch_bytes(r);
|
||||
if (!a->scratch || a->scratch_bytes < need) return R4D_NO(RAD_E_SCRATCH);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_attn_decode_h512_gqa8_fp8(const RadArgs* a, RadStream s) {
|
||||
R4DArgs r;
|
||||
int rc = h5_decode_common(a, &r, 0);
|
||||
if (rc != RAD_OK) return rc;
|
||||
/* Absent descales mean 1.0; see the prefill twin above. */
|
||||
int rc2 = r4d_attn_decode_h512_gqa8_fp8kv(&r, (hipStream_t)s);
|
||||
verify_attn_paged(&r, (long)s, 1, 1);
|
||||
return rc2;
|
||||
}
|
||||
extern "C" int r4d_rad_attn_decode_h512_gqa8_bf16(const RadArgs* a, RadStream s) {
|
||||
R4DArgs r;
|
||||
int rc = h5_decode_common(a, &r, 1);
|
||||
if (rc != RAD_OK) return rc;
|
||||
int rc2 = r4d_attn_decode_h512_gqa8_bf16kv(&r, (hipStream_t)s);
|
||||
verify_attn_paged(&r, (long)s, 0, 1);
|
||||
return rc2;
|
||||
}
|
||||
@@ -76,7 +76,12 @@ __device__ __forceinline__ uint32_t r4d_qcvt(uint32_t w, float s) {
|
||||
__builtin_bit_cast(float, w & 0xffff0000u) * s);
|
||||
}
|
||||
|
||||
template<int NWARPS, int TILE, int HEAD_DIM, int GQA, int BS, int KVP, int OPT>
|
||||
// WIN turns on a sliding window (a.window positions, ending at each row's causal limit), the way
|
||||
// the decode kernel's WIN does. The window's lower bound masks the keys before it, and the KV walk
|
||||
// starts at the tile holding the window's first token (taken at query row 0, whose window reaches
|
||||
// furthest back) so the tiles no row can reach are never walked. window <= 0 (or WIN off) is the
|
||||
// full-attention form, unchanged.
|
||||
template<int NWARPS, int TILE, int HEAD_DIM, int GQA, int BS, int KVP, int OPT, int WIN = 0>
|
||||
__global__ __launch_bounds__(NWARPS * 32)
|
||||
void r4d_attn_prefill_kernel(const R4DArgs a)
|
||||
{
|
||||
@@ -159,6 +164,11 @@ void r4d_attn_prefill_kernel(const R4DArgs a)
|
||||
}
|
||||
|
||||
const int klimit = ctx - qlen + qpos;
|
||||
// The window's first key this row may attend to: the causal limit back by the window, floored
|
||||
// at zero. The decode kernel's r4d_attn_wlo is the same formula, inlined here because the
|
||||
// prefill header is compiled before the decode one that defines it.
|
||||
int wlo = (WIN && a.window > 0) ? (ctx - qlen + qpos - a.window + 1) : 0;
|
||||
if (WIN && a.window > 0 && wlo < 0) wlo = 0;
|
||||
|
||||
float m_ref = -3.0e38f;
|
||||
float off = PSHIFT + 3.0e38f;
|
||||
@@ -170,6 +180,10 @@ void r4d_attn_prefill_kernel(const R4DArgs a)
|
||||
const int qmax_cta = min(qb * BLOCK_Q + BLOCK_Q - 1, qlen - 1);
|
||||
const int klimit_cta = ctx - qlen + qmax_cta;
|
||||
const int ntiles = (min(klimit_cta + 1, ctx) + TILE - 1) / TILE;
|
||||
// The window's first tile, taken at query row 0 (whose window reaches furthest back); the walk
|
||||
// starts there so no row's below-window tiles are fetched. WIN off (or window <= 0) starts at 0.
|
||||
const int wlo0 = (WIN && a.window > 0) ? (ctx - qlen - a.window + 1) : 0;
|
||||
const int tstart = (WIN && a.window > 0 && wlo0 > 0) ? (wlo0 / TILE) : 0;
|
||||
|
||||
// ---- staging, split into a global FETCH (registers) and an LDS STORE ------------------------
|
||||
// Splitting them is what lets GPREV move the fetch out from between the barriers; without
|
||||
@@ -302,10 +316,10 @@ void r4d_attn_prefill_kernel(const R4DArgs a)
|
||||
}
|
||||
};
|
||||
int blk[NB];
|
||||
if (BTS) load_bt(0, blk);
|
||||
if (GPREV) fetchV(0, blk);
|
||||
if (BTS) load_bt(tstart * NB, blk);
|
||||
if (GPREV) fetchV(tstart * TILE, blk);
|
||||
|
||||
for (int ti = 0; ti < ntiles; ++ti) {
|
||||
for (int ti = tstart; ti < ntiles; ++ti) {
|
||||
const int k0 = ti * TILE;
|
||||
int blkn[NB];
|
||||
if (BTS && ti + 1 < ntiles) {
|
||||
@@ -352,9 +366,12 @@ void r4d_attn_prefill_kernel(const R4DArgs a)
|
||||
for (int e = 0; e < 8; ++e) sc[e] = FOLDQ ? s[e] : s[e] * mul;
|
||||
|
||||
const int kbase = k0 + mt * 16 + 8 * h;
|
||||
if (!MSKIP || wave_any(kbase + 7 > klimit)) {
|
||||
// The causal bound masks the keys past klimit; the window bound (WIN) also masks the
|
||||
// keys before wlo. MSKIP skips both when an m-tile straddles neither limit.
|
||||
if (!MSKIP || wave_any(kbase + 7 > klimit) || (WIN && wave_any(kbase < wlo))) {
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) sc[e] = (kbase + e <= klimit) ? sc[e] : -INFINITY;
|
||||
for (int e = 0; e < 8; ++e) sc[e] = ((kbase + e <= klimit) && (!WIN || kbase + e >= wlo))
|
||||
? sc[e] : -INFINITY;
|
||||
}
|
||||
|
||||
float smax = sc[0];
|
||||
|
||||
@@ -89,6 +89,14 @@ int r4d_rad_attn_decode_h128_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h128_bf16 (const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_attn_decode_h256_scratch(const RadArgs*);
|
||||
int64_t r4d_rad_attn_decode_h128_scratch(const RadArgs*);
|
||||
/* Gemma 4 dense sliding window (r4d_attn_paged_h256_gqa2.hip): head_dim 256, 2 queries per KV head,
|
||||
* windowed. Both bands: the decode band (q_len*gqa <= 64, verify width 32 against the gqa6 leg's
|
||||
* 10) and the windowed prefill band (the same r4d_attn_prefill_kernel the gqa6 leg uses, WIN on). */
|
||||
int r4d_rad_attn_prefill_h256_gqa2_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_prefill_h256_gqa2_bf16 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h256_gqa2_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h256_gqa2_bf16 (const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_attn_decode_h256_gqa2_scratch(const RadArgs*);
|
||||
int r4d_rad_attn_vit (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_dense (const RadArgs*, RadStream);
|
||||
/* Eight queries per KV head (r4d_attn_paged_gqa8.hip): the routed 35B-A3B at head_dim 256
|
||||
@@ -111,6 +119,15 @@ int r4d_rad_attn_prefill_h256_gqa4_bf16(const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h256_gqa4_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h256_gqa4_bf16 (const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_attn_decode_h256_gqa4_scratch(const RadArgs*);
|
||||
/* Eight queries per KV head at head_dim 512, full attention -- the Gemma 4 dense GLOBAL layers.
|
||||
* Its own instantiation, not the gqa8 body above: that one's prefill tile is 48, which 512-channel
|
||||
* tiles do not fit in LDS, so head_dim 512 is a stand-alone dispatch
|
||||
* (r4d_attn_paged_h512_gqa8.hip). The verify width is the gqa bound, 64/8 = 8. */
|
||||
int r4d_rad_attn_prefill_h512_gqa8_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_prefill_h512_gqa8_bf16(const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h512_gqa8_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h512_gqa8_bf16 (const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_attn_decode_h512_gqa8_scratch(const RadArgs*);
|
||||
/* Twelve queries a KV head, head_dim 256 only -- Qwen3.8-Flash-Next's QSA layers. Same body,
|
||||
* different instantiation; r4d_attn_paged_h256_gqa12.hip is four lines. */
|
||||
int r4d_rad_attn_prefill_h256_gqa12_fp8 (const RadArgs*, RadStream);
|
||||
@@ -417,6 +434,7 @@ int r4d_rad_scatter_rows_bf16(const RadArgs*, RadStream);
|
||||
int r4d_rad_scatter_rows_bf16_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
|
||||
int r4d_rad_gather_rows_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
|
||||
int r4d_rad_rmsnorm (const RadArgs*, RadStream);
|
||||
int r4d_rad_rmsnorm_p (const RadArgs*, RadStream);
|
||||
int r4d_rad_add (const RadArgs*, RadStream);
|
||||
int r4d_rad_mul (const RadArgs*, RadStream);
|
||||
int r4d_rad_mul_weight (const RadArgs*, RadStream);
|
||||
@@ -434,7 +452,10 @@ int r4d_rad_rel_sinusoid (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_local_rel (const RadArgs*, RadStream);
|
||||
int r4d_rad_sigmoid (const RadArgs*, RadStream);
|
||||
int r4d_rad_fill (const RadArgs*, RadStream);
|
||||
int r4d_rad_logit_softcap (const RadArgs*, RadStream);
|
||||
int r4d_rad_scale (const RadArgs*, RadStream);
|
||||
int r4d_rad_silu_mul (const RadArgs*, RadStream);
|
||||
int r4d_rad_gelu_mul (const RadArgs*, RadStream);
|
||||
int r4d_rad_rope (const RadArgs*, RadStream);
|
||||
/* -- the vision tower's ops (r4d_vit_bf16.hip) -- */
|
||||
int r4d_rad_layernorm (const RadArgs*, RadStream);
|
||||
@@ -540,6 +561,7 @@ int r4d_shape_moe_gemm_q (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_moe_gemm_q_mod4(const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_moe_gather (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_scale_rows (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_scale (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_row_topk_merge (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
|
||||
int r4d_shape_quant_act_i8 (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
|
||||
Reference in new issue
Block a user