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/core/kld.h
T
Deadcode 9ed3ef7a7b radiance 1.15.0
New models and formats:
- IQR for Qwen3.8-27B: weights on the int8 grid under GPTQ, a trellis down each GPTQ column
  (IQR-V2), and IQR-V3, a computed trellis code under one scale a row, kept at its codes' width in
  VRAM and decoded in the GEMM; 15 IQR-V3 recipes from ~2 to ~7 bits a weight
  (data/recipes/q38-27b-*-v3.recipe, docs/IQR.md)
- libhalo: a kernel library for Strix Halo (gfx1151), preferred there over libr3d
- Nemotron 3.5 Embed 1B and 8B (nemotron35_embed): text and image embeddings through a Pixtral
  tower, on the card and on the CPU
- Nemotron 3.5 Rerank 8B
- /v1/systemone: Cloudflare's clef and clef-flash, Convai Innovations' Laya and Mica beside
  LiquidAI d1; scored steps read the candidates' logits from the head table with no lm_head pass
- LFM2's 8-bit trunk: block-fp8 or int8 linears with the fused producers, and the d1-3B recipe
- DeepSeek-V4.1's reply format with DSML tool calls and typed argument tags

Weights on the drive:
- The file tier reads only the experts the pass routes rows to; the draft head's weights stay in
  the host pool
- A hot VRAM cache for routed experts read off the drive, reads deposited straight into free host
  pool slots, and copyless evictions; the file tier's counters are now ssd_*
- GLM-5 Next shards its routed experts by expert, not by width, so they can live on the drive
- The stall watch counts drive reads as progress, and the DMA lane's room check reads the lane's
  own completion fence

Quantisation tooling:
- --calib-corpus, --calib-dir and --calib-layers record each linear's input Hessian; beside
  --kld-record the reference's own run records them
