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/tests/r4d_emu/test_attn.cpp
T
Deadcode b93b531f63 radiance 1.5.0
- Gemma 4 31B (PRs #74, #75, #81, #86; sfjes, keptn42): sliding and global attention with their
  own kernels, the assistant head as a drafter that reads the trunk's cache, the vision tower, a
  fused w4a8 decode step, bf16 and compressed-tensors int4 checkpoints, and window snapshots so a
  prefix hit on a sliding-window model resumes instead of prefilling again.
  deploy/compose/gemma4-31b-w4a8.yaml serves keptn42's container on two cards
- ParoQuant (PRs #65, #66, #67; malic): Qwen3.8-27B checkpoints with learned Givens rotations,
  as int4, int5 and MXFP4, imported without re-quantising, the prefill's rotations on the matrix
  cores. deploy/compose/qwen3.8-27b-paro.yaml serves them on one card. A ParoQuant container made
  with the PR builds is refused: convert it again with --reuse <old container>
- Gemma 4 26B-A4B with z-lab's DFlash drafter (PR #88; Perire): arch/gemma4 serves Google's bf16
  checkpoint with the routed experts in block fp8 or bf16 (data/recipes/gemma4-26b-a4b-df*.recipe),
  and libr4d gains its windowed paged attention, the fused sandwich-norm joins, expert_scale and
  the sharded draft head's pair_argmax. A prefill chunk runs as two slices with its all-reduces
  beside the compute, bit-exact with the whole step. The 31B's global layers take the windowed
  attention too: an 11.5K-token prefill on two R9700s in 3.1 s instead of 4.9
- a startup that logs nothing for two minutes says where it waits (#89): the system call (a KFD
  request by name), the library that made it, and its stack, which addr2line resolves against the
  binary
- the prefix cache is kept in VRAM and host memory only (#70): the disk tier wrote every block and
  snapshot a prompt produced, and its copies never outlived the process. --prefix-cache-host-mib
  is renamed --prefix-cache-mib; --prefix-cache-dir and --prefix-cache-disk-mib are removed, and
  startup refuses all three with what replaced them. The compose files no longer mount /data, and
  .env no longer needs RADIANCE_STATE
- --prefix-cache-ttl S (default 3600, 0 never): a conversation no request has used for that long
  leaves the cache, whichever tier holds it; radiance:prefix_cache_expired_total counts them. A
  full host tier now drops its least recently used conversations
- --vision-tower-placement vram|ram (#84): ram keeps an encoder tower in pinned host memory and
  gives its VRAM to the KV cache, read over the link once an image
- timings carry ttft_ms and total_ms (#85): the server's own time from the request body's arrival
  to the first and the last token, queueing and tokenisation included
- a KV-cache recall that found a moved expert with nowhere to go stopped halfway and leaked the
  slots it had already freed: after it every recall failed, the cache stayed short until a
  restart, and requests failed with HTTP 500. A recall now waits for the slots it is owed and
  always finishes
- a tool result or tool-call arguments that are not JSON are no longer refused as nesting past
  1024 levels for their unmatched brackets (#90): only text that parses as JSON is held to the
  bound, since nothing else is parsed into a tree
- the host-pool refusal names the limit that applies to pinned memory (RAM less 1/64 and
  1.5 GiB), not ttm.pages_limit
- --embedding-placement auto, removed in 1.3.0, is refused with what replaced it
- for out-of-tree kernel libraries: gemm_nt_q gains two operands (pb1, pb2, the part bounds), and
  the loader requires identical op schemas, so a library built against 1.4.0 must be rebuilt. For
  architecture plugins, rad_op_declared(b, op) asks whether an op is in the loaded vocabulary
  without declaring it
2026-10-13 19:35:16 +02:00

251 lines
15 KiB
C++

/* Drives libr4d's windowed attn_paged entry points (r4d_attn_paged_window.hip) through the SIMT
* emulator and checks every output element against a port of libref's ref_attn_paged (same mask
* predicates, f64 accumulation). */
#include "rad_abi.h"
#include "rad_plugin.h"
#include "r4d_plugin.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <random>
#include <string>
#include <vector>
static std::mt19937 rng(1234);
extern int emu_grids;
extern int emu_last_block; /* threads a block of the last grid: 512 is the WMMA kernel */
static float nrm() { static std::normal_distribution<float> d(0.f, 1.f); return d(rng); }
static RadTensor tens(void* p, uint32_t dt, std::vector<int64_t> shape) {
RadTensor t{}; t.data = p; t.dtype = dt; t.rank = (uint32_t)shape.size();
int64_t st = 1;
for (int i = (int)shape.size() - 1; i >= 0; --i) { t.shape[i] = shape[i]; t.stride[i] = st; st *= shape[i]; }
return t;
}
struct Case {
std::string name;
int hd, gqa, kvh;
bool fp8, causal, ragged, descale;
int window;
std::vector<int> qlen, ctx; /* per sequence */
int missing_block = -1; /* sequence 0's block index to mark -1, or -1 */
long long max_ctx = 0; /* 0: not passed */
int64_t scratch = 0; /* 0: no scratch */
int qpitch_extra = 0; /* q as a column slice of a wider buffer */
int expect_wmma = -1; /* 1: must take the WMMA kernel, 0: must not, -1: either */
};
using Fn = int (*)(const RadArgs*, RadStream);
static Fn pick(int hd, int gqa, bool fp8) {
#define P(H, G) if (hd == H && gqa == G) return fp8 ? r4d_rad_attn_window_h##H##_gqa##G##_fp8 : r4d_rad_attn_window_h##H##_gqa##G##_bf16;
P(256, 1) P(256, 2) P(256, 4) P(256, 8) P(512, 1) P(512, 2) P(512, 4) P(512, 8)
#undef P
return nullptr;
}
static int run(const Case& c) {
const int n_seq = (int)c.qlen.size(), HD = c.hd, kvh = c.kvh, qh = kvh * c.gqa;
int rows = 0; for (int q : c.qlen) rows += q;
/* blocks: each sequence gets ceil(ctx/16) blocks at shuffled ids */
int maxb = 0; for (int x : c.ctx) maxb = std::max(maxb, (x + 15) / 16);
maxb += 1; /* a spare column: the table is wider than any sequence needs */
const int nblocks = n_seq * maxb + 3;
std::vector<int> ids(nblocks); for (int i = 0; i < nblocks; ++i) ids[i] = i;
std::shuffle(ids.begin(), ids.end(), rng);
std::vector<int> bt(n_seq * maxb, -1);
int nx = 0;
for (int s = 0; s < n_seq; ++s) for (int b = 0; b < (c.ctx[s] + 15) / 16; ++b) bt[s * maxb + b] = ids[nx++];
if (c.missing_block >= 0) bt[c.missing_block] = -1;
/* the cache, filled everywhere -- including slots past each context, with NaN in some, so a
* kernel that reads a masked key's V without guarding is caught */
const int64_t slot = 2 * HD, nel = (int64_t)nblocks * kvh * 16 * slot;
std::vector<float> kvf(nel);
std::vector<uint16_t> kvb(nel); std::vector<uint8_t> kv8(nel);
for (int64_t i = 0; i < nel; ++i) {
float v = nrm();
if (c.fp8) { kv8[i] = rad_f32_to_fp8e4m3(v * 4.f); kvf[i] = rad_fp8e4m3_to_f32(kv8[i]); }
else { kvb[i] = rad_f32_to_bf16(v); kvf[i] = rad_bf16_to_f32(kvb[i]); }
}
/* poison: every slot at or past a sequence's context in its last block, and every block no
* table names */
std::vector<char> named(nblocks, 0);
for (int s = 0; s < n_seq; ++s) for (int b = 0; b < maxb; ++b) if (bt[s * maxb + b] >= 0) named[bt[s * maxb + b]] = 1;
auto poison = [&](int64_t i) { if (c.fp8) { kv8[i] = 0x7f; } else { kvb[i] = 0x7fc0; } kvf[i] = NAN; };
for (int blk = 0; blk < nblocks; ++blk) if (!named[blk]) for (int64_t i = 0; i < kvh * 16 * slot; ++i) poison((int64_t)blk * kvh * 16 * slot + i);
for (int s = 0; s < n_seq; ++s) {
const int last = (c.ctx[s] - 1) / 16, blk = bt[s * maxb + last];
if (blk < 0) continue;
for (int o = c.ctx[s] % 16 ? c.ctx[s] % 16 : 16; o < 16; ++o)
for (int h = 0; h < kvh; ++h) for (int d = 0; d < slot; ++d) poison((((int64_t)blk * kvh + h) * 16 + o) * slot + d);
}
const int qcols = qh * HD + c.qpitch_extra;
std::vector<uint16_t> qb((size_t)rows * qcols); std::vector<float> qf(qb.size());
for (size_t i = 0; i < qb.size(); ++i) { qb[i] = rad_f32_to_bf16(nrm()); qf[i] = rad_bf16_to_f32(qb[i]); }
std::vector<int> su(c.ctx), cu(n_seq + 1, 0);
for (int s = 0; s < n_seq; ++s) cu[s + 1] = cu[s] + c.qlen[s];
std::vector<float> kd(n_seq * kvh, 1.f), vd(n_seq * kvh, 1.f);
if (c.descale) for (auto* v : {&kd, &vd}) for (auto& x : *v) x = 0.5f + (float)(rng() % 100) / 100.f;
std::vector<uint16_t> out((size_t)rows * qh * HD, 0xffff);
std::vector<char> scratch(c.scratch + 64);
char* sp = scratch.data() + ((16 - ((uintptr_t)scratch.data() & 15)) & 15);
RadTensor t[8];
t[0] = tens(qb.data(), RAD_BF16, {rows, qh * HD}); t[0].stride[0] = qcols;
t[1] = tens(c.fp8 ? (void*)kv8.data() : (void*)kvb.data(), c.fp8 ? RAD_F8E4M3 : RAD_BF16, {nblocks, kvh, 16, slot});
t[2] = tens(bt.data(), RAD_I32, {n_seq, maxb});
t[3] = tens(su.data(), RAD_I32, {n_seq});
t[4] = c.descale ? tens(kd.data(), RAD_F32, {n_seq, kvh}) : RadTensor{};
t[5] = c.descale ? tens(vd.data(), RAD_F32, {n_seq, kvh}) : RadTensor{};
t[6] = c.ragged ? tens(cu.data(), RAD_I32, {n_seq + 1}) : RadTensor{};
t[7] = tens(out.data(), RAD_BF16, {rows, qh, HD});
int qmax = 0; for (int q : c.qlen) qmax = std::max(qmax, q);
std::vector<RadParam> p;
auto pi = [&](const char* k, long long v) { RadParam x{}; x.key = k; x.kind = RAD_P_INT; x.ival = v; p.push_back(x); };
auto ps = [&](const char* k, const char* v) { RadParam x{}; x.key = k; x.kind = RAD_P_STR; x.sval = v; p.push_back(x); };
auto pf = [&](const char* k, double v) { RadParam x{}; x.key = k; x.kind = RAD_P_F64; x.dval = v; p.push_back(x); };
pi("q_len", qmax + 3); /* the band bound, above the step's max */
pi("head_dim", HD); pi("gqa", c.gqa); pi("block_size", 16); pi("causal", c.causal); pi("window", c.window);
ps("q_dtype", "bf16"); ps("kv_dtype", c.fp8 ? "fp8_e4m3" : "bf16");
const double scale = 1.0 / std::sqrt((double)HD) * 1.3; /* not the default: the param must be read */
pf("scale", scale); pi("n_head", qh); pi("max_seqs", n_seq);
if (c.max_ctx) pi("max_ctx", c.max_ctx);
RadArgs a{}; a.t = t; a.n_t = 8; a.p = p.data(); a.n_p = (int)p.size();
a.scratch = c.scratch ? sp : nullptr; a.scratch_bytes = c.scratch;
Fn fn = pick(HD, c.gqa, c.fp8);
const int g0 = emu_grids;
const int rc = fn(&a, nullptr);
const int launches = emu_grids - g0;
if (rc != RAD_OK) { std::printf("%-44s launch returned %d\n", c.name.c_str(), rc); return 1; }
/* the reference */
double max_err = 0, max_ref = 0, se = 0, sr = 0; long bad = 0, nan_out = 0;
for (int s = 0; s < n_seq; ++s) for (int i = 0; i < c.qlen[s]; ++i) for (int h = 0; h < qh; ++h) {
const int tok = cu[s] + i, kh = h / c.gqa, pos = c.ctx[s] - c.qlen[s] + i;
auto visible = [&](int j) {
if (c.causal ? j > pos : j >= c.ctx[s]) return false;
if (c.window > 0) { const int d = pos - j; if (c.causal ? d >= c.window : (d >= c.window || d <= -c.window)) return false; }
return bt[s * maxb + j / 16] >= 0;
};
std::vector<double> sc(c.ctx[s], -INFINITY); double mx = -INFINITY;
for (int j = 0; j < c.ctx[s]; ++j) {
if (!visible(j)) continue;
const int64_t kb = (((int64_t)bt[s * maxb + j / 16] * kvh + kh) * 16 + j % 16) * slot;
double dot = 0; for (int d = 0; d < HD; ++d) dot += (double)qf[(size_t)tok * qcols + h * HD + d] * kvf[kb + d] * kd[s * kvh + kh];
sc[j] = dot * scale; mx = std::max(mx, sc[j]);
}
std::vector<double> acc(HD, 0.0); double sum = 0;
if (mx > -INFINITY)
for (int j = 0; j < c.ctx[s]; ++j) {
if (sc[j] == -INFINITY) continue;
const double w = std::exp(sc[j] - mx); sum += w;
const int64_t kb = (((int64_t)bt[s * maxb + j / 16] * kvh + kh) * 16 + j % 16) * slot;
for (int d = 0; d < HD; ++d) acc[d] += w * kvf[kb + HD + d] * vd[s * kvh + kh];
}
for (int d = 0; d < HD; ++d) {
const double ref = sum > 0 ? acc[d] / sum : 0.0;
const float got = rad_bf16_to_f32(out[((size_t)tok * qh + h) * HD + d]);
if (std::isnan(got)) { ++nan_out; continue; }
const double e = std::fabs(got - ref);
max_err = std::max(max_err, e); max_ref = std::max(max_ref, std::fabs(ref));
se += e * e; sr += ref * ref;
if (e > 1e-2 * std::max(1.0, std::fabs(ref)) + 4e-3 * std::fabs(ref)) ++bad;
}
}
const double rel = std::sqrt(se / std::max(sr, 1e-30));
const bool ok = bad == 0 && nan_out == 0 && rel < 5e-3;
std::printf("%-44s rows %4d %s rel_l2 %.2e max_abs %.2e bad %ld nan %ld %s\n", c.name.c_str(), rows,
launches > 1 ? "split" : emu_last_block == 512 ? "wmma " : " ", rel, max_err, bad, nan_out,
ok ? "PASS" : "FAIL");
if (c.expect_wmma >= 0 && (emu_last_block == 512) != (c.expect_wmma == 1)) {
std::printf("%-44s took the %s kernel, expected the %s one\n", c.name.c_str(),
emu_last_block == 512 ? "WMMA" : "scalar", c.expect_wmma ? "WMMA" : "scalar");
return 1;
}
return ok ? 0 : 1;
}
int main(int argc, char** argv) {
const int64_t s256 = r4d_rad_attn_window_h256_scratch(nullptr), s512 = r4d_rad_attn_window_h512_scratch(nullptr);
std::vector<Case> cases = {
/* Gemma 4 sliding: h256 gqa2, causal, windowed; a ragged mixed step */
{"h256 gqa2 swa w64 ragged bf16", 256, 2, 2, false, true, true, false, 64, {1, 5, 40}, {200, 77, 140}},
{"h256 gqa2 swa w64 ragged fp8+descale", 256, 2, 2, true, true, true, true, 64, {1, 5, 40}, {200, 77, 140}},
/* the real window, a context past it, a verify-sized query */
{"h256 gqa2 swa w1024 verify16", 256, 2, 1, false, true, false, false, 1024, {16}, {1100}},
/* split-KV decode: one row a sequence, long context, scratch present */
{"h256 gqa2 swa w1024 decode split", 256, 2, 2, false, true, true, false, 1024, {1, 1}, {1300, 40}, -1, 0, s256},
/* Gemma 4 full: h512, causal, no window, at the three TP ratios */
{"h512 gqa8 full ragged bf16", 512, 8, 1, false, true, true, false, 0, {2, 7}, {90, 33}},
{"h512 gqa8 full decode split fp8", 512, 8, 1, true, true, false, true, 0, {1}, {700}, -1, 4096, s512},
{"h512 gqa4 full verify16 split", 512, 4, 1, false, true, false, false, 0, {16}, {300}, -1, 1024, s512},
/* the serving case: max_ctx 262144 gives a decode step 64 splits, most of them empty */
{"h512 gqa4 full decode, 64 splits, short ctx", 512, 4, 1, false, true, false, false, 0, {4}, {37}, -1, 262144, s512},
{"h256 gqa2 swa w1024 verify, ragged, splits", 256, 2, 2, true, true, true, true, 1024, {3, 5}, {20, 900}, -1, 262144, s256},
{"h512 gqa2 full ragged", 512, 2, 2, false, true, true, false, 0, {3, 1, 9}, {50, 64, 9}},
/* the coverage the rows claim beyond Gemma: non-causal, two-sided window, a missing block */
{"h256 gqa4 noncausal w20 missing-block", 256, 4, 1, false, false, true, false, 20, {6, 2}, {70, 30}, 2},
{"h512 gqa1 noncausal full", 512, 1, 1, false, false, false, false, 0, {4}, {37}},
{"h256 gqa8 causal q-pitch slice", 256, 8, 1, false, true, false, false, 0, {5}, {21}, -1, 0, 0, 512},
/* no scratch where a split was wanted: falls back to the unsplit kernel */
{"h256 gqa2 decode, split wanted, no scratch", 256, 2, 1, false, true, false, false, 0, {1}, {500}},
/* PREFILL-SHAPED: the WMMA kernel (a 64-row query, or longer) */
{"wmma h512 gqa4 prefill 200 of 900", 512, 4, 1, false, true, false, false, 0, {200}, {900}, -1, 262144, s512, 0, 1},
{"wmma h512 gqa8 prefill fp8+descale", 512, 8, 1, true, true, false, true, 0, {130}, {130}, -1, 0, 0, 0, 1},
{"wmma h512 gqa2 kvh2 ragged + decode rows", 512, 2, 2, false, true, true, false, 0, {150, 1, 70}, {300, 50, 70}, -1, 0, 0, 0, 1},
{"wmma h512 gqa1 noncausal", 512, 1, 1, false, false, false, false, 0, {64}, {100}, -1, 0, 0, 0, 1},
{"wmma h256 gqa2 swa w1024 prefill 300 of 1500", 256, 2, 2, false, true, false, false, 1024, {300}, {1500}, -1, 0, 0, 0, 1},
{"wmma h256 gqa2 swa w64 ragged fp8+descale", 256, 2, 2, true, true, true, true, 64, {100, 90}, {250, 90}, -1, 0, 0, 0, 1},
{"wmma h256 gqa4 noncausal w20 missing-block", 256, 4, 1, false, false, true, false, 20, {80, 70}, {200, 70}, 3, 0, 0, 0, 1},
{"wmma h256 gqa8 causal q-pitch slice", 256, 8, 1, false, true, false, false, 0, {64}, {64}, -1, 0, 0, 512, 1},
{"wmma h256 gqa1 causal w5 fp8", 256, 1, 1, true, true, false, false, 5, {96}, {150}, -1, 0, 0, 0, 1},
/* and what must stay scalar: a verify batch, many sequences, a few rows each */
{"scalar: 8 x verify16, ragged", 512, 4, 1, false, true, true, false, 0, {16, 16, 16, 16, 16, 16, 16, 16},
{40, 50, 60, 70, 80, 90, 100, 110}, -1, 262144, s512, 0, 0},
};
if (argc > 1 && std::string(argv[1]) == "sweep") {
const int n = argc > 2 ? std::atoi(argv[2]) : 200;
int fails = 0, splits = 0, wmmas = 0;
const int hds[2] = {256, 512}, gqas[4] = {1, 2, 4, 8};
for (int it = 0; it < n; ++it) {
Case c;
c.hd = hds[rng() % 2]; c.gqa = gqas[rng() % 4]; c.kvh = 1 + rng() % 3;
c.fp8 = rng() % 2; c.causal = rng() % 4 != 0; c.ragged = rng() % 2; c.descale = rng() % 2;
c.window = rng() % 3 == 0 ? 0 : 1 + rng() % 200;
const int ns = 1 + rng() % 4;
const bool pre = rng() % 3 == 0; /* prefill-shaped: the WMMA kernel */
const int qspan = pre ? 160 : 40, qmin = pre ? 64 : 1;
const int ql = qmin + rng() % qspan;
for (int s = 0; s < ns; ++s) {
int q = c.ragged ? (int)(rng() % 3 == 0 ? 0 : qmin + rng() % qspan) : ql;
if (c.ragged && s == 0 && q == 0) q = 1;
c.qlen.push_back(q);
c.ctx.push_back(q + (int)(rng() % 400));
}
if (rng() % 2) c.scratch = c.hd == 256 ? s256 : s512;
if (rng() % 2) c.max_ctx = 512;
if (rng() % 5 == 0) c.missing_block = (int)(rng() % ((c.ctx[0] + 15) / 16));
c.name = "sweep " + std::to_string(it) + " h" + std::to_string(c.hd) + " g" + std::to_string(c.gqa) +
" kvh" + std::to_string(c.kvh) + (c.fp8 ? " fp8" : " bf16") + (c.causal ? " c" : " nc") +
" w" + std::to_string(c.window) + (c.ragged ? " rag" : "");
const int g0 = emu_grids;
fails += run(c);
splits += emu_grids - g0 > 1;
wmmas += emu_last_block == 512;
}
std::printf("sweep: %d cases, %d took the split path, %d the WMMA kernel, %d failure(s)\n", n, splits,
wmmas, fails);
return fails ? 1 : 0;
}
int fails = 0;
for (auto& c : cases) {
if (argc > 1 && c.name.find(argv[1]) == std::string::npos) continue;
fails += run(c);
}
std::printf("%d failure(s)\n", fails);
return fails ? 1 : 0;
}