diff --git a/arch/common/rad_fp8.h b/arch/common/rad_fp8.h index fe5058a2..42edfa39 100644 --- a/arch/common/rad_fp8.h +++ b/arch/common/rad_fp8.h @@ -78,20 +78,26 @@ struct ActFP8 { * `rows` defaults to g.max_tok and must be given for any buffer that is not one row per token * (a MoE expert plane is `rows * top_k`, an encoder tower's its patch count); too small a value * overruns the arena (core/runtime/issue.cpp refuses such an issue). */ + /* `i8` declares the int8 pair; `fp8_pair` the E4M3 one. A trunk read entirely by int8 linears + * (a W8A8 model's) passes i8 with fp8_pair false: no consumer reads the E4M3 codes, and an + * unused plane still takes arena the KV cache needs. */ int declare_qs(RadBuilder* b, Names& nm, const Geom& g, const char* name, int64_t n, - int64_t rows = 0, bool i8 = false) { + int64_t rows = 0, bool i8 = false, bool fp8_pair = true) { /* bf16 linears read only `x` (Geom::w_bf16), so declare no codes; an unused plane would * still take arena space. */ if (g.w_bf16) { q = s = q8 = s8 = 0; return RAD_OK; } const int64_t m = rows > 0 ? rows : g.max_tok; - q = decl_b(b, nm.f("%s.q", name), RAD_F8E4M3, { m, n }); - s = decl_b(b, nm.f("%s.s", name), RAD_F32, { m, fp8_blocks(n) }); + if (fp8_pair) { + q = decl_b(b, nm.f("%s.q", name), RAD_F8E4M3, { m, n }); + s = decl_b(b, nm.f("%s.s", name), RAD_F32, { m, fp8_blocks(n) }); + if (!q || !s) return RAD_E_INVAL; + } if (i8) { q8 = decl_b(b, nm.f("%s.q8", name), RAD_I8, { m, n }); s8 = decl_b(b, nm.f("%s.s8", name), RAD_F32, { m, fp8_blocks(n) }); if (!q8 || !s8) return RAD_E_INVAL; } - return (q && s) ? RAD_OK : RAD_E_INVAL; + return (fp8_pair || i8) ? RAD_OK : RAD_E_INVAL; } }; @@ -489,9 +495,12 @@ struct GateQuantFP8 { * codes (`op_q`). */ rad_op op_q = 0; + /* `silu_only` takes the `silu_mul`-into-bf16 path regardless of `g.w_bf16`: an int8 trunk whose + * down projection quantises its own input (LinearFP8::i8's op_q) needs only the bf16 product, + * and `gated_quant_fp8` cannot supply it alone (its code and scale outputs are mandatory). */ int declare(RadBuilder* b, const Geom& g, rad_buf gate_up, const ActFP8& act, int64_t n, const char* act_name = "silu", int64_t max_rows = 0, bool bf16_out = true, - bool rot = false); + bool rot = false, bool silu_only = false); void step(RadCtx* c, rad_buf gate_up, int64_t T, int64_t r0 = 0, int64_t rows = 0) const; }; @@ -500,13 +509,13 @@ struct GateQuantFP8 { * bf16 output is unrotated either way. */ inline int GateQuantFP8::declare(RadBuilder* b, const Geom& g, rad_buf gate_up, const ActFP8& act, int64_t n, const char* act_name, int64_t max_rows, - bool bf16_out, bool rot) { + bool bf16_out, bool rot, bool silu_only) { a = act; want_x = bf16_out; this->n = n; - /* bf16 linears read the product itself: `silu_mul` into the bf16 plane only, and no - * rotation. */ - bf16 = g.w_bf16; + /* bf16 linears read the product itself, and an int8 trunk's down projection quantises its own + * input (`silu_only`): `silu_mul` into the bf16 plane only, and no rotation. */ + bf16 = g.w_bf16 || silu_only; if (bf16) { if (!gate_up || !a.x || n <= 0 || rot || std::strcmp(act_name, "silu") != 0) { fprintf(stderr, "radiance: a bf16 gate is silu over [gate|up] into a bf16 plane; " diff --git a/arch/minimaxh3/minimaxh3.cpp b/arch/minimaxh3/minimaxh3.cpp index 71782ac4..cadba8dc 100644 --- a/arch/minimaxh3/minimaxh3.cpp +++ b/arch/minimaxh3/minimaxh3.cpp @@ -450,8 +450,13 @@ 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 rungs; - 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; + 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, + 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 + * the activation code planes are declared; switches the trunk's producers from the fused E4M3 + * quantisers to bf16 planes each int8 linear quantises itself (LinearFP8::i8). */ + bool dit_i8 = false; std::vector dit; rad_weight w_no_n = 0, w_vout = 0, b_vout = 0, w_aout = 0, b_aout = 0; rad_op op_nout = 0, op_hcast = 0, op_vout = 0, op_aout = 0, op_ev = 0, op_ea = 0; @@ -1133,6 +1138,22 @@ static int declare_dit(RadBuilder* b, const RadModelMeta* meta, Model& m) { /* ---- the packed sequence: the lane's prompt rows, the soundtrack's and the video's latents * through their f32 projections, rounded to bf16 as the reference packs them */ m.b_h = decl_b(b, nm.f("dit.h"), RAD_BF16, { T, D }); + /* 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. */ + 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), @@ -1149,7 +1170,7 @@ static int declare_dit(RadBuilder* b, const RadModelMeta* meta, Model& m) { return RAD_E_INVAL; /* ---- the blocks */ - RAD_ARCH_TRY(m.a_hm.declare_qs(b, nm, g, "dit.hm", D)); + RAD_ARCH_TRY(m.a_hm.declare_qs(b, nm, g, "dit.hm", D, 0, m.dit_i8, !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; @@ -1163,27 +1184,49 @@ static int declare_dit(RadBuilder* b, const RadModelMeta* meta, Model& m) { m.b_v8 = decl_b(b, nm.f("dit.v8"), RAD_F8E4M3, { T, qd }); m.b_vs = decl_b(b, nm.f("dit.vs"), RAD_F32, { qd }); m.a_attn.x = decl_b(b, nm.f("dit.attn"), RAD_BF16, { T, qd }); - RAD_ARCH_TRY(m.a_attn.declare_qs(b, nm, g, "dit.attn", qd)); + RAD_ARCH_TRY(m.a_attn.declare_qs(b, nm, g, "dit.attn", qd, 0, m.dit_i8, !m.dit_i8)); + /* For an int8 trunk the standalone quantiser (q_attn) writes int8 codes and o_proj reads them + * directly (q8_fed), so o declares no quantiser of its own. */ + m.a_attn.q8_fed = m.dit_i8; m.b_ao = decl_b(b, nm.f("dit.ao"), RAD_BF16, { T, D }); /* More than one rank: the all-reduces of b_ao's halves run on the second lane (step_dit). */ if (g.world > 1) RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_ao)); m.b_gu = decl_b(b, nm.f("dit.gate_up"), RAD_BF16, { T, 2 * g.n_ff }); m.a_ffn.x = decl_b(b, nm.f("dit.ffn"), RAD_BF16, { T, g.n_ff }); - RAD_ARCH_TRY(m.a_ffn.declare_qs(b, nm, g, "dit.ffn", g.n_ff)); + RAD_ARCH_TRY(m.a_ffn.declare_qs(b, nm, g, "dit.ffn", g.n_ff, 0, m.dit_i8, !m.dit_i8)); 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. */ auto mod_q = [&](rad_weight w, bool gated) { - rad_op 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; + 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)); + } if (!op) return op; rad_buf rd[3] = { m.b_modg, m.b_h, m.b_ao }; - rad_buf wr[3] = { m.a_hm.q, m.a_hm.s, m.b_h }; rad_op_reads(b, op, rd, gated ? 3 : 2); - rad_op_writes(b, op, wr, 3); + 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); + } return op; }; @@ -1295,7 +1338,8 @@ static int declare_dit(RadBuilder* b, const RadModelMeta* meta, Model& m) { RAD_ARCH_TRY(B.gate_up.declare(b, nm, g, nm.f("%s.ffn_gate_up", dn), 2 * g.n_ff, D, RAD_SHARD_ROW, grp_layer(grp), { fsrc }, m.a_hm, m.b_gu, 0, { g.n_ff, g.n_ff })); - RAD_ARCH_TRY(B.gq.declare(b, g, m.b_gu, m.a_ffn, g.n_ff, "silu", 0, false)); + RAD_ARCH_TRY(B.gq.declare(b, g, m.b_gu, m.a_ffn, g.n_ff, "silu", 0, false, false, + m.dit_i8)); RAD_ARCH_TRY(B.down.declare(b, nm, g, nm.f("%s.ffn_down", dn), D, g.n_ff, RAD_SHARD_COL, grp_layer(grp), { nm.ckpt("%s.ff.net.2.weight", pre) }, m.a_ffn, m.b_ao)); @@ -2905,7 +2949,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c RAD_ARCH_TRY(declare_dit(b, meta, m)); if (m.dit[0].qkv.bf16) { fprintf(stderr, "radiance: minimaxh3: the DiT's linears are bf16; this plugin serves the " - "block-fp8 container (data/recipes/minimax-h3-fp8.recipe)\n"); + "block-fp8 container (data/recipes/minimax-h3-fp8.recipe) or the int8 one " + "(data/recipes/minimax-h3-w8a8.recipe)\n"); return RAD_E_UNSUPPORTED; } RAD_ARCH_TRY(declare_ups(b, meta, m)); @@ -3430,10 +3475,18 @@ 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(); - 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); + 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); 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), @@ -3464,9 +3517,15 @@ 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]; - 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); + 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); 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); diff --git a/data/recipes/minimax-h3-w8a8.recipe b/data/recipes/minimax-h3-w8a8.recipe new file mode 100644 index 00000000..82443344 --- /dev/null +++ b/data/recipes/minimax-h3-w8a8.recipe @@ -0,0 +1,93 @@ +# minimax-h3-w8a8.recipe -- MiniMax-H3 with the DiT denoiser's linears at int8 (W8A8). +# +# This is minimax-h3-fp8.recipe with the DiT's four per-token linears (qkv, output, gate_up, down) +# quantised to symmetric int8 on a per-row 128-column grid instead of E4M3, and read by the w8a8 +# GEMMs. The point is the denoiser's OUTPUT QUALITY: E4M3 has three mantissa bits and clamps the +# activation at 448, so the fp8a8 denoiser banded smooth gradients (a watercolour look) and clipped +# highlights (oversaturated colour). int8 is 126 uniform levels a group with no clamp -- far finer on +# the smooth per-token activations that carry detail -- at the same one byte a weight, so the +# container's footprint and the served memory are unchanged. The text encoder, the token refiner and +# the modulation (ada) projections stay E4M3: they are read once or a few times a step, not on every +# token, so they are not the fidelity limit. Serve with the minimaxh3 plugin's W8A8 DiT path. +# +# rad-convert --recipe data/recipes/minimax-h3-w8a8.recipe \ +# -o minimax-h3-w8a8.rad +# +# From an existing minimax-h3-fp8.rad only the DiT changes, so reuse the rest: +# +# rad-convert --recipe data/recipes/minimax-h3-w8a8.recipe \ +# --reuse minimax-h3-fp8.rad --changed -o minimax-h3-w8a8.rad +# +# The directory needs the text-to-video-and-audio components only: text_encoder/, transformer/, +# vae/, audio_vae/, the processor or tokenizer and both scheduler folders (transformer_ref/ and +# the original-layout FL2VA/ and Ref2VA/ are not read). +# +# FastVideo's FastH3 releases (the denoiser distilled to eight steps with video sparse attention) +# take the same recipe: their text encoder and decoders are MiniMax-H3's files, so a container of +# the base model can supply them, with every transformer tensor listed as changed: +# +# rad-convert --recipe data/recipes/minimax-h3-fp8.recipe \ +# --reuse minimax-h3-fp8.rad --changed \ +# -o fasth3-8step-fp8.rad +# +# LBH-123-AI's latent upscaler (Minimax_h3_latent_Upscaler, Apache-2.0) is a companion folder: +# put its config.json (minimax_h3_latent_upscaler_3d_conv_v1/config.json) and its bf16 weights, +# named diffusion_pytorch_model.safetensors, in latent_upscaler/ of the pipeline directory. It +# converts with the rest, or is appended to a container converted without it, which keeps every +# weight the container holds where it is: +# +# rad-convert --recipe data/recipes/minimax-h3-fp8.recipe \ +# --reuse minimax-h3-fp8.rad --in-place +# +# Every linear of the text encoder's first 50 layers, of the token refiner and of the DiT's 50 +# blocks, and the per-modality modulation projections, become E4M3 codes with a bf16 scale a +# 128 x 128 block, read by the fp8a8 GEMMs. What keeps the checkpoint's precision is read once a +# step or less: the embedding table, context_embedder and norm_out (bf16), the timestep MLP and the +# four patch projections (f32), every norm and bias. The video decoder's linears are cast to bf16 +# from the checkpoint's f32, its norms, layer scales, biases and register tokens stay f32; the +# audio decoder stays f32 (its weight norms folded at convert). The ref2va encoders convert with +# them when the checkpoint ships them: the audio encoder's f32 chain convs are weight-normalised +# and stored folded; the video encoder's 3x3x3 convs are cast to bf16, which is how the implicit- +# GEMM kernel stores them (conv_in and the k=1 convs read f32 as declared and need no rule). + +te.blk.*.attn_qkv.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +te.blk.*.attn_output.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +te.blk.*.ffn_gate_up.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +te.blk.*.ffn_down.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +te.blk.*.attn_norm.weight cast dtype=f32 +te.blk.*.ffn_norm.weight cast dtype=f32 +ref.blk.*.attn_qkv.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +ref.blk.*.attn_output.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +ref.blk.*.ffn_gate_up.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +ref.blk.*.ffn_down.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dit.blk.*.attn_qkv.weight rtn codes=i8 group=128 scale=bf16 clamp=sym +dit.blk.*.attn_output.weight rtn codes=i8 group=128 scale=bf16 clamp=sym +dit.blk.*.ffn_gate_up.weight rtn codes=i8 group=128 scale=bf16 clamp=sym +dit.blk.*.ffn_down.weight rtn codes=i8 group=128 scale=bf16 clamp=sym +dit.blk.*.ada*.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +vae.blk.*.attn_qkv.weight cast dtype=bf16 +vae.blk.*.attn_out.weight cast dtype=bf16 +vae.blk.*.ffn_up.weight cast dtype=bf16 +vae.blk.*.ffn_down.weight cast dtype=bf16 +vae.proj_out.* cast dtype=bf16 +aud.*.weight cast dtype=f32 +# The audio encoder's strided downsamplers and its attention/MLP pre-block run in a bf16 island +# inside the f32 chain (arch/minimaxh3/minimaxh3.cpp, declare_aenc): conv1d, layernorm and gemm +# pin every operand to one dtype, so their weights cast; the chain's own f32 weights are stored +# folded from the checkpoint's weight-norm pairs. +aenc.d*.w cast dtype=bf16 +aenc.d*.b cast dtype=bf16 +aenc.pb.* cast dtype=bf16 +aenc.b*.u*.c1.weight cast dtype=f32 +aenc.b*.u*.c2.weight cast dtype=f32 +aenc.cin.weight cast dtype=f32 +aenc.fin.weight cast dtype=f32 +# The video encoder: the implicit-GEMM kernel stores its 3x3x3 weights bf16 tap-major, so those +# four name classes cast; conv_in (3 input channels) and the k=1 shortcut/quant convs run on the +# reference kernel with their f32 weights as they are, and the norms and biases need no rule. +venc.l*.b*.c1w cast dtype=bf16 +venc.l*.b*.c2w cast dtype=bf16 +venc.l*.dnw cast dtype=bf16 +venc.cout.weight cast dtype=bf16 +ups.embed.* cast dtype=f32 +ups.*.emb_layers.* cast dtype=f32