Archived
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:
commit
f6a9467cb5
40 files changed
+4206
-143
No files matched your search
@@ -17,6 +17,7 @@
|
||||
* thousands in every GEMM a transformer issues. Each thread owns whole output elements, so the
|
||||
* summation order does not depend on the thread count.
|
||||
*/
|
||||
#include <algorithm>
|
||||
#include "ref_common.h"
|
||||
#include "ref_gemm.h"
|
||||
#include "ref_ops.h"
|
||||
@@ -253,6 +254,52 @@ static int gemm_ntq_impl(const RadArgs* a, bool bias_operand) {
|
||||
if (numel(A) < M * K || numel(B) < N * K) return RAD_E_SHAPE;
|
||||
if (bias && numel(bias) < N) return RAD_E_SHAPE;
|
||||
|
||||
/* PARTS (`pb1`, `pb2`): the weight's rows are [0, pb1), [pb1, pb2) and [pb2, N), each against
|
||||
* its own copy of the activation -- a fused projection whose pieces were rotated separately
|
||||
* (ParoQuant). The copies sit side by side in a row of `a`, so `a` is [M, parts * K] and its
|
||||
* scale [M, parts * K / 128]; a part is the plain product over its rows, which is how it is
|
||||
* computed. 0 leaves a bound out: no `pb1` is one part, no `pb2` ends the second at N. */
|
||||
const int64_t pb1 = p_int(a, "pb1", 0), pb2 = p_int(a, "pb2", 0);
|
||||
if (pb1 > 0 || pb2 > 0) {
|
||||
if (bias_operand || B->dtype != RAD_FP4E2M1 || A->rank < 2) return RAD_E_UNSUPPORTED;
|
||||
const int64_t e1 = pb1 > 0 ? std::min<int64_t>(pb1, N) : N;
|
||||
const int64_t e2 = pb2 > 0 ? std::max<int64_t>(e1, std::min<int64_t>(pb2, N)) : N;
|
||||
const int64_t bound[4] = { 0, e1, e2, N };
|
||||
const int64_t acols = A->shape[A->rank - 1];
|
||||
const int64_t parts = e2 < N ? 3 : (e1 < N ? 2 : 1);
|
||||
if (acols != parts * K || (e1 % 16) || (e2 % 16)) return RAD_E_SHAPE;
|
||||
const bool sa_row = as_ && as_->data && as_->rank >= 2;
|
||||
if (!sa_row) return RAD_E_INVAL;
|
||||
const int64_t sa_cols = as_->shape[as_->rank - 1];
|
||||
if (sa_cols % parts) return RAD_E_SHAPE;
|
||||
const int64_t sa_part = sa_cols / parts; /* K / 128 */
|
||||
for (int p = 0; p < 3; ++p) {
|
||||
const int64_t n0 = bound[p], n1 = bound[p + 1];
|
||||
if (n1 <= n0) continue;
|
||||
RadTensor Ap = *A, Sp = *as_, Bp = *B, Bsp = *bs_, Yp = *Y;
|
||||
Ap.data = (uint8_t*)A->data + rad_dtype_bytes(A->dtype, p * K);
|
||||
Ap.rank = 2; Ap.shape[0] = M; Ap.shape[1] = K;
|
||||
Ap.stride[0] = row_stride(A, acols); Ap.stride[1] = 1;
|
||||
Sp.data = (uint8_t*)as_->data + rad_dtype_bytes(as_->dtype, p * sa_part);
|
||||
Sp.shape[Sp.rank - 1] = sa_part;
|
||||
Bp.data = (uint8_t*)B->data + rad_dtype_bytes(B->dtype, n0 * K);
|
||||
Bp.rank = 2; Bp.shape[0] = n1 - n0; Bp.shape[1] = K; Bp.stride[0] = K; Bp.stride[1] = 1;
|
||||
const int64_t bs_cols = bs_->shape[bs_->rank - 1];
|
||||
Bsp.data = (uint8_t*)bs_->data + rad_dtype_bytes(bs_->dtype, n0 * bs_cols);
|
||||
Bsp.rank = 2; Bsp.shape[0] = n1 - n0; Bsp.shape[1] = bs_cols;
|
||||
Bsp.stride[0] = bs_cols; Bsp.stride[1] = 1;
|
||||
const int64_t ys = row_stride(Y, N);
|
||||
Yp.data = (uint8_t*)Y->data + rad_dtype_bytes(Y->dtype, n0);
|
||||
Yp.rank = 2; Yp.shape[0] = M; Yp.shape[1] = n1 - n0; Yp.stride[0] = ys; Yp.stride[1] = 1;
|
||||
Scales SAp = make_scales(&Sp, M, K, false), SBp = make_scales(&Bsp, n1 - n0, K, true);
|
||||
const Mat map_ = as_mat(&Ap, M, K), mbp = as_mat(&Bp, n1 - n0, K);
|
||||
QuantFn fn = pick_quant(Ap.dtype, Bp.dtype);
|
||||
if (fn) fn(map_, mbp, SAp, SBp, &Yp, nullptr, M, n1 - n0, K, 1.0f);
|
||||
else gemm_quant_generic(map_, mbp, SAp, SBp, &Yp, nullptr, M, n1 - n0, K, 1.0f);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
const bool mx = B->dtype == RAD_FP4E2M1;
|
||||
Scales SA = make_scales(as_, M, K, false);
|
||||
Scales SB = make_scales(bs_, N, K, mx);
|
||||
|
||||
@@ -106,6 +106,8 @@ int ref_quant_act_i8 (const RadArgs*, RadStream);
|
||||
int ref_had_quant_act_i8 (const RadArgs*, RadStream);
|
||||
int ref_quant_act_fp8 (const RadArgs*, RadStream);
|
||||
int ref_had_quant_act_fp8 (const RadArgs*, RadStream);
|
||||
int ref_givens_dense_quant(const RadArgs*, RadStream);
|
||||
int ref_givens_rotate(const RadArgs*, RadStream);
|
||||
int ref_rmsnorm_quant_fp8 (const RadArgs*, RadStream);
|
||||
int ref_gated_quant_fp8 (const RadArgs*, RadStream);
|
||||
int ref_gemm_nt_q_gated (const RadArgs*, RadStream);
|
||||
@@ -240,6 +242,8 @@ REF_SHAPE_FN(ref_quant_act_i8);
|
||||
REF_SHAPE_FN(ref_had_quant_act_i8);
|
||||
REF_SHAPE_FN(ref_quant_act_fp8);
|
||||
REF_SHAPE_FN(ref_had_quant_act_fp8);
|
||||
REF_SHAPE_FN(ref_givens_dense_quant);
|
||||
REF_SHAPE_FN(ref_givens_rotate);
|
||||
REF_SHAPE_FN(ref_rmsnorm_quant_fp8);
|
||||
REF_SHAPE_FN(ref_gated_quant_fp8);
|
||||
REF_SHAPE_FN(ref_gemm_nt_q_gated);
|
||||
|
||||
@@ -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
|
||||
|
||||
+34
-2
@@ -210,6 +210,17 @@ static const RadOperandSpec oHadQuant[] = { OPD("x"), OUT("q"), OUT("scale") };
|
||||
/* No `asum`: that output exists for the asymmetric integer grids and an fp8 grid is symmetric by
|
||||
* construction (libr4d/r4d_rows.cpp says the same). */
|
||||
static const RadOperandSpec oQuantFp8[] = { OPD("x"), OUT("q"), OUT("scale") };
|
||||
/* ParoQuant's rotation: a rotation per part, to f16 for the 16-bit GEMMs. The three
|
||||
* tables are the "givens" encoding's (abi/rad_encoding.h) and are WEIGHT operands, so they are
|
||||
* resolved, moved and placed with the weight they belong to. */
|
||||
static const RadParamSpec pGivensRotate[] = { P_INT("M"), P_INT("n"), P_INT("group"), P_STR("dtype"),
|
||||
P_INT("stages"), P_INT("parts") };
|
||||
static const RadParamSpec pGivensDense[] = { P_INT("M"), P_INT("n"), P_INT("group"), P_STR("dtype"),
|
||||
P_INT("parts"), P_STR("out") };
|
||||
static const RadOperandSpec oGivensDense[] = { OPD("x"), WGT("tdense"), WGT("rscale"), OUT("q"),
|
||||
OUT("scale") };
|
||||
static const RadOperandSpec oGivensRotate[] = { OPD("x"), WGT("pairs"), WGT("angle"), WGT("rscale"),
|
||||
OUT("y") };
|
||||
|
||||
/* ---- the fp8 fusions ----------------------------------------------------------------------
|
||||
* A fused op's operands are the unfused pair's minus the tensor between them, and `out_bf16`
|
||||
@@ -273,7 +284,10 @@ static const RadOperandSpec oGemmBias[] = { OPD("a"), WGT("b"), WGT("bias"), OPD
|
||||
OUT("y") };
|
||||
|
||||
static const RadParamSpec pGemmQ[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("group"),
|
||||
P_STR("dtype") };
|
||||
P_STR("dtype"), P_INTO("pb1"), P_INTO("pb2") };
|
||||
/* The bias form has no parts: a fused projection rotated apart is the plain product's. */
|
||||
static const RadParamSpec pGemmQBias[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("group"),
|
||||
P_STR("dtype") };
|
||||
/* `a_sum` and `b_ref` are libr4d's, appended AFTER `y` so docs/OPS.md's list stays a positional
|
||||
* prefix: the asymmetric 4-bit and 2-bit grids subtract the activation's per-group code sums, and
|
||||
* mxfp4 folds a per-output-row reference exponent. Neither is expressible as a weight aux stream,
|
||||
@@ -825,6 +839,17 @@ static const RadOpSchema kSchemas[] = {
|
||||
"previous pick. `hp` absent is a plane of ones, which drops the form to a plain low-rank "
|
||||
"bigram. Serial in l by construction -- the predecessor is not known until the previous step "
|
||||
"has been decided." },
|
||||
{ "givens_dense_quant", ARR(pGivensDense), ARR(oGivensDense),
|
||||
"ParoQuant's activation quantiser with the rotation as a dense per-group matrix: y = T (x / "
|
||||
"rscale) for each of `parts` rotated copies, T the [128, 128] f16 matrix a (part, 128-column "
|
||||
"group) that its Givens stages compose to, then the block-scaled quantiser `out` names "
|
||||
"(\"i8\": scale amax / 127, round to nearest; \"fp8\": E4M3, amax / 448). q is [M, parts * n] "
|
||||
"and scale [M, parts * n / group]; tdense is [parts * n, 128]. The same activation as "
|
||||
"the sequential rotation, up to the f16 rounding of T." },
|
||||
{ "givens_rotate", ARR(pGivensRotate), ARR(oGivensRotate),
|
||||
"ParoQuant's rotation without a quantiser: y = x / rscale rotated by `stages` rounds of Givens "
|
||||
"pairs, `parts` copies side by side ([M, parts * n]) in y's own dtype. For a weight whose GEMM "
|
||||
"reads a 16-bit activation. `group` is the width a pair stays inside." },
|
||||
{ "had_quant_act_fp8", ARR(pQuant), ARR(oQuantFp8),
|
||||
"A Walsh-Hadamard rotation of width `group` along n, then symmetric E4M3 per group: scale = "
|
||||
"amax(rotated) / 448, shaped [M, n/group] in f32. n must be a multiple of `group` -- a rotation "
|
||||
@@ -866,7 +891,7 @@ static const RadOpSchema kSchemas[] = {
|
||||
"gemm_nt with quantised operands. The group sub-sum is accumulated from the raw codes and "
|
||||
"scaled once per group, which is what an int8 or fp8 WMMA kernel does. Scale streams are "
|
||||
"[rows, groups] or [rows]; an mxfp4 weight's u8 scale stream is read as OCP e8m0." },
|
||||
{ "gemm_nt_q_bias", ARR(pGemmQ), ARR(oGemmQBias), "gemm_nt_q with a per-column bias." },
|
||||
{ "gemm_nt_q_bias", ARR(pGemmQBias), ARR(oGemmQBias), "gemm_nt_q with a per-column bias." },
|
||||
|
||||
{ "attn_paged", ARR(pAttnPaged), ARR(oAttnPaged),
|
||||
"Paged attention over kv_cache[num_blocks, kv_heads, block_size, 2*head_dim] (K then V per "
|
||||
@@ -1201,6 +1226,13 @@ SIMPLE("ref_gemm_nt_q_gated", "gemm_nt_q_gated", "gemm",
|
||||
SIMPLE("ref_dflash_select", "dflash_select", "draft",
|
||||
"DFlash2's greedy walk over the candidate lattice",
|
||||
"i32 candidates, bf16 codebooks, i32 tokens out", ref_dflash_select),
|
||||
SIMPLE("ref_givens_rotate", "givens_rotate", "quant",
|
||||
"channel scale and Givens rotations per part, in the output's dtype",
|
||||
"float in, float out; int32 pairs, f32 angles and channel scales", ref_givens_rotate),
|
||||
SIMPLE("ref_givens_dense_quant", "givens_dense_quant", "quant",
|
||||
"the rotation as a dense per-group matrix, then the block-scaled int8 or E4M3 quantiser",
|
||||
"float in, int8 or fp8_e4m3 out, f32 scale per group; f16 matrices and f32 channel scales",
|
||||
ref_givens_dense_quant),
|
||||
SIMPLE("ref_had_quant_act_fp8", "had_quant_act_fp8", "quant",
|
||||
"width-`group` Hadamard, then the same symmetric block-scaled E4M3 quantiser",
|
||||
"float in, fp8_e4m3 out, f32 scale per group; n a multiple of group",
|
||||
|
||||
@@ -890,6 +890,18 @@ int ref_gram_accum_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* ou
|
||||
return o.finish("gram_accum", operand, out);
|
||||
}
|
||||
|
||||
/* DECLINED, on purpose: `pairs` must be a pairing of each group's columns (every column once a
|
||||
* stage, the partner inside the group), and a described operand can only be drawn, not constructed
|
||||
* -- an index draw has repeats, and a Givens stage over a repeated column is not a rotation. A case
|
||||
* built from one would compare two implementations of nothing. r4d_selftest builds real pairings. */
|
||||
int ref_givens_dense_quant_shape(const RadParam*, int, int, RadOpdDesc*) {
|
||||
return RAD_E_UNSUPPORTED; /* a composed rotation cannot be drawn: it is a weight, built from real pairings */
|
||||
}
|
||||
|
||||
int ref_givens_rotate_shape(const RadParam*, int, int, RadOpdDesc*) {
|
||||
return RAD_E_UNSUPPORTED; /* a pairing cannot be drawn: a described operand can only be drawn, and an index draw has repeats */
|
||||
}
|
||||
|
||||
int ref_had_quant_act_fp8_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
|
||||
Reference in new issue
Block a user