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/libquant/lq_grid.h
T
Deadcode 19d4e8fe0e radiance 1.17.0
IQR-V4 for Qwen3.8-27B:
- 15 recipes, data/recipes/q38-27b-iq2r-v4 .. q38-27b-iq8r-v4 (2.5 to 8 bits a weight), docs/IQR.md
  "IQR-V4". On the quantisation board's private split every entry has 2-19% less KL than its V3 twin
  of the same size (10-19% from 2.5 to 5.4 bits); IQ4R-L-V4 is 24% below the official FP8 checkpoint
  at 55% of its size, and IQ2R-L-V4 (2.8 bpw) is below the MXFP4 checkpoint. Containers with the
  DFlash2 drafter and the vision tower: huggingface.co/StillDeadcode/qwen3.8-27b-iqr-v4, served by
  deploy/compose/qwen3.8-27b-iqr-v4.yaml (IQR_V4 names the size)
- GPTQ runs in act order inside the column trellis (tcq_order=1); the head-fed projections
  (attn_output, ssm_out) take a bf16 scale a (row, 128) block from five bits up; widths from
  measured prices
- A vocabulary-weighted lm_head: four width classes of the computed trellis (output.k<B>), the
  tokens carrying the probability on the widest, decoded in one launch by logits_gemm_vocab with
  16-bit head activations; 0.62 ms at one token on a TP2 rank against the int8 head's 1.02 ms.
  tools/vhead.py and data/vhead/ build the class split from the bf16 checkpoint; libquant reads the
  head's input Hessian for every class
- At IQ6R-L and up, the int8 lm_head is GPTQ'd against its input Hessian (same bytes, median KL
  -8..-12%)
- embed_lookup_iqr takes u7/u8 tables under a zero point and the 4096-state trellis at 6 and 7 bits
- The whitepaper: radiance.stilldeadco.de/whitepapers/iqr-v4.pdf

Faster IQR kernels:
- The one-token GEMV gives each 16-row weight tile a block whose waves split K (5120 x 6144 at one
  token 42.3 -> 30.0 us), with a biased-int fold, a v_perm activation broadcast and one code stream
  a column
- A 16-token WMMA tile without block barriers serves 2 to 16 tokens (6- and 7-bit codes from one
  token): a 4-bit weight takes 0.52-0.66 of FP8's time from one token to sixteen, 1.15-1.74x V3's
  kernels from four tokens up
- The activation quantisers after the IQR gate_up (gemm_iqr_gated), the delta net's decode conv
  fold (gdn_conv_recurrent_update_had) and the attention gate (gate_had_quant_i8g) are folded into
  their producers, byte for byte the same codes: 128 fewer launches a decode step
- End to end on one R9700 (TP1, no speculation): IQ4R-S-V4 decodes one stream in 24.7 ms against
  the MXFP4 checkpoint's 25.8 at five times lower KL; IQ3R-S-V4 is faster than MXFP4 at 1 to 8
  streams. Prefill stays 8-9% behind MXFP4's 4-bit tile

Quantisation tooling:
- rescale=1 refits each row's scale to its codes after GPTQ; gptqsim measures a tensor's W16A16
  form (tcq_code=mad16); an int8 trunk's calibration Grams can be recorded from the codes its GEMMs
  read (--calib-dir, TP1)
- Fixed: the GPU trellis packs tcq_code=mad16's values two int16 a register

New models (merged):
- Spark 2.5 (spark25: bf16 and rotated-int8 IQ8R checkpoints, tensor parallelism with exact
  collectives, reasoning and tagged tool calls) with its DSpark drafter (exact attention and Markov
  walk ops), docs/SPARK25-IQ8R.md (@gostar)
- Ling-3.0-flash (bailing_hybrid: KDA, dense latent attention with mla_attn_paged, group-limited
  sigmoid routing, the clipped SwiGLU and its MTP layer as a serial drafter), its w4a8 recipes and
  compose files for four and two cards (@keptn42)