- --kld-top N records a sparse KL reference (the N most likely tokens and the tail's mass)
- GPTQ in a third of the time with the same codes, trellis GPTQ on the card, vector codebooks
  with coordinate descent, and rad-convert --reuse repeated, each container supplying what the
  earlier ones lack

Speculation:
- Prompt lookup alone (--spec-ngram K, --spec-ngram-min/-max): each sequence keeps an index of its
  n-grams, and a proposal is verified only when its measured step cost says it pays;
  --draft-window adaptive follows it

Speed:
- IQR decode: the narrow tile splits K for four times the int8 tile's waves and folds each 128-K
  block once; at 8 tokens down and the output projections run in 0.61-0.82x and 0.70-0.92x FP8's
  time (2-6 bits); IQ6R-S-V3 with its DFlash2 drafter steps in 26.3 ms at TP2, against 29.4 ms
  before and FP8's 27.3
- Qwen MXFP4 feed-forward: the gated fold up to 16 rows, not 8
- libr4d: the gated prefill fold, the short-prompt split row, and the rows forms of rmsnorm and
  rope; KLATE again for the head-256 gqa6 paged prefill; gram_accum on the matrix cores

Features:
- /health, /v1/health and /ping answer 503 with "status": "stuck" once the device has advanced
  nothing for a minute or a queue has faulted, so a supervisor can restart a wedged server (#356)
- --image-gen-max-pixels and --image-gen-max-refs size an image generator's buffers (#344)
- The startup report lists only the options not at their default, with a count; -v lists all
  (#250)
- --preempt-wait-ms bounds how long a preempted prefill waits for its whole-recompute room
- --placement layer_offload spills dense layers so one --max-model-len sequence fits
- Startup refuses amdgpu with CWSR off and says how to fix it;
  --proceed-with-incompatible-configuration starts anyway under an UNSUPPORTED banner

Fixes:
- The n-gram read-ahead hands each row to the reader once and reads only as far ahead as the row
  cache holds, and pauses while decode reads: about one drive read a row instead of two (#355)
- A file-tier window reads each of its runs once, not once a run per run
- The decode split law's verify floor is bounded by the batch's workgroups: max_seqs 256 no longer
  declares 64x the scratch (#342)
- A prefetch DMA list's mark stays out of the signal pool until the engine has read it, and a
  prefill stage copies only the blocks its own tables name (#343)
- The split-KV row sum is read with fetch-inactive (#328)
- LFM2 on the host backend: libref and libavx serve the row-form ops it declares (#348)
- The preempted-prefill gate skips a KV group that holds no blocks (#352)
- Every request entry point bounded, gated and sanitised (JSON depth, invalid UTF-8, control bytes
  in the log, the dashboard's CSP, cross-origin browsers on the loopback-only routes); a prompt id
  outside the vocabulary refused at admission; --api-key "" refused at startup
- The telemetry the HTTP thread reads is atomic
- An RMS norm row no longer depends on how many rows its launch carries; prefill attention's
  rescale is decided by live rows only
- A --kld-record prefill chunk that finishes no sequence gets its logits on a tied-head model
- A kernel's lazily made counter block is zeroed outside the lock other ranks take (a TP2 first-step
  hang)

Changes: Plugin ABI 32. Every plugin exports a layout fingerprint of the ABI structs and the loader
refuses a plugin without it or with another (#302), so out-of-tree plugins must be rebuilt.
Appended: RadBuildCtx calib_* and gen_max_*, RadArchProbe gen_max_*, RadBatch score_only and
plugin_words, RadDeviceProps::unified, RadChatFormat arg_json_value_prefix.
2026-10-30 22:01:01 +01:00

159 lines
7.3 KiB
C++

/* kld.h -- the KL divergence of this model against a recorded reference, at every scored position
* of a fixed corpus.
*
* `--kld-record DIR` runs the corpus through the reference model (e.g. the bf16 checkpoint) once
* and writes its full-vocabulary log-probabilities at every scored position. `--kld-ref DIR` runs a
* candidate over the same tokens and compares its logits row by row, so each candidate costs one
* prefill of the corpus.
*
* Reported: the distribution of per-position KL(P_ref || Q_candidate) (mean, median, 99th, 99.9th,
* 99.99th percentile, max), top-1 agreement, and both models' perplexity on the corpus's next
* tokens. The tail matters: a small mean KL can hide confidently wrong positions.
*
* The whole vocabulary by default, not a top-k: a truncated KL is a lower bound, loosest for flat
* distributions, which are the high-KL tail. `--kld-top N` records the sparse form instead, for a
* corpus whose full rows would not fit a disk (10M positions of a 248K vocabulary are 5 TB): the N
* most likely tokens exactly and the mass of the rest. A candidate is then scored by the KL between
* the two distributions coarsened to those N tokens and one bucket for every other token, which by
* the data-processing inequality is at most the full KL and loses only what the reference spreads
* over its tail (at N = 512 a few 1e-4 nats on a flat position, nothing on a confident one).
*
* Reference files (DIR):
* kld.json the manifest: the model, the corpus, every document's source, token count, first
* scored position and first row. Written last, so an interrupted run leaves a
* directory no --kld-ref reads.
* tokens.i32 every document's tokens, concatenated, so the candidate scores the reference's
* tokens rather than a re-tokenisation.
* logp.f16 [rows, n_vocab] log-probabilities, f16: finest near log p = 0, coarse only for
* tail tokens with little mass.
* top.bin [rows, kKldTop] {int32 id, f32 log p}: the most likely tokens exactly, where a
* confident position's KL lives (f16 would cost ~1e-4 nats).
* rows.f32 [rows, 2] the reference's own next-token NLL and entropy.
*
* The sparse form (`--kld-top N`, format radiance-kld-top-1) has no logp.f16; top.bin is
* [rows, N] {int32 id, f32 log p}, most likely first, and rows.f32 is [rows, 3], the third column
* the mass of every token outside the row's N, summed in double from the full row.
*
* Document d scores positions [score_from, n - 1): the logits at p predict token p + 1, and the
* last token predicts nothing in the corpus. Its row r is row0 + p - score_from.
*/
#pragma once
#include "rad_core.h"
#include <atomic>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
namespace rad {
class Vocab;
/* One scoring entry's rows of a step: request `id`'s prompt positions [pos0, pos0 + n) are rows
* [row0, row0 + n) of the block the engine copied back. */
struct KldSpan {
uint64_t id = 0;
int64_t pos0 = 0;
int64_t n = 0;
int64_t row0 = 0;
};
/* One rank's part of that block: its rows of `width` f32 logits, the vocabulary from `vocab0`. */
struct KldShard {
const float* rows = nullptr;
int64_t vocab0 = 0;
int64_t width = 0;
};
/* The exact head kept beside every f16 row. */
static constexpr int kKldTop = 16;
/* The largest --kld-top: a row of the sparse form is 8 bytes a token. */
static constexpr int kKldTopMax = 8192;
class Kld {
public:
Kld();
~Kld();
/* Reads the corpus and creates DIR (record), or reads DIR's manifest and opens its rows (ref).
* `model` names this run's model in the manifest and the report. */
int open(const Config& cfg, std::shared_ptr<const Vocab> vocab, int64_t n_vocab,
const std::string& model);
/* open() split for already-tokenised documents (tests): add_doc() each, then record_into()
* creates DIR; score_against() is the whole ref mode. */
int add_doc(const std::string& source, const std::vector<int32_t>& ids, int64_t score_from);
int record_into(const std::string& dir, int64_t n_vocab, const std::string& model, int top = 0);
int score_against(const std::string& dir, const std::string& out, int64_t n_vocab,
const std::string& model);
/* The corpus alone, for a run that only drives it through prefill (--calib-corpus). */
int load(const std::string& corpus, std::shared_ptr<const Vocab> vocab) { return load_corpus(corpus, vocab); }
int64_t n_tokens() const { return (int64_t)tokens_.size(); }
size_t n_docs() const { return docs_.size(); }
const std::string& source(size_t d) const { return docs_[d].source; }
/* Document d's tokens, and the request id the engine submits it under: d + 1. */
std::vector<int32_t> tokens(size_t d) const;
int64_t n_rows() const { return n_rows_; }
int64_t rows_done() const { return done_n_.load(std::memory_order_relaxed); }
/* A step's scored rows. Rows outside a document's scored range are skipped; a row seen twice
* (re-prefill after preemption) is recomputed. Blocks until every row is written or scored,
* since the engine reuses the block next step. */
int score(const std::vector<KldSpan>& spans, const std::vector<KldShard>& shards);
/* Record: checks every row was written and writes the manifest. Ref: the report, printed and,
* with --kld-out, written. */
int finish();
private:
struct Doc {
std::string source;
int64_t off = 0; /* into tokens_ */
int64_t n = 0;
int64_t score_from = 0;
int64_t row0 = 0;
int64_t rows() const { return n - 1 - score_from; }
};
struct Job {
int64_t row; /* the reference's row */
int64_t at; /* the row in the copied block */
int32_t next; /* the corpus's next token */
};
int load_corpus(const std::string& path, const std::shared_ptr<const Vocab>& vocab);
int load_ref(const std::string& dir);
void size_rows();
/* A worker's row-sized scratch: the f16 row (full form), the head and the head's marks
* (sparse form). */
struct Scratch;
void record_row(const Job& j, const std::vector<KldShard>& sh, Scratch& w);
void score_row(const Job& j, const std::vector<KldShard>& sh, Scratch& w);
void score_row_top(const Job& j, const std::vector<KldShard>& sh, Scratch& w);
int report();
bool record_ = false;
std::string dir_, out_, model_, ref_model_, corpus_;
uint64_t corpus_hash_ = 0;
int64_t n_vocab_ = 0;
int top_ = kKldTop; /* entries a row of top.bin holds */
bool sparse_ = false; /* the --kld-top form: no logp.f16, tail mass in rows.f32 */
int64_t rows_w_ = 2; /* rows.f32 columns */
std::vector<int32_t> tokens_;
std::vector<Doc> docs_;
int64_t n_rows_ = 0;
int fd_logp_ = -1, fd_top_ = -1, fd_rows_ = -1;
/* Per reference row. `seen_` marks a row written or scored; the rest is the ref mode's. */
std::vector<uint8_t> seen_;
std::atomic<int64_t> done_n_{0};
std::vector<float> kl_, nll_q_, nll_p_;
std::vector<uint8_t> top1_;
std::atomic<int> io_err_{0};
float f16_lut_[65536];
};
} /* namespace rad */