Archived
The prefill attention kernel gives each workgroup 64 query rows of one KV head and walks the whole causal context serially. A launch with few query rows therefore has few workgroups: the tail chunk of a prompt (53 rows) or a follow-up turn of a few hundred tokens runs 2-10 workgroups on 32 WGPs, each walking up to 188k keys (17.7 ms per layer at 188k). When kv_heads * qtiles * seqs is below the WGP count, the launch now splits the KV range into S segments (blockIdx.x = qtile * S + segment), writes unnormalised f32 partials (O, m, l) in the decode partial layout and merges them with the existing split-KV combine. S is the smaller of one or two rounds of workgroups, capped by the context's tiles and the declared scratch. A segment past a workgroup's causal end still writes an empty partial; the merge reads every segment it counts. The merges take a SPAN parameter so a launch over part of a step (pipelined pieces) only rewrites its own rows; the decode instances are unchanged (identical ISA). R4D_PREFILL_SPLITS: unset/0 = the law, 1 = off, N = force N (debug/tuning). Measured on 2x Radeon AI PRO R9700, Qwen3.8-27B MXFP4, TP 2, k6_v4 KV: - r4d_selftest --perfpaged, off -> on: 53 rows over 40k 3.71 -> 0.47 ms, 80 rows 3.64 -> 0.68 ms, 345 rows 3.80 -> 1.79 ms, 1024 rows 4.29 -> 4.29 ms (no split). - Engine, 188k prompt, time to first token: 62.78 -> 62.54 s (the tail chunk). - KLD vs the bf16 reference: chunk 64 0.040384 -> 0.040337, chunk 2048 0.040237 -> 0.040220; top-1 agreement 93.985 -> 93.998 %. Split vs unsplit outputs differ by at most 1 bf16 ulp. - With R4D_PREFILL_SPLITS=1 the engine is bit-identical to before (top-5 logprobs, 100 positions). Tests: attn_split_case / attn_split_scratch_case for k6_v4, fp8_v4, fp8 and bf16 KV (q 1-345 over 8k-20k keys, empty segments, ragged batches, pipelined pieces), scratch poisoned; 410/410 ok. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
662 lines
33 KiB
Plaintext
662 lines
33 KiB
Plaintext
// R4D paged attention dispatch for head_dim 256, 6 queries per KV head, paged block 16. The
|
|
// KV-dtype variants are template instantiations of one device code. The geometry is a hard gate:
|
|
// anything else is rejected.
|
|
//
|
|
// The query is bf16 in every exact variant. A single-term 8-bit query is ~10x less accurate
|
|
// against an fp32 reference (2.58e-02 vs ~2.3e-03 relative), and the two-term e4m3 form that
|
|
// reaches f16 accuracy is 4-10% slower end to end. Two families ship, differing in KV dtype: bf16
|
|
// and 8-bit (fp8, the production configuration, and the fp8_v4 and k6_v4 caches). Over the 8-bit
|
|
// caches the prefill also has lossy rows that run Q.K^T, P.V or both as e4m3 WMMA (O_QK8, O_PV8),
|
|
// taken only when the caller's `prefill_fp8` asks for them.
|
|
//
|
|
// `opt` is a bitmask of optimisation steps:
|
|
// prefill 3954187 = f16 operands | mask skip | folded query scale | contiguous-k fragments
|
|
// | prefetch 2 + scheduling barrier | dot2 row sum | block table in SGPRs
|
|
// | wide K fetch | V prefetch across the m-tile loop | LDS-scoped barrier
|
|
// decode 3429947 = the same minus the wide K fetch (its K never reaches LDS), plus K read
|
|
// straight from the paged cache (bit 16) and f16 split-KV partials (32).
|
|
// Each bit is defined where it is used (r4d_attn_prefill_h256_gqa6.hip,
|
|
// r4d_attn_decode_h256_gqa6.hip) and can be turned off on its own after a geometry change.
|
|
#include "r4d.h"
|
|
#include "r4d_attn_args.h"
|
|
#include "r4d_common.h"
|
|
#include <cmath>
|
|
#include <algorithm>
|
|
using std::max;
|
|
#include "r4d_attn_prefill_h256_gqa6.hip"
|
|
#include "r4d_attn_decode_h256_gqa6.hip"
|
|
|
|
#define CHK() do { hipError_t e_ = hipGetLastError(); if (e_ != hipSuccess) return -100 - (int)e_; } while (0)
|
|
|
|
// The geometry of every kernel in this unit. BLOCK_SIZE is not in the entry-point names because
|
|
// it is 16 for all of them.
|
|
#define A_HEAD_DIM 256
|
|
#define A_GQA 6
|
|
#define A_BLOCK_SIZE 16
|
|
#define A_MAX_DECODE_ROWS 64 // q_len * gqa a single decode workgroup can hold
|
|
|
|
extern "C" void r4d_attn_dims(int* head_dim, int* gqa, int* block_size, int* max_decode_rows) {
|
|
if (head_dim) *head_dim = A_HEAD_DIM;
|
|
if (gqa) *gqa = A_GQA;
|
|
if (block_size) *block_size = A_BLOCK_SIZE;
|
|
if (max_decode_rows) *max_decode_rows = A_MAX_DECODE_ROWS;
|
|
}
|
|
|
|
// ---- prefill ---------------------------------------------------------------------------------
|
|
// LDS at head_dim 256, 16-bit K and V: sK = TILE*264*2, sV = 256*(TILE+8)*2. TILE 64 would need
|
|
// 68096 B > 65536, so TILE is 48.
|
|
#define P_NWARPS 24
|
|
#define P_TILE 48
|
|
// K prefetch 2, not 4: four fragments in flight make the m-tile loop spill at six waves per SIMD
|
|
// (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 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.
|
|
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());
|
|
}
|
|
// 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;
|
|
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);
|
|
|
|
// KVP: 0 = an 8-bit cache (XOPT names fp8_v4 or k6_v4 and the e4m3 legs), 1 = bf16 cache.
|
|
// a->splits > 1 with a->scratch set (the plugin entries below, prefill_splits) is the split form:
|
|
// segments in the scratch, then the merge. Anything else is the plain form, unchanged.
|
|
template <int KVP, int XOPT = 0>
|
|
static int prefill_launch(const R4DArgs* a, hipStream_t s) {
|
|
if (a->head_dim != A_HEAD_DIM || a->block_size != A_BLOCK_SIZE) return -1;
|
|
if (a->q_heads / a->kv_heads != A_GQA) return -2;
|
|
constexpr int BQ = (P_NWARPS * 16) / A_GQA;
|
|
// The split form serves the f16 operand legs; the lossy e4m3 legs (O_QK8, O_PV8 rows) stay unsplit
|
|
// rather than double their instances.
|
|
constexpr bool CAN_SPLIT = !(XOPT & (O_QK8 | O_PV8));
|
|
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<P_NWARPS, P_TILE, A_HEAD_DIM, A_GQA, A_BLOCK_SIZE,
|
|
KVP, P_OPT | XOPT | O_PSPLIT>),
|
|
grid, dim3(P_NWARPS * 32), 0, s, *a);
|
|
CHK();
|
|
return prefill_combine(a, splits, s);
|
|
}
|
|
}
|
|
hipLaunchKernelGGL((r4d_attn_prefill_kernel<P_NWARPS, P_TILE, A_HEAD_DIM, A_GQA, A_BLOCK_SIZE,
|
|
KVP, P_OPT | XOPT>),
|
|
grid, dim3(P_NWARPS * 32), 0, s, *a);
|
|
CHK();
|
|
return 0;
|
|
}
|
|
|
|
extern "C" int r4d_attn_prefill_h256_gqa6_fp8kv(const R4DArgs* a, hipStream_t s) {
|
|
return prefill_launch<0>(a, s);
|
|
}
|
|
extern "C" int r4d_attn_prefill_h256_gqa6_bf16kv(const R4DArgs* a, hipStream_t s) {
|
|
return prefill_launch<1>(a, s);
|
|
}
|
|
extern "C" int r4d_attn_prefill_h256_gqa6_fp8v4kv(const R4DArgs* a, hipStream_t s) {
|
|
return prefill_launch<0, O_V4>(a, s);
|
|
}
|
|
extern "C" int r4d_attn_prefill_h256_gqa6_k6v4kv(const R4DArgs* a, hipStream_t s) {
|
|
return prefill_launch<0, O_V4 | O_K6>(a, s);
|
|
}
|
|
// The e4m3 product legs over an 8-bit cache (XOPT 0 fp8, O_V4 fp8_v4, O_V4 | O_K6 k6_v4). `legs`
|
|
// is 1 Q.K^T, 2 P.V, 3 both. Lossy, so reached only through the rows a caller's attn_paged
|
|
// `prefill_fp8` selects.
|
|
template <int XOPT>
|
|
static int prefill_legs_launch(const R4DArgs* a, hipStream_t s, int legs) {
|
|
if (legs == 1) return prefill_launch<0, XOPT | O_QK8>(a, s);
|
|
if (legs == 2) return prefill_launch<0, XOPT | O_PV8>(a, s);
|
|
return prefill_launch<0, XOPT | O_QK8 | O_PV8>(a, s);
|
|
}
|
|
|
|
// ---- decode ----------------------------------------------------------------------------------
|
|
#define D_TILE 16
|
|
// Decode prefetches two K fragments per WMMA group, not four. Four costs 8 more VGPRs at the peak
|
|
// of the QK loop and spills 18, reloading part of the query from scratch on every tile. Two fits in
|
|
// 255 VGPRs and is 1.7-7.5% faster, with identical output.
|
|
#define D_OPT 3429947
|
|
// Output rows (waves) per block in the vectorised merge. 1, 2 and 4 are within 1%; 2 is best on
|
|
// average.
|
|
#define D_JPB 2
|
|
// Minimum output rows for the vectorised merge. It gives a row 32 lanes instead of 256, trading
|
|
// parallelism for 8x fewer loads, which pays only when there are enough rows to fill the GPU. At
|
|
// one request (96 rows, most segments) it loses up to 23%; the crossover is ~192 rows and above
|
|
// 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.
|
|
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;
|
|
}
|
|
|
|
// Split law: r4d_decode_splits (r4d_attn_decode_h256_gqa6.hip).
|
|
extern "C" long r4d_attn_decode_h256_gqa6_scratch_bytes(const R4DArgs* a) {
|
|
return r4d_decode_scratch(a, r4d_decode_splits(a, D_TILE), D_OPT);
|
|
}
|
|
|
|
template <int KVP, int XOPT = 0>
|
|
static int decode_launch(const R4DArgs* a, hipStream_t s) {
|
|
if (a->head_dim != A_HEAD_DIM || a->block_size != A_BLOCK_SIZE) return -1;
|
|
if (a->q_heads / a->kv_heads != A_GQA) return -2;
|
|
const int rows = a->q_len * A_GQA;
|
|
if (rows > A_MAX_DECODE_ROWS) return -4; // verify width <= 10 at gqa 6
|
|
const int nw = (rows + 15) / 16; // gqa-aligned height; no dead m-tiles issued
|
|
const int splits = r4d_decode_splits(a, D_TILE);
|
|
|
|
#define D_LAUNCH(NW) \
|
|
hipLaunchKernelGGL((r4d_attn_decode_kernel<NW, D_TILE, A_HEAD_DIM, A_GQA, A_BLOCK_SIZE, \
|
|
KVP, D_OPT | XOPT>), \
|
|
dim3(splits, a->kv_heads, a->num_seqs), dim3(NW * 32), 0, s, *a, splits)
|
|
if (nw == 1) D_LAUNCH(1);
|
|
else if (nw == 2) D_LAUNCH(2);
|
|
else if (nw == 3) D_LAUNCH(3);
|
|
else D_LAUNCH(4);
|
|
#undef D_LAUNCH
|
|
CHK();
|
|
|
|
// The vectorised merge's wave sum reproduces the general LDS tree only while splits <= 64.
|
|
// Above that (only when kv_heads * num_seqs is 1, giving 128 splits) use the general form.
|
|
// Both produce identical bytes; the choice is speed only.
|
|
const int jobs3 = r4d_q_total(*a) * a->q_heads;
|
|
if (splits <= 64 && jobs3 >= D_VECMIN) {
|
|
const int blocks = (jobs3 + D_JPB - 1) / D_JPB;
|
|
if (D_OPT & 32)
|
|
hipLaunchKernelGGL((r4d_attn_splitkv_combine_vec_kernel<A_HEAD_DIM, 4, 1, D_JPB>),
|
|
dim3(blocks), dim3(32 * D_JPB), 0, s, *a, splits, D_TILE);
|
|
else
|
|
hipLaunchKernelGGL((r4d_attn_splitkv_combine_vec_kernel<A_HEAD_DIM, 4, 0, D_JPB>),
|
|
dim3(blocks), dim3(32 * D_JPB), 0, s, *a, splits, D_TILE);
|
|
} else if (D_OPT & 32)
|
|
hipLaunchKernelGGL((r4d_attn_splitkv_combine_kernel<A_HEAD_DIM, 4, 1>), dim3(jobs3),
|
|
dim3(256), splits * sizeof(float), s, *a, splits, D_TILE);
|
|
else
|
|
hipLaunchKernelGGL((r4d_attn_splitkv_combine_kernel<A_HEAD_DIM, 4, 0>), dim3(jobs3),
|
|
dim3(256), splits * sizeof(float), s, *a, splits, D_TILE);
|
|
CHK();
|
|
return 0;
|
|
}
|
|
|
|
extern "C" int r4d_attn_decode_h256_gqa6_fp8kv(const R4DArgs* a, hipStream_t s) {
|
|
return decode_launch<0>(a, s);
|
|
}
|
|
extern "C" int r4d_attn_decode_h256_gqa6_bf16kv(const R4DArgs* a, hipStream_t s) {
|
|
return decode_launch<1>(a, s);
|
|
}
|
|
extern "C" int r4d_attn_decode_h256_gqa6_fp8v4kv(const R4DArgs* a, hipStream_t s) {
|
|
return decode_launch<0, O_V4>(a, s);
|
|
}
|
|
extern "C" int r4d_attn_decode_h256_gqa6_k6v4kv(const R4DArgs* a, hipStream_t s) {
|
|
return decode_launch<0, O_V4 | O_K6>(a, s);
|
|
}
|
|
|
|
// ============================================================== the plugin ABI entry points
|
|
//
|
|
// The operand reader is r4d_attn_args.h, shared by every paged geometry.
|
|
//
|
|
// Note: entry points belong in this translation unit, not in the r4d_attn_prefill_*.hip or
|
|
// r4d_attn_decode_*.hip files it includes. An entry point added there does not get compiled, and
|
|
// 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. */
|
|
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);
|
|
}
|
|
|
|
/* 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. */
|
|
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;
|
|
}
|
|
|
|
/* 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);
|
|
}
|
|
extern "C" int64_t r4d_rad_attn_prefill_h256_v4_scratch(const RadArgs* a) {
|
|
return prefill_scratch(a, 400);
|
|
}
|
|
extern "C" int64_t r4d_rad_attn_prefill_h256_k6_scratch(const RadArgs* a) {
|
|
return prefill_scratch(a, 352);
|
|
}
|
|
|
|
extern "C" int r4d_rad_attn_prefill_h256_fp8(const RadArgs* a, RadStream s) {
|
|
if (check_dims_once() != RAD_OK) return R4D_NO(RAD_E_SHAPE);
|
|
R4DArgs r;
|
|
int rc = fill_paged(a, &r, 256);
|
|
if (rc != RAD_OK) return rc;
|
|
r.splits = prefill_splits(a, &r);
|
|
/* Absent descales mean 1.0, not an error: the kernels read them that way, vLLM defaults both
|
|
* scales to 1.0, and kv_store_fp8 at its default scale needs no descale plane. The operands
|
|
* are optional in the schema and the kernel, so they are optional here too. A calibrated pair
|
|
* is used when passed. */
|
|
return r4d_attn_prefill_h256_gqa6_fp8kv(&r, (hipStream_t)s);
|
|
}
|
|
|
|
/* The 8-bit caches with e4m3 products. `legs` is the row's `prefill_fp8`, which its constraint has
|
|
* matched: 1 Q.K^T, 2 P.V, 3 both. `row` is a token's bytes in a (block, head) page: 512 for fp8,
|
|
* 400 for fp8_v4, 352 for k6_v4. */
|
|
template <int XOPT>
|
|
static int prefill_h256_legs(const RadArgs* a, RadStream s, long long row, int legs) {
|
|
if (check_dims_once() != RAD_OK) return R4D_NO(RAD_E_SHAPE);
|
|
R4DArgs r;
|
|
int rc = fill_paged_row(a, &r, 256, row);
|
|
if (rc != RAD_OK) return rc;
|
|
return prefill_legs_launch<XOPT>(&r, (hipStream_t)s, legs);
|
|
}
|
|
extern "C" int r4d_rad_attn_prefill_h256_fp8_qk8(const RadArgs* a, RadStream s) {
|
|
return prefill_h256_legs<0>(a, s, 512, 1);
|
|
}
|
|
extern "C" int r4d_rad_attn_prefill_h256_fp8_pv8(const RadArgs* a, RadStream s) {
|
|
return prefill_h256_legs<0>(a, s, 512, 2);
|
|
}
|
|
extern "C" int r4d_rad_attn_prefill_h256_fp8_qk8pv8(const RadArgs* a, RadStream s) {
|
|
return prefill_h256_legs<0>(a, s, 512, 3);
|
|
}
|
|
extern "C" int r4d_rad_attn_prefill_h256_fp8v4_qk8(const RadArgs* a, RadStream s) {
|
|
return prefill_h256_legs<O_V4>(a, s, 400, 1);
|
|
}
|
|
extern "C" int r4d_rad_attn_prefill_h256_fp8v4_pv8(const RadArgs* a, RadStream s) {
|
|
return prefill_h256_legs<O_V4>(a, s, 400, 2);
|
|
}
|
|
extern "C" int r4d_rad_attn_prefill_h256_fp8v4_qk8pv8(const RadArgs* a, RadStream s) {
|
|
return prefill_h256_legs<O_V4>(a, s, 400, 3);
|
|
}
|
|
extern "C" int r4d_rad_attn_prefill_h256_k6v4_qk8(const RadArgs* a, RadStream s) {
|
|
return prefill_h256_legs<O_V4 | O_K6>(a, s, 352, 1);
|
|
}
|
|
extern "C" int r4d_rad_attn_prefill_h256_k6v4_pv8(const RadArgs* a, RadStream s) {
|
|
return prefill_h256_legs<O_V4 | O_K6>(a, s, 352, 2);
|
|
}
|
|
extern "C" int r4d_rad_attn_prefill_h256_k6v4_qk8pv8(const RadArgs* a, RadStream s) {
|
|
return prefill_h256_legs<O_V4 | O_K6>(a, s, 352, 3);
|
|
}
|
|
|
|
extern "C" int r4d_rad_attn_prefill_h256_bf16(const RadArgs* a, RadStream s) {
|
|
if (check_dims_once() != RAD_OK) return R4D_NO(RAD_E_SHAPE);
|
|
R4DArgs r;
|
|
int rc = fill_paged(a, &r, 256);
|
|
if (rc != RAD_OK) return rc;
|
|
r.splits = prefill_splits(a, &r);
|
|
return r4d_attn_prefill_h256_gqa6_bf16kv(&r, (hipStream_t)s);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ paged decode */
|
|
|
|
/* Split-KV partial buffer: written by the decode kernel, read by the combine, nothing else. Arena
|
|
* scratch sized at declare from the band's upper bound (RadScratchFn).
|
|
*
|
|
* Declare passes parameters and no tensors (the batch is not known yet), so parameters are the
|
|
* primary source and tensors the fallback. Reading tensors first would refuse every attn_paged
|
|
* row with "invalid argument". q_len is the band's upper bound, since the region is sized once
|
|
* for the worst step.
|
|
*
|
|
* The size is the maximum over every sequence count up to max_seqs: S * splits(S) is not monotone
|
|
* in S (at kv_heads 2 the h256 law gives 60 at twelve sequences and 63 at nine, and the h128 law's
|
|
* step-up doubles splits at one count only). The laws are closed forms, so the sweep is cheap. */
|
|
// Per-geometry byte counts over the shared partial layout, so one sweep serves all.
|
|
// which: 0 = h256/gqa6, 1 = h128/gqa4, 2 = h256/gqa2 (Gemma 4 windowed).
|
|
static int64_t decode_scratch_bytes_of(const R4DArgs* r, int which) {
|
|
if (which == 1) return (int64_t)r4d_attn_decode_h128_gqa4_scratch_bytes(r);
|
|
if (which == 2) return (int64_t)r4d_attn_decode_h256_gqa2_scratch_bytes(r);
|
|
if (which == 3) return (int64_t)r4d_attn_decode_h128_gqa2w_scratch_bytes(r);
|
|
return (int64_t)r4d_attn_decode_h256_gqa6_scratch_bytes(r);
|
|
}
|
|
|
|
/* `row`: a token's bytes in a (block, head) page (fill_paged_row), 0 for 2 * head_dim; `rep`: the
|
|
* launch maps each kv head onto `rep` query groups (R4DArgs::kv_rep), so the split law counts
|
|
* groups, gqa / rep of them a kv head; `q_pitched`: q may be a slice (attn_paged_qrow). */
|
|
static int64_t decode_scratch(const RadArgs* a, int head_dim, int which, long long row = 0,
|
|
int rep = 1, bool q_pitched = false) {
|
|
const int sp = split_of(a);
|
|
if (sp < 0) return R4D_NO(RAD_E_INVAL);
|
|
|
|
R4DArgs r;
|
|
const long long n_head = r4d_geti_or(a, "n_head", 0);
|
|
const long long max_seqs = r4d_geti_or(a, "max_seqs", 0);
|
|
if (n_head > 0 && max_seqs > 0) {
|
|
std::memset(&r, 0, sizeof(r));
|
|
r.head_dim = head_dim;
|
|
r.q_heads = (int)n_head;
|
|
const long long gqa = r4d_geti_or(a, "gqa", 1);
|
|
r.kv_heads = (int)(gqa > 0 ? n_head / gqa : n_head) * rep;
|
|
r.kv_rep = rep;
|
|
r.num_seqs = (int)max_seqs;
|
|
r.q_len = (int)r4d_geti_or(a, "q_len", 1);
|
|
r.max_ctx = (int)r4d_geti_or(a, "max_ctx", 0);
|
|
r.block_size = (int)r4d_geti_or(a, "block_size", 16);
|
|
r.splits = sp;
|
|
/* max_ctx feeds only the split law. Zero would pick one split and undersize the buffer
|
|
* for a launch that later picks more, so refuse. */
|
|
if (r.max_ctx <= 0) return R4D_NO(RAD_E_INVAL);
|
|
int64_t worst = 0;
|
|
for (long long n = 1; n <= max_seqs; ++n) {
|
|
r.num_seqs = (int)n;
|
|
const int64_t need = decode_scratch_bytes_of(&r, which);
|
|
if (need > worst) worst = need;
|
|
}
|
|
return worst;
|
|
}
|
|
|
|
int rc = fill_paged_row(a, &r, head_dim, row > 0 ? row : 2LL * head_dim, q_pitched);
|
|
if (rc != RAD_OK) return rc;
|
|
r.kv_heads *= rep;
|
|
r.kv_rep = rep;
|
|
r.splits = sp;
|
|
return decode_scratch_bytes_of(&r, which);
|
|
}
|
|
|
|
extern "C" int64_t r4d_rad_attn_decode_h256_scratch(const RadArgs* a) {
|
|
return decode_scratch(a, 256, 0);
|
|
}
|
|
|
|
extern "C" int64_t r4d_rad_attn_decode_h128_scratch(const RadArgs* a) {
|
|
return decode_scratch(a, 128, 1);
|
|
}
|
|
|
|
static int decode_common(const RadArgs* a, R4DArgs* r, int head_dim, int which, long long row = 0,
|
|
int rep = 1, bool q_pitched = false) {
|
|
const int sp = split_of(a);
|
|
if (sp < 0) return R4D_NO(RAD_E_INVAL);
|
|
int rc = fill_paged_row(a, r, head_dim, row > 0 ? row : 2LL * head_dim, q_pitched);
|
|
if (rc != RAD_OK) return rc;
|
|
/* a QLD instance reads q_ld whatever q is: dense is the pitch q_heads * head_dim */
|
|
if (q_pitched && r->q_ld == 0) r->q_ld = (long)r->q_heads * head_dim;
|
|
/* each cache head serves `rep` query groups: the kernel's grid and its gqa check count groups */
|
|
r->kv_heads *= rep;
|
|
r->kv_rep = rep;
|
|
r->splits = sp;
|
|
const int64_t need = decode_scratch_bytes_of(r, which);
|
|
if (!a->scratch || a->scratch_bytes < need) return R4D_NO(RAD_E_SCRATCH);
|
|
return RAD_OK;
|
|
}
|
|
|
|
extern "C" int r4d_rad_attn_decode_h256_fp8(const RadArgs* a, RadStream s) {
|
|
if (check_dims_once() != RAD_OK) return R4D_NO(RAD_E_SHAPE);
|
|
R4DArgs r;
|
|
int rc = decode_common(a, &r, 256, 0);
|
|
if (rc != RAD_OK) return rc;
|
|
/* Absent descales mean 1.0; see the prefill twin above. */
|
|
return r4d_attn_decode_h256_gqa6_fp8kv(&r, (hipStream_t)s);
|
|
}
|
|
|
|
/* The fp8_v4 cache: a token's row is 25 * 256 / 16 = 400 bytes (E4M3 K, int4 V, f16 scales). */
|
|
extern "C" int r4d_rad_attn_prefill_h256_fp8v4(const RadArgs* a, RadStream s) {
|
|
if (check_dims_once() != RAD_OK) return R4D_NO(RAD_E_SHAPE);
|
|
R4DArgs r;
|
|
int rc = fill_paged_row(a, &r, 256, 400);
|
|
if (rc != RAD_OK) return rc;
|
|
r.splits = prefill_splits(a, &r);
|
|
return r4d_attn_prefill_h256_gqa6_fp8v4kv(&r, (hipStream_t)s);
|
|
}
|
|
|
|
extern "C" int64_t r4d_rad_attn_decode_h256_v4_scratch(const RadArgs* a) {
|
|
return decode_scratch(a, 256, 0, 400);
|
|
}
|
|
|
|
extern "C" int r4d_rad_attn_decode_h256_fp8v4(const RadArgs* a, RadStream s) {
|
|
if (check_dims_once() != RAD_OK) return R4D_NO(RAD_E_SHAPE);
|
|
R4DArgs r;
|
|
int rc = decode_common(a, &r, 256, 0, 400);
|
|
if (rc != RAD_OK) return rc;
|
|
return r4d_attn_decode_h256_gqa6_fp8v4kv(&r, (hipStream_t)s);
|
|
}
|
|
|
|
/* The k6_v4 cache: a token's row is 22 * 256 / 16 = 352 bytes (int6 K, int4 V, f16 scales). */
|
|
extern "C" int r4d_rad_attn_prefill_h256_k6v4(const RadArgs* a, RadStream s) {
|
|
if (check_dims_once() != RAD_OK) return R4D_NO(RAD_E_SHAPE);
|
|
R4DArgs r;
|
|
int rc = fill_paged_row(a, &r, 256, 352);
|
|
if (rc != RAD_OK) return rc;
|
|
r.splits = prefill_splits(a, &r);
|
|
return r4d_attn_prefill_h256_gqa6_k6v4kv(&r, (hipStream_t)s);
|
|
}
|
|
|
|
extern "C" int64_t r4d_rad_attn_decode_h256_k6_scratch(const RadArgs* a) {
|
|
return decode_scratch(a, 256, 0, 352);
|
|
}
|
|
|
|
extern "C" int r4d_rad_attn_decode_h256_k6v4(const RadArgs* a, RadStream s) {
|
|
if (check_dims_once() != RAD_OK) return R4D_NO(RAD_E_SHAPE);
|
|
R4DArgs r;
|
|
int rc = decode_common(a, &r, 256, 0, 352);
|
|
if (rc != RAD_OK) return rc;
|
|
return r4d_attn_decode_h256_gqa6_k6v4kv(&r, (hipStream_t)s);
|
|
}
|
|
|
|
extern "C" int r4d_rad_attn_decode_h256_bf16(const RadArgs* a, RadStream s) {
|
|
if (check_dims_once() != RAD_OK) return R4D_NO(RAD_E_SHAPE);
|
|
R4DArgs r;
|
|
int rc = decode_common(a, &r, 256, 0);
|
|
if (rc != RAD_OK) return rc;
|
|
return r4d_attn_decode_h256_gqa6_bf16kv(&r, (hipStream_t)s);
|
|
}
|
|
|
|
extern "C" int r4d_rad_attn_decode_h128_fp8(const RadArgs* a, RadStream s) {
|
|
R4DArgs r;
|
|
int rc = decode_common(a, &r, 128, 1);
|
|
if (rc != RAD_OK) return rc;
|
|
/* Absent descales mean 1.0; see the prefill twin above. */
|
|
return r4d_attn_decode_h128_gqa4_fp8kv(&r, (hipStream_t)s);
|
|
}
|
|
|
|
extern "C" int r4d_rad_attn_decode_h128_bf16(const RadArgs* a, RadStream s) {
|
|
R4DArgs r;
|
|
int rc = decode_common(a, &r, 128, 1);
|
|
if (rc != RAD_OK) return rc;
|
|
return r4d_attn_decode_h128_gqa4_bf16kv(&r, (hipStream_t)s);
|
|
}
|
|
|
|
/* Gemma 4 dense sliding window (head_dim 256, gqa 2, window 1024) decode; its prefill entry points
|
|
* are in r4d_attn_paged_h256_gqa2.hip. No check_dims_once: this is not the geometry that reports
|
|
* dims. */
|
|
extern "C" int64_t r4d_rad_attn_decode_h256_gqa2_scratch(const RadArgs* a) {
|
|
return decode_scratch(a, 256, 2);
|
|
}
|
|
|
|
extern "C" int r4d_rad_attn_decode_h256_gqa2_fp8(const RadArgs* a, RadStream s) {
|
|
R4DArgs r;
|
|
int rc = decode_common(a, &r, 256, 2);
|
|
if (rc != RAD_OK) return rc;
|
|
/* Absent descales mean 1.0; see the prefill twin above. */
|
|
return r4d_attn_decode_h256_gqa2_fp8kv(&r, (hipStream_t)s);
|
|
}
|
|
|
|
extern "C" int r4d_rad_attn_decode_h256_gqa2_bf16(const RadArgs* a, RadStream s) {
|
|
R4DArgs r;
|
|
int rc = decode_common(a, &r, 256, 2);
|
|
if (rc != RAD_OK) return rc;
|
|
return r4d_attn_decode_h256_gqa2_bf16kv(&r, (hipStream_t)s);
|
|
}
|
|
|
|
/* Gemma 3 sliding window (head_dim 128, gqa 2, window 1024) decode; its prefill entry points
|
|
* are in r4d_attn_paged_h128_gqa2w.hip. */
|
|
extern "C" int64_t r4d_rad_attn_decode_h128_gqa2w_scratch(const RadArgs* a) {
|
|
return decode_scratch(a, 128, 3);
|
|
}
|
|
|
|
extern "C" int r4d_rad_attn_decode_h128_gqa2w_fp8(const RadArgs* a, RadStream s) {
|
|
R4DArgs r;
|
|
int rc = decode_common(a, &r, 128, 3);
|
|
if (rc != RAD_OK) return rc;
|
|
return r4d_attn_decode_h128_gqa2w_fp8kv(&r, (hipStream_t)s);
|
|
}
|
|
|
|
extern "C" int r4d_rad_attn_decode_h128_gqa2w_bf16(const RadArgs* a, RadStream s) {
|
|
R4DArgs r;
|
|
int rc = decode_common(a, &r, 128, 3);
|
|
if (rc != RAD_OK) return rc;
|
|
return r4d_attn_decode_h128_gqa2w_bf16kv(&r, (hipStream_t)s);
|
|
}
|
|
|
|
/* The same decode under attn_paged_qrow: q read at its own row pitch (R4DArgs::q_ld), the sliding
|
|
* layers' q where Gemma 4 31B's fused q|k|v projection left it. Same kernel, same arithmetic. */
|
|
extern "C" int64_t r4d_rad_attn_qrow_decode_h256_gqa2_scratch(const RadArgs* a) {
|
|
return decode_scratch(a, 256, 2, 0, 1, true);
|
|
}
|
|
extern "C" int r4d_rad_attn_qrow_decode_h256_gqa2_fp8(const RadArgs* a, RadStream s) {
|
|
R4DArgs r;
|
|
int rc = decode_common(a, &r, 256, 2, 0, 1, true);
|
|
if (rc != RAD_OK) return rc;
|
|
return r4d_attn_decode_h256_gqa2_qld_fp8kv(&r, (hipStream_t)s);
|
|
}
|
|
extern "C" int r4d_rad_attn_qrow_decode_h256_gqa2_bf16(const RadArgs* a, RadStream s) {
|
|
R4DArgs r;
|
|
int rc = decode_common(a, &r, 256, 2, 0, 1, true);
|
|
if (rc != RAD_OK) return rc;
|
|
return r4d_attn_decode_h256_gqa2_qld_bf16kv(&r, (hipStream_t)s);
|
|
}
|
|
|
|
/* Head_dim 128 at 8 queries per kv head, any window, causal or not, q_len up to 16, under
|
|
* attn_paged_qrow: the gqa-4 kernel with each kv head mapped onto two query groups
|
|
* (R4DArgs::kv_rep = 2), the 31B's DFlash drafter's attention over a cache that holds each kv head
|
|
* once. The workgroup holds q_len * 4 rows, so a block of 16 fits where gqa 8 (128 rows) does not;
|
|
* the two groups of a kv head read its blocks twice, as the copies they replace did. */
|
|
extern "C" int64_t r4d_rad_attn_qrow_h128_gqa8_scratch(const RadArgs* a) {
|
|
return decode_scratch(a, 128, 1, 0, 2, true);
|
|
}
|
|
extern "C" int r4d_rad_attn_qrow_h128_gqa8_fp8(const RadArgs* a, RadStream s) {
|
|
R4DArgs r;
|
|
int rc = decode_common(a, &r, 128, 1, 0, 2, true);
|
|
if (rc != RAD_OK) return rc;
|
|
return r4d_attn_decode_h128_gqa4_rep_fp8kv(&r, (hipStream_t)s);
|
|
}
|
|
extern "C" int r4d_rad_attn_qrow_h128_gqa8_bf16(const RadArgs* a, RadStream s) {
|
|
R4DArgs r;
|
|
int rc = decode_common(a, &r, 128, 1, 0, 2, true);
|
|
if (rc != RAD_OK) return rc;
|
|
return r4d_attn_decode_h128_gqa4_rep_bf16kv(&r, (hipStream_t)s);
|
|
}
|