This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libr4d/r4d_attn_decode_h256_gqa6.hip
T
Deadcode 9eaf10da6e radiance 1.11.0
New models:
- GLM-5.3-Flash reads pictures: its vision tower and processor (--image-tokens sets the budget),
  with decode kernels for a few rows and the DSA indexer split over the ranks
- Sage-A1.5 (fp8 and MXFP4) with its vision tower and a DFlash2 drafter
- Unlimited-OCR (bf16)
- Gemma 4 31B with z-lab's DFlash and RedHatAI's DFlash2 drafters; at four cards its all-reduces
  are fused with the junctions, overlapped with the compute and sent compressed
- Ornith-1.5-35B-A3B w4a8 compose files

Speed:
- Block drafters (DFlash, DFlash2) accept their drafts on the card, and the next step is issued
  ahead
- The four-rank all-reduce is faster for every --tp 4 model
- qwen35_w8a8: the norms and SiLU write the int8 linears' operands directly
- --attn-prefill-fp8 off|qk|pv|both: fp8 QK and PV legs for the h256 prefill attention (about +20%
  prefill on a 221K-token prompt for the 27B MXFP4 at TP2); lossy, off by default

Features:
- Control vectors: --control-vector off|on|disabled (disabled by default), a per-request switch,
  and the published Qwen3.8-Flash-Next refusal-direction vector (its license is in NOTICE)
- Krea 2: the `rebalance` request field, as ComfyUI's ConditioningKrea2Rebalance
- Cards with a small PCI BAR run, with kernel arguments kept in host memory
- Dashboard: live acceptance figures; statsdump rotates its file at a size cap
- KV caches every rank holds whole (GLM-5.3's latent and index caches) take one host-tier slot

Fixes:
- Long conversations taking turns no longer fall back to an early snapshot: a host-tier restore
  makes room as memory pressure does and waits a step for the previous turn's release (two
  conversations of 142K and 147K tokens sent back to back: 31.3 s per turn before, 2.8 s now)
- Kernels before Linux 6.16 (amdgpu below 3.64) no longer hang at startup: virtual memory
  management is turned off there, with a warning
- Gemma 4 dense scales its token embeddings as Hugging Face does (bf16), which changes its output
- The four-rank all-reduce fences prefill-sized messages, for a rare divergence after a fresh start
- GLM-5.3: every refusal names the setting and the reason; its template keeps string content
- Several pictures in one request no longer refuse processors that take one image at a time

Changes: Plugin ABI 26: out-of-tree plugins must be rebuilt. The environment switches the PRs
added are fixed rules or flags now (R4D_ATTN_FP8 is --attn-prefill-fp8).
2026-10-23 11:57:52 +02:00

1047 lines
51 KiB
Plaintext

// R4D split-KV decode (also the MTP-verify shape). Same core as the prefill kernel (lazy
// reference-max softmax, f16 operands, folded query scale, contiguous-k fragments, prefetch +
// scheduling barrier, dot2 row sum), plus two decode-only options:
//
// KLDS0 reads K straight from the paged cache (bf16 or fp8), widening once per warp instead of
// once per workgroup. Costs VALU on a bandwidth-bound leg and frees sK, which at TILE 16
// is 8.3 KB of the 14.5 KB that limits resident workgroups. Best at every depth.
// PF16 split-KV partials stored as f16. At 16K context the partial round trip is ~14 MB
// against ~17 MB of KV. Storing O/l instead of O bounds values by max|v|, so f16 cannot
// overflow; the 11-bit mantissa costs ~5e-4 relative.
//
// No fp8-P leg and no non-CONTIG alternative here; r4d_attn_prefill_h256_gqa6.hip documents the
// full option set.
#include "r4d.h"
#include "r4d_common.h"
#include "r4d_dt16.h"
#include "r4d_q8.h" // the gate quantiser's 8-bit codes: E4M3, or int8 (GQ == 2)
#include "r4d_chain.h"
#include <algorithm>
#include <type_traits>
#define O_F16 1
#define O_MSKIP 2
#define O_FOLDQ 8
#define O_KLDS0 16
#define O_PF16 32
#define O_CONTIG 512
#define O_PF2 1024
#define O_PF4 2048
#define O_PF8 3072
#define O_SGB 4096
#define O_DOT2 16384
// Tile-staging options, shared with r4d_attn_prefill_h256_gqa6.hip:
// BTS the tile's block-table entries in SGPRs, loaded a tile ahead. With KLDS0 this also takes
// the dependent bt[kk/BS] load out of the m-tile loop.
// GPREV tile ti+1's V fetched into registers before the m-tile loop.
// LDSB an LDS-scoped barrier, so the barrier does not invalidate the vector cache.
#define O_BTS 262144
#define O_GPREV 1048576
#define O_LDSB 2097152
// Bit 4194304 is free: a non-temporal hint on the K load gained nothing on the target GPU (the KV
// stream does not evict the neighbouring GEMMs' activations).
// O_V8: an fp8 cache's V tile staged in LDS as codes (6 KiB instead of 12 KiB of f16), converted
// when a fragment is read. For a one-wave workgroup the f16 tile is what caps residency (ten
// workgroups per WGP). At one wave the conversion count is unchanged and the conversion is exact,
// so results are identical; with more waves each would convert the whole tile, so the launcher
// uses it only at one wave.
#define O_V8 8388608
// O_KRAW: with K read from the paged cache (KLDS0, CONTIG), issue all HEAD_DIM/16 fragment loads
// of an m-tile in one round and convert each fragment just before its WMMA. LLVM 24 honours the PF
// groups and scheduling barriers literally, so with PF 2 K arrives in HEAD_DIM/32 serial round
// trips (16-19% on the head_dim-128 drafter leg, bf16 cache). Raw cache bytes are held (8 per
// fragment for fp8, 16 for bf16), costing 2 or 4 x HEAD_DIM/16 VGPRs; PF = HEAD_DIM/16 would hold
// converted fragments and cost occupancy. Results are unchanged.
#define O_KRAW 16777216
// O_V4: the fp8_v4 cache (E4M3 K, int4 V in groups of 32 with an f16 scale), and with O_K6 the
// k6_v4 cache (int6 K, int4 V); a token's row layout is RadBuildCtx::kv_quant's (abi/rad_builder.h).
#define O_V4 33554432
#define O_K6 67108864
// The two views of R4DArgs (r4d.h), compiled in only where a row asks for them, since the h256
// instances sit at the 256-VGPR bound and a runtime pitch and head map spill there:
// QLD q rows at R4DArgs::q_ld (attn_paged_qrow), not dense.
// KVREP query group g reads cache head g / R4DArgs::kv_rep, descales per cache head.
// Neither changes the arithmetic.
#define O_QLD 134217728
#define O_KVREP 268435456
__host__ __device__ __forceinline__ int r4d_attn_tiles(int ctx, int tile) { return (ctx + tile - 1) / tile; }
__host__ __device__ __forceinline__ int r4d_attn_tps(int ctx, int tile, int splits) {
int t = r4d_attn_tiles(ctx, tile); return (t + splits - 1) / splits;
}
// First KV position query row `qrow` may attend to under a sliding window: the row's
// causal limit, less the window, floored at zero. window <= 0 means no window.
__host__ __device__ __forceinline__ int r4d_attn_wlo(int ctx, int q_len, int qrow, int window) {
if (window <= 0) return 0;
const int lo = ctx - q_len + qrow - window + 1;
return lo > 0 ? lo : 0;
}
// WIN enables a sliding window (a.window positions ending at each row's causal limit). As a
// template parameter defaulting to 0, full attention compiles with no window code.
// The body: its blocks return early (empty slots, tiles past the window), so the chained form
// (r4d_chain.h) wraps it below rather than bracketing it in place.
template<int NWARPS, int TILE, int HEAD_DIM, int GQA, int BS, int KVP, int OPT,
int WIN = 0>
__device__ __forceinline__
void r4d_attn_decode_body(const R4DArgs& a, int splits)
{
constexpr int F16 = (OPT & O_F16) ? 1 : 0;
constexpr int MSKIP = (OPT & O_MSKIP) ? 1 : 0;
constexpr int FOLDQ = (OPT & O_FOLDQ) ? 1 : 0;
constexpr int KLDS = (OPT & O_KLDS0) ? 0 : 1;
constexpr int PF16 = (OPT & O_PF16) ? 1 : 0;
constexpr int CTG = (OPT & O_CONTIG) ? 1 : 0;
constexpr int PF = 1 << ((OPT >> 10) & 3);
constexpr int SGB = (OPT & O_SGB) ? 1 : 0;
constexpr int DOT2 = ((OPT & O_DOT2) && (OPT & O_F16)) ? 1 : 0;
constexpr int BTS = (OPT & O_BTS) ? 1 : 0;
constexpr int LDSB = (OPT & O_LDSB) ? 1 : 0;
constexpr int V4 = ((OPT & O_V4) && !KVP) ? 1 : 0;
constexpr int K6 = ((OPT & O_K6) && V4) ? 1 : 0;
// A token's row in a page, and where in it V's codes, V's scales and K's scales start.
constexpr int ROWB = K6 ? HEAD_DIM * 22 / 16 : V4 ? HEAD_DIM * 25 / 16 : 2 * HEAD_DIM;
constexpr int VOFF = K6 ? HEAD_DIM * 3 / 4 : HEAD_DIM;
constexpr int VSOFF = K6 ? HEAD_DIM * 21 / 16 : HEAD_DIM * 3 / 2;
constexpr int KSOFF = HEAD_DIM * 5 / 4;
constexpr int V8 = ((OPT & O_V8) && !KVP && !V4) ? 1 : 0;
static_assert(!V8 || (OPT & O_CONTIG), "the code tile is read in the contiguous layout");
constexpr int KRAW = ((OPT & O_KRAW) && !KLDS) ? 1 : 0;
static_assert(!KRAW || (OPT & O_CONTIG), "the raw K round reads the contiguous layout");
static_assert(!K6 || (!KLDS && (OPT & O_CONTIG) && !KRAW),
"k6_v4 K is widened from the paged cache, a lane's eight dims contiguous");
static_assert(!V4 || (OPT & O_F16), "fp8_v4 widens to f16");
constexpr int QLD = (OPT & O_QLD) ? 1 : 0;
constexpr int KVREP = (OPT & O_KVREP) ? 1 : 0;
typedef DT16<F16> D;
typedef typename D::frag frag16;
constexpr int NKS = HEAD_DIM / 16;
constexpr int MT = TILE / 16;
constexpr int KSTR = HEAD_DIM + (CTG ? 8 : 4);
constexpr int VSTR = TILE + (CTG ? 8 : 4);
constexpr int NTHR = NWARPS * 32;
constexpr int NB = TILE / BS;
constexpr int NTR = (TILE / 8) * (HEAD_DIM / (KVP ? 32 : 64));
constexpr int VREGS = (NTR + NWARPS - 1) / NWARPS;
// A V prefetch costs VREGS x 4 VGPRs on top of a 128-VGPR accumulator and a 64-VGPR query.
// Up to three uint4 it fits in 255 VGPRs without spilling (two: +6-8% at depth; three, the
// 48-row shape q_len 8 x gqa 6 at NWARPS 3: a further 2-15%). At four or more (NWARPS 1-2, or a
// bf16 cache) it spills and loses ~10%, so the geometry gates it, not just the flag.
constexpr int GPREV = ((OPT & O_GPREV) && VREGS <= 3 && !V4) ? 1 : 0;
constexpr float PSHIFT = D::SHIFT;
constexpr float PGROW = D::GROW;
__shared__ uint16_t sK[KLDS ? TILE * KSTR : 1];
__shared__ uint16_t sV16[V8 ? 1 : HEAD_DIM * VSTR];
/* A code-tile row is 16 keys + 8 padding = 24 bytes (six dwords): the sixteen lanes of a
* fragment read hit distinct banks, and a lane's eight codes are one aligned 8-byte load. */
constexpr int VSTR8 = TILE + 8;
__shared__ uint8_t sV8[V8 ? HEAD_DIM * VSTR8 : 1];
const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5;
const int c = lane & 15, h = lane >> 4;
const int sp = blockIdx.x, kvh = blockIdx.y, seq = blockIdx.z;
// kvh is the query group; under KVREP the cache head it reads is kvh / kv_rep
const int kvc = KVREP ? kvh / a.kv_rep : kvh;
const int ctx = a.seqused_k[seq];
if (ctx <= 0) return;
// Ragged batches (r4d.h, R4DArgs::cu_q): this sequence's q/out rows and query length. With
// cu_q, a.q_len is the batch maximum that sized the wave count; rows past qlen are not live.
const int qoff = r4d_q_off(a.cu_q, seq, a.q_len);
const int qlen = r4d_q_len(a.cu_q, seq, a.q_len);
if (qlen <= 0) return; // a slot carrying no query rows this step
const int ntl = r4d_attn_tiles(ctx, TILE);
// A sliding-window layer must not read below the window: those blocks may already be freed
// and reassigned to another sequence. The walk starts at the tile holding row 0's first
// window token (row 0 reaches furthest back), and the splits divide that range.
const int wlo0 = WIN ? r4d_attn_wlo(ctx, qlen, 0, a.window) : 0;
const int tb = WIN ? wlo0 / TILE : 0;
const int neff = ntl - tb;
const int tps = (neff + splits - 1) / splits;
const int t_lo = tb + sp * tps;
if (t_lo >= ntl) return;
const int t_hi = min(t_lo + tps, ntl);
const int row = warp * 16 + c;
const int qi = row / GQA, hi = row - qi * GQA;
const bool live = (qi < qlen);
const int qrow = live ? qi : (qlen - 1);
const int qhead = kvh * GQA + hi;
const int* __restrict__ bt = a.block_table + (size_t)seq * a.max_blocks;
const float kdesc = a.k_descale
? a.k_descale[seq * (KVREP ? a.kv_heads / a.kv_rep : a.kv_heads) + kvc]
: 1.0f;
const float mul = a.scale * kdesc * 1.44269504089f;
frag16 qf[NKS];
{
const uint16_t* qp = QLD ? (const uint16_t*)a.q + (size_t)(qoff + qrow) * a.q_ld
+ (size_t)qhead * HEAD_DIM
: (const uint16_t*)a.q
+ ((size_t)(qoff + qrow) * a.q_heads + qhead) * HEAD_DIM;
#pragma unroll
for (int t = 0; t < NKS; ++t) {
const uint2 l0 = *(const uint2*)(qp + 16 * t + (CTG ? 8 * h : 4 * h));
const uint2 h0 = *(const uint2*)(qp + 16 * t + (CTG ? 8 * h + 4 : 8 + 4 * h));
qf[t] = D::mk(make_uint2(r4d_qcvt<D, FOLDQ, F16>(l0.x, mul),
r4d_qcvt<D, FOLDQ, F16>(l0.y, mul)),
make_uint2(r4d_qcvt<D, FOLDQ, F16>(h0.x, mul),
r4d_qcvt<D, FOLDQ, F16>(h0.y, mul)));
}
}
// The row's absolute position. Causal masking bounds the key by it; non-causal masking
// bounds it by the sequence, with the window applied on both sides (a block-diffusion
// drafter's own block).
const int pos = ctx - qlen + qrow;
const int cz = a.causal_seq ? (int)((const unsigned char*)a.causal_seq)[seq] : a.causal;
const int klimit = cz ? pos
: ((WIN && a.window > 0) ? min(ctx - 1, pos + a.window - 1)
: ctx - 1);
const int wlo = (WIN && a.window > 0) ? (pos - a.window + 1 > 0
? pos - a.window + 1 : 0) : 0;
float m_ref = -3.0e38f;
float off = PSHIFT + 3.0e38f;
float l_i = 0.0f;
v8f acc[NKS];
#pragma unroll
for (int t = 0; t < NKS; ++t) acc[t] = (v8f){0,0,0,0,0,0,0,0};
const int vg = lane >> 3, vj = lane & 7;
uint4 vreg[VREGS];
// ---- fp8_v4 / k6_v4: V's int4 codes into sV16 ([dim][key]) as the prefill kernel stages them
// (r4d_attn_prefill_h256_gqa6.hip: a key pair's 8 dims a thread, two code words and the pair's
// scale, widened with packed f16 into one dword a dim), fetched in the tile: no prefetch.
constexpr int NP4 = (TILE / 2) * (HEAD_DIM / 8);
constexpr int U4 = V4 ? (NP4 + NTHR - 1) / NTHR : 1;
uint32_t v4c[U4][2], v4s[U4];
auto fetchV4 = [&](int k0, const int* blks) {
#pragma unroll
for (int r = 0; r < U4; ++r) {
const int u = tid + r * NTHR;
if (NP4 % NTHR == 0 || u < NP4) {
const int kp = u % (TILE / 2), o = u / (TILE / 2);
uint32_t sc[2];
#pragma unroll
for (int j = 0; j < 2; ++j) {
int kk = min(k0 + 2 * kp + j, ctx - 1);
if (WIN && kk < wlo0) kk = wlo0;
const int bk = BTS ? blks[(kk - k0) / BS] : bt[kk / BS];
const uint8_t* base = (const uint8_t*)a.kv + (size_t)bk * a.kv_block_stride
+ (size_t)kvc * a.kv_head_stride + (size_t)(kk % BS) * ROWB;
v4c[r][j] = *(const uint32_t*)(base + VOFF + o * 4);
sc[j] = *(const uint16_t*)(base + VSOFF + (o >> 2) * 2);
}
v4s[r] = sc[0] | (sc[1] << 16);
}
}
};
auto storeV4 = [&]() {
typedef _Float16 h2 __attribute__((ext_vector_type(2)));
#pragma unroll
for (int r = 0; r < U4; ++r) {
const int u = tid + r * NTHR;
if (NP4 % NTHR == 0 || u < NP4) {
const int kp = u % (TILE / 2), o = u / (TILE / 2);
// Two's complement nibbles; ^ 8 makes them offset binary (value + 8).
const uint32_t n0 = (v4c[r][0] & 0x0F0F0F0Fu) ^ 0x08080808u;
const uint32_t m0 = ((v4c[r][0] >> 4) & 0x0F0F0F0Fu) ^ 0x08080808u;
const uint32_t n1 = (v4c[r][1] & 0x0F0F0F0Fu) ^ 0x08080808u;
const uint32_t m1 = ((v4c[r][1] >> 4) & 0x0F0F0F0Fu) ^ 0x08080808u;
const h2 s2 = __builtin_bit_cast(h2, v4s[r]);
const h2 bias = { (_Float16)1032.0f, (_Float16)1032.0f };
uint16_t* dst = &sV16[(o * 8) * VSTR + 2 * kp];
#pragma unroll
for (int b = 0; b < 4; ++b) {
const uint32_t sel = (uint32_t)b | 0x0c00u | ((uint32_t)(4 + b) << 16) | 0x0c000000u;
const uint32_t lo = __builtin_amdgcn_perm(n1, n0, sel) | 0x64006400u;
const uint32_t hi = __builtin_amdgcn_perm(m1, m0, sel) | 0x64006400u;
const h2 vl = (__builtin_bit_cast(h2, lo) - bias) * s2;
const h2 vh = (__builtin_bit_cast(h2, hi) - bias) * s2;
*(uint32_t*)(dst + (2 * b) * VSTR) = __builtin_bit_cast(uint32_t, vl);
*(uint32_t*)(dst + (2 * b + 1) * VSTR) = __builtin_bit_cast(uint32_t, vh);
}
}
}
};
auto fetchV = [&](int k0, const int* blks) {
#pragma unroll
for (int r = 0; r < VREGS; ++r) {
const int idx = warp + r * NWARPS;
if (NTR % NWARPS == 0 || idx < NTR) {
constexpr int DG = HEAD_DIM / (KVP ? 32 : 64);
const int kg = idx / DG, dg = idx - kg * DG;
int kk = min(k0 + kg * 8 + vj, ctx - 1);
if (WIN && kk < wlo0) kk = wlo0;
const int d0 = dg * (KVP ? 32 : 64) + vg * (KVP ? 8 : 16);
const int blk = BTS ? blks[(kk - k0) / BS] : bt[kk / BS];
const size_t o = (size_t)blk * a.kv_block_stride
+ (size_t)kvc * a.kv_head_stride
+ (size_t)(kk % BS) * ROWB + HEAD_DIM + d0;
const void* p = KVP ? (const void*)((const uint16_t*)a.kv + o)
: (const void*)((const uint8_t*)a.kv + o);
vreg[r] = __builtin_bit_cast(uint4, load_tr_b128(p));
}
}
};
auto storeV = [&]() {
#pragma unroll
for (int r = 0; r < VREGS; ++r) {
const int idx = warp + r * NWARPS;
if (NTR % NWARPS == 0 || idx < NTR) {
constexpr int DG = HEAD_DIM / (KVP ? 32 : 64);
const int kg = idx / DG, dg = idx - kg * DG;
const int d0 = dg * (KVP ? 32 : 64) + vg * (KVP ? 8 : 16);
const uint4 v = vreg[r];
if (KVP) {
uint16_t* dst = &sV16[(d0 + vj) * VSTR + kg * 8];
*(uint2*)(dst) = make_uint2(D::from_bf16w(v.x), D::from_bf16w(v.y));
*(uint2*)(dst + 4) = make_uint2(D::from_bf16w(v.z), D::from_bf16w(v.w));
} else if (V8) {
const uint32_t lo0 = byte_gather(v.x, v.y, 0), lo1 = byte_gather(v.z, v.w, 0);
const uint32_t hi0 = byte_gather(v.x, v.y, 1), hi1 = byte_gather(v.z, v.w, 1);
*(uint2*)&sV8[(d0 + 2 * vj) * VSTR8 + kg * 8] = make_uint2(lo0, lo1);
*(uint2*)&sV8[(d0 + 2 * vj + 1) * VSTR8 + kg * 8] = make_uint2(hi0, hi1);
} else {
const uint32_t lo0 = byte_gather(v.x, v.y, 0), lo1 = byte_gather(v.z, v.w, 0);
const uint32_t hi0 = byte_gather(v.x, v.y, 1), hi1 = byte_gather(v.z, v.w, 1);
uint32_t wl[4], wh[4];
fp8x8_to_16x4w<D>(make_uint2(lo0, lo1), wl);
fp8x8_to_16x4w<D>(make_uint2(hi0, hi1), wh);
uint16_t* a0 = &sV16[(d0 + 2 * vj) * VSTR + kg * 8];
uint16_t* a1 = &sV16[(d0 + 2 * vj + 1) * VSTR + kg * 8];
*(uint2*)(a0) = make_uint2(wl[0], wl[1]);
*(uint2*)(a0 + 4) = make_uint2(wl[2], wl[3]);
*(uint2*)(a1) = make_uint2(wh[0], wh[1]);
*(uint2*)(a1 + 4) = make_uint2(wh[2], wh[3]);
}
}
}
};
// A tile's NB entries are read whole, and a multi-block tile can run past the sequence's last
// block (on the last row, past the end of the table). Keys are clamped to ctx - 1, so entries
// past lastb are never used; only tiles reaching past lastb take the clamped load, and the
// rest keep one wide scalar load. With NB == 1 the test folds away.
const int lastb = (ctx - 1) / BS;
auto load_bt = [&](int t0, int* out) {
if (NB > 1 && t0 + NB - 1 > lastb) {
#pragma unroll
for (int i = 0; i < NB; ++i) out[i] = bt[min(t0 + i, lastb)];
} else {
#pragma unroll
for (int i = 0; i < NB; ++i) out[i] = bt[t0 + i];
}
};
int blk[NB];
if (BTS) load_bt(t_lo * NB, blk);
if (GPREV) fetchV(t_lo * TILE, blk);
for (int ti = t_lo; ti < t_hi; ++ti) {
const int k0 = ti * TILE;
int blkn[NB];
if (BTS && ti + 1 < t_hi) {
load_bt((ti + 1) * NB, blkn);
}
if (LDSB) lds_barrier(); else __syncthreads();
if (KLDS) {
#pragma unroll 1
for (int i = tid; i < TILE * (HEAD_DIM / 8); i += NTHR) {
const int key = i / (HEAD_DIM / 8), ch = i - key * (HEAD_DIM / 8);
int kk = min(k0 + key, ctx - 1);
if (WIN && kk < wlo0) kk = wlo0;
const int kb = BTS ? blk[(kk - k0) / BS] : bt[kk / BS];
const size_t o = (size_t)kb * a.kv_block_stride
+ (size_t)kvc * a.kv_head_stride + (size_t)(kk % BS) * ROWB;
uint16_t* kd = &sK[key * KSTR];
uint32_t w4[4];
if (KVP) {
const uint4 r = *(const uint4*)((const uint16_t*)a.kv + o + ch * 8);
w4[0] = D::from_bf16w(r.x); w4[1] = D::from_bf16w(r.y);
w4[2] = D::from_bf16w(r.z); w4[3] = D::from_bf16w(r.w);
} else {
fp8x8_to_16x4w<D>(*(const uint2*)((const uint8_t*)a.kv + o + ch * 8), w4);
}
*(uint2*)(kd + ch * 8) = make_uint2(w4[0], w4[1]);
*(uint2*)(kd + ch * 8 + 4) = make_uint2(w4[2], w4[3]);
}
}
if constexpr (V4) { fetchV4(k0, blk); storeV4(); }
else { if (!GPREV) fetchV(k0, blk); storeV(); }
if (LDSB) lds_barrier(); else __syncthreads();
if (GPREV && ti + 1 < t_hi) fetchV(k0 + TILE, blkn);
#pragma unroll 1
for (int mt = 0; mt < MT; ++mt) {
const uint8_t* grow = nullptr;
if (!KLDS) {
int kk = min(k0 + mt * 16 + c, ctx - 1);
if (WIN && kk < wlo0) kk = wlo0;
const int kb = BTS ? blk[(kk - k0) / BS] : bt[kk / BS];
grow = (const uint8_t*)a.kv + ((size_t)kb * a.kv_block_stride
+ (size_t)kvc * a.kv_head_stride + (size_t)(kk % BS) * ROWB)
* (KVP ? 2 : 1);
}
// A fragment's raw K: 8 elements of 1 byte (fp8) or 2 (bf16).
typedef typename std::conditional<KVP != 0, uint4, uint2>::type kraw_t;
kraw_t kraw[KRAW ? NKS : 1];
if constexpr (KRAW) {
#pragma unroll
for (int t = 0; t < NKS; ++t)
kraw[t] = *(const kraw_t*)(grow + (16 * t + 8 * h) * (KVP ? 2 : 1));
}
v8f s = (v8f){0,0,0,0,0,0,0,0};
#pragma unroll
for (int g = 0; g < NKS; g += PF) {
frag16 kb[PF];
#pragma unroll
for (int j = 0; j < PF; ++j) {
const int t = g + j;
if (KLDS) {
const uint16_t* kp = &sK[(mt * 16 + c) * KSTR + 16 * t];
if (CTG) {
const uint4 u = *(const uint4*)(kp + 8 * h);
kb[j] = D::mk(make_uint2(u.x, u.y), make_uint2(u.z, u.w));
} else {
kb[j] = D::mk(*(const uint2*)(kp + 4 * h), *(const uint2*)(kp + 8 + 4 * h));
}
} else if (KVP) {
const uint16_t* kp = (const uint16_t*)grow + 16 * t;
uint2 l0, h0;
if constexpr (KRAW && KVP != 0) {
const uint4 u = kraw[t];
l0 = make_uint2(u.x, u.y);
h0 = make_uint2(u.z, u.w);
} else {
l0 = *(const uint2*)(kp + (CTG ? 8 * h : 4 * h));
h0 = *(const uint2*)(kp + (CTG ? 8 * h + 4 : 8 + 4 * h));
}
kb[j] = D::mk(make_uint2(D::from_bf16w(l0.x), D::from_bf16w(l0.y)),
make_uint2(D::from_bf16w(h0.x), D::from_bf16w(h0.y)));
} else if constexpr (K6) {
// k6_v4: this lane's eight dims (16t + 8h ..) from their low nibbles, the
// chunk's high-bit word and the group's scale.
uint32_t w4[4];
const uint32_t sb = *(const uint16_t*)(grow + KSOFF + (t >> 1) * 2);
k6x8_to_f16x4w(*(const uint32_t*)(grow + 8 * t + 4 * h),
*(const uint32_t*)(grow + HEAD_DIM / 2 + 4 * t), h,
sb | (sb << 16), w4);
kb[j] = D::mk(make_uint2(w4[0], w4[1]), make_uint2(w4[2], w4[3]));
} else {
const uint8_t* kp = grow + 16 * t;
uint32_t w4[4];
// CONTIG: one 8-byte load; the split map needs two 4-byte loads.
uint2 raw;
if constexpr (KRAW && KVP == 0) raw = kraw[t];
else raw = CTG ? *(const uint2*)(kp + 8 * h)
: make_uint2(*(const uint32_t*)(kp + 4 * h),
*(const uint32_t*)(kp + 8 + 4 * h));
fp8x8_to_16x4w<D>(raw, w4);
kb[j] = D::mk(make_uint2(w4[0], w4[1]), make_uint2(w4[2], w4[3]));
}
}
if (SGB) sched_barrier();
#pragma unroll
for (int j = 0; j < PF; ++j) s = D::wmma(kb[j], qf[g + j], s);
}
float sc[8];
#pragma unroll
for (int e = 0; e < 8; ++e) sc[e] = FOLDQ ? s[e] : s[e] * mul;
const int kbase = k0 + mt * 16 + 8 * h;
if (!MSKIP || wave_any(kbase + 7 > klimit) || (WIN && wave_any(kbase < wlo))) {
#pragma unroll
for (int e = 0; e < 8; ++e)
sc[e] = ((kbase + e <= klimit) && (!WIN || kbase + e >= wlo))
? sc[e] : -INFINITY;
}
float smax = sc[0];
#pragma unroll
for (int e = 1; e < 8; ++e) smax = fmaxf(smax, sc[e]);
if (wave_any(smax > m_ref + PGROW)) {
const float rmax = fmaxf(smax, swap16(smax));
const float mnew = fmaxf(fmaxf(m_ref, rmax), -3.0e38f);
const float alpha = ex2(m_ref - mnew);
#pragma unroll
for (int dt = 0; dt < NKS; ++dt)
#pragma unroll
for (int e = 0; e < 8; ++e) acc[dt][e] *= alpha;
l_i *= alpha;
m_ref = mnew;
off = PSHIFT - mnew;
}
float pf[8], lsum = 0.0f;
#pragma unroll
for (int e = 0; e < 8; ++e) {
const float p = ex2(sc[e] + off); pf[e] = p;
if (!DOT2) lsum += p;
}
if (!DOT2) l_i += lsum;
{
uint32_t pw[4];
#pragma unroll
for (int e = 0; e < 8; e += 2) pw[e >> 1] = D::pk(pf[e], pf[e + 1]);
if (DOT2) {
const v2hf one = {(__fp16)1.0f, (__fp16)1.0f};
#pragma unroll
for (int e = 0; e < 4; ++e)
l_i = __builtin_amdgcn_fdot2(__builtin_bit_cast(v2hf, pw[e]), one, l_i, false);
}
const uint2 lo = make_uint2(pw[0], pw[1]);
const uint2 hi2 = make_uint2(pw[2], pw[3]);
frag16 p16;
if (CTG) {
p16 = D::mk(lo, hi2); // QK already left the keys lane-local
} else {
const uint2 y = swap16_u2p((h == 0) ? hi2 : lo);
p16 = (h == 0) ? D::mk(lo, y) : D::mk(y, hi2);
}
const uint16_t* vbase = &sV16[c * VSTR + mt * 16] + (CTG ? 8 * h : 0);
#pragma unroll
for (int g = 0; g < NKS; g += PF) {
frag16 vb[PF];
#pragma unroll
for (int j = 0; j < PF; ++j) {
const uint16_t* vp = vbase + (g + j) * 16 * VSTR;
if (V8) {
uint32_t w4[4];
fp8x8_to_16x4w<D>(*(const uint2*)&sV8[(c + (g + j) * 16) * VSTR8 +
mt * 16 + 8 * h], w4);
vb[j] = D::mk(make_uint2(w4[0], w4[1]), make_uint2(w4[2], w4[3]));
} else if (CTG) {
const uint4 u = *(const uint4*)vp;
vb[j] = D::mk(make_uint2(u.x, u.y), make_uint2(u.z, u.w));
} else {
vb[j] = D::mk(*(const uint2*)(vp + 4 * h),
*(const uint2*)(vp + 8 + 4 * h));
}
}
if (SGB) sched_barrier();
#pragma unroll
for (int j = 0; j < PF; ++j) acc[g + j] = D::wmma(vb[j], p16, acc[g + j]);
}
}
}
if (BTS) {
#pragma unroll
for (int i = 0; i < NB; ++i) blk[i] = blkn[i];
}
}
if (!live) return;
const float lrow = l_i + swap16(l_i);
const int tok = qoff + qrow;
const size_t idx = (size_t)(tok * a.q_heads + qhead) * splits + sp;
const size_t nrow = (size_t)r4d_q_total(a) * a.q_heads * splits;
if (PF16) {
// O/l in f16: bounded by max|v|, so no overflow, and the merge weight becomes w*l.
uint16_t* so = (uint16_t*)a.scratch;
float* sm = (float*)(so + nrow * HEAD_DIM);
float* sl = sm + nrow;
uint16_t* op = so + idx * HEAD_DIM;
const float inv = (lrow > 0.0f) ? (1.0f / lrow) : 0.0f;
#pragma unroll
for (int dt = 0; dt < NKS; ++dt) {
uint32_t tw[4];
#pragma unroll
for (int e = 0; e < 8; e += 2) tw[e >> 1] = DT16<1>::pk(acc[dt][e] * inv,
acc[dt][e + 1] * inv);
*(uint4*)(op + dt * 16 + 8 * h) = make_uint4(tw[0], tw[1], tw[2], tw[3]);
}
if (h == 0) { sm[idx] = m_ref; sl[idx] = lrow; }
} else {
float* so = (float*)a.scratch;
float* sm = so + nrow * HEAD_DIM;
float* sl = sm + nrow;
float* op = so + idx * HEAD_DIM;
#pragma unroll
for (int dt = 0; dt < NKS; ++dt) *(v8f*)(op + dt * 16 + 8 * h) = acc[dt];
if (h == 0) { sm[idx] = m_ref; sl[idx] = lrow; }
}
}
// ---- Output gate and fp8 quantiser as the merge's epilogue (attn_paged_gate_quant) ----
//
// A gated attention's merged row feeds gate_quant_fp8: bf16(sigmoid(gate)) times the row, rounded
// to bf16, then E4M3 codes with one scale per 128 columns. A merge row is one (token, head) pair
// and a scale group is 128 columns, so the `_gq` forms finish the row in registers instead of in a
// second dispatch.
//
// Byte-identical to gate_quant_fp8 (r4d_fused_quant_fp8.hip): gate rounded to bf16 before the
// multiply, product rounded, scale = group absmax / 448 (1 at zero), codes clamped to +-448, and
// the bf16 product stored to `out`. A padded slot merges to zeros and takes the same epilogue.
// GQ == 2 is the int8 form (scale amax / 127, round to nearest-even, r4d_q8.h).
struct R4DGateQuant {
const uint16_t* g; // gate, row `job` at g + job * g_ld
uint8_t* q; // E4M3 (or int8, `i8`) codes, row `job` at q + job * q_ld
float* s; // one scale per 128 columns, row `job` at s + job * s_ld
unsigned g_ld, q_ld, s_ld;
int i8; // read by the launcher only: which combine to instantiate
};
constexpr int kGqGroup = 128;
__device__ __forceinline__ uint16_t r4d_gq_gate(uint16_t gb, uint16_t xb) {
const float gv = bf16_to_f32(gb);
const uint16_t ab = f32_to_bf16(1.0f / (1.0f + expf(-gv)));
return f32_to_bf16(bf16_to_f32(ab) * bf16_to_f32(xb));
}
__device__ __forceinline__ float r4d_gq_clamp(float v) { return fminf(fmaxf(v, -448.0f), 448.0f); }
// The plain decode kernel, and the chained one (r4dchain*, r4d_chain.h) over the same body.
template<int NWARPS, int TILE, int HEAD_DIM, int GQA, int BS, int KVP, int OPT,
int WIN = 0>
__global__ __launch_bounds__(NWARPS * 32)
void r4d_attn_decode_kernel(const R4DArgs a, int splits)
{
r4d_attn_decode_body<NWARPS, TILE, HEAD_DIM, GQA, BS, KVP, OPT, WIN>(a, splits);
}
template<int NWARPS, int TILE, int HEAD_DIM, int GQA, int BS, int KVP, int OPT,
int WIN = 0>
__global__ __launch_bounds__(NWARPS * 32)
void r4dchain_attn_decode(const R4DArgs a, int splits)
{
r4d_chain_wait();
r4d_attn_decode_body<NWARPS, TILE, HEAD_DIM, GQA, BS, KVP, OPT, WIN>(a, splits);
r4d_chain_done();
}
// Vectorised merge, used by the h256 launcher whenever the split count fits a wave. Bit-identical
// to splitkv_combine_body below; only the thread map differs. One wave owns one output row and
// each lane HEAD_DIM/32 dims, so a segment is one 16-byte load per lane; a block carries JPB rows.
// No barrier and no LDS, so rows in one block may have different contexts and segment counts.
template<int HEAD_DIM, int CHAINS, int PF16, int JPB, int WIN, int GQ>
__device__ __forceinline__
void splitkv_combine_vec_body(const R4DArgs& a, int splits, int tile, const R4DGateQuant& gq)
{
constexpr int DPL = HEAD_DIM / 32; // dims a lane owns
const int lane = threadIdx.x & 31;
const int job = blockIdx.x * JPB + (int)(threadIdx.x >> 5);
if (job >= r4d_q_total(a) * a.q_heads) return;
const int tok = job / a.q_heads;
const int seq = r4d_q_seq(a.cu_q, a.num_seqs, a.q_len, tok);
const int ctx = a.seqused_k[seq];
uint16_t* outp = (uint16_t*)a.out + (size_t)job * HEAD_DIM + lane * DPL;
/* Plain store, or gated + quantised. A lane's DPL columns lie in one 128-column group of
* 128 / DPL lanes, so the group absmax is a butterfly over those lanes only. */
auto finish = [&](const uint16_t (&ob)[DPL]) {
if constexpr (!GQ) {
union { uint4 u4; uint2 u2; uint16_t w[DPL]; } o_;
#pragma unroll
for (int j = 0; j < DPL; ++j) o_.w[j] = ob[j];
if (DPL == 8) *(uint4*)outp = o_.u4; else *(uint2*)outp = o_.u2;
} else {
constexpr int LPG = kGqGroup / DPL; // lanes a scale group
static_assert(DPL == 8 || DPL == 4, "a lane's columns are one or two dwords of codes");
const uint16_t* gp = gq.g + (size_t)job * gq.g_ld + lane * DPL;
union { uint4 u4; uint2 u2; uint16_t w[DPL]; } gi, o_;
if (DPL == 8) gi.u4 = *(const uint4*)gp; else gi.u2 = *(const uint2*)gp;
float v[DPL];
float m = 0.0f;
#pragma unroll
for (int j = 0; j < DPL; ++j) {
o_.w[j] = r4d_gq_gate(gi.w[j], ob[j]);
v[j] = bf16_to_f32(o_.w[j]);
m = fmaxf(m, fabsf(v[j]));
}
if (DPL == 8) *(uint4*)outp = o_.u4; else *(uint2*)outp = o_.u2;
if constexpr (LPG > 16) m = fmaxf(m, r4d_xor_lane<16>(m));
if constexpr (LPG > 8) m = fmaxf(m, r4d_xor_lane<8>(m));
m = fmaxf(m, r4d_xor_lane<4>(m));
m = fmaxf(m, r4d_xor_lane<2>(m));
m = fmaxf(m, r4d_xor_lane<1>(m));
const float sc = m > 0.0f ? m * (1.0f / q8_top<GQ == 2>()) : 1.0f;
const float inv = 1.0f / sc;
if ((lane % LPG) == 0) gq.s[(size_t)job * gq.s_ld + (lane * DPL) / kGqGroup] = sc;
uint32_t pk[DPL / 4];
#pragma unroll
for (int w = 0; w < DPL / 4; ++w)
pk[w] = q8_pack4<GQ == 2>(v[4 * w + 0], v[4 * w + 1], v[4 * w + 2], v[4 * w + 3],
inv);
uint8_t* qp = gq.q + (size_t)job * gq.q_ld + lane * DPL;
if (DPL == 8) *(uint2*)qp = make_uint2(pk[0], pk[DPL / 4 - 1]);
else *(uint32_t*)qp = pk[0];
}
};
if (ctx <= 0) { // padded slot: zeros, read nothing
uint16_t ob[DPL];
#pragma unroll
for (int i = 0; i < DPL; ++i) ob[i] = 0;
finish(ob);
return;
}
const int nef = r4d_attn_tiles(ctx, tile)
- (WIN ? r4d_attn_wlo(ctx, r4d_q_len(a.cu_q, seq, a.q_len), 0, a.window) / tile : 0);
const int tp_ = (nef + splits - 1) / splits;
const int u = (nef + tp_ - 1) / tp_;
const size_t nrow = (size_t)r4d_q_total(a) * a.q_heads * splits;
const size_t base = (size_t)job * splits;
const float* sm; const float* sl;
if (PF16) { const uint16_t* so = (const uint16_t*)a.scratch;
sm = (const float*)(so + nrow * HEAD_DIM); sl = sm + nrow; }
else { const float* so = (const float*)a.scratch;
sm = so + nrow * HEAD_DIM; sl = sm + nrow; }
// max is order-independent and wave-uniform, so it is a scalar walk. The sum reproduces the
// LDS tree's association (stride-32 pre-accumulation, then xor butterfly 16..1) so results
// match the non-vector merge bit for bit.
float gmax = -INFINITY;
for (int s = 0; s < u; ++s) gmax = fmaxf(gmax, sm[base + s]);
float tot = 0.0f;
for (int s = lane; s < u; s += 32) {
const float mm = sm[base + s];
const float w = (mm <= -3.0e38f) ? 0.0f : ex2(mm - gmax);
tot += w * sl[base + s];
}
tot = r4d_wave_sum(tot);
float ch[CHAINS][DPL];
#pragma unroll
for (int i = 0; i < CHAINS; ++i)
#pragma unroll
for (int j = 0; j < DPL; ++j) ch[i][j] = 0.0f;
auto wgt = [&](int sx) {
const float mm = sm[base + sx];
const float w = (mm <= -3.0e38f) ? 0.0f : ex2(mm - gmax);
return PF16 ? w * sl[base + sx] : w;
};
int s = 0;
if (PF16) {
const uint16_t* op = (const uint16_t*)a.scratch + base * HEAD_DIM + lane * DPL;
// One aligned access per lane per segment: 16 bytes at head_dim 256, 8 at 128.
union { uint4 u4; uint2 u2; __fp16 h[DPL]; } r;
auto rd = [&](int sx) {
if (DPL == 8) r.u4 = *(const uint4*)(op + (size_t)sx * HEAD_DIM);
else r.u2 = *(const uint2*)(op + (size_t)sx * HEAD_DIM);
};
for (; s + CHAINS <= u; s += CHAINS) {
#pragma unroll
for (int i = 0; i < CHAINS; ++i) {
rd(s + i);
const float w = wgt(s + i);
#pragma unroll
for (int j = 0; j < DPL; ++j) ch[i][j] = fmaf(w, (float)r.h[j], ch[i][j]);
}
}
for (; s < u; ++s) {
rd(s);
const float w = wgt(s);
#pragma unroll
for (int j = 0; j < DPL; ++j) ch[0][j] = fmaf(w, (float)r.h[j], ch[0][j]);
}
} else {
const float* op = (const float*)a.scratch + base * HEAD_DIM + lane * DPL;
for (; s + CHAINS <= u; s += CHAINS) {
#pragma unroll
for (int i = 0; i < CHAINS; ++i) {
const float* p = op + (size_t)(s + i) * HEAD_DIM;
const float w = wgt(s + i);
#pragma unroll
for (int j = 0; j < DPL; ++j) ch[i][j] = fmaf(w, p[j], ch[i][j]);
}
}
for (; s < u; ++s) {
const float* p = op + (size_t)s * HEAD_DIM;
const float w = wgt(s);
#pragma unroll
for (int j = 0; j < DPL; ++j) ch[0][j] = fmaf(w, p[j], ch[0][j]);
}
}
const int kvh = (job % a.q_heads) / (a.q_heads / a.kv_heads); /* the query group */
const int krep = a.kv_rep > 1 ? a.kv_rep : 1; /* its cache head: kvh / krep */
const float vd = a.v_descale ? a.v_descale[seq * (a.kv_heads / krep) + kvh / krep] : 1.0f;
uint16_t ob[DPL];
#pragma unroll
for (int j = 0; j < DPL; ++j) {
float av = 0.0f;
#pragma unroll
for (int i = 0; i < CHAINS; ++i) av += ch[i][j];
// (av * vd) / tot in this order; folding vd/tot first rounds differently (one bf16 ULP).
ob[j] = f32_to_bf16(tot > 0.0f ? av * vd / tot : 0.0f);
}
finish(ob);
}
template<int HEAD_DIM, int CHAINS, int PF16, int JPB, int WIN = 0>
__global__ __launch_bounds__(32 * JPB)
void r4d_attn_splitkv_combine_vec_kernel(const R4DArgs a, int splits, int tile)
{
splitkv_combine_vec_body<HEAD_DIM, CHAINS, PF16, JPB, WIN, 0>(a, splits, tile,
R4DGateQuant{});
}
template<int HEAD_DIM, int CHAINS, int PF16, int JPB, int WIN = 0>
__global__ __launch_bounds__(32 * JPB)
void r4dchain_attn_splitkv_combine_vec(const R4DArgs a, int splits, int tile)
{
r4d_chain_wait();
splitkv_combine_vec_body<HEAD_DIM, CHAINS, PF16, JPB, WIN, 0>(a, splits, tile,
R4DGateQuant{});
r4d_chain_done();
}
template<int HEAD_DIM, int CHAINS, int PF16, int JPB, int WIN = 0, int GQ = 1>
__global__ __launch_bounds__(32 * JPB)
void r4d_attn_splitkv_combine_vec_gq_kernel(const R4DArgs a, int splits, int tile,
const R4DGateQuant gq)
{
splitkv_combine_vec_body<HEAD_DIM, CHAINS, PF16, JPB, WIN, GQ>(a, splits, tile, gq);
}
// Merge. PF16=1 reads normalised f16 partials and folds l into the weight; PF16=0 reads
// unnormalised f32 partials with l summed separately. Both walk segments in fixed index order so
// rounding never depends on workgroup scheduling.
template<int HEAD_DIM, int CHAINS, int PF16, int WIN, int GQ>
__device__ __forceinline__
void splitkv_combine_body(const R4DArgs& a, int splits, int tile, const R4DGateQuant& gq)
{
extern __shared__ float sw[];
__shared__ float red[HEAD_DIM];
const int job = blockIdx.x, d = threadIdx.x;
const int tok = job / a.q_heads;
const int seq = r4d_q_seq(a.cu_q, a.num_seqs, a.q_len, tok);
const int ctx = a.seqused_k[seq];
/* One thread per column, so a 128-column scale group is four waves, combined through `red`.
* Reusing `red` is safe: its last read (the total) is behind a barrier before the merge loop,
* and a padded slot never used it. */
auto finish = [&](uint16_t ob) {
uint16_t* op = (uint16_t*)a.out + (size_t)job * HEAD_DIM + d;
if constexpr (!GQ) {
*op = ob;
} else {
constexpr int WPG = kGqGroup / 32; // waves a scale group
const uint16_t h = r4d_gq_gate(gq.g[(size_t)job * gq.g_ld + d], ob);
*op = h;
const float v = bf16_to_f32(h);
const float m = r4d_wave_max(fabsf(v));
if ((d & 31) == 0) red[d / 32] = m;
__syncthreads();
const int g0 = (d / kGqGroup) * WPG;
float gm = red[g0];
#pragma unroll
for (int w = 1; w < WPG; ++w) gm = fmaxf(gm, red[g0 + w]);
const float sc = gm > 0.0f ? gm * (1.0f / q8_top<GQ == 2>()) : 1.0f;
const float inv = 1.0f / sc;
if ((d % kGqGroup) == 0) gq.s[(size_t)job * gq.s_ld + d / kGqGroup] = sc;
if constexpr (GQ == 2) {
gq.q[(size_t)job * gq.q_ld + d] =
(uint8_t)(int)rintf(fminf(fmaxf(v * inv, -127.0f), 127.0f));
} else {
const int p = __builtin_amdgcn_cvt_pk_fp8_f32(r4d_gq_clamp(v * inv), 0.0f, 0, false);
gq.q[(size_t)job * gq.q_ld + d] = (uint8_t)(p & 0xff);
}
}
};
// A padded slot (ctx 0) would divide by zero below; write zeros and stop. ctx is
// block-uniform, so the barriers below stay matched.
if (ctx <= 0) { finish(0); return; }
// Segment count, derived exactly as in the decode kernel, including the window's tile floor;
// otherwise the merge would read partials no segment wrote.
const int nef = r4d_attn_tiles(ctx, tile)
- (WIN ? r4d_attn_wlo(ctx, r4d_q_len(a.cu_q, seq, a.q_len), 0, a.window) / tile : 0);
const int tp_ = (nef + splits - 1) / splits;
const int u = (nef + tp_ - 1) / tp_;
const size_t nrow = (size_t)r4d_q_total(a) * a.q_heads * splits;
const size_t base = (size_t)job * splits;
const float* sm; const float* sl;
if (PF16) { const uint16_t* so = (const uint16_t*)a.scratch;
sm = (const float*)(so + nrow * HEAD_DIM); sl = sm + nrow; }
else { const float* so = (const float*)a.scratch;
sm = so + nrow * HEAD_DIM; sl = sm + nrow; }
float gmax = -INFINITY;
for (int s = d; s < u; s += HEAD_DIM) gmax = fmaxf(gmax, sm[base + s]);
red[d] = gmax; __syncthreads();
for (int off = HEAD_DIM / 2; off; off >>= 1) {
if (d < off) red[d] = fmaxf(red[d], red[d + off]);
__syncthreads();
}
gmax = red[0]; __syncthreads();
float tot = 0.0f;
for (int s = d; s < u; s += HEAD_DIM) {
const float mm = sm[base + s];
float w = (mm <= -3.0e38f) ? 0.0f : ex2(mm - gmax);
if (PF16) { w *= sl[base + s]; sw[s] = w; tot += w; }
else { sw[s] = w; tot += w * sl[base + s]; }
}
red[d] = tot; __syncthreads();
for (int off = HEAD_DIM / 2; off; off >>= 1) {
if (d < off) red[d] += red[d + off];
__syncthreads();
}
tot = red[0]; __syncthreads();
float ch[CHAINS];
#pragma unroll
for (int i = 0; i < CHAINS; ++i) ch[i] = 0.0f;
int s = 0;
if (PF16) {
const uint16_t* op = (const uint16_t*)a.scratch + base * HEAD_DIM + d;
for (; s + CHAINS <= u; s += CHAINS) {
#pragma unroll
for (int i = 0; i < CHAINS; ++i)
ch[i] = fmaf(sw[s + i], (float)__builtin_bit_cast(
__fp16, op[(size_t)(s + i) * HEAD_DIM]), ch[i]);
}
for (; s < u; ++s)
ch[0] = fmaf(sw[s], (float)__builtin_bit_cast(__fp16, op[(size_t)s * HEAD_DIM]), ch[0]);
} else {
const float* op = (const float*)a.scratch + base * HEAD_DIM + d;
for (; s + CHAINS <= u; s += CHAINS) {
#pragma unroll
for (int i = 0; i < CHAINS; ++i)
ch[i] = fmaf(sw[s + i], op[(size_t)(s + i) * HEAD_DIM], ch[i]);
}
for (; s < u; ++s) ch[0] = fmaf(sw[s], op[(size_t)s * HEAD_DIM], ch[0]);
}
float av = 0.0f;
#pragma unroll
for (int i = 0; i < CHAINS; ++i) av += ch[i];
const int kvh = (job % a.q_heads) / (a.q_heads / a.kv_heads); /* the query group */
const int krep = a.kv_rep > 1 ? a.kv_rep : 1; /* its cache head: kvh / krep */
const float vd = a.v_descale ? a.v_descale[seq * (a.kv_heads / krep) + kvh / krep] : 1.0f;
finish(f32_to_bf16(tot > 0.0f ? av * vd / tot : 0.0f));
}
template<int HEAD_DIM, int CHAINS, int PF16, int WIN = 0>
__global__ __launch_bounds__(HEAD_DIM)
void r4d_attn_splitkv_combine_kernel(const R4DArgs a, int splits, int tile)
{
splitkv_combine_body<HEAD_DIM, CHAINS, PF16, WIN, 0>(a, splits, tile, R4DGateQuant{});
}
template<int HEAD_DIM, int CHAINS, int PF16, int WIN = 0>
__global__ __launch_bounds__(HEAD_DIM)
void r4dchain_attn_splitkv_combine(const R4DArgs a, int splits, int tile)
{
r4d_chain_wait();
splitkv_combine_body<HEAD_DIM, CHAINS, PF16, WIN, 0>(a, splits, tile, R4DGateQuant{});
r4d_chain_done();
}
template<int HEAD_DIM, int CHAINS, int PF16, int WIN = 0, int GQ = 1>
__global__ __launch_bounds__(HEAD_DIM)
void r4d_attn_splitkv_combine_gq_kernel(const R4DArgs a, int splits, int tile,
const R4DGateQuant gq)
{
splitkv_combine_body<HEAD_DIM, CHAINS, PF16, WIN, GQ>(a, splits, tile, gq);
}
// ---- host helpers shared by the paged decode dispatches -----------------------------------------
// Split law of the full-attention legs (h256/gqa6 and the gqa8 family): as many workgroups as fit
// under 128 (grid = splits * kv_heads * num_seqs), at least two tiles per segment. It does not
// depend on gqa or head_dim.
//
// floor(128 / units), not a power of two: a power of two leaves 96-112 workgroups at 3, 5, 6 and
// 7 requests, and floor is 2-20% faster there. Staying under a multiple of 64 matters (at five
// requests, 13 splits = 130 workgroups is three rounds of 64 and slower than 12).
//
// Fewer splits mainly save partial-buffer traffic (seqs x q_len x q_heads x splits rows of
// head_dim f16, written once and read once): at eight requests 12.8 MB at splits 32 vs 3.2 MB at
// 8, 19% of a 16K step's KV but 1.2% at 128K. The effect fades with depth, never enough to need a
// context term.
static inline int r4d_decode_splits(const R4DArgs* a, int tile) {
if (a->splits > 0) return a->splits;
const int tiles = (a->max_ctx + tile - 1) / tile;
const int units = std::max(1, a->kv_heads * a->num_seqs);
int s = 128 / units;
const int cap = tiles / 2;
if (s > cap) s = cap;
if (s > tiles) s = tiles;
if (s < 1) s = 1;
return s;
}
// Split law of the windowed legs (the DFlash2 drafter at h128/gqa4, Gemma 4 at h256/gqa2): target
// 192 workgroups, round down to a power of two (min 16), keep at least two tiles per segment, then
// step up off splits * units == 256, a slow spot for these geometries. The tile count comes from
// the window when one is set, since that is the span the kernel walks.
static inline int r4d_decode_splits_win(const R4DArgs* a, int tile) {
if (a->splits > 0) return a->splits;
const int span = (a->window > 0 && a->window + a->q_len < a->max_ctx)
? a->window + a->q_len : a->max_ctx;
const int tiles = (span + tile - 1) / tile;
const int units = std::max(1, a->kv_heads * a->num_seqs);
const int raw = 192 / units;
int s = 128;
while (s > 16 && s > raw) s >>= 1;
const int cap = tiles / 2;
while (s > 16 && s > cap) s >>= 1;
if (s > tiles) s = tiles;
if (s < 1) s = 1;
if (s * units == 256 && s * 2 <= tiles) s <<= 1;
return s;
}
// Bytes of split-KV partials one decode launch needs, the same for both cache dtypes: f16 O/l plus
// m and l under O_PF16, else f32 O plus m and l.
static inline long r4d_decode_scratch(const R4DArgs* a, int splits, int opt) {
const long rows = (long)r4d_q_total(*a) * a->q_heads;
return (opt & O_PF16) ? rows * splits * (long)(a->head_dim * 2 + 8)
: rows * splits * (long)(a->head_dim + 2) * 4L;
}
// The windowed legs' decode (WIN = 1): the attention kernel at the gqa-aligned height, then the
// merge, one wave per row when there are enough rows and the segment count fits a wave, else the
// LDS-tree form; both give identical bytes. Tile 16, paged block 16, at most 64 query rows, and
// the full-attention legs' merge constants (two rows a block, the wave form from 256 rows).
// Returns 0, a negative code for a shape it does not serve, or -100 - hipError_t.
template <int HEAD_DIM, int GQA, int OPT, int KVP>
static int r4d_decode_launch_win(const R4DArgs* a, hipStream_t s) {
constexpr int TILE = 16, BS = 16, JPB = 2, VECMIN = 256, PF16 = (OPT & O_PF16) ? 1 : 0;
if (a->head_dim != HEAD_DIM || a->block_size != BS) return -1;
if (a->q_heads / a->kv_heads != GQA) return -2;
const int rows = a->q_len * GQA;
if (rows > 64) return -4;
const int nw = (rows + 15) / 16;
const int splits = r4d_decode_splits_win(a, TILE);
#define R4D_WIN_LAUNCH(NW) do { if (a->chain) \
hipLaunchKernelGGL((r4dchain_attn_decode<NW, TILE, HEAD_DIM, GQA, BS, KVP, OPT, 1>), \
dim3(splits, a->kv_heads, a->num_seqs), dim3(NW * 32), 0, s, *a, splits); \
else \
hipLaunchKernelGGL((r4d_attn_decode_kernel<NW, TILE, HEAD_DIM, GQA, BS, KVP, OPT, 1>), \
dim3(splits, a->kv_heads, a->num_seqs), dim3(NW * 32), 0, s, *a, splits); \
} while (0)
if (nw == 1) R4D_WIN_LAUNCH(1);
else if (nw == 2) R4D_WIN_LAUNCH(2);
else if (nw == 3) R4D_WIN_LAUNCH(3);
else R4D_WIN_LAUNCH(4);
#undef R4D_WIN_LAUNCH
hipError_t e = hipGetLastError();
if (e != hipSuccess) return -100 - (int)e;
const int jobs3 = r4d_q_total(*a) * a->q_heads;
if (splits <= 64 && jobs3 >= VECMIN && a->chain)
hipLaunchKernelGGL((r4dchain_attn_splitkv_combine_vec<HEAD_DIM, 4, PF16, JPB, 1>),
dim3((jobs3 + JPB - 1) / JPB), dim3(32 * JPB), 0, s, *a, splits, TILE);
else if (splits <= 64 && jobs3 >= VECMIN)
hipLaunchKernelGGL((r4d_attn_splitkv_combine_vec_kernel<HEAD_DIM, 4, PF16, JPB, 1>),
dim3((jobs3 + JPB - 1) / JPB), dim3(32 * JPB), 0, s, *a, splits, TILE);
else if (a->chain)
hipLaunchKernelGGL((r4dchain_attn_splitkv_combine<HEAD_DIM, 4, PF16, 1>), dim3(jobs3),
dim3(HEAD_DIM), splits * sizeof(float), s, *a, splits, TILE);
else
hipLaunchKernelGGL((r4d_attn_splitkv_combine_kernel<HEAD_DIM, 4, PF16, 1>), dim3(jobs3),
dim3(HEAD_DIM), splits * sizeof(float), s, *a, splits, TILE);
e = hipGetLastError();
return e == hipSuccess ? 0 : -100 - (int)e;
}