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
@@ -8,6 +8,7 @@
|
||||
*/
|
||||
#include "rad_test.h"
|
||||
#include "format/checkpoint.h"
|
||||
#include "format/encoding.h"
|
||||
#include "rad_plugin.h"
|
||||
|
||||
#include <cmath>
|
||||
@@ -845,3 +846,419 @@ TEST(a_ct_int4_checkpoint_this_does_not_read_is_refused) {
|
||||
remove_checkpoint(dir);
|
||||
}
|
||||
}
|
||||
|
||||
/* PAROQUANT MXFP4 is Quark's codes and exponents in a rotated basis plus the rotations: each
|
||||
* projection's pairs (group-local int16), theta and channel scales. Gate and up are two PARTS of
|
||||
* the fused weight, and the tables must say so: pairs made whole-row, angles widened, the channel
|
||||
* scale inverted, the part ends. Decoding the planes through the core must then give a weight
|
||||
* that, rotated forward again, is the codes the checkpoint shipped. */
|
||||
TEST(a_paroquant_mxfp4_weight_carries_its_rotations_as_parts) {
|
||||
const int64_t K = 128, S = 2;
|
||||
uint32_t rng = 12345;
|
||||
auto next = [&]() { rng = rng * 1664525u + 1013904223u; return rng >> 8; };
|
||||
auto codes = [&](const std::string& n, int64_t N) {
|
||||
T t{ n, "U8", { N, K / 2 }, {} };
|
||||
for (int64_t i = 0; i < N * K / 2; ++i) t.bytes.push_back((uint8_t)(next() & 0xff));
|
||||
return t;
|
||||
};
|
||||
auto exps = [&](const std::string& n, int64_t N) {
|
||||
T t{ n, "U8", { N, K / 32 }, {} };
|
||||
for (int64_t i = 0; i < N * K / 32; ++i) t.bytes.push_back((uint8_t)(122 + next() % 5));
|
||||
return t;
|
||||
};
|
||||
auto pairs = [&](const std::string& n) {
|
||||
T t{ n, "I16", { S, K }, {} };
|
||||
for (int64_t s = 0; s < S; ++s) {
|
||||
std::vector<int16_t> perm((size_t)K);
|
||||
for (int64_t i = 0; i < K; ++i) perm[(size_t)i] = (int16_t)i;
|
||||
for (int64_t i = K - 1; i > 0; --i) std::swap(perm[(size_t)i], perm[next() % (i + 1)]);
|
||||
for (int64_t i = 0; i < K; ++i) {
|
||||
t.bytes.push_back((uint8_t)(perm[(size_t)i] & 0xff));
|
||||
t.bytes.push_back((uint8_t)(perm[(size_t)i] >> 8));
|
||||
}
|
||||
}
|
||||
return t;
|
||||
};
|
||||
auto f16s = [&](const std::string& n, std::vector<int64_t> shape, float lo, float hi) {
|
||||
T t{ n, "F16", shape, {} };
|
||||
int64_t cnt = 1;
|
||||
for (int64_t d : shape) cnt *= d;
|
||||
for (int64_t i = 0; i < cnt; ++i) {
|
||||
const float f = lo + (hi - lo) * (float)(next() % 1000) / 1000.0f;
|
||||
const uint16_t h = rad_f32_to_f16(f);
|
||||
t.bytes.push_back((uint8_t)(h & 0xff));
|
||||
t.bytes.push_back((uint8_t)(h >> 8));
|
||||
}
|
||||
return t;
|
||||
};
|
||||
const T gw = codes("g.weight", 16), gs = exps("g.weight_scale", 16), gp = pairs("g.pairs"),
|
||||
gt = f16s("g.theta", { S, K / 2 }, -3.0f, 3.0f), gc = f16s("g.channel_scales", { 1, K }, 0.5f, 2.0f);
|
||||
const T uw = codes("u.weight", 16), us = exps("u.weight_scale", 16), up = pairs("u.pairs"),
|
||||
ut = f16s("u.theta", { S, K / 2 }, -3.0f, 3.0f), uc = f16s("u.channel_scales", { 1, K }, 0.5f, 2.0f);
|
||||
// a per-row fp8 head beside them: weight_scale is one f32 a row
|
||||
T hs{ "h.weight_scale", "F32", { 4 }, {} };
|
||||
for (int i = 0; i < 4; ++i) { float f = 0.25f * (float)(i + 1); hs.bytes.resize(hs.bytes.size() + 4); std::memcpy(hs.bytes.data() + 4 * i, &f, 4); }
|
||||
const std::string dir = write_checkpoint(
|
||||
{ gw, gs, gp, gt, gc, uw, us, up, ut, uc, fp8("h.weight", { 4, 8 }), hs },
|
||||
"{\"model_type\":\"qwen3_5\",\"quantization_config\":{\"quant_method\":\"paroquant_mxfp4\","
|
||||
"\"format\":\"mxfp4\",\"bits\":4,\"group_size\":128,\"mxfp4_block\":32,\"krot\":2}}");
|
||||
Checkpoint ck;
|
||||
REQUIRE_EQ(ck.open(dir), RAD_OK);
|
||||
CHECK(ck.imports() == Checkpoint::Import::ParoMx);
|
||||
Program prog;
|
||||
prog.name_map.push_back(map("gu", RAD_MAP_CONCAT, { "g.weight", "u.weight" }));
|
||||
prog.name_map.push_back(map("h", RAD_MAP_COPY, { "h.weight" }));
|
||||
CkptWeight w;
|
||||
std::string why;
|
||||
REQUIRE_EQ(ckpt_resolve(ck, prog, "gu", false, &w, &why), RAD_OK);
|
||||
REQUIRE(w.paro);
|
||||
CHECK(w.imported && !w.trivial);
|
||||
REQUIRE(rad_enc_valid(&w.enc));
|
||||
CHECK_EQ(std::string(w.enc.transform), std::string("givens"));
|
||||
CHECK_EQ(rad_enc_givens_parts(&w.enc), (int64_t)2);
|
||||
CHECK_EQ(w.rows, (int64_t)32);
|
||||
REQUIRE_EQ(w.enc.n_planes, 7);
|
||||
|
||||
std::vector<std::vector<uint8_t>> pl((size_t)w.enc.n_planes);
|
||||
for (int k = 0; k < w.enc.n_planes; ++k) {
|
||||
int64_t pr = 0, pc = 0;
|
||||
rad_enc_plane_dims(&w.enc.plane[k], w.rows, w.cols, &pr, &pc);
|
||||
pl[(size_t)k].resize((size_t)(pr * rad_enc_row_bytes(w.enc.plane[k].dtype, pc)));
|
||||
REQUIRE_EQ(ckpt_plane_rows(w, k, 0, pr, pl[(size_t)k].data(), &why), RAD_OK);
|
||||
}
|
||||
// codes and exponents are the checkpoint's bytes, joined
|
||||
CHECK_EQ(std::memcmp(pl[0].data(), gw.bytes.data(), gw.bytes.size()), 0);
|
||||
CHECK_EQ(std::memcmp(pl[0].data() + gw.bytes.size(), uw.bytes.data(), uw.bytes.size()), 0);
|
||||
CHECK_EQ(std::memcmp(pl[1].data() + gs.bytes.size(), us.bytes.data(), us.bytes.size()), 0);
|
||||
const int32_t* tp = (const int32_t*)pl[2].data();
|
||||
const float* ta = (const float*)pl[3].data();
|
||||
const float* tsc = (const float*)pl[4].data();
|
||||
const int32_t* tpart = (const int32_t*)pl[5].data();
|
||||
CHECK_EQ(tpart[0], 16);
|
||||
CHECK_EQ(tpart[1], 32);
|
||||
for (int64_t c = 0; c < K; ++c) {
|
||||
CHECK_EQ(tp[c], (int32_t)(*(const int16_t*)(gp.bytes.data() + 2 * c))); // part 0, stage 0
|
||||
CHECK_EQ(tp[(S + 1) * K + c], (int32_t)(*(const int16_t*)(up.bytes.data() + 2 * (K + c)))); // part 1, stage 1
|
||||
const uint16_t h = *(const uint16_t*)(gc.bytes.data() + 2 * c);
|
||||
CHECK(std::fabs(tsc[c] - 1.0f / rad_f16_to_f32(h)) < 1e-6f);
|
||||
}
|
||||
CHECK(std::fabs(ta[0] - rad_f16_to_f32(*(const uint16_t*)gt.bytes.data())) == 0.0f);
|
||||
/* t.dense: each (part, group) row is that part's stages composed, so T x is x rotated stage by
|
||||
* stage -- checked on a vector against the tables above, in f64, to the f16 the matrix is in */
|
||||
REQUIRE_EQ(std::string(w.enc.plane[6].role), std::string("t.dense"));
|
||||
{
|
||||
const uint16_t* td = (const uint16_t*)pl[6].data();
|
||||
const int64_t nkb = K / 128;
|
||||
double worst_d = 0;
|
||||
for (int64_t part = 0; part < 2; ++part)
|
||||
for (int64_t g = 0; g < nkb; ++g) {
|
||||
std::vector<double> x(128), y(128, 0.0);
|
||||
for (int i = 0; i < 128; ++i) x[(size_t)i] = std::sin(0.7 * i + part + 3.0 * g);
|
||||
for (int j = 0; j < 128; ++j)
|
||||
for (int i = 0; i < 128; ++i)
|
||||
y[(size_t)j] += rad_f16_to_f32(td[((part * nkb + g) * 128 + j) * 128 + i]) * x[(size_t)i];
|
||||
std::vector<double> v = x;
|
||||
for (int64_t st = 0; st < S; ++st)
|
||||
for (int j = 0; j < 64; ++j) {
|
||||
const int64_t row = part * S + st;
|
||||
const int a = tp[row * K + g * 128 + 2 * j] - (int)(g * 128);
|
||||
const int b = tp[row * K + g * 128 + 2 * j + 1] - (int)(g * 128);
|
||||
const double t = ta[row * (K / 2) + g * 64 + j], va = v[(size_t)a], vb = v[(size_t)b];
|
||||
v[(size_t)a] = va * std::cos(t) + vb * std::sin(t);
|
||||
v[(size_t)b] = vb * std::cos(t) - va * std::sin(t);
|
||||
}
|
||||
for (int j = 0; j < 128; ++j) worst_d = std::max(worst_d, std::fabs(y[(size_t)j] - v[(size_t)j]));
|
||||
}
|
||||
CHECK(worst_d < 5e-3);
|
||||
}
|
||||
|
||||
// decode, rotate forward again with the checkpoint's own convention, compare to the codes
|
||||
const void* data[7];
|
||||
for (int k = 0; k < 7; ++k) data[k] = pl[(size_t)k].data();
|
||||
EncodedView v;
|
||||
enc_view(w.enc, w.rows, w.cols, data, &v);
|
||||
std::vector<float> dec((size_t)(w.rows * K));
|
||||
REQUIRE_EQ(enc_decode_rows(v, 0, w.rows, dec.data(), &why), RAD_OK);
|
||||
const T* cw[2] = { &gw, &uw };
|
||||
const T* es[2] = { &gs, &us };
|
||||
const T* pp[2] = { &gp, &up };
|
||||
const T* th[2] = { >, &ut };
|
||||
const T* cc[2] = { &gc, &uc };
|
||||
double worst = 0;
|
||||
for (int64_t r = 0; r < w.rows; ++r) {
|
||||
const int part = r >= 16, rr = r - 16 * part;
|
||||
std::vector<double> x((size_t)K);
|
||||
for (int64_t k = 0; k < K; ++k)
|
||||
x[(size_t)k] = (double)dec[(size_t)(r * K + k)] *
|
||||
(1.0 / rad_f16_to_f32(*(const uint16_t*)(cc[part]->bytes.data() + 2 * k)));
|
||||
for (int64_t s = 0; s < S; ++s)
|
||||
for (int64_t p = 0; p < K / 2; ++p) {
|
||||
const int16_t* pv = (const int16_t*)pp[part]->bytes.data();
|
||||
const int64_t a = pv[s * K + 2 * p], b = pv[s * K + 2 * p + 1];
|
||||
const double t = rad_f16_to_f32(*(const uint16_t*)(th[part]->bytes.data() + 2 * (s * (K / 2) + p)));
|
||||
const double c = std::cos(t), sn = std::sin(t);
|
||||
const double xa = x[(size_t)a], xb = x[(size_t)b];
|
||||
x[(size_t)a] = xa * c + xb * sn;
|
||||
x[(size_t)b] = xb * c - xa * sn;
|
||||
}
|
||||
for (int64_t k = 0; k < K; ++k) {
|
||||
const uint8_t bt = cw[part]->bytes[(size_t)(rr * K / 2 + k / 2)];
|
||||
const float code = rad_fp4e2m1_to_f32((uint8_t)(k % 2 ? bt >> 4 : bt & 15));
|
||||
const float sc = std::ldexp(1.0f, (int)es[part]->bytes[(size_t)(rr * K / 32 + k / 32)] - 127);
|
||||
worst = std::max(worst, std::fabs(x[(size_t)k] - (double)code * sc));
|
||||
}
|
||||
}
|
||||
CHECK(worst < 1e-4);
|
||||
|
||||
// the per-row fp8 head: one f32 scale a row, read as [N, 1]
|
||||
REQUIRE_EQ(ckpt_resolve(ck, prog, "h", false, &w, &why), RAD_OK);
|
||||
REQUIRE(w.trivial);
|
||||
CHECK_EQ(w.enc.plane[1].block[0], (int64_t)1);
|
||||
CHECK_EQ(w.enc.plane[1].block[1], (int64_t)0);
|
||||
std::vector<float> f(4 * 8);
|
||||
REQUIRE_EQ(ckpt_rows_f32(w, 0, 4, f.data(), &why), RAD_OK);
|
||||
for (int r = 0; r < 4; ++r) {
|
||||
const float q = rad_load_f32(fp8("h.weight", { 4, 8 }).bytes.data(), RAD_F8E4M3, r * 8);
|
||||
CHECK_EQ(f[(size_t)(r * 8)], q * 0.25f * (float)(r + 1));
|
||||
}
|
||||
remove_checkpoint(dir);
|
||||
}
|
||||
|
||||
/* PAROQUANT INT5-BITPLANE: the low nibbles AWQ-packed along N (qweight [K, N/8], eight columns a
|
||||
* word in the order 0,2,4,6,1,3,5,7), a fifth-bit plane (qweight_hi [K, N/32], bit n % 32) and the
|
||||
* same two packings of the zero points. The logical [N, K] weight comes out as u5 codes (a bit
|
||||
* stream along K), an f16 scale and a u8 zero a 128 of K, plus the rotation as `givens` tables. */
|
||||
TEST(a_paroquant_int5_weight_is_relaid_row_by_row) {
|
||||
const int64_t N = 64, K = 256, G = K / 128;
|
||||
uint32_t rng = 777;
|
||||
auto next = [&]() { rng = rng * 1664525u + 1013904223u; return rng >> 8; };
|
||||
std::vector<int> code((size_t)(N * K)), zero((size_t)(N * G));
|
||||
for (auto& c : code) c = (int)(next() % 32);
|
||||
for (auto& z : zero) z = (int)(next() % 32);
|
||||
static const int kOrder[8] = { 0, 2, 4, 6, 1, 3, 5, 7 };
|
||||
auto pack_awq = [&](const std::vector<int>& v, int64_t rows) { // v[n * rows + r] -> [rows, N/8]
|
||||
T t{ "", "I32", { rows, N / 8 }, {} };
|
||||
t.bytes.assign((size_t)(rows * N / 8 * 4), 0);
|
||||
for (int64_t r = 0; r < rows; ++r)
|
||||
for (int64_t n = 0; n < N; ++n) {
|
||||
int j = 0;
|
||||
while (kOrder[j] != n % 8) ++j;
|
||||
uint32_t w;
|
||||
uint8_t* p = t.bytes.data() + (r * (N / 8) + n / 8) * 4;
|
||||
std::memcpy(&w, p, 4);
|
||||
w |= (uint32_t)(v[(size_t)(n * rows + r)] & 15) << (4 * j);
|
||||
std::memcpy(p, &w, 4);
|
||||
}
|
||||
return t;
|
||||
};
|
||||
auto pack_hi = [&](const std::vector<int>& v, int64_t rows) {
|
||||
T t{ "", "I32", { rows, N / 32 }, {} };
|
||||
t.bytes.assign((size_t)(rows * N / 32 * 4), 0);
|
||||
for (int64_t r = 0; r < rows; ++r)
|
||||
for (int64_t n = 0; n < N; ++n) {
|
||||
uint32_t w;
|
||||
uint8_t* p = t.bytes.data() + (r * (N / 32) + n / 32) * 4;
|
||||
std::memcpy(&w, p, 4);
|
||||
w |= (uint32_t)((v[(size_t)(n * rows + r)] >> 4) & 1) << (n % 32);
|
||||
std::memcpy(p, &w, 4);
|
||||
}
|
||||
return t;
|
||||
};
|
||||
T qw = pack_awq(code, K), qwh = pack_hi(code, K), qz = pack_awq(zero, G), qzh = pack_hi(zero, G);
|
||||
qw.name = "q.qweight"; qwh.name = "q.qweight_hi"; qz.name = "q.qzeros"; qzh.name = "q.qzeros_hi";
|
||||
T sc{ "q.scales", "F16", { G, N }, {} };
|
||||
for (int64_t i = 0; i < G * N; ++i) {
|
||||
const uint16_t h = rad_f32_to_f16(0.01f * (float)(1 + next() % 50));
|
||||
sc.bytes.push_back((uint8_t)(h & 0xff));
|
||||
sc.bytes.push_back((uint8_t)(h >> 8));
|
||||
}
|
||||
T pr{ "q.pairs", "I16", { 2, K }, {} };
|
||||
for (int64_t s = 0; s < 2; ++s)
|
||||
for (int64_t i = 0; i < K; ++i) {
|
||||
const int16_t v = (int16_t)(i % 128 ^ 1); // adjacent swap: a pairing
|
||||
pr.bytes.push_back((uint8_t)(v & 0xff));
|
||||
pr.bytes.push_back((uint8_t)(v >> 8));
|
||||
}
|
||||
T th{ "q.theta", "F16", { 2, K / 2 }, {} }, cs{ "q.channel_scales", "F16", { 1, K }, {} };
|
||||
for (int64_t i = 0; i < 2 * K / 2; ++i) { const uint16_t h = rad_f32_to_f16(0.3f); th.bytes.push_back(h & 0xff); th.bytes.push_back(h >> 8); }
|
||||
for (int64_t i = 0; i < K; ++i) { const uint16_t h = rad_f32_to_f16(1.5f); cs.bytes.push_back(h & 0xff); cs.bytes.push_back(h >> 8); }
|
||||
const std::string dir = write_checkpoint(
|
||||
{ qw, qwh, qz, qzh, sc, pr, th, cs },
|
||||
"{\"model_type\":\"qwen3_5\",\"quantization_config\":{\"quant_method\":\"paroquant\","
|
||||
"\"bits\":5,\"group_size\":128,\"krot\":2,\"format\":\"int5-bitplane\"}}");
|
||||
Checkpoint ck;
|
||||
REQUIRE_EQ(ck.open(dir), RAD_OK);
|
||||
CHECK(ck.imports() == Checkpoint::Import::ParoI5);
|
||||
Program prog;
|
||||
prog.name_map.push_back(map("w", RAD_MAP_COPY, { "q.weight" }));
|
||||
CkptWeight w;
|
||||
std::string why;
|
||||
REQUIRE_EQ(ckpt_resolve(ck, prog, "w", false, &w, &why), RAD_OK);
|
||||
REQUIRE(w.paro && w.imported);
|
||||
CHECK_EQ(w.rows, N);
|
||||
CHECK_EQ(w.cols, K);
|
||||
REQUIRE(rad_enc_valid(&w.enc));
|
||||
CHECK_EQ(w.enc.plane[0].dtype, (uint32_t)RAD_U5);
|
||||
CHECK_EQ(w.enc.plane[1].dtype, (uint32_t)RAD_F16);
|
||||
CHECK_EQ(w.enc.plane[2].dtype, (uint32_t)RAD_U8);
|
||||
std::vector<std::vector<uint8_t>> pl((size_t)w.enc.n_planes);
|
||||
for (int k = 0; k < w.enc.n_planes; ++k) {
|
||||
int64_t pr_ = 0, pc = 0;
|
||||
rad_enc_plane_dims(&w.enc.plane[k], w.rows, w.cols, &pr_, &pc);
|
||||
pl[(size_t)k].assign((size_t)(pr_ * rad_enc_row_bytes(w.enc.plane[k].dtype, pc)) + 8, 0);
|
||||
REQUIRE_EQ(ckpt_plane_rows(w, k, 0, pr_, pl[(size_t)k].data(), &why), RAD_OK);
|
||||
}
|
||||
const int64_t rb = rad_enc_row_bytes(RAD_U5, K);
|
||||
int bad = 0;
|
||||
for (int64_t n = 0; n < N; ++n) {
|
||||
for (int64_t k = 0; k < K; ++k)
|
||||
if ((int)rad_load_f32(pl[0].data() + n * rb, RAD_U5, k) != code[(size_t)(n * K + k)]) ++bad;
|
||||
for (int64_t g = 0; g < G; ++g) {
|
||||
if (pl[2][(size_t)(n * G + g)] != zero[(size_t)(n * G + g)]) ++bad;
|
||||
if (std::memcmp(pl[1].data() + (n * G + g) * 2, sc.bytes.data() + (g * N + n) * 2, 2) != 0) ++bad;
|
||||
}
|
||||
}
|
||||
CHECK_EQ(bad, 0);
|
||||
remove_checkpoint(dir);
|
||||
}
|
||||
|
||||
/* Z-LAB'S INT4 (`paroquant`, bits 4, no `format`): the int5 test's AWQ nibbles with no fifth-bit
|
||||
* planes, here as a fused gate|up -- two pieces, each with its own rotation, so the weight carries
|
||||
* two parts. Codes and zero points come out row by row per piece, and t.part ends each piece. */
|
||||
TEST(a_paroquant_int4_fused_weight_is_two_rotated_parts) {
|
||||
const int64_t N = 32, K = 128, G = K / 128;
|
||||
uint32_t rng = 4242;
|
||||
auto next = [&]() { rng = rng * 1664525u + 1013904223u; return rng >> 8; };
|
||||
static const int kOrder[8] = { 0, 2, 4, 6, 1, 3, 5, 7 };
|
||||
auto pack_awq = [&](const std::string& name, const std::vector<int>& v, int64_t rows) {
|
||||
T t{ name, "I32", { rows, N / 8 }, {} };
|
||||
t.bytes.assign((size_t)(rows * N / 8 * 4), 0);
|
||||
for (int64_t r = 0; r < rows; ++r)
|
||||
for (int64_t n = 0; n < N; ++n) {
|
||||
int j = 0;
|
||||
while (kOrder[j] != n % 8) ++j;
|
||||
uint32_t w;
|
||||
uint8_t* p = t.bytes.data() + (r * (N / 8) + n / 8) * 4;
|
||||
std::memcpy(&w, p, 4);
|
||||
w |= (uint32_t)(v[(size_t)(n * rows + r)] & 15) << (4 * j);
|
||||
std::memcpy(p, &w, 4);
|
||||
}
|
||||
return t;
|
||||
};
|
||||
auto f16s = [&](const std::string& name, std::vector<int64_t> shape, float v) {
|
||||
T t{ name, "F16", shape, {} };
|
||||
int64_t cnt = 1;
|
||||
for (int64_t d : shape) cnt *= d;
|
||||
const uint16_t h = rad_f32_to_f16(v);
|
||||
for (int64_t i = 0; i < cnt; ++i) { t.bytes.push_back((uint8_t)(h & 0xff)); t.bytes.push_back((uint8_t)(h >> 8)); }
|
||||
return t;
|
||||
};
|
||||
auto pairs = [&](const std::string& name, int shift) {
|
||||
T t{ name, "I16", { 1, K }, {} };
|
||||
for (int64_t i = 0; i < K; ++i) {
|
||||
const int16_t v = (int16_t)((i ^ shift) % 128);
|
||||
t.bytes.push_back((uint8_t)(v & 0xff));
|
||||
t.bytes.push_back((uint8_t)(v >> 8));
|
||||
}
|
||||
return t;
|
||||
};
|
||||
std::vector<T> ts;
|
||||
std::vector<int> code[2], zero[2];
|
||||
const char* names[2] = { "g", "u" };
|
||||
for (int p = 0; p < 2; ++p) {
|
||||
code[p].resize((size_t)(N * K));
|
||||
zero[p].resize((size_t)(N * G));
|
||||
for (auto& c : code[p]) c = (int)(next() % 16);
|
||||
for (auto& z : zero[p]) z = (int)(next() % 16);
|
||||
const std::string b = names[p];
|
||||
ts.push_back(pack_awq(b + ".qweight", code[p], K));
|
||||
ts.push_back(pack_awq(b + ".qzeros", zero[p], G));
|
||||
ts.push_back(f16s(b + ".scales", { G, N }, 0.01f * (float)(p + 1)));
|
||||
ts.push_back(pairs(b + ".pairs", p + 1)); // adjacent / next-but-one swaps
|
||||
ts.push_back(f16s(b + ".theta", { 1, K / 2 }, 0.2f));
|
||||
ts.push_back(f16s(b + ".channel_scales", { 1, K }, 1.25f));
|
||||
}
|
||||
const std::string dir = write_checkpoint(ts,
|
||||
"{\"model_type\":\"qwen3_5\",\"quantization_config\":{\"quant_method\":\"paroquant\","
|
||||
"\"bits\":4,\"group_size\":128,\"krot\":1}}");
|
||||
Checkpoint ck;
|
||||
REQUIRE_EQ(ck.open(dir), RAD_OK);
|
||||
CHECK(ck.imports() == Checkpoint::Import::ParoI4);
|
||||
Program prog;
|
||||
prog.name_map.push_back(map("gu", RAD_MAP_CONCAT, { "g.weight", "u.weight" }));
|
||||
CkptWeight w;
|
||||
std::string why;
|
||||
REQUIRE_EQ(ckpt_resolve(ck, prog, "gu", false, &w, &why), RAD_OK);
|
||||
REQUIRE(w.paro && w.imported);
|
||||
CHECK_EQ(w.rows, 2 * N);
|
||||
CHECK_EQ(w.enc.plane[0].dtype, (uint32_t)RAD_U4);
|
||||
CHECK_EQ(rad_enc_givens_parts(&w.enc), (int64_t)2);
|
||||
std::vector<std::vector<uint8_t>> pl((size_t)w.enc.n_planes);
|
||||
for (int k = 0; k < w.enc.n_planes; ++k) {
|
||||
int64_t pr_ = 0, pc = 0;
|
||||
rad_enc_plane_dims(&w.enc.plane[k], w.rows, w.cols, &pr_, &pc);
|
||||
pl[(size_t)k].assign((size_t)(pr_ * rad_enc_row_bytes(w.enc.plane[k].dtype, pc)) + 8, 0);
|
||||
REQUIRE_EQ(ckpt_plane_rows(w, k, 0, pr_, pl[(size_t)k].data(), &why), RAD_OK);
|
||||
}
|
||||
const int64_t rb = rad_enc_row_bytes(RAD_U4, K);
|
||||
int bad = 0;
|
||||
for (int p = 0; p < 2; ++p)
|
||||
for (int64_t n = 0; n < N; ++n) {
|
||||
const int64_t r = p * N + n;
|
||||
for (int64_t k = 0; k < K; ++k)
|
||||
if ((int)rad_load_f32(pl[0].data() + r * rb, RAD_U4, k) != code[p][(size_t)(n * K + k)]) ++bad;
|
||||
for (int64_t g = 0; g < G; ++g)
|
||||
if (pl[2][(size_t)(r * G + g)] != zero[p][(size_t)(n * G + g)]) ++bad;
|
||||
}
|
||||
CHECK_EQ(bad, 0);
|
||||
const int tpart = rad_enc_find(&w.enc, "t.part");
|
||||
REQUIRE(tpart > 0);
|
||||
const int32_t* ends = (const int32_t*)pl[(size_t)tpart].data();
|
||||
CHECK_EQ(ends[0], (int32_t)N);
|
||||
CHECK_EQ(ends[1], (int32_t)(2 * N));
|
||||
remove_checkpoint(dir);
|
||||
}
|
||||
|
||||
/* THE COMPOSITION IS PINNED BY ITS BYTES. The same pairs and angles the kernel library composed at load
|
||||
* before "t.dense" was a table give the same hash here, so a container that stores the table serves the
|
||||
* matrices the load-time composition did; and each matrix is orthogonal to f16 accuracy. */
|
||||
TEST(paro_dense_matrices_are_the_exact_composition) {
|
||||
const int64_t K = 256, P = 2, S = 3, PS = P * S, G = 128;
|
||||
std::vector<int32_t> pairs((size_t)(PS * K));
|
||||
std::vector<float> ang((size_t)(PS * K / 2));
|
||||
for (int64_t r = 0; r < PS; ++r) {
|
||||
const int64_t mul = 2 * r + 3, add = 17 * r + 5; /* odd multiplier: a permutation */
|
||||
for (int64_t c = 0; c < K; ++c)
|
||||
pairs[(size_t)(r * K + c)] = (int32_t)((c / G) * G + (c % G * mul + add) % G);
|
||||
for (int64_t j = 0; j < K / 2; ++j) ang[(size_t)(r * K / 2 + j)] = 0.37f * (float)(r + 1) - 0.011f * (float)j;
|
||||
}
|
||||
std::vector<uint16_t> t((size_t)(P * (K / G) * G * G));
|
||||
for (int64_t pt = 0; pt < P; ++pt)
|
||||
for (int64_t g = 0; g < K / G; ++g) {
|
||||
std::vector<int32_t> ia((size_t)(S * 64)), ib((size_t)(S * 64));
|
||||
std::vector<float> th((size_t)(S * 64));
|
||||
for (int64_t st = 0; st < S; ++st)
|
||||
for (int64_t j = 0; j < 64; ++j) {
|
||||
const int64_t row = pt * S + st, o = st * 64 + j;
|
||||
ia[(size_t)o] = pairs[(size_t)(row * K + g * G + 2 * j)] - (int32_t)(g * G);
|
||||
ib[(size_t)o] = pairs[(size_t)(row * K + g * G + 2 * j + 1)] - (int32_t)(g * G);
|
||||
th[(size_t)o] = ang[(size_t)(row * (K / 2) + g * 64 + j)];
|
||||
}
|
||||
paro_compose_dense(S, ia.data(), ib.data(), th.data(), t.data() + (pt * (K / G) + g) * G * G);
|
||||
}
|
||||
uint64_t h = 0xcbf29ce484222325ull;
|
||||
for (uint8_t b : std::vector<uint8_t>((const uint8_t*)t.data(), (const uint8_t*)t.data() + t.size() * 2))
|
||||
h = (h ^ b) * 1099511628211ull;
|
||||
CHECK_EQ(h, (uint64_t)0x8e72bd2bb541c6e6ull);
|
||||
double worst = 0;
|
||||
for (int64_t pg = 0; pg < P * K / G; ++pg)
|
||||
for (int64_t i = 0; i < G; ++i) {
|
||||
double nrm = 0;
|
||||
for (int64_t j = 0; j < G; ++j) {
|
||||
const double v = rad_f16_to_f32(t[(size_t)((pg * G + j) * G + i)]);
|
||||
nrm += v * v;
|
||||
}
|
||||
worst = std::max(worst, std::fabs(nrm - 1.0));
|
||||
}
|
||||
CHECK(worst < 2e-3);
|
||||
}
|
||||
@@ -433,6 +433,52 @@ TEST(a_givens_transform_with_channel_scales_inverts) {
|
||||
for (int k = 0; k < K; ++k) CHECK(std::fabs(out[k] - w[k]) < 1e-5f);
|
||||
}
|
||||
|
||||
TEST(a_givens_weight_with_two_parts_undoes_each_rows_own_rotation) {
|
||||
/* Two rows over 4 columns, each its own PART (row 0 ends part 0, row 1 ends part 1): the
|
||||
* tables stack part-major, t.scale has a row a part, and decoding row r reads its part's. */
|
||||
const int K = 4;
|
||||
const float w[2][4] = { { 1.0f, -2.0f, 0.5f, 3.0f }, { -0.25f, 1.5f, 2.0f, -1.0f } };
|
||||
const float dsc[2][4] = { { 2.0f, 0.5f, 1.0f, 4.0f }, { 1.0f, 3.0f, 0.25f, 2.0f } };
|
||||
const int32_t pairs[2][4] = { { 0, 1, 2, 3 }, { 0, 3, 1, 2 } }; /* one stage a part */
|
||||
const float ang[2][2] = { { 0.3f, -1.1f }, { 0.9f, 2.2f } };
|
||||
const int32_t end[2] = { 1, 2 };
|
||||
float vq[2][4];
|
||||
for (int r = 0; r < 2; ++r) {
|
||||
double x[4];
|
||||
for (int k = 0; k < K; ++k) x[k] = (double)w[r][k] * dsc[r][k];
|
||||
for (int p = 0; p < 2; ++p) {
|
||||
const int a = pairs[r][2 * p], b = pairs[r][2 * p + 1];
|
||||
const double c = std::cos((double)ang[r][p]), sn = std::sin((double)ang[r][p]);
|
||||
const double xa = x[a], xb = x[b];
|
||||
x[a] = xa * c + xb * sn;
|
||||
x[b] = xb * c - xa * sn;
|
||||
}
|
||||
for (int k = 0; k < K; ++k) vq[r][k] = (float)x[k];
|
||||
}
|
||||
RadEncoding e = rad_enc_plain(RAD_F32);
|
||||
rad_enc_copy_str(e.scheme, "affine");
|
||||
rad_enc_add_plane(&e, "scale", RAD_F32, 0, 0);
|
||||
rad_enc_add_table(&e, "t.pairs", RAD_I32, 2, 4);
|
||||
rad_enc_add_table(&e, "t.angle", RAD_F32, 2, 2);
|
||||
rad_enc_add_table(&e, "t.scale", RAD_F32, 2, 4);
|
||||
rad_enc_add_table(&e, "t.part", RAD_I32, 1, 2);
|
||||
rad_enc_copy_str(e.transform, "givens");
|
||||
REQUIRE(rad_enc_valid(&e));
|
||||
CHECK_EQ(rad_enc_givens_parts(&e), (int64_t)2);
|
||||
const float one = 1.0f;
|
||||
const void* planes[6] = { vq, &one, pairs, ang, dsc, end };
|
||||
EncodedView v;
|
||||
enc_view(e, 2, 4, planes, &v);
|
||||
float out[8];
|
||||
REQUIRE_EQ(enc_decode_rows(v, 0, 2, out), RAD_OK);
|
||||
for (int r = 0; r < 2; ++r)
|
||||
for (int k = 0; k < K; ++k) CHECK(std::fabs(out[r * K + k] - w[r][k]) < 1e-5f);
|
||||
/* a part table that does not stack to the pairs' rows is not well formed */
|
||||
RadEncoding bad = e;
|
||||
bad.plane[bad.n_planes - 1].extent[1] = 3;
|
||||
CHECK(!rad_enc_valid(&bad));
|
||||
}
|
||||
|
||||
TEST(the_core_declines_a_scheme_it_did_not_define) {
|
||||
RadEncoding e;
|
||||
rad_enc_clear(&e);
|
||||
|
||||
+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
|
||||
|
||||
@@ -166,6 +166,7 @@ const Hooks nHc { r4d_rad_layout_hc_e4m3, r4d_rad_relayout_hc_e4m3,
|
||||
const Hooks nMtp { r4d_rad_layout_mtp_e4m3, r4d_rad_relayout_mtp_e4m3,
|
||||
r4d_rad_unrelayout_mtp_e4m3 };
|
||||
const Hooks nW4 { r4d_rad_layout_w4, r4d_rad_relayout_w4, r4d_rad_unrelayout_w4 };
|
||||
const Hooks nW5 { r4d_rad_layout_w5, r4d_rad_relayout_w5, r4d_rad_unrelayout_w5 };
|
||||
const Hooks nW8a16 { r4d_rad_layout_w8a16, r4d_rad_relayout_w8a16, nullptr };
|
||||
const Hooks nW8a8 { r4d_rad_layout_w8a8, r4d_rad_relayout_w8a8, nullptr };
|
||||
const Hooks nW2 { r4d_rad_layout_w2a8, r4d_rad_relayout_w2a8, r4d_rad_unrelayout_w2a8 };
|
||||
@@ -284,6 +285,34 @@ TEST(dense_int4_symmetric_and_asymmetric) {
|
||||
}
|
||||
}
|
||||
|
||||
/* ParoQuant's int5: u5 codes as w4's nibble plane plus a fifth-bit plane, the (scale, zero) dwords
|
||||
* of the asymmetric w4 grid -- read back exactly; and a zero point that is not a 5-bit code refused,
|
||||
* since the int8 prefill kernel reads five bits of it where the f16 one reads the whole value. */
|
||||
TEST(dense_int5_asymmetric) {
|
||||
NEED_LIBQUANT();
|
||||
const int64_t N = 64, K = 256;
|
||||
const std::vector<float> w = weight(N, K, 19, 1, 128);
|
||||
Opts kp, g;
|
||||
kp.i("N", N).i("K", K).s("dtype", "w5a16");
|
||||
g.s("codes", "u5").s("zero", "u8").i("group", 128).s("scale", "f16");
|
||||
golden("w5", stored("w5", nW5, kp, "rtn", g, "blk.0.ffn_up.weight", N, K, w,
|
||||
{ { B, { "codes" } }, { BSCALE, { "scale", "zero" } } }),
|
||||
0xb0b1f9d187cbf267ull);
|
||||
const Quantised q = quantise(quantizer("rtn"), g, "x", N, K, w);
|
||||
REQUIRE_EQ(q.status, RAD_OK);
|
||||
std::vector<std::vector<uint8_t>> bufs;
|
||||
Selection s = select(q, N, K, { "scale", "zero" }, &bufs);
|
||||
std::memcpy(bufs[0].data(), q.planes[(size_t)s.sel[0]].data(), bufs[0].size());
|
||||
std::memcpy(bufs[1].data(), q.planes[(size_t)s.sel[1]].data(), bufs[1].size());
|
||||
bufs[1][3] = 32;
|
||||
RadLayout L{};
|
||||
REQUIRE_EQ(r4d_rad_layout_w5(kp.data(), kp.n(), BSCALE, &q.enc, s.sel.data(), s.t.data(),
|
||||
(int)s.t.size(), &L), RAD_OK);
|
||||
std::vector<uint8_t> dst((size_t)L.bytes);
|
||||
CHECK_EQ(r4d_rad_relayout_w5(kp.data(), kp.n(), BSCALE, &q.enc, s.sel.data(), s.t.data(),
|
||||
(int)s.t.size(), dst.data(), L.bytes), RAD_E_FORMAT);
|
||||
}
|
||||
|
||||
TEST(dense_int8_for_the_f16_and_the_int8_kernel) {
|
||||
NEED_LIBQUANT();
|
||||
const int64_t N = 48, K = 256;
|
||||
@@ -760,3 +789,77 @@ TEST(a_kernel_refuses_an_encoding_it_does_not_read) {
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
|
||||
/* ParoQuant's rotation pairs are stored as they are -- and refused at load when a stage is not a
|
||||
* pairing of its own group's 128 columns, which the rotate kernels would index out of bounds. */
|
||||
TEST(givens_pairs_are_stored_as_they_are_and_checked) {
|
||||
const int64_t K = 256, R = 2; /* two groups, two stages */
|
||||
RadEncoding e = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, 32);
|
||||
rad_enc_add_table(&e, "t.pairs", RAD_I32, R, K);
|
||||
const int sel[1] = { rad_enc_find(&e, "t.pairs") };
|
||||
std::vector<int32_t> pairs((size_t)(R * K));
|
||||
for (int64_t r = 0; r < R; ++r)
|
||||
for (int64_t c = 0; c < K; ++c) /* stage 0 reverses each group, stage 1 not */
|
||||
pairs[(size_t)(r * K + c)] = (int32_t)((c / 128) * 128 + (r ? c % 128 : 127 - c % 128));
|
||||
RadTensor t{};
|
||||
t.data = pairs.data();
|
||||
t.dtype = RAD_I32;
|
||||
t.rank = 2;
|
||||
t.shape[0] = R;
|
||||
t.shape[1] = K;
|
||||
t.stride[0] = K;
|
||||
t.stride[1] = 1;
|
||||
Opts kp;
|
||||
kp.i("n", K).i("stages", 1).i("parts", 2);
|
||||
RadLayout L{};
|
||||
CHECK_EQ(r4d_rad_layout_givens_pairs(kp.data(), kp.n(), 2, &e, sel, &t, 1, &L), RAD_E_UNSUPPORTED);
|
||||
REQUIRE_EQ(r4d_rad_layout_givens_pairs(kp.data(), kp.n(), 1, &e, sel, &t, 1, &L), RAD_OK);
|
||||
REQUIRE_EQ(L.bytes, (int64_t)(R * K * 4));
|
||||
std::vector<uint8_t> dst((size_t)L.bytes);
|
||||
REQUIRE_EQ(r4d_rad_relayout_givens_pairs(kp.data(), kp.n(), 1, &e, sel, &t, 1, dst.data(), L.bytes),
|
||||
RAD_OK);
|
||||
CHECK_EQ(std::memcmp(dst.data(), pairs.data(), dst.size()), 0);
|
||||
std::vector<int32_t> back((size_t)(R * K), -1);
|
||||
RadTensor tb = t;
|
||||
tb.data = back.data();
|
||||
REQUIRE_EQ(r4d_rad_unrelayout_givens_pairs(kp.data(), kp.n(), 1, &e, sel, dst.data(), L.bytes, &tb, 1),
|
||||
RAD_OK);
|
||||
CHECK(back == pairs);
|
||||
/* a pair leaving its group, and a column paired twice in a stage */
|
||||
std::vector<int32_t> out = pairs, dup = pairs;
|
||||
out[(size_t)(K + 5)] = 130;
|
||||
dup[3] = dup[4];
|
||||
for (std::vector<int32_t>* bad : { &out, &dup }) {
|
||||
t.data = bad->data();
|
||||
CHECK_EQ(r4d_rad_relayout_givens_pairs(kp.data(), kp.n(), 1, &e, sel, &t, 1, dst.data(), L.bytes),
|
||||
RAD_E_FORMAT);
|
||||
}
|
||||
}
|
||||
|
||||
/* ParoQuant's dense rotation matrices are the "t.dense" table rad-convert composed (checkpoint_test pins the
|
||||
* composition by hash): the layout takes the table as stored, byte for byte, and refuses a view of anything
|
||||
* else -- the pairs and angles, which it would have to compose, included. */
|
||||
TEST(givens_dense_layout_is_the_stored_table) {
|
||||
const int64_t K = 256, P = 2, G = 128;
|
||||
RadEncoding e = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, 32);
|
||||
rad_enc_add_table(&e, "t.pairs", RAD_I32, P * 3, K);
|
||||
rad_enc_add_table(&e, "t.angle", RAD_F32, P * 3, K / 2);
|
||||
rad_enc_add_table(&e, "t.dense", RAD_F16, P * K / G, G * G);
|
||||
std::vector<uint16_t> dense((size_t)(P * K / G * G * G));
|
||||
for (size_t i = 0; i < dense.size(); ++i) dense[i] = (uint16_t)(i * 2654435761u >> 7);
|
||||
RadTensor t{};
|
||||
t.data = dense.data(); t.dtype = RAD_F16; t.rank = 2;
|
||||
t.shape[0] = P * K / G; t.shape[1] = G * G; t.stride[0] = G * G; t.stride[1] = 1;
|
||||
Opts kp;
|
||||
kp.i("n", K).i("parts", P);
|
||||
const int sel[1] = { rad_enc_find(&e, "t.dense") };
|
||||
RadLayout L{};
|
||||
REQUIRE_EQ(r4d_rad_layout_gdense(kp.data(), kp.n(), 1, &e, sel, &t, 1, &L), RAD_OK);
|
||||
REQUIRE_EQ(L.bytes, (int64_t)(P * K * G * 2));
|
||||
std::vector<uint8_t> dst((size_t)L.bytes);
|
||||
REQUIRE_EQ(r4d_rad_relayout_gdense(kp.data(), kp.n(), 1, &e, sel, &t, 1, dst.data(), L.bytes), RAD_OK);
|
||||
CHECK_EQ(std::memcmp(dst.data(), dense.data(), dst.size()), 0);
|
||||
const int sel2[2] = { rad_enc_find(&e, "t.pairs"), rad_enc_find(&e, "t.angle") };
|
||||
RadTensor two[2] = { t, t };
|
||||
CHECK(r4d_rad_layout_gdense(kp.data(), kp.n(), 1, &e, sel2, two, 2, &L) != RAD_OK);
|
||||
}
|
||||
Reference in new issue
Block a user