diff --git a/arch/qwen35_fp8/qwen35_fp8.cpp b/arch/qwen35_fp8/qwen35_fp8.cpp index 0ceffff0..b781fe71 100644 --- a/arch/qwen35_fp8/qwen35_fp8.cpp +++ b/arch/qwen35_fp8/qwen35_fp8.cpp @@ -151,6 +151,8 @@ struct Model { ActFP8 a_eff{}; rad_buf b_sgu = 0, b_sout = 0, b_sgate = 0; ActFP8 a_sff{}; + /* Rotated codes of the expert input, for rotated four-bit experts (MoeFP8::Config::expert_rot). */ + ActFP8 a_hr{}; #endif /* The DFlash2 drafter, declared only when the container carries one and the deployment @@ -713,6 +715,21 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c (long long)mc.top_k, (long long)mc.n_expert); return RAD_E_INVAL; } + /* GPTQ calibration run (MoeFP8::Config::calib), on when the output directory is set. */ + mc.calib = calib_dir() != nullptr ? 1 : 0; + /* The experts' stored format, read from layer 0's first expert as qwen4exp does, so a + * container with four-bit experts is served in that format. */ + { + MoeFP8::Src ps{}; + RAD_ARCH_TRY(moe_expert_sources(b, nm, "format", + "model.language_model.layers.0.mlp.experts", &ps)); + RAD_ARCH_TRY(MoeFP8::probe(b, nm, 0, ps, &mc)); + } + if (mc.expert_bf16) { + fprintf(stderr, "radiance: qwen35moe: plain bf16 experts are not served by this " + "plugin; convert with a quantising recipe\n"); + return RAD_E_UNSUPPORTED; + } } { /* The step's height for everything with one row per token, and a pass's height only for @@ -743,6 +760,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c m.b_sout = decl_b(b, nm.f("shexp_out"), g.act_dtype, {R, g.n_embd}); m.b_sgate = decl_b(b, nm.f("shexp_gate"), g.act_dtype, {R, 1}); } + if (mc.expert_rot) RAD_ARCH_TRY(m.a_hr.declare_qs(b, nm, g, "moe_hr", g.n_embd)); } #else m.b_gate_up = decl_b(b, nm.f("gate_up"), g.act_dtype, {g.max_tok, 2 * g.n_ff}); @@ -865,6 +883,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c mw.sorted = m.b_sorted; mw.eoff = m.b_eoff; mw.ecnt = m.b_ecnt; mw.egu = m.b_egu; mw.eff = m.a_eff; mw.edn = m.b_edn; mw.moe = m.b_emoe; mw.sgu = m.b_sgu; mw.sff = m.a_sff; mw.sout = m.b_sout; mw.sgate = m.b_sgate; + mw.hr = m.a_hr; #else mw.x = m.b_x; mw.h = m.a_h; mw.gate_up = m.b_gate_up; mw.ffn = m.a_ffn; #endif @@ -916,7 +935,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c ms.cfg = m.moecfg; ms.norm = nm.ckpt("%s.%lld.post_attention_layernorm.weight", pre, (long long)l); ms.router = nm.ckpt("%s.%lld.mlp.gate.weight", pre, (long long)l); - ms.experts = nm.ckpt("%s.%lld.mlp.experts", pre, (long long)l); + RAD_ARCH_TRY(moe_expert_sources(b, nm, "layer", + nm.ckpt("%s.%lld.mlp.experts", pre, (long long)l), &ms)); if (m.moecfg.n_ff_shared > 0) { ms.shared = nm.ckpt("%s.%lld.mlp.shared_expert", pre, (long long)l); ms.shared_gate = nm.ckpt("%s.%lld.mlp.shared_expert_gate.weight", pre, (long long)l); @@ -1019,7 +1039,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c * MtpBlockFP8 is a template over the feed-forward. */ ts.mlp.cfg = m.moecfg; ts.mlp.router = nm.ckpt("mtp.layers.0.mlp.gate.weight"); - ts.mlp.experts = nm.ckpt("mtp.layers.0.mlp.experts"); + RAD_ARCH_TRY(moe_expert_sources(b, nm, "mtp", nm.ckpt("mtp.layers.0.mlp.experts"), + &ts.mlp)); if (m.moecfg.n_ff_shared > 0) { ts.mlp.shared = nm.ckpt("mtp.layers.0.mlp.shared_expert"); ts.mlp.shared_gate = nm.ckpt("mtp.layers.0.mlp.shared_expert_gate.weight"); diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index 253b8486..a3bd4457 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -9031,7 +9031,7 @@ struct ArchCases { nullptr, 7, { "ar_rmsnorm_quant_fp8" }, 0x2c00f131709dedcfull); add("qwen35moe_fp8 + MTP", [this, sc](RadBuilder* b) { auto c = sc(1, 3); return qwen35moe_fp8::declare(b, q35moe.meta(), &c); }, - qwen35moe_fp8::step, 3, {}, 0x3157a986f13a9a72ull); + qwen35moe_fp8::step, 3, {}, 0x1bd3b53543cd06eeull); add("qwen4exp_fp8", [this](RadBuilder* b) { auto c = q4_ctx(0, 1); return qwen4exp_fp8::declare(b, &q4, &c); }, qwen4exp_fp8::step, 3, {}, 0xbe74d476c7fd04b8ull); @@ -9811,8 +9811,8 @@ TEST(a_4096_budget_without_a_span_is_the_pinned_graph_and_steps) { { "qwen4exp_fp8", 4, 3, 0x6669aa0573931943ull, 0x36df57cac613c6cfull }, { "qwen35_fp8", 1, 0, 0xcfc82ca915ec549dull, 0x4d6fca823d1e97efull }, { "qwen35_fp8", 2, 1, 0x36da8d762194e24eull, 0xe4022daff02ec8bdull }, - { "qwen35moe_fp8", 1, 0, 0xb7c4d121acb388b3ull, 0x3fcf71676e558d79ull }, - { "qwen35moe_fp8", 2, 0, 0x5c02b2d6cd49ed0cull, 0x05e0778848bf416cull }, + { "qwen35moe_fp8", 1, 0, 0x7fa7b015e1bc8873ull, 0x3fcf71676e558d79ull }, + { "qwen35moe_fp8", 2, 0, 0xf60c9508dd197370ull, 0x05e0778848bf416cull }, }; for (const Case& cs : cases) { RadBuilder b;