libr4d: a non-causal window that bounds the keys behind a row only

attn_paged_ahead and attn_paged_qrow_ahead are attn_paged at causal 0
where `window` limits the keys behind a row and every later key of the
sequence is visible. The windowed kernels take it as a launch flag (h256,
1 to 8 queries per kv head). Gemma 4's sliding layers read a picture's
rows with transformers' mask, the window AND (causal OR the same
picture), which this is for a picture longer than the window. Emulator
cases for the scalar, split and WMMA kernels.
This commit is contained in:
keptn42 committed 2026-11-04 07:21:07 +01:00
1 parent eb3aa7eae7
commit a0ea6ad1ba
5 files changed
+110 -9

No files matched your search

+25 -5
View File
@@ -24,6 +24,7 @@
* pos = seqused[s] - q_len(s) + i (seqused INCLUDES this step's queries)
* causal key j visible when j <= pos; non-causal when j < seqused[s]
* window causal: pos - j < window; non-causal: |pos - j| < window; 0 = none
* ahead non-causal, window > 0: only pos - j < window, the keys after pos unbounded (FWD, below)
* a block-table entry < 0 is a missing block: its keys are not visible
* no visible key at all -> a zero row
* k_descale / v_descale per (sequence, kv head), absent = 1
@@ -197,6 +198,8 @@ struct AttnArgs {
float qscale; /* softmax scale * log2(e) */
int max_blocks, n_seq, q_len, tps, kv_heads, causal, window, n_split;
int shared_window; /* all query rows use sequence anchor */
/* 1: a non-causal window bounds the keys behind only (launch_attn's FWD) */
int ahead;
/* A tree verify (attn_paged_qrow_tree): RadBatch::spec_parent, one a query row; read only by
* the TREE instances. */
const int* tpar;
@@ -310,7 +313,7 @@ __device__ __forceinline__ void attn_body(AttnArgs A) {
/* ---- the tile's key range, and this split's share of it */
int hi = causal ? (A.shared_window ? tbase + 1 : plast + 1) : ctx;
if (!causal && window > 0 && !A.shared_window) hi = imin(hi, plast + window);
if (!causal && window > 0 && !A.shared_window && !A.ahead) hi = imin(hi, plast + window);
hi = imin(imin(hi, ctx), A.max_blocks * 16);
/* A tree row's window is measured from the block's first position plus its depth, which can
* be below its own position; an anchor-window launch also uses the first row for every query. */
@@ -445,7 +448,7 @@ __device__ __forceinline__ void attn_body(AttnArgs A) {
if (A.shared_window)
vis = vis && (wpq - kk < window);
else if (window > 0)
vis = vis && (wpq - kk < window) && (causal || kk - wpq < window);
vis = vis && (wpq - kk < window) && (causal || A.ahead || kk - wpq < window);
const float sv = vis ? sc[0] : -__builtin_inff();
const float mn = __builtin_fmaxf(m[i], wave_max(sv));
if (mn == -__builtin_inff()) return; /* nothing visible yet: uniform */
@@ -639,7 +642,7 @@ __global__ __launch_bounds__(kWThreads) void attn_wmma_kernel(AttnArgs A) {
const int plast = qbase + npos - 1;
const int causal = A.causal, window = A.window;
int hi = causal ? plast + 1 : ctx;
if (!causal && window > 0) hi = imin(hi, plast + window);
if (!causal && window > 0 && !A.ahead) hi = imin(hi, plast + window);
hi = imin(imin(hi, ctx), A.max_blocks * 16);
const int lo = window > 0 ? imax(0, qbase - window + 1) : 0;
const int lo_a = lo & ~15;
@@ -830,7 +833,7 @@ __global__ __launch_bounds__(kWThreads) void attn_wmma_kernel(AttnArgs A) {
const int kk = k0 + 8 * h + e;
bool vis = rval && tok && kk >= lo && kk < hi;
if (causal) vis = vis && kk <= pos;
if (window > 0) vis = vis && (pos - kk < window) && (causal || kk - pos < window);
if (window > 0) vis = vis && (pos - kk < window) && (causal || A.ahead || kk - pos < window);
sv[e] = vis ? tot[e] * sscale : -__builtin_inff();
mx = __builtin_fmaxf(mx, sv[e]);
}
@@ -1047,7 +1050,7 @@ __global__ __launch_bounds__(kThreads) void r4dchain_merge_window(AttnArgs A) {
r4d_chain_done();
}
template <int HD, int GQA, bool KV8, bool TREE = false, bool SHARED = false>
template <int HD, int GQA, bool KV8, bool TREE = false, bool SHARED = false, bool FWD = false>
static int launch_attn(const RadArgs* a, RadStream stream) {
constexpr int R = Geo<HD>::R, BK = Geo<HD>::BK, BQ = R / GQA, ESZ = KV8 ? 1 : 2;
if (!a || a->n_t < (TREE ? 9 : 8)) return RAD_E_INVAL;
@@ -1072,6 +1075,9 @@ static int launch_attn(const RadArgs* a, RadStream stream) {
const long long causal = rad_args_geti_or(a, "causal", 1);
const long long window = rad_args_geti_or(a, "window", 0);
if ((causal != 0 && causal != 1) || window < 0 || window > (1ll << 30)) return RAD_E_SHAPE;
/* The open-ended window is a non-causal one: a causal row has nothing ahead, and without a
* window there is nothing to bound. */
if (FWD && (causal != 0 || window == 0)) return RAD_E_SHAPE;
if (q->dtype != RAD_BF16 || o->dtype != RAD_BF16) return RAD_E_DTYPE;
if (kv->dtype != (uint32_t)(KV8 ? RAD_F8E4M3 : RAD_BF16)) return RAD_E_DTYPE;
@@ -1180,6 +1186,7 @@ static int launch_attn(const RadArgs* a, RadStream stream) {
W.causal = (int)causal;
W.window = (int)window;
W.shared_window = SHARED;
W.ahead = FWD ? 1 : 0;
W.n_split = 1;
R4D_KLAUNCH(R4D_KNAME(attn_wmma_kernel<HD, GQA, KV8>), dim3((unsigned)ntw, (unsigned)kvh, 1u),
dim3(kWThreads), (hipStream_t)stream, W);
@@ -1224,6 +1231,7 @@ static int launch_attn(const RadArgs* a, RadStream stream) {
A.causal = (int)causal;
A.window = (int)window;
A.shared_window = SHARED;
A.ahead = FWD ? 1 : 0;
A.n_split = (int)ns;
A.tpar = TREE ? (const int*)tp->data : nullptr;
@@ -1265,6 +1273,18 @@ R4D_WIN_DEF(256, 1) R4D_WIN_DEF(256, 2) R4D_WIN_DEF(256, 4) R4D_WIN_DEF(256, 8)
R4D_WIN_DEF(512, 1) R4D_WIN_DEF(512, 2) R4D_WIN_DEF(512, 4) R4D_WIN_DEF(512, 8) R4D_WIN_DEF(512, 16)
#undef R4D_WIN_DEF
/* attn_paged_ahead and attn_paged_qrow_ahead: a non-causal window that bounds the keys behind only
* (FWD), the sliding layers' mask over a picture's rows (docs/OPS.md). */
#define R4D_WIN_AHEAD_DEF(HD, G) \
int r4d_rad_attn_window_ahead_h##HD##_gqa##G##_bf16(const RadArgs* a, RadStream s) { \
return r4d_win::launch_attn<HD, G, false, false, false, true>(a, s); \
} \
int r4d_rad_attn_window_ahead_h##HD##_gqa##G##_fp8(const RadArgs* a, RadStream s) { \
return r4d_win::launch_attn<HD, G, true, false, false, true>(a, s); \
}
R4D_WIN_AHEAD_DEF(256, 1) R4D_WIN_AHEAD_DEF(256, 2) R4D_WIN_AHEAD_DEF(256, 4) R4D_WIN_AHEAD_DEF(256, 8)
#undef R4D_WIN_AHEAD_DEF
/* attn_paged_qrow_tree: the scalar kernel with a tree verify's mask (TREE). */
#define R4D_WIN_TREE_DEF(HD, G) \
int r4d_rad_attn_window_tree_h##HD##_gqa##G##_bf16(const RadArgs* a, RadStream s) { \
+7
View File
@@ -211,6 +211,13 @@ int r4d_rad_attn_qrow_tree_decode_h256_gqa2_bf16(const RadArgs*, RadStream);
R4D_WIN_TREE_DECL(256, 1) R4D_WIN_TREE_DECL(256, 2) R4D_WIN_TREE_DECL(256, 4) R4D_WIN_TREE_DECL(256, 8)
R4D_WIN_TREE_DECL(512, 1) R4D_WIN_TREE_DECL(512, 2) R4D_WIN_TREE_DECL(512, 4) R4D_WIN_TREE_DECL(512, 8)
#undef R4D_WIN_TREE_DECL
/* attn_paged_ahead and attn_paged_qrow_ahead (docs/OPS.md): the windowed kernels with a non-causal
* window that bounds the keys behind only. */
#define R4D_WIN_AHEAD_DECL(HD, G) \
int r4d_rad_attn_window_ahead_h##HD##_gqa##G##_bf16(const RadArgs*, RadStream); \
int r4d_rad_attn_window_ahead_h##HD##_gqa##G##_fp8 (const RadArgs*, RadStream);
R4D_WIN_AHEAD_DECL(256, 1) R4D_WIN_AHEAD_DECL(256, 2) R4D_WIN_AHEAD_DECL(256, 4) R4D_WIN_AHEAD_DECL(256, 8)
#undef R4D_WIN_AHEAD_DECL
/* dflash_tree (r4d_dflash_tree_bf16.hip): dflash_select's lattice grown best-first into a tree. */
int r4d_rad_dflash_tree(const RadArgs*, RadStream);
/* The top-1 of a row of all-gathered (id, value) pairs, written one id a row: the vocab-sharded
+45
View File
@@ -3284,6 +3284,12 @@ static const RadOpSchema kSchemas[] = {
"attn_paged, parameter for parameter and value for value, with q read at its own row "
"pitch: a column slice of a wider row -- the q part of a fused [q|k|v] projection -- read "
"where it lies rather than lifted into a dense block first."),
SCHEMA("attn_paged_ahead", pAttnPaged, oAttnPaged,
"attn_paged, non-causal, with `window` (> 0) bounding the keys behind a row only: key j is "
"visible when pos - j < window, every later key of the sequence is. A sliding layer's mask "
"over a picture's rows read both ways."),
SCHEMA("attn_paged_qrow_ahead", pAttnPaged, oAttnPaged,
"attn_paged_ahead with q read at its own row pitch, as attn_paged_qrow."),
SCHEMA("attn_paged_qrow_tree", pAttnPaged, oAttnPagedTree,
"attn_paged_qrow over a tree verify (rad_drafter_tree): spec_parent is -2 for an "
"ordinary causal row, else the row's parent's index within its sequence's rows (-1 for "
@@ -4243,6 +4249,17 @@ R4D_WIN_C(cAnchorH512G4Bf16, 512, 4, "bf16", 1) R4D_WIN_C(cAnchorH512G4Fp8, 512,
R4D_WIN_C(cAnchorH512G8Bf16, 512, 8, "bf16", 1) R4D_WIN_C(cAnchorH512G8Fp8, 512, 8, "fp8_e4m3", 1)
R4D_WIN_C(cWinH512G16Bf16, 512, 16, "bf16", 0) R4D_WIN_C(cWinH512G16Fp8, 512, 16, "fp8_e4m3", 0)
#undef R4D_WIN_C
/* The open-ended window (attn_paged_ahead): non-causal, a window of at least one. */
#define R4D_WIN_AHEAD_C(NAME, HD, G, KV) \
static const RadConstraint NAME[] = { \
C_EQ("head_dim", HD), C_EQ("gqa", G), C_EQ("block_size", 16), C_EQ("causal", 0), \
C_GE("window", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", KV), \
};
R4D_WIN_AHEAD_C(cWinAheadH256G1Bf16, 256, 1, "bf16") R4D_WIN_AHEAD_C(cWinAheadH256G1Fp8, 256, 1, "fp8_e4m3")
R4D_WIN_AHEAD_C(cWinAheadH256G2Bf16, 256, 2, "bf16") R4D_WIN_AHEAD_C(cWinAheadH256G2Fp8, 256, 2, "fp8_e4m3")
R4D_WIN_AHEAD_C(cWinAheadH256G4Bf16, 256, 4, "bf16") R4D_WIN_AHEAD_C(cWinAheadH256G4Fp8, 256, 4, "fp8_e4m3")
R4D_WIN_AHEAD_C(cWinAheadH256G8Bf16, 256, 8, "bf16") R4D_WIN_AHEAD_C(cWinAheadH256G8Fp8, 256, 8, "fp8_e4m3")
#undef R4D_WIN_AHEAD_C
/* Twelve queries per KV head, head_dim 256 (r4d_attn_paged_h256_gqa12.hip): Qwen3.8-Flash-Next's
* QSA layers, 24 query heads over 2. `q_len <= 5` is the same `q_len * gqa <= 64` bound, tighter
* than the legal speculation depths: a depth-7 verify (q_len 8) takes the prefill kernel, which
@@ -7257,6 +7274,34 @@ R4D_ANCHOR_PAIR(256, 1) R4D_ANCHOR_PAIR(256, 2) R4D_ANCHOR_PAIR(256, 4) R4D_ANCH
R4D_ANCHOR_PAIR(512, 1) R4D_ANCHOR_PAIR(512, 2) R4D_ANCHOR_PAIR(512, 4) R4D_ANCHOR_PAIR(512, 8)
#undef R4D_ANCHOR_PAIR
#undef R4D_ANCHOR_ROW
/* ...and the same kernels under attn_paged_ahead and attn_paged_qrow_ahead, where the window
* bounds the keys behind only. */
#define R4D_WIN_AHEAD_ROW(HD, G, KVN, KVS, C, KVDOC) \
{ "attn_ahead_h" #HD "_gqa" #G "_" #KVN, "attn_paged_ahead", "attn", \
"paged attention, non-causal, a window over the keys behind only", \
"head_dim " #HD ", " #G " queries per kv head, paged block 16, any q_len, varlen", \
"bf16 query, " KVDOC " kv cache, bf16 out, f32 partials", \
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, \
ROW(C), NOTUNE, \
nullptr, nullptr, r4d_rad_attn_window_ahead_h##HD##_gqa##G##_##KVS, \
r4d_rad_attn_window_h##HD##_scratch, \
nullptr, nullptr, nullptr }, \
{ "attn_qrow_ahead_h" #HD "_gqa" #G "_" #KVN, "attn_paged_qrow_ahead", "attn", \
"paged attention reading q at its own row pitch, non-causal, a window over the keys behind only", \
"head_dim " #HD ", " #G " queries per kv head, paged block 16, any q_len, varlen", \
"bf16 query (any row pitch), " KVDOC " kv cache, bf16 out, f32 partials", \
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, \
ROW(C), NOTUNE, \
nullptr, nullptr, r4d_rad_attn_window_ahead_h##HD##_gqa##G##_##KVS, \
r4d_rad_attn_window_h##HD##_scratch, \
nullptr, nullptr, nullptr },
#define R4D_WIN_AHEAD_PAIR(HD, GQ) \
R4D_WIN_AHEAD_ROW(HD, GQ, bf16kv, bf16, cWinAheadH##HD##G##GQ##Bf16, "bf16") \
R4D_WIN_AHEAD_ROW(HD, GQ, fp8kv, fp8, cWinAheadH##HD##G##GQ##Fp8, "fp8-e4m3 (per-(seq,head) descales)")
R4D_WIN_AHEAD_PAIR(256, 1) R4D_WIN_AHEAD_PAIR(256, 2) R4D_WIN_AHEAD_PAIR(256, 4)
R4D_WIN_AHEAD_PAIR(256, 8)
#undef R4D_WIN_AHEAD_PAIR
#undef R4D_WIN_AHEAD_ROW
/* ...and under attn_paged_qrow_tree, the scalar kernel with a tree verify's mask (the launch
* refuses a non-causal one and never takes the WMMA kernel). */