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

+193 -95
View File
@@ -1,14 +1,10 @@
/* ref_gemm.h -- how libref reads a matrix operand and its scale stream.
*
* Shared by the dense GEMM and the mixture-of-experts GEMM, which need the same three answers:
* where row `r` of an operand begins, what the scale of a (row, k) pair is, and what a block-
* scaled grid looks like against the operand it scales. A grouped GEMM is a GEMM per row against
* a different weight, so it needs exactly the machinery the dense one has and nothing else; a
* second copy of `make_scales` in ref_moe.cpp would be a second place for the rblk rule to be
* wrong, and the rblk rule is what makes this reference usable as an oracle for a block-scaled
* fp8 checkpoint.
* Shared by the dense and mixture-of-experts GEMMs, which need the same answers: where row `r` of
* an operand begins, the scale of a (row, k) pair, and how a block-scaled grid maps onto its
* operand. Keeping one copy keeps one rblk rule, which block-scaled fp8 checkpoints depend on.
*
* Everything here is a pure reading of a RadTensor. No op, no dispatch, no allocation.
* Pure reads of a RadTensor: no op, no dispatch, no allocation.
*/
#ifndef REF_GEMM_H
#define REF_GEMM_H
@@ -17,24 +13,16 @@
namespace ref {
/* A tensor read as [rows, cols], which is what every GEMM here wants and is NOT the same as
* "dim 0 and dim 1".
/* Row stride of a tensor read as [rows, cols], which is not the same as dim 0 by dim 1.
*
* `stride[rank - 2]` is the row stride only when the LAST dimension is already `cols`. An
* activation that arrives rank 3 -- [tokens, heads, width], which is how the gated delta net's
* out-projection input is declared, because the scan kernels require that rank -- is read as
* [tokens, heads * width], and its row stride is the product of the trailing dimensions, not the
* head stride. Taking the head stride instead lands row 0 correctly and starts every row after it
* `width` elements in -- which reads as a correct implementation right up to the second row.
* `stride[rank - 2]` is the row stride only when the last dimension is already `cols`. A rank-3
* activation [tokens, heads, width] (the gated delta net's out-projection input, rank 3 because the
* scan kernels need it) is read as [tokens, heads * width], whose row stride spans the trailing
* dimensions. Using the head stride gets row 0 right and every later row wrong.
*
* THE OUTPUT SIDE IS THE WORSE HALF and has the same rule. A gated attention layer's q|gate
* projection is declared [tokens, heads, 2 * head_dim] -- rank 3, because the de-interleaving that
* follows needs the head axis -- and writing it at the HEAD stride does not merely write the wrong
* bytes, it leaves most of the buffer NEVER WRITTEN, so rows after the first carry a tail of exact
* zeros.
*
* This is libref, so it is the ORACLE: every tool that certifies a kernel against this arithmetic
* inherits whatever it gets wrong, which is why the rule is stated here once. */
* Outputs follow the same rule. A gated attention q|gate projection is [tokens, heads,
* 2 * head_dim]; writing it at the head stride leaves most of the buffer unwritten, so later rows
* end in exact zeros. */
static inline int64_t row_stride(const RadTensor* t, int64_t cols) {
if (!t || t->rank < 2) return cols;
/* Fold trailing dimensions until they span `cols`; the next one out carries the row. */
@@ -47,18 +35,16 @@ static inline int64_t row_stride(const RadTensor* t, int64_t cols) {
return t->stride[t->rank - 2]; /* the shapes disagree; fall back to the row axis */
}
/* The element offset of row `r` of a [rows, cols] operand, and the stride along cols. Weights
* arrive contiguous from the mapper; activations may be a slice of a wider arena buffer, so both
* are read through their strides. */
/* Row and column strides of a [rows, cols] operand. Weights arrive contiguous; activations may be
* a slice of a wider arena buffer, so both are read through their strides. */
struct Mat {
const RadTensor* t;
int64_t rs; /* elements between rows */
int64_t cs; /* elements between columns */
};
static inline Mat as_mat(const RadTensor* t, int64_t rows, int64_t cols) {
static inline Mat as_mat(const RadTensor* t, int64_t cols) {
Mat m{ t, cols, 1 };
(void)rows;
if (t->rank < 2) return m;
m.cs = t->stride[t->rank - 1];
m.rs = row_stride(t, cols);
@@ -66,48 +52,39 @@ static inline Mat as_mat(const RadTensor* t, int64_t rows, int64_t cols) {
}
/* ------------------------------------------------------------------ scale streams
* A quantised operand's scales are a separate SoA stream (spec §4.1), shaped [rows, groups] with
* the group index fastest, or [rows] for one scale per row. The group SIZE is derived from the
* stream's own extent rather than trusted from the parameter: a `group` that disagrees with the
* scale tensor is the kind of mismatch that produces plausible wrong numbers, and deriving it
* makes the disagreement structurally impossible.
* A quantised operand's scales are a separate SoA stream (spec §4.1), [rows, groups] with the
* group index fastest, or [rows] for one scale per row. The group size is derived from the
* stream's extent rather than the `group` parameter, so the two cannot disagree.
*
* A BLOCK-SCALED grid is the same stream with fewer rows than the operand. DeepSeek-style fp8 --
* which is what Qwen3.8-27B-FP8 and every other `weight_block_size: [128, 128]` checkpoint ships --
* stores one scale per 128x128 TILE, so `weight_scale_inv` is [ceil(N/128)][ceil(K/128)] against an
* [N][K] weight. `rblk` is how many operand rows share a scale row; it is 1 for every per-row grid,
* which is every other format in this project, so the division below costs those nothing.
*
* Without it this reference reads a 17408-row weight's 136-row scale plane as if it had 17408 rows
* and lands on `groups = 0`, which is not a diagnosable failure -- it is a plausible wrong number,
* and it makes ref useless as the oracle for exactly the format the production models are in. */
* A block-scaled grid is the same stream with fewer rows than the operand. DeepSeek-style fp8
* (Qwen3.8-27B-FP8 and any `weight_block_size: [128, 128]` checkpoint) stores one scale per
* 128x128 tile, so `weight_scale_inv` is [ceil(N/128)][ceil(K/128)] against an [N][K] weight.
* `rblk` is how many operand rows share a scale row; 1 for every per-row grid. Without it a
* 17408-row weight's 136-row scale plane would be read per row and give plausible wrong numbers. */
struct Scales {
const void* p;
uint32_t dt;
int64_t rs; /* elements between scale rows */
int64_t cs; /* elements between groups */
int64_t group; /* K elements per scale */
int64_t rblk; /* OPERAND rows per scale row; 1 unless the grid is block-scaled */
int64_t rblk; /* operand rows per scale row; 1 unless the grid is block-scaled */
bool e8m0; /* the scale stream is OCP e8m0 exponent bytes, not floats */
inline float at(int64_t row, int64_t k) const {
int64_t g = group > 0 ? k / group : 0;
int64_t i = (rblk > 1 ? row / rblk : row) * rs + g * cs;
return e8m0 ? e8m0_to_f32(((const uint8_t*)p)[i]) : ld_dt(dt, p, i);
}
/* The scale of group `g` of `row`. */
inline float grp(int64_t row, int64_t g) const {
int64_t i = (rblk > 1 ? row / rblk : row) * rs + g * cs;
return e8m0 ? e8m0_to_f32(((const uint8_t*)p)[i]) : ld_dt(dt, p, i);
}
/* The scale of element `k` of `row`. */
inline float at(int64_t row, int64_t k) const { return grp(row, group > 0 ? k / group : 0); }
};
/* THE BLOCK A GRID OF `blocks` PIECES IMPLIES OVER `ext` ELEMENTS, and it is not ext / blocks.
* A grid's last block may be partial -- ceil(300 / 128) is 3 scales over 128, 128 and 44 -- so
* the quotient (100 there) assigns elements 100..127 to the second scale. The block is the POWER
* OF TWO whose ceiling division gives `blocks`, which is unique whenever there is more than one
* block, and every scale grid in this project is one (32, 64, 128). Where no power of two fits,
* the smallest block that yields `blocks` pieces is taken. Either way ceil(ext / block) is at
* most `blocks`, so an index derived from it never leaves the grid. */
/* The block size a grid of `blocks` pieces implies over `ext` elements; not ext / blocks, since
* the last block may be partial (300 over 128 is 3 scales covering 128, 128 and 44, but 300 / 3 =
* 100 would misassign 100..127). It is the power of two whose ceiling division gives `blocks`,
* unique for more than one block; every scale grid here is one (32, 64, 128). Otherwise the
* smallest block giving `blocks` pieces. Either way ceil(ext / block) <= `blocks`, so derived
* indices stay in the grid. */
static inline int64_t grid_block(int64_t ext, int64_t blocks) {
if (ext <= 0) return 1;
if (blocks <= 1) return ext;
@@ -125,15 +102,13 @@ static inline Scales make_scales(const RadTensor* s, int64_t rows, int64_t K, bo
}
q.p = s->data;
q.dt = s->dtype;
/* A rank-2 stream states its own grid, and that statement is preferred: it is the only source
* that can distinguish "one scale per row over `groups` groups" from "one scale per row BLOCK".
* FEWER scale rows than operand rows is a block grid, whatever the remainder: [ceil(N/128)]
* rows against N = 129 is two blocks, the second of one row. As many or more is one scale row
* per operand row, read through the stream's own row stride.
/* A rank-2 stream states its own grid, the only way to tell per-row groups from per-row-block
* scales. Fewer scale rows than operand rows is a block grid whatever the remainder
* ([ceil(N/128)] rows against N = 129 is two blocks, the second one row); otherwise one scale
* row per operand row, through the stream's row stride.
*
* A rank-1 or flattened stream has no grid to state. With a scale for every row it is per row
* over nel / rows groups; with fewer it is one scale per BLOCK of rows. Reading it per row
* there walks off the end of the plane at the first row past its length. */
* A rank-1 or flattened stream states no grid. With a scale per row it is nel / rows groups per
* row; with fewer it is one scale per block of rows (reading it per row would run off the end). */
int64_t groups = 0;
if (s->rank >= 2) {
const int64_t srows = s->shape[s->rank - 2];
@@ -154,40 +129,44 @@ static inline Scales make_scales(const RadTensor* s, int64_t rows, int64_t K, bo
q.cs = 1;
}
q.group = grid_block(K, groups);
/* mxfp4's block scale is an e8m0 exponent byte. When the stream arrives as u8 beside an mxfp4
* weight there is nothing else it could be; a float stream beside the same weight is read as
* a float, so a quantiser that chose to store the scale plainly still works. */
/* mxfp4's block scale is an e8m0 exponent byte: a u8 stream beside an mxfp4 weight is read as
* e8m0, while a float stream is read as floats, so plainly stored scales still work. */
q.e8m0 = mxfp4_weight && (s->dtype == RAD_U8);
return q;
}
/* ------------------------------------------------------------------ one quantised product
*
* ONE (m, n) dot of a quantised GEMM, and the ONE DEFINITION of how the scales fold into it.
* One (m, n) dot of a quantised GEMM, and the single definition of how scales fold into it.
*
* The group sub-sum is accumulated from the RAW CODES and scaled once at the end of the group,
* which is what an int8 or fp8 WMMA kernel does: it accumulates in the fragment's type and applies
* the block scales when the tile closes. Folding the scales into every product instead is the same
* value in exact arithmetic and a different one in the last bits, and rad-kbench would blame the
* kernel for the reference's choice.
* Each group's sub-sum is accumulated from the raw codes and scaled once at the group's end, as an
* int8 or fp8 WMMA kernel does when a tile closes. Scaling every product is equal in exact
* arithmetic but differs in the last bits.
*
* `sub * sa * sb` IS AN ORDER AND NOT AN EXPRESSION. Float multiplication is commutative and not
* associative, so (sub*sa)*sb and (sub*sb)*sa differ by an ulp on some operands -- which is enough
* to move one fp8 code, and a fused op being byte-identical to the pair it replaces is the whole
* claim such a fold rests on. Every caller goes through here so that there is one order.
* `sub * sa * sb` is an order: (sub*sa)*sb and (sub*sb)*sa can differ by an ulp, enough to move an
* fp8 code, and fused ops must be byte-identical to the pair they replace. Every caller uses this
* function so there is one order.
*
* The loads go through `ld_dt` rather than `Cvt<DT>` so that one function serves every dtype pair.
* The two return the same float for every dtype, so a caller that dispatches on the type at
* compile time for speed still computes these bits.
* LA and LB load A's and B's elements: `Dyn` switches on the dtype per element and serves every
* pair; `Fix<DT>` resolves it at compile time for the GEMM's hot pairs. Both return the same
* floats, so the result does not depend on which was used.
*/
struct Dyn {
static inline float ld(const RadTensor* t, int64_t i) { return ld_dt(t->dtype, t->data, i); }
};
template <uint32_t DT> struct Fix {
static inline float ld(const RadTensor* t, int64_t i) { return Cvt<DT>::ld(t->data, i); }
};
template <class LA = Dyn, class LB = Dyn>
static inline float quant_dot(const Mat& A, const Mat& B, const Scales& SA, const Scales& SB,
int64_t m, int64_t n, int64_t K) {
const int64_t g = SB.group > 0 ? SB.group : K;
const int64_t ngrp = (K + g - 1) / g;
/* No format in this project gives the activation a finer group than the weight; where one
* appears the scale cannot be hoisted out of the group and the sub-sum is formed per element. */
/* No format here gives the activation a finer group than the weight; if one does, its scale
* cannot be hoisted and is applied per element. */
const bool coarse_a = !SA.p || SA.group >= g;
const uint32_t da = A.t->dtype, db = B.t->dtype;
const int64_t a0 = m * A.rs, b0 = n * B.rs;
float acc = 0.0f;
for (int64_t gi = 0; gi < ngrp; ++gi) {
@@ -196,28 +175,147 @@ static inline float quant_dot(const Mat& A, const Mat& B, const Scales& SA, cons
float sub = 0.0f;
if (coarse_a) {
for (int64_t k = k0; k < k1; ++k)
sub += ld_dt(da, A.t->data, a0 + k * A.cs) * ld_dt(db, B.t->data, b0 + k * B.cs);
sub += LA::ld(A.t, a0 + k * A.cs) * LB::ld(B.t, b0 + k * B.cs);
acc += sub * (SA.p ? SA.at(m, k0) : 1.0f) * sb;
} else {
for (int64_t k = k0; k < k1; ++k)
sub += ld_dt(da, A.t->data, a0 + k * A.cs) * SA.at(m, k) *
ld_dt(db, B.t->data, b0 + k * B.cs);
sub += LA::ld(A.t, a0 + k * A.cs) * SA.at(m, k) * LB::ld(B.t, b0 + k * B.cs);
acc += sub * sb;
}
}
return acc;
}
/* ------------------------------------------------------------------ the blocked product
*
* quant_dot over a tile of outputs: A rows `arow[0..R)` against B rows [n0, n0 + nb). Every
* output is the chain quant_dot forms, per group a sub-sum from zero over ascending k and then
* `acc + (sub * sa) * sb`, so the result is bit-identical to calling it per element. What changes
* is that REF_NB outputs advance together: the chains are independent, so they run in vector lanes
* and their add latencies overlap, and B's codes are widened into an f32 panel once per K chunk
* and reused by every row of the tile (exact conversions, so the panel holds the same floats the
* loaders return).
*
* Only the coarse activation grid (no finer scale on A than on B) is tiled; quant_dot keeps the
* other, which no format uses. */
static constexpr int64_t REF_NB = 16, REF_KC = 256, REF_MB = 2;
typedef float RefV4 __attribute__((vector_size(16)));
/* Floats a tile of R rows needs: the panel, two A rows, and the sub-sums and accumulators of R
* rows rounded up to whole row pairs. One ws_get() carves all of it. */
static inline size_t tile_ws_floats(int64_t R) {
const int64_t rp = (R + REF_MB - 1) / REF_MB * REF_MB;
return (size_t)(REF_KC * REF_NB + REF_MB * REF_KC + 2 * rp * REF_NB);
}
/* sub[2][REF_NB] += abuf[i][k] * panel[k][*] over k < kl, one chain per output. */
static inline void tile_kernel(const float* abuf, const float* panel, float* sub, int64_t kl) {
static_assert(REF_NB == 16 && REF_MB == 2, "the kernel holds two rows of four vectors");
auto ld = [](const float* f) { RefV4 v; std::memcpy(&v, f, sizeof v); return v; };
auto st = [](float* f, RefV4 v) { std::memcpy(f, &v, sizeof v); };
RefV4 c00 = ld(sub), c01 = ld(sub + 4), c02 = ld(sub + 8), c03 = ld(sub + 12);
RefV4 c10 = ld(sub + 16), c11 = ld(sub + 20), c12 = ld(sub + 24), c13 = ld(sub + 28);
for (int64_t k = 0; k < kl; ++k) {
const float a0 = abuf[k], a1 = abuf[REF_KC + k];
const float* p = panel + k * REF_NB;
const RefV4 p0 = ld(p), p1 = ld(p + 4), p2 = ld(p + 8), p3 = ld(p + 12);
c00 += a0 * p0; c01 += a0 * p1; c02 += a0 * p2; c03 += a0 * p3;
c10 += a1 * p0; c11 += a1 * p1; c12 += a1 * p2; c13 += a1 * p3;
}
st(sub, c00); st(sub + 4, c01); st(sub + 8, c02); st(sub + 12, c03);
st(sub + 16, c10); st(sub + 20, c11); st(sub + 24, c12); st(sub + 28, c13);
}
/* The tile's results land in `ws`'s accumulator block, [R][REF_NB] row-major, which this returns;
* columns at and past `nb` are padding. Requires the coarse activation grid (see above). */
template <class LA, class LB>
static float* quant_tile(const Mat& A, const Mat& B, const Scales& SA, const Scales& SB,
const int64_t* arow, int64_t R, int64_t n0, int64_t nb, int64_t K,
float* ws) {
const int64_t rp = (R + REF_MB - 1) / REF_MB * REF_MB;
float* const panel = ws;
float* const abuf = panel + REF_KC * REF_NB;
float* const sub = abuf + REF_MB * REF_KC;
float* const acc = sub + rp * REF_NB;
const int64_t g = SB.group > 0 ? SB.group : K;
const int64_t ngrp = (K + g - 1) / g;
/* No scale on either side: quant_dot's acc is 0 + (sub * 1) * 1, which is sub exactly (a
* sub-sum started at +0 is never -0). */
const bool unscaled = !SA.p && !SB.p && ngrp == 1;
for (int64_t i = 0; i < rp * REF_NB; ++i) acc[i] = 0.0f;
float sb[REF_NB];
for (int64_t gi = 0; gi < ngrp; ++gi) {
const int64_t k0 = gi * g, k1 = (k0 + g < K) ? k0 + g : K;
for (int64_t j = 0; j < REF_NB; ++j) sb[j] = (j < nb && SB.p) ? SB.grp(n0 + j, gi) : 1.0f;
for (int64_t i = 0; i < rp * REF_NB; ++i) sub[i] = 0.0f;
for (int64_t kc0 = k0; kc0 < k1; kc0 += REF_KC) {
const int64_t kl = (kc0 + REF_KC < k1) ? REF_KC : k1 - kc0;
for (int64_t j = 0; j < REF_NB; ++j) {
if (j < nb) {
const int64_t b0 = (n0 + j) * B.rs + kc0 * B.cs;
for (int64_t k = 0; k < kl; ++k)
panel[k * REF_NB + j] = LB::ld(B.t, b0 + k * B.cs);
} else {
for (int64_t k = 0; k < kl; ++k) panel[k * REF_NB + j] = 0.0f;
}
}
for (int64_t r0 = 0; r0 < R; r0 += REF_MB) {
for (int64_t i = 0; i < REF_MB; ++i) {
float* const ar = abuf + i * REF_KC;
if (r0 + i < R) {
const int64_t a0 = arow[r0 + i] * A.rs + kc0 * A.cs;
for (int64_t k = 0; k < kl; ++k) ar[k] = LA::ld(A.t, a0 + k * A.cs);
} else {
for (int64_t k = 0; k < kl; ++k) ar[k] = 0.0f;
}
}
tile_kernel(abuf, panel, sub + r0 * REF_NB, kl);
}
}
if (unscaled) {
for (int64_t i = 0; i < rp * REF_NB; ++i) acc[i] = sub[i];
continue;
}
for (int64_t r = 0; r < R; ++r) {
const float sa = SA.p ? SA.at(arow[r], k0) : 1.0f;
for (int64_t j = 0; j < REF_NB; ++j)
acc[r * REF_NB + j] += sub[r * REF_NB + j] * sa * sb[j];
}
}
return acc;
}
/* How a grid of R rows by N outputs is cut into tiles for one parallel loop: `nblk` column blocks
* of REF_NB, each split into `rgc` row groups of `rg` rows. Rows are split only when there are too
* few column blocks to occupy the threads (a narrow projection at a prefill M); a row group then
* pays its own panel widening, which is cheap against rg rows of products. */
struct TileGrid {
int64_t nblk, rg, rgc;
int64_t items() const { return nblk * rgc; }
};
static inline TileGrid tile_grid(int64_t R, int64_t N) {
TileGrid t;
t.nblk = (N + REF_NB - 1) / REF_NB;
int64_t rg = R;
if (t.nblk < 128 && R > 8 * REF_MB) {
rg = (R * t.nblk + 127) / 128;
if (rg < 8 * REF_MB) rg = 8 * REF_MB;
rg = (rg + REF_MB - 1) / REF_MB * REF_MB;
}
t.rg = rg > 0 ? rg : 1;
t.rgc = R > 0 ? (R + t.rg - 1) / t.rg : 0;
return t;
}
/* ------------------------------------------------------------------ weight tables
*
* A RAD_OPD_WTAB operand: `data` is a device (here, host) array of `shape[0]` pointers, one per
* expert, and `stride[0]` is 0 to say so -- see RAD_WTAB in abi/rad_runtime.h. The STACKED form,
* a plain rank-3 [n_expert, N, K] tensor with an ordinary leading stride, is accepted too and is
* what a caller outside the engine hands over: rad-kbench draws one dense plane and the fixture
* has nowhere to put a pointer array. Both are one expert per call to this, so nothing downstream
* has to know which arrived.
* A RAD_OPD_WTAB operand: `data` is an array of `shape[0]` pointers, one per expert, marked by
* `stride[0]` == 0 (RAD_WTAB in abi/rad_runtime.h). The stacked form, a plain rank-3
* [n_expert, N, K] tensor, is also accepted; callers outside the engine such as rad-kbench pass
* that, having nowhere to put a pointer array.
*
* Returns entry `e` as a rank-(rank-1) tensor over the same dtype, or a null-data tensor when the
* Returns entry `e` as a rank-(rank-1) tensor of the same dtype, or a null-data tensor when the
* operand cannot be read that way. */
static inline RadTensor expert_entry(const RadTensor* w, int64_t e) {
RadTensor t{};
@@ -232,8 +330,8 @@ static inline RadTensor expert_entry(const RadTensor* w, int64_t e) {
void* const* tab = (void* const*)w->data;
t.data = tab[e];
} else {
/* An element offset in a packed dtype can be mid-byte; the stacked form is only ever
* whole planes, so the arithmetic is done in bytes off the leading stride. */
/* A packed-dtype element offset can be mid-byte; stacked planes are whole bytes, so
* compute in bits off the leading stride and refuse a fractional byte. */
const int64_t bits = rad_dtype_bits(w->dtype);
const int64_t nbits = (int64_t)bits * w->stride[0] * e;
if (bits <= 0 || (nbits & 7)) return t; /* data stays null: unreadable */