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

+110 -112
View File
@@ -1,39 +1,23 @@
/* kfixture.h -- the recorded reference a kernel library is checked against, with no model in the
* room.
/* kfixture.h -- a recorded reference to check kernel libraries against without a model.
*
* WHY THIS EXISTS. Checking against a real container -- declare the model's graph, take the
* weights out of the .rad in the layout the resolved kernel asked for, run the oracle beside the
* kernel and compare -- is the right test of a CONTAINER and the wrong test of a LIBRARY. It needs
* a 27B file nobody has on a build machine, and it spends its whole runtime inside libref, which
* is a straight-loop host implementation: at model shapes a single `logits_gemm` over a 248K
* vocabulary costs seconds of wall clock and a single fp8 GEMM tens of seconds, so a full pass
* runs for tens of minutes to produce numbers that do not change between runs.
* Checking against a real container needs a 27B model file and runs libref (straight host loops)
* at model shapes, which takes tens of minutes. Instead the oracle runs once when a fixture is
* recorded, and the result is checked in. Libraries are then checked in seconds on any machine,
* and the fixture doubles as a regression test of libref itself (a live comparison would move
* both sides together).
*
* So the oracle runs ONCE, when a fixture is recorded, and what it produced is checked in. A
* library is then checked against a fixed artifact in seconds, on any machine, with no model
* present -- and the artifact is a regression test on libref itself, which a live oracle can never
* be: if ref's arithmetic drifts, a live comparison moves both sides together and reports
* agreement.
* Full outputs at model shapes would be tens of gigabytes, so per output operand it stores:
*
* WHAT IS RECORDED, AND WHY IT IS NOT THE WHOLE OUTPUT. A recorded reference for every case at
* real model shapes is tens of gigabytes -- one `logits_gemm` output alone is 31 MB -- and a
* repository is not the place for it. What is stored instead, per output operand:
* - the sum of squares over every element, and the count: the rel_l2 denominator, and the check
* a sample cannot make (a correct first tile followed by zeros);
* - the number of non-finite elements (a NaN against a number is a failure, checked first);
* - the values at KF_SAMPLES seed-derived positions. 1024 samples catch an error rate of 1e-3
* with probability 64% and 1e-2 with probability > 1 - 3e-5;
* - from version 5, KF_SKETCH seeded random-sign projections of the whole output. Their
* difference estimates the full-tensor rel_l2 to about +-25%, so a localised error the samples
* miss and the norm cannot see (one permuted or sign-flipped tile of a large output) fails.
*
* - the sum of squares over EVERY element, and the element count. This is the rel_l2
* denominator and it is also the check a sample cannot make: a kernel that writes a correct
* first tile and zeros the rest has the right values everywhere the sample looks and the
* wrong norm.
* - how many elements were non-finite. Not a tolerance question (spec: a NaN on one side and a
* number on the other is a failure, checked first).
* - the value at N sampled positions, the positions derived from a seed so only the values are
* stored. 1024 of them: a kernel wrong in one element per thousand is caught with probability
* 1 - (1 - 1e-3)^1024 = 64%, one wrong in a hundred with probability better than 1 - 3e-5, and
* a kernel wrong in one element out of seventeen million is beneath what an rel_l2 over
* seventeen million elements would have reported anyway.
*
* INPUTS ARE NOT STORED AT ALL. They are drawn from a seed that IS stored, by the same code on
* both sides, so the bytes are identical without anyone shipping them. That is also what keeps
* the file small enough to read at every ctest run.
* Inputs are not stored: both sides draw them from the stored seed with the same code.
*/
#pragma once
#include "rad_core.h"
@@ -44,29 +28,35 @@
namespace rad {
/* How many positions of each output operand are sampled. Fixed rather than a parameter: it is
* baked into every recorded file, and a file whose sample count differs from the reader's is a
* file the reader would silently misinterpret. */
/* Positions sampled per output operand. Fixed because it is baked into every recorded file. */
constexpr int KF_SAMPLES = 1024;
/* The block the full-tensor reduction is threaded over. Part of the contract for the same reason
* RAD_FILL_BLOCK is: the partial sums are added back in block order, so the answer is the same
* however many threads ran, and a reference recorded on one machine reads on another. */
/* Block size of the threaded full-tensor reduction. Part of the contract, like RAD_FILL_BLOCK:
* partial sums are combined in block order, so the result is independent of thread count. */
constexpr int64_t KF_REDUCE_BLOCK = 1 << 18;
/* The fill laws, mirroring OpdShape::Fill. Named here rather than shared with it because the
* fixture is the on-disk contract: OpdShape is free to renumber, this is not. */
/* Projections per output in a version-5 recording. 32 gives the rel_l2 estimate a relative spread
* of sqrt(2/32), about 25%. Fixed because it is baked into every recorded file. */
constexpr int KF_SKETCH = 32;
/* The format version this tree writes. Versions 4 and 5 still load: version 4 lacks the sketch, the
* prefix fill law and drawn weight tables (KfOperand::undrawn), version 5 the sampler laws (the
* request-row fill and the token history). */
constexpr uint32_t KF_VERSION = 6;
/* The fill laws, mirroring OpdShape::Fill. Separate because these values are on disk and must not
* be renumbered. */
enum {
KF_FILL_NORMAL = 0,
KF_FILL_SIGMOID = 1,
KF_FILL_GATE_CUMSUM = 2,
KF_FILL_TRI_LOWER = 3,
KF_FILL_UNIT_ROW = 4,
KF_FILL_SAMPLE_PARAMS = 5,
};
/* A geometry key/value, stored TYPED. The alternative is to store `Geometry::str()` and parse it
* back, and there is no parser -- writing one means deciding whether `1e+07` is a rope theta or a
* string and whether `f32` is a float, on every load, forever. */
/* A geometry key/value, stored typed, so loading needs no type-guessing parser of
* `Geometry::str()`. */
struct KfParam {
std::string key;
int kind = 0; /* RAD_P_INT | RAD_P_F64 | RAD_P_STR */
@@ -81,21 +71,25 @@ struct KfOperand {
std::vector<int64_t> shape;
bool absent = false;
/* HOW THE INPUT WAS DRAWN. Replay has to reproduce the exact bytes the oracle saw, and the
* draw is not uniform: an index operand is drawn over a range, a cumulative-length operand is
* the batch's shape rather than a draw at all, and a gated-delta-net decay is a cumulative sum
* of log-decays because a normal draw makes the reference overflow. All of that lives in
* OpdShape at record time and none of it is recoverable from the shape alone. */
/* How the input was drawn (copied from OpdShape at record time), so replay reproduces the
* exact bytes; it cannot be recovered from the shape. */
int fill = 0;
bool is_index = false, idx_unique = false, idx_cu = false;
bool is_index = false, idx_unique = false, idx_cu = false, idx_prefix = false;
bool idx_history = false;
int64_t idx_max = 0, idx_const = -1, fill_chunk = 0;
/* A weight table (RAD_OPD_WTAB) in a version-4 recording, which drew it as zeros. Its
* reference was computed on zeros, so the replay draws zeros too; such a case checks nothing
* and is reported as vacuous until the fixture is recorded again. */
bool undrawn = false;
/* A WEIGHT OPERAND IS PLANES OF AN ENCODING (spec §4.3), and a kernel lays it out from those
* planes rather than from the tensor the reference reads. So the recording carries the
* encoding, which planes this operand selects (indices into it) and each one's extents in its
* own elements; the planes are drawn from the operand's seed and the reference reads what
* they decode to (kf_planes_logical). Absent for every other operand, and for a weight
* written by hand in a test, which is then drawn as the tensor it is. */
/* A weight operand is planes of an encoding (spec §4.3), which kernels lay out themselves.
* Recorded: the encoding, the selected plane indices, and each plane's extents in its own
* elements. The planes are drawn from the operand's seed; the reference reads what they decode
* to (kf_planes_logical). Absent for other operands and for hand-written test weights, which
* are drawn as plain tensors.
*
* A weight table (RAD_OPD_WTAB) records one expert's encoding and selection: its shape is
* [n_expert, ...] and each expert's planes are drawn from its own seed (kf_draw_table). */
bool has_enc = false;
RadEncoding enc{};
std::vector<int> sel;
@@ -107,6 +101,7 @@ struct KfOperand {
double ref_sumsq = 0;
int64_t ref_nonfinite = 0;
std::vector<float> sample; /* KF_SAMPLES values at seed-derived positions */
std::vector<double> sketch; /* KF_SKETCH projections (kf_sketch); empty in version 4 */
};
struct KfCase {
@@ -114,70 +109,58 @@ struct KfCase {
std::vector<KfParam> params;
std::vector<KfOperand> opd;
uint64_t seed = 0;
/* THE RANGED PARAMETER'S VALUE, recorded rather than re-derived. The draw needs it as a
* scalar -- a cumulative-length operand divides a token count across sequences -- and the key
* it lives under is the op's own ("M", "q_len", ...). A reader guessing which key to read
* would guess wrong for exactly the ops whose ranged key is unusual. */
/* The ranged parameter's value, recorded because its key varies by op ("M", "q_len", ...).
* The draw needs it, e.g. to divide a token count across sequences. */
int64_t m = 1;
/* The band this point came from, carried for the report only: "M=65 is the bottom of the
* band M in (64, 512]" is most of what makes a tail failure legible. */
/* The band this point came from, for the report only. */
std::string band;
/* A case with no reference: recorded because it is worth TIMING at a real model shape and its
* output is too large to carry. Speed is measurable without an oracle; correctness is not. */
bool speed_only = false;
};
struct Kfixture {
/* 4 carries each weight operand's encoding and selected planes (KfOperand::enc); an earlier
* file has neither and is refused -- record it again. */
uint32_t version = 4;
/* Version 4 carries each weight operand's encoding and selected planes (KfOperand::enc).
* Version 5 adds the sketch, the prefix fill law and drawn weight tables, version 6 the
* sampler laws. Earlier files are refused and must be re-recorded. */
uint32_t version = KF_VERSION;
std::string source; /* the container it was recorded from */
std::string oracle; /* the plugin and version that produced the references */
std::string recorded; /* an ISO date, so a stale fixture is visible */
std::vector<KfCase> cases;
/* load() refuses, as RAD_E_FORMAT, a file it cannot read whole or whose contents cannot
* describe a case: an unknown role, fill law or index flag, a law newer than the file's
* version, a dtype the core cannot size, an extent that is not positive or an element count
* past 2^40, a plane selection without extents, a sample count other than the operand's. A
* corrupt file must not reach an allocation, and an unknown law must not be drawn as another.
*/
int load(const std::string& path);
/* Writes KF_VERSION whatever `version` holds. */
int save(const std::string& path) const;
};
/* The positions sampled in an operand of `n` elements, derived rather than stored. Both the
* recorder and the reader call this, which is the only reason storing just the values works. */
/* The sampled positions in an operand of `n` elements; derived identically by recorder and reader,
* so only values are stored. */
void kf_sample_positions(uint64_t seed, int64_t n, std::vector<int64_t>* out);
/* The seed for operand `k` of a case. One function so record and replay cannot drift. */
/* The seed for operand `k` of a case, shared by record and replay. Inputs use k; an output's
* sample positions and sketch use kf_output_seed. */
uint64_t kf_operand_seed(uint64_t case_seed, int k);
inline uint64_t kf_output_seed(uint64_t case_seed, int k) {
return kf_operand_seed(case_seed, 0x5000 + k);
}
/* ================================================================== drawing an input
*
* ONE IMPLEMENTATION, CALLED BY BOTH SIDES. The recorder and the reader must produce byte-
* identical inputs or every recorded reference is noise, and the draw is not uniform: an index
* operand is drawn over a range that depends on the operand before it, a cumulative-length
* operand is the batch's shape rather than a draw at all, and a gated-delta-net decay is a
* cumulative sum of log-decays because a normal draw makes the reference overflow to 2e38. Two
* copies of that, one in the recorder and one in the reader, is two copies that will differ.
*
* The operand before this one -- `opd[k - 1]` -- supplies an index operand's default range in its
* leading extent; for the first operand there is none. `vocab_rows` is the fallback range in that
* case: a token id has to come from somewhere and the model's vocabulary is where.
*/
/* ================================================================== the weight a kernel reads
*
* A LIBRARY MAY WANT ITS WEIGHTS ARRANGED, AND A FIXTURE CANNOT CARRY EVERY ARRANGEMENT.
*
* libr4d's fp8 GEMMs read their weight in WMMA fragment order and its int4 experts with their
* nibbles reordered; libref reads rows. A container holds neither -- it holds the encoding's
* canonical planes, and each kernel relayouts them at load. The fixture does the same: it carries
* the encoding, draws the planes, and each library runs its own layout hooks over them to get the
* bytes it actually reads, while the reference reads what the planes decode to. Same planes in,
* each kernel's own bytes out, which is what makes one recording serve libraries that disagree.
* Kernels may want weights arranged differently (libr4d's fp8 GEMMs read WMMA fragment order, its
* int4 experts reordered nibbles; libref reads rows). Like a container, the fixture carries the
* encoding's canonical planes; each library lays them out with its own hooks, and the reference
* reads what the planes decode to. One recording thus serves libraries with different layouts.
*/
/* The operand's encoding and selection, from the declaration it was recorded against. */
void kf_operand_enc(const WeightInfo& w, KfOperand* d);
/* A weight operand's selected planes, drawn from `seed`: each dense in its own elements, a row
* starting on a byte, and every value one its role can hold -- a code inside its codebook, a
* permutation that is one, an exponent scale that neither overflows nor vanishes. */
/* A weight operand's selected planes drawn from `seed`: dense in their own elements, rows starting
* on a byte, and every value valid for its role (codes inside the codebook, real permutations,
* exponent scales that neither overflow nor vanish). */
void kf_draw_planes(const KfOperand& d, uint64_t seed, std::vector<std::vector<uint8_t>>* out);
/* What a reference reads for those planes: the operand's dtype and shape (enc_selection_logical).
@@ -185,37 +168,52 @@ void kf_draw_planes(const KfOperand& d, uint64_t seed, std::vector<std::vector<u
int kf_planes_logical(const KfOperand& d, const std::vector<std::vector<uint8_t>>& planes,
std::vector<uint8_t>* out, std::string* why);
/* The bytes kernel `info` reads for operand `operand`, laid out from the planes. RAD_OK with
* `out` holding them and `lay` describing them; RAD_E_UNSUPPORTED when the kernel reads the
* selected plane as it is -- the bytes kf_planes_logical gives, widened if the operand is wider;
* RAD_E_DTYPE when the kernel does not read this encoding at all. Anything else is a decline the
* caller must name rather than absorb. */
/* The bytes kernel `info` reads for `operand`, laid out from the planes. Returns RAD_OK with `out`
* and `lay` filled; RAD_E_UNSUPPORTED when the kernel reads the plane as is (the
* kf_planes_logical bytes, widened if the operand is wider); RAD_E_DTYPE when the kernel does not
* read this encoding. Any other error is a decline the caller must report. */
int kf_lay_weight(const RadKernelInfo* info, const RadParam* p, int n_p, int operand,
const KfOperand& d, const std::vector<std::vector<uint8_t>>& planes,
RadLayout* lay, std::vector<uint8_t>* out);
/* How the engine describes a laid-out weight operand: the STORED dtype with the DECLARED shape.
* Not the RadLayout's shape -- see the comment on the definition. */
/* Describe a laid-out weight operand as the engine does: stored dtype, declared shape (not the
* RadLayout's; see the definition). */
void kf_describe_laid(const KfOperand& d, const RadLayout& lay, void* data, RadTensor* out);
/* The seed a WEIGHT operand is drawn from: its own identity, not the case's. Public because it
* is also the key a caller caches the drawn bytes under -- see kf_draw. */
/* The seed a weight operand is drawn from: its own identity, not the case's. Also the cache key for
* drawn bytes (see kf_draw). */
uint64_t kf_weight_seed(const KfOperand& d, size_t k);
/* THE FALLBACK RANGE FOR AN INDEX OPERAND WITH NOTHING TO INDEX, and it exists so that the
* recorder and the reader cannot disagree about it. The recorder knows the model's vocabulary and
* the reader does not, so a rule that reached for `meta.n_vocab` on one side and something else on
* the other would draw different token ids from the same seed -- a divergence that surfaces as a
* wrong answer in a kernel that is right. Derived from the case itself, identically, on both. */
/* A weight table's logical bytes, [n_expert, ...] in the operand's dtype: expert e's planes drawn
* from the table's identity seed and e, and decoded as kf_planes_logical does. RAD_OK, or the
* refusal with `why`. */
int kf_draw_table(const KfOperand& d, size_t k, std::vector<uint8_t>* out, std::string* why);
/* ================================================================== drawing an input
*
* One implementation for recorder and reader, which must produce byte-identical inputs. An index
* operand's default range is the leading extent of the previous operand, `opd[k - 1]`; the first
* operand falls back to `vocab_rows`.
*/
/* The fallback index range, derived from the case alone so recorder (which knows the model's
* vocabulary) and reader (which does not) agree. */
int64_t kf_vocab_rows(const std::vector<KfOperand>& opd);
/* Draws operand `k`'s bytes. Large plain operands are drawn on every core: the stream is cut into
* RAD_FILL_BLOCK blocks seeded by index, so the bytes do not depend on the thread count. A weight
* table with an encoding goes through kf_draw_table (and an undecodable one comes back zeroed;
* call kf_draw_table to learn why). */
void kf_draw(const std::vector<KfOperand>& opd, size_t k, int64_t M, int64_t vocab_rows,
uint64_t seed, std::vector<uint8_t>* out);
/* The three numbers a reference is reduced to, computed over a widened output. Called by the
* recorder on the ORACLE's output and by the reader on the KERNEL's, so the two are reduced the
* same way. */
/* Reduce a widened output to the reference's three quantities; used on the oracle's output when
* recording and the kernel's when replaying. */
void kf_reduce(const float* v, int64_t n, uint64_t seed, double* sumsq, int64_t* nonfinite,
std::vector<float>* sample);
/* KF_SKETCH projections of the finite elements of `v`, each element's signs derived from `seed`
* and its index. Summed in double, block by block in block order, so the result is independent of
* thread count. */
void kf_sketch(const float* v, int64_t n, uint64_t seed, std::vector<double>* out);
} /* namespace rad */