From 36c39efdcac1c7a7715c126f2f195da83c5f9866 Mon Sep 17 00:00:00 2001 From: Constantin Date: Sun, 25 Oct 2026 07:21:35 -0400 Subject: [PATCH] Fold HYV3 residual adds into the following norms Reuse the existing BF16-rounded add/norm fusion while retaining final output materialization and the original collective positions. Cover TP1, TP2 and multi-pass prefill without introducing calibration hooks. --- arch/common/rad_fp8.h | 13 ++++--- arch/hyv3_common/hyv3_attention.h | 16 +++++---- arch/hyv3_common/hyv3_impl.cpp | 10 ++++-- tests/arch_test.cpp | 58 +++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 16 deletions(-) diff --git a/arch/common/rad_fp8.h b/arch/common/rad_fp8.h index bf9f0a93..a1618d2a 100644 --- a/arch/common/rad_fp8.h +++ b/arch/common/rad_fp8.h @@ -753,8 +753,7 @@ struct LinearFP8 { * (`a8q`, `a8s`) for the w4a8i8_asym GEMM `op2`. */ bool w4 = false; int64_t w4_split = 0; - rad_buf a16 = 0, a8q = 0, a8s = 0; - rad_op op_a16 = 0; + rad_buf a8q = 0, a8s = 0; /* Prefill band for z-lab's int4 and the int5: above `rot_split` rows the f16 skinny kernel is * bound by the slow f16 matrix instruction, so a second chain runs: the same rotation to int8 * with a scale per (row, 128) into `rot_q2`/`rot_s2`, then the int8 tiled GEMM (`w4a8i8_asym` / @@ -955,8 +954,8 @@ inline bool linear_is_paro_m2xfp4(const RadEncoding& e) { /* A 4-bit weight with a zero point and no transform: u4 codes, an f16 scale and a u4 or u8 zero * point per 128 of K (libquant `codes=u4 zero=u4 group=128 scale=f16`, or rad-convert's import rule - * over an int4 release). Read by gemm_nt_q at w4a16_asym over an f16 activation, or w4a8i8_asym - * over an int8 one, the rotated int4's GEMMs with no rotation. */ + * over an int4 release). At decode w4a16_asym narrows the BF16 input to F16 on load; larger + * batches use w4a8i8_asym over an int8 activation, the rotated int4's GEMMs with no rotation. */ inline bool linear_is_w4(const RadEncoding& e) { if (!rad_enc_is(&e, "affine") || e.n_planes != 3 || e.transform[0]) return false; const RadEncPlane *sc = rad_enc_plane(&e, "scale"), *zp = rad_enc_plane(&e, "zero"); @@ -1288,9 +1287,9 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha } /* ---- 4-bit weight with a zero point and no rotation: the rotated int4's two GEMMs over this - * linear's own copies of the input's bf16 plane. At or below kSplit rows (the skinny f16 - * kernel's ceiling) the GEMM reads the bf16 plane and narrows it on load; above it, where that kernel is - * bound by the f16 matrix instruction, it is quantised to int8 with an f32 scale per + * linear's BF16 input. At or below kSplit rows (the skinny F16 kernel's ceiling) the GEMM + * narrows that input on load; above it, where that kernel is bound by the F16 matrix + * instruction, the input is quantised to int8 with an F32 scale per * (row, 128) for w4a8i8_asym. Two views: codes, and (scale, zero) as `b_scale`, both at the * logical extents, so row parts and spans apply to both as they are; a column slice must be * whole 128s, the scale group. */ diff --git a/arch/hyv3_common/hyv3_attention.h b/arch/hyv3_common/hyv3_attention.h index 96b5303d..00ecc82f 100644 --- a/arch/hyv3_common/hyv3_attention.h +++ b/arch/hyv3_common/hyv3_attention.h @@ -59,12 +59,13 @@ struct AttnQKFP8 { rad_op op_ar = 0, op_add = 0; int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, rad_kvgroup group, - const Wire& wire, const Src& src); + const Wire& wire, const Src& src, bool fold = false, bool add_out = true); void step(RadCtx* c, const RadBatch* batch) const; }; inline int AttnQKFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, - rad_kvgroup group, const Wire& wire, const Src& src) { + rad_kvgroup group, const Wire& wire, const Src& src, + bool fold, bool add_out) { g = geom; w = wire; layer = l; @@ -96,7 +97,10 @@ inline int AttnQKFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l)); if (!w_norm || !w_q_norm || !w_k_norm) return RAD_E_INVAL; - RAD_ARCH_TRY(nq_h.declare(b, g, w_norm, w.x, w.h, g.n_embd)); + // As in the other pre-norm blocks, absorb a preceding delta into the residual stream. + // The fused kernel normalises the BF16-rounded sum, not an unrounded FP32 substitute. + RAD_ARCH_TRY(nq_h.declare(b, g, w_norm, fold ? w.h.x : w.x, w.h, g.n_embd, + fold ? w.x : 0)); RAD_ARCH_TRY(q.declare(b, nm, g, nm.f("blk.%d.attn_q", l), q_dim, g.n_embd, RAD_SHARD_ROW, grp_layer(l), {src.q}, w.h, w.q)); RAD_ARCH_TRY(k.declare(b, nm, g, nm.f("blk.%d.attn_k", l), kv_dim, g.n_embd, @@ -186,12 +190,12 @@ inline int AttnQKFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, RAD_INT("min_bytes", g.wire_min_bytes)), RAD_NOWEIGHTS), {w.h.x}, {w.h.x}); - op_add = rw(b, RAD_OP(b, "add", + if (add_out) op_add = rw(b, RAD_OP(b, "add", RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd), RAD_STR("dtype", g.dtype)), RAD_NOWEIGHTS), {w.x, w.h.x}, {w.x}); - return op_attn && op_kv_store && op_add && + return op_attn && op_kv_store && (!add_out || op_add) && (!fp8_cache || (k_descale && v_descale && op_k_descale && op_v_descale)) ? RAD_OK : RAD_E_INVAL; } @@ -237,7 +241,7 @@ inline void AttnQKFP8::step(RadCtx* c, const RadBatch* batch) const { q_attn.step(c, T); o.step(c, w.attn, w.h.x, T); if (op_ar) RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h.x, T), RAD_NONE); - RAD_ISSUE(c, op_add, brows(w.x, T), brows(w.h.x, T), brows(w.x, T)); + if (op_add) RAD_ISSUE(c, op_add, brows(w.x, T), brows(w.h.x, T), brows(w.x, T)); } } // namespace arch diff --git a/arch/hyv3_common/hyv3_impl.cpp b/arch/hyv3_common/hyv3_impl.cpp index b1b127ef..435bcf1f 100644 --- a/arch/hyv3_common/hyv3_impl.cpp +++ b/arch/hyv3_common/hyv3_impl.cpp @@ -348,7 +348,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c } #endif RAD_ARCH_TRY(rad_bind_layer_kv(b, (int)l, m.kv_full)); - status = lay.attn.declare(b, nm, g, (int)l, m.kv_full, aw, as); + // Each norm folds its predecessor's residual add. The first attention reads the + // embedding directly; the final feed-forward keeps its add for the plain output norm. + // Keep the collectives in the same places and the routed/shared FP32 combine separate. + status = lay.attn.declare(b, nm, g, (int)l, m.kv_full, aw, as, l > 0, false); if (status < 0) { fprintf(stderr, "radiance: hyv3: attention layer %lld declaration failed (%d)\n", (long long)l, status); @@ -369,7 +372,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c ds.up = nm.ckpt("%s.up_proj.weight", mlp); ds.down = nm.ckpt("%s.down_proj.weight", mlp); #endif - status = lay.dense_mlp.declare(b, nm, g, (int)l, dw, ds); + status = lay.dense_mlp.declare(b, nm, g, (int)l, dw, ds, true, + l + 1 == g.n_layer); if (status < 0) { fprintf(stderr, "radiance: hyv3: dense MLP declaration failed (%d)\n", status); return status; @@ -403,7 +407,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c return status; } #endif - status = lay.moe.declare(b, nm, g, (int)l, m.mw, ms); + status = lay.moe.declare(b, nm, g, (int)l, m.mw, ms, true, l + 1 == g.n_layer); if (status < 0) { fprintf(stderr, "radiance: hyv3: MoE layer %lld declaration failed (%d)\n", (long long)l, status); diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index 35a28736..d12ff256 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -692,6 +692,64 @@ TEST(hyv3_bf16_selects_block_fp8_and_mxfp4_expert_formats) { } } +TEST(hyv3_folds_residual_adds_into_the_next_norm_but_materializes_the_final_output) { + RadModelMeta meta = hyv3_fp8_test_meta(); + meta.quant = ""; + meta.n_layers = 3; // dense, a non-final MoE, and the final MoE + std::vector vals(meta.kv_val, meta.kv_val + meta.n_kv); + for (int i = 0; i < meta.n_kv; ++i) + if (std::string(meta.kv_key[i]) == "mlp_layer_types") vals[(size_t)i] = "dense sparse sparse"; + meta.kv_val = vals.data(); + RadEncoding w4a = rad_enc_affine(RAD_U4, RAD_F16, 1, 128); + rad_enc_add_plane(&w4a, "zero", RAD_U8, 1, 128); + const RadEncoding w4 = rad_enc_affine(RAD_I4, RAD_BF16, 1, 128); + const RadEncoding plain = rad_enc_plain(RAD_BF16); + for (int world : { 1, 2 }) for (int rank = 0; rank < world; ++rank) { + RadBuilder b; + b.encs = { + { "attn_q.weight", w4a }, { "attn_k.weight", w4a }, + { "attn_v.weight", w4a }, { "attn_output.weight", w4a }, + { "ffn_gate_up.weight", w4a }, { "ffn_down.weight", w4a }, + { "ffn_gate_up_exps", w4 }, { "ffn_down_exps", w4 }, + { "ffn_gate_up_shexp.weight", plain }, { "ffn_down_shexp.weight", plain }, + { "output.weight", plain }, + }; + RadBuildCtx ctx = build_ctx(rank, world); + ctx.max_tok = 4096; + REQUIRE_EQ(hyv3_bf16::declare(&b, &meta, &ctx), RAD_OK); + const auto& m = hyv3_bf16::models[rank]; + CHECK_EQ(m.layers[0].attn.nq_h.res, (rad_buf)0); + CHECK_EQ(m.layers[0].attn.nq_h.src, m.x); + CHECK_EQ(m.layers[0].dense_mlp.nq_h.res, m.x); + CHECK_EQ(m.layers[0].dense_mlp.op_add, (rad_op)0); + for (size_t l = 0; l < m.layers.size(); ++l) { + CHECK_EQ(m.layers[l].attn.op_add, (rad_op)0); + if (l == 0) continue; + CHECK_EQ(m.layers[l].attn.nq_h.src, m.h.x); + CHECK_EQ(m.layers[l].attn.nq_h.res, m.x); + CHECK_EQ(m.layers[l].moe.nq_h.src, m.h.x); + CHECK_EQ(m.layers[l].moe.nq_h.res, m.x); + CHECK_EQ(m.layers[l].moe.op_add != 0, l + 1 == m.layers.size()); + CHECK(m.layers[l].moe.op_combine != 0); // retain the separate BF16-rounded arms + const RecOp* norm = b.op_of(m.layers[l].moe.nq_h.op); + REQUIRE(norm); + CHECK(std::find(norm->reads.begin(), norm->reads.end(), m.x) != norm->reads.end()); + CHECK(std::find(norm->writes.begin(), norm->writes.end(), m.x) != norm->writes.end()); + } + CHECK_EQ(count_op(b, "add"), 3); // two routed/shared combines, one final residual + for (int rows : { 1, 8, 65, 2049, 4096 }) { + RadBatch batch = decode_batch(); batch.n_tok = rows; batch.max_q_len = rows; + RadCtx run{}; run.b = &b; run.batch = &batch; run.rank = rank; + hyv3_bf16::step(&run, &batch); + CHECK(lint_step(b, {&run}).empty()); + int final_adds = 0; + for (const auto& issue : run.issues) + if (issue.op == m.layers.back().moe.op_add) ++final_adds; + CHECK_EQ(final_adds, (rows + 2047) / 2048); + } + } +} + TEST(hyv3_decode_fuses_qk_norm_rope_and_store_with_unfused_fallbacks) { for (int form = 0; form < 3; ++form) { RadModelMeta meta = hyv3_fp8_test_meta();