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
@@ -771,6 +771,55 @@ static bool hand_cases(const Lib& ref, const Lib& avx, bool verbose, int* n_chec
|
||||
hs.push_back(std::move(s));
|
||||
}
|
||||
|
||||
/* ---- ParoQuant's rotation over real pairings: every stage of every group a permutation of
|
||||
* its 128 columns read as pairs, two parts of three stages. The dense form over a drawn matrix
|
||||
* a (part, group) -- the op takes whatever matrices the weight stores. --------------------- */
|
||||
{
|
||||
const int64_t M = 5, K = 256, S = 3, P = 2, G = 128;
|
||||
std::vector<int32_t> pr((size_t)(P * S * K));
|
||||
for (int64_t ps = 0; ps < P * S; ++ps)
|
||||
for (int64_t g = 0; g < K / G; ++g) {
|
||||
std::vector<int32_t> perm((size_t)G);
|
||||
for (int64_t i = 0; i < G; ++i) perm[(size_t)i] = (int32_t)i;
|
||||
uint64_t x = 0x9E3779B97F4A7C15ull * (uint64_t)(ps * 31 + g + 1);
|
||||
for (int64_t i = G - 1; i > 0; --i) {
|
||||
x ^= x >> 31; x *= 0xBF58476D1CE4E5B9ull; x ^= x >> 29;
|
||||
std::swap(perm[(size_t)i], perm[(size_t)(x % (uint64_t)(i + 1))]);
|
||||
}
|
||||
for (int64_t i = 0; i < G; ++i)
|
||||
pr[(size_t)(ps * K + g * G + i)] = (int32_t)(g * G + perm[(size_t)i]);
|
||||
}
|
||||
const auto h63 = hfill(63);
|
||||
const auto scl = [h63](int64_t i) { return 1.25f + 0.75f * h63(i); }; /* (0.5, 2) */
|
||||
const auto zero = [](int64_t) { return 0.0f; };
|
||||
for (uint32_t ydt : { (uint32_t)RAD_F16, (uint32_t)RAD_F32 }) {
|
||||
Hand h(ydt == RAD_F16 ? "two parts of three stages, y f16" : "two parts of three stages, y f32",
|
||||
"givens_rotate", 1e-5);
|
||||
h.dense(RAD_BF16, { M, K }, hfill(61))
|
||||
.dense(RAD_I32, { P * S, K }, [pr](int64_t i) { return (float)pr[(size_t)i]; })
|
||||
.dense(RAD_F32, { P * S, K / 2 }, hfill(62, 3.0f))
|
||||
.dense(RAD_F32, { P, K }, scl)
|
||||
.dense(ydt, { M, P * K }, zero, true)
|
||||
.pi("M", M).pi("n", K).pi("group", G).ps("dtype", "bf16").pi("stages", S)
|
||||
.pi("parts", P);
|
||||
hs.push_back(std::move(h));
|
||||
}
|
||||
/* A code may sit on a rounding edge that libref's f64 sum and libavx's f32 sum land on
|
||||
* either side of, one step of the grid: the tolerance is a few such codes in 2560. */
|
||||
for (const char* outq : { "fp8", "i8" }) {
|
||||
const bool i8 = outq[0] == 'i';
|
||||
Hand h(i8 ? "two parts, i8" : "two parts, fp8", "givens_dense_quant", 5e-3);
|
||||
h.dense(RAD_BF16, { M, K }, hfill(64))
|
||||
.dense(RAD_F16, { P * K, G }, hfill(65, 0.1f))
|
||||
.dense(RAD_F32, { P, K }, scl)
|
||||
.dense(i8 ? RAD_I8 : RAD_F8E4M3, { M, P * K }, zero, true)
|
||||
.dense(RAD_F32, { M, P * K / G }, zero, true)
|
||||
.pi("M", M).pi("n", K).pi("group", G).ps("dtype", "bf16").pi("parts", P)
|
||||
.ps("out", outq);
|
||||
hs.push_back(std::move(h));
|
||||
}
|
||||
}
|
||||
|
||||
bool ok = true;
|
||||
int fails = 0;
|
||||
for (Hand& h : hs) {
|
||||
|
||||
@@ -755,6 +755,10 @@ static int gemm_ntq_impl(const RadArgs* a, bool bias_operand) {
|
||||
const RadTensor* bs_ = rad_arg_in(a, 3);
|
||||
const RadTensor* bias = bias_operand ? rad_arg_in(a, 4) : nullptr;
|
||||
|
||||
/* `pb1`/`pb2` split the weight's rows into parts that meet separate copies of the activation
|
||||
* (ParoQuant); this driver reads one. Refused rather than computed against the wrong copy. */
|
||||
if (p_int(a, "pb1", 0) || p_int(a, "pb2", 0)) return RAD_E_UNSUPPORTED;
|
||||
|
||||
const int64_t K = p_int(a, "K", B->rank >= 2 ? B->shape[B->rank - 1] : 0);
|
||||
const int64_t N = p_int(a, "N", B->rank >= 2 ? B->shape[B->rank - 2] : 0);
|
||||
if (K <= 0 || N <= 0) return RAD_E_SHAPE;
|
||||
|
||||
+1
-1
@@ -97,7 +97,7 @@ int avx_isa_force(int level);
|
||||
/* ---- quantisation and rotation */ \
|
||||
X(quant_act_i8) X(had_quant_act_i8) X(quant_act_fp8) X(had_quant_act_fp8) \
|
||||
X(gram_accum) X(rmsnorm_had_quant_i8) X(gated_had_quant_i8) X(dequant) \
|
||||
X(rmsnorm_quant_fp8) X(gated_quant_fp8) \
|
||||
X(rmsnorm_quant_fp8) X(gated_quant_fp8) X(givens_rotate) X(givens_dense_quant) \
|
||||
/* ---- gemm and the vocabulary edges */ \
|
||||
X(gemm_nt) X(gemm_nt_bias) X(gemm_nt_q) X(gemm_nt_q_bias) \
|
||||
X(gemm_nt_q_gated) X(embed_lookup) X(embed_lookup_q) X(logits_gemm) \
|
||||
|
||||
@@ -551,6 +551,126 @@ AVX_KERNEL(had_quant_act_fp8) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------- givens_rotate */
|
||||
/* ParoQuant's activation side for a 16-bit GEMM: each row divided by its channel scales and rotated
|
||||
* by `stages` rounds of disjoint Givens pairs, once a part, the parts side by side in y. libref's
|
||||
* arithmetic (ref_quant.cpp) in its order: the divide, then a' = a cos t + b sin t,
|
||||
* b' = b cos t - a sin t in f32, stage by stage. A pair walk is a gather and a scatter with nothing
|
||||
* to vectorise; what is hoisted is the cos and sin of every angle, a property of the weight and the
|
||||
* same for every row, so they are taken once a call rather than once a row. */
|
||||
AVX_KERNEL(givens_rotate) {
|
||||
if (!rad_args_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], *y = &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;
|
||||
const int64_t NP = P * S * (n / 2);
|
||||
if (numel(pr) < P * S * n || numel(an) < NP || numel(sc) < P * n) return RAD_E_SHAPE;
|
||||
const int64_t rows = numel(x) / n;
|
||||
if (rows <= 0 || numel(y) < rows * P * n) return RAD_E_SHAPE;
|
||||
|
||||
int32_t* idx = (int32_t*)scratch_raw((size_t)(P * S * n) * sizeof(int32_t) +
|
||||
(size_t)(2 * NP + P * n) * sizeof(float) + 64);
|
||||
float* cs = (float*)(idx + P * S * n);
|
||||
float* sn = cs + NP;
|
||||
float* rs = sn + NP;
|
||||
for (int64_t i = 0; i < P * S * n; ++i) {
|
||||
const int64_t c = ldi(pr, i);
|
||||
if (c < 0 || c >= n) return RAD_E_INVAL;
|
||||
idx[i] = (int32_t)c;
|
||||
}
|
||||
for (int64_t j = 0; j < NP; ++j) {
|
||||
const float th = ldt(an, j);
|
||||
cs[j] = std::cos(th);
|
||||
sn[j] = std::sin(th);
|
||||
}
|
||||
for (int64_t k = 0; k < P * n; ++k) rs[k] = ldt(sc, k);
|
||||
|
||||
AVX_PARALLEL_FOR_IF(rows * P * n * S >= AVX_PAR_MIN)
|
||||
for (int64_t r = 0; r < rows; ++r) {
|
||||
float* buf = scratch_f32((size_t)n);
|
||||
float* out = scratch_f32_b((size_t)(P * n));
|
||||
const float* v = in_row(x, rowoff(x, r, n), n, buf);
|
||||
for (int64_t p = 0; p < P; ++p) {
|
||||
float* w = out + p * n;
|
||||
for (int64_t k = 0; k < n; ++k) w[k] = v[k] / rs[p * n + k];
|
||||
for (int64_t st = 0; st < S; ++st) {
|
||||
const int32_t* ip = idx + (p * S + st) * n;
|
||||
const int64_t a0 = (p * S + st) * (n / 2);
|
||||
for (int64_t j = 0; j < n / 2; ++j) {
|
||||
const float c = cs[a0 + j], s1 = sn[a0 + j];
|
||||
const float va = w[ip[2 * j]], vb = w[ip[2 * j + 1]];
|
||||
w[ip[2 * j]] = va * c + vb * s1;
|
||||
w[ip[2 * j + 1]] = vb * c - va * s1;
|
||||
}
|
||||
}
|
||||
}
|
||||
out_row(y, rowoff(y, r, P * n), P * n, out);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------- givens_dense_quant */
|
||||
/* The quantising form: the rotation as one dense 128 x 128 f16 matrix a (part, group) -- `tdense`,
|
||||
* which rad-convert composes from the pairs and angles and stores as the weight's "t.dense" table --
|
||||
* then the block-scaled quantiser `out` names: "fp8" E4M3 with scale amax / 448, "i8" scale
|
||||
* amax / 127 rounded to nearest; scale 1 for an all-zero group, as libref. The matrices are widened
|
||||
* to f32 once a call; a row is then a matrix-vector product a group, in f32 where libref sums in
|
||||
* f64, so a code can differ from libref's where the value sits on a rounding edge. */
|
||||
AVX_KERNEL(givens_dense_quant) {
|
||||
if (!rad_args_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;
|
||||
const char* outs = p_str(a, "out", "");
|
||||
if (std::strcmp(outs, 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;
|
||||
|
||||
float* T = (float*)scratch_raw((size_t)(P * n * g + P * n) * sizeof(float) + 64);
|
||||
float* rs = T + P * n * g;
|
||||
for (int64_t i = 0; i < P * n; ++i) {
|
||||
row_to_f32(tm->dtype, byte_at(tm->data, tm->dtype, rowoff(tm, i, g)), T + i * g, g);
|
||||
rs[i] = ldt(sc, i);
|
||||
}
|
||||
|
||||
AVX_PARALLEL_FOR_IF(rows * P * n * g >= AVX_PAR_MIN)
|
||||
for (int64_t r = 0; r < rows; ++r) {
|
||||
float* buf = scratch_f32((size_t)n);
|
||||
float* out = scratch_f32_b((size_t)(P * n));
|
||||
float* in = scratch_f32_c((size_t)g);
|
||||
const float* v = in_row(x, rowoff(x, r, n), n, buf);
|
||||
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[i] = v[gi * g + i] / rs[p * n + gi * g + i];
|
||||
float* yv = out + p * n + gi * g;
|
||||
const float* Tg = T + (p * n + gi * g) * g;
|
||||
for (int64_t j = 0; j < g; ++j) {
|
||||
vf acc = vf_zero();
|
||||
for (int64_t i = 0; i < g; i += VF_N)
|
||||
acc = vf_fma(vf_loadu(Tg + j * g + i), vf_loadu(in + i), acc);
|
||||
yv[j] = vf_hsum(acc);
|
||||
}
|
||||
const float amax = row_amax(yv, g);
|
||||
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 t = yv[j] * inv;
|
||||
t = t > qmax ? qmax : (t < -qmax ? -qmax : t);
|
||||
yv[j] = i8 ? std::nearbyint(t) : t;
|
||||
}
|
||||
stt(s, rowoff(s, r, P * ngrp) + (p * ngrp + gi) * laststride(s), scl);
|
||||
}
|
||||
out_row(q, rowoff(q, r, P * n), P * n, out);
|
||||
}
|
||||
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 a
|
||||
|
||||
+27
-1
@@ -181,6 +181,16 @@ static const RadParamSpec pQuant[] = { P_INT("M"), P_INT("n"), P_INT("group
|
||||
static const RadOperandSpec oQuant[] = { OPD("x"), OUT("q"), OUT("scale"), OUT_O("asum") };
|
||||
static const RadOperandSpec oHadQuant[] = { OPD("x"), OUT("q"), OUT("scale") };
|
||||
static const RadOperandSpec oQuantFp8[] = { OPD("x"), OUT("q"), OUT("scale") };
|
||||
/* ParoQuant's rotation, libref's declaration: the tables are the "givens" encoding's
|
||||
* (abi/rad_encoding.h) and WEIGHT operands, resolved 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: the unfused pair's operands minus the tensor between them, and `out_bf16` to
|
||||
* put even that back. */
|
||||
@@ -218,6 +228,8 @@ 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_INTO("pb1"), P_INTO("pb2") };
|
||||
static const RadParamSpec pGemmQBias[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("group"),
|
||||
P_STR("dtype") };
|
||||
static const RadOperandSpec oGemmQ[] = { OPD("a"), OPD_O("a_scale"), WGT("b"), WGT("b_scale"),
|
||||
OUT("y"), OPD_O("a_sum"), WGT_O("b_ref") };
|
||||
@@ -565,6 +577,13 @@ static const RadOpSchema kSchemas[] = {
|
||||
"the 1/group is folded into the stored weight scale at convert (docs/OPS.md)." },
|
||||
{ "gram_accum", ARR(pQuant), ARR(oGram),
|
||||
"h += x^T x over a dequantised E4M3 activation. `h` is [n, n] and INOUT." },
|
||||
{ "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, then the block-scaled quantiser `out` names "
|
||||
"(\"i8\": amax / 127, round to nearest; \"fp8\": E4M3, amax / 448)." },
|
||||
{ "givens_rotate", ARR(pGivensRotate), ARR(oGivensRotate),
|
||||
"ParoQuant's rotation without a quantiser: x / rscale rotated by `stages` rounds of Givens "
|
||||
"pairs, `parts` copies side by side, in y's own dtype." },
|
||||
{ "had_quant_act_i8", ARR(pQuant), ARR(oHadQuant),
|
||||
"Walsh-Hadamard rotation at width `group`, then symmetric int8 with ONE scale per row carrying "
|
||||
"the 1/sqrt(group)." },
|
||||
@@ -589,7 +608,7 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "gemm_nt_q", ARR(pGemmQ), ARR(oGemmQ),
|
||||
"C = A B^T with group-scaled quantised operands. `a_sum` and `b_ref` are libr4d's asymmetric and "
|
||||
"mxfp4 forms and are ignored here, as they are in libref." },
|
||||
{ "gemm_nt_q_bias", ARR(pGemmQ), ARR(oGemmQBias), "quantised C = A B^T + bias." },
|
||||
{ "gemm_nt_q_bias", ARR(pGemmQBias), ARR(oGemmQBias), "quantised C = A B^T + bias." },
|
||||
{ "gemm_nt_q_gated", ARR(pGemmQGated), ARR(oGemmQGated),
|
||||
"gemm_nt_q over a [N, K] gate_up plane with gated_quant_fp8's pass in the epilogue, byte for "
|
||||
"byte the pair. `N` is the weight's rows, twice the fused width; the output is [M, N/2]." },
|
||||
@@ -782,6 +801,13 @@ SIMPLE("avx_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",
|
||||
L(had_quant_act_fp8)),
|
||||
SIMPLE("avx_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", L(givens_rotate)),
|
||||
SIMPLE("avx_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",
|
||||
L(givens_dense_quant)),
|
||||
SIMPLE("avx_gram_accum", "gram_accum", "quant",
|
||||
"h += x^T x over a dequantised E4M3 activation, or a plain bf16 one",
|
||||
"fp8_e4m3 plus an f32 scale per group, or bf16 with no scale, in; an f32 [n][n] "
|
||||
|
||||
Reference in new issue
Block a user