radiance 1.7.0

- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves
  every shipped container
- Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime
- /v1/messages works with Claude Code as it ships (gearwave00001)
- a long agent conversation keeps its cached history when the client rewrites its last turn
  (manueloverride)
- a stream's first event goes out with the first token, so llama-benchy measures prefill right
- opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42)
- four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b)
- NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA
  adapters (baKon)
- many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool
  arguments and the CPU's vision and audio encoders
- plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
This commit is contained in:
Deadcode committed 2026-10-16 18:25:18 +02:00
1 parent b068190326
commit 1e968f6e1d
739 files changed
+128447 -84632

No files matched your search

+408 -83
View File
@@ -1,18 +1,373 @@
/* opshapes.cpp -- the non-inline half of opshapes.h: dtype widening and the seeded fill.
/* opshapes.cpp -- operand shapes, dtype widening and the seeded fill (opshapes.h).
*
* Shared by rad-kbench and rad-tune. Two copies of a Box-Muller and a bf16 rounding rule is two
* places for them to differ, and a tuner measuring different bytes from the checker is a tuner
* whose numbers describe a program nobody ran.
* Shared by rad-kbench and rad-tune so the tuner and the checker generate identical bytes.
*/
#include "opshapes.h"
#include <thread>
#include <cmath>
#include <cstring>
#include <thread>
namespace rad {
int rad_operand_shapes_hook(const RadKernelInfo* k, const Geometry& g, int n_opd,
std::vector<OpdShape>* out) {
if (!k || !k->opd_shape || n_opd <= 0) return RAD_E_UNSUPPORTED;
out->assign((size_t)n_opd, OpdShape{});
for (int i = 0; i < n_opd; ++i) {
RadOpdDesc d{};
d.idx_const = -1; /* the ABI's "draw" default */
const int st = k->opd_shape(g.params(), g.n_params(), i, &d);
if (st < 0) return st;
/* An inexpressible rank is a hook bug, not a decline; it must not become a zero-extent
* buffer that passes every comparison vacuously. */
if (d.rank == 0 || d.rank > RAD_MAX_RANK) {
if (!(d.flags & RAD_OPD_F_ABSENT)) return RAD_E_SHAPE;
}
OpdShape& o = (*out)[(size_t)i];
o.dtype = d.dtype;
o.shape.assign(d.shape, d.shape + (d.rank <= RAD_MAX_RANK ? d.rank : 0));
o.absent = (d.flags & RAD_OPD_F_ABSENT) != 0;
o.idx_unique = (d.flags & RAD_OPD_F_IDX_UNIQUE) != 0;
o.idx_cu = (d.flags & RAD_OPD_F_IDX_CU) != 0;
o.idx_prefix = (d.flags & RAD_OPD_F_IDX_PREFIX) != 0;
o.idx_history = (d.flags & RAD_OPD_F_IDX_HISTORY) != 0;
o.is_index = d.fill == RAD_FILL_INDEX;
o.idx_max = d.idx_max;
o.idx_const = d.idx_const;
o.fill_chunk = d.fill_chunk;
switch (d.fill) {
case RAD_FILL_SIGMOID: o.fill = OpdShape::FillSigmoid; break;
case RAD_FILL_GATE_CUMSUM: o.fill = OpdShape::FillGateCumsum; break;
case RAD_FILL_TRI_LOWER: o.fill = OpdShape::FillTriLower; break;
case RAD_FILL_UNIT_ROW: o.fill = OpdShape::FillUnitRow; break;
case RAD_FILL_SAMPLE_PARAMS: o.fill = OpdShape::FillSampleParams; break;
default: o.fill = OpdShape::FillNormal; break;
}
for (int64_t e : o.shape) if (e <= 0 && !o.absent) return RAD_E_SHAPE;
}
return RAD_OK;
}
int rad_operand_shapes(const std::string& op, const Geometry& g, int n_opd, int64_t M,
std::vector<OpdShape>* out, const ShapeCtx* ctx) {
auto gi = [&](const char* k, int64_t dflt) {
long long v = 0;
return g.get_i(k, &v) ? (int64_t)v : dflt;
};
/* Activation dtype: `dtype`, else attention's `q_dtype` / `kv_dtype`, else bf16 (the
* project's activation type). Some ops, e.g. `rope` and `qk_norm_rope`, carry no dtype; an
* f32 fallback would make a bf16 kernel refuse the case. */
const char* dts = g.get_s("dtype");
if (!dts) dts = g.get_s("q_dtype");
if (!dts) dts = g.get_s("kv_dtype");
const uint32_t dt = dts ? rad_dtype_parse(dts) : (uint32_t)RAD_BF16;
const uint32_t D = dt == RAD_DT_INVALID ? (uint32_t)RAD_BF16 : dt;
const int64_t n = gi("n", 0);
const int64_t N = gi("N", 0);
const int64_t K = gi("K", 0);
const int64_t group = gi("group", 128);
out->clear();
auto add = [&](uint32_t d, std::vector<int64_t> s, bool idx = false) {
OpdShape o;
o.dtype = d; o.shape = std::move(s); o.is_index = idx;
out->push_back(std::move(o));
};
auto add_fill = [&](uint32_t d, std::vector<int64_t> s, int f, int64_t chunk = 0) {
OpdShape o;
o.dtype = d; o.shape = std::move(s); o.fill = f; o.fill_chunk = chunk;
out->push_back(std::move(o));
};
auto add_idx = [&](std::vector<int64_t> s, int64_t max, int64_t konst = -1,
bool uniq = false) {
OpdShape o;
o.dtype = (uint32_t)RAD_I32; o.shape = std::move(s); o.is_index = true;
o.idx_max = max; o.idx_const = konst; o.idx_unique = uniq;
out->push_back(std::move(o));
};
/* Cumulative sequence lengths for S sequences (OpdShape::idx_cu). */
auto add_cu = [&](int64_t S) { add_idx({S + 1}, 0, -1); out->back().idx_cu = true; };
/* One optional operand this case does not pass. See OpdShape::absent. */
auto skip_opd = [&]() { OpdShape o; o.absent = true; out->push_back(std::move(o)); };
const int64_t head_dim = gi("head_dim", 0);
const int64_t n_head = gi("n_head", 0);
const int64_t n_head_kv = gi("n_head_kv", n_head);
if (op == "rmsnorm") { add(D, {M, n}); add(D, {n}); add(D, {M, n}); }
else if (op == "rope") {
/* One inout operand plus positions. qkv is the fused row: n_head queries and n_head_kv
* each of k and v, all head_dim wide. */
if (head_dim <= 0 || n_head <= 0) return RAD_E_UNSUPPORTED;
add(D, {M, (n_head + 2 * n_head_kv) * head_dim});
add((uint32_t)RAD_I32, {M}, true);
}
else if (op == "rmsnorm_add") { add(D, {M, n}); add(D, {M, n}); add(D, {n});
add(D, {M, n}); add(D, {M, n}); }
else if (op == "layernorm") { add(D, {M, n}); add(D, {n}); add(D, {n}); add(D, {M, n}); }
else if (op == "add" || op == "mul") { add(D, {M, n}); add(D, {M, n}); add(D, {M, n}); }
else if (op == "silu_mul") { add(D, {M, 2 * n}); add(D, {M, n}); }
else if (op == "gelu" || op == "silu" || op == "sigmoid") { add(D, {M, n}); add(D, {M, n}); }
else if (op == "softmax") { add(D, {M, n}); add(D, {M, n}); }
else if (op == "gather_rows") {
/* idx picks M of 4M rows, so the gather is not the identity (which passes while broken). */
add(D, {4 * M, n}); add((uint32_t)RAD_I32, {M}, true); add(D, {M, n});
}
else if (op == "cast") {
const uint32_t from = rad_dtype_parse(g.get_s("from") ? g.get_s("from") : "f32");
const uint32_t to = rad_dtype_parse(g.get_s("to") ? g.get_s("to") : "f32");
add(from ? from : (uint32_t)RAD_F32, {M, n});
add(to ? to : (uint32_t)RAD_F32, {M, n});
}
else if (op == "gemm_nt") { add(D, {M, K}); add(D, {N, K}); add(D, {M, N}); }
else if (op == "gemm_nt_bias") {
/* The optional residual is passed, to cover the epilogue, as libref's description does. */
add(D, {M, K}); add(D, {N, K}); add(D, {N}); add(D, {M, N}); add(D, {M, N});
}
else if (op == "embed_lookup") {
add((uint32_t)RAD_I32, {M}, true);
add(D, {gi("n_vocab", 0), gi("n_embd", 0)});
add(D, {M, gi("n_embd", 0)});
}
else if (op == "logits_gemm") {
add(D, {M, gi("n_embd", 0)});
add(D, {gi("n_vocab", 0), gi("n_embd", 0)});
add((uint32_t)RAD_F32, {M, gi("n_vocab", 0)});
}
else if (op == "quant_act_i8" || op == "had_quant_act_i8") {
/* One scale per row. quant_act_i8's optional `asum` (group-major per-group code sums, rows
* padded to 16) is passed when `group` divides n, as libref describes it. */
add(D, {M, n}); add((uint32_t)RAD_I8, {M, n}); add((uint32_t)RAD_F32, {M, 1});
const int64_t g = gi("group", 0);
if (op == "quant_act_i8") {
if (g > 0 && n % g == 0) add((uint32_t)RAD_F32, {n / g, (M + 15) / 16 * 16});
else skip_opd();
}
}
else if (op == "quant_act_fp8") {
/* quant_act_i8's operands at fp8 width. `dtype` names the (bf16) input. */
if (n <= 0 || group <= 0) return RAD_E_UNSUPPORTED;
add(D, {M, n});
add((uint32_t)RAD_F8E4M3, {M, n});
add((uint32_t)RAD_F32, {M, (n + group - 1) / group});
}
else if (op == "gemm_nt_q") {
/* Block-scaled fp8 and mxfp4 only. `gemm_nt_q` covers six weight formats whose scale grids
* (per row, per row-group, per output-row-block, e8m0 exponents, zero points, reference
* exponents) belong to the reading kernel, not the schema. The fp8 grid is fixed by the
* checkpoint: E4M3 [N][K] with a bf16 [ceil(N/128)][ceil(K/128)] scale plane, `group`
* being the 128. Other formats are skipped and counted.
*
* `a_sum` and `b_ref` belong to the asymmetric-integer and mxfp4 grids. */
const char* q = g.get_s("dtype");
/* OCP-MXFP4: e2m1 codes with one E8M0 exponent per 32 of K (`group` 32), against the fp8
* activation pair (E4M3 plus an f32 scale per row and 128 of K, as LinearFP8 feeds it).
* Weights are in libr4d's stored layout and sized in bytes: codes K/2 per row in fragment
* order, exponents [K/32][N], per-row reference [N]. Random bytes suffice for timing. */
if (q && std::strcmp(q, "mxfp4a8") == 0) {
if (N <= 0 || K <= 0 || group != 32) return RAD_E_UNSUPPORTED;
add((uint32_t)RAD_F8E4M3, {M, K});
add((uint32_t)RAD_F32, {M, (K + 127) / 128});
add((uint32_t)RAD_U8, {N, K / 2}); /* b -- e2m1 codes, fragment order */
add((uint32_t)RAD_U8, {K / 32, N}); /* b_scale -- E8M0, [K/32][N] */
add((uint32_t)RAD_BF16, {M, N}); /* y */
skip_opd(); /* a_sum */
add((uint32_t)RAD_U8, {N}); /* b_ref -- the per-row reference */
} else {
const bool a8 = q && std::strcmp(q, "fp8a8") == 0;
const bool a16 = q && std::strcmp(q, "fp8a16") == 0;
if ((!a8 && !a16) || N <= 0 || K <= 0 || group <= 0) return RAD_E_UNSUPPORTED;
const int64_t kb = (K + group - 1) / group;
const int64_t nb = (N + group - 1) / group;
/* a, a_scale */
if (a8) { add((uint32_t)RAD_F8E4M3, {M, K}); add((uint32_t)RAD_F32, {M, kb}); }
else { add((uint32_t)RAD_BF16, {M, K}); skip_opd(); }
add((uint32_t)RAD_F8E4M3, {N, K}); /* b -- the checkpoint's own bytes */
add((uint32_t)RAD_BF16, {nb, kb}); /* b_scale -- weight_scale_inv, one per 128x128 */
add((uint32_t)RAD_BF16, {M, N}); /* y */
skip_opd(); /* a_sum */
skip_opd(); /* b_ref */
}
}
else if (op == "dequant") {
add((uint32_t)RAD_I8, {M, n}); add((uint32_t)RAD_F32, {M, group ? n / group : 1});
add(D, {M, n});
}
else if (op == "all_reduce" || op == "all_gather") {
/* all_gather's second operand is [world_size * numel]. all_reduce's is the optional second
* destination (absent means in place, the usual form); it is passed to exercise the
* out-of-place form. */
const int64_t numel = gi("numel", n ? n : M);
add(D, {numel});
add(D, {op == "all_gather" ? numel * gi("world_size", 1) : numel});
}
/* ---- the paged pair, sharing the layout stated in libref/ref_ops.h:
*
* kv_cache[num_blocks, kv_heads, block_size, 2 * head_dim] K first, V second in a slot
*
* The block count and context length are this tool's choice; any cache large enough for the
* case works as long as the indices land inside it. */
else if (op == "kv_store") {
const int64_t bs = gi("block_size", 0);
const int64_t kvh = gi("n_head_kv", 0);
if (bs <= 0 || kvh <= 0 || head_dim <= 0) return RAD_E_UNSUPPORTED;
/* Two blocks of slack so the slot mapping is not a dense prefix, which would not test the
* block index. */
const int64_t nb = (M + bs - 1) / bs + 2;
/* Only the cache takes `kv_dtype`; k and v are always bf16 (D would fall back to
* kv_dtype). */
const char* kvs = g.get_s("kv_dtype");
const uint32_t kvd = kvs ? rad_dtype_parse(kvs) : (uint32_t)RAD_DT_INVALID;
add((uint32_t)RAD_BF16, {M, kvh, head_dim});
add((uint32_t)RAD_BF16, {M, kvh, head_dim});
add_idx({M}, nb * bs, -1, /*uniq*/ true);
add(kvd == (uint32_t)RAD_DT_INVALID ? (uint32_t)RAD_BF16 : kvd, {nb, kvh, bs, 2 * head_dim});
}
else if (op == "attn_paged") {
/* `M` is q_len (decode and prefill are different kernels, docs/OPS.md). One sequence whose
* context is exactly this query: causal attention over its own prefix, as a prefill chunk
* runs. */
const int64_t bs = gi("block_size", 16);
const int64_t gqa = gi("gqa", 1);
const int64_t nh = gi("n_head", gqa);
if (bs <= 0 || gqa <= 0 || nh <= 0 || head_dim <= 0 || nh % gqa) return RAD_E_UNSUPPORTED;
const int64_t kvh = nh / gqa;
const int64_t n_seq = 1, ctx = M;
const int64_t max_blocks = (ctx + bs - 1) / bs;
add(D, {n_seq * M, nh, head_dim});
add(D, {max_blocks, kvh, bs, 2 * head_dim});
add_idx({n_seq, max_blocks}, max_blocks);
/* seqused is fixed, not drawn (see OpdShape::idx_const). */
add_idx({n_seq}, 0, ctx);
skip_opd(); /* k_descale -- this cache is not quantised */
skip_opd(); /* v_descale */
skip_opd(); /* cu_seqlens -- one sequence, read rectangular */
add(D, {n_seq * M, nh, head_dim});
}
/* ---- gated delta net. The layouts are libref/ref_gdn.cpp's, stated there once:
*
* q, k [T, Hk, head_k] v, o [T, Hv, head_v]
* g, beta [T, Hv] fp32 A [T, Hv, chunk]
* h0, ht [N, Hv, head_v, head_k] fp32 cu [N+1]
* conv w [conv_dim, conv_width] conv_state [n_slots, conv_dim, state_len]
*
* with conv_dim = 2 * Hk * head_k + Hv * head_v.
*
* Head counts are not op parameters; they come from container metadata via `ctx`. Without
* them the op is skipped, not guessed. */
else if (op.rfind("gdn_", 0) == 0 && op != "gdn_gated_rmsnorm") {
if (!ctx || ctx->n_head_k <= 0 || ctx->n_head_v <= 0) return RAD_E_UNSUPPORTED;
const int64_t Hk = ctx->n_head_k, Hv = ctx->n_head_v;
const int64_t hk = gi("head_k", 0), hv = gi("head_v", hk);
const int64_t chunk = gi("chunk", 64);
const int64_t width = gi("conv_width", 4);
if (hk <= 0 || hv <= 0 || chunk <= 0 || width <= 1) return RAD_E_UNSUPPORTED;
const int64_t conv_dim = 2 * Hk * hk + Hv * hv;
const int64_t S = ctx->n_seq > 0 ? ctx->n_seq : 1;
if (op == "gdn_conv_prep") {
/* T tokens over S sequences, one conv-state slot each. The state length is
* conv_width - 1 + n_spec, i.e. conv_width - 1 without speculation. */
add(D, {M, conv_dim}); /* x -- the fused projection row */
add(D, {conv_dim, width}); /* w */
skip_opd(); /* b -- this checkpoint has no conv bias */
add((uint32_t)RAD_F32, {Hv}); /* A_log */
add((uint32_t)RAD_F32, {Hv}); /* dt_bias */
add(D, {S, conv_dim, width - 1}); /* conv_state, INOUT */
add_cu(S); /* cu */
add(D, {M, Hv}); /* a */
add(D, {M, Hv}); /* b_gate */
add_idx({S}, S, -1, /*uniq*/ true); /* cache_idx -- one slot a sequence */
add_idx({S}, 0, 0); /* has_init -- cold prefill, passed
* explicitly: ref reads a null as warm,
* libr4d as cold. */
add(D, {M, Hk, hk}); /* q */
add(D, {M, Hk, hk}); /* k */
add(D, {M, Hv, hv}); /* v */
add_fill((uint32_t)RAD_F32, {M, Hv}, OpdShape::FillGateCumsum, chunk); /* g */
add_fill((uint32_t)RAD_F32, {M, Hv}, OpdShape::FillSigmoid); /* beta */
}
else if (op == "gdn_kkt_solve") {
add(D, {M, Hk, hk}); /* k */
add_fill((uint32_t)RAD_F32, {M, Hv}, OpdShape::FillSigmoid); /* beta */
add_fill((uint32_t)RAD_F32, {M, Hv}, OpdShape::FillGateCumsum, chunk); /* g */
add_cu(S); /* cu */
add_fill(D, {M, Hv, chunk}, OpdShape::FillTriLower, chunk); /* A */
}
else if (op == "gdn_chunk_scan") {
add(D, {M, Hk, hk}); /* q */
add(D, {M, Hk, hk}); /* k */
add(D, {M, Hv, hv}); /* v */
add_fill(D, {M, Hv, chunk}, OpdShape::FillTriLower, chunk); /* A */
add_fill((uint32_t)RAD_F32, {M, Hv}, OpdShape::FillGateCumsum, chunk); /* g */
add_fill((uint32_t)RAD_F32, {M, Hv}, OpdShape::FillSigmoid); /* beta */
add((uint32_t)RAD_F32, {S, Hv, hv, hk}); /* h0 */
add_cu(S); /* cu */
add(D, {M, Hv, hv}); /* o */
add((uint32_t)RAD_F32, {S, Hv, hv, hk}); /* ht */
add_idx({S}, S, -1, /*uniq*/ true); /* state_idx */
}
else if (op == "gdn_conv_update") {
/* Decode. `M` is q_len, one row per candidate token; the window is
* conv_width - 2 + q_len deep so a rejection only moves the read offset (ref_ops.h's
* rollback contract). num_accepted is 1-based; all ones is non-speculative decode. */
const int64_t sl = width - 2 + M;
add(D, {S * M, conv_dim}); /* x */
add(D, {conv_dim, width}); /* w */
skip_opd(); /* b */
add(D, {S, conv_dim, sl}); /* conv_state, INOUT */
add_idx({S, M}, S, -1); /* state_idx */
add_idx({S}, 0, 1); /* num_accepted -- none rejected */
add_cu(S); /* cu */
add(D, {S * M, Hk, hk}); /* q */
add(D, {S * M, Hk, hk}); /* k */
add(D, {S * M, Hv, hv}); /* v */
}
else if (op == "gdn_recurrent_update") {
add(D, {S * M, Hk, hk}); /* q */
add(D, {S * M, Hk, hk}); /* k */
add(D, {S * M, Hv, hv}); /* v */
add(D, {S * M, Hv}); /* a */
add(D, {S * M, Hv}); /* b */
add((uint32_t)RAD_F32, {Hv}); /* A_log */
add((uint32_t)RAD_F32, {Hv}); /* dt_bias */
add((uint32_t)RAD_F32, {S * M, Hv, hv, hk}); /* state, one per candidate token */
add_idx({S, M}, S * M, -1, /*uniq*/ true); /* state_idx */
add_idx({S}, 0, 1); /* num_accepted */
add_cu(S); /* cu */
add(D, {S * M, Hv, hv}); /* z */
add((uint32_t)RAD_F32, {hv}); /* norm_w */
add(D, {S * M, Hv, hv}); /* o */
skip_opd(); /* o_q -- the folded fp8 quantiser */
skip_opd(); /* o_scale */
}
else return RAD_E_UNSUPPORTED;
}
else if (op == "gdn_gated_rmsnorm") {
/* One row per (token, head): `M` is already tokens x heads and `channels` is head_v. */
const int64_t ch = gi("channels", 0);
if (ch <= 0) return RAD_E_UNSUPPORTED;
add(D, {M, ch}); add(D, {M, ch}); add((uint32_t)RAD_F32, {ch}); add(D, {M, ch});
}
/* Deliberately absent: ssm_conv and qk_norm_rope_gate take extents from the KV group's state
* geometry and from interleaved strided views (attn_q is [n_embd, 2*head_dim*n_head] with query
* and gate alternating per head). Only the kernel's hook can describe them. */
else return RAD_E_UNSUPPORTED;
if ((int)out->size() != n_opd) {
/* Recipe and schema disagree: a bug in one of them. Do not pad with a guess. */
RAD_DEBUG("%s: shape recipe produces %zu operands, the schema declares %d",
op.c_str(), out->size(), n_opd);
return RAD_E_UNSUPPORTED;
}
for (const OpdShape& s : *out)
for (int64_t d : s.shape)
if (d <= 0) return RAD_E_UNSUPPORTED;
return RAD_OK;
}
void rad_widen(const void* src, uint32_t dtype, int64_t n, std::vector<float>* out) {
out->assign((size_t)n, 0.0f);
switch (dtype) {
@@ -34,8 +389,8 @@ void rad_widen(const void* src, uint32_t dtype, int64_t n, std::vector<float>* o
const uint32_t e = (h >> 10) & 0x1F, m = h & 0x3FF;
uint32_t bits;
if (e == 0) {
/* Subnormal. Renormalise rather than flush: a flushed small weight is a wrong
* one, and it is wrong in a way that never trips a norm-based tolerance. */
/* Subnormal: renormalise rather than flush; a flushed value would pass a
* norm-based tolerance while wrong. */
if (m == 0) bits = sign;
else {
int sh = -1;
@@ -53,13 +408,9 @@ void rad_widen(const void* src, uint32_t dtype, int64_t n, std::vector<float>* o
(*out)[(size_t)i] = f;
}
break;
/* OCP fp8. These two are what make the block-scaled fp8 family checkable at all, and
* without them it fails VACUOUSLY rather than visibly: rad_narrow would leave the E4M3
* activation at its zero fill, so kernel and oracle both compute zero, rad_widen reads the
* E4M3 output back as zero, and every case reports rel_l2 = 0 and "ok". A vacuous pass is
* worse than a skip, which is what the `default` below is careful to produce for a dtype
* whose bytes only its own kernel understands. e4m3 has no infinity and S1111111 is its
* only NaN; e5m2 is IEEE-shaped. */
/* OCP fp8. Without these the fp8 family would pass vacuously: inputs and outputs would
* all read as zero. e4m3 has no infinity and S1111111 is its only NaN; e5m2 is
* IEEE-shaped. */
case RAD_F8E4M3:
for (int64_t i = 0; i < n; ++i) {
const uint8_t b = ((const uint8_t*)src)[i];
@@ -117,18 +468,10 @@ void rad_widen(const void* src, uint32_t dtype, int64_t n, std::vector<float>* o
case RAD_I32:
for (int64_t i = 0; i < n; ++i) (*out)[(size_t)i] = (float)((const int32_t*)src)[i];
break;
/* THE INTEGER TYPES MUST BE READABLE HERE OR A COMPARISON AGAINST THEM IS VACUOUS.
*
* An output operand is widened on the recorder's side and again on the reader's, so a
* dtype that falls through to `default` is reduced to a norm of zero and a sample of
* zeros on BOTH sides -- which agrees perfectly and reports as a pass. A kernel writing
* arbitrary bytes into such an operand is indistinguishable from a correct one.
*
* The set is the one libref's own element accessor reads, because libref is what the
* recording is of: a dtype the oracle can produce and this cannot is an output nothing
* checks. i64 past 2^24 does not survive the float this widens into, which is the same
* bound rad_narrow documents and is fine for what these operands carry (sizes, offsets,
* token ids below a quarter-million vocabulary). */
/* Integer types must be readable here: an unhandled dtype widens to zeros on both the
* recorder's and the reader's side, so any output would pass. The set matches what
* libref's element accessor reads. i64 is exact only up to 2^24 in float, enough for
* what these operands carry (sizes, offsets, token ids). */
case RAD_I16:
for (int64_t i = 0; i < n; ++i) (*out)[(size_t)i] = (float)((const int16_t*)src)[i];
break;
@@ -142,26 +485,28 @@ void rad_widen(const void* src, uint32_t dtype, int64_t n, std::vector<float>* o
for (int64_t i = 0; i < n; ++i) (*out)[(size_t)i] = ((const uint8_t*)src)[i] ? 1.0f : 0.0f;
break;
default:
/* A plugin-private dtype: only its own kernel knows what the bytes mean. Zeroed, and
* the caller sees a zero reference and reports it as such rather than as agreement. */
/* Plugin-private dtype: left zeroed; the caller reports a zero reference as such. */
break;
}
}
/* Fixture inputs are redrawn from recorded seeds (kfixture.h), so every rounding below is part of
* the recorded contract: changing one invalidates the recorded cases of that dtype. */
void rad_narrow(const float* src, uint32_t dtype, int64_t n, void* dst) {
switch (dtype) {
case RAD_F32:
std::memcpy(dst, src, (size_t)n * 4);
break;
case RAD_BF16:
/* Round-to-nearest-even, the same rule rad-convert uses. Truncating here would bias
* every generated input toward zero and quietly flatter every kernel. */
/* Round-to-nearest-even, as rad-convert does; truncation would bias inputs toward
* zero. */
for (int64_t i = 0; i < n; ++i) {
uint32_t b;
std::memcpy(&b, &src[i], 4);
((uint16_t*)dst)[i] = (uint16_t)((b + 0x7FFFu + ((b >> 16) & 1u)) >> 16);
}
break;
/* f16 and e5m2 truncate and flush subnormals to zero. */
case RAD_F16:
for (int64_t i = 0; i < n; ++i) {
uint32_t x;
@@ -176,11 +521,8 @@ void rad_narrow(const float* src, uint32_t dtype, int64_t n, void* dst) {
((uint16_t*)dst)[i] = h;
}
break;
/* The encode side of the pair above. `rad_fill_normal` draws unit normals and E4M3 holds
* 448, so no scaling is applied -- the values land in the middle of the format's range,
* which is where a quantised GEMM actually operates. Round-to-nearest-even on the 3-bit
* mantissa with a real subnormal path, matching libref/ref_common.h; truncating would
* bias every generated input toward zero and flatter every kernel. */
/* Unit normals fit E4M3 (max 448) unscaled. Round-to-nearest-even on the 3-bit mantissa
* with a subnormal path, matching libref/ref_common.h. */
case RAD_F8E4M3:
for (int64_t i = 0; i < n; ++i) {
float a = src[i] < 0 ? -src[i] : src[i];
@@ -220,6 +562,7 @@ void rad_narrow(const float* src, uint32_t dtype, int64_t n, void* dst) {
((uint8_t*)dst)[i] = h;
}
break;
/* Unit normals scaled by 32 to spread over the int8 range. */
case RAD_I8:
for (int64_t i = 0; i < n; ++i) {
float v = src[i] * 32.0f;
@@ -237,24 +580,15 @@ void rad_narrow(const float* src, uint32_t dtype, int64_t n, void* dst) {
case RAD_I32:
for (int64_t i = 0; i < n; ++i) ((int32_t*)dst)[i] = (int32_t)std::lrintf(src[i]);
break;
/* i64 MUST BE HANDLED HERE, because the `default` below zeroes: an unhandled i64 operand
* is drawn as a buffer of zeros, silently, since zero is a legal value for most of them.
* The ops that do check read them as sizes -- ngram_ids refuses `vocab_size <= 0` by name,
* so with zeroed sizes it would be refused at every geometry and never checked on any
* plugin, and that refusal reads exactly like a shape the reference does not serve.
/* Integer types must be handled here, since `default` zeroes silently; e.g. ngram_ids
* refuses `vocab_size <= 0`, so zeroed sizes would skip it at every geometry. Keep this
* set in step with rad_widen.
*
* `llrintf` and not `lrintf`: long is 32-bit on some targets and this type is not.
* A float carries 24 bits, so a drawn i64 past 2^24 does not round-trip -- which is fine
* for what i64 operands in this vocabulary actually are (sizes, offsets, multipliers) and
* is why the shapes that need a large exact value pass it as idx_const rather than a draw. */
* `llrintf`, not `lrintf`: long is 32-bit on some targets. Values past 2^24 do not
* round-trip through float; shapes needing a large exact value use idx_const. */
case RAD_I64:
for (int64_t i = 0; i < n; ++i) ((int64_t*)dst)[i] = (int64_t)std::llrintf(src[i]);
break;
/* The rest of the integer vocabulary, for the reason the i64 note above gives and with
* the same consequence: zeroed by `default`, an operand of one of these types is handed
* to the kernel as a buffer of zeros, which is a legal value for most of them and so
* never reports as anything. Kept in step with rad_widen -- a type this can write and
* that cannot read back is an output whose comparison is vacuous. */
case RAD_I16:
for (int64_t i = 0; i < n; ++i) ((int16_t*)dst)[i] = (int16_t)std::lrintf(src[i]);
break;
@@ -282,21 +616,13 @@ uint64_t rad_case_seed(uint64_t base, std::string_view op, int band, int domain,
return h ? h : 1;
}
/* Standard normal by Box-Muller. Activations in a transformer are roughly Gaussian, and the tails
* are the point: an outlier is what breaks a per-row scale, and a uniform fill has none. */
/* BLOCK-SEEDED AND THREADED, and the block seeding is what makes the threading legal.
/* Standard normals by Box-Muller: transformer activations are roughly Gaussian, and the tails
* (outliers that break a per-row scale) matter, which a uniform fill lacks.
*
* This is the hot loop of the whole tool: a replay draws every operand of every case, and an fp8
* GEMM weight at N=34816 K=5120 is 178 million elements of Box-Muller -- four transcendentals per
* pair, chained through one splitmix state. Single threaded that is most of a replay's wall clock,
* and a benchmark that spends its time generating its own inputs is a benchmark nobody runs.
*
* The chain is the problem: element i depends on element i-2, so nothing can be split. So the
* stream is cut into fixed blocks and each block derives its OWN seed from (seed, block index).
* The values then do not depend on how many threads ran, or on whether any did -- which is not a
* nicety here but the contract: a fixture recorded on one machine is replayed on another, and a
* draw that varied with core count would make every recorded reference wrong somewhere else.
*/
* This is the replay's hot loop (an fp8 GEMM weight at N=34816 K=5120 is 178M elements), so it is
* threaded. One splitmix chain cannot be split, so the stream is cut into fixed blocks, each seeded
* from (seed, block index). Values are then independent of thread count, which the contract
* requires: fixtures recorded on one machine are replayed on another. */
const int64_t RAD_FILL_BLOCK = 1 << 16;
static void fill_block(float* p, int64_t n, uint64_t seed, float sigma) {
@@ -315,33 +641,32 @@ static uint64_t block_seed(uint64_t seed, int64_t b) {
return rad_splitmix(s);
}
/* ONE BLOCK OF THAT STREAM, for a caller drawing a large operand a block at a time so the float
* intermediate never gets as big as the tensor. Same seed, same block index, same bytes. */
/* One block of that stream, for callers drawing a large operand block by block to bound the float
* intermediate. Same seed and block index give the same bytes. */
void rad_fill_normal_block(float* p, int64_t n, uint64_t seed, int64_t block, float sigma) {
fill_block(p, n, block_seed(seed, block), sigma);
}
void rad_fill_normal(float* p, int64_t n, uint64_t seed, float sigma) {
if (n <= 0) return;
const int64_t nb = (n + RAD_FILL_BLOCK - 1) / RAD_FILL_BLOCK;
if (nb == 1) { fill_block(p, n, block_seed(seed, 0), sigma); return; }
unsigned hw = std::thread::hardware_concurrency();
if (hw == 0) hw = 1;
const int64_t nthreads = std::min<int64_t>(nb, (int64_t)hw);
std::vector<std::thread> th;
th.reserve((size_t)nthreads);
for (int64_t t = 0; t < nthreads; ++t) {
th.emplace_back([&, t]() {
for (int64_t b = t; b < nb; b += nthreads) {
const int64_t lo = b * RAD_FILL_BLOCK;
fill_block(p + lo, std::min<int64_t>(RAD_FILL_BLOCK, n - lo),
block_seed(seed, b), sigma);
}
});
rad_for_blocks((n + RAD_FILL_BLOCK - 1) / RAD_FILL_BLOCK, [&](int64_t b) {
const int64_t lo = b * RAD_FILL_BLOCK;
fill_block(p + lo, std::min<int64_t>(RAD_FILL_BLOCK, n - lo), block_seed(seed, b), sigma);
});
}
void rad_for_blocks(int64_t nb, const std::function<void(int64_t)>& fn) {
const unsigned hw = std::thread::hardware_concurrency();
const int64_t nt = std::min<int64_t>(nb, hw ? (int64_t)hw : 1);
if (nt <= 1) {
for (int64_t b = 0; b < nb; ++b) fn(b);
return;
}
std::vector<std::thread> th;
th.reserve((size_t)nt);
for (int64_t t = 0; t < nt; ++t)
th.emplace_back([&, t]() { for (int64_t b = t; b < nb; b += nt) fn(b); });
for (auto& x : th) x.join();
}
} /* namespace rad */