From 5681da471c956c4cf5e7140cd9a4770e2967e146 Mon Sep 17 00:00:00 2001 From: Malic Zilyas Date: Wed, 7 Oct 2026 23:52:17 +0000 Subject: [PATCH] qwen35_fp8: serve rotated MXFP4 linears (LinearFP8 rotates its input first); paro recipe A rotated weight reads the producer's bf16 plane, rotates and quantises it into its own planes (parts side by side) and runs the ordinary mxfp4a8 GEMM. Adds data/recipes/q38-27b-paro-mxfp4-df2.recipe and the GUIDE entry. --- arch/common/rad_block_gdn_fp8.h | 4 +- arch/common/rad_fp8.h | 137 ++++++++++++++++++++- data/recipes/q38-27b-paro-mxfp4-df2.recipe | 38 ++++++ docs/GUIDE.md | 9 +- 4 files changed, 180 insertions(+), 8 deletions(-) create mode 100644 data/recipes/q38-27b-paro-mxfp4-df2.recipe diff --git a/arch/common/rad_block_gdn_fp8.h b/arch/common/rad_block_gdn_fp8.h index 67b12a6c..a410d813 100644 --- a/arch/common/rad_block_gdn_fp8.h +++ b/arch/common/rad_block_gdn_fp8.h @@ -275,7 +275,9 @@ inline int GdnFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, const Con * all of q, all of k and a third of v, a well-formed wrong model. */ RAD_ARCH_TRY(in.declare(b, nm, g, nm.f("blk.%d.ssm_inz", l), conv_dim + v_dim, g.n_embd, RAD_SHARD_ROW, grp_layer(l), { s.in_qkv, s.in_z }, w.h, w.in.x, 0, - { cfg.k_dim(), cfg.k_dim(), cfg.v_dim(), cfg.v_dim() })); + { cfg.k_dim(), cfg.k_dim(), cfg.v_dim(), cfg.v_dim() }, 0, 0, + /* a rotated weight was rotated per CHECKPOINT tensor: q|k|v, then z */ + { 2 * cfg.k_dim() + cfg.v_dim(), cfg.v_dim() })); if (cfg.uneven()) { /* THIS RANK'S HEADS OF EACH PART, in the container's rows: q and k by K head, v and z by * V head. The scale plane of a row shard has the same rows (its blocks run along K). */ diff --git a/arch/common/rad_fp8.h b/arch/common/rad_fp8.h index e0212f47..7415fbae 100644 --- a/arch/common/rad_fp8.h +++ b/arch/common/rad_fp8.h @@ -58,6 +58,7 @@ #include #include +#include namespace rad { namespace arch { @@ -690,6 +691,17 @@ struct LinearFP8 { * feeds an MXFP4 linear unchanged. Read off the weight's encoding like `bf16` and `i8`. */ bool mx = false; rad_weight wref = 0; + /* ...ROTATED (ParoQuant), a variant of `mx`: the same GEMM over an activation that + * `givens_quant_act_fp8` made from the input's bf16 plane -- the channel scale, the Givens + * stages and the E4M3 quantiser in one pass, once for each rotated PART of a fused projection + * (gate|up, qkv|z: each piece was rotated on its own) -- into this linear's own code and + * scale planes, `parts` copies side by side. The tables are views of the weight's own planes. + * The GEMM takes the parts' row bounds as `pb1`/`pb2` and meets copy p with part p's rows. */ + bool paro = false, probed_paro = false; + rad_weight rot_pairs = 0, rot_angle = 0, rot_scale = 0; + rad_buf rot_q = 0, rot_s = 0; + rad_op op_rot = 0; + int64_t rot_parts = 1; /* What probe() read: the weight is int8. Lets a model learn its trunk's format from one linear * before it declares the activation buffers that format decides (ActFP8::declare_qs's `i8`). */ bool probed_i8 = false; @@ -728,12 +740,16 @@ struct LinearFP8 { /* `span_lo`/`span_hi`, when hi is set, is this rank's uneven slice of the split dimension in * the model's units -- rows of each row part for RAD_SHARD_ROW, columns for RAD_SHARD_COL -- * and n_out / n_in is already its width; see rad_weight_shard_span. Whole scale blocks. */ + /* `rot_parts`: the rows of each independently rotated part of a ROTATED weight (probed_paro), + * which `row_parts` is when the pieces and the shard parts are the same cut -- gate|up -- and + * is not when they are not: the delta net's qkv|z is four shard parts and two rotated ones. */ int declare(RadBuilder* b, Names& nm, const Geom& g, const char* base, int64_t n_out, int64_t n_in, int shard, RadWeightGroup grp, std::initializer_list src, const ActFP8& a, rad_buf y, int64_t max_rows = 0, std::initializer_list row_parts = {}, - int64_t span_lo = 0, int64_t span_hi = 0); + int64_t span_lo = 0, int64_t span_hi = 0, + std::initializer_list rot_part_rows = {}); void step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, int64_t r0 = 0, int64_t rows = 0) const; /* A SPAN PER STACKED ROW PART (rad_weight_shard_span_parts), in the weight's rows; the scale @@ -797,6 +813,21 @@ inline bool linear_is_mxfp4(const RadEncoding& e) { sc->dtype == RAD_E8M0 && sc->block[0] == 1 && sc->block[1] == RAD_MX_GROUP; } +/* The ROTATED OCP-MXFP4 weight (ParoQuant): the same codes and E8M0 exponents, stored in the basis + * a "givens" transform names, with the rotation's tables as planes of their own (int32 column + * pairs, f32 angles, f32 channel scales, and for a fused projection one set a part). The GEMM + * reads it as the plain MXFP4 weight; the activation in front of it is rotated first. */ +inline bool linear_is_paro_mxfp4(const RadEncoding& e) { + if (!rad_enc_is(&e, "affine") || std::strncmp(e.transform, "givens", RAD_ENC_STR) != 0) return false; + if (!rad_enc_valid(&e) || e.n_planes < 5) return false; + const RadEncPlane* sc = rad_enc_plane(&e, "scale"); + const RadEncPlane *pr = rad_enc_plane(&e, "t.pairs"), *an = rad_enc_plane(&e, "t.angle"), + *rs = rad_enc_plane(&e, "t.scale"); + return e.plane[0].dtype == RAD_FP4E2M1 && e.plane[0].block[1] == 1 && sc && + sc->dtype == RAD_E8M0 && sc->block[0] == 1 && sc->block[1] == RAD_MX_GROUP && + pr && an && rs && pr->dtype == RAD_I32 && an->dtype == RAD_F32 && rs->dtype == RAD_F32; +} + inline int LinearFP8::probe(RadBuilder* b, Names& nm, const Geom& g, const char* base, std::initializer_list src) { if (src.size() == 0) return RAD_E_INVAL; @@ -808,7 +839,9 @@ inline int LinearFP8::probe(RadBuilder* b, Names& nm, const Geom& g, const char* const bool plain = known && rad_enc_is(&enc, "plain") && enc.n_planes == 1 && enc.plane[0].dtype == RAD_BF16; probed_i8 = known && linear_is_w8(enc); - reads_x = g.w_bf16 || plain || probed_i8; + /* A ROTATED weight rotates the bf16 plane itself: its quantiser is not the producer's. */ + probed_paro = known && linear_is_paro_mxfp4(enc); + reads_x = g.w_bf16 || plain || probed_i8 || probed_paro; return RAD_OK; } @@ -817,7 +850,8 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha std::initializer_list src, const ActFP8& a, rad_buf y, int64_t max_rows, std::initializer_list row_parts, - int64_t span_lo, int64_t span_hi) { + int64_t span_lo, int64_t span_hi, + std::initializer_list rot_part_rows) { N = n_out; K = n_in; if (N <= 0 || K <= 0 || src.size() == 0) return RAD_E_INVAL; @@ -941,9 +975,24 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha * the exponent plane as `b_scale`, and the same plane again as `b_ref`. Every view takes the * weight's row parts as they are -- the exponent grid is one row tall -- and a column slice in * whole 128s, which is the ACTIVATION's scale block and so the stricter of the two. */ - mx = known && linear_is_mxfp4(enc); + paro = known && linear_is_paro_mxfp4(enc); + mx = known && (linear_is_mxfp4(enc) || paro); if (mx) { - if (!a.q || !a.s) { + if (paro) { + /* THE ROTATION OF A ROTATED WEIGHT IS THE ACTIVATION'S, so this linear needs the input's + * bf16 plane (probe() set reads_x; the producer wrote it) and quantises it itself. */ + if (!a.x) { + fprintf(stderr, "radiance: '%s' is a rotated MXFP4 weight, rotated and quantised " + "from its input's bf16 plane, and its input has none\n", wn); + return RAD_E_DTYPE; + } + if (g.world > 1 || span_hi > 0) { + fprintf(stderr, "radiance: '%s' is a rotated MXFP4 weight; its rotation is per " + "projection and is not cut by a tensor-parallel shard (world %d)\n", + wn, g.world); + return RAD_E_UNSUPPORTED; + } + } else if (!a.q || !a.s) { fprintf(stderr, "radiance: '%s' is an MXFP4 weight read against its input's E4M3 " "pair, and its input has none\n", wn); return RAD_E_DTYPE; @@ -966,6 +1015,58 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha } parts[np++] = p; } + int64_t pb1 = 0, pb2 = 0; + if (paro) { + /* THE PARTS, in rows, and the tables as weights of their own. The container records + * how many parts there are and not where they end -- a declaration has no plane bytes + * -- so the block says: `rot_part_rows`, or its row parts when those are the cut. */ + const int64_t P = rad_enc_givens_parts(&enc); + const RadEncPlane* tp = rad_enc_plane(&enc, "t.pairs"); + if (P < 1 || !tp || tp->extent[0] % P || tp->extent[1] != K) { + fprintf(stderr, "radiance: '%s' has rotation tables that do not fit %lld columns\n", + wn, (long long)K); + return RAD_E_SHAPE; + } + const int64_t S = tp->extent[0] / P; + std::vector cut; + for (int64_t p : rot_part_rows) cut.push_back(p); + if (cut.empty()) for (int64_t p : row_parts) cut.push_back(p); + if (cut.empty() && P == 1) cut.push_back(N); + int64_t sum = 0; + for (int64_t p : cut) sum += p; + if ((int64_t)cut.size() != P || sum != N || P > 3) { + fprintf(stderr, "radiance: '%s' was rotated as %lld part(s) and is declared as %zu " + "rotated part(s) of %lld rows; the GEMM meets up to three copies " + "of the activation\n", wn, (long long)P, cut.size(), (long long)sum); + return RAD_E_SHAPE; + } + for (int64_t i = 0; i + 1 < P; ++i) if (cut[(size_t)i] % 128) { + fprintf(stderr, "radiance: '%s' rotation part of %lld rows is not whole 128s\n", + wn, (long long)cut[(size_t)i]); + return RAD_E_SHAPE; + } + rot_parts = P; + pb1 = P > 1 ? cut[0] : 0; + pb2 = P > 2 ? cut[0] + cut[1] : 0; + rot_pairs = decl_view(b, nm.f("%s.rot_pairs", base), wn, "t.pairs", RAD_I32, + { P * S, K }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp); + rot_angle = decl_view(b, nm.f("%s.rot_angle", base), wn, "t.angle", RAD_F32, + { P * S, K / 2 }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp); + rot_scale = decl_view(b, nm.f("%s.rot_scale", base), wn, "t.scale", RAD_F32, + { P, K }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp); + if (!rot_pairs || !rot_angle || !rot_scale) return RAD_E_INVAL; + const int64_t rows = max_rows > 0 ? max_rows : g.max_tok; + rot_q = decl_b(b, nm.f("%s.rot_q", base), RAD_F8E4M3, { rows, P * K }); + rot_s = decl_b(b, nm.f("%s.rot_s", base), RAD_F32, { rows, P * (K / RAD_FP8_BLOCK) }); + if (!rot_q || !rot_s) return RAD_E_INVAL; + op_rot = rw(b, RAD_OP(b, "givens_quant_act_fp8", + RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("n", K), + RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", g.dtype), + RAD_INT("stages", S), RAD_INT("parts", P)), + RAD_WEIGHTS(rot_pairs, rot_angle, rot_scale)), + { a.x }, { rot_q, rot_s }); + if (!op_rot) return RAD_E_INVAL; + } const char* sn = nm.f("%s.scale", base); const char* rn = nm.f("%s.ref", base); w = decl_view(b, wn, nullptr, "codes", RAD_FP4E2M1, { N, K }, RAD_ACCESS_PER_TOKEN, @@ -990,6 +1091,21 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha RAD_ARCH_TRY(rad_weight_shard_span(b, wref, lo, hi)); } const int64_t hi = max_rows > 0 ? max_rows : g.max_tok; + if (paro) { + op = rot_parts > 1 + ? 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", "mxfp4a8"), + RAD_INT("pb1", pb1), RAD_INT("pb2", pb2)), + RAD_WEIGHTS(w, ws, wref)), + { rot_q, rot_s }, { y }) + : 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", "mxfp4a8")), + RAD_WEIGHTS(w, ws, wref)), + { rot_q, rot_s }, { y }); + return op ? RAD_OK : RAD_E_INVAL; + } op = 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", "mxfp4a8")), @@ -1096,6 +1212,17 @@ inline void LinearFP8::step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, in RAD_W(w), RAD_W(ws), brow_slice(y, r0, rows, N), RAD_NONE, RAD_NONE); return; } + if (paro) { + /* The rotation first: this linear's own codes, `rot_parts` copies side by side. */ + RAD_ISSUE_N(c, op_rot, rows, brow_slice(a.x, r0, rows, K), RAD_W(rot_pairs), + RAD_W(rot_angle), RAD_W(rot_scale), + brow_slice(rot_q, r0, rows, rot_parts * K), + brow_slice(rot_s, r0, rows, rot_parts * (K / RAD_FP8_BLOCK))); + RAD_ISSUE_N(c, op, rows, brow_slice(rot_q, r0, rows, rot_parts * K), + brow_slice(rot_s, r0, rows, rot_parts * (K / RAD_FP8_BLOCK)), + RAD_W(w), RAD_W(ws), brow_slice(y, r0, rows, N), RAD_NONE, RAD_W(wref)); + return; + } if (mx) { RAD_ISSUE_N(c, op, rows, brow_slice(a.q, r0, rows, K), brow_slice(a.s, r0, rows, K / RAD_FP8_BLOCK), diff --git a/data/recipes/q38-27b-paro-mxfp4-df2.recipe b/data/recipes/q38-27b-paro-mxfp4-df2.recipe new file mode 100644 index 00000000..2059d764 --- /dev/null +++ b/data/recipes/q38-27b-paro-mxfp4-df2.recipe @@ -0,0 +1,38 @@ +# q38-27b-paro-mxfp4-df2.recipe -- a ParoQuant MXFP4 Qwen3.8-27B (quant_method paroquant_mxfp4) with the +# DFlash2 drafter. +# +# rad-convert --draft-model \ +# --recipe q38-27b-paro-mxfp4-df2.recipe -o q38-27b-paro-mxfp4-df2.rad +# +# The trunk's linears are imported as they ship, and need no rule: e2m1 codes and an E8M0 exponent a +# 32 of K -- Quark's planes byte for byte -- stored in the ROTATED basis ParoQuant trained them in, +# with the rotations beside them as the encoding's "givens" transform (abi/rad_encoding.h): per +# projection the Givens pairs and angles of 8 stages inside groups of 128 columns, and the channel +# scales. A fused gate|up or qkv|z keeps one set of tables a checkpoint tensor (parts). rad-convert +# writes them as planes of the weight; the GEMM is the plain MXFP4 one, and the activation is +# rotated in front of it (givens_quant_act_fp8) -- nothing is re-quantised, so the weights are the +# release's to the bit. +# +# What the checkpoint does NOT ship in a form the architecture declares gets a rule. The release is +# fp16 throughout (norms, the delta net's a|b and conv, the vision tower), where the plugin declares +# bf16: `cast` rounds f16 to bf16 once. Its embedding and head are fp8 with one f32 scale a ROW, +# read exactly and re-expressed: the embedding as bf16, the head as block fp8 like every other +# recipe here. Its MTP head is not read: the DFlash2 drafter displaces it. +# +# THE DRAFTER is made as small as its acceptance allows, as in q38-27b-quark-mxfp4-df2.recipe. + +blk.*.ssm_ab.weight cast dtype=bf16 +blk.*.ssm_conv1d.weight cast dtype=bf16 +token_embd.weight cast dtype=bf16 +v.* cast dtype=bf16 + +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 rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_q.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_k.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_v.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_output.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.ffn_gate_up.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.ffn_down.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 diff --git a/docs/GUIDE.md b/docs/GUIDE.md index f7e7a529..414d3e98 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -344,12 +344,13 @@ that points each name at its copy. A name two shards hold that no index entry se The conversions of record live in `scripts/convfn.sh` and `scripts/convdspark.sh`, with their recipes in `data/recipes/`. -**A release someone else already quantised is read as it ships.** Two forms are imported weight +**A release someone else already quantised is read as it ships.** Three forms are imported weight by weight, so their quantised linears need no recipe rule: | `quantization_config` | read as | | |---|---|---| | `quark`, MXFP4 weights (fp4, per_group 32, e8m0) | `fp4_e2m1*e8m0[1x32]` | byte for byte: the same codes and exponents. A checkpoint of this form is also served as it stands (ยง3.4) | +| `paroquant_mxfp4` (z-lab's ParoQuant on the MXFP4 grid: `format mxfp4`, `mxfp4_block 32`, `group_size 128`) | `fp4_e2m1*e8m0[1x32]/givens` | the codes and exponents byte for byte, plus the learned rotation as the encoding's `givens` tables: per checkpoint tensor the Givens pairs (made whole-row columns), angles and `1 / channel_scales`, a fused gate\|up or qkv\|z carrying one set a part. Convert-time only (the tables are built, not mapped); served by the rotating activation quantiser `givens_quant_act_fp8` and the ordinary MXFP4 GEMM (docs/OPS.md). Its fp8 heads and embedding (one f32 scale a row) are read exactly, for a recipe's `cast`/`rtn` rule | | `auto-round` with `packing_format: auto_round:auto_gptq`, and `gptq` (v1) | `i4*bf16[1x128]` or `i8*bf16[1x128]` | the GPTQ-packed codes transposed and made signed, the f16 scales rounded to bf16. Symmetric, group 128 or a multiple of it (per channel included), no act-order. Convert-time only, because the scales are not served as shipped | The width is read per tensor, so a release mixing int4 experts with int8 projections imports both. @@ -359,13 +360,17 @@ form, so it is kept in the container as `imported_quantization_config.*`, and th its family's quantised plugin (`qwen35_fp8` for Qwen3.8-27B; `qwen4exp_fp8` for Flash-Next). A weight the architecture declares in another form than the release holds still takes a rule: the -recipe dequantises the import exactly and quantises from that. The two recipes of record: +recipe dequantises the import exactly and quantises from that. The recipes of record: ```sh # AMD's Quark AWQ-MXFP4 Qwen3.8-27B with the DFlash2 drafter rad-convert ~/models/amd/Qwen3.8-27B-Quark-AWQ-MXFP4 --draft-model ~/models/z-lab/Qwen3.8-27B-DFlash2 \ --recipe data/recipes/q38-27b-quark-mxfp4-df2.recipe -o q38-27b-quark-mxfp4-df2.rad +# A ParoQuant MXFP4 Qwen3.8-27B (rotated MXFP4 trunk imported with its rotations; fp16 parts cast to bf16) +rad-convert ~/models/MisterSnrub/Qwen3.8-27B-PARO-MXFP4-v2 --draft-model ~/models/z-lab/Qwen3.8-27B-DFlash2 \ + --recipe data/recipes/q38-27b-paro-mxfp4-df2.recipe -o q38-27b-paro-mxfp4-df2.rad + # An AutoRound int4 Qwen3.8-Flash-Next: its experts imported, its int4 projections made int8 rad-convert ~/models/ukisai/Swift-1.5-Qwen3.8-Flash-Next-W4A16-AutoRound \ --recipe data/recipes/qwen4exp-autoround-i8-hc8m.recipe -o swift-autoround.rad