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
+178
-1
@@ -262,7 +262,8 @@ TEST(op_vocabulary) {
|
||||
"embed_lookup_q", "cast",
|
||||
"scale_rows",
|
||||
"softmax",
|
||||
"quant_act_i8", "quant_act_fp8", "had_quant_act_fp8", "had_quant_act_i8", "gram_accum",
|
||||
"quant_act_i8", "quant_act_fp8", "had_quant_act_fp8", "givens_dense_quant", "givens_rotate", "had_quant_act_i8",
|
||||
"gram_accum",
|
||||
/* The fused fp8 forms. A host run needs EVERY op covered, so these are what stands
|
||||
* between a host-backend run and "site 'host' is unavailable for this band". */
|
||||
"rmsnorm_quant_fp8", "gated_quant_fp8", "gemm_nt_q_gated", "dflash_select",
|
||||
@@ -1644,6 +1645,182 @@ TEST(op_gemm_nt_q) {
|
||||
CHECK_EQ(y3.at(1), (3 * 2.0f + 4 * 4.0f) * 1.0f);
|
||||
}
|
||||
|
||||
/* A weight whose row PARTS meet their own copy of the activation: parts = 2 rows [0, 16) and
|
||||
* [16, 32) over K = 128, the copies side by side in one row of `a`. The parted product must be the
|
||||
* plain product of each part's rows against its own copy -- computed here as two plain calls. */
|
||||
TEST(op_gemm_nt_q_parts_read_their_own_activation_copy) {
|
||||
const int64_t M = 3, N = 32, K = 128, P = 2;
|
||||
uint32_t rng = 7;
|
||||
auto nxt = [&]() { rng = rng * 1664525u + 1013904223u; return rng >> 9; };
|
||||
T a2(RAD_F8E4M3, {M, P * K});
|
||||
std::vector<float> av((size_t)(M * P * K));
|
||||
for (auto& v : av) v = (float)((int)(nxt() % 15) - 7) * 0.25f;
|
||||
a2.f(av);
|
||||
T as2(RAD_F32, {M, P});
|
||||
std::vector<float> asv((size_t)(M * P));
|
||||
for (auto& v : asv) v = 0.01f * (float)(1 + nxt() % 9);
|
||||
as2.f(asv);
|
||||
T b_(RAD_FP4E2M1, {N, K});
|
||||
for (int64_t i = 0; i < N * K / 2; ++i) b_.mem[(size_t)i] = (uint8_t)nxt();
|
||||
T bs(RAD_U8, {N, K / 32});
|
||||
for (int64_t i = 0; i < N * K / 32; ++i) bs.mem[(size_t)i] = (uint8_t)(124 + nxt() % 5);
|
||||
|
||||
T y(RAD_BF16, {M, N});
|
||||
A a; a.t(a2).t(as2).t(b_).t(bs).t(y).tnull().tnull()
|
||||
.i("M", M).i("N", N).i("K", K).i("group", 32).s("dtype", "mxfp4a8").i("pb1", 16);
|
||||
CHECK_OK(run("gemm_nt_q", a));
|
||||
|
||||
for (int64_t p = 0; p < P; ++p) {
|
||||
T ap(RAD_F8E4M3, {M, K}), asp(RAD_F32, {M, 1});
|
||||
std::vector<float> v1((size_t)(M * K)), v2((size_t)M);
|
||||
for (int64_t m = 0; m < M; ++m) {
|
||||
for (int64_t k = 0; k < K; ++k) v1[(size_t)(m * K + k)] = av[(size_t)(m * P * K + p * K + k)];
|
||||
v2[(size_t)m] = asv[(size_t)(m * P + p)];
|
||||
}
|
||||
ap.f(v1);
|
||||
asp.f(v2);
|
||||
T bp(RAD_FP4E2M1, {N / P, K}), bsp(RAD_U8, {N / P, K / 32});
|
||||
std::memcpy(bp.mem.data(), b_.mem.data() + p * (N / P) * K / 2, (size_t)((N / P) * K / 2));
|
||||
std::memcpy(bsp.mem.data(), bs.mem.data() + p * (N / P) * K / 32, (size_t)((N / P) * K / 32));
|
||||
T yp(RAD_BF16, {M, N / P});
|
||||
A c; c.t(ap).t(asp).t(bp).t(bsp).t(yp).tnull().tnull()
|
||||
.i("M", M).i("N", N / P).i("K", K).i("group", 32).s("dtype", "mxfp4a8");
|
||||
CHECK_OK(run("gemm_nt_q", c));
|
||||
int bad = 0;
|
||||
for (int64_t m = 0; m < M; ++m)
|
||||
for (int64_t n = 0; n < N / P; ++n)
|
||||
if (y.at(m * N + p * (N / P) + n) != yp.at(m * (N / P) + n)) ++bad;
|
||||
CHECK_EQ(bad, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* ParoQuant's activation quantiser, the rotation as dense per-group matrices. T[j][i] of a (part, group) is
|
||||
* the group's composed network applied to the unit vector e_i, built here in double from the same pairs and
|
||||
* angles the device layout hook reads; with it the dequantised row is the rotated row to within the
|
||||
* quantiser's grid (plus the f16 rounding of T), and the rotation preserves the row's norm -- both checked
|
||||
* per part against a double-precision rotation. With T the identity and unit channel scales it IS
|
||||
* quant_act_fp8, exactly. Run for both quantisers `out` names. */
|
||||
TEST(op_givens_dense_quant) {
|
||||
const int64_t M = 3, n = 256, S = 3, P = 2, G = 128;
|
||||
uint32_t rng = 99;
|
||||
auto nxt = [&]() { rng = rng * 1664525u + 1013904223u; return rng >> 8; };
|
||||
auto frand = [&](float lo, float hi) { return lo + (hi - lo) * (float)(nxt() % 10000) / 10000.0f; };
|
||||
T x(RAD_BF16, {M, n});
|
||||
std::vector<float> xv((size_t)(M * n));
|
||||
for (auto& v : xv) v = frand(-3.0f, 3.0f);
|
||||
for (int64_t m = 0; m < M; ++m) xv[(size_t)(m * n + 7)] = 0; // a zero in the mix
|
||||
x.f(xv);
|
||||
// a pairing of each group of 128 per stage per part
|
||||
std::vector<int32_t> pairs((size_t)(P * S * n));
|
||||
for (int64_t ps = 0; ps < P * S; ++ps)
|
||||
for (int64_t g = 0; g < n / G; ++g) {
|
||||
std::vector<int32_t> perm((size_t)G);
|
||||
for (int i = 0; i < G; ++i) perm[(size_t)i] = (int32_t)(g * G + i);
|
||||
for (int i = (int)G - 1; i > 0; --i) std::swap(perm[(size_t)i], perm[nxt() % (i + 1)]);
|
||||
for (int i = 0; i < G; ++i) pairs[(size_t)(ps * n + g * G + i)] = perm[(size_t)i];
|
||||
}
|
||||
std::vector<float> av((size_t)(P * S * n / 2)), sv((size_t)(P * n));
|
||||
for (auto& v : av) v = frand(-3.0f, 3.0f);
|
||||
for (auto& v : sv) v = frand(0.5f, 2.0f);
|
||||
T rs(RAD_F32, {P, n});
|
||||
rs.f(sv);
|
||||
|
||||
// the composed matrices, [P * n/G groups][G (j)][G (i)], as f16
|
||||
auto compose = [&](const std::vector<float>& angles, std::vector<float>* tm) {
|
||||
tm->assign((size_t)(P * (n / G) * G * G), 0.0f);
|
||||
for (int64_t p = 0; p < P; ++p)
|
||||
for (int64_t g = 0; g < n / G; ++g)
|
||||
for (int64_t i = 0; i < G; ++i) {
|
||||
std::vector<double> v((size_t)G, 0.0);
|
||||
v[(size_t)i] = 1.0;
|
||||
for (int64_t st = 0; st < S; ++st)
|
||||
for (int64_t j = 0; j < G / 2; ++j) {
|
||||
const int64_t ia = pairs[(size_t)((p * S + st) * n + g * G + 2 * j)] - g * G;
|
||||
const int64_t ib = pairs[(size_t)((p * S + st) * n + g * G + 2 * j + 1)] - g * G;
|
||||
const double t = angles[(size_t)((p * S + st) * (n / 2) + g * (G / 2) + j)];
|
||||
const double c = std::cos(t), sn = std::sin(t);
|
||||
const double va = v[(size_t)ia], vb = v[(size_t)ib];
|
||||
v[(size_t)ia] = va * c + vb * sn;
|
||||
v[(size_t)ib] = vb * c - va * sn;
|
||||
}
|
||||
for (int64_t j = 0; j < G; ++j)
|
||||
(*tm)[(size_t)(((p * (n / G) + g) * G + j) * G + i)] = (float)v[(size_t)j];
|
||||
}
|
||||
};
|
||||
std::vector<float> tmv;
|
||||
compose(av, &tmv);
|
||||
T tm(RAD_F16, {P * n, G});
|
||||
tm.f(tmv);
|
||||
|
||||
for (int i8 = 0; i8 < 2; ++i8) {
|
||||
const double qmax = i8 ? 127.0 : 448.0;
|
||||
const uint32_t qdt = i8 ? RAD_I8 : RAD_F8E4M3;
|
||||
T q(qdt, {M, P * n}), s(RAD_F32, {M, P * n / G});
|
||||
A a; a.t(x).t(tm).t(rs).t(q).t(s)
|
||||
.i("M", M).i("n", n).i("group", G).s("dtype", "bf16").i("parts", P).s("out", i8 ? "i8" : "fp8");
|
||||
CHECK_OK(run("givens_dense_quant", a));
|
||||
double worst = 0;
|
||||
int bad_norm = 0;
|
||||
for (int64_t m = 0; m < M; ++m)
|
||||
for (int64_t p = 0; p < P; ++p) {
|
||||
std::vector<double> b((size_t)n);
|
||||
double n0 = 0;
|
||||
for (int64_t k = 0; k < n; ++k) {
|
||||
b[(size_t)k] = (double)x.at(m * n + k) / (double)sv[(size_t)(p * n + k)];
|
||||
n0 += b[(size_t)k] * b[(size_t)k];
|
||||
}
|
||||
for (int64_t st = 0; st < S; ++st)
|
||||
for (int64_t j = 0; j < n / 2; ++j) {
|
||||
const int64_t ia = pairs[(size_t)((p * S + st) * n + 2 * j)];
|
||||
const int64_t ib = pairs[(size_t)((p * S + st) * n + 2 * j + 1)];
|
||||
const double t = av[(size_t)((p * S + st) * (n / 2) + j)], c = std::cos(t), sn = std::sin(t);
|
||||
const double va = b[(size_t)ia], vb = b[(size_t)ib];
|
||||
b[(size_t)ia] = va * c + vb * sn;
|
||||
b[(size_t)ib] = vb * c - va * sn;
|
||||
}
|
||||
double n1 = 0;
|
||||
for (int64_t k = 0; k < n; ++k) n1 += b[(size_t)k] * b[(size_t)k];
|
||||
if (std::fabs(n1 - n0) > 1e-9 * n0) ++bad_norm;
|
||||
for (int64_t g = 0; g < n / G; ++g) {
|
||||
double amax = 0;
|
||||
for (int64_t i = 0; i < G; ++i) amax = std::max(amax, std::fabs(b[(size_t)(g * G + i)]));
|
||||
const double sc = s.at(m * (P * n / G) + p * (n / G) + g);
|
||||
CHECK(std::fabs(sc - amax / qmax) < 2e-3 * amax / qmax + 1e-12); // T is f16
|
||||
for (int64_t i = 0; i < G; ++i) {
|
||||
const double deq = (double)q.at(m * P * n + p * n + g * G + i) * sc;
|
||||
worst = std::max(worst, std::fabs(deq - b[(size_t)(g * G + i)]) / amax);
|
||||
}
|
||||
}
|
||||
}
|
||||
CHECK_EQ(bad_norm, 0);
|
||||
CHECK(worst < (i8 ? 0.006 : 0.04)); // half a step of the grid (int8: 1/254 of the maximum), and T's f16
|
||||
}
|
||||
|
||||
// T the identity, unit scales: exactly quant_act_fp8
|
||||
std::vector<float> eye((size_t)(P * (n / G) * G * G), 0.0f), ones((size_t)(P * n), 1.0f);
|
||||
for (int64_t pg = 0; pg < P * (n / G); ++pg)
|
||||
for (int64_t i = 0; i < G; ++i) eye[(size_t)((pg * G + i) * G + i)] = 1.0f;
|
||||
T tid(RAD_F16, {P * n, G}), rs1(RAD_F32, {P, n});
|
||||
tid.f(eye);
|
||||
rs1.f(ones);
|
||||
T q1(RAD_F8E4M3, {M, P * n}), s1(RAD_F32, {M, P * n / G});
|
||||
A b; b.t(x).t(tid).t(rs1).t(q1).t(s1)
|
||||
.i("M", M).i("n", n).i("group", G).s("dtype", "bf16").i("parts", P).s("out", "fp8");
|
||||
CHECK_OK(run("givens_dense_quant", b));
|
||||
T q2(RAD_F8E4M3, {M, n}), s2(RAD_F32, {M, n / G});
|
||||
A c; c.t(x).t(q2).t(s2).i("M", M).i("n", n).i("group", G).s("dtype", "bf16");
|
||||
CHECK_OK(run("quant_act_fp8", c));
|
||||
int diff = 0;
|
||||
for (int64_t m = 0; m < M; ++m)
|
||||
for (int64_t p = 0; p < P; ++p) {
|
||||
for (int64_t k = 0; k < n; ++k)
|
||||
if (q1.mem[(size_t)(m * P * n + p * n + k)] != q2.mem[(size_t)(m * n + k)]) ++diff;
|
||||
for (int64_t g = 0; g < n / G; ++g)
|
||||
if (s1.at(m * (P * n / G) + p * (n / G) + g) != s2.at(m * (n / G) + g)) ++diff;
|
||||
}
|
||||
CHECK_EQ(diff, 0);
|
||||
}
|
||||
|
||||
/* A BLOCK GRID WHOSE LAST BLOCK IS PARTIAL, on both axes. K = 300 at 128 is three scales over 128,
|
||||
* 128 and 44 columns; N = 129 is two row blocks, the second of one row, and N = 300 is three. The
|
||||
* expected value of every output is written out from that definition: with every code 1, output
|
||||
|
||||
Reference in new issue
Block a user