minimaxh3: fuse the int8 modulated-norm producer, dropping the bf16 plane

b025018's W8A8 DiT path unfused the norm: rms_mod wrote a bf16 plane and
each int8 linear re-quantised it. That bf16 plane grew the activation arena
14.41->15.54 GiB, which left vram_kv=0 and the server aborted at startup.

Mirror the E4M3 path instead. rms_mod_kernel gains a third quant mode (i8),
sharing r4d_q8.h's q8_top/q8_pack4 with the int8 quantiser, exposed as the
new rms_mod_quant_i8 op; the block's modulated norm now writes a_hm's int8
code pair directly (a_hm is q8_fed, so qkv/gate_up read the codes with no
per-linear quantiser) and a_hm carries no bf16 plane. ActFP8::cq()/cs()
resolve the pair by trunk format, so the norm producer and both step issues
collapse to one path. LinearFP8's int8 guard now demands the bf16 plane only
when it quantises itself, matching the fp8 branch, which never required it.

Arena is back to fp8 parity (243 buffers, vram_kv 938 MiB); the container
serves at TP2 and renders all five task types device-clean.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
gearwave00001andClaude committed 2026-10-20 10:40:03 -05:00
1 parent cbbab03183
commit 0fe7771210
5 files changed
+82 -85

No files matched your search

