Merge ParoQuant: PRs #65, #66 and #67 (malic), with fixes

Rotated Qwen3.8-27B checkpoints: ParoQuant MXFP4, int5 bit-plane and z-lab's int4 import (the givens
transform with parts, per-row scales, zero points), the w5a16/w4a16_asym decode GEMMs, the
w4a8i8/w5a8i8 prefill band, the givens_rotate kernel, and the dense per-group rotation
(givens_dense_quant: one 128x128 f16 matrix a part and group on the matrix cores) for the quantising
rotation in prefill. gemm_nt_q gains part bounds (pb1/pb2), an op-schema change an out-of-tree
kernel library has to follow.

Fixes on top: the dense matrices are a container table ("t.dense") that rad-convert composes in f64
from the pairs and angles, and the loader only moves its bits. A container converted before this is
refused at load, naming the re-convert (--reuse copies every unchanged weight). A gemm_nt_q row that
does not read part bounds refuses them instead of ignoring them. libavx implements givens_rotate and
givens_dense_quant.
This commit is contained in:
Deadcode committed 2026-10-12 20:46:37 +02:00
commit f6a9467cb5
40 files changed
+4206 -143

No files matched your search

+104
View File
@@ -410,6 +410,110 @@ int ref_had_quant_act_fp8(const RadArgs* a, RadStream) {
return RAD_OK;
}
/* ------------------------------------------------------------- givens_rotate */
/* ParoQuant's activation side: every row scaled by its channel factors and rotated by `stages` rounds of
* disjoint Givens pairs, written in y's dtype. A weight stored as v = w . D . G_1 ... G_S
* (abi/rad_encoding.h, "givens") meets the activation x . D^-1 . G_1 ... G_S, because
* <v, x D^-1 G_1..G_S> = <w, x> and each G is orthogonal. For a weight whose GEMM reads a 16-bit
* activation (w5a16); the quantising form is givens_dense_quant, over the composed matrices.
*
* PARTS. A fused projection (gate|up, q|k|v) was rotated per piece, so a row has `parts` rotated
* copies, written side by side: y is [rows, parts * n], part p at columns [p*n, (p+1)*n). The tables
* stack the parts: pairs and angle `stages` rows each, scale one row, per part. A part's pairs address
* columns of the row and stay inside one group of `group` (the checkpoint's are within groups of 128).
*
* The arithmetic is the device kernel's, restated: the divide by the channel scale, then for each
* stage a' = a cos t + b sin t, b' = b cos t - a sin t in f32. Not bit-identical to the device on the
* angle: cos and sin of an f32 differ by an ulp between libm and the GPU's. */
int ref_givens_rotate(const RadArgs* a, RadStream) {
if (!have(a, 5)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *pr = &a->t[1], *an = &a->t[2], *sc = &a->t[3], *q = &a->t[4];
const int64_t n = p_int(a, "n", 0), S = p_int(a, "stages", 0), P = p_int(a, "parts", 1);
const int64_t g = p_int(a, "group", 0);
if (n <= 0 || S <= 0 || P <= 0 || (n & 1)) return RAD_E_SHAPE;
if (g <= 0 || g > n || n % g != 0) return RAD_E_SHAPE;
if (numel(pr) < P * S * n || numel(an) < P * S * (n / 2) || numel(sc) < P * n) return RAD_E_SHAPE;
const int64_t rows = numel(x) / n;
if (rows <= 0 || numel(q) < rows * P * n) return RAD_E_SHAPE;
// validated before the loop, because an OpenMP body cannot return
for (int64_t i = 0; i < P * S * n; ++i) {
const int64_t c = ld_int(pr->dtype, pr->data, i);
if (c < 0 || c >= n) return RAD_E_INVAL;
}
#pragma omp parallel for schedule(static)
for (int64_t r = 0; r < rows; ++r) {
std::vector<float> buf((size_t)n);
const int64_t xb = rowoff(x, r, n), qb = rowoff(q, r, P * n);
const int64_t xs = laststride(x), qs = laststride(q);
for (int64_t p = 0; p < P; ++p) {
for (int64_t k = 0; k < n; ++k) buf[(size_t)k] = ldt(x, xb + k * xs) / ldt(sc, p * n + k);
for (int64_t st = 0; st < S; ++st) {
const int64_t t0 = (p * S + st) * n, a0 = (p * S + st) * (n / 2);
for (int64_t j = 0; j < n / 2; ++j) {
const int64_t ia = ld_int(pr->dtype, pr->data, t0 + 2 * j);
const int64_t ib = ld_int(pr->dtype, pr->data, t0 + 2 * j + 1);
const float th = ldt(an, a0 + j), c = std::cos(th), sn = std::sin(th);
const float va = buf[(size_t)ia], vb = buf[(size_t)ib];
buf[(size_t)ia] = va * c + vb * sn;
buf[(size_t)ib] = vb * c - va * sn;
}
}
for (int64_t k = 0; k < n; ++k) st_dt(q->dtype, q->data, qb + (p * n + k) * qs, buf[(size_t)k]);
}
}
return RAD_OK;
}
/* ParoQuant's activation quantiser: the rotation applied as the dense matrix, y[j] = sum_i T[j][i] * x[i] / D[i] over
* each (part, 128-column group), then the quantiser `q`'s dtype names. T is the weight, f16, one [128, 128]
* matrix a (part, group) -- what rad-convert composes from the givens tables and stores as "t.dense". */
int ref_givens_dense_quant(const RadArgs* a, RadStream) {
if (!have(a, 5)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *tm = &a->t[1], *sc = &a->t[2], *q = &a->t[3], *s = &a->t[4];
const int64_t n = p_int(a, "n", 0), P = p_int(a, "parts", 1), g = p_int(a, "group", 0);
if (n <= 0 || P <= 0 || g != 128 || n % g != 0) return RAD_E_SHAPE;
const bool i8 = q->dtype == RAD_I8;
if (!i8 && q->dtype != RAD_F8E4M3) return RAD_E_DTYPE;
if (std::strcmp(p_str(a, "out", ""), i8 ? "i8" : "fp8") != 0) return RAD_E_DTYPE; // `out` names q's dtype
const int64_t rows = numel(x) / n, ngrp = n / g;
if (rows <= 0 || numel(tm) < P * n * g || numel(sc) < P * n || numel(q) < rows * P * n ||
numel(s) < rows * P * ngrp) return RAD_E_SHAPE;
const float qmax = i8 ? 127.0f : 448.0f;
#pragma omp parallel for schedule(static)
for (int64_t r = 0; r < rows; ++r) {
const int64_t xb = rowoff(x, r, n), qb = rowoff(q, r, P * n);
const int64_t xs = laststride(x), qs = laststride(q);
std::vector<float> in((size_t)g), y((size_t)g);
for (int64_t p = 0; p < P; ++p)
for (int64_t gi = 0; gi < ngrp; ++gi) {
for (int64_t i = 0; i < g; ++i)
in[(size_t)i] = ldt(x, xb + (gi * g + i) * xs) / ldt(sc, p * n + gi * g + i);
float amax = 0.0f;
for (int64_t j = 0; j < g; ++j) {
double acc = 0.0;
for (int64_t i = 0; i < g; ++i)
acc += (double)ldt(tm, ((p * ngrp + gi) * g + j) * g + i) * (double)in[(size_t)i];
y[(size_t)j] = (float)acc;
amax = std::max(amax, std::fabs(y[(size_t)j]));
}
const float scl = amax > 0.0f ? amax * (1.0f / qmax) : 1.0f;
const float inv = 1.0f / scl;
for (int64_t j = 0; j < g; ++j) {
float v = y[(size_t)j] * inv;
if (v > qmax) v = qmax;
if (v < -qmax) v = -qmax;
if (i8) v = std::nearbyint(v);
st_dt(q->dtype, q->data, qb + (p * n + gi * g + j) * qs, v);
}
stt(s, rowoff(s, r, P * ngrp) + (p * ngrp + gi) * laststride(s), scl);
}
}
return RAD_OK;
}
/* ---------------------------------------------------------------- the Gram accumulator
*
* h[k1][k2] += sum over rows of x[m][k1] * x[m][k2], where x is the DEQUANTISED E4M3 input, or