- Gemma 4 12B (gemma4_unified, served by the gemma4 plugins): its picture and audio embedders, a
  picture's placeholders read both ways on the sliding layers as the model was trained (the
  scheduler ends no prefill chunk inside such a run; RadBatch::n_bidi), windowed attention at
  head_dim 512, the assistant head as MTP drafter at one, two and four ranks, suppress_tokens from
  generation_config, bf16 and i8g compose files (@keptn42)

Merged:
- The prefill KV split sized by the live KV length rather than the model's max_ctx, recorded passes
  keyed by their split geometry, and --gpu-memory-mib, the inverse of --gpu-headroom-mib (@gostar)
- Possessive quantifiers in tokenizer patterns where the greedy reading provably matches the same
  (Ling-3.0's), and the Grafana dashboard's first screen of totals (@keptn42)
- The dashboard's history line (decode, prefill and TTFT over finished requests, kept in the
  browser, JSON export), session extremes that age and reset on a restart, card-count changes, an
  Advanced tab for the poll intervals, each request's prefill time from admission in /stats
  (prefill_s), and the page's JavaScript run by tests/js (@Memoria)
- From PR #390: the file tier reads a routed layer with its own router's counts and waits for its
  deposits; the stop-string hold-back keeps back only a tail that can still become a stop string
  (before, every delta waited up to the longest stop's length); the sampler's planes are sized by
  the step's token bound, the n-gram ban runs beside a drafter, and a top-1 row issues no candidate
  cut (@Code000)
- The n-gram reader refills its reads as they land, and a decode gather waits on the other rank's
  read of a row instead of reading it again (#369, @pokesjw)
- libr3d's GEMM structs value-initialised so the tape audit sees no padding (@RikshaDriver)

Added:
- --ngram-placement disk=DIR: the n-gram table read from its own file, DIR/ngram-<id>.bin, filled
  from the container at the first start, on a faster or separate drive (#130)

Fixed:
- input_reference's H.264 round trip runs on libopenh264, which the image carries, at a quantiser
  held at the crf (a side under 16 padded and cut back); a build with no H.264 encoder answers 501
  (#163)
- kvtier: at most 1024 session rows under one shared first block; with one shared prompt the list
  grew a row a request and prefill fell from 9k to 2.5k tok/s after 500K requests (#375)
- Speculative steps on an architecture with no pass-key callback keep their recorded passes under
  the live-KV prefill split

Plugin ABI 35:
- RadArgs::kv_used_max_h and wgp_count, RadBatch::kv_used_max_h and positions_h, the optional
  rad_arch_pass_key export and RAD_KERNEL_CAP_DYNAMIC_PREFILL_SPLIT; RadEncoderDecl::bidirectional
  and RadBatch's n_bidi runs. The layout fingerprint table is regenerated; out-of-tree plugins
  rebuild against 35

Thanks to @gostar, @keptn42, @Memoria, @Code000, @pokesjw and @RikshaDriver.
2026-11-04 18:56:58 +01:00

285 lines
17 KiB
C++

/* lq_grid.h -- a quantisation grid: the codes a weight may take and how its scales sit over it.
*
* libquant separates three things a recipe names independently (spec.md §4.5): the grid (code type
* plus the scale, zero and transform structure around it); the algorithm that picks codes on it
* (round-to-nearest, GPTQ, AWQ, ...); and the stored encoding, which follows from the grid alone.
* Every algorithm takes the same grid options.
*
* A grid is parsed from a rule's options:
*
* codes=DT the code dtype: i2..i8, i16, i32 (symmetric), u1..u8, u16 with zero=
* (asymmetric), or a float code: fp8_e4m3, fp8_e5m2, fp6_e2m3, fp6_e3m2,
* fp4_e2m1. Required, except beside table=, which implies it.
* table=T a scalar codebook: each code indexes a table of values, which a block's scale
* multiplies. nf4 (QLoRA's NormalFloat), iq4nl (llama.cpp's IQ4_NL values),
* w4nl (the rotated expert grid libr4d's w4nla8h kernel decodes), w5nl (its
* 32-level form, libr4d's w5nl64a8h),
* binary {-1, +1}, ternary {-1, 0, +1}, or the values themselves, `;`-separated.
* The codes are the narrowest unsigned width that indexes it.
* group=N one scale per N columns of a row -- block [1 x N]
* block=RxC the scale block in general; `*` is the whole extent: 128x128, 1x*, *x*
* (default: one scale a row, 1x*)
* scale=DT the scale dtype: f32, bf16, f16, fp8_e4m3, e8m0 (default bf16); under scale2=
* also an integer, u4..u8, i6, i8 -- a K-quant's sub-block scale
* scale2=DT a second scale level the first is relative to: f32, bf16, f16, fp8_e4m3, e8m0.
* NVFP4 is fp4 codes, an fp8_e4m3 scale per [1 x 16] and an f32 scale2 per tensor.
* scale2_value=X the second level fixed at X for all its blocks instead of derived from the
* largest first-level scale under it; must be exact in scale2=. For E4M3
* sub-scales over a model whose scales fit E4M3's range, only a known offset is
* needed (libr4d's w4nl64a8h experts). A first-level value it would push past its
* dtype's top is clamped there, reported once on stderr.
* group2=N the second level's block, [1 x N]; or
* block2=RxC in general (default: the whole tensor, *x*). A multiple of the first level's
* block in each extent.
* zero=DT an integer zero point a block, making the grid asymmetric: u8, u4 ...
* transform=fwhtN rotate each row by the unnormalised Hadamard before quantising
* rule=R how a block's scale is chosen: absmax (the default), mx (the OCP shared
* exponent, the default for an e8m0 scale), fixed (scale_value=, every block),
* search (the scale, or the asymmetric range, of least squared error over a
* sweep of candidates and their least-squares refits, weighted by the imatrix's
* column importances when the converter has them)
* scale_value=X the fixed scale for rule=fixed
* clamp=full|sym a signed code's range: [-2^(b-1), 2^(b-1)-1], or symmetric about zero
*
* The rules are exactly those the containers in service were made with (the order of a division,
* which number is rounded to the scale dtype before codes are chosen, what an all-zero block gets),
* so a recipe reproduces those containers byte for byte. Each is stated where it is implemented.
* Forms no container in service uses (a codebook, a second level, rule=search, 16- and 32-bit
* codes) take a separate general path, which leaves those rules untouched.
*/
#pragma once
#include "rad_quant.h"
#include <string>
#include <vector>
namespace lq {
struct Grid {
uint32_t codes = RAD_DT_INVALID;
bool is_float = false; /* a float code: fp8, fp6, fp4 */
bool asym = false; /* a zero plane */
bool wide = false; /* a 16- or 32-bit integer code, chosen in double */
uint32_t scale_dt = RAD_BF16;
uint32_t zero_dt = RAD_DT_INVALID;
int64_t br = 1, bc = 0; /* the scale block; 0 = the whole extent */
int64_t fwht = 0; /* the transform's order, or 0 */
int qmin = 0, qmax = 0; /* an integer code's range */
float fmax = 0.0f; /* a float code's largest magnitude */
int emax = 0; /* a float code's largest normal exponent, for rule=mx */
enum Rule { ABSMAX, MX, FIXED, SEARCH } rule = ABSMAX;
double fixed = 0.0;
/* A scalar codebook: the value of each code, in code order. */
std::vector<float> table;
uint32_t table_dt = RAD_DT_INVALID;
std::vector<int> table_order; /* the codes, by ascending value */
float tpeak = 0.0f; /* the entry of largest magnitude, signed */
/* The extremes are not mirror images (iq4nl: -127 and +113), so a block's scale takes the
* sign of its largest-magnitude value and that value lands on `tpeak` exactly. */
bool tsigned = false;
/* A second scale level, which the first is relative to. */
bool lv2 = false;
uint32_t scale2_dt = RAD_DT_INVALID;
int64_t br2 = 0, bc2 = 0; /* its block; 0 = the whole extent */
double fixed2 = 0.0; /* scale2_value: every block's stored value, or 0 */
bool l1_int = false; /* the first level is an integer sub-scale */
int l1_lo = 0, l1_hi = 0; /* its range */
float l1max = 0.0f; /* the largest first-level value its dtype holds */
/* A vector codebook (vq=FILE): one code per `vdim` consecutive columns, indexing `vq`
* (`ventries` rows of `vdim` integer values in -127..127), stored as the encoding's `grid`
* table. A block's scale matches its rms to the codebook's: a value v is the entry over
* `vq_unit`, the units a unit-rms vector the codebook was fitted to spans. */
std::vector<float> vq;
int64_t vdim = 0, ventries = 0;
double vq_unit = 1.0;
/* The search's layout: vq_soa[j * ventries + k] = entry k's j-th value, vq_n2[k] its squared
* norm, so the distances to every entry vectorise across entries. */
std::vector<float> vq_soa, vq_n2;
/* A trellis (tcq=L): the codes of a column, one per row, are the bits a bitshift trellis
* shifts in. Row r's value is tcq_lut[state_r] times its block's scale, where state_r =
* (state_{r-1} << k | code_r) mod 2^L, k the code's bits. Each run of tcq_rows rows is
* tail-biting: the state before its first row is the one after its last, which its last
* ceil(L/k) codes make. A block's scale matches its rms to the table's, as a vector grid's
* does (vq_unit). */
int tcq_L = 0, tcq_k = 0;
int64_t tcq_rows = 0;
std::vector<float> tcq_lut; /* 2^L integer values in -127..127 */
float tcq_peak = 0.0f; /* tcq_peak=1: the table's largest magnitude, which no
block's largest value may pass; else 0 */
/* tcq_rb=R: the values on the int8 grid an int8 GEMM reads with one scale per R rows (and the
* block's 128 columns), the largest of the R rows' scales: row r's value is
* B * rint(lut[state] * (s_r / B)), B that largest. The row holding it keeps its values; the
* others' are rounded onto B's grid, inside the trellis's search. 0: off. */
int64_t tcq_rb = 0;
/* tcq_colf=1 (one scale a row, block=1x*): each 128-column block's values also take a factor
* shared by every row, its share of the weight's energy, so a value's scale is its row's times
* its block's. A GEMM folds the factor into the activation's per-128 scale, which it multiplies
* anyway, and the row's in its epilogue. Chosen from the weight before GPTQ. */
bool tcq_colf = false;
/* tcq_code=mad: the states' values are computed, not chosen: with h = (st * A + B) mod 2^32
* (the product of the factors' low 24 bits), state st's value is bytesum(h & 0x3F3F3F3F) - 126
* (tcq_lut_mad). A reader needs no table: a multiply-add, a mask and a byte-sum dot a value. The
* table is still stored, A and B beside it ("trellis.mad"). */
bool tcq_mad = false;
uint32_t tcq_a = 0, tcq_b = 0;
/* tcq_code=mad16: the same hash with every byte whole, bytesum(h) - 510 (sigma ~148, where mad's
* is ~37): values a 16-bit reader takes exactly (f16 holds integers to 2048), so the code is not
* held to the int8 grid's ~42 dB floor. No int8 encoding can carry it; gptqsim measures it. */
bool tcq_wide = false;
/* The planes of the encoding, by index; -1 when the grid has none. */
int plane_zero() const { return asym ? 2 : -1; }
int plane_grid() const {
return vq.empty() && !tcq_L ? -1 : 2 + (asym ? 1 : 0) + (lv2 ? 1 : 0);
}
int plane_scale2() const { return lv2 ? (asym ? 3 : 2) : -1; }
int plane_mad() const { return tcq_mad ? plane_grid() + 1 : -1; }
/* A trellis under one scale a row (block=1x*) carries the 128-column blocks' factors beside it,
* tcq_colf's or ones, so a reader has one form for both (libr4d's kRowF32). */
bool row_scaled() const { return tcq_L && br == 1 && bc == 0 && !lv2; }
int plane_colf() const { return row_scaled() ? plane_grid() + (tcq_mad ? 2 : 1) : -1; }
int plane_table() const {
return table.empty() ? -1 : 2 + (asym ? 1 : 0) + (lv2 ? 1 : 0);
}
/* Whether this grid takes the general path (see the file header). */
bool general() const { return lv2 || rule == SEARCH || !table.empty() || wide; }
};
/* Parse a grid from options. RAD_OK, or RAD_E_INVAL with `why` naming the option. */
int grid_parse(const RadParam* o, int n_o, Grid* g, std::string* why);
/* The encoding a grid writes. */
RadEncoding grid_encoding(const Grid& g);
/* The rows one quantize call may take for this grid: a multiple of every level's block rows, or 0
* when a scale covers every row and has to see them all. */
int64_t grid_row_block(const Grid& g);
/* One block's quantiser state: the scale the codes are chosen against, its stored form, the
* reciprocal, and for an asymmetric grid the zero. */
struct BlockScale {
float s = 1.0f; /* the value codes are chosen against: the stored scale(s), widened */
float inv = 1.0f;
double inv_d = 1.0; /* rule=fixed reads its reciprocal in double; see grid.cpp */
int zero = 0;
int e8 = 0; /* rule=mx: the shared exponent's E8M0 code */
float stored = 1.0f; /* what the scale plane holds: s divided by a transform's order, or
under a second level the first level's own value */
float lo = 0.0f; /* an asymmetric block's low end, which the zero is taken against */
};
/* The scale of the block covering logical rows [r0, r1) and columns [c0, c1) of `w`, f32 [*, cols],
* already transformed. `imp` is a column's importance for rule=search, or null. Under a second
* level this is the first level's ideal scale, unrounded; grid_scales finishes it. */
BlockScale grid_block_scale(const Grid& g, const float* w, int64_t cols, int64_t r0, int64_t r1,
int64_t c0, int64_t c1, const float* imp = nullptr);
/* One value onto the grid: its code, and the value the code decodes to (for error feedback). */
int grid_code(const Grid& g, const BlockScale& b, float v);
/* A vector grid's code for the `vdim` values at `v`: the entry nearest v times b.inv. */
int grid_vq_code(const Grid& g, const BlockScale& b, const float* v);
float grid_decode(const Grid& g, const BlockScale& b, int code);
/* tcq_code=mad's value of every one of the 2^L states (Grid::tcq_mad); with `wide`, mad16's
* (Grid::tcq_wide). */
std::vector<float> tcq_lut_mad(int L, uint32_t a, uint32_t b, bool wide = false);
/* A trellis grid's search state for one run, reused across runs. */
struct TcqWork {
std::vector<float> cost, next, best, rx, rs, rr;
std::vector<uint8_t> back;
std::vector<int64_t> path;
};
/* A trellis grid's codes for a run of n <= tcq_rows rows of one column: a tail-biting path (the
* state before the first row is the state after the last) of least sum over the rows of
* (x_r - v_r)^2, v_r = s_r * lut[state_r], its codes into `codes` and the values they decode to
* into `dq`. With `ratio` (tcq_rb), v_r = s_r * rint(lut[state_r] * ratio_r) instead: s_r is then
* the row block's scale and ratio_r the row's own scale over it. */
void grid_tcq_run(const Grid& g, const float* x, const float* s, int64_t n, int32_t* codes,
float* dq, TcqWork& w, const float* ratio = nullptr);
/* A trellis grid's two-level scales are read as one bf16 a block: the bf16 of the first level
* (b->stored) times the second's stored value s2, which is what libr4d widens them to for its
* int8 GEMMs. Settle b's scale to that. */
void grid_tcq_settle(const Grid& g, float s2, BlockScale* b);
/* A trellis block's two-level scale from its ideal (unrounded) scale under the second level's
* stored s2, settled: dyn=1's choice when GPTQ reaches the block. */
BlockScale grid_tcq_block_lv2(const Grid& g, double ideal, float s2);
/* Rows [row0, row0 + rows) of a "trellis" weight of rows_all x cols back to f32 in its logical
* basis (the transform undone), from the plane starts: abi/rad_quant.h's decode hook. Whole runs
* only, but for the weight's last. RAD_E_UNSUPPORTED for any other encoding. */
int grid_tcq_decode(const RadEncoding* e, int64_t rows_all, int64_t cols, const void* const* planes,
int64_t row0, int64_t rows, float* out);
/* Write a scale (and a zero) into element `i` of its plane row. */
void grid_store_scale(const Grid& g, const BlockScale& b, void* scale_row, void* zero_row,
int64_t i);
/* The geometry of a quantize call: where each plane's rows for this call start, as the ABI hands
* them over, and the plane row widths in bytes. */
struct Planes {
uint8_t* codes = nullptr;
uint8_t* scale = nullptr;
uint8_t* zero = nullptr;
uint8_t* scale2 = nullptr;
uint8_t* table = nullptr;
uint8_t* grid = nullptr;
uint8_t* mad = nullptr;
uint8_t* colf = nullptr;
int64_t colf_cols = 0;
int64_t codes_row = 0, scale_row = 0, zero_row = 0, scale2_row = 0; /* bytes */
int64_t scale_cols = 0, scale2_cols = 0; /* elements a row */
};
Planes grid_planes(const Grid& g, int64_t cols, void* const* planes);
/* Rotate rows in place when the grid carries a transform. */
void grid_transform_rows(const Grid& g, float* w, int64_t rows, int64_t cols);
/* Every scale of a call's rows, both levels: the first level block by block in plane order, the
* second level's stored values in theirs. */
struct Scales {
std::vector<BlockScale> b;
int64_t nr = 0, nc = 0; /* first-level blocks */
int64_t br = 1, bc = 1; /* their extents, resolved against the call */
std::vector<float> s2; /* the second level as stored */
int64_t nr2 = 0, nc2 = 0;
std::vector<float> colf; /* tcq_colf: each 128-column block's factor, all rows' */
const BlockScale& at(int64_t r, int64_t c) const {
return b[(size_t)((r / br) * nc + c / bc)];
}
};
/* The whole scale grid of rows [0, rows) of `w`, without choosing codes: what round-to-nearest and
* the static-groups form of an error-feedback algorithm quantise against. `imp` is the columns'
* importances for rule=search, or null. */
Scales grid_scales(const Grid& g, const float* w, int64_t rows, int64_t cols,
const float* imp = nullptr);
/* Write every scale plane, the zeros and the codebook of a call. */
void grid_store_scales(const Grid& g, const Scales& s, const Planes& p);
/* Store one code into element `i` of a code row, any width this grid writes. */
void grid_store_code(const Grid& g, uint8_t* row, int64_t i, int code);
/* Quantise and decode rows [0, rows) of `w` into `out` [rows, cols]: the weight a reader would see,
* for algorithms that measure a candidate. `rows` covers whole blocks. */
void grid_fake(const Grid& g, const float* w, int64_t rows, int64_t cols, float* out,
const float* imp = nullptr);
/* A block's state from an unrounded scale `s` and the asymmetric low end `lo` the zero is taken
* against, rounded as the general path rounds. For algorithms that choose the scale themselves;
* not for grids with a second level, which have their own pass. */
BlockScale grid_finish(const Grid& g, double s, float lo);
/* Round-to-nearest over rows [0, rows) of `w` (logical rows [row0, row0+rows)). The block scales
* are taken over each block's own rows, so `rows` must cover whole blocks. */
int grid_rtn(const Grid& g, const float* w, int64_t row0, int64_t rows, int64_t cols,
const Planes& p, const float* imp = nullptr);
} /* namespace lq */