feat(qwen4exp): support U4 gate with W6NL down experts

This commit is contained in:
Constantin authored and StillDeadcode committed 2026-11-17 18:35:11 +01:00
1 parent 590dbcc178
commit d1b5a45e31
20 files changed
+618 -79

No files matched your search

+20 -3
View File
@@ -2663,12 +2663,24 @@ TEST(flashnext_trellis_gate_and_w5nl_down_use_separate_activation_formats) {
rad_enc_copy_str(r8.transform, "fwht128");
REQUIRE(rad_enc_valid(&r8));
for (int gate_bits : { 4, 5 }) for (int down_bits : { 5, 6 }) {
RadEncoding gateq = tq, downq = w5;
gateq.plane[0].dtype = gate_bits == 4 ? RAD_U4 : RAD_U5;
downq.plane[0].dtype = down_bits == 5 ? RAD_U5 : RAD_U6;
downq.plane[3].extent[1] = 1 << down_bits;
REQUIRE(rad_enc_valid(&gateq));
REQUIRE(rad_enc_valid(&downq));
for (int rank = 0; rank < 2; ++rank) {
RadModelMeta meta = qwen4exp_meta();
RadBuildCtx c = build_ctx(rank, 2);
c.max_tok = 64; c.max_seqs = 2; c.max_spec = 0; c.max_ctx = 4096;
RadBuilder b;
b.encs = { { "ffn_gate_up_exps", tq }, { "ffn_down_exps", w5 },
const RadEncoding plain = rad_enc_plain(RAD_BF16);
b.encs = { { "blk.1.ffn_gate_up_exps.3.weight", plain },
{ "blk.1.ffn_down_exps.3.weight", plain },
{ "blk.1.ffn_gate_up_exps.10.weight", plain },
{ "blk.1.ffn_down_exps.10.weight", plain },
{ "ffn_gate_up_exps", gateq }, { "ffn_down_exps", downq },
{ "ssm_inz.weight", tq }, { "attn_qg.weight", tq } };
for (const char* name : { "qsa_qk.weight", "attn_k.weight", "attn_v.weight",
"attn_output.weight", "ssm_out.weight",
@@ -2678,7 +2690,10 @@ TEST(flashnext_trellis_gate_and_w5nl_down_use_separate_activation_formats) {
const auto& m = qwen4exp_fp8::g_model[rank];
const auto& moe = m.layers[0].mlp;
CHECK_EQ(moe.fmt[0], 11);
CHECK_EQ(moe.fmt[1], 7);
CHECK_EQ(moe.fmt[1], down_bits == 5 ? 7 : 12);
CHECK_EQ(m.layers[1].mlp.prot.size(), (size_t)2);
CHECK_EQ(m.layers[1].mlp.fmt[0], 11);
CHECK_EQ(m.layers[1].mlp.fmt[1], down_bits == 5 ? 7 : 12);
CHECK(m.a_eff.q && m.a_eff.s && m.a_eff.q8 && m.a_eff.s8);
CHECK_EQ(moe.gq_exp.a.cq(), m.a_eff.q);
CHECK_EQ(moe.gq_exp.a.cs(), m.a_eff.s);
@@ -2691,7 +2706,8 @@ TEST(flashnext_trellis_gate_and_w5nl_down_use_separate_activation_formats) {
CHECK_EQ(down->reads[0], m.a_eff.q);
bool gate_i8 = false, down_w5 = false;
for (const auto& p : gate->p) gate_i8 |= p.key == "dtype" && p.sval == "i8a8h";
for (const auto& p : down->p) down_w5 |= p.key == "dtype" && p.sval == "w5nl64a8h";
for (const auto& p : down->p)
down_w5 |= p.key == "dtype" && p.sval == (down_bits == 5 ? "w5nl64a8h" : "w6nl64a8h");
CHECK(gate_i8);
CHECK(down_w5);
@@ -2724,6 +2740,7 @@ TEST(flashnext_trellis_gate_and_w5nl_down_use_separate_activation_formats) {
CHECK_EQ(dn_issue->opd[1].offset, quant_issue->opd[2].offset);
CHECK(gu_issue->opd[0].handle != quant_issue->opd[1].handle);
}
}
}
/* The MTP-only expert tap must see history rows even in chunks that would ordinarily
+38
View File
@@ -282,6 +282,39 @@ TEST(gptq_with_a_hessian_lowers_the_output_error) {
CHECK(gc.planes[1] == g.planes[1]);
}
TEST(w6nl_gptq_and_coordinate_descent_accept_bf16_domain_hessians) {
NEED_LIBQUANT();
const int64_t R = 16, C = 128, T = 4096;
std::vector<float> h((size_t)(C * C), (float)T * 0.25f);
for (int64_t i = 0; i < C; ++i) h[(size_t)(i * C + i)] += (float)T;
const TempDir td("rad_w6nl_gptq");
REQUIRE(!td.path.empty());
REQUIRE(write_gram(td.path + "/gram.r0.blk.3.ffn_down_exps.bin", C, T, h));
Opts o;
o.s("table", "w6nl").i("group", 64).s("scale", "fp8_e4m3").s("scale2", "f32")
.s("block2", "*x*").f("scale2_value", 0x1p-13).s("transform", "fwht128")
.s("rule", "search").i("cd", 3).s("calib", td.path.c_str());
const std::vector<float> w = draw(R * C, 770, 1.0f);
const Quantised q = quantise(quantizer("gptq"), o, "blk.3.ffn_down_exps.17.weight", R, C, w);
REQUIRE_EQ(q.status, RAD_OK);
CHECK_EQ(q.enc.plane[0].dtype, (uint32_t)RAD_U6);
CHECK_EQ(q.planes[0].size(), (size_t)(R * C * 6 / 8));
CHECK_EQ(q.enc.plane[3].extent[1], 64LL);
for (int i = 0; i < 64; ++i) {
float value = 0.0f;
std::memcpy(&value, q.planes[3].data() + i * sizeof(float), sizeof value);
const uint8_t fp8 = rad_f32_to_fp8e4m3(value);
CHECK_EQ(rad_load_f32(&fp8, RAD_F8E4M3, 0), value);
if (i >= 32) {
float positive = 0.0f;
std::memcpy(&positive, q.planes[3].data() + (i - 32) * sizeof(float), sizeof positive);
CHECK_EQ(value, -positive);
}
}
const std::vector<float> decoded = decode(q, R, C);
CHECK(rel_err(w, decoded) < 0.2);
}
/* ------------------------------------------------------------------ recipes, compiled
*
* A rule's options are typed by its quantiser's own table before declare runs, so a misspelt key or
@@ -337,7 +370,10 @@ TEST(every_shipped_recipe_compiles_and_its_grids_are_accepted) {
REQUIRE(!calib.path.empty());
const char* old = std::getenv("CALIB");
const std::string saved = old ? old : "";
const char* old_mtp = std::getenv("MTP_CALIB");
const std::string saved_mtp = old_mtp ? old_mtp : "";
setenv("CALIB", calib.path.c_str(), 1);
setenv("MTP_CALIB", calib.path.c_str(), 1);
int files = 0, rules = 0;
std::error_code ec;
@@ -391,6 +427,7 @@ TEST(every_shipped_recipe_compiles_and_its_grids_are_accepted) {
CHECK(rules >= 250);
std::filesystem::current_path(cwd, ec);
if (old) setenv("CALIB", saved.c_str(), 1); else unsetenv("CALIB");
if (old_mtp) setenv("MTP_CALIB", saved_mtp.c_str(), 1); else unsetenv("MTP_CALIB");
}
/* ------------------------------------------------------------------ GPTQ against the textbook
@@ -660,6 +697,7 @@ TEST(round_to_nearest_stores_the_nearest_code_on_every_grid) {
"table=iq4nl,group=32,scale=f16",
"table=w4nl,group=128,scale=bf16,transform=fwht128",
"table=w5nl,group=64,scale=fp8_e4m3,scale2=f32",
"table=w6nl,group=64,scale=fp8_e4m3,scale2=f32,transform=fwht128,rule=search",
"table=binary,group=32,scale=f16,rule=search",
"table=ternary,group=32,scale=bf16",
"table=-1.5;-0.25;0;0.5;2,group=32,scale=f32",
+55 -1
View File
@@ -4907,12 +4907,66 @@ TEST(op_moe_gemm_q_mod4) {
* (w4nl64a8h/w5nl64a8h) or 32 (w4nl32a8h). The oracle inverts a relaid weight into exactly this
* description (core/runtime/oracle.cpp). The final case is one last-class expert at K = 128 whose
* 64-wide halves scale by 2 and 3 under one 0.5 activation scale; any other grid reads differently. */
TEST(op_moe_gemm_q_reads_six_bit_codebook_experts) {
constexpr int64_t N = 2, K = 128;
T codes0(RAD_U6, {N, K}), codes1(RAD_U6, {N, K});
T table(RAD_F32, {1, 64}), scale(RAD_F8E4M3, {N, 2}), level(RAD_F32, {1, 1});
std::vector<float> levels(64);
for (int c = 0; c < 64; ++c) levels[(size_t)c] = c < 32 ? (float)(c + 1) : (float)(31 - c);
table.f(levels); scale.f({ 1, 2, 2, 1 }); level.f({ 0.5f });
for (int64_t n = 0; n < N; ++n) for (int64_t k = 0; k < K; ++k) {
const int base = (int)((k % 32 + (k / 64) * 32 + n * 5) % 64);
rad_store_code(codes0.mem.data() + n * (K * 6 / 8), RAD_U6, k, base);
rad_store_code(codes1.mem.data() + n * (K * 6 / 8), RAD_U6, k, (base + 3) % 64);
}
RadEncoding enc{};
rad_enc_clear(&enc);
rad_enc_copy_str(enc.scheme, "affine");
rad_enc_copy_str(enc.transform, "fwht128");
rad_enc_add_plane(&enc, "codes", RAD_U6, 1, 1);
rad_enc_add_plane(&enc, "scale", RAD_F8E4M3, 1, 64);
rad_enc_add_plane(&enc, "scale.1", RAD_F32, 0, 0);
rad_enc_add_table(&enc, "table", RAD_F32, 1, 64);
const int cs[] = { 0, 3 }, ss[] = { 1, 2 };
RadTensor cp0[] = { codes0.t, table.t }, cp1[] = { codes1.t, table.t }, sp[] = { scale.t, level.t };
RadLayout cl{}, sl{};
REQUIRE_EQ(rad_planes_layout(&enc, cs, cp0, 2, &cl), RAD_OK);
REQUIRE_EQ(rad_planes_layout(&enc, ss, sp, 2, &sl), RAD_OK);
std::vector<uint8_t> even((size_t)cl.bytes), odd((size_t)cl.bytes), scales((size_t)sl.bytes);
REQUIRE_EQ(rad_planes_relayout(&enc, cs, cp0, 2, even.data(), cl.bytes), RAD_OK);
REQUIRE_EQ(rad_planes_relayout(&enc, cs, cp1, 2, odd.data(), cl.bytes), RAD_OK);
REQUIRE_EQ(rad_planes_relayout(&enc, ss, sp, 2, scales.data(), sl.bytes), RAD_OK);
T input(RAD_F32, {2, K}), as(RAD_F32, {2, 1}), sorted(RAD_I32, {2});
T off(RAD_I32, {3}), out(RAD_F32, {2, N});
T w0(RAD_U6, {1, N, K}), w1(RAD_U6, {1, N, K});
T s0(RAD_F8E4M3, {1, N, K}), s1(RAD_F8E4M3, {1, N, K});
input.f(std::vector<float>((size_t)(2 * K), 1.0f)); as.f({1, 1});
sorted.n({0, 1}); off.n({0, 1, 2});
A a;
a.t(input).t(as).t(w0).t(s0).t(sorted).t(off).t(w1).t(s1).t(out)
.i("M", 2).i("N", N).i("K", K).i("n_expert", 2).i("top_k", 1).i("group", 128)
.s("a_order", "token").s("dtype", "w6nl64a8h");
void* p0[] = { even.data() }, *p1[] = { odd.data() }, *ps[] = { scales.data() };
a.ts[2].data = p0; a.ts[3].data = ps; a.ts[6].data = p1; a.ts[7].data = ps;
for (int i : { 2, 3, 6, 7 }) a.ts[(size_t)i].stride[0] = 0;
REQUIRE_EQ(run("moe_gemm_q", a), RAD_OK);
for (int e = 0; e < 2; ++e) for (int n = 0; n < N; ++n) {
float want = 0;
for (int k = 0; k < K; ++k) {
const int code = ((k % 32 + (k / 64) * 32 + n * 5) + e * 3) % 64;
const float subscale = n == 0 ? (k < 64 ? 1.0f : 2.0f) : (k < 64 ? 2.0f : 1.0f);
want += levels[(size_t)code] * subscale * 0.5f;
}
CHECK_EQ(out.at(e * N + n), want);
}
}
TEST(op_moe_gemm_q_scale_grid_by_dtype) {
struct Want { const char* dt; int64_t rows, cols; };
/* N 256, K 512, group 128; the last class at N 128, K 256. */
const Want wants[] = { { "fp8a8", 2, 4 }, { "w4a8", 256, 4 }, { "w4a8h", 256, 4 },
{ "w4nla8h", 256, 4 }, { "w4nl64a8h", 256, 8 }, { "w5nl64a8h", 256, 8 },
{ "w4nl32a8h", 256, 16 }, { "mxfp4a8", 256, 16 } };
{ "w6nl64a8h", 256, 8 }, { "w4nl32a8h", 256, 16 }, { "mxfp4a8", 256, 16 } };
for (const char* op : { "moe_gemm_q", "moe_gemm_q_mod4" }) {
const RadKernelInfo* k = kern(op);
REQUIRE(k != nullptr && k->opd_shape != nullptr);
+57
View File
@@ -687,11 +687,68 @@ std::vector<MoeFmt> moe_formats() {
{ "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,