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.
This commit is contained in:
Constantin authored and StillDeadcode committed 2026-10-27 18:28:46 +01:00
1 parent d59cdce040
commit 36c39efdca
4 files changed
+81 -16

No files matched your search

+6 -7
View File
@@ -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. */
+10 -6
View File
@@ -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
+7 -3
View File
@@ -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);