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_hess.cpp
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

415 lines
17 KiB
C++

/* lq_hess.cpp -- reading the calibration Hessians, and D . H. See lq_hess.h. */
#include "lq_hess.h"
#include "lq_common.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <map>
#include <mutex>
namespace lq {
namespace {
/* "RADGRAM2", k, rank, rows, domain, a pad word, then k*k f32 row-major: the file the engine's
* calibration tap writes (arch/common/rad_arch.h, calib_write). The header is 32 bytes. */
constexpr int kHdr = 32;
/* One file per rank, ranks 0 .. kMaxRanks - 1. */
constexpr int kMaxRanks = 16;
std::string gram_path(const char* dir, int rank, const std::string& stem) {
return std::string(dir) + "/gram.r" + std::to_string(rank) + "." + stem + ".bin";
}
/* The unnormalised Hadamard of width `n` over every n-wide block of a length-k row, in place. */
void fwht_row(double* v, int64_t k, int64_t n) {
for (int64_t b0 = 0; b0 < k; b0 += n) fwht_inplace(v + b0, n);
}
/* H -> F H F for the block-diagonal Hadamard F of width `n`: every row, then every column. F is
* symmetric, so the column pass is the row pass over the transpose; the result stays bit-exactly
* symmetric only because both passes run the same butterflies in the same order. The column pass
* runs fwht_inplace's butterflies on whole rows of a block of `n` rows, so each column sees the
* same operations in the same order as the transform of that column alone; rows, and blocks of
* rows, are independent, so threads change nothing. */
void rotate_gram(std::vector<double>& h, int64_t k, int64_t n) {
double* d = h.data();
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int64_t r = 0; r < k; ++r) fwht_row(d + r * k, k, n);
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int64_t b0 = 0; b0 < k; b0 += n)
for (int64_t s = 1; s < n; s <<= 1)
for (int64_t i = b0; i < b0 + n; i += s << 1)
for (int64_t j = i; j < i + s; ++j) {
double* x = d + j * k;
double* y = d + (j + s) * k;
for (int64_t c = 0; c < k; ++c) {
const double a = x[c], b = y[c];
x[c] = a + b;
y[c] = a - b;
}
}
}
bool read_gram(const std::string& path, int64_t k, int64_t domain, std::vector<double>* acc) {
FILE* f = std::fopen(path.c_str(), "rb");
if (!f) return false;
char hdr[kHdr];
if (std::fread(hdr, 1, kHdr, f) != (size_t)kHdr || std::memcmp(hdr, "RADGRAM2", 8) != 0) {
std::fprintf(stderr, "libquant: %s is not a RADGRAM2 file\n", path.c_str());
std::fclose(f);
return false;
}
uint32_t kk = 0, dom = 0;
std::memcpy(&kk, hdr + 8, 4);
std::memcpy(&dom, hdr + 24, 4);
if ((int64_t)kk != k) {
std::fprintf(stderr, "libquant: %s holds a %u-wide Hessian and this weight's K is %lld\n",
path.c_str(), kk, (long long)k);
std::fclose(f);
return false;
}
/* Domains convert only through the model's own (domain 0): a file rotated at one width is
* rotated back before rotating to another. Both widths must tile K. */
const int64_t from = (int64_t)dom;
auto tiles = [k](int64_t n) { return n == 0 || (n >= 2 && (n & (n - 1)) == 0 && k % n == 0); };
if (!tiles(from) || !tiles(domain)) {
std::fprintf(stderr, "libquant: %s is in domain %lld and the weight wants %lld, which do "
"not both tile K=%lld\n", path.c_str(), (long long)from,
(long long)domain, (long long)k);
std::fclose(f);
return false;
}
std::vector<float> buf((size_t)(k * k));
const bool okr = std::fread(buf.data(), 4, buf.size(), f) == buf.size();
std::fclose(f);
if (!okr) {
std::fprintf(stderr, "libquant: %s is short\n", path.c_str());
return false;
}
std::vector<double> h(buf.begin(), buf.end());
if (from != domain) {
if (from != 0) {
rotate_gram(h, k, from);
const double inv = 1.0 / ((double)from * (double)from);
for (double& x : h) x *= inv;
}
if (domain != 0) rotate_gram(h, k, domain);
/* Round to f32, as a tap in that domain would have stored it, so a converted Hessian
* matches one summed there and a bf16 model's calibration and a rotated container's
* quantise a weight identically. */
for (double& x : h) x = (double)(float)x;
}
if (acc->empty()) acc->assign(h.size(), 0.0);
for (size_t i = 0; i < h.size(); ++i) (*acc)[i] += h[i];
return true;
}
/* The total rows summed by every rank's file for `stem`, or -1 when `dir` holds none. */
int64_t calib_rows(const char* dir, const std::string& stem) {
int64_t total = -1;
for (int r = 0; r < kMaxRanks; ++r) {
FILE* f = std::fopen(gram_path(dir, r, stem).c_str(), "rb");
if (!f) continue;
char hdr[kHdr];
if (std::fread(hdr, 1, kHdr, f) == (size_t)kHdr && std::memcmp(hdr, "RADGRAM2", 8) == 0) {
uint64_t n = 0;
std::memcpy(&n, hdr + 16, 8);
total = (total < 0 ? 0 : total) + (int64_t)n;
}
std::fclose(f);
}
return total;
}
/* The name less a `.weight` or `.scale` tail. */
std::string own_stem(const char* name) {
std::string s = name ? name : "";
const char* tails[] = { ".weight", ".scale" };
for (const char* t : tails) {
const size_t n = std::strlen(t);
if (s.size() > n && s.compare(s.size() - n, n, t) == 0) { s.resize(s.size() - n); break; }
}
return s;
}
/* A few layers' Grams, the oldest dropped first: a MoE layer's experts read their layer's again
* and again, a dense model reads each once, and every layer's kept at once outgrows the host
* (Qwen3.8-27B's are ~100 GB; a GPTQ convert with descent was OOM-killed at layer 50). */
constexpr size_t kGrams = 4;
struct GramCache {
std::mutex mu;
std::map<std::string, Gram> g; /* null = looked and found nothing */
std::deque<std::string> order; /* g's keys, oldest first */
};
GramCache& gram_cache() {
static GramCache c;
return c;
}
/* A panel is CB columns of H over all K rows, packed into a K x CB buffer so each k step reads one
* contiguous line instead of striding K floats; this keeps the product compute-bound (3-5x faster
* than unpacked). A row block is RB rows of D held as RB*CB register accumulators: 8 x 32 under
* AVX-512 and 4 x 16 under AVX2 use sixteen and eight vector registers. Each accumulator sums k in
* ascending order and is stored once, so panel, block and instruction set change the schedule, not
* any sum. Both functions are forced inline so each compiles under its caller's target ISA. */
template <int RB, int CB>
__attribute__((always_inline)) inline void panel(const float* D, int64_t K, const float* P,
float* G) {
float acc[RB][CB];
for (int r = 0; r < RB; ++r)
for (int c = 0; c < CB; ++c) acc[r][c] = 0.0f;
for (int64_t k = 0; k < K; ++k) {
const float* h = P + k * CB;
for (int r = 0; r < RB; ++r) {
const float d = D[r * K + k];
#pragma omp simd
for (int c = 0; c < CB; ++c) acc[r][c] += d * h[c];
}
}
for (int r = 0; r < RB; ++r)
for (int c = 0; c < CB; ++c) G[r * K + c] = acc[r][c];
}
/* A task: one panel of H packed, then a run of row blocks against it. */
template <int RB, int CB>
__attribute__((always_inline)) inline void task(const float* D, int64_t K, const float* H,
int64_t j0, float* G, int64_t b0, int64_t b1,
float* pack) {
for (int64_t k = 0; k < K; ++k) std::memcpy(pack + k * CB, H + k * K + j0, CB * sizeof(float));
for (int64_t b = b0; b < b1; ++b) panel<RB, CB>(D + b * RB * K, K, pack, G + b * RB * K + j0);
}
/* The edges the blocks leave, one element at a time in the same k order. */
void edge(const float* D, int64_t K, const float* H, int64_t r0, int64_t r1, int64_t j0,
int64_t j1, float* G) {
for (int64_t r = r0; r < r1; ++r)
for (int64_t j = j0; j < j1; ++j) {
float s = 0.0f;
for (int64_t k = 0; k < K; ++k) s += D[r * K + k] * H[k * K + j];
G[r * K + j] = s;
}
}
/* The blocked part at one shape, threaded over (panel, run of row blocks). Returns the rows and
* columns it covered. */
template <int RB, int CB>
void blocked(const float* D, int64_t R, int64_t K, const float* H, float* G,
void (*run)(const float*, int64_t, const float*, int64_t, float*, int64_t, int64_t,
float*),
int64_t* rf, int64_t* cf) {
const int64_t nrb = R / RB, ncb = K / CB;
*rf = nrb * RB;
*cf = ncb * CB;
if (nrb == 0 || ncb == 0) { *rf = *cf = 0; return; }
const int64_t chunk = 64; /* row blocks a task */
const int64_t nch = (nrb + chunk - 1) / chunk;
#ifdef _OPENMP
#pragma omp parallel
#endif
{
std::vector<float> pack((size_t)(K * CB));
#ifdef _OPENMP
#pragma omp for collapse(2) schedule(dynamic, 1)
#endif
for (int64_t cb = 0; cb < ncb; ++cb)
for (int64_t ch = 0; ch < nch; ++ch)
run(D, K, H, cb * CB, G, ch * chunk, std::min(nrb, (ch + 1) * chunk), pack.data());
}
}
__attribute__((target("avx512f")))
void run_avx512(const float* D, int64_t K, const float* H, int64_t j0, float* G, int64_t b0,
int64_t b1, float* pack) {
task<8, 32>(D, K, H, j0, G, b0, b1, pack);
}
__attribute__((target("avx2")))
void run_avx2(const float* D, int64_t K, const float* H, int64_t j0, float* G, int64_t b0,
int64_t b1, float* pack) {
task<4, 16>(D, K, H, j0, G, b0, b1, pack);
}
void run_base(const float* D, int64_t K, const float* H, int64_t j0, float* G, int64_t b0,
int64_t b1, float* pack) {
task<4, 8>(D, K, H, j0, G, b0, b1, pack);
}
} /* namespace */
std::string calib_stem(const char* name) {
std::string s = own_stem(name);
const size_t dot = s.rfind('.');
if (dot != std::string::npos && dot + 1 < s.size()) {
bool all_digits = true;
for (size_t i = dot + 1; i < s.size(); ++i)
if (s[i] < '0' || s[i] > '9') { all_digits = false; break; }
if (all_digits) s.resize(dot);
}
/* A vocabulary-weighted lm_head's width classes (output.k<B>) are rows of one weight: they
* read the head's input Gram. */
if (s.size() > 8 && s.compare(0, 8, "output.k") == 0 &&
s.find_first_not_of("0123456789", 8) == std::string::npos)
s.resize(6);
return s;
}
/* The rank parts of `stem` (see calib_sum), block diagonal into `acc`; the number read. */
int calib_sum_parts(const char* dir, const std::string& stem, int64_t K, int64_t domain,
std::vector<double>* acc) {
std::vector<std::vector<float>> parts;
int64_t total = 0;
for (int r = 0; r < kMaxRanks; ++r) {
const std::string path = gram_path(dir, r, stem + ".part");
FILE* f = std::fopen(path.c_str(), "rb");
if (!f) break;
char hdr[kHdr];
uint32_t kk = 0, dom = 0;
bool ok = std::fread(hdr, 1, kHdr, f) == (size_t)kHdr && std::memcmp(hdr, "RADGRAM2", 8) == 0;
if (ok) { std::memcpy(&kk, hdr + 8, 4); std::memcpy(&dom, hdr + 24, 4); }
ok = ok && dom == 0 && kk > 0 && total + kk <= K;
std::vector<float> h;
if (ok) {
h.resize((size_t)kk * kk);
ok = std::fread(h.data(), 4, h.size(), f) == h.size();
}
std::fclose(f);
if (!ok) {
std::fprintf(stderr, "libquant: %s is not a usable part of the %lld-wide '%s' (a domain-0 "
"RADGRAM2 within K)\n", path.c_str(), (long long)K, stem.c_str());
return 0;
}
total += kk;
parts.push_back(std::move(h));
}
if (parts.empty()) return 0;
if (total != K) {
std::fprintf(stderr, "libquant: the %zu part(s) of '%s' cover %lld of K=%lld columns\n",
parts.size(), stem.c_str(), (long long)total, (long long)K);
return 0;
}
std::vector<double> h((size_t)(K * K), 0.0);
int64_t off = 0;
for (const auto& p : parts) {
const int64_t k = (int64_t)std::llround(std::sqrt((double)p.size()));
for (int64_t i = 0; i < k; ++i)
for (int64_t j = 0; j < k; ++j)
h[(size_t)((off + i) * K + off + j)] = p[(size_t)(i * k + j)];
off += k;
}
if (domain != 0) {
rotate_gram(h, K, domain);
for (double& x : h) x = (double)(float)x;
}
if (acc->empty()) acc->assign(h.size(), 0.0);
for (size_t i = 0; i < h.size(); ++i) (*acc)[i] += h[i];
return (int)parts.size();
}
int calib_sum(const char* dir, const std::string& stem, int64_t K, int64_t domain,
std::vector<double>* acc) {
/* Sum every rank's file. Under expert parallelism a rank's `down` Hessian covers only tokens
* routed to its own experts, so no single file holds the layer's distribution. The gate_up
* files are duplicated across ranks; summing them scales H by the rank count, which every
* algorithm here is invariant to. */
int found = 0;
for (int r = 0; r < kMaxRanks; ++r)
if (read_gram(gram_path(dir, r, stem), K, domain, acc)) ++found;
if (!found && (domain == 0 || (domain >= 2 && (domain & (domain - 1)) == 0 && K % domain == 0)))
found = calib_sum_parts(dir, stem, K, domain, acc);
return found;
}
std::string calib_resolve(const char* dir, const char* name, int64_t K) {
const std::string pooled = calib_stem(name);
const std::string own = own_stem(name);
if (!dir || !*dir || own == pooled) return pooled;
const int64_t rows = calib_rows(dir, own);
if (rows >= kOwnRowsPerK * K) return own;
if (rows >= 0)
std::fprintf(stderr, "libquant: '%s' was routed %lld calibration rows, fewer than %lld; it "
"reads the layer's Hessian '%s'\n", own.c_str(), (long long)rows,
(long long)(kOwnRowsPerK * K), pooled.c_str());
return pooled;
}
Gram calib_gram(const char* dir, const char* name, int64_t K, int64_t domain, const char* who) {
if (!dir || !*dir || !name || !*name || K <= 0) return nullptr;
const std::string stem = calib_resolve(dir, name, K);
int found = 0;
auto read = [&]() -> Gram {
std::vector<double> acc;
found = calib_sum(dir, stem, K, domain, &acc);
if (!found) {
std::fprintf(stderr, "libquant: no calibration for '%s' in %s (looked for "
"gram.r*.%s.bin); %s rounds it to nearest\n", name, dir,
stem.c_str(), who);
return nullptr;
}
auto h = std::make_shared<std::vector<float>>(acc.size());
for (size_t i = 0; i < acc.size(); ++i) (*h)[i] = (float)acc[i];
return h;
};
if (stem != calib_stem(name)) return read();
const std::string key = std::string(dir) + "|" + stem + "|" + std::to_string(K) + "|" +
std::to_string(domain);
GramCache& c = gram_cache();
std::lock_guard<std::mutex> lk(c.mu);
auto it = c.g.find(key);
if (it != c.g.end()) return it->second;
Gram h = read();
if (h)
std::fprintf(stderr, "libquant: %s reads '%s' against %d calibration file(s), K=%lld\n",
who, stem.c_str(), found, (long long)K);
while (c.order.size() >= kGrams) {
c.g.erase(c.order.front());
c.order.pop_front();
}
c.g.emplace(key, h);
c.order.push_back(key);
return h;
}
void mat_hess(const float* D, int64_t R, int64_t K, const float* H, float* G) {
if (R <= 0 || K <= 0) return;
int64_t rf = 0, cf = 0;
if (__builtin_cpu_supports("avx512f")) blocked<8, 32>(D, R, K, H, G, run_avx512, &rf, &cf);
else if (__builtin_cpu_supports("avx2")) blocked<4, 16>(D, R, K, H, G, run_avx2, &rf, &cf);
else blocked<4, 8>(D, R, K, H, G, run_base, &rf, &cf);
/* The column edge of the blocked rows, then the row edge across every column. */
if (cf < K && rf > 0) {
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int64_t r = 0; r < rf; ++r) edge(D, K, H, r, r + 1, cf, K, G);
}
if (rf < R) {
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int64_t j = 0; j < K; ++j) edge(D, K, H, rf, R, j, j + 1, G);
}
}
void row_dots(const float* D, const float* G, int64_t R, int64_t K, double* out) {
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int64_t r = 0; r < R; ++r) {
double s = 0.0;
for (int64_t k = 0; k < K; ++k) s += (double)D[r * K + k] * (double)G[r * K + k];
out[r] = s;
}
}
} /* namespace lq */