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

+26 -19
View File
@@ -259,6 +259,7 @@ struct MoeFP8 {
* layer's; the 64-group codebook forms may differ by layer and projection, and declare()
* resolves each layer's (MoeFP8::fmt). */
int expert_w5 = 0;
int expert_w6 = 0; /* six-bit codebook, E4M3 scale per 64 */
/* Int8 experts (`i8*bf16[1x128]/fwht128`): codes -127..127 with a bf16 scale per (row, 128
* of K), each 128 of K rotated as `expert_rot` describes, read against int8 activations
* (dtype `i8a8h`). The eight-bit expert of an int8 model, where block fp8 is that of an
@@ -512,12 +513,12 @@ struct MoeFP8 {
static int format_of(const RadEncoding& e);
/* format_of's numbers as the grouped GEMMs' dtype strings against an E4M3 activation, and
* against an int8 one (null: the format has no such row). Int8 experts read int8 only. */
static constexpr const char* kServed[12] = { "fp8a8", "w4a8", "w4a8h", "bf16", "w4nla8h",
static constexpr const char* kServed[13] = { "fp8a8", "w4a8", "w4a8h", "bf16", "w4nla8h",
"w4nl64a8h", "w4nl32a8h", "w5nl64a8h", "mxfp4a8",
"fp8a8f32", nullptr, nullptr };
static constexpr const char* kServed8[12] = { nullptr, nullptr, "w4a8i8h", nullptr, nullptr,
"fp8a8f32", nullptr, nullptr, "w6nl64a8h" };
static constexpr const char* kServed8[13] = { nullptr, nullptr, "w4a8i8h", nullptr, nullptr,
"w4nl64a8i8h", "w4nl32a8i8h", nullptr, nullptr,
nullptr, "i8a8h", "i8a8h" };
nullptr, "i8a8h", "i8a8h", nullptr };
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
const Wire& wire, const Src& src, bool fold = false, bool add_out = true,
@@ -569,6 +570,8 @@ inline int MoeFP8::format_of(const RadEncoding& e) {
return sc->block[1] == RAD_FP8_BLOCK / 2 ? 5 : 6;
if (t->extent[1] == 32 && e.plane[0].dtype == RAD_U5 && sc->block[1] == RAD_FP8_BLOCK / 2)
return 7;
if (t->extent[1] == 64 && e.plane[0].dtype == RAD_U6 && sc->block[1] == RAD_FP8_BLOCK / 2)
return 12;
return RAD_E_DTYPE;
}
if (!sc) return RAD_E_DTYPE;
@@ -685,13 +688,14 @@ inline int MoeFP8::probe(RadBuilder* b, Names& nm, int l, const Src& src, Config
return linear_refuse_retired(guw, f == 8 ? "MXFP4" : "block fp8");
if (f != 3) break;
}
cfg->expert_w4 = (f == 1 || f == 2 || f == 4 || f == 5 || f == 6 || f == 7) ? 1 : 0;
cfg->expert_rot = (f == 2 || f == 4 || f == 5 || f == 6 || f == 7 || f == 10 || f == 11) ? 1 : 0;
cfg->expert_w4 = (f == 1 || f == 2 || f == 4 || f == 5 || f == 6 || f == 7 || f == 12) ? 1 : 0;
cfg->expert_rot = (f == 2 || f == 4 || f == 5 || f == 6 || f == 7 || f == 10 || f == 11 || f == 12) ? 1 : 0;
cfg->expert_i8 = f == 10 ? 1 : 0;
cfg->expert_iqr = f == 11 ? 1 : 0;
cfg->expert_nl = (f == 4 || f == 5 || f == 6 || f == 7) ? 1 : 0;
cfg->expert_g64 = (f == 5 || f == 7) ? 2 : f == 6 ? 4 : 0;
cfg->expert_nl = (f == 4 || f == 5 || f == 6 || f == 7 || f == 12) ? 1 : 0;
cfg->expert_g64 = (f == 5 || f == 7 || f == 12) ? 2 : f == 6 ? 4 : 0;
cfg->expert_w5 = f == 7 ? 1 : 0;
cfg->expert_w6 = f == 12 ? 1 : 0;
cfg->expert_bf16 = f == 3 ? 1 : 0;
cfg->expert_mx = f == 8 ? 1 : 0;
cfg->expert_f32_scale = f == 9 ? 1 : 0;
@@ -777,7 +781,7 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
a8 = w.eff.q8_fed && !c.expert_bf16;
prot.clear();
{
const int want = c.expert_iqr ? 11 : c.expert_i8 ? 10 : c.expert_f32_scale ? 9 : c.expert_mx ? 8 : c.expert_bf16 ? 3 : c.expert_w5 ? 7 : c.expert_g64 == 4 ? 6
const int want = c.expert_iqr ? 11 : c.expert_i8 ? 10 : c.expert_f32_scale ? 9 : c.expert_mx ? 8 : c.expert_bf16 ? 3 : c.expert_w6 ? 12 : c.expert_w5 ? 7 : c.expert_g64 == 4 ? 6
: c.expert_g64 ? 5 : c.expert_nl ? 4
: c.expert_w4 ? (c.expert_rot ? 2 : 1) : 0;
auto fmt_of = [&](int64_t e, RadEncoding (&enc)[2], int (&fe)[2], const char* (&nmv)[2]) {
@@ -799,7 +803,7 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
const bool a8f = a8;
auto family = [a8f](int f) {
return a8f ? (f == 2 || f == 5 || f == 6 || f == 10 || f == 11)
: (f == 5 || f == 6 || f == 7);
: (f == 5 || f == 6 || f == 7 || f == 12);
};
fmt[0] = fmt[1] = want;
fenc[0] = fenc[1] = RadEncoding{};
@@ -812,8 +816,9 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
for (int i = 0; i < 2; ++i) {
/* A trellis gate_up reads rotated int8, but the down projection can use the
* W5NL E4M3 kernel: its gated intermediate is quantised independently below. */
const bool mixed_w5_down = a8f && want == 11 && i == 1 && fe[i] == 7;
if (family(want) && (family(fe[i]) || mixed_w5_down)) fmt[i] = fe[i];
const bool mixed_codebook_down = a8f && want == 11 && i == 1 &&
(fe[i] == 7 || fe[i] == 12);
if (family(want) && (family(fe[i]) || mixed_codebook_down)) fmt[i] = fe[i];
fenc[i] = enc[i];
}
break;
@@ -1041,6 +1046,7 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
switch (f) {
/* An IQR weight: the codes at their stored width with the code's planes, and the row and
* block scales as one view at the weight's extents (LinearFP8's views of the same form). */
case 12: return { RAD_U6, "codes,table", RAD_F8E4M3, 1, "scale,scale.1" };
case 11: return { fe.plane[0].dtype, "codes,trellis,trellis.mad", RAD_F32, 1,
"scale,scale.col" };
case 10: return { RAD_I8, "codes", RAD_BF16, RAD_FP8_BLOCK, "scale" };
@@ -1077,15 +1083,16 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
return RAD_E_INVAL;
}
/* ---- The routed arm's activations (see `a8`). */
if ((c.expert_i8 || c.expert_iqr || fmt[0] >= 10 || fmt[1] >= 10) && !a8) {
if ((c.expert_i8 || c.expert_iqr || fmt[0] == 10 || fmt[0] == 11 ||
fmt[1] == 10 || fmt[1] == 11) && !a8) {
fprintf(stderr, "radiance: layer %d's experts read int8 activations (int8, or an IQR weight); the "
"architecture declared the routed arm's codes E4M3\n", l);
return RAD_E_UNSUPPORTED;
}
const bool down_w5_fp8 = a8 && fmt[0] == 11 && fmt[1] == 7;
const bool down_codebook_fp8 = a8 && fmt[0] == 11 && (fmt[1] == 7 || fmt[1] == 12);
if (a8) {
const bool pair = w.eff.q8 && w.eff.s8 && w.eff.q8_rot && w.eff.g8 == RAD_FP8_BLOCK;
const bool down_supported = down_w5_fp8 ? (w.eff.q && w.eff.s && kServed[fmt[1]])
const bool down_supported = down_codebook_fp8 ? (w.eff.q && w.eff.s && kServed[fmt[1]])
: kServed8[fmt[1]] != nullptr;
/* A clamped layer is served too: GateQuantFP8 clamps into the bf16 plane, then
* quantises it into the rotated codes. Not before a W5NL down projection, which reads
@@ -1116,7 +1123,7 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
* experts' encoding or the kernel is not a candidate. */
gu_trellis = fmt[0] == 11;
const char* const edtype[2] = { a8 ? kServed8[fmt[0]] : kServed[fmt[0]],
a8 && !down_w5_fp8 ? kServed8[fmt[1]] : kServed[fmt[1]] };
a8 && !down_codebook_fp8 ? kServed8[fmt[1]] : kServed[fmt[1]] };
/* This class's slice of each expert: gate and up rows [lo, hi), down columns [lo, hi), scale
* planes the same in their own units. */
auto span = [&](rad_weight h, int q, int64_t unit) -> int {
@@ -1316,9 +1323,9 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
/* Gate_up and down are separate GEMMs. A trellis gate uses int8 input while W5NL down
* reads rotated E4M3: select the latter pair only for the gated intermediate. */
ActFP8 down_eff = w.eff;
if (down_w5_fp8) down_eff.q8_fed = down_eff.q8_rot = false;
if (down_codebook_fp8) down_eff.q8_fed = down_eff.q8_rot = false;
RAD_ARCH_TRY(gq_exp.declare(b, g, w.egu, down_eff, c.n_ff_exp, gact, c.rows * c.top_k,
false, rot && (!a8 || down_w5_fp8), c.swiglu_limit,
false, rot && (!a8 || down_codebook_fp8), c.swiglu_limit,
c.swiglu_step != 0));
{
const std::vector<rad_weight> tab = tables(w_dn, w_dns);
@@ -1336,7 +1343,7 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
RAD_STR("a_order", "sorted"), RAD_STR("dtype", edtype[1]),
RAD_INT("N_odd", g.n_embd), RAD_INT("K_odd", wq[1]),
RAD_INT("parts", 1), RAD_INT("a_group", RAD_FP8_BLOCK) };
const int cut = a8 && !down_w5_fp8 ? 0 : 1;
const int cut = a8 && !down_codebook_fp8 ? 0 : 1;
rad_op h = ncls == 4
? rad_decl_op(b, "moe_gemm_q_mod4", p4, (int)(sizeof p4 / sizeof *p4) - cut,
tab.data(), (int)tab.size())