qwen35_w8a8: the int8 linears take their rotated codes from the norm and silu producers

- ActFP8::q8_rot marks fed codes as rotated. NormQuantFP8 declares (ar_)rmsnorm_had_quant_i8g and
  GateQuantFP8 declares gated_had_quant_i8g when an activation is fed and rotated. LinearFP8 takes
  fed codes on an i8a8h weight only if their rotation is the weight's.
- The MLP no longer writes the bf16 silu product when the down projection is fed.
- qwen35_w8a8 feeds the normed input and the FFN activation only where libr4d declares the ops
  (at TP2 the all-reduce form too), so the CPU graphs are unchanged.

A decode step's had_quant_act_i8g launches fall from 288 to 64; the 64 left quantise the attention
and delta-net outputs. The codes are the same bytes. On 2x R9700, against 1.9.0 with the RTN
container:
- KL rows are identical, and greedy answers are byte-identical at TP2 and TP1, with acceptance
  unchanged.
- TP2: 33.98 -> 32.87 ms a decode step, 111.2 -> 114.9 tok/s.
- TP1: 55.81 -> 54.59 ms, 62.7 -> 64.1 tok/s.
- Prefill: +2%.

arch_test pins the unfused chain where the ops are undeclared, and the fed path where they are
(had_quant_act_i8g 8, rmsnorm_had_quant_i8g 16, gated_had_quant_i8g 8). The every-op-declared graph
hashes are re-pinned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
eddie168168andClaude Opus 5.5 authored and StillDeadcode committed 2026-10-21 09:25:39 +02:00
1 parent d999524054
commit e28e2d89f8
4 files changed
+91 -25

No files matched your search

