From 22df1c7264c448a58562ef41eb6bbed595a39498 Mon Sep 17 00:00:00 2001 From: hugypufy Date: Sun, 25 Oct 2026 12:49:01 +0000 Subject: [PATCH] arch/common, data/recipes: an M2XFP4 DFlash2 drafter -- DenseFP8 declares and issues an m2xfp4 weight, and q38-27b-m2xfp4-df2.recipe converts one DenseFP8, the drafter's linear, reads a plain m2xfp4 encoding as LinearFP8 does: the weight view takes the codes and meta planes together, the E8M0 plane is b_scale and b_ref, and gemm() issues gemm_nt_q at dtype m2xfp4 over the same E4M3 pair. declare_slice stays block fp8 and clears the flag. The DFlash2 block keeps an M2XFP4 gate_up off the fp8a8 gated fold, as it does an MXFP4 one. The recipe is q38-27b-mxfp4-df2.recipe with its 4-bit trunk and drafter rules at m2xfp4. Co-Authored-By: Claude Opus 5.5 --- arch/common/rad_block_dflash2.h | 4 +-- arch/common/rad_fp8.h | 42 +++++++++++++++++++++++--- data/recipes/q38-27b-m2xfp4-df2.recipe | 42 ++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 data/recipes/q38-27b-m2xfp4-df2.recipe diff --git a/arch/common/rad_block_dflash2.h b/arch/common/rad_block_dflash2.h index 0dc7a52e..abeb31a7 100644 --- a/arch/common/rad_block_dflash2.h +++ b/arch/common/rad_block_dflash2.h @@ -673,8 +673,8 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co {w.h}, {w.coef}); op_gu[i] = w_gu[i].gemm(b, qrows, 2 * g.n_ff, g.n_embd, {w.h2q, w.h2s}, {w.gate_up}); /* The fold saves two launches and the bf16 round trip of the [T, n_ff] product; it is an - * fp8a8 tile, so an MXFP4 gate_up takes the pair. */ - if (!w_gu[i].mx) + * fp8a8 tile, so an MXFP4 or M2XFP4 gate_up takes the pair. */ + if (!w_gu[i].mx && !w_gu[i].m2x) op_gug[i] = decl_gated_fold(b, qrows, 2 * g.n_ff, g.n_embd, w_gu[i].w, w_gu[i].s, {w.h2q, w.h2s}, {w.ffq, w.ffs}, &gated_rows); op_dn[i] = w_dn[i].gemm(b, qrows, g.n_embd, g.n_ff, {w.ffq, w.ffs}, {w.h}); diff --git a/arch/common/rad_fp8.h b/arch/common/rad_fp8.h index 94e37aab..a202ecb0 100644 --- a/arch/common/rad_fp8.h +++ b/arch/common/rad_fp8.h @@ -1641,6 +1641,11 @@ struct DenseFP8 { * works against the same E4M3 activation pair. */ rad_weight ref = 0; bool mx = false; + /* Or M2XFP4 (libquant `m2xfp4`): the MXFP4 weight plus one metadata byte a group, read by + * `gemm_nt_q` at dtype m2xfp4 with the same views and the same `b_ref`. The meta plane rides + * the weight operand's stored blob (libr4d's m2xfp4 layout), so the declared weight takes the + * codes and meta planes together. */ + bool m2x = false; /* `mapped`: the caller made the name map already, to ask the encoding before choosing this * struct over another grid's -- a COPY map declared twice is refused (rad_builder.cpp). */ @@ -1659,6 +1664,7 @@ struct DenseFP8 { if (lo < 0 || hi <= lo || lo % RAD_FP8_BLOCK || hi % RAD_FP8_BLOCK || K % RAD_FP8_BLOCK) return RAD_E_INVAL; mx = false; + m2x = false; ref = 0; const char* wn = nm.f("%s.weight", base); const char* sn = nm.f("%s.scale", base); @@ -1671,11 +1677,18 @@ struct DenseFP8 { RAD_ARCH_TRY(rad_weight_shard_span(b, w, lo, hi)); return rad_weight_shard_span(b, s, lo / RAD_FP8_BLOCK, hi / RAD_FP8_BLOCK); } - /* `gemm_nt_q` over this weight, `fp8a8` or `mxfp4a8` as it was declared, ranged M 1..hi. - * `split_n`, when set, is the stored width a row slice takes its K split from (docs/OPS.md). */ + /* `gemm_nt_q` over this weight, `fp8a8`, `mxfp4a8` or `m2xfp4` as it was declared, ranged + * M 1..hi. `split_n`, when set, is the stored width a row slice takes its K split from + * (docs/OPS.md). */ rad_op gemm(RadBuilder* b, int64_t hi, int64_t N, int64_t K, std::initializer_list reads, std::initializer_list writes, int64_t split_n = 0) const { + if (m2x) + return rw(b, RAD_OP(b, "gemm_nt_q", + RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K), + RAD_INT("group", RAD_MX_GROUP), RAD_STR("dtype", "m2xfp4")), + RAD_WEIGHTS(w, s, ref)), + reads, writes); if (mx) return rw(b, RAD_OP(b, "gemm_nt_q", RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K), @@ -1695,8 +1708,9 @@ struct DenseFP8 { RAD_WEIGHTS(w, s)), reads, writes); } - /* The op's trailing `b_ref` operand: the reference view for MXFP4, absent for fp8. */ - RadOperand bref() const { return mx ? RAD_W(ref) : RAD_NONE; } + /* The op's trailing `b_ref` operand: the reference view for MXFP4 and M2XFP4, absent for + * fp8. */ + RadOperand bref() const { return mx || m2x ? RAD_W(ref) : RAD_NONE; } }; inline int DenseFP8::declare(RadBuilder* b, Names& nm, const char* base, int64_t N, int64_t K, @@ -1712,6 +1726,7 @@ inline int DenseFP8::declare(RadBuilder* b, Names& nm, const char* base, int64_t if (!mapped) RAD_ARCH_TRY(map_linear(b, wn, src)); RadEncoding enc{}; mx = weight_enc(b, wn, &enc) && linear_is_mxfp4(enc); + m2x = !mx && linear_is_m2xfp4(enc); /* enc stays zero when there is no encoding */ if (mx) { if (N % 16 || K % RAD_FP8_BLOCK) { fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; an MXFP4 weight takes rows " @@ -1729,6 +1744,25 @@ inline int DenseFP8::declare(RadBuilder* b, Names& nm, const char* base, int64_t access, shard, grp, rp.w, rp.n); return w && s && ref ? RAD_OK : RAD_E_INVAL; } + /* ---- M2XFP4: the MXFP4 views with the metadata plane riding the weight operand, the + * same bounds. */ + if (m2x) { + if (N % 16 || K % RAD_FP8_BLOCK) { + fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; an M2XFP4 weight takes rows " + "in 16s and K in whole %d-column activation scale blocks\n", + base, (long long)N, (long long)K, RAD_FP8_BLOCK); + return RAD_E_INVAL; + } + RowParts rp; + RAD_ARCH_TRY(rp.set(row_parts, 16, 1, base, "M2XFP4 tiles")); + w = decl_view(b, wn, nullptr, "codes,meta", RAD_FP4E2M1, { N, K }, access, shard, grp, + rp.w, rp.n); + s = decl_view(b, nm.f("%s.scale", base), wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP }, + access, shard, grp, rp.w, rp.n); + ref = decl_view(b, nm.f("%s.ref", base), wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP }, + access, shard, grp, rp.w, rp.n); + return w && s && ref ? RAD_OK : RAD_E_INVAL; + } } if (N % RAD_FP8_BLOCK || K % RAD_FP8_BLOCK) { fprintf(stderr, "radiance: '%s' is %lldx%lld per rank, which is not a whole number of " diff --git a/data/recipes/q38-27b-m2xfp4-df2.recipe b/data/recipes/q38-27b-m2xfp4-df2.recipe new file mode 100644 index 00000000..58a6671e --- /dev/null +++ b/data/recipes/q38-27b-m2xfp4-df2.recipe @@ -0,0 +1,42 @@ +# q38-27b-m2xfp4-df2.recipe -- a bf16 Qwen3.8-27B (or a finetune of it) at M2XFP4 with the DFlash2 +# drafter. +# +# rad-convert --draft-model \ +# --recipe q38-27b-m2xfp4-df2.recipe \ +# --set quantization_config.quant_method=fp8 --set quantization_config.fmt=e4m3 \ +# -o q38-27b-m2xfp4-df2.rad +# +# The --set keys give the container the family's fp8 descriptor, so qwen35_fp8 serves it as it +# serves the Quark, ParoQuant and NVFP4 releases; without them a bf16 checkpoint selects +# qwen35_bf16, which has no 4-bit GEMM. +# +# q38-27b-mxfp4-df2.recipe with its 4-bit rules at M2XFP4 (`m2xfp4`, docs/TOOLS.md): the MXFP4 +# codes and e8m0 exponent per 32 of K plus one metadata byte a group, whose four 2-bit extra +# mantissas refine the exponent per 8 columns, chosen by least squared error from the bf16 source, +# once. The delta net's a/b projections, the norms, the embedding and the vision tower stay bf16; +# the head goes block fp8 as in the other 27B recipes. The checkpoint's MTP head is not read: the +# DFlash2 drafter displaces it. +# +# The drafter's linears go to M2XFP4 as well, beside the same 2-bit copy of the target's head (u2 +# with a u8 zero, least-squares clip search) whose top 32 the engine rescores against the target's +# fp8 head. + +blk.*.ffn_gate_up.weight m2xfp4 +blk.*.ffn_down.weight m2xfp4 +blk.*.attn_qg.weight m2xfp4 +blk.*.attn_k.weight m2xfp4 +blk.*.attn_v.weight m2xfp4 +blk.*.attn_output.weight m2xfp4 +blk.*.ssm_inz.weight m2xfp4 +blk.*.ssm_out.weight m2xfp4 + +output.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.draft_head.weight rtn codes=u2 zero=u8 group=128 scale=f16 rule=search + +dflash.fc.weight m2xfp4 +dflash.blk.*.attn_q.weight m2xfp4 +dflash.blk.*.attn_k.weight m2xfp4 +dflash.blk.*.attn_v.weight m2xfp4 +dflash.blk.*.attn_output.weight m2xfp4 +dflash.blk.*.ffn_gate_up.weight m2xfp4 +dflash.blk.*.ffn_down.weight m2xfp4