+30 -58
View File
@@ -450,7 +450,7 @@ struct Model {
rad_buf b_v8 = 0, b_vs = 0; /* its values at E4M3 codes and their column scales */
/* The trained schedule's base times (a few-step checkpoint's), or none. */
std::vector<double> rungs;
rad_buf b_h = 0, b_hm = 0, b_qkv = 0, b_ao = 0, b_gu = 0, b_hn = 0, b_hf = 0, b_vel = 0,
rad_buf b_h = 0, b_qkv = 0, b_ao = 0, b_gu = 0, b_hn = 0, b_hf = 0, b_vel = 0,
b_avel = 0;
ActFP8 a_hm{}, a_attn{}, a_ffn{};
/* The DiT's trunk linears are int8 (a W8A8 container). Learned by probing one linear before
@@ -1141,19 +1141,13 @@ static int declare_dit(RadBuilder* b, const RadModelMeta* meta, Model& m) {
/* Learn the trunk's weight format from one linear before declaring the activation code planes.
* An int8 (W8A8) denoiser quantises its per-token activations to int8 too -- E4M3's three
* mantissa bits band the smooth activations that carry detail, and its 448 clamp clips
* highlights -- so its producers write a bf16 plane and each int8 linear quantises that plane
* into its own codes (LinearFP8::i8's op_q). Probe block 0's down projection: a single-source
* linear whose name map declare() will not re-make. */
* highlights. The int8 path mirrors the E4M3 one exactly: the fused modulated-norm producer
* writes the int8 codes and scale (`rms_mod_quant_i8`, fed into `a_hm` via its q8 pair) and each
* int8 linear reads those codes directly, with no bf16 plane and no per-linear quantiser. Probe
* block 0's down projection: a single-source linear whose name map declare() will not re-make. */
RAD_ARCH_TRY(m.dit[0].down.probe(b, nm, g, nm.f("dit.blk.0.ffn_down"),
{ nm.ckpt("transformer.transformer_blocks.0.ff.net.2.weight") }));
m.dit_i8 = m.dit[0].down.probed_i8;
if (m.dit_i8) {
/* The bf16 modulated-norm plane the producers write for the int8 trunk; the E4M3 pair is
* never read, so a_hm is declared without it below. */
m.b_hm = decl_b(b, nm.f("dit.hmb"), RAD_BF16, { T, D });
if (!m.b_hm) return RAD_E_INVAL;
m.a_hm.x = m.b_hm;
}
m.op_text = rw(b, RAD_OP(b, "lane_gather",
RAD_PARAMS(RAD_RANGE("M", 1, (int64_t)kMaxText), RAD_INT("n", D),
RAD_INT("stride", (int64_t)kMaxText),
@@ -1171,6 +1165,10 @@ static int declare_dit(RadBuilder* b, const RadModelMeta* meta, Model& m) {
/* ---- the blocks */
RAD_ARCH_TRY(m.a_hm.declare_qs(b, nm, g, "dit.hm", D, 0, m.dit_i8, !m.dit_i8));
/* The fused `rms_mod_quant*` producer writes a_hm's code pair (int8 here), and qkv / gate_up
* read it directly -- so it is `q8_fed` and declares no quantiser of its own, exactly as its
* E4M3 sibling is fed by `rms_mod_quant_fp8`. */
m.a_hm.q8_fed = m.dit_i8;
/* With video sparse attention a fourth projection beside q, k and v gates the pooled-tile
* attention into each row's output. */
const int64_t nparts = m.vsa ? 4 : 3;
@@ -1197,36 +1195,22 @@ static int declare_dit(RadBuilder* b, const RadModelMeta* meta, Model& m) {
const char* sections = nm.f("%lld %lld %lld", (long long)rf, (long long)rf, (long long)rf);
const int64_t ff_all = g.n_ff_all;
/* The block's modulated norm of `b_h`. For the E4M3 container it is fused with the quantiser:
* `rms_mod_quant_fp8` folds the previous block's all-reduced delta in, renormalises with the
* timestep/segment modulation, and writes `a_hm`'s E4M3 codes plus the updated residual. For the
* int8 trunk the quantiser is the whole point of the change -- E4M3 banded these activations --
* so it comes apart: plain `rms_mod` (no `group` param) writes only the bf16 plane `b_hm`, and
* each int8 linear reading `a_hm` (qkv, gate_up) quantises that plane into its own int8 codes. */
/* The block's modulated norm of `b_h`, fused with the quantiser: it folds the previous block's
* all-reduced delta in, renormalises with the timestep/segment modulation, and writes `a_hm`'s
* code pair plus the updated residual. Both trunks take the same path; only the op -- and hence
* the code dtype -- differs (`a_hm.cq()` / `cs()` resolve to the int8 pair for a W8A8 trunk, the
* E4M3 pair otherwise), and the two schemas share a shape and a group of 128. */
auto mod_q = [&](rad_weight w, bool gated) {
rad_op op;
if (m.dit_i8) {
op = RAD_OP(b, "rms_mod",
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n", D), RAD_F64("eps", eps),
RAD_INT("gated", gated ? 1 : 0), RAD_STR("dtype", "bf16")),
RAD_WEIGHTS(w));
} else {
op = RAD_OP(b, "rms_mod_quant_fp8",
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n", D), RAD_F64("eps", eps),
RAD_INT("group", RAD_FP8_BLOCK),
RAD_INT("gated", gated ? 1 : 0), RAD_STR("dtype", "bf16")),
RAD_WEIGHTS(w));
}
rad_op op = RAD_OP(b, m.dit_i8 ? "rms_mod_quant_i8" : "rms_mod_quant_fp8",
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n", D), RAD_F64("eps", eps),
RAD_INT("group", RAD_FP8_BLOCK),
RAD_INT("gated", gated ? 1 : 0), RAD_STR("dtype", "bf16")),
RAD_WEIGHTS(w));
if (!op) return op;
rad_buf rd[3] = { m.b_modg, m.b_h, m.b_ao };
rad_op_reads(b, op, rd, gated ? 3 : 2);
if (m.dit_i8) {
rad_buf wr[2] = { m.b_hm, m.b_h };
rad_op_writes(b, op, wr, 2);
} else {
rad_buf wr[3] = { m.a_hm.q, m.a_hm.s, m.b_h };
rad_op_writes(b, op, wr, 3);
}
rad_buf wr[3] = { m.a_hm.cq(), m.a_hm.cs(), m.b_h };
rad_op_writes(b, op, wr, 3);
return op;
};
@@ -3475,18 +3459,11 @@ static void step_dit(RadCtx* c, const RadBatch* batch, const Model& m) {
for (int h = 0; h < nh; ++h) {
const int64_t r0 = hr0[h], n = hn[h];
if (h > 0 && l > 0) wait_ar();
if (m.dit_i8)
/* rms_mod: z (the bf16 plane the int8 qkv quantises) bound, no q/scale. */
RAD_ISSUE_N(c, B.op_mq1, n, l > 0 ? brow_slice(m.b_ao, r0, n, D) : RAD_NONE,
l > 0 ? modh(h, l - 1, 5, 1) : RAD_NONE, modh(h, l, 0, 2),
RAD_W(B.w_n1), segh[h], brow_slice(m.b_h, r0, n, D),
brow_slice(m.b_hm, r0, n, D));
else
RAD_ISSUE_N(c, B.op_mq1, n, l > 0 ? brow_slice(m.b_ao, r0, n, D) : RAD_NONE,
l > 0 ? modh(h, l - 1, 5, 1) : RAD_NONE, modh(h, l, 0, 2),
RAD_W(B.w_n1), segh[h], brow_slice(m.b_h, r0, n, D),
brow_slice(m.a_hm.q, r0, n, D), brow_slice(m.a_hm.s, r0, n, nD),
RAD_NONE);
RAD_ISSUE_N(c, B.op_mq1, n, l > 0 ? brow_slice(m.b_ao, r0, n, D) : RAD_NONE,
l > 0 ? modh(h, l - 1, 5, 1) : RAD_NONE, modh(h, l, 0, 2),
RAD_W(B.w_n1), segh[h], brow_slice(m.b_h, r0, n, D),
brow_slice(m.a_hm.cq(), r0, n, D), brow_slice(m.a_hm.cs(), r0, n, nD),
RAD_NONE);
B.qkv.step(c, m.a_hm, m.b_qkv, R, r0, n);
}
RAD_ISSUE_N(c, B.op_qkr, R, bcol(m.b_qkv, 0, 3 * qd, R), pos, RAD_W(B.w_qn),
@@ -3517,15 +3494,10 @@ static void step_dit(RadCtx* c, const RadBatch* batch, const Model& m) {
}
for (int h = 0; h < nh; ++h) {
const int64_t r0 = hr0[h], n = hn[h];
if (m.dit_i8)
RAD_ISSUE_N(c, B.op_mq2, n, brow_slice(m.b_ao, r0, n, D), modh(h, l, 2, 1),
modh(h, l, 3, 2), RAD_W(B.w_n2), segh[h], brow_slice(m.b_h, r0, n, D),
brow_slice(m.b_hm, r0, n, D));
else
RAD_ISSUE_N(c, B.op_mq2, n, brow_slice(m.b_ao, r0, n, D), modh(h, l, 2, 1),
modh(h, l, 3, 2), RAD_W(B.w_n2), segh[h], brow_slice(m.b_h, r0, n, D),
brow_slice(m.a_hm.q, r0, n, D), brow_slice(m.a_hm.s, r0, n, nD),
RAD_NONE);
RAD_ISSUE_N(c, B.op_mq2, n, brow_slice(m.b_ao, r0, n, D), modh(h, l, 2, 1),
modh(h, l, 3, 2), RAD_W(B.w_n2), segh[h], brow_slice(m.b_h, r0, n, D),
brow_slice(m.a_hm.cq(), r0, n, D), brow_slice(m.a_hm.cs(), r0, n, nD),
RAD_NONE);
B.gate_up.step(c, m.a_hm, m.b_gu, R, r0, n);
B.gq.step(c, m.b_gu, R, r0, n);
B.down.step(c, m.a_ffn, m.b_ao, R, r0, n);