diff --git a/docs/OPS.md b/docs/OPS.md index 2084d90c..dad4df74 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -507,6 +507,20 @@ depth, the position its token takes once its path is committed. Causal layers on `attn_paged_tree`; libr4d `attn_paged_qrow_tree` at h256/gqa2 on the split-KV decode kernel and at every geometry of the windowed kernels, to 64 rows a block. +| op | params | operands | +|---|---|---| +| `attn_paged_ahead` | `q_len`(range) `head_dim` `gqa` `block_size`/der `causal` `window` `q_dtype` `kv_dtype` `scale`:f64/opt `max_ctx`/opt `n_head`/opt `max_seqs`/opt `prefill_fp8`/opt | `q`, `kv_cache`, `block_table`, `seqused`, `k_descale`?, `v_descale`?, `cu_seqlens`? → `out` | +| `attn_paged_qrow_ahead` | `q_len`(range) `head_dim` `gqa` `block_size`/der `causal` `window` `q_dtype` `kv_dtype` `scale`:f64/opt `max_ctx`/opt `n_head`/opt `max_seqs`/opt `prefill_fp8`/opt | `q`, `kv_cache`, `block_table`, `seqused`, `k_descale`?, `v_descale`?, `cu_seqlens`? → `out` | + +`attn_paged_ahead` is `attn_paged` at `causal` 0 with a window that bounds the keys behind a row +only: key `j` is visible to row `i` when `i - j < window` and `j < seqused`, so every key after the +row is. `attn_paged` reads a non-causal window two-sided (`|i - j| < window`). Gemma 4's sliding +layers read a picture's rows this way (transformers' mask is the window AND (causal OR the same +picture)), and the two differ only for a picture longer than the window. `window` is at least 1. +`attn_paged_qrow_ahead` is the same with `q` at its own row pitch, as `attn_paged_qrow`. libr4d +serves both at h256, 1 to 8 queries per kv head, on the windowed kernels; no other library +declares them, so a plugin asking for them learns whether a card serves the geometry. + Of the ops above, libref serves only `attn_paged_tree`, which is not a fusion but `attn_paged`'s tree form. It has no row for any of the fusions, deliberately. An architecture asks for a fusion and declares the chain where it does not resolve (`rad_op_resolved`); a reference row would make it resolve diff --git a/libr4d/r4d_attn_paged_window.hip b/libr4d/r4d_attn_paged_window.hip index 55bbf68b..3e97e629 100644 --- a/libr4d/r4d_attn_paged_window.hip +++ b/libr4d/r4d_attn_paged_window.hip @@ -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 +template static int launch_attn(const RadArgs* a, RadStream stream) { constexpr int R = Geo::R, BK = Geo::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), 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(a, s); \ + } \ + int r4d_rad_attn_window_ahead_h##HD##_gqa##G##_fp8(const RadArgs* a, RadStream s) { \ + return r4d_win::launch_attn(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) { \ diff --git a/libr4d/r4d_plugin.h b/libr4d/r4d_plugin.h index 13fe628a..1639bb95 100644 --- a/libr4d/r4d_plugin.h +++ b/libr4d/r4d_plugin.h @@ -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 diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 4cf1b5b4..54342257 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -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). */ diff --git a/tests/r4d_emu/test_attn.cpp b/tests/r4d_emu/test_attn.cpp index 91a56590..756b5a6f 100644 --- a/tests/r4d_emu/test_attn.cpp +++ b/tests/r4d_emu/test_attn.cpp @@ -37,10 +37,17 @@ struct Case { int64_t scratch = 0; /* 0: no scratch */ int qpitch_extra = 0; /* q as a column slice of a wider buffer */ int expect_wmma = -1; /* 1: must take the WMMA kernel, 0: must not, -1: either */ + bool ahead = false; /* non-causal, the window bounds the keys behind only */ }; using Fn = int (*)(const RadArgs*, RadStream); -static Fn pick(int hd, int gqa, bool fp8) { +static Fn pick(int hd, int gqa, bool fp8, bool ahead) { + if (ahead) { +#define A(G) if (hd == 256 && gqa == G) return fp8 ? r4d_rad_attn_window_ahead_h256_gqa##G##_fp8 : r4d_rad_attn_window_ahead_h256_gqa##G##_bf16; + A(1) A(2) A(4) A(8) +#undef A + return nullptr; + } #define P(H, G) if (hd == H && gqa == G) return fp8 ? r4d_rad_attn_window_h##H##_gqa##G##_fp8 : r4d_rad_attn_window_h##H##_gqa##G##_bf16; P(128, 1) P(256, 1) P(256, 2) P(256, 4) P(256, 8) P(512, 1) P(512, 2) P(512, 4) P(512, 8) P(512, 16) @@ -109,14 +116,14 @@ static int run(const Case& c) { auto ps = [&](const char* k, const char* v) { RadParam x{}; x.key = k; x.kind = RAD_P_STR; x.sval = v; p.push_back(x); }; auto pf = [&](const char* k, double v) { RadParam x{}; x.key = k; x.kind = RAD_P_F64; x.dval = v; p.push_back(x); }; pi("q_len", qmax + 3); /* the band bound, above the step's max */ - pi("head_dim", HD); pi("gqa", c.gqa); pi("block_size", 16); pi("causal", c.causal); pi("window", c.window); + pi("head_dim", HD); pi("gqa", c.gqa); pi("block_size", 16); pi("causal", c.causal && !c.ahead); pi("window", c.window); ps("q_dtype", "bf16"); ps("kv_dtype", c.fp8 ? "fp8_e4m3" : "bf16"); const double scale = 1.0 / std::sqrt((double)HD) * 1.3; /* not the default: the param must be read */ pf("scale", scale); pi("n_head", qh); pi("max_seqs", n_seq); if (c.max_ctx) pi("max_ctx", c.max_ctx); RadArgs a{}; a.t = t; a.n_t = 8; a.p = p.data(); a.n_p = (int)p.size(); a.scratch = c.scratch ? sp : nullptr; a.scratch_bytes = c.scratch; - Fn fn = pick(HD, c.gqa, c.fp8); + Fn fn = pick(HD, c.gqa, c.fp8, c.ahead); const int g0 = emu_grids; const int rc = fn(&a, nullptr); const int launches = emu_grids - g0; @@ -128,7 +135,7 @@ static int run(const Case& c) { const int tok = cu[s] + i, kh = h / c.gqa, pos = c.ctx[s] - c.qlen[s] + i; auto visible = [&](int j) { if (c.causal ? j > pos : j >= c.ctx[s]) return false; - if (c.window > 0) { const int d = pos - j; if (c.causal ? d >= c.window : (d >= c.window || d <= -c.window)) return false; } + if (c.window > 0) { const int d = pos - j; if (c.causal ? d >= c.window : (d >= c.window || (!c.ahead && d <= -c.window))) return false; } return bt[s * maxb + j / 16] >= 0; }; std::vector sc(c.ctx[s], -INFINITY); double mx = -INFINITY; @@ -229,6 +236,14 @@ int main(int argc, char** argv) { {"wmma h256 gqa2 picture 280 of 1500 fp8", 256, 2, 2, true, false, false, true, 1024, {280}, {1500}, -1, 0, 0, 0, 1}, {"wmma h256 gqa2 picture q-pitch slice", 256, 2, 2, false, false, false, false, 1024, {96}, {120}, -1, 0, 0, 512, 1}, {"h256 gqa2 picture tail split", 256, 2, 2, false, false, false, false, 1024, {10}, {1300}, -1, 0, s256, 0, 0}, + /* a picture longer than the window, whose rows read every later key (attn_paged_ahead): the + * window is 20 here, the rows 70 to 100, and the scalar, split and WMMA kernels all take it */ + {"h256 gqa2 ahead w20 verify-like", 256, 2, 2, false, false, false, false, 20, {6}, {70}, -1, 0, 0, 0, 0, true}, + {"h256 gqa2 ahead w20 ragged fp8", 256, 2, 2, true, false, true, true, 20, {3, 1, 9}, {50, 64, 40}, -1, 0, 0, 0, 0, true}, + {"h256 gqa1 ahead w5 missing-block", 256, 1, 1, false, false, true, false, 5, {4, 2}, {37, 20}, 1, 0, 0, 0, 0, true}, + {"h256 gqa4 ahead w20 split", 256, 4, 1, false, false, false, false, 20, {5}, {300}, -1, 4096, s256, 0, 0, true}, + {"wmma h256 gqa2 ahead w20 picture 100 of 100", 256, 2, 2, false, false, false, false, 20, {100}, {100}, -1, 0, 0, 0, 1, true}, + {"wmma h256 gqa2 ahead w20 picture 90 of 300 fp8", 256, 2, 2, true, false, false, true, 20, {90}, {300}, -1, 0, 0, 0, 1, true}, /* and what must stay scalar: a verify batch, many sequences, a few rows each */ {"scalar: 8 x verify16, ragged", 512, 4, 1, false, true, true, false, 0, {16, 16, 16, 16, 16, 16, 16, 16}, {40, 50, 60, 70, 80, 90, 100, 110}, -1, 262144, s512, 0, 0},