attn prefill h256: the small-launch KV split for every GQA geometry

The KV split of the head-256 / GQA-6 prefill kernel (few workgroups -> split the KV walk into segments,
f32 partials, the split-KV merge with a cu span) now serves every head-256 prefill row: GQA 12
(4-token pages: fp8, bf16, fp8_v4, k6_v4), GQA 6 with 4-token pages (the same four), GQA 8, GQA 4 and
GQA 2 (fp8, bf16). The fewer KV heads a rank holds, the more it matters: at TP 2 Qwen4-Exp (GQA 12) and
Qwen3.5-35B-A3B (GQA 8) keep one KV head per rank, so a 53-row tail runs 2 workgroups on 32 WGPs.

The host side (the law, the scratch sweep, the scratch-capped count, R4D_PREFILL_SPLITS, the merge
launch) moves into r4d_attn_prefill_split.h; each unit passes its own geometry (query rows per workgroup
= 24 * 16 / gqa, tile 48). The GQA-6 unit is refactored onto it with the same law (Qwen3.8-27B in the
engine: greedy top-5 logprobs bit-identical before and after at 300 positions after 25k and 41k prompts).

Left unsplit: GQA-2 launches with a sliding window (the partition has no window floor; a 1024 window
walks at most ~26 tiles), the lossy e4m3 legs, and the head-128 / head-64 legs sharing the bodies.

Split count, one sequence, N query rows -> S: GQA 12 with 1 KV head: 1-32 -> 32, 53 -> 16, 160 -> 6,
1024 -> 1; GQA 8 with 1 KV head: 53 -> 16, 345 -> 4; GQA 2 with 4 KV heads: up to 160 -> 8. Scratch at
declare at most 25.4 MB per row. Split vs unsplit: at most 0.000488 (fp8 KV) and 0.0039 (bf16, fp8_v4,
k6_v4; about 1 bf16 ulp).

Tests: the split tests generalised over geometry for all 18 split rows (query lengths 1-640 over 8k-20k
keys, empty segments, ragged batches, pipelined pieces, poisoned scratch, oracle rows at each tile edge,
declare-time scratch): 1782 ok; the mixed-step case over 9 geometry x cache pairs: 54 ok. Every kernel
that existed before is byte-identical (311 of 311).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 committed 2026-10-26 09:25:35 +01:00
1 parent 22df1c7264
commit 96bc0465b3
8 files changed
+433 -226

No files matched your search

+52 -2
View File
@@ -28,6 +28,7 @@ using std::max;
// The decode kernel uses helpers defined in the prefill unit, so prefill is included first.
#include "r4d_attn_prefill_h256_gqa6.hip"
#include "r4d_attn_decode_h256_gqa6.hip"
#include "r4d_attn_prefill_split.h"
#define CHK8() do { hipError_t e_ = hipGetLastError(); if (e_ != hipSuccess) return -100 - (int)e_; } while (0)
@@ -86,12 +87,30 @@ using std::max;
#define G8_D_VECMIN 256
// ---- prefill -----------------------------------------------------------------------------------
// Query positions a workgroup: 384 / gqa.
#define G8_P_BQ ((G8_P_NWARPS * 16) / G8_GQA)
// a->splits > 1 with a->scratch set is the KV split (r4d_attn_prefill_split.h): segments in the
// scratch, then the merge. Only the head_dim 256 entries set it (their rows declare the scratch);
// the 128 and 64 legs have no split instance and always run the plain form, unchanged.
template <int HEAD_DIM, int OPT, int KVP>
static int g8_prefill_launch(const R4DArgs* a, hipStream_t s) {
if (a->head_dim != HEAD_DIM || a->block_size != G8_BLOCK_SIZE) return -1;
if (a->q_heads / a->kv_heads != G8_GQA) return -2;
constexpr int BQ = (G8_P_NWARPS * 16) / G8_GQA;
dim3 grid((a->q_len + BQ - 1) / BQ, a->kv_heads, a->num_seqs);
constexpr int BQ = G8_P_BQ;
constexpr bool CAN_SPLIT = HEAD_DIM == 256;
const int splits = (CAN_SPLIT && a->scratch && a->splits > 1) ? a->splits : 1;
g_prefill_last_splits = splits;
dim3 grid((a->q_len + BQ - 1) / BQ * splits, a->kv_heads, a->num_seqs);
if constexpr (CAN_SPLIT) {
if (splits > 1) {
hipLaunchKernelGGL((r4d_attn_prefill_kernel<G8_P_NWARPS, G8_P_TILE, HEAD_DIM, G8_GQA,
G8_BLOCK_SIZE, KVP, OPT | O_PSPLIT>),
grid, dim3(G8_P_NWARPS * 32), 0, s, *a);
CHK8();
return r4d_prefill_combine<HEAD_DIM, G8_D_JPB, G8_D_VECMIN>(a, splits, G8_P_TILE, s);
}
}
hipLaunchKernelGGL((r4d_attn_prefill_kernel<G8_P_NWARPS, G8_P_TILE, HEAD_DIM, G8_GQA,
G8_BLOCK_SIZE, KVP, OPT>),
grid, dim3(G8_P_NWARPS * 32), 0, s, *a);
@@ -189,10 +208,36 @@ static int g8_decode_launch(const R4DArgs* a, RadStream stream,
// ============================================================== the plugin ABI entry points
#if R4D_ATTN_H256
/* The head_dim 256 prefill's KV split at this ratio's workgroup height (r4d_attn_prefill_split.h).
* Test hooks (r4d_selftest), not plugin entries, named by ratio and page since gqa 6 exists at 16-
* and 4-token pages in different units: the law at this card's WGP count, and the split count this
* unit's last prefill launch used. */
#define G8_HOOK_(g, b, suf) r4d_attn_prefill_h256_gqa##g##_bs##b##suf
#define G8_HOOK(g, b, suf) G8_HOOK_(g, b, suf)
extern "C" int G8_HOOK(R4D_ATTN_GQA, G8_BLOCK_SIZE, _splits)(int q_len, int total_q, int num_seqs,
int kv_heads, int max_ctx) {
return r4d_prefill_splits_law(G8_P_BQ, G8_P_TILE, q_len, total_q, num_seqs, kv_heads, max_ctx,
r4d_prefill_wgps());
}
extern "C" int G8_HOOK(R4D_ATTN_GQA, G8_BLOCK_SIZE, _last_splits)(void) {
return g_prefill_last_splits;
}
#undef G8_HOOK
#undef G8_HOOK_
/* The split's f32 partials at declare, by the cache's token row (r4d_prefill_scratch). */
static int64_t g8_prefill_scratch(const RadArgs* a, long long row) {
return r4d_prefill_scratch(a, 256, row, G8_P_BQ, G8_P_TILE);
}
extern "C" int64_t GQ_SYM(r4d_rad_attn_prefill_h256_gqa, _scratch)(const RadArgs* a) {
return g8_prefill_scratch(a, 512);
}
extern "C" int GQ_SYM(r4d_rad_attn_prefill_h256_gqa, _fp8)(const RadArgs* a, RadStream s) {
R4DArgs r;
const int rc = fill_paged(a, &r, 256);
if (rc != RAD_OK) return rc;
r.splits = r4d_prefill_splits(a, &r, G8_P_BQ, G8_P_TILE);
/* Absent descales mean 1.0, as on the gqa6 leg. */
return g8_prefill_launch<256, G8_P_OPT_256, 0>(&r, (hipStream_t)s);
}
@@ -200,6 +245,7 @@ extern "C" int GQ_SYM(r4d_rad_attn_prefill_h256_gqa, _bf16)(const RadArgs* a, Ra
R4DArgs r;
const int rc = fill_paged(a, &r, 256);
if (rc != RAD_OK) return rc;
r.splits = r4d_prefill_splits(a, &r, G8_P_BQ, G8_P_TILE);
return g8_prefill_launch<256, G8_P_OPT_256, 1>(&r, (hipStream_t)s);
}
#endif
@@ -397,10 +443,14 @@ extern "C" int GQ_SYM(r4d_rad_attn_decode_h128_gqa, _bf16)(const RadArgs* a, Rad
* the k6_v4 cache (352 bytes: int6 K, int4 V), prefill, decode and decode with the gate and
* quantiser in the merge. */
#define G8_V4_ENTRIES(TAG, ROWB, XOPT) \
extern "C" int64_t GQ_SYM(r4d_rad_attn_prefill_h256_gqa, _##TAG##_scratch)(const RadArgs* a) { \
return g8_prefill_scratch(a, ROWB); \
} \
extern "C" int GQ_SYM(r4d_rad_attn_prefill_h256_gqa, _##TAG)(const RadArgs* a, RadStream s) { \
R4DArgs r; \
const int rc = fill_paged_row(a, &r, 256, ROWB); \
if (rc != RAD_OK) return rc; \
r.splits = r4d_prefill_splits(a, &r, G8_P_BQ, G8_P_TILE); \
return g8_prefill_launch<256, G8_P_OPT_256 | (XOPT), 0>(&r, (hipStream_t)s); \
} \
extern "C" int64_t GQ_SYM(r4d_rad_attn_decode_h256_gqa, _##TAG##_scratch)(const RadArgs* a) { \
+48 -2
View File
@@ -15,6 +15,7 @@ using std::max;
// are instantiated here with WIN = 1.
#include "r4d_attn_prefill_h256_gqa6.hip"
#include "r4d_attn_decode_h256_gqa6.hip"
#include "r4d_attn_prefill_split.h"
#define CHK4() do { hipError_t e_ = hipGetLastError(); if (e_ != hipSuccess) return -100 - (int)e_; } while (0)
@@ -53,12 +54,44 @@ extern "C" int r4d_attn_decode_h256_gqa2_qld_bf16kv(const R4DArgs* a, hipStream_
#define P_TILE 48
#define P_OPT 3955211
#define P_BQ ((P_NWARPS * 16) / B_GQA)
// ---- the prefill KV split (r4d_attn_prefill_split.h) -------------------------------------------
// Full-attention launches only (window <= 0). The split's partition has no window floor, so the
// split instance is the WIN = 0 form, which computes what WIN = 1 does at window <= 0. A windowed
// launch stays unsplit: its walk is bounded by the window (Gemma 4's 1024 keys: at most
// (1024 + 192) / 48 = 26 tiles a workgroup, whatever the context), so few workgroups cost little.
static bool prefill_h256_gqa2_can_split(const R4DArgs* r) { return r->window <= 0; }
// Test hooks (r4d_selftest), not plugin entries: the law at this unit's geometry and this card's
// WGP count, and the split count this unit's last prefill launch used.
extern "C" int r4d_attn_prefill_h256_gqa2_splits(int q_len, int total_q, int num_seqs,
int kv_heads, int max_ctx) {
return r4d_prefill_splits_law(P_BQ, P_TILE, q_len, total_q, num_seqs, kv_heads, max_ctx,
r4d_prefill_wgps());
}
extern "C" int r4d_attn_prefill_h256_gqa2_last_splits(void) { return g_prefill_last_splits; }
// a->splits > 1 with a->scratch set and no window is the split form: segments in the scratch, then
// the merge. Anything else is the plain (windowed) form, unchanged.
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);
constexpr int BQ = P_BQ;
const int splits =
(a->scratch && a->splits > 1 && prefill_h256_gqa2_can_split(a)) ? a->splits : 1;
g_prefill_last_splits = splits;
dim3 grid((a->q_len + BQ - 1) / BQ * splits, a->kv_heads, a->num_seqs);
if (splits > 1) {
hipLaunchKernelGGL((r4d_attn_prefill_kernel<P_NWARPS, P_TILE, B_HEAD_DIM, B_GQA,
B_BLOCK_SIZE, KVP, P_OPT | O_PSPLIT, 0>),
grid, dim3(P_NWARPS * 32), 0, s, *a);
CHK4();
// The full-attention legs' merge constants, as r4d_decode_launch_win takes them: two rows
// a block, the wave form from 256 rows.
return r4d_prefill_combine<B_HEAD_DIM, 2, 256>(a, splits, P_TILE, s);
}
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);
@@ -66,6 +99,17 @@ static int prefill_launch_h256_gqa2(const R4DArgs* a, hipStream_t s) {
return 0;
}
/* The split's f32 partials at declare (r4d_prefill_scratch). None for a windowed layer: `window`
* is the op's parameter, the same at declare and at every launch, and a windowed launch never
* splits. */
static int64_t prefill_scratch_h256_gqa2(const RadArgs* a) {
if (r4d_geti_or(a, "window", 0) > 0) return 0;
return r4d_prefill_scratch(a, B_HEAD_DIM, 2LL * B_HEAD_DIM, P_BQ, P_TILE);
}
extern "C" int64_t r4d_rad_attn_prefill_h256_gqa2_scratch(const RadArgs* a) {
return prefill_scratch_h256_gqa2(a);
}
extern "C" int r4d_attn_prefill_h256_gqa2_fp8kv(const R4DArgs* a, hipStream_t s) {
return prefill_launch_h256_gqa2<0>(a, s);
}
@@ -79,11 +123,13 @@ 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;
if (prefill_h256_gqa2_can_split(&r)) r.splits = r4d_prefill_splits(a, &r, P_BQ, P_TILE);
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;
if (prefill_h256_gqa2_can_split(&r)) r.splits = r4d_prefill_splits(a, &r, P_BQ, P_TILE);
return r4d_attn_prefill_h256_gqa2_bf16kv(&r, (hipStream_t)s);
}
+17 -139
View File
@@ -25,6 +25,7 @@
using std::max;
#include "r4d_attn_prefill_h256_gqa6.hip"
#include "r4d_attn_decode_h256_gqa6.hip"
#include "r4d_attn_prefill_split.h"
#define CHK() do { hipError_t e_ = hipGetLastError(); if (e_ != hipSuccess) return -100 - (int)e_; } while (0)
@@ -51,66 +52,18 @@ extern "C" void r4d_attn_dims(int* head_dim, int* gqa, int* block_size, int* max
// (36-48 bytes per lane, ~4% of the prefill). At 2 the spill is 20 bytes, outside the WMMA groups.
#define P_OPT 3954187
// ---- the prefill KV split ----------------------------------------------------------------------
// The query-tiled grid is ceil(q_len / 64) x kv_heads x num_seqs, one 768-thread workgroup a WGP,
// and each workgroup walks its whole causal range serially. So below one workgroup per WGP a launch
// costs one full walk of the context whatever its row count: Qwen3.8-27B at TP2 (kv_heads 2) runs a
// 53-row tail over 188k keys as 2 workgroups on 32 WGPs, ~17.7 ms a layer, against ~21 ms a
// workgroup for a 1024-row piece filling all 32 (audits 9a/9b; profiled tails, attention: 63 ms
// at 41k / 80 rows, 152 at 97k / 345, 283 at 188k / 53). Such launches split the KV (O_PSPLIT)
// and finish in the split-KV merge.
//
// Workgroups the card runs at once: one a WGP. HIP's multiProcessorCount counts WGPs on RDNA (32 on
// the R9700, see want_wg in r4d_hc_bf16.hip); 32 if it cannot be read.
static int prefill_wgps() {
static const int v = [] {
int dev = 0;
hipDeviceProp_t pr{};
if (hipGetDevice(&dev) != hipSuccess || hipGetDeviceProperties(&pr, dev) != hipSuccess)
return 32;
return pr.multiProcessorCount > 0 ? pr.multiProcessorCount : 32;
}();
return v;
}
// ---- the prefill KV split (r4d_attn_prefill_split.h) -------------------------------------------
// Query positions a workgroup: 64.
#define P_BQ ((P_NWARPS * 16) / A_GQA)
// The split law. `units` counts the workgroups that do work: a ragged batch's grid is sized from
// its longest sequence (q_len), but a shorter sequence's query tiles past its end exit at once, so
// the count is bounded by sum ceil(q_s / 64) <= (total_q + 63 n) / 64 as well (exact for one
// sequence), so a ragged launch's longest sequence does not make its others look like full tiles.
// One S applies to every sequence of the launch; a mixed step's decode rows reach the split-KV
// decode row instead, by the arch's per-band issue (AttnGatedFP8::step). A launch over part of a
// step (q and out whole, cu past row 0) counts the step's rows in total_q, so the bound is loose
// there and may keep it unsplit.
// At `wgps` units or more the card is full: no split. Otherwise S rounds of equal workgroups cost
// rounds / S walks, so take one round (S1 = wgps / units) or two (S2 = 2 wgps / units), whichever
// is faster, the smaller S on a tie (fewer partials): 53 rows (2 units) -> 16 splits, 80 -> 8,
// 345 (12 units) -> 5 (60 workgroups, 0.4 walks against 0.5 at 2), 576 -> 3, 1024 and up -> 1.
// Each split keeps at least 8 tiles of max_ctx, the only context the host sees (the engine passes
// the model's bound, so in serving this caps nothing; a short context's splits past its tiles exit
// at once). At most 64, the vectorised merge's limit.
static inline int r4d_prefill_splits_law(int q_len, int total_q, int num_seqs, int kv_heads,
int max_ctx, int wgps) {
constexpr int BQ = (P_NWARPS * 16) / A_GQA;
const long n = std::max(1, num_seqs);
const long qt = std::min(n * ((std::max(1, q_len) + BQ - 1) / BQ),
(std::max(1, total_q) + (BQ - 1) * n) / BQ);
const long units = (long)std::max(1, kv_heads) * std::max(1L, qt);
if (units >= wgps) return 1;
const int s1 = (int)(wgps / units), s2 = (int)(2L * wgps / units);
int sp = s2 > 2 * s1 ? s2 : s1;
const int cap = ((max_ctx > 0 ? max_ctx : (1 << 30) - 1) + P_TILE - 1) / P_TILE / 8;
sp = std::min(sp, std::max(1, cap));
return std::max(1, std::min(sp, 64));
}
// The law at this card's WGP count. A test hook (r4d_selftest), not a plugin entry.
// The law at this unit's geometry and this card's WGP count. A test hook (r4d_selftest), not a
// plugin entry.
extern "C" int r4d_attn_prefill_h256_gqa6_splits(int q_len, int total_q, int num_seqs,
int kv_heads, int max_ctx) {
return r4d_prefill_splits_law(q_len, total_q, num_seqs, kv_heads, max_ctx, prefill_wgps());
return r4d_prefill_splits_law(P_BQ, P_TILE, q_len, total_q, num_seqs, kv_heads, max_ctx,
r4d_prefill_wgps());
}
// The split count the last prefill launch in this process used, 1 unsplit. A test hook: lets
// r4d_selftest tell a split launch from an unsplit one whose bytes happen to agree. Not atomic.
static int g_prefill_last_splits = 0;
// The split count this unit's last prefill launch used, 1 unsplit. A test hook (r4d_selftest).
extern "C" int r4d_attn_prefill_h256_gqa6_last_splits(void) { return g_prefill_last_splits; }
static int prefill_combine(const R4DArgs* a, int splits, hipStream_t s);
@@ -182,21 +135,9 @@ static int prefill_legs_launch(const R4DArgs* a, hipStream_t s, int legs) {
// 288 it wins by 1.2-20%. The cut is four rows per CU.
#define D_VECMIN 256
// The prefill split's merge: the decode merge over f32 partials (PF16 0), at the prefill tile, so
// it recomputes the kernel's partition; vectorised on the decode launcher's terms. SPAN: the launch
// may cover part of a step's rows (ArPieces' pieces, a band-split step's prefill half: cu_q starts
// past row 0, or ends before total_q), and the rest are another launch's.
// The prefill split's merge (r4d_prefill_combine), vectorised on the decode launcher's terms.
static int prefill_combine(const R4DArgs* a, int splits, hipStream_t s) {
const int jobs3 = r4d_q_total(*a) * a->q_heads;
if (splits <= 64 && jobs3 >= D_VECMIN)
hipLaunchKernelGGL((r4d_attn_splitkv_combine_vec_kernel<A_HEAD_DIM, 4, 0, D_JPB, 0, 1>),
dim3((jobs3 + D_JPB - 1) / D_JPB), dim3(32 * D_JPB), 0, s, *a, splits,
P_TILE);
else
hipLaunchKernelGGL((r4d_attn_splitkv_combine_kernel<A_HEAD_DIM, 4, 0, 0, 1>), dim3(jobs3),
dim3(256), splits * sizeof(float), s, *a, splits, P_TILE);
CHK();
return 0;
return r4d_prefill_combine<A_HEAD_DIM, D_JPB, D_VECMIN>(a, splits, P_TILE, s);
}
// Split law: r4d_decode_splits (r4d_attn_decode_h256_gqa6.hip).
@@ -268,80 +209,17 @@ extern "C" int r4d_attn_decode_h256_gqa6_k6v4kv(const R4DArgs* a, hipStream_t s)
// the .so carries an undefined symbol that only r4d_selftest catches.
/* ------------------------------------------------------------------ paged prefill */
/* The KV split's count before the scratch bound: the case's `splits` parameter when it names one
* (r4d_selftest; the prefill rows declare no such axis, so the engine passes none), else
* R4D_PREFILL_SPLITS, else the law. R4D_PREFILL_SPLITS is a tuning and A/B knob, not
* configuration: 0 or unset = the law, 1 = never split, N = N splits for every launch (at most 64,
* and at most what the scratch holds, see prefill_splits). Read once. */
static int prefill_splits_want(const RadArgs* a, const R4DArgs* r) {
const int sp = split_of(a);
if (sp > 0) return std::min(sp, 64);
static const int env = [] {
const char* e = getenv("R4D_PREFILL_SPLITS");
return e && *e ? atoi(e) : 0;
}();
if (env > 0) return std::min(env, 64);
return r4d_prefill_splits_law(r->q_len, r4d_q_total(*r), r->num_seqs, r->kv_heads, r->max_ctx,
prefill_wgps());
}
/* The launch's split count, 1 for the plain form: what prefill_splits_want asks, cut to the
* segments the caller's scratch holds, so a forced count past the declared worst case runs with as
* many as fit rather than writing past the region. No scratch (a row without the hook, a direct
* caller of the r4d_attn_prefill_* entry points) is the plain form. */
/* The launch's KV split count (r4d_prefill_splits) at this unit's geometry, 1 for the plain form. */
static int prefill_splits(const RadArgs* a, const R4DArgs* r) {
if (!a->scratch || a->scratch_bytes <= 0) return 1;
int sp = prefill_splits_want(a, r);
const long per = r4d_decode_scratch(r, 1, 0); /* one segment of every row, f32 */
if (sp > 1 && per > 0 && a->scratch_bytes / per < sp) sp = (int)(a->scratch_bytes / per);
return std::max(1, sp);
return r4d_prefill_splits(a, r, P_BQ, P_TILE);
}
/* Bytes of the split's f32 partials, at declare the most any launch in the band can take.
*
* Declare passes the band's upper bound as q_len and no tensors, so the worst case is swept over
* every sequence count up to max_seqs and every row count up to the band, in both the uniform form
* (q_len = rows / n) and the ragged one (q_len = min(band, rows), as fill_paged takes it). Only
* launches with fewer than one workgroup a WGP split, and the units of a launch of `t` rows are at
* least kv_heads * ceil(t / 64), so the sweep stops there. The law keeps units * S <= 2 wgps, so
* rows * S <= 64 * gqa * 2 wgps: 24576 rows of 1032 bytes, 25.4 MB, for any kv_heads (23.8 MB is
* reached, at 384 rows x 5 and 640 x 3). Not monotone in the rows, hence the sweep. A launch over
* part of a step's rows counts the whole step's in its partial layout (total_q), more than this
* covers when it is long; prefill_splits then cuts S to what fits, down to the plain form.
*
* Never an error: a refusal would unseat the prefill row, which ran without scratch before the
* split. A shape it cannot read gets none, and its launches run the plain form. */
/* The split's f32 partials at declare (r4d_prefill_scratch: 23.8 MB is reached at this geometry,
* at 384 rows x 5 and 640 x 3), by the cache's token row: fp8 and bf16 (2 * head_dim), fp8_v4 (400
* bytes), k6_v4 (352). */
static int64_t prefill_scratch(const RadArgs* a, long long row) {
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) {
const long long gqa = r4d_geti_or(a, "gqa", 1);
const int kv = (int)std::max(1LL, gqa > 0 ? n_head / gqa : n_head);
const int band = (int)std::max(1LL, r4d_geti_or(a, "q_len", 1));
const int mctx = (int)r4d_geti_or(a, "max_ctx", 0);
const int w = prefill_wgps();
constexpr int BQ = (P_NWARPS * 16) / A_GQA;
const int64_t per_row = (int64_t)n_head * (A_HEAD_DIM + 2) * 4;
int64_t worst = 0;
for (long long n = 1; n <= max_seqs; ++n)
for (long long t = n; t <= band; ++t) {
if ((long long)kv * ((t + BQ - 1) / BQ) >= w) break;
int sp = r4d_prefill_splits_law((int)std::min<long long>(band, t), (int)t, (int)n,
kv, mctx, w);
if (t % n == 0)
sp = std::max(sp, r4d_prefill_splits_law((int)(t / n), (int)t, (int)n, kv,
mctx, w));
if (sp > 1) worst = std::max(worst, (int64_t)t * sp * per_row);
}
return worst;
}
if (fill_paged_row(a, &r, 256, row) != RAD_OK) return 0;
const int sp = prefill_splits_want(a, &r);
return sp > 1 ? (int64_t)r4d_decode_scratch(&r, sp, 0) : 0;
return r4d_prefill_scratch(a, A_HEAD_DIM, row, P_BQ, P_TILE);
}
/* By the cache's token row: fp8 and bf16 (2 * head_dim), fp8_v4 (400 bytes), k6_v4 (352). */
extern "C" int64_t r4d_rad_attn_prefill_h256_scratch(const RadArgs* a) {
return prefill_scratch(a, 512);
}
+2 -2
View File
@@ -69,8 +69,8 @@
// O_V4: the fp8_v4 cache, O_V4 | O_K6 the k6_v4 cache (see r4d_attn_decode_h256_gqa6.hip).
#define O_V4 33554432
#define O_K6 67108864
// O_PSPLIT: a KV split for launches with too few workgroups to fill the card (prefill_splits in
// r4d_attn_paged_h256_gqa6.hip). blockIdx.x is qtile * a.splits + split, and the workgroup walks
// O_PSPLIT: a KV split for launches with too few workgroups to fill the card (r4d_prefill_splits
// in r4d_attn_prefill_split.h). blockIdx.x is qtile * a.splits + split, and the workgroup walks
// only its segment of the sequence's tiles, cut as r4d_attn_tps cuts them (the partition the
// split-KV merge recomputes from ctx), clipped at its causal end. Instead of bf16 out it stores the
// unnormalised f32 O, m_ref and l in the decode kernel's partial layout (r4d_attn_decode_body,
+159
View File
@@ -0,0 +1,159 @@
// The prefill KV split, host side: the law, the scratch it declares and the merge launch, shared by
// every head-256 query-tiled prefill launcher (the h256/gqa6 unit, r4d_attn_paged_gqa8.hip's ratios
// 4, 6 over 4-token pages, 8 and 12, and the h256/gqa2 unit). The kernel side is O_PSPLIT in
// r4d_attn_prefill_h256_gqa6.hip. Include after r4d_attn_decode_h256_gqa6.hip, whose partial
// layout and merges it reuses. No entry points here: each unit defines its own, with its geometry.
//
// Why. The query-tiled grid is ceil(q_len / BQ) x kv_heads x num_seqs, one 768-thread workgroup a
// WGP (BQ = NWARPS * 16 / gqa query positions: 192 at gqa 2, 96 at 4, 64 at 6, 48 at 8, 32 at 12),
// and each workgroup walks its whole causal range serially. So below one workgroup per WGP a launch
// costs one full walk of the context whatever its row count: Qwen3.8-27B at TP2 (gqa 6, kv_heads 2)
// runs a 53-row tail over 188k keys as 2 workgroups on 32 WGPs, ~17.7 ms a layer, against ~21 ms a
// workgroup for a 1024-row piece filling all 32 (profiled tails, attention: 63 ms at 41k / 80 rows,
// 152 at 97k / 345, 283 at 188k / 53). The geometries with one KV head a rank are worse off:
// Flash-Next's gqa 12 at TP2 runs 53 rows as 2 workgroups as well, and any chunk up to 32 rows as
// one. Such launches split the KV and finish in the split-KV merge.
#pragma once
#include <cstdlib>
// Workgroups the card runs at once: one a WGP. HIP's multiProcessorCount counts WGPs on RDNA (32 on
// the R9700, see want_wg in r4d_hc_bf16.hip); 32 if it cannot be read.
static inline int r4d_prefill_wgps() {
static const int v = [] {
int dev = 0;
hipDeviceProp_t pr{};
if (hipGetDevice(&dev) != hipSuccess || hipGetDeviceProperties(&pr, dev) != hipSuccess)
return 32;
return pr.multiProcessorCount > 0 ? pr.multiProcessorCount : 32;
}();
return v;
}
// The split law, at the launch's own geometry: `bq` query positions a workgroup, `tile` keys a KV
// tile. `units` counts the workgroups that do work: a ragged batch's grid is sized from its longest
// sequence (q_len), but a shorter sequence's query tiles past its end exit at once, so the count is
// bounded by sum ceil(q_s / bq) <= (total_q + (bq - 1) n) / bq as well (exact for one sequence), so a
// ragged launch's longest sequence does not make its others look like full tiles. One S applies to
// every sequence of the launch; a mixed step's decode rows reach the split-KV decode row instead, by
// the arch's per-band issue (AttnGatedFP8::step). A launch over part of a step (q and out whole, cu
// past row 0) counts the step's rows in total_q, so the bound is loose there and may keep it
// unsplit.
// At `wgps` units or more the card is full: no split. Otherwise S rounds of equal workgroups cost
// rounds / S walks, so take one round (S1 = wgps / units) or two (S2 = 2 wgps / units), whichever
// is faster, the smaller S on a tie (fewer partials). At gqa 6 with 2 KV heads: 53 rows (2 units)
// -> 16 splits, 80 -> 8, 345 (12 units) -> 5 (60 workgroups, 0.4 walks against 0.5 at 2), 576 -> 3,
// 1024 and up -> 1. At gqa 12 with 1 KV head: up to 32 rows (1 unit) -> 32, 53 -> 16, 345 (11) -> 5.
// Each split keeps at least 8 tiles of max_ctx, the only context the host sees (the engine passes
// the model's bound, so in serving this caps nothing; a short context's splits past its tiles exit
// at once). At most 64, the vectorised merge's limit.
static inline int r4d_prefill_splits_law(int bq, int tile, int q_len, int total_q, int num_seqs,
int kv_heads, int max_ctx, int wgps) {
const long n = std::max(1, num_seqs);
const long qt = std::min(n * ((std::max(1, q_len) + bq - 1) / bq),
(std::max(1, total_q) + (long)(bq - 1) * n) / bq);
const long units = (long)std::max(1, kv_heads) * std::max(1L, qt);
if (units >= wgps) return 1;
const int s1 = (int)(wgps / units), s2 = (int)(2L * wgps / units);
int sp = s2 > 2 * s1 ? s2 : s1;
const int cap = ((max_ctx > 0 ? max_ctx : (1 << 30) - 1) + tile - 1) / tile / 8;
sp = std::min(sp, std::max(1, cap));
return std::max(1, std::min(sp, 64));
}
// The split count the last prefill launch of this unit used, 1 unsplit. Read by each unit's test
// hook (r4d_selftest), which tells a split launch from an unsplit one whose bytes happen to agree.
// Not atomic.
static int g_prefill_last_splits = 0;
/* The KV split's count before the scratch bound: the case's `splits` parameter when it names one
* (r4d_selftest; the prefill rows declare no such axis, so the engine passes none), else
* R4D_PREFILL_SPLITS, else the law. R4D_PREFILL_SPLITS is a tuning and A/B knob, not
* configuration: 0 or unset = the law, 1 = never split, N = N splits for every launch (at most 64,
* and at most what the scratch holds, see r4d_prefill_splits). Read once. */
static inline int r4d_prefill_splits_want(const RadArgs* a, const R4DArgs* r, int bq, int tile) {
const int sp = split_of(a);
if (sp > 0) return std::min(sp, 64);
static const int env = [] {
const char* e = getenv("R4D_PREFILL_SPLITS");
return e && *e ? atoi(e) : 0;
}();
if (env > 0) return std::min(env, 64);
return r4d_prefill_splits_law(bq, tile, r->q_len, r4d_q_total(*r), r->num_seqs, r->kv_heads,
r->max_ctx, r4d_prefill_wgps());
}
/* The launch's split count, 1 for the plain form: what r4d_prefill_splits_want asks, cut to the
* segments the caller's scratch holds, so a forced count past the declared worst case runs with as
* many as fit rather than writing past the region. No scratch (a row without the hook, a direct
* caller of the r4d_attn_prefill_* entry points) is the plain form. */
static inline int r4d_prefill_splits(const RadArgs* a, const R4DArgs* r, int bq, int tile) {
if (!a->scratch || a->scratch_bytes <= 0) return 1;
int sp = r4d_prefill_splits_want(a, r, bq, tile);
const long per = r4d_decode_scratch(r, 1, 0); /* one segment of every row, f32 */
if (sp > 1 && per > 0 && a->scratch_bytes / per < sp) sp = (int)(a->scratch_bytes / per);
return std::max(1, sp);
}
/* Bytes of the split's f32 partials, at declare the most any launch in the band can take.
*
* Declare passes the band's upper bound as q_len and no tensors, so the worst case is swept over
* every sequence count up to max_seqs and every row count up to the band, in both the uniform form
* (q_len = rows / n) and the ragged one (q_len = min(band, rows), as fill_paged takes it). Only
* launches with fewer than one workgroup a WGP split, and the units of a launch of `t` rows are at
* least kv_heads * ceil(t / bq), so the sweep stops there. The law keeps units * S <= 2 wgps, so
* rows * S * q_heads <= bq * gqa * 2 wgps = NWARPS * 16 * 2 wgps, the same for every ratio at 24
* waves: 24576 (row, head) pairs of 1032 bytes at head_dim 256, 25.4 MB, for any kv_heads. Not
* monotone in the rows, hence the sweep. A launch over part of a step's rows counts the whole
* step's in its partial layout (total_q), more than this covers when it is long; r4d_prefill_splits
* then cuts S to what fits, down to the plain form.
*
* Never an error: a refusal would unseat the prefill row, which ran without scratch before the
* split. A shape it cannot read gets none, and its launches run the plain form. */
static inline int64_t r4d_prefill_scratch(const RadArgs* a, int head_dim, long long row, int bq,
int tile) {
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) {
const long long gqa = r4d_geti_or(a, "gqa", 1);
const int kv = (int)std::max(1LL, gqa > 0 ? n_head / gqa : n_head);
const int band = (int)std::max(1LL, r4d_geti_or(a, "q_len", 1));
const int mctx = (int)r4d_geti_or(a, "max_ctx", 0);
const int w = r4d_prefill_wgps();
const int64_t per_row = (int64_t)n_head * (head_dim + 2) * 4;
int64_t worst = 0;
for (long long n = 1; n <= max_seqs; ++n)
for (long long t = n; t <= band; ++t) {
if ((long long)kv * ((t + bq - 1) / bq) >= w) break;
int sp = r4d_prefill_splits_law(bq, tile, (int)std::min<long long>(band, t), (int)t,
(int)n, kv, mctx, w);
if (t % n == 0)
sp = std::max(sp, r4d_prefill_splits_law(bq, tile, (int)(t / n), (int)t, (int)n,
kv, mctx, w));
if (sp > 1) worst = std::max(worst, (int64_t)t * sp * per_row);
}
return worst;
}
if (fill_paged_row(a, &r, head_dim, row) != RAD_OK) return 0;
const int sp = r4d_prefill_splits_want(a, &r, bq, tile);
return sp > 1 ? (int64_t)r4d_decode_scratch(&r, sp, 0) : 0;
}
// The split's merge: the decode merge over f32 partials (PF16 0), at the prefill tile, so it
// recomputes the kernel's partition; one wave a row from VECMIN rows (JPB rows a block), else the
// LDS-tree form, on the decode launchers' terms. SPAN: the launch may cover part of a step's rows
// (ArPieces' pieces, a band-split step's prefill half: cu_q starts past row 0, or ends before
// total_q), and the rest are another launch's.
template <int HEAD_DIM, int JPB, int VECMIN>
static inline int r4d_prefill_combine(const R4DArgs* a, int splits, int tile, hipStream_t s) {
const int jobs3 = r4d_q_total(*a) * a->q_heads;
if (splits <= 64 && jobs3 >= VECMIN)
hipLaunchKernelGGL((r4d_attn_splitkv_combine_vec_kernel<HEAD_DIM, 4, 0, JPB, 0, 1>),
dim3((jobs3 + JPB - 1) / JPB), dim3(32 * JPB), 0, s, *a, splits, tile);
else
hipLaunchKernelGGL((r4d_attn_splitkv_combine_kernel<HEAD_DIM, 4, 0, 0, 1>), dim3(jobs3),
dim3(HEAD_DIM), splits * sizeof(float), s, *a, splits, tile);
const hipError_t e = hipGetLastError();
return e == hipSuccess ? 0 : -100 - (int)e;
}
+9
View File
@@ -132,6 +132,7 @@ int64_t r4d_rad_attn_decode_h128_scratch(const RadArgs*);
* windowed prefill (the gqa6 leg's r4d_attn_prefill_kernel with WIN on). */
int r4d_rad_attn_prefill_h256_gqa2_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_prefill_h256_gqa2_bf16 (const RadArgs*, RadStream);
int64_t r4d_rad_attn_prefill_h256_gqa2_scratch(const RadArgs*);
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*);
@@ -299,6 +300,7 @@ int64_t r4d_rad_quant_cols_fp8_scratch(const RadArgs*);
* row's constraint value) is 8, against the gqa6 leg's 10. */
int r4d_rad_attn_prefill_h256_gqa8_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_prefill_h256_gqa8_bf16(const RadArgs*, RadStream);
int64_t r4d_rad_attn_prefill_h256_gqa8_scratch(const RadArgs*);
int r4d_rad_attn_decode_h256_gqa8_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_decode_h256_gqa8_bf16 (const RadArgs*, RadStream);
int64_t r4d_rad_attn_decode_h256_gqa8_scratch(const RadArgs*);
@@ -318,6 +320,7 @@ int64_t r4d_rad_attn_decode_h128_gqa1_scratch(const RadArgs*);
* (r4d_attn_paged_h256_gqa4.hip). */
int r4d_rad_attn_prefill_h256_gqa4_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_prefill_h256_gqa4_bf16(const RadArgs*, RadStream);
int64_t r4d_rad_attn_prefill_h256_gqa4_scratch(const RadArgs*);
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*);
@@ -335,6 +338,7 @@ int64_t r4d_rad_attn_decode_h128_gqa2_scratch(const RadArgs*);
* another instantiation (r4d_attn_paged_h256_gqa12.hip). */
int r4d_rad_attn_prefill_h256_gqa12_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_prefill_h256_gqa12_bf16(const RadArgs*, RadStream);
int64_t r4d_rad_attn_prefill_h256_gqa12_scratch(const RadArgs*);
int r4d_rad_attn_decode_h256_gqa12_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_decode_h256_gqa12_bf16 (const RadArgs*, RadStream);
int r4d_rad_attn_decode_gq_h256_gqa12_fp8(const RadArgs*, RadStream);
@@ -345,6 +349,7 @@ int64_t r4d_rad_attn_decode_h256_gqa12_scratch(const RadArgs*);
* the decode rows' constraint value. */
int r4d_rad_attn_prefill_h256_gqa6_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_prefill_h256_gqa6_bf16(const RadArgs*, RadStream);
int64_t r4d_rad_attn_prefill_h256_gqa6_scratch(const RadArgs*);
int r4d_rad_attn_decode_h256_gqa6_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_decode_h256_gqa6_bf16 (const RadArgs*, RadStream);
int64_t r4d_rad_attn_decode_h256_gqa6_scratch(const RadArgs*);
@@ -356,18 +361,22 @@ int r4d_rad_attn_decode_gq_h256_gqa6_bf16(const RadArgs*, RadStream);
/* The fp8_v4 and k6_v4 caches on both: prefill, decode, and decode with the gate and quantiser in
* the merge (R4D_ATTN_V4 in r4d_attn_paged_gqa8.hip). */
int r4d_rad_attn_prefill_h256_gqa12_fp8v4(const RadArgs*, RadStream);
int64_t r4d_rad_attn_prefill_h256_gqa12_fp8v4_scratch(const RadArgs*);
int r4d_rad_attn_decode_h256_gqa12_fp8v4(const RadArgs*, RadStream);
int r4d_rad_attn_decode_gq_h256_gqa12_fp8v4(const RadArgs*, RadStream);
int64_t r4d_rad_attn_decode_h256_gqa12_fp8v4_scratch(const RadArgs*);
int r4d_rad_attn_prefill_h256_gqa12_k6v4(const RadArgs*, RadStream);
int64_t r4d_rad_attn_prefill_h256_gqa12_k6v4_scratch(const RadArgs*);
int r4d_rad_attn_decode_h256_gqa12_k6v4(const RadArgs*, RadStream);
int r4d_rad_attn_decode_gq_h256_gqa12_k6v4(const RadArgs*, RadStream);
int64_t r4d_rad_attn_decode_h256_gqa12_k6v4_scratch(const RadArgs*);
int r4d_rad_attn_prefill_h256_gqa6_fp8v4(const RadArgs*, RadStream);
int64_t r4d_rad_attn_prefill_h256_gqa6_fp8v4_scratch(const RadArgs*);
int r4d_rad_attn_decode_h256_gqa6_fp8v4(const RadArgs*, RadStream);
int r4d_rad_attn_decode_gq_h256_gqa6_fp8v4(const RadArgs*, RadStream);
int64_t r4d_rad_attn_decode_h256_gqa6_fp8v4_scratch(const RadArgs*);
int r4d_rad_attn_prefill_h256_gqa6_k6v4(const RadArgs*, RadStream);
int64_t r4d_rad_attn_prefill_h256_gqa6_k6v4_scratch(const RadArgs*);
int r4d_rad_attn_decode_h256_gqa6_k6v4(const RadArgs*, RadStream);
int r4d_rad_attn_decode_gq_h256_gqa6_k6v4(const RadArgs*, RadStream);
int64_t r4d_rad_attn_decode_h256_gqa6_k6v4_scratch(const RadArgs*);
+28 -14
View File
@@ -6425,7 +6425,8 @@ static const RadKernelInfo kKernels[] = {
"bf16 query, fp8-e4m3 kv cache with per-(seq,head) descales, bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH256Gqa2Fp8), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa2_fp8, nullptr, nullptr, nullptr,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa2_fp8, r4d_rad_attn_prefill_h256_gqa2_scratch,
nullptr, nullptr,
r4d_shape_attn_paged },
{ "attn_prefill_h256_gqa2_bf16kv", "attn_paged", "attn",
@@ -6435,7 +6436,8 @@ static const RadKernelInfo kKernels[] = {
"bf16 query, bf16 kv cache, bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH256Gqa2Bf16), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa2_bf16, nullptr, nullptr, nullptr,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa2_bf16, r4d_rad_attn_prefill_h256_gqa2_scratch,
nullptr, nullptr,
r4d_shape_attn_paged },
{ "attn_decode_h256_gqa2_fp8kv", "attn_paged", "attn",
@@ -6590,7 +6592,8 @@ static const RadKernelInfo kKernels[] = {
"bf16 query, fp8-e4m3 kv cache with per-(seq,head) descales, bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH256G8Fp8), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa8_fp8, nullptr, nullptr, nullptr,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa8_fp8, r4d_rad_attn_prefill_h256_gqa8_scratch,
nullptr, nullptr,
r4d_shape_attn_paged },
{ "attn_prefill_h256_gqa8_bf16kv", "attn_paged", "attn",
@@ -6599,7 +6602,8 @@ static const RadKernelInfo kKernels[] = {
"bf16 query, bf16 kv cache, bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH256G8Bf16), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa8_bf16, nullptr, nullptr, nullptr,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa8_bf16, r4d_rad_attn_prefill_h256_gqa8_scratch,
nullptr, nullptr,
r4d_shape_attn_paged },
{ "attn_decode_h256_gqa8_fp8kv", "attn_paged", "attn",
@@ -6628,7 +6632,8 @@ static const RadKernelInfo kKernels[] = {
"bf16 query, fp8-e4m3 kv cache with per-(seq,head) descales, bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH256G4Fp8), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa4_fp8, nullptr, nullptr, nullptr,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa4_fp8, r4d_rad_attn_prefill_h256_gqa4_scratch,
nullptr, nullptr,
r4d_shape_attn_paged },
{ "attn_prefill_h256_gqa4_bf16kv", "attn_paged", "attn",
@@ -6637,7 +6642,8 @@ static const RadKernelInfo kKernels[] = {
"bf16 query, bf16 kv cache, bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH256G4Bf16), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa4_bf16, nullptr, nullptr, nullptr,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa4_bf16, r4d_rad_attn_prefill_h256_gqa4_scratch,
nullptr, nullptr,
r4d_shape_attn_paged },
{ "attn_decode_h256_gqa4_fp8kv", "attn_paged", "attn",
@@ -6716,7 +6722,8 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
"bf16 query, fp8-e4m3 kv cache with per-(seq,head) descales, bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH256G12Fp8), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa12_fp8, nullptr, nullptr, nullptr,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa12_fp8, r4d_rad_attn_prefill_h256_gqa12_scratch,
nullptr, nullptr,
r4d_shape_attn_paged },
{ "attn_prefill_h256_gqa12_bf16kv", "attn_paged", "attn",
@@ -6725,7 +6732,8 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
"bf16 query, bf16 kv cache, bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH256G12Bf16), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa12_bf16, nullptr, nullptr, nullptr,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa12_bf16, r4d_rad_attn_prefill_h256_gqa12_scratch,
nullptr, nullptr,
r4d_shape_attn_paged },
{ "attn_decode_h256_gqa12_fp8kv", "attn_paged", "attn",
@@ -6782,7 +6790,8 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
"bf16 query, fp8-e4m3 kv cache with per-(seq,head) descales, bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH256G6Bs4Fp8), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa6_fp8, nullptr, nullptr, nullptr,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa6_fp8, r4d_rad_attn_prefill_h256_gqa6_scratch,
nullptr, nullptr,
r4d_shape_attn_paged },
{ "attn_prefill_h256_gqa6_bs4_bf16kv", "attn_paged", "attn",
@@ -6791,7 +6800,8 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
"bf16 query, bf16 kv cache, bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH256G6Bs4Bf16), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa6_bf16, nullptr, nullptr, nullptr,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa6_bf16, r4d_rad_attn_prefill_h256_gqa6_scratch,
nullptr, nullptr,
r4d_shape_attn_paged },
{ "attn_decode_h256_gqa6_bs4_fp8kv", "attn_paged", "attn",
@@ -6846,7 +6856,8 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
"bf16 query, fp8_v4 kv cache (E4M3 K, int4 V in groups of 32 with f16 scales), bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH256G12Fp8v4), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa12_fp8v4, nullptr, nullptr, nullptr,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa12_fp8v4, r4d_rad_attn_prefill_h256_gqa12_fp8v4_scratch,
nullptr, nullptr,
nullptr },
{ "attn_decode_h256_gqa12_fp8v4", "attn_paged", "attn",
@@ -6876,7 +6887,8 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
"bf16 query, k6_v4 kv cache (int6 K, int4 V, each in groups of 32 with f16 scales), bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH256G12K6v4), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa12_k6v4, nullptr, nullptr, nullptr,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa12_k6v4, r4d_rad_attn_prefill_h256_gqa12_k6v4_scratch,
nullptr, nullptr,
nullptr },
{ "attn_decode_h256_gqa12_k6v4", "attn_paged", "attn",
@@ -6906,7 +6918,8 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
"bf16 query, fp8_v4 kv cache (E4M3 K, int4 V in groups of 32 with f16 scales), bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH256G6Bs4Fp8v4), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa6_fp8v4, nullptr, nullptr, nullptr,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa6_fp8v4, r4d_rad_attn_prefill_h256_gqa6_fp8v4_scratch,
nullptr, nullptr,
nullptr },
{ "attn_decode_h256_gqa6_bs4_fp8v4", "attn_paged", "attn",
@@ -6936,7 +6949,8 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
"bf16 query, k6_v4 kv cache (int6 K, int4 V, each in groups of 32 with f16 scales), bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH256G6Bs4K6v4), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa6_k6v4, nullptr, nullptr, nullptr,
nullptr, nullptr, r4d_rad_attn_prefill_h256_gqa6_k6v4, r4d_rad_attn_prefill_h256_gqa6_k6v4_scratch,
nullptr, nullptr,
nullptr },
{ "attn_decode_h256_gqa6_bs4_k6v4", "attn_paged", "attn",
+118 -67
View File
@@ -1281,13 +1281,20 @@ static void attn_numeric_case(const Plugin& pl, const char* name, int64_t head_d
fmt("corrupted ref must fail: max_err=%.5g bad=%d", nmax, nbadc));
}
/* The prefill kernel's KV split (prefill_splits, r4d_attn_paged_h256_gqa6.hip): a launch with too
* few query tiles to fill the card walks the KV in segments, each into f32 partials, and finishes in
* the split-KV merge. `seqs` are {q_len, ctx} each, ragged (cu_seqlens, q_len at the band's bound
/* A head-256 prefill geometry the KV split serves: queries per KV head, KV heads (a rank's), page
* size, and the unit's test hooks' prefix (`<hook>_splits`, the law; `<hook>_last_splits`, the
* count the unit's last launch used). The workgroup holds 384 / gqa query positions. */
struct SplitGeo { int64_t gqa, kv_heads, bs; const char* hook; };
static const SplitGeo kSplitG6 = { 6, 2, 16, "r4d_attn_prefill_h256_gqa6" };
/* The prefill kernel's KV split (r4d_attn_prefill_split.h): a launch with too few query tiles to
* fill the card walks the KV in segments, each into f32 partials, and finishes in the split-KV
* merge, at geometry `g`. `seqs` are {q_len, ctx} each, ragged (cu_seqlens, q_len at the band's bound
* 2048 as the engine issues it) when there are two or more. The row runs three times on the same
* operands: unsplit (`splits` 1), at `force` segments, and at the law's own count, which must be
* what the law hook says (and above one when `law_splits`). Both split runs are checked against the
* oracle on sampled rows (every head of each sequence's rows 0, 62, 63, 64, middle and last) at
* oracle on sampled rows (every head of each sequence's rows 0, bq - 2, bq - 1, bq, middle and
* last, bq = 384 / gqa the workgroup's query positions, so both sides of a query tile's edge) at
* attn_numeric_case's tolerance, and against the unsplit run on every element; the largest
* difference is in the case's name. A chunk long against its context (345 of 345 or 1000 keys)
* gives its early query tiles segments past their causal end, which must be written empty.
@@ -1296,23 +1303,24 @@ static void attn_numeric_case(const Plugin& pl, const char* name, int64_t head_d
* launch over part of a step (ArPieces' two pieces, the prefill half of a band-split mixed step):
* cu_seqlens starts at `lead` and q/out are the whole step. Those rows must come out untouched. */
struct SplitSeq { int64_t q, ctx; };
static void attn_split_case(const Plugin& pl, const char* name, const char* kv_dtype,
const std::vector<SplitSeq>& seqs, int force, bool law_splits,
int64_t lead = 0, int64_t tail = 0) {
static void attn_split_case(const Plugin& pl, const SplitGeo& g, const char* name,
const char* kv_dtype, const std::vector<SplitSeq>& seqs, int force,
bool law_splits, int64_t lead = 0, int64_t tail = 0) {
if (!sel(name) && !sel("attn_prefill_split")) return;
const RadKernelInfo* r = find(pl, name);
if (!r) { ok(name, false, "row absent"); return; }
std::string tag = std::string(name) + " split";
std::string tag = std::string(name) + fmt(" kv%lld split", (long long)g.kv_heads);
for (const SplitSeq& s : seqs) tag += fmt(" %lld/%lld", (long long)s.q, (long long)s.ctx);
tag += fmt(" x%d", force);
if (lead || tail) tag += fmt(" rows %lld+..+%lld", (long long)lead, (long long)tail);
using LawFn = int (*)(int, int, int, int, int);
using LastFn = int (*)(void);
const LawFn law = (LawFn)dlsym(pl.h, "r4d_attn_prefill_h256_gqa6_splits");
const LastFn last = (LastFn)dlsym(pl.h, "r4d_attn_prefill_h256_gqa6_last_splits");
const LawFn law = (LawFn)dlsym(pl.h, (std::string(g.hook) + "_splits").c_str());
const LastFn last = (LastFn)dlsym(pl.h, (std::string(g.hook) + "_last_splits").c_str());
if (!law || !last) { ok(tag.c_str(), false, "libr4d lacks the split's test hooks"); return; }
const int64_t head_dim = 256, gqa = 6, kv_heads = 2, bs = 16, q_heads = kv_heads * gqa;
const int64_t head_dim = 256, gqa = g.gqa, kv_heads = g.kv_heads, bs = g.bs;
const int64_t q_heads = kv_heads * gqa, bq = 384 / gqa;
const int64_t ns = (int64_t)seqs.size();
const int kv_is_fp8 = std::strcmp(kv_dtype, "bf16") != 0;
const int kvq = !std::strcmp(kv_dtype, "fp8_v4") ? 1 : !std::strcmp(kv_dtype, "k6_v4") ? 2 : 0;
@@ -1461,7 +1469,7 @@ static void attn_split_case(const Plugin& pl, const char* name, const char* kv_d
int obad = 0, nchk = 0;
for (int64_t s = 0; s < ns; ++s) {
const int64_t q = seqs[(size_t)s].q;
std::vector<int64_t> rows = { 0, 62, 63, 64, q / 2, q - 1 };
std::vector<int64_t> rows = { 0, bq - 2, bq - 1, bq, q / 2, q - 1 };
std::sort(rows.begin(), rows.end());
rows.erase(std::unique(rows.begin(), rows.end()), rows.end());
for (const int64_t i : rows) {
@@ -1490,27 +1498,31 @@ static void attn_split_case(const Plugin& pl, const char* name, const char* kv_d
}
/* A mixed step's attention as the engine issues it (AttnGatedFP8::step's per-band issue) at
* Qwen3.8-27B's per-rank production geometry: `nd` decode sequences of 8-10 query rows (verify
* blocks) beside one prefill chunk of `pq` rows, max_seqs 16, max_ctx 200000, the band bounds 10
* (decode) and 2048. Each band's row gets the scratch its hook sized at declare, from the band's
* geometry `g` (Qwen3.8-27B's per-rank production geometry for kSplitG6): `nd` decode sequences of
* the widest verify blocks the decode row holds (64 / gqa query rows and the two below: 8-10 at
* gqa 6, 3-5 at gqa 12) beside one prefill chunk of `pq` rows, max_seqs 16, max_ctx 200000, the
* band bounds 64 / gqa (decode) and 2048. Each band's row gets the scratch its hook sized at declare, from the band's
* frozen geometry and no tensors, and nothing more, as the core binds it (rad_builder.cpp,
* ctx.cpp): the decode half (prefix rows, its own cu) at the decode band; the prefill half (q and
* out whole, cu and the per-sequence arrays past the decode sequences) at band `pband`, which
* picks the row as the core would (at most 10: the decode row). Both launches must succeed, and
* picks the row as the core would (at most the decode band: the decode row). Both launches must succeed, and
* every sequence's rows match the oracle afterwards: the prefill half must not write the decode
* rows. 1.10-next6u issued the prefill half at its own query length, so a chunk of 10 rows or
* fewer reached the decode row with the whole step's rows and refused for scratch. */
static void attn_mixed_step_case(const Plugin& pl, const char* dec_name, const char* pre_name,
const char* kv_dtype, int nd, int64_t pq, int64_t pband) {
static void attn_mixed_step_case(const Plugin& pl, const SplitGeo& g, const char* dec_name,
const char* pre_name, const char* kv_dtype, int nd, int64_t pq,
int64_t pband) {
if (!sel(pre_name) && !sel("attn_mixed_step")) return;
const RadKernelInfo* rd = find(pl, dec_name);
const RadKernelInfo* rp = find(pl, pre_name);
const std::string tag = fmt("%s mixed step: %d decode rows of 8-10 + a %lld-row chunk, "
"prefill half at band %lld", pre_name, nd, (long long)pq,
const int64_t head_dim = 256, gqa = g.gqa, kv_heads = g.kv_heads, bs = g.bs;
const int64_t q_heads = kv_heads * gqa;
const int64_t dband = 64 / gqa, max_seqs = 16, max_ctx = 200000, top = 2048;
const std::string tag = fmt("%s kv%lld mixed step: %d decode rows of %lld-%lld + a %lld-row "
"chunk, prefill half at band %lld", pre_name, (long long)kv_heads,
nd, (long long)(dband - 2), (long long)dband, (long long)pq,
(long long)pband);
if (!rd || !rp || !rd->scratch) { ok(tag.c_str(), false, "row or scratch hook absent"); return; }
const int64_t head_dim = 256, gqa = 6, kv_heads = 2, bs = 16, q_heads = kv_heads * gqa;
const int64_t dband = 10, max_seqs = 16, max_ctx = 200000, top = 2048;
const int kvq = !std::strcmp(kv_dtype, "fp8_v4") ? 1 : !std::strcmp(kv_dtype, "k6_v4") ? 2 : 0;
const int64_t rowe = kvq == 2 ? 22 * head_dim / 16 : kvq == 1 ? 25 * head_dim / 16 : head_dim * 2;
const int64_t voff = kvq == 2 ? head_dim * 3 / 4 : head_dim;
@@ -1520,7 +1532,7 @@ static void attn_mixed_step_case(const Plugin& pl, const char* dec_name, const c
/* decode-first, as the scheduler sorts a step; contexts differ per sequence */
const int64_t ns = nd + 1;
std::vector<int64_t> ql, cx;
for (int s = 0; s < nd; ++s) { ql.push_back(8 + s % 3); cx.push_back(3000 + 397 * s); }
for (int s = 0; s < nd; ++s) { ql.push_back(dband - 2 + s % 3); cx.push_back(3000 + 397 * s); }
ql.push_back(pq); cx.push_back(pq + 2500);
int64_t mb = 0;
std::vector<int32_t> cu(1, 0), used;
@@ -1615,7 +1627,7 @@ static void attn_mixed_step_case(const Plugin& pl, const char* dec_name, const c
};
int dq = 0;
for (int s = 0; s < nd; ++s) dq = std::max<int>(dq, (int)ql[(size_t)s]);
issue(dq, DT, 0, nd); /* the decode half, at its band (<= 10) */
issue(dq, DT, 0, nd); /* the decode half, at its band (<= 64 / gqa) */
issue(pband, nt, nd, 1); /* the prefill half */
std::vector<uint16_t> oh(on);
@@ -1647,26 +1659,27 @@ static void attn_mixed_step_case(const Plugin& pl, const char* dec_name, const c
dfree_all();
}
/* The split's scratch at declare (the band's bound 2048, Qwen3.8-27B's per-rank 12 heads over 2,
* the model's context, no tensors): the hook must cover the largest launch the law can split in the
* band, over every sequence count up to max_seqs, and stay near the 25.4 MB bound its comment
* derives. Sizes it in the name. */
static void attn_split_scratch_case(const Plugin& pl, const char* name, const char* kv_dtype) {
/* The split's scratch at declare (the band's bound 2048, geometry `g`'s per-rank heads -- 12 over 2
* for Qwen3.8-27B --, the model's context, no tensors): the hook must cover the largest launch the
* law can split in the band, over every sequence count up to max_seqs, and stay within the 25.4 MB
* bound its comment derives for every ratio. Sizes it in the name. */
static void attn_split_scratch_case(const Plugin& pl, const SplitGeo& g, const char* name,
const char* kv_dtype) {
if (!sel(name) && !sel("attn_prefill_split")) return;
const RadKernelInfo* r = find(pl, name);
if (!r) { ok(name, false, "row absent"); return; }
using LawFn = int (*)(int, int, int, int, int);
const LawFn law = (LawFn)dlsym(pl.h, "r4d_attn_prefill_h256_gqa6_splits");
const LawFn law = (LawFn)dlsym(pl.h, (std::string(g.hook) + "_splits").c_str());
if (!r->scratch || !law) {
ok((std::string(name) + ": split scratch at declare").c_str(), false,
!r->scratch ? "the row has no scratch hook" : "libr4d lacks the law's test hook");
return;
}
const int band = 2048, n_head = 12, kv = 2, mctx = 262144;
const int band = 2048, kv = (int)g.kv_heads, n_head = (int)(g.kv_heads * g.gqa), mctx = 262144;
for (const int ms : { 1, 16, 64 }) {
std::vector<RadParam> ps = {
PI("q_len", band), PI("head_dim", 256), PI("gqa", 6), PI("n_head", n_head),
PI("max_seqs", ms), PI("max_ctx", mctx), PI("block_size", 16), PI("causal", 1),
PI("q_len", band), PI("head_dim", 256), PI("gqa", g.gqa), PI("n_head", n_head),
PI("max_seqs", ms), PI("max_ctx", mctx), PI("block_size", g.bs), PI("causal", 1),
PI("window", 0), PS("q_dtype", "bf16"), PS("kv_dtype", kv_dtype),
};
RadArgs a;
@@ -1681,8 +1694,8 @@ static void attn_split_scratch_case(const Plugin& pl, const char* name, const ch
if (t % n == 0) sp = std::max(sp, law(t / n, t, n, kv, mctx));
if (sp > 1) worst = std::max(worst, (int64_t)t * sp * n_head * (256 + 2) * 4);
}
ok(fmt("%s: split scratch at declare, %d sequences: %.2f MB (the law's worst %.2f MB)", name,
ms, need / 1e6, worst / 1e6).c_str(),
ok(fmt("%s kv%d: split scratch at declare, %d sequences: %.2f MB (the law's worst %.2f MB)",
name, kv, ms, need / 1e6, worst / 1e6).c_str(),
worst > 0 && need >= worst && need <= 24576LL * 1032,
fmt("need %lld worst %lld bound %lld", (long long)need, (long long)worst,
24576LL * 1032));
@@ -26659,49 +26672,87 @@ static void device_phase(const Plugin& pl) {
attn_numeric_case(pl, "attn_prefill_h256_gqa6_k6v4", 256, 6, 100, "k6_v4", 0, 1024);
attn_numeric_case(pl, "attn_decode_h256_gqa6_k6v4", 256, 6, 1, "k6_v4", 0, 1024);
attn_numeric_case(pl, "attn_decode_h256_gqa6_k6v4", 256, 6, 4, "k6_v4", 0, 2048);
/* The prefill KV split at Qwen3.8-27B's per-rank geometry, every cache layout: the tails the
* audits measured (11 to 345 rows over 8k to 20k keys, where the law splits), one row through
* the prefill kernel, chunks long against their context (early query tiles own empty
* segments), and ragged batches of a chunk beside decode rows of other contexts. Then the
* declare-time scratch. */
static const struct { const char* row; const char* kv; } split_rows[] = {
{ "attn_prefill_h256_gqa6_k6v4", "k6_v4" }, { "attn_prefill_h256_gqa6_fp8v4", "fp8_v4" },
{ "attn_prefill_h256_gqa6_fp8kv", "fp8_e4m3" }, { "attn_prefill_h256_gqa6_bf16kv", "bf16" },
/* The prefill KV split on every head-256 prefill row, each at a rank's production geometry
* and every cache layout it has: Qwen3.8-27B (gqa 6, 2 KV heads at TP2), Flash-Next's QSA
* layers (gqa 12 over 4-token pages, 1 KV head at TP2; gqa 6 over 4-token pages, 1 at TP4),
* Qwen3.6-35B-A3B (gqa 8, 1 at TP2), Qwen3.5-0.8B (gqa 4, 2 at TP1) and the gqa 2 rows'
* full-attention form (4 KV heads). The tails the audits measured (11 to 345 rows over
* 8k to 20k keys, where the law splits), one row through the prefill kernel, chunks long
* against their context (early query tiles own empty segments), and ragged batches of a chunk
* beside decode rows of other contexts. Then the declare-time scratch. */
static const SplitGeo kSplitG12 = { 12, 1, 4, "r4d_attn_prefill_h256_gqa12_bs4" };
static const SplitGeo kSplitG6Bs4 = { 6, 1, 4, "r4d_attn_prefill_h256_gqa6_bs4" };
static const SplitGeo kSplitG8 = { 8, 1, 16, "r4d_attn_prefill_h256_gqa8_bs16" };
static const SplitGeo kSplitG4 = { 4, 2, 16, "r4d_attn_prefill_h256_gqa4_bs16" };
static const SplitGeo kSplitG2 = { 2, 4, 16, "r4d_attn_prefill_h256_gqa2" };
/* The mixed step at gqa 2 with 2 KV heads: 15 decoders of 32-row blocks at 4 heads make a
* 280 MB cache, and beside a production server the card has less than that free (the launch
* then fails for its scratch, split or not). */
static const SplitGeo kSplitG2Kv2 = { 2, 2, 16, "r4d_attn_prefill_h256_gqa2" };
static const struct { const SplitGeo* g; const char* row; const char* kv; } split_rows[] = {
{ &kSplitG6, "attn_prefill_h256_gqa6_k6v4", "k6_v4" },
{ &kSplitG6, "attn_prefill_h256_gqa6_fp8v4", "fp8_v4" },
{ &kSplitG6, "attn_prefill_h256_gqa6_fp8kv", "fp8_e4m3" },
{ &kSplitG6, "attn_prefill_h256_gqa6_bf16kv", "bf16" },
{ &kSplitG12, "attn_prefill_h256_gqa12_k6v4", "k6_v4" },
{ &kSplitG12, "attn_prefill_h256_gqa12_fp8v4", "fp8_v4" },
{ &kSplitG12, "attn_prefill_h256_gqa12_fp8kv", "fp8_e4m3" },
{ &kSplitG12, "attn_prefill_h256_gqa12_bf16kv", "bf16" },
{ &kSplitG6Bs4, "attn_prefill_h256_gqa6_bs4_k6v4", "k6_v4" },
{ &kSplitG6Bs4, "attn_prefill_h256_gqa6_bs4_fp8v4", "fp8_v4" },
{ &kSplitG6Bs4, "attn_prefill_h256_gqa6_bs4_fp8kv", "fp8_e4m3" },
{ &kSplitG6Bs4, "attn_prefill_h256_gqa6_bs4_bf16kv", "bf16" },
{ &kSplitG8, "attn_prefill_h256_gqa8_fp8kv", "fp8_e4m3" },
{ &kSplitG8, "attn_prefill_h256_gqa8_bf16kv", "bf16" },
{ &kSplitG4, "attn_prefill_h256_gqa4_fp8kv", "fp8_e4m3" },
{ &kSplitG4, "attn_prefill_h256_gqa4_bf16kv", "bf16" },
{ &kSplitG2, "attn_prefill_h256_gqa2_fp8kv", "fp8_e4m3" },
{ &kSplitG2, "attn_prefill_h256_gqa2_bf16kv", "bf16" },
};
for (const auto& sr : split_rows) {
const SplitGeo& g = *sr.g;
const char* n = sr.row;
const char* kv = sr.kv;
attn_split_case(pl, n, kv, { { 1, 20000 } }, 16, true);
attn_split_case(pl, n, kv, { { 11, 8192 } }, 7, true);
attn_split_case(pl, n, kv, { { 53, 18000 } }, 16, true);
attn_split_case(pl, n, kv, { { 64, 12000 } }, 13, true);
attn_split_case(pl, n, kv, { { 65, 9000 } }, 8, true);
attn_split_case(pl, n, kv, { { 345, 16000 } }, 16, true);
attn_split_case(pl, n, kv, { { 345, 345 } }, 16, false);
attn_split_case(pl, n, kv, { { 345, 1000 } }, 16, true);
attn_split_case(pl, n, kv, { { 640, 2000 } }, 5, true);
attn_split_case(pl, n, kv, { { 53, 12000 }, { 1, 20000 } }, 16, true);
attn_split_case(pl, n, kv, { { 11, 8192 }, { 64, 9000 }, { 1, 12000 } }, 6, true);
attn_split_case(pl, n, kv, { { 300, 600 }, { 1, 10000 } }, 16, true);
attn_split_case(pl, g, n, kv, { { 1, 20000 } }, 16, true);
attn_split_case(pl, g, n, kv, { { 11, 8192 } }, 7, true);
attn_split_case(pl, g, n, kv, { { 53, 18000 } }, 16, true);
attn_split_case(pl, g, n, kv, { { 64, 12000 } }, 13, true);
attn_split_case(pl, g, n, kv, { { 65, 9000 } }, 8, true);
attn_split_case(pl, g, n, kv, { { 345, 16000 } }, 16, true);
attn_split_case(pl, g, n, kv, { { 345, 345 } }, 16, false);
attn_split_case(pl, g, n, kv, { { 345, 1000 } }, 16, true);
attn_split_case(pl, g, n, kv, { { 640, 2000 } }, 5, true);
attn_split_case(pl, g, n, kv, { { 53, 12000 }, { 1, 20000 } }, 16, true);
attn_split_case(pl, g, n, kv, { { 11, 8192 }, { 64, 9000 }, { 1, 12000 } }, 6, true);
attn_split_case(pl, g, n, kv, { { 300, 600 }, { 1, 10000 } }, 16, true);
/* Launches over part of a step: ArPieces' first piece (rows after it) and second (rows
* before it), and a band-split mixed step's prefill half behind its decode rows. The law
* sees the step's rows (total_q), so it leaves the 512-row pieces unsplit. */
attn_split_case(pl, n, kv, { { 512, 12000 } }, 4, false, 0, 512);
attn_split_case(pl, n, kv, { { 512, 12512 } }, 4, false, 512, 0);
attn_split_case(pl, n, kv, { { 53, 12000 } }, 16, true, 8, 0);
attn_split_case(pl, n, kv, { { 64, 9000 }, { 11, 8192 } }, 6, true, 3, 0);
attn_split_scratch_case(pl, n, kv);
attn_split_case(pl, g, n, kv, { { 512, 12000 } }, 4, false, 0, 512);
attn_split_case(pl, g, n, kv, { { 512, 12512 } }, 4, false, 512, 0);
attn_split_case(pl, g, n, kv, { { 53, 12000 } }, 16, true, 8, 0);
attn_split_case(pl, g, n, kv, { { 64, 9000 }, { 11, 8192 } }, 6, true, 3, 0);
attn_split_scratch_case(pl, g, n, kv);
}
/* A mixed step's two halves at the production geometry, each with only its band's declared
* scratch: up to 15 verify blocks beside chunks of 1 to 640 rows. */
for (const auto& sr : { std::make_pair("attn_prefill_h256_gqa6_k6v4", "k6_v4"),
std::make_pair("attn_prefill_h256_gqa6_fp8v4", "fp8_v4") }) {
const std::string dec = std::string("attn_decode_h256_gqa6_") + (sr.second[0] == 'k' ? "k6v4" : "fp8v4");
/* A mixed step's two halves at each geometry's production shape, each with only its band's
* declared scratch: up to 15 verify blocks beside chunks of 1 to 640 rows. */
static const struct { const SplitGeo* g; const char* pre; const char* dec; const char* kv; }
mixed_rows[] = {
{ &kSplitG6, "attn_prefill_h256_gqa6_k6v4", "attn_decode_h256_gqa6_k6v4", "k6_v4" },
{ &kSplitG6, "attn_prefill_h256_gqa6_fp8v4", "attn_decode_h256_gqa6_fp8v4", "fp8_v4" },
{ &kSplitG12, "attn_prefill_h256_gqa12_k6v4", "attn_decode_h256_gqa12_k6v4", "k6_v4" },
{ &kSplitG12, "attn_prefill_h256_gqa12_fp8v4", "attn_decode_h256_gqa12_fp8v4", "fp8_v4" },
{ &kSplitG12, "attn_prefill_h256_gqa12_fp8kv", "attn_decode_h256_gqa12_fp8kv", "fp8_e4m3" },
{ &kSplitG6Bs4, "attn_prefill_h256_gqa6_bs4_k6v4", "attn_decode_h256_gqa6_bs4_k6v4", "k6_v4" },
{ &kSplitG8, "attn_prefill_h256_gqa8_fp8kv", "attn_decode_h256_gqa8_fp8kv", "fp8_e4m3" },
{ &kSplitG4, "attn_prefill_h256_gqa4_fp8kv", "attn_decode_h256_gqa4_fp8kv", "fp8_e4m3" },
{ &kSplitG2Kv2, "attn_prefill_h256_gqa2_fp8kv", "attn_decode_h256_gqa2_fp8kv", "fp8_e4m3" },
};
for (const auto& mr : mixed_rows)
for (const auto& c : { std::make_pair(15, 5), std::make_pair(15, 10), std::make_pair(3, 1),
std::make_pair(8, 53), std::make_pair(1, 300),
std::make_pair(15, 640) })
attn_mixed_step_case(pl, dec.c_str(), sr.first, sr.second, c.first, c.second, 2048);
}
attn_mixed_step_case(pl, *mr.g, mr.dec, mr.pre, mr.kv, c.first, c.second, 2048);
/* Flash-Next's QSA layers: twelve queries per KV head over 4-token pages, and six at
* four ranks, each layout's prefill (a long history and a ragged last tile), decode, and the
* widest verify (q_len 5 at gqa 12, 10 at gqa 6). */