+5 -3
View File
@@ -96,12 +96,14 @@ inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, co
{ src.gate, src.up }, w.h, w.gate_up, 0, { g.n_ff, g.n_ff }));
/* The bf16 silu product is written only if the down projection reads the bf16 plane
* (LinearFP8::reads_x); a block-fp8 down_proj reads only codes and scales. */
* (LinearFP8::reads_x); a block-fp8 down_proj reads only codes and scales, and so does an
* int8 one fed its codes (ActFP8::q8_fed). */
const char* down_base = nm.f("blk.%d.ffn_down", l);
RAD_ARCH_TRY(down.probe(b, nm, g, down_base, { src.down }));
gq_ffn.fuse_clamped = swiglu_quant;
RAD_ARCH_TRY(gq_ffn.declare(b, g, w.gate_up, w.ffn, g.n_ff, "silu", 0, down.reads_x,
false, swiglu_limit));
RAD_ARCH_TRY(gq_ffn.declare(b, g, w.gate_up, w.ffn, g.n_ff, "silu", 0,
down.reads_x && !(w.ffn.q8_fed && down.probed_i8), false,
swiglu_limit));
/* The fold (decl_gated_fold), an fp8a8 tile, so bf16, f32-scaled, MXFP4, rotated 5-bit, 4-bit
* and int8 gate_up weights take the pair, and so does a clamped (swiglu_limit) product.
+34 -15
View File
@@ -68,6 +68,10 @@ struct ActFP8 {
* epilogue), so an int8 linear reading it declares no quantiser. Set by the model when it
* declares the pair, before the wires are copied. */
bool q8_fed = false;
/* With q8_fed: the producer's int8 codes are rotated, had_quant_act_i8g's (a `_had_quant_i8g`
* producer), so only a weight stored rotated (LinearFP8 at i8a8h) reads them, and it declares
* no quantiser of its own. Set by the model with q8_fed. */
bool q8_rot = false;
/* The code pair the producer writes: int8 when the model fed it, E4M3 otherwise. */
rad_buf cq() const { return q8_fed ? q8 : q; }
@@ -368,6 +372,8 @@ struct NormQuantFP8 {
* (the T * n_embd * 2-byte message then crosses --tp-wire-min-kb). The wire is a declare-time
* parameter, hence a separate op. */
rad_op op_arn6 = 0;
/* The ops write had_quant_act_i8g's rotated int8 codes into the int8 pair (ActFP8::q8_rot). */
bool hi8 = false;
Geom gm{};
/* The row width, for turning a row slice into element offsets. */
int64_t n = 0;
@@ -401,8 +407,12 @@ inline int NormQuantFP8::declare(RadBuilder* b, const Geom& g, rad_weight gain,
"linears are bf16; its blocks take a prepared input instead\n");
return RAD_E_UNSUPPORTED;
}
if (!src || !a.x || !a.q || !a.s || !w || n <= 0) return RAD_E_INVAL;
op = RAD_OP(b, "rmsnorm_quant_fp8",
/* Rotated int8 codes (ActFP8::q8_rot) come from the `_had_quant_i8g` twins, into the int8
* pair; the model checked they are declared before feeding. Otherwise E4M3, as always. */
hi8 = a.q8_fed && a.q8_rot;
const rad_buf cq = hi8 ? a.q8 : a.q, cs = hi8 ? a.s8 : a.s;
if (!src || !a.x || !cq || !cs || !w || n <= 0) return RAD_E_INVAL;
op = RAD_OP(b, hi8 ? "rmsnorm_had_quant_i8g" : "rmsnorm_quant_fp8",
RAD_PARAMS(RAD_RANGE("M", 1, max_rows > 0 ? max_rows : g.max_tok),
RAD_INT("n", n), RAD_F64("eps", g.eps),
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", g.dtype),
@@ -413,7 +423,7 @@ inline int NormQuantFP8::declare(RadBuilder* b, const Geom& g, rad_weight gain,
* lifetime-extended. `residual` is INOUT, so it is in both sets; otherwise the planner could
* reorder a reader around it. */
rad_buf rd[2] = { src, res };
rad_buf wr[4] = { a.q, a.s, a.x, res };
rad_buf wr[4] = { cq, cs, a.x, res };
rad_op_reads (b, op, rd, res ? 2 : 1);
rad_op_writes(b, op, wr, res ? 4 : 3);
@@ -425,7 +435,7 @@ inline int NormQuantFP8::declare(RadBuilder* b, const Geom& g, rad_weight gain,
* with `x` the stream and no residual. The kernel supports it (r4d_fused_quant_fp8.hip) and the
* schema marks `residual` optional. */
if (fuse_ar && g.world == 2) {
op_arn = RAD_OP(b, "ar_rmsnorm_quant_fp8",
op_arn = RAD_OP(b, hi8 ? "ar_rmsnorm_had_quant_i8g" : "ar_rmsnorm_quant_fp8",
RAD_PARAMS(RAD_RANGE("M", 1, max_rows > 0 ? max_rows : g.max_tok),
RAD_INT("n", n), RAD_F64("eps", g.eps),
RAD_INT("group", RAD_FP8_BLOCK),
@@ -433,14 +443,14 @@ inline int NormQuantFP8::declare(RadBuilder* b, const Geom& g, rad_weight gain,
RAD_STR("dtype", g.dtype), RAD_F64("wadd", g.wadd)),
RAD_WEIGHTS(w));
if (!op_arn) return RAD_E_INVAL;
rad_buf wr_ar[4] = { a.q, a.s, a.x, res ? res : src };
rad_buf wr_ar[4] = { cq, cs, a.x, res ? res : src };
rad_op_reads (b, op_arn, rd, res ? 2 : 1);
rad_op_writes(b, op_arn, wr_ar, 4);
/* Only where the compressed wire is reachable and servable: a lossy payload accepted, and
* the width tiled by the chunk (otherwise a fatal step-time shape refusal). */
if (fuse_ar != kArExact && !g.wire_exact && (n % kArWire6Chunk) == 0) {
op_arn6 = RAD_OP(b, "ar_rmsnorm_quant_fp8",
op_arn6 = RAD_OP(b, hi8 ? "ar_rmsnorm_had_quant_i8g" : "ar_rmsnorm_quant_fp8",
RAD_PARAMS(RAD_RANGE("M", 1, max_rows > 0 ? max_rows : g.max_tok),
RAD_INT("n", n), RAD_F64("eps", g.eps),
RAD_INT("group", RAD_FP8_BLOCK),
@@ -464,8 +474,8 @@ inline void NormQuantFP8::step(RadCtx* c, int64_t T, int64_t r0, int64_t rows) c
if (rows <= 0) rows = T - r0;
RAD_ISSUE_N(c, pick(rows), rows, brow_slice(src, r0, rows, n),
res ? brow_slice(res, r0, rows, n) : RAD_NONE, RAD_W(w),
brow_slice(a.q, r0, rows, n),
brow_slice(a.s, r0, rows, n / RAD_FP8_BLOCK),
brow_slice(hi8 ? a.q8 : a.q, r0, rows, n),
brow_slice(hi8 ? a.s8 : a.s, r0, rows, n / RAD_FP8_BLOCK),
brow_slice(a.x, r0, rows, n));
}
@@ -521,7 +531,10 @@ inline int GateQuantFP8::declare(RadBuilder* b, const Geom& g, rad_buf gate_up,
clamped_fused = false;
if (!std::isfinite(limit) || limit < 0) return RAD_E_INVAL;
if (clipped) {
if (!gate_up || !a.x || n <= 0 || rot || std::strcmp(act_name, "silu")) return RAD_E_INVAL;
/* No rotated int8 form: its quantiser would be quant_act_i8g, unrotated. */
if (!gate_up || !a.x || n <= 0 || rot || (a.q8_fed && a.q8_rot) ||
std::strcmp(act_name, "silu"))
return RAD_E_INVAL;
const int64_t rows = max_rows > 0 ? max_rows : g.max_tok;
if (fuse_clamped && !bf16 && !a.q8_fed && a.cq() && a.cs() &&
rad_op_declared(b, "swiglu_clamped_quant_fp8")) {
@@ -575,9 +588,13 @@ inline int GateQuantFP8::declare(RadBuilder* b, const Geom& g, rad_buf gate_up,
if (!gate_up || !a.x || !a.cq() || !a.cs() || n <= 0) return RAD_E_INVAL;
/* Rotated codes feed a four-bit expert, which reads E4M3 only. */
if (rot && a.q8_fed) return RAD_E_INVAL;
/* Rotated int8 codes (ActFP8::q8_rot) feed a down projection stored at i8a8h:
* gated_had_quant_i8g, which the model checked is declared before feeding. */
const bool hi8 = a.q8_fed && a.q8_rot;
const int64_t rows = max_rows > 0 ? max_rows : g.max_tok;
const char* gq = hi8 ? "gated_had_quant_i8g" : rot ? "gated_had_quant_fp8" : "gated_quant_fp8";
if (!rot || rad_op_declared(b, "gated_had_quant_fp8"))
op = RAD_OP(b, rot ? "gated_had_quant_fp8" : "gated_quant_fp8",
op = RAD_OP(b, gq,
RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("n", n),
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", g.dtype),
RAD_STR("act", act_name)),
@@ -1064,14 +1081,16 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
* of 128. The codes come from the producer (ActFP8::q8_fed) or from op_q over `a.x` (probe()
* set reads_x). The model must have declared the pair (ActFP8::declare_qs's `i8`).
*
* A rotated weight (i8a8h) takes had_quant_act_i8g instead, so it always quantises its own
* input: a producer's codes are unrotated. */
* A rotated weight (i8a8h) takes had_quant_act_i8g instead. It reads a producer's codes only
* when they are rotated too (ActFP8::q8_rot, a `_had_quant_i8g` producer); a plain int8 weight
* only when they are not. */
const bool rot8 = known && linear_is_w8h(enc);
i8 = known && (linear_is_w8(enc) || rot8);
if (i8) {
if (rot8 && a.q8_fed) {
fprintf(stderr, "radiance: '%s' is a rotated int8 weight and its input is fed codes "
"its producer wrote unrotated\n", wn);
if (a.q8_fed && rot8 != a.q8_rot) {
fprintf(stderr, "radiance: '%s' is a%s int8 weight and its input is fed codes its "
"producer wrote %s\n", wn, rot8 ? " rotated" : " plain",
rot8 ? "unrotated" : "rotated");
return RAD_E_DTYPE;
}
if (!a.x || !a.q8 || !a.s8) {
+14
View File
@@ -750,6 +750,20 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
RAD_ARCH_TRY(m.a_ffn.declare_qs(b, nm, g, "ffn", g.n_ff, 0, m.trunk_i8));
#endif
#if QWEN35_W8A8
/* ---- the rotated trunk's quantisers in their producers. Every reader of `h`'s codes is a
* rotated int8 projection (q, k, v; the delta net's qkv|z; gate_up), and of `ffn`'s the down
* projection, so where libr4d serves the `_had_quant_i8g` forms the norms and the gated
* product write had_quant_act_i8g's codes themselves (ActFP8::q8_rot) and those projections
* declare no quantiser: one launch for q, k and v instead of three, and none before down. The
* codes are the same bytes. Where the forms are not declared (the CPU kernels), each
* projection quantises its own input from the bf16 plane, as before. */
if (rad_op_declared(b, "rmsnorm_had_quant_i8g") &&
(g.world != 2 || rad_op_declared(b, "ar_rmsnorm_had_quant_i8g")))
m.a_h.q8_fed = m.a_h.q8_rot = true;
if (rad_op_declared(b, "gated_had_quant_i8g")) m.a_ffn.q8_fed = m.a_ffn.q8_rot = true;
#endif
m.b_hout = decl_b(b, nm.f("h_out"), g.act_dtype, {g.max_logit_rows, g.n_embd});
/* The auxiliary taps, only when the container carries the drafter and the deployment
+38 -7
View File
@@ -7675,15 +7675,21 @@ int w8a8_declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* ctx
} /* namespace */
/* Every trunk linear of a rotated int8 container is an i8a8h GEMM right behind its own
* had_quant_act_i8g, at one rank and two: 2 attention layers of four linears, 6 delta-net layers
* of two and 8 feed-forwards of two. None is left at fp8, which is what a linear that missed the
* encoding falls back to, and the MLP declares no gated fold (an fp8a8 tile over E4M3 codes). The
* four trunk activations carry an int8 pair; the delta net's `gdn_in` feeds no int8 linear. */
/* The `_had_quant_i8g` producers, which libr4d serves and libref does not. */
const char* const kW8a8Fused[] = { "rmsnorm_had_quant_i8g", "ar_rmsnorm_had_quant_i8g",
"gated_had_quant_i8g" };
/* Where the `_had_quant_i8g` producers are not served, every trunk linear of a rotated int8
* container is an i8a8h GEMM right behind its own had_quant_act_i8g, at one rank and two: 2
* attention layers of four linears, 6 delta-net layers of two and 8 feed-forwards of two. None is
* left at fp8, which is what a linear that missed the encoding falls back to, and the MLP declares
* no gated fold (an fp8a8 tile over E4M3 codes). The four trunk activations carry an int8 pair;
* the delta net's `gdn_in` feeds no int8 linear. */
TEST(qwen35_w8a8_serves_every_trunk_linear_rotated_int8) {
MetaKV mk = q35w8a8_meta();
for (int world : { 1, 2 }) {
RadBuilder b;
for (const char* op : kW8a8Fused) b.undeclared.insert(op);
w8a8_encs(&b);
RadBuildCtx ctx = build_ctx(0, world);
REQUIRE_EQ(qwen35_w8a8::declare(&b, mk.meta(), &ctx), RAD_OK);
@@ -7706,6 +7712,31 @@ TEST(qwen35_w8a8_serves_every_trunk_linear_rotated_int8) {
}
}
/* Where they are served, the norms and the gated product write had_quant_act_i8g's codes into the
* int8 pair, and the projections reading them declare no quantiser: had_quant_act_i8g is left only
* before the attention and delta-net output projections (2 + 6), every norm is the rotated twin (at
* two ranks with its fused all-reduce form beside it), and every feed-forward's product is
* gated_had_quant_i8g. No E4M3 norm or gated quantiser is left, and the 36 GEMMs are unchanged. */
TEST(qwen35_w8a8_feeds_its_rotated_codes_from_the_producers_where_libr4d_serves_them) {
MetaKV mk = q35w8a8_meta();
for (int world : { 1, 2 }) {
RadBuilder b;
w8a8_encs(&b);
RadBuildCtx ctx = build_ctx(0, world);
REQUIRE_EQ(qwen35_w8a8::declare(&b, mk.meta(), &ctx), RAD_OK);
int rot = 0;
for (const RecOp& o : b.ops) rot += o.op == "gemm_nt_q" && pstr(&o, "dtype") == "i8a8h";
CHECK_EQ(rot, 2 * 4 + 6 * 2 + 8 * 2);
CHECK_EQ(count_op(b, "had_quant_act_i8g"), 2 + 6);
CHECK_EQ(count_op(b, "rmsnorm_had_quant_i8g"), 8 + 8);
CHECK_EQ(count_op(b, "gated_had_quant_i8g"), 8);
for (const char* op : { "rmsnorm_quant_fp8", "ar_rmsnorm_quant_fp8", "gated_quant_fp8",
"quant_act_i8g" })
CHECK_EQ(count_op(b, op), 0);
CHECK_EQ(count_op(b, "ar_rmsnorm_had_quant_i8g") > 0, world == 2);
}
}
/* What the int8 build refuses: a trunk that is not int8 (no stated encoding, which a block-fp8
* plugin reads as fp8), a plain int8 trunk (the descriptor names the rotation, and a plain weight
* served behind the rotated quantiser would be wrong), and a container stated for another format
@@ -8994,10 +9025,10 @@ struct ArchCases {
qwen35_fp8::step, 7, {}, 0xb5691456a0ce7e9dull);
add("qwen35_w8a8 + DFlash2", [this, sc](RadBuilder* b) {
auto c = sc(1, 7); return w8a8_declare(b, q35w8df.meta(), &c); },
qwen35_w8a8::step, 7, {}, 0x88e964c824896422ull);
qwen35_w8a8::step, 7, {}, 0xed8219da9fcc0ceaull);
add("qwen35_w8a8 + DFlash2, two ranks", [this, sc](RadBuilder* b) {
auto c = sc(2, 7); return w8a8_declare(b, q35w8df.meta(), &c); },
nullptr, 7, { "ar_rmsnorm_quant_fp8" }, 0x7c73e987c475e40dull);
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);