Archived
983 lines
50 KiB
C++
983 lines
50 KiB
C++
/* relayout_parity_test.cpp -- a kernel's relayout of libquant's planes is the stored form the
|
|
* kernel reads, pinned byte for byte.
|
|
*
|
|
* A container holds a quantiser's canonical planes; the kernel only arranges them at load
|
|
* (spec.md §4.3). For every stored format, libquant's planes passed through the kernel's relayout
|
|
* are pinned to a recorded FNV-1a hash of the load-time bytes. A deliberate kernel packing change
|
|
* updates those hashes, not the container: its canonical codes must still round-trip exactly.
|
|
* Fragment-tiled MoE I4 also checks every nibble's address independently of the packing hooks,
|
|
* so updating a golden cannot hide a matching forward/inverse permutation error.
|
|
*
|
|
* libr4d needs a HIP toolchain to build but its layouts do not, so their host sources are compiled
|
|
* into this test (tests/CMakeLists.txt). The n-gram table's hook is libavx's, from the plugin. One
|
|
* test here therefore reaches back into tools/opshapes.cpp: the fallback table sizes the packed
|
|
* weights these hooks own, and only this file can hold both sides of that claim.
|
|
*/
|
|
#include "quant_fixture.h"
|
|
#include "r4d_plugin.h"
|
|
#include "opshapes.h"
|
|
|
|
#include <array>
|
|
#include <dlfcn.h>
|
|
#include <unistd.h>
|
|
|
|
using namespace qfix;
|
|
|
|
namespace {
|
|
|
|
/* One kernel family's hooks. */
|
|
struct Hooks {
|
|
RadLayoutFn layout = nullptr;
|
|
RadRelayoutFn relayout = nullptr;
|
|
RadUnrelayoutFn unrelayout = nullptr;
|
|
};
|
|
|
|
/* Two hashes into one, order-sensitive (XOR would cancel two equal planes to zero). */
|
|
uint64_t chain(uint64_t a, uint64_t b) {
|
|
for (int i = 0; i < 8; ++i) a = (a ^ ((b >> (8 * i)) & 0xFF)) * 0x100000001b3ull;
|
|
return a;
|
|
}
|
|
|
|
/* The planes `roles` names, as the loader hands them to a relayout: dense [rows, cols] in each
|
|
* plane's own elements. `bufs`, when given, replaces the data (the inverse's destinations). */
|
|
struct Selection {
|
|
std::vector<int> sel;
|
|
std::vector<RadTensor> t;
|
|
};
|
|
Selection select(const Quantised& q, int64_t R, int64_t C, const std::vector<const char*>& roles,
|
|
std::vector<std::vector<uint8_t>>* bufs = nullptr) {
|
|
Selection s;
|
|
for (const char* role : roles) {
|
|
const int i = rad_enc_find(&q.enc, role);
|
|
s.sel.push_back(i);
|
|
RadTensor t{};
|
|
if (i < 0) { s.t.push_back(t); continue; }
|
|
const RadEncPlane& p = q.enc.plane[i];
|
|
int64_t pr = 0, pc = 0;
|
|
rad_enc_plane_dims(&p, R, C, &pr, &pc);
|
|
if (bufs) bufs->emplace_back(q.planes[(size_t)i].size(), 0);
|
|
t.data = bufs ? bufs->back().data() : const_cast<uint8_t*>(q.planes[(size_t)i].data());
|
|
t.dtype = p.dtype;
|
|
t.rank = 2;
|
|
t.shape[0] = pr;
|
|
t.shape[1] = pc;
|
|
t.stride[0] = pc;
|
|
t.stride[1] = 1;
|
|
s.t.push_back(t);
|
|
}
|
|
return s;
|
|
}
|
|
|
|
/* One operand of one weight: what to compare. */
|
|
struct Op {
|
|
int operand;
|
|
std::vector<const char*> roles;
|
|
};
|
|
|
|
/* The check: quantise `w` with libquant, store each operand through the kernel's hooks, invert.
|
|
* Returns the hash of everything stored, in operand order. */
|
|
uint64_t stored(const char* label, const Hooks& nf, Opts kp, const char* quant,
|
|
Opts qo, const char* wname, int64_t R, int64_t C, const std::vector<float>& w,
|
|
const std::vector<Op>& ops, Quantised* keep = nullptr) {
|
|
uint64_t h = 0xcbf29ce484222325ull;
|
|
const Quantised q = quantise(quantizer(quant), qo, wname, R, C, w);
|
|
if (q.status != RAD_OK) {
|
|
CHECK_EQ(q.status, RAD_OK);
|
|
return 0;
|
|
}
|
|
for (const Op& op : ops) {
|
|
Selection s = select(q, R, C, op.roles);
|
|
RadLayout L{};
|
|
const int lrc = nf.layout(kp.data(), kp.n(), op.operand, &q.enc, s.sel.data(), s.t.data(),
|
|
(int)s.t.size(), &L);
|
|
std::vector<uint8_t> got;
|
|
if (lrc == RAD_E_UNSUPPORTED) {
|
|
/* stored as it is: the one plane, untouched */
|
|
CHECK_EQ(op.roles.size(), (size_t)1);
|
|
got = q.planes[(size_t)s.sel[0]];
|
|
} else {
|
|
CHECK_EQ(lrc, RAD_OK);
|
|
CHECK(nf.relayout != nullptr);
|
|
if (lrc != RAD_OK || !nf.relayout) {
|
|
std::fprintf(stderr, " %s operand %d: layout %d\n", label, op.operand, lrc);
|
|
return 0;
|
|
}
|
|
got.assign((size_t)L.bytes, 0xA5);
|
|
CHECK_EQ(nf.relayout(kp.data(), kp.n(), op.operand, &q.enc, s.sel.data(), s.t.data(),
|
|
(int)s.t.size(), got.data(), L.bytes), RAD_OK);
|
|
}
|
|
if (lrc == RAD_OK && L.tag && std::strstr(L.tag, ".w.t16k64.g128")) {
|
|
/* Kernel meaning, not the relayout algorithm: lane%16 owns output column n, the
|
|
* lane's half owns eight K values, byte j holds k+j low and k+j+4 high. */
|
|
CHECK_EQ(got.size(), (size_t)(R * C / 2));
|
|
if (got.size() != (size_t)(R * C / 2)) return 0;
|
|
const auto& codes = q.planes[(size_t)s.sel[0]];
|
|
int64_t bad = 0;
|
|
for (int64_t n = 0; n < R; ++n)
|
|
for (int64_t k = 0; k < C; ++k) {
|
|
const int64_t lane = n % 16 + ((k % 16) / 8) * 16;
|
|
const size_t at = (size_t)(((n / 16 * (C / 64) + k / 64) * 32 + lane)
|
|
* 16 + (k % 64 / 16) * 4 + k % 4);
|
|
const int a = (got[at] >> ((k % 8 / 4) * 4)) & 15;
|
|
const int b = (codes[(size_t)(n * (C / 2) + k / 2)] >> ((k % 2) * 4)) & 15;
|
|
bad += a != b;
|
|
}
|
|
CHECK_EQ(bad, 0LL);
|
|
}
|
|
h = fnv(got, h);
|
|
|
|
/* ...and back, where the kernel can be read back at all */
|
|
if (lrc == RAD_OK && nf.unrelayout) {
|
|
std::vector<std::vector<uint8_t>> bufs;
|
|
Selection back = select(q, R, C, op.roles, &bufs);
|
|
CHECK_EQ(nf.unrelayout(kp.data(), kp.n(), op.operand, &q.enc, back.sel.data(),
|
|
got.data(), (int64_t)got.size(), back.t.data(),
|
|
(int)back.t.size()), RAD_OK);
|
|
for (size_t i = 0; i < bufs.size(); ++i)
|
|
CHECK(bufs[i] == q.planes[(size_t)s.sel[i]]);
|
|
}
|
|
}
|
|
if (keep) *keep = q;
|
|
return h;
|
|
}
|
|
|
|
/* A golden of the kernel's load-time bytes, not the container's canonical planes. */
|
|
void golden(const char* label, uint64_t got, uint64_t want) {
|
|
if (got != want)
|
|
std::fprintf(stderr, " %s: stored hash 0x%016llxull, recorded 0x%016llxull\n", label,
|
|
(unsigned long long)got, (unsigned long long)want);
|
|
CHECK_EQ(got, want);
|
|
}
|
|
|
|
/* The edges every scale rule handles: a zero block, an all-positive one and an all-negative one
|
|
* (an asymmetric grid widens each to include zero), at blocks (0, 0), (1, 1) and (2, 0) of a
|
|
* [br x bc] grid, clipped to the weight. */
|
|
void edges(std::vector<float>& w, int64_t R, int64_t C, int64_t br, int64_t bc) {
|
|
auto block = [&](int64_t bi, int64_t bj, int sign) {
|
|
const int64_t r0 = bi * br, c0 = (bj * bc < C) ? bj * bc : 0;
|
|
for (int64_t r = r0; r < r0 + br && r < R; ++r)
|
|
for (int64_t c = c0; c < c0 + bc && c < C; ++c) {
|
|
float& v = w[(size_t)(r * C + c)];
|
|
v = sign == 0 ? 0.0f : (float)sign * std::fabs(v);
|
|
}
|
|
};
|
|
block(0, 0, 0);
|
|
block(1, 1, 1);
|
|
block(2, 0, -1);
|
|
}
|
|
|
|
std::vector<float> weight(int64_t R, int64_t C, uint64_t seed, int64_t br, int64_t bc) {
|
|
std::vector<float> w = draw(R * C, seed, 20.0f);
|
|
edges(w, R, C, br, bc);
|
|
return w;
|
|
}
|
|
|
|
const Hooks nHc { r4d_rad_layout_hc_e4m3, r4d_rad_relayout_hc_e4m3,
|
|
r4d_rad_unrelayout_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 };
|
|
const Hooks nMx { r4d_rad_layout_mxfp4, r4d_rad_relayout_mxfp4, nullptr };
|
|
const Hooks nMoeW4 { r4d_rad_layout_moe_w4, r4d_rad_relayout_moe_w4,
|
|
r4d_rad_unrelayout_moe_w4 };
|
|
const Hooks nMoeW4Mod4 { r4d_rad_layout_moe_w4_mod4, r4d_rad_relayout_moe_w4_mod4,
|
|
r4d_rad_unrelayout_moe_w4_mod4 };
|
|
|
|
/* gemm_nt_q's operands and the other ops' weight slots, as the kernels number them */
|
|
enum { B = 2, BSCALE = 3, BREF = 6, LM_W = 1, HC_DOWN = 2, HC_UP = 3, MTP_FCH = 4, MTP_FCE = 5 };
|
|
|
|
Opts fp8_grid() {
|
|
Opts o;
|
|
o.s("codes", "fp8_e4m3").s("block", "128x128").s("scale", "bf16");
|
|
return o;
|
|
}
|
|
|
|
} /* namespace */
|
|
|
|
TEST(a_convolution_weight_is_stored_tap_major_and_reads_back) {
|
|
for (const auto& [co, ci, k] : std::vector<std::array<long long, 3>>{ { 3, 5, 3 }, { 16, 64, 3 },
|
|
{ 8, 40, 1 }, { 4, 7, 2 } }) {
|
|
Opts kp;
|
|
kp.i("cout", co).i("cin", ci).i("k", k);
|
|
const long long kk = k * k;
|
|
std::vector<uint16_t> w((size_t)(co * ci * kk));
|
|
for (size_t i = 0; i < w.size(); ++i) w[i] = (uint16_t)(i * 40503u + 7);
|
|
RadTensor t{};
|
|
t.data = w.data();
|
|
t.dtype = RAD_BF16;
|
|
t.rank = 2;
|
|
t.shape[0] = co;
|
|
t.shape[1] = ci * kk;
|
|
t.stride[0] = ci * kk;
|
|
t.stride[1] = 1;
|
|
const RadEncoding enc = rad_enc_plain(RAD_BF16);
|
|
const int sel = 0;
|
|
RadLayout L{};
|
|
REQUIRE_EQ(r4d_rad_layout_conv_tap(kp.data(), kp.n(), 1, &enc, &sel, &t, 1, &L), RAD_OK);
|
|
std::vector<uint16_t> got((size_t)(L.bytes / 2), 0xA5A5);
|
|
REQUIRE_EQ(r4d_rad_relayout_conv_tap(kp.data(), kp.n(), 1, &enc, &sel, &t, 1, got.data(), L.bytes), RAD_OK);
|
|
int64_t bad = 0;
|
|
for (long long o = 0; o < co; ++o)
|
|
for (long long c = 0; c < ci; ++c)
|
|
for (long long tp = 0; tp < kk; ++tp)
|
|
bad += got[(size_t)((o * kk + tp) * ci + c)] != w[(size_t)((o * ci + c) * kk + tp)];
|
|
CHECK_EQ(bad, (int64_t)0);
|
|
std::vector<uint16_t> back(w.size(), 0);
|
|
RadTensor tb = t;
|
|
tb.data = back.data();
|
|
REQUIRE_EQ(r4d_rad_unrelayout_conv_tap(kp.data(), kp.n(), 1, &enc, &sel, got.data(), L.bytes, &tb, 1), RAD_OK);
|
|
CHECK(back == w);
|
|
}
|
|
}
|
|
|
|
TEST(the_fp8_hyper_connection_and_mtp_rows) {
|
|
NEED_LIBQUANT();
|
|
const int64_t n = 128, hc = 4, lr = 256;
|
|
Opts kp;
|
|
kp.i("n", n).i("hc", hc).i("lowrank", lr);
|
|
{
|
|
/* mix_down: [lowrank, hc*n], a scale per 128 columns */
|
|
const std::vector<float> w = weight(lr, hc * n, 14, 1, 128);
|
|
Opts g;
|
|
g.s("codes", "fp8_e4m3").i("group", 128).s("scale", "f32");
|
|
golden("hc down", stored("hc down", nHc, kp, "rtn", g, "blk.0.hc_down.weight", lr,
|
|
hc * n, w, { { HC_DOWN, { "codes", "scale" } } }),
|
|
0x62a46db4dbf831aeull);
|
|
}
|
|
{
|
|
/* mix_up: [hc*n, lowrank], a scale per quarter row */
|
|
const std::vector<float> w = weight(hc * n, lr, 15, 1, lr / 4);
|
|
Opts g;
|
|
g.s("codes", "fp8_e4m3").i("group", lr / 4).s("scale", "f32");
|
|
golden("hc up", stored("hc up", nHc, kp, "rtn", g, "blk.0.hc_up.weight", hc * n, lr,
|
|
w, { { HC_UP, { "codes", "scale" } } }),
|
|
0x764b1ae9ee02e27full);
|
|
}
|
|
{
|
|
const int64_t m = 256;
|
|
Opts mp;
|
|
mp.i("n", m);
|
|
const std::vector<float> wh = weight(m, m, 16, 1, 128);
|
|
const std::vector<float> we = weight(m, m, 116, 1, 128);
|
|
Opts g;
|
|
g.s("codes", "fp8_e4m3").i("group", 128).s("scale", "f32");
|
|
golden("mtp", chain(stored("mtp fc_hidden", nMtp, mp, "rtn", g,
|
|
"mtp.fc_hidden.weight", m, m, wh,
|
|
{ { MTP_FCH, { "codes", "scale" } } }),
|
|
stored("mtp fc_embedding", nMtp, mp, "rtn", g,
|
|
"mtp.fc_embd.weight", m, m, we,
|
|
{ { MTP_FCE, { "codes", "scale" } } })),
|
|
0x5fb1a57420d4471dull);
|
|
}
|
|
}
|
|
|
|
TEST(dense_int4_symmetric_and_asymmetric) {
|
|
NEED_LIBQUANT();
|
|
const int64_t N = 64, K = 256;
|
|
const std::vector<float> w = weight(N, K, 17, 1, 128);
|
|
{
|
|
Opts kp, g;
|
|
kp.i("N", N).i("K", K).s("dtype", "w4a8");
|
|
g.s("codes", "i4").i("group", 128).s("scale", "f16");
|
|
golden("w4", stored("w4", nW4, kp, "rtn", g, "blk.0.ffn_up.weight", N, K, w,
|
|
{ { B, { "codes" } }, { BSCALE, { "scale" } } }),
|
|
0x289633b39d08e864ull);
|
|
}
|
|
for (const char* z : { "u4", "u8" }) {
|
|
Opts kp, g;
|
|
kp.i("N", N).i("K", K).s("dtype", "w4a8_asym");
|
|
g.s("codes", "u4").s("zero", z).i("group", 128).s("scale", "f16");
|
|
golden("w4 asym", stored("w4 asym", nW4, kp, "rtn", g, "blk.0.ffn_up.weight", N, K, w,
|
|
{ { B, { "codes" } }, { BSCALE, { "scale", "zero" } } }),
|
|
0x5854c2996f14a80eull);
|
|
}
|
|
}
|
|
|
|
/* ParoQuant's int5: u5 codes as w4's nibble plane plus a fifth-bit plane, with the asymmetric w4
|
|
* grid's (scale, zero) dwords, read back exactly. A zero point that is not a 5-bit code is
|
|
* refused, since the int8 prefill kernel reads five bits of it where the f16 one reads all of it. */
|
|
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;
|
|
const std::vector<float> w = weight(N, K, 18, 1, 128);
|
|
Opts kp, g;
|
|
kp.i("N", N).i("K", K);
|
|
g.s("codes", "i8").i("group", 128).s("scale", "f16").s("clamp", "sym");
|
|
golden("w8a16", stored("w8a16", nW8a16, kp, "rtn", g, "blk.0.ffn_up.weight", N, K, w,
|
|
{ { B, { "codes" } }, { BSCALE, { "scale" } } }),
|
|
0x61e5176f24b6d72bull);
|
|
golden("w8a8", stored("w8a8", nW8a8, kp, "rtn", g, "blk.0.ffn_up.weight", N, K, w,
|
|
{ { B, { "codes" } }, { BSCALE, { "scale" } } }),
|
|
0x7c2b502335fe84bbull);
|
|
}
|
|
|
|
TEST(two_bit_asymmetric) {
|
|
NEED_LIBQUANT();
|
|
const int64_t N = 32, K = 256;
|
|
const std::vector<float> w = weight(N, K, 19, 1, 128);
|
|
Opts kp, g;
|
|
kp.i("N", N).i("K", K);
|
|
g.s("codes", "u2").s("zero", "u8").i("group", 128).s("scale", "f16");
|
|
golden("w2a8", stored("w2a8", nW2, kp, "rtn", g, "output.weight", N, K, w,
|
|
{ { B, { "codes" } }, { BSCALE, { "scale", "zero" } } }),
|
|
0x3bfb973c838751c2ull);
|
|
}
|
|
|
|
TEST(mxfp4_codes_exponents_and_the_row_reference) {
|
|
NEED_LIBQUANT();
|
|
const int64_t N = 32, K = 256;
|
|
const std::vector<float> w = weight(N, K, 20, 1, 32);
|
|
Opts kp, g;
|
|
kp.i("N", N).i("K", K);
|
|
g.s("codes", "fp4_e2m1").i("group", 32).s("scale", "e8m0");
|
|
golden("mxfp4", stored("mxfp4", nMx, kp, "rtn", g, "blk.0.ffn_up.weight", N, K, w,
|
|
{ { B, { "codes" } }, { BSCALE, { "scale" } }, { BREF, { "scale" } } }),
|
|
0xc35d26b146836d77ull);
|
|
}
|
|
|
|
/* tools/opshapes.cpp sizes a packed weight from the kernel's stored form, because no schema states
|
|
* an extent and libref declines every grid with a zero point (ref_planes.h joins scale and zero
|
|
* into one operand a description cannot state). A stale entry is not a failed check: rad-tune
|
|
* allocates what the table says and hands the kernel the short buffer, which is a read past the
|
|
* allocation on the card, not a refusal. So the table's byte total and the hook's `bytes` must
|
|
* agree. The table's shapes are the tool's own view -- mxfp4's exponents are [K/32][N] there and
|
|
* [N][K/32] in the store -- so the totals, not the shapes, are the contract. */
|
|
TEST(the_tool_table_sizes_packed_weights_as_the_kernel_stores_them) {
|
|
NEED_LIBQUANT();
|
|
const int64_t N = 128, K = 512, M = 8;
|
|
auto check = [&](const char* dtype, int64_t group, const Hooks& hk,
|
|
const std::vector<const char*>& bscale_roles, Opts qo) {
|
|
const std::vector<float> w = weight(N, K, 21, 1, group);
|
|
const Quantised q = quantise(quantizer("rtn"), qo, "output.weight", N, K, w);
|
|
if (q.status != RAD_OK) {
|
|
CHECK_EQ(q.status, RAD_OK);
|
|
return;
|
|
}
|
|
Opts kp;
|
|
kp.i("N", N).i("K", K);
|
|
rad::Geometry g;
|
|
g.set_i("M", M); g.set_i("N", N); g.set_i("K", K); g.set_i("group", group);
|
|
g.set_s("dtype", dtype);
|
|
std::vector<rad::OpdShape> tab;
|
|
if (rad::rad_operand_shapes("gemm_nt_q", g, 7, M, &tab) != RAD_OK) {
|
|
std::fprintf(stderr, " %s: the table refuses the geometry\n", dtype);
|
|
CHECK(false);
|
|
return;
|
|
}
|
|
CHECK_EQ((int)tab.size(), 7);
|
|
struct P { int operand; size_t opd; std::vector<const char*> roles; };
|
|
const std::vector<P> ops = { { B, 2, { "codes" } }, { BSCALE, 3, bscale_roles } };
|
|
for (const P& o : ops) {
|
|
const Selection s = select(q, N, K, o.roles);
|
|
RadLayout L{};
|
|
CHECK_EQ(hk.layout(kp.data(), kp.n(), o.operand, &q.enc, s.sel.data(), s.t.data(),
|
|
(int)s.t.size(), &L), RAD_OK);
|
|
const rad::OpdShape& t = tab[o.opd];
|
|
const int64_t got = t.absent ? 0 : rad_dtype_bytes(t.dtype, rad::rad_numel(t.shape));
|
|
if (got != (int64_t)L.bytes)
|
|
std::fprintf(stderr, " %s operand %d: the table sizes %lld byte(s), the kernel "
|
|
"stores %lld\n", dtype, o.operand, (long long)got,
|
|
(long long)L.bytes);
|
|
CHECK_EQ(got, (int64_t)L.bytes);
|
|
}
|
|
};
|
|
{
|
|
Opts qo;
|
|
qo.s("codes", "u2").s("zero", "u8").i("group", 128).s("scale", "f16");
|
|
check("w2a8", 128, nW2, { "scale", "zero" }, qo);
|
|
}
|
|
{
|
|
Opts qo;
|
|
qo.s("codes", "fp4_e2m1").i("group", 32).s("scale", "e8m0");
|
|
check("mxfp4a8", 32, nMx, { "scale" }, qo);
|
|
}
|
|
/* The guards are the kernel's own: K a whole number of 128-column blocks
|
|
* (r4d_layout.cpp refuses a weight whose K is not), N a whole number of 16-row tiles (the
|
|
* row's constraint), and the group the stored form has. A geometry the kernel would refuse has
|
|
* to be refused here too, or rad-tune would size buffers for a launch that never happens. */
|
|
auto refuses = [&](int64_t n, int64_t k, int64_t group) {
|
|
rad::Geometry g;
|
|
g.set_i("M", M); g.set_i("N", n); g.set_i("K", k); g.set_i("group", group);
|
|
g.set_s("dtype", "w2a8");
|
|
std::vector<rad::OpdShape> tab;
|
|
CHECK_EQ(rad::rad_operand_shapes("gemm_nt_q", g, 7, M, &tab), RAD_E_UNSUPPORTED);
|
|
};
|
|
refuses(128, 500, 128); /* K leaves a partial block */
|
|
refuses(120, 512, 128); /* N leaves a partial tile */
|
|
refuses(128, 512, 64); /* a group the stored form does not have */
|
|
/* The shape the DFlash2 draft head tunes at on a two-rank serve, which the kernel takes. */
|
|
{
|
|
rad::Geometry g;
|
|
g.set_i("M", M); g.set_i("N", 124160); g.set_i("K", 2560); g.set_i("group", 128);
|
|
g.set_s("dtype", "w2a8");
|
|
std::vector<rad::OpdShape> tab;
|
|
CHECK_EQ(rad::rad_operand_shapes("gemm_nt_q", g, 7, M, &tab), RAD_OK);
|
|
CHECK_EQ((int)tab.size(), 7);
|
|
}
|
|
}
|
|
|
|
TEST(moe_int4_experts_plain_and_rotated_both_classes) {
|
|
NEED_LIBQUANT();
|
|
const int64_t N = 64, K = 256, N1 = 48, K1 = 128;
|
|
const std::vector<float> w = weight(N, K, 21, 1, 128);
|
|
const std::vector<float> w1 = weight(N1, K1, 22, 1, 128);
|
|
for (const char* dt : { "w4a8", "w4a8h" }) {
|
|
const bool rot = std::strcmp(dt, "w4a8h") == 0;
|
|
Opts kp, g;
|
|
kp.i("N", N).i("K", K).i("N_odd", N1).i("K_odd", K1).s("dtype", dt);
|
|
g.s("codes", "i4").i("group", 128).s("scale", "bf16");
|
|
if (rot) g.s("transform", "fwht128");
|
|
const uint64_t h = chain(
|
|
stored(dt, nMoeW4, kp, "rtn", g, "blk.0.ffn_down_exps.0.weight", N, K, w,
|
|
{ { B, { "codes" } }, { BSCALE, { "scale" } } }),
|
|
stored(dt, nMoeW4, kp, "rtn", g, "blk.0.ffn_down_exps.1.weight", N1, K1, w1,
|
|
{ { 6, { "codes" } }, { 7, { "scale" } } }));
|
|
golden(dt, h, rot ? 0x4acff22a616a9476ull : 0x565ae6cfe9b94a6aull);
|
|
}
|
|
}
|
|
|
|
TEST(moe_codebook_experts_both_classes) {
|
|
NEED_LIBQUANT();
|
|
/* w4nla8h: the rotated plane whose codes index libquant's w4nl table. The codes view takes the
|
|
* table beside the codes, the relayout checks it against the kernel's and stores the codes
|
|
* exactly as w4a8h stores its own, and the inverse gives back both planes. */
|
|
const int64_t N = 64, K = 256, N1 = 48, K1 = 128;
|
|
const std::vector<float> w = weight(N, K, 21, 1, 128);
|
|
const std::vector<float> w1 = weight(N1, K1, 22, 1, 128);
|
|
Opts kp, g;
|
|
kp.i("N", N).i("K", K).i("N_odd", N1).i("K_odd", K1).s("dtype", "w4nla8h");
|
|
g.s("table", "w4nl").i("group", 128).s("scale", "bf16").s("transform", "fwht128")
|
|
.s("rule", "search");
|
|
const uint64_t h = chain(
|
|
stored("w4nla8h", nMoeW4, kp, "rtn", g, "blk.0.ffn_down_exps.0.weight", N, K, w,
|
|
{ { B, { "codes", "table" } }, { BSCALE, { "scale" } } }),
|
|
stored("w4nla8h", nMoeW4, kp, "rtn", g, "blk.0.ffn_down_exps.1.weight", N1, K1, w1,
|
|
{ { 6, { "codes", "table" } }, { 7, { "scale" } } }));
|
|
golden("w4nla8h", h, 0x874c1a30937ee1d3ull);
|
|
}
|
|
|
|
TEST(moe_codebook_experts_at_e4m3_scales_a_64) {
|
|
NEED_LIBQUANT();
|
|
/* w4nl64a8h: the codebook's codes keep their row-major nibble order, and an E4M3 scale per
|
|
* (row, 64 of K) under one fixed f32. The scale view selects both; the relayout checks the
|
|
* second is the kernel's constant and stores the E4M3 plane as is; the inverse gives back
|
|
* both. A second level of any other value is refused when its bytes are relaid. */
|
|
const int64_t N = 64, K = 256, N1 = 48, K1 = 128;
|
|
const std::vector<float> w = weight(N, K, 21, 1, 128);
|
|
const std::vector<float> w1 = weight(N1, K1, 22, 1, 128);
|
|
Opts kp, g;
|
|
kp.i("N", N).i("K", K).i("N_odd", N1).i("K_odd", K1).s("dtype", "w4nl64a8h").i("group", 128);
|
|
g.s("table", "w4nl").i("group", 64).s("scale", "fp8_e4m3").s("scale2", "f32")
|
|
.s("block2", "*x*").f("scale2_value", 0x1p-13).s("transform", "fwht128").s("rule", "search");
|
|
const uint64_t h = chain(
|
|
stored("w4nl64a8h", nMoeW4, kp, "rtn", g, "blk.0.ffn_down_exps.0.weight", N, K, w,
|
|
{ { B, { "codes", "table" } }, { BSCALE, { "scale", "scale.1" } } }),
|
|
stored("w4nl64a8h", nMoeW4, kp, "rtn", g, "blk.0.ffn_down_exps.1.weight", N1, K1, w1,
|
|
{ { 6, { "codes", "table" } }, { 7, { "scale", "scale.1" } } }));
|
|
golden("w4nl64a8h", h, 0x528e273bdee3be1dull);
|
|
|
|
Opts g2;
|
|
g2.s("table", "w4nl").i("group", 64).s("scale", "fp8_e4m3").s("scale2", "f32")
|
|
.s("block2", "*x*").f("scale2_value", 0x1p-12).s("transform", "fwht128").s("rule", "search");
|
|
const Quantised q2 = quantise(quantizer("rtn"), g2, "blk.0.ffn_down_exps.0.weight", N, K, w);
|
|
REQUIRE_EQ(q2.status, RAD_OK);
|
|
Selection s = select(q2, N, K, { "scale", "scale.1" });
|
|
RadLayout L{};
|
|
REQUIRE_EQ(r4d_rad_layout_moe_w4(kp.data(), kp.n(), BSCALE, &q2.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_moe_w4(kp.data(), kp.n(), BSCALE, &q2.enc, s.sel.data(), s.t.data(),
|
|
(int)s.t.size(), dst.data(), L.bytes), RAD_E_DTYPE);
|
|
/* and the bf16-scaled codebook's encoding is not this dtype's */
|
|
Opts g3;
|
|
g3.s("table", "w4nl").i("group", 128).s("scale", "bf16").s("transform", "fwht128");
|
|
const Quantised q3 = quantise(quantizer("rtn"), g3, "blk.0.ffn_down_exps.0.weight", N, K, w);
|
|
REQUIRE_EQ(q3.status, RAD_OK);
|
|
Selection s3 = select(q3, N, K, { "codes", "table" });
|
|
CHECK_EQ(r4d_rad_layout_moe_w4(kp.data(), kp.n(), B, &q3.enc, s3.sel.data(), s3.t.data(),
|
|
(int)s3.t.size(), &L), RAD_E_DTYPE);
|
|
}
|
|
|
|
TEST(moe_codebook_experts_at_e4m3_scales_a_32) {
|
|
NEED_LIBQUANT();
|
|
/* w4nl32a8h: the 64-wide form's planes at a scale per 32 of K, four E4M3 bytes per 128. */
|
|
const int64_t N = 64, K = 256, N1 = 48, K1 = 128;
|
|
const std::vector<float> w = weight(N, K, 21, 1, 128);
|
|
const std::vector<float> w1 = weight(N1, K1, 22, 1, 128);
|
|
Opts kp, g;
|
|
kp.i("N", N).i("K", K).i("N_odd", N1).i("K_odd", K1).s("dtype", "w4nl32a8h").i("group", 128);
|
|
g.s("table", "w4nl").i("group", 32).s("scale", "fp8_e4m3").s("scale2", "f32")
|
|
.s("block2", "*x*").f("scale2_value", 0x1p-13).s("transform", "fwht128").s("rule", "search");
|
|
const uint64_t h = chain(
|
|
stored("w4nl32a8h", nMoeW4, kp, "rtn", g, "blk.0.ffn_down_exps.0.weight", N, K, w,
|
|
{ { B, { "codes", "table" } }, { BSCALE, { "scale", "scale.1" } } }),
|
|
stored("w4nl32a8h", nMoeW4, kp, "rtn", g, "blk.0.ffn_down_exps.1.weight", N1, K1, w1,
|
|
{ { 6, { "codes", "table" } }, { 7, { "scale", "scale.1" } } }));
|
|
golden("w4nl32a8h", h, 0xf5ce1cb0bac69211ull);
|
|
/* and the 64-wide encoding is not this dtype's */
|
|
Opts g64;
|
|
g64.s("table", "w4nl").i("group", 64).s("scale", "fp8_e4m3").s("scale2", "f32")
|
|
.s("block2", "*x*").f("scale2_value", 0x1p-13).s("transform", "fwht128");
|
|
const Quantised q = quantise(quantizer("rtn"), g64, "blk.0.ffn_down_exps.0.weight", N, K, w);
|
|
REQUIRE_EQ(q.status, RAD_OK);
|
|
Selection s = select(q, N, K, { "codes", "table" });
|
|
RadLayout L{};
|
|
CHECK_EQ(r4d_rad_layout_moe_w4(kp.data(), kp.n(), B, &q.enc, s.sel.data(), s.t.data(),
|
|
(int)s.t.size(), &L), RAD_E_DTYPE);
|
|
}
|
|
|
|
TEST(moe_codebook_experts_at_five_bits) {
|
|
NEED_LIBQUANT();
|
|
/* w5nl64a8h: u5 codes into libquant's 32-entry w5nl table at w4nl64a8h's scales. The codes
|
|
* view relays each row into nibbles and sign bits, the scale view is the 64-wide form's, and
|
|
* the inverse gives back the packed codes and the table. A table other than the kernel's is
|
|
* refused when its bytes are relaid, and the four-bit encoding is not this dtype's. */
|
|
const int64_t N = 64, K = 256, N1 = 48, K1 = 128;
|
|
const std::vector<float> w = weight(N, K, 21, 1, 128);
|
|
const std::vector<float> w1 = weight(N1, K1, 22, 1, 128);
|
|
Opts kp, g;
|
|
kp.i("N", N).i("K", K).i("N_odd", N1).i("K_odd", K1).s("dtype", "w5nl64a8h").i("group", 128);
|
|
g.s("table", "w5nl").i("group", 64).s("scale", "fp8_e4m3").s("scale2", "f32")
|
|
.s("block2", "*x*").f("scale2_value", 0x1p-13).s("transform", "fwht128").s("rule", "search");
|
|
const uint64_t h = chain(
|
|
stored("w5nl64a8h", nMoeW4, kp, "rtn", g, "blk.0.ffn_down_exps.0.weight", N, K, w,
|
|
{ { B, { "codes", "table" } }, { BSCALE, { "scale", "scale.1" } } }),
|
|
stored("w5nl64a8h", nMoeW4, kp, "rtn", g, "blk.0.ffn_down_exps.1.weight", N1, K1, w1,
|
|
{ { 6, { "codes", "table" } }, { 7, { "scale", "scale.1" } } }));
|
|
golden("w5nl64a8h", h, 0xd773da64690cb3f6ull);
|
|
|
|
/* the same 32 levels with one moved: the kernel's table is the only one it decodes */
|
|
Opts g2;
|
|
g2.s("table", "0.8125;2.5;4;5.5;7.5;9;11;12;14;16;18;20;22;24;28;30;-0.8125;-2.5;-4;-5.5;"
|
|
"-7.5;-9;-11;-12;-14;-16;-18;-20;-22;-24;-28;-30")
|
|
.i("group", 64).s("scale", "fp8_e4m3").s("scale2", "f32").s("block2", "*x*")
|
|
.f("scale2_value", 0x1p-13).s("transform", "fwht128");
|
|
const Quantised q2 = quantise(quantizer("rtn"), g2, "blk.0.ffn_down_exps.0.weight", N, K, w);
|
|
REQUIRE_EQ(q2.status, RAD_OK);
|
|
Selection s2 = select(q2, N, K, { "codes", "table" });
|
|
RadLayout L{};
|
|
REQUIRE_EQ(r4d_rad_layout_moe_w4(kp.data(), kp.n(), B, &q2.enc, s2.sel.data(), s2.t.data(),
|
|
(int)s2.t.size(), &L), RAD_OK);
|
|
CHECK_EQ(L.bytes, N * K / 8 * 5);
|
|
std::vector<uint8_t> dst((size_t)L.bytes);
|
|
CHECK_EQ(r4d_rad_relayout_moe_w4(kp.data(), kp.n(), B, &q2.enc, s2.sel.data(), s2.t.data(),
|
|
(int)s2.t.size(), dst.data(), L.bytes), RAD_E_DTYPE);
|
|
/* and the four-bit codebook's encoding is not this dtype's */
|
|
Opts g4;
|
|
g4.s("table", "w4nl").i("group", 64).s("scale", "fp8_e4m3").s("scale2", "f32")
|
|
.s("block2", "*x*").f("scale2_value", 0x1p-13).s("transform", "fwht128");
|
|
const Quantised q4 = quantise(quantizer("rtn"), g4, "blk.0.ffn_down_exps.0.weight", N, K, w);
|
|
REQUIRE_EQ(q4.status, RAD_OK);
|
|
Selection s4 = select(q4, N, K, { "codes", "table" });
|
|
CHECK_EQ(r4d_rad_layout_moe_w4(kp.data(), kp.n(), B, &q4.enc, s4.sel.data(), s4.t.data(),
|
|
(int)s4.t.size(), &L), RAD_E_DTYPE);
|
|
}
|
|
|
|
namespace {
|
|
|
|
/* What the op oracle reads for a relaid weight, obtained as Ctx::oracle_unrelayout does: the
|
|
* stored bytes back through the kernel's inverse into the selected planes, then through the
|
|
* core's enc_selection_logical into the f32 values a dense reference multiplies. These must equal
|
|
* the values read straight off the quantiser's planes (a code as itself or as its table entry, a
|
|
* scale times the levels above it). A selection the core cannot read alone is a weight the oracle
|
|
* must decline. A view stored as is has nothing to invert and is passed through unchanged. */
|
|
void oracle_reads(const char* label, const Hooks& nf, Opts kp, int operand, const Quantised& q,
|
|
int64_t R, int64_t C, const std::vector<const char*>& roles) {
|
|
Selection s = select(q, R, C, roles);
|
|
RadLayout L{};
|
|
const int lrc = nf.layout(kp.data(), kp.n(), operand, &q.enc, s.sel.data(), s.t.data(),
|
|
(int)s.t.size(), &L);
|
|
if (lrc == RAD_E_UNSUPPORTED) return;
|
|
REQUIRE_EQ(lrc, RAD_OK);
|
|
std::vector<uint8_t> laid((size_t)L.bytes);
|
|
REQUIRE_EQ(nf.relayout(kp.data(), kp.n(), operand, &q.enc, s.sel.data(), s.t.data(),
|
|
(int)s.t.size(), laid.data(), L.bytes), RAD_OK);
|
|
std::vector<std::vector<uint8_t>> bufs;
|
|
Selection back = select(q, R, C, roles, &bufs);
|
|
REQUIRE_EQ(nf.unrelayout(kp.data(), kp.n(), operand, &q.enc, back.sel.data(), laid.data(),
|
|
L.bytes, back.t.data(), (int)back.t.size()), RAD_OK);
|
|
std::vector<int64_t> rows, cols;
|
|
std::vector<const void*> pd;
|
|
for (size_t k = 0; k < back.t.size(); ++k) {
|
|
rows.push_back(back.t[k].shape[0]);
|
|
cols.push_back(back.t[k].shape[1]);
|
|
pd.push_back(bufs[k].data());
|
|
}
|
|
const int64_t n = rows[0] * cols[0];
|
|
std::vector<float> got((size_t)n);
|
|
std::string why;
|
|
const int rc = rad::enc_selection_logical(q.enc, back.sel.data(), (int)back.sel.size(),
|
|
rows.data(), cols.data(), pd.data(), RAD_F32, n,
|
|
got.data(), &why);
|
|
if (rc != RAD_OK)
|
|
std::fprintf(stderr, " %s operand %d: %s\n", label, operand, why.c_str());
|
|
REQUIRE_EQ(rc, RAD_OK);
|
|
const RadEncPlane& p0 = q.enc.plane[s.sel[0]];
|
|
const uint8_t* src = q.planes[(size_t)s.sel[0]].data();
|
|
const int64_t rb = rad_enc_row_bytes(p0.dtype, cols[0]);
|
|
const float* table = nullptr;
|
|
float level = 1.0f;
|
|
for (size_t k = 1; k < roles.size(); ++k) {
|
|
const std::vector<uint8_t>& pk = q.planes[(size_t)s.sel[k]];
|
|
if (!std::strcmp(roles[k], "table")) table = (const float*)pk.data();
|
|
else level *= rad_load_f32(pk.data(), q.enc.plane[s.sel[k]].dtype, 0);
|
|
}
|
|
int64_t bad = 0;
|
|
for (int64_t i = 0; i < rows[0]; ++i)
|
|
for (int64_t j = 0; j < cols[0]; ++j) {
|
|
float v = rad_load_f32(src + i * rb, p0.dtype, j);
|
|
if (table) v = table[(int64_t)v];
|
|
if (got[(size_t)(i * cols[0] + j)] != v * level) ++bad;
|
|
}
|
|
if (bad) std::fprintf(stderr, " %s operand %d: %lld of %lld read wrong\n", label, operand,
|
|
(long long)bad, (long long)n);
|
|
CHECK_EQ(bad, (int64_t)0);
|
|
}
|
|
|
|
/* A four-bit expert format as the quantiser is asked for it, and its two views. */
|
|
struct MoeFmt {
|
|
const char* dtype;
|
|
Opts quant;
|
|
std::vector<const char*> codes, scales;
|
|
};
|
|
std::vector<MoeFmt> moe_formats() {
|
|
auto nl = [](const char* table, long long group) {
|
|
Opts g;
|
|
g.s("table", table).i("group", group).s("scale", "fp8_e4m3").s("scale2", "f32")
|
|
.s("block2", "*x*").f("scale2_value", 0x1p-13).s("transform", "fwht128")
|
|
.s("rule", "search");
|
|
return g;
|
|
};
|
|
Opts i4, nla;
|
|
i4.s("codes", "i4").i("group", 128).s("scale", "bf16").s("transform", "fwht128");
|
|
nla.s("table", "w4nl").i("group", 128).s("scale", "bf16").s("transform", "fwht128")
|
|
.s("rule", "search");
|
|
return {
|
|
{ "w4a8h", i4, { "codes" }, { "scale" } },
|
|
{ "w4nla8h", nla, { "codes", "table" }, { "scale" } },
|
|
{ "w4nl64a8h", nl("w4nl", 64), { "codes", "table" }, { "scale", "scale.1" } },
|
|
{ "w4nl32a8h", nl("w4nl", 32), { "codes", "table" }, { "scale", "scale.1" } },
|
|
{ "w5nl64a8h", nl("w5nl", 64), { "codes", "table" }, { "scale", "scale.1" } },
|
|
{ "w6nl64a8h", nl("w6nl", 64), { "codes", "table" }, { "scale", "scale.1" } },
|
|
};
|
|
}
|
|
|
|
} /* namespace */
|
|
|
|
TEST(w6nl_all_codes_have_independent_bit_planes_and_round_trip) {
|
|
NEED_LIBQUANT();
|
|
constexpr int64_t N = 64, K = 256;
|
|
Opts qopt;
|
|
qopt.s("table", "w6nl").i("group", 64).s("scale", "fp8_e4m3").s("scale2", "f32")
|
|
.s("block2", "*x*").f("scale2_value", 0x1p-13).s("transform", "fwht128");
|
|
Quantised q = quantise(quantizer("rtn"), qopt, "blk.0.ffn_down_exps.0.weight", N, K,
|
|
draw(N * K, 187, 1.0f));
|
|
REQUIRE_EQ(q.status, RAD_OK);
|
|
REQUIRE_EQ(q.enc.plane[0].dtype, (uint32_t)RAD_U6);
|
|
const int64_t row = rad_enc_row_bytes(RAD_U6, K);
|
|
for (int64_t n = 0; n < N; ++n)
|
|
for (int64_t k = 0; k < K; ++k)
|
|
rad_store_code(q.planes[0].data() + n * row, RAD_U6, k, (int)((k + n * 17) % 64));
|
|
Opts kp;
|
|
kp.i("N", N).i("K", K).i("group", 128).s("dtype", "w6nl64a8h");
|
|
Selection s = select(q, N, K, { "codes", "table" });
|
|
RadLayout L{};
|
|
REQUIRE_EQ(nMoeW4.layout(kp.data(), kp.n(), B, &q.enc, s.sel.data(), s.t.data(), 2, &L), RAD_OK);
|
|
REQUIRE_EQ(L.bytes, N * K * 6 / 8);
|
|
std::vector<uint8_t> packed((size_t)L.bytes);
|
|
REQUIRE_EQ(nMoeW4.relayout(kp.data(), kp.n(), B, &q.enc, s.sel.data(), s.t.data(), 2,
|
|
packed.data(), L.bytes), RAD_OK);
|
|
for (int64_t n = 0; n < N; ++n) for (int64_t k = 0; k < K; ++k) {
|
|
const uint8_t* r = packed.data() + n * K * 6 / 8;
|
|
const int want = (int)((k + n * 17) % 64);
|
|
const int lo = (r[(k / 8) * 4 + k % 4] >> ((k % 8 / 4) * 4)) & 15;
|
|
uint32_t h = 0, sign = 0;
|
|
std::memcpy(&h, r + K / 2 + (k / 32) * 4, 4);
|
|
std::memcpy(&sign, r + K / 2 + K / 8 + (k / 32) * 4, 4);
|
|
const uint32_t bit = 8u * (uint32_t)(k % 4) + 7u - (uint32_t)((k % 32) / 4);
|
|
CHECK_EQ(lo | (int)(((h >> bit) & 1u) << 4) | (int)(((sign >> bit) & 1u) << 5), want);
|
|
}
|
|
std::vector<std::vector<uint8_t>> backbuf;
|
|
Selection back = select(q, N, K, { "codes", "table" }, &backbuf);
|
|
REQUIRE_EQ(nMoeW4.unrelayout(kp.data(), kp.n(), B, &q.enc, back.sel.data(), packed.data(),
|
|
L.bytes, back.t.data(), 2), RAD_OK);
|
|
CHECK(backbuf[0] == q.planes[0]);
|
|
CHECK(backbuf[1] == q.planes[(size_t)s.sel[1]]);
|
|
/* A table with the right shape but different values must never reach the hard-coded GPU LUT. */
|
|
float bad = 0.0f;
|
|
std::memcpy(q.planes[(size_t)s.sel[1]].data(), &bad, sizeof bad);
|
|
s = select(q, N, K, { "codes", "table" });
|
|
CHECK_EQ(nMoeW4.relayout(kp.data(), kp.n(), B, &q.enc, s.sel.data(), s.t.data(), 2,
|
|
packed.data(), L.bytes), RAD_E_DTYPE);
|
|
Selection scale = select(q, N, K, { "scale", "scale.1" });
|
|
RadLayout LS{};
|
|
REQUIRE_EQ(nMoeW4.layout(kp.data(), kp.n(), BSCALE, &q.enc, scale.sel.data(), scale.t.data(), 2,
|
|
&LS), RAD_OK);
|
|
std::vector<uint8_t> scales((size_t)LS.bytes);
|
|
const float wrong_scale = 0x1p-12f;
|
|
std::memcpy(q.planes[(size_t)scale.sel[1]].data(), &wrong_scale, sizeof wrong_scale);
|
|
CHECK_EQ(nMoeW4.relayout(kp.data(), kp.n(), BSCALE, &q.enc, scale.sel.data(), scale.t.data(), 2,
|
|
scales.data(), LS.bytes), RAD_E_DTYPE);
|
|
}
|
|
|
|
/* The four-class hooks are the two-class hooks extended. moe_gemm_q_mod4's class q is operands 2/3
|
|
* at N/K for q = 0 and 2q+4/2q+5 at N_q/K_q after it, and each is stored and read back exactly as
|
|
* moe_gemm_q's hooks store an expert of that geometry: codes and scales, in every four-bit format,
|
|
* at four class geometries. Then the op oracle reads through each class's inverse (codes and E4M3
|
|
* scale views alike) and through the two-class op's own. */
|
|
TEST(moe_four_classes_store_and_read_back_as_two_classes_do) {
|
|
NEED_LIBQUANT();
|
|
const int64_t Nq[4] = { 64, 128, 64, 48 }, Kq[4] = { 256, 256, 128, 128 };
|
|
for (MoeFmt& f : moe_formats()) {
|
|
Opts kp4;
|
|
kp4.i("N", Nq[0]).i("K", Kq[0]).i("N_1", Nq[1]).i("K_1", Kq[1]).i("N_2", Nq[2])
|
|
.i("K_2", Kq[2]).i("N_3", Nq[3]).i("K_3", Kq[3]).s("dtype", f.dtype).i("group", 128);
|
|
for (int q = 0; q < 4; ++q) {
|
|
const std::vector<float> w = weight(Nq[q], Kq[q], 30 + q, 1, 128);
|
|
const int cop = q == 0 ? B : 2 * q + 4, sop = cop + 1;
|
|
Opts kp2;
|
|
kp2.i("N", Nq[q]).i("K", Kq[q]).s("dtype", f.dtype).i("group", 128);
|
|
const std::string label = std::string(f.dtype) + " class " + std::to_string(q);
|
|
Quantised kept;
|
|
const uint64_t h4 = stored(label.c_str(), nMoeW4Mod4, kp4, "rtn", f.quant,
|
|
"blk.0.ffn_down_exps.0.weight", Nq[q], Kq[q], w,
|
|
{ { cop, f.codes }, { sop, f.scales } }, &kept);
|
|
const uint64_t h2 = stored(label.c_str(), nMoeW4, kp2, "rtn", f.quant,
|
|
"blk.0.ffn_down_exps.0.weight", Nq[q], Kq[q], w,
|
|
{ { B, f.codes }, { BSCALE, f.scales } });
|
|
if (h4 != h2)
|
|
std::fprintf(stderr, " %s: four-class hash 0x%016llx, two-class 0x%016llx\n",
|
|
label.c_str(), (unsigned long long)h4, (unsigned long long)h2);
|
|
CHECK_EQ(h4, h2);
|
|
REQUIRE_EQ(kept.status, RAD_OK);
|
|
oracle_reads(label.c_str(), nMoeW4Mod4, kp4, cop, kept, Nq[q], Kq[q], f.codes);
|
|
oracle_reads(label.c_str(), nMoeW4Mod4, kp4, sop, kept, Nq[q], Kq[q], f.scales);
|
|
oracle_reads(label.c_str(), nMoeW4, kp2, B, kept, Nq[q], Kq[q], f.codes);
|
|
oracle_reads(label.c_str(), nMoeW4, kp2, BSCALE, kept, Nq[q], Kq[q], f.scales);
|
|
/* A class reads its own geometry and nobody else's: class 1 is 128 wide, so an
|
|
* expert of any other class's width is refused there. */
|
|
if (q != 1) {
|
|
Selection s = select(kept, Nq[q], Kq[q], f.codes);
|
|
RadLayout L{};
|
|
CHECK_EQ(nMoeW4Mod4.layout(kp4.data(), kp4.n(), 6, &kept.enc, s.sel.data(),
|
|
s.t.data(), (int)s.t.size(), &L), RAD_E_SHAPE);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST(moe_int4_gptq_against_a_calibration_hessian) {
|
|
NEED_LIBQUANT();
|
|
/* Correlated activations, so the Hessian is far from diagonal and error feedback moves codes
|
|
* that rounding alone would not. Checked below, so a GPTQ that silently fell back to rounding
|
|
* cannot pass as parity. */
|
|
const int64_t N = 64, K = 256, T = 1024;
|
|
const std::vector<float> w = weight(N, K, 23, 1, 128);
|
|
const std::vector<float> z = draw(T * K, 24);
|
|
const std::vector<float> mix = draw(K * K, 25);
|
|
std::vector<float> x((size_t)(T * K), 0.0f);
|
|
for (int64_t t = 0; t < T; ++t)
|
|
for (int64_t i = 0; i < K; ++i) {
|
|
double s = z[(size_t)(t * K + i)] * 4.0;
|
|
for (int64_t j = 0; j < 8; ++j)
|
|
s += mix[(size_t)(i * K + j)] * z[(size_t)(t * K + j)] * 40.0;
|
|
x[(size_t)(t * K + i)] = (float)s;
|
|
}
|
|
std::vector<float> h((size_t)(K * K), 0.0f);
|
|
for (int64_t t = 0; t < T; ++t)
|
|
for (int64_t i = 0; i < K; ++i)
|
|
for (int64_t j = 0; j < K; ++j)
|
|
h[(size_t)(i * K + j)] += x[(size_t)(t * K + i)] * x[(size_t)(t * K + j)];
|
|
const TempDir td("rad_relayout_parity");
|
|
REQUIRE(!td.path.empty());
|
|
const char* dir = td.path.c_str();
|
|
const std::string path = std::string(dir) + "/gram.r0.blk.5.ffn_down_exps.bin";
|
|
/* The Gram of `x` as it stands (the model's own domain), so the rotated recipe below rotates
|
|
* it into its own domain, as it would a bf16 model's calibration. */
|
|
REQUIRE(write_gram(path, K, T, h, 0));
|
|
|
|
const char* name = "blk.5.ffn_down_exps.9.weight";
|
|
for (const char* dt : { "w4a8", "w4a8h" }) {
|
|
const bool rot = std::strcmp(dt, "w4a8h") == 0;
|
|
Opts kp, g, r;
|
|
kp.i("N", N).i("K", K).s("dtype", dt).s("calib", dir).s("weight", name);
|
|
g.s("codes", "i4").i("group", 128).s("scale", "bf16").s("calib", dir);
|
|
r.s("codes", "i4").i("group", 128).s("scale", "bf16");
|
|
if (rot) { g.s("transform", "fwht128"); r.s("transform", "fwht128"); }
|
|
Quantised qg, qr;
|
|
const uint64_t hg = stored(dt, nMoeW4, kp, "gptq", g, name, N, K, w,
|
|
{ { B, { "codes" } }, { BSCALE, { "scale" } } }, &qg);
|
|
qr = quantise(quantizer("rtn"), r, name, N, K, w);
|
|
REQUIRE_EQ(qr.status, RAD_OK);
|
|
CHECK(qg.planes[0] != qr.planes[0]); /* the Hessian moved codes */
|
|
CHECK(qg.planes[1] == qr.planes[1]); /* and not the scales: static groups */
|
|
golden(rot ? "gptq w4a8h" : "gptq w4a8", hg,
|
|
rot ? 0x2006e855d8d09225ull : 0x041995f37ab68a83ull);
|
|
}
|
|
}
|
|
|
|
/* The build makes libavx on every x86-64 and on no other machine. */
|
|
TEST(the_ngram_table_is_the_canonical_plane) {
|
|
#if !defined(__x86_64__)
|
|
SKIP("libavx is x86-64 only");
|
|
#endif
|
|
NEED_LIBQUANT();
|
|
const std::string avx = home() + "/kernels/libavx.so";
|
|
void* so = dlopen(avx.c_str(), RTLD_NOW | RTLD_LOCAL);
|
|
if (!so) ARTIFACT_MISSING(avx + " (does not load: " + dlerror() + ")");
|
|
Hooks nf;
|
|
nf.layout = (RadLayoutFn)dlsym(so, "avx_layout_ngram");
|
|
REQUIRE(nf.layout != nullptr);
|
|
const int64_t V = 100, E = 64;
|
|
const double wscale = 1.99317932128906e-4;
|
|
const std::vector<float> w = draw(V * E, 26, 20.0f);
|
|
Opts kp, g;
|
|
kp.i("n_vocab", V).i("n_embd", E);
|
|
g.s("codes", "fp8_e4m3").s("block", "*x*").s("scale", "bf16").s("rule", "fixed")
|
|
.f("scale_value", wscale);
|
|
golden("ngram", stored("ngram", nf, kp, "rtn", g, "ple.ngram.weight", V, E, w,
|
|
{ { 1, { "codes" } }, { 2, { "scale" } } }),
|
|
0xa5e81d9b9ff05c2aull);
|
|
dlclose(so);
|
|
}
|
|
|
|
TEST(a_kernel_refuses_an_encoding_it_does_not_read) {
|
|
NEED_LIBQUANT();
|
|
const int64_t N = 64, K = 256;
|
|
const std::vector<float> w = draw(N * K, 27);
|
|
Opts i4, i4r, u4, f8;
|
|
i4.s("codes", "i4").i("group", 128).s("scale", "bf16");
|
|
i4r.s("codes", "i4").i("group", 128).s("scale", "bf16").s("transform", "fwht128");
|
|
u4.s("codes", "u4").s("zero", "u4").i("group", 128).s("scale", "f16");
|
|
f8 = fp8_grid();
|
|
const Quantised q_i4 = quantise(quantizer("rtn"), i4, "x", N, K, w);
|
|
const Quantised q_i4r = quantise(quantizer("rtn"), i4r, "x", N, K, w);
|
|
const Quantised q_u4 = quantise(quantizer("rtn"), u4, "x", N, K, w);
|
|
const Quantised q_f8 = quantise(quantizer("rtn"), f8, "x", N, K, w);
|
|
Opts nl, nf4;
|
|
nl.s("table", "w4nl").i("group", 128).s("scale", "bf16").s("transform", "fwht128");
|
|
nf4.s("table", "nf4").i("group", 128).s("scale", "bf16").s("transform", "fwht128");
|
|
const Quantised q_nl = quantise(quantizer("rtn"), nl, "x", N, K, w);
|
|
const Quantised q_nf4 = quantise(quantizer("rtn"), nf4, "x", N, K, w);
|
|
auto lay = [&](RadLayoutFn f, Opts kp, int operand, const Quantised& q,
|
|
std::vector<const char*> roles) {
|
|
Selection s = select(q, N, K, roles);
|
|
RadLayout L{};
|
|
return f(kp.data(), kp.n(), operand, &q.enc, s.sel.data(), s.t.data(), (int)s.t.size(), &L);
|
|
};
|
|
Opts dense, plain, rot, sym, asym;
|
|
dense.i("N", N).i("K", K);
|
|
plain.i("N", N).i("K", K).s("dtype", "w4a8");
|
|
rot.i("N", N).i("K", K).s("dtype", "w4a8h");
|
|
sym.i("N", N).i("K", K).s("dtype", "w4a8");
|
|
asym.i("N", N).i("K", K).s("dtype", "w4a8_asym");
|
|
/* a rotated weight to the experts that do not rotate their activation, and the reverse */
|
|
CHECK_EQ(lay(r4d_rad_layout_moe_w4, plain, B, q_i4r, { "codes" }), RAD_E_DTYPE);
|
|
CHECK_EQ(lay(r4d_rad_layout_moe_w4, rot, B, q_i4, { "codes" }), RAD_E_DTYPE);
|
|
CHECK_EQ(lay(r4d_rad_layout_moe_w4, rot, B, q_i4r, { "codes" }), RAD_OK);
|
|
/* the symmetric dense kernel handed an asymmetric grid, and the reverse; and a bf16 scale */
|
|
CHECK_EQ(lay(r4d_rad_layout_w4, sym, B, q_u4, { "codes" }), RAD_E_DTYPE);
|
|
CHECK_EQ(lay(r4d_rad_layout_w4, asym, B, q_i4, { "codes" }), RAD_E_DTYPE);
|
|
CHECK_EQ(lay(r4d_rad_layout_w4, sym, B, q_i4, { "codes" }), RAD_E_DTYPE);
|
|
/* a block-fp8 weight to the int8 kernel */
|
|
CHECK_EQ(lay(r4d_rad_layout_w8a8, dense, B, q_f8, { "codes" }), RAD_E_DTYPE);
|
|
/* a codebook to the two's complement experts, and -8..7 to the codebook's; the codebook's codes
|
|
* without their table */
|
|
Opts nlk;
|
|
nlk.i("N", N).i("K", K).s("dtype", "w4nla8h");
|
|
CHECK_EQ(lay(r4d_rad_layout_moe_w4, rot, B, q_nl, { "codes" }), RAD_E_DTYPE);
|
|
CHECK_EQ(lay(r4d_rad_layout_moe_w4, nlk, B, q_i4r, { "codes" }), RAD_E_DTYPE);
|
|
CHECK_EQ(lay(r4d_rad_layout_moe_w4, nlk, B, q_nl, { "codes" }), RAD_E_SHAPE);
|
|
CHECK_EQ(lay(r4d_rad_layout_moe_w4, nlk, B, q_nl, { "codes", "table" }), RAD_OK);
|
|
/* Another table of the same shape passes declare (an encoding cannot say what its table
|
|
* holds) and is refused when its bytes are relaid, before the kernel decodes them as w4nl */
|
|
CHECK_EQ(lay(r4d_rad_layout_moe_w4, nlk, B, q_nf4, { "codes", "table" }), RAD_OK);
|
|
{
|
|
Selection s = select(q_nf4, N, K, { "codes", "table" });
|
|
RadLayout L{};
|
|
REQUIRE_EQ(r4d_rad_layout_moe_w4(nlk.data(), nlk.n(), B, &q_nf4.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_moe_w4(nlk.data(), nlk.n(), B, &q_nf4.enc, s.sel.data(),
|
|
s.t.data(), (int)s.t.size(), dst.data(), L.bytes),
|
|
RAD_E_DTYPE);
|
|
}
|
|
}
|
|
|
|
/* The dense integer kernels compile their group in and read no `group` argument, so a hook that
|
|
* accepted another one would hand the kernel planes it reads at 128. */
|
|
TEST(a_dense_int_kernel_refuses_a_group_it_does_not_compute) {
|
|
NEED_LIBQUANT();
|
|
const int64_t N = 64, K = 256;
|
|
const std::vector<float> w = draw(N * K, 28);
|
|
auto rtn = [&](const char* codes, const char* zero, bool clamp, int64_t group) {
|
|
Opts g;
|
|
g.s("codes", codes).i("group", group).s("scale", "f16");
|
|
if (zero) g.s("zero", zero);
|
|
if (clamp) g.s("clamp", "sym");
|
|
Quantised q = quantise(quantizer("rtn"), g, "x", N, K, w);
|
|
CHECK_EQ(q.status, RAD_OK);
|
|
return q;
|
|
};
|
|
auto lay = [&](RadLayoutFn f, const char* dtype, int64_t group, const Quantised& q) {
|
|
Opts kp;
|
|
kp.i("N", N).i("K", K).i("group", group).s("dtype", dtype);
|
|
Selection s = select(q, N, K, { "codes" });
|
|
RadLayout L{};
|
|
return f(kp.data(), kp.n(), B, &q.enc, s.sel.data(), s.t.data(), (int)s.t.size(), &L);
|
|
};
|
|
const Quantised i4 = rtn("i4", nullptr, false, 64), u5 = rtn("u5", "u8", false, 64);
|
|
const Quantised i8 = rtn("i8", nullptr, true, 64), u2 = rtn("u2", "u8", false, 64);
|
|
CHECK_EQ(lay(r4d_rad_layout_w4, "w4a8", 64, i4), RAD_E_DTYPE);
|
|
CHECK_EQ(lay(r4d_rad_layout_w5, "w5a16", 64, u5), RAD_E_DTYPE);
|
|
CHECK_EQ(lay(r4d_rad_layout_w8a16, "w8a16", 64, i8), RAD_E_DTYPE);
|
|
CHECK_EQ(lay(r4d_rad_layout_w2a8, "w2a8", 64, u2), RAD_E_DTYPE);
|
|
|
|
/* llama.cpp's Q8_0 is this encoding, an f16 scale per 32 i8 codes; the w8 kernels would read
|
|
* it at 128. */
|
|
const RadQuantizerInfo* ggml = quantizer("ggml");
|
|
REQUIRE(ggml != nullptr);
|
|
Opts o;
|
|
o.s("type", "q8_0");
|
|
const Quantised q8 = quantise(ggml, o, "x", N, K, w);
|
|
REQUIRE_EQ(q8.status, RAD_OK);
|
|
CHECK_EQ(lay(r4d_rad_layout_w8a16, "w8a16", 32, q8), RAD_E_DTYPE);
|
|
|
|
const Quantised i4_128 = rtn("i4", nullptr, false, 128), i8_128 = rtn("i8", nullptr, true, 128);
|
|
const Quantised u2_128 = rtn("u2", "u8", false, 128);
|
|
CHECK_EQ(lay(r4d_rad_layout_w4, "w4a8", 128, i4_128), RAD_OK);
|
|
CHECK_EQ(lay(r4d_rad_layout_w8a16, "w8a16", 128, i8_128), RAD_OK);
|
|
CHECK_EQ(lay(r4d_rad_layout_w2a8, "w2a8", 128, u2_128), RAD_OK);
|
|
}
|
|
|
|
RAD_TEST_MAIN()
|