Round Spark TP projections before the FP32 residual add

Reduce attention-output and FFN-down shards in exact BF16, then widen the merged result for the FP32 residual. Keep the disjoint embedding reduction unchanged. Declare the reductions before the casts as well as issuing them in that order so buffer liveness follows the fixed graph. Clarify that TP can still differ from one card through sharded dot-product rounding.
This commit is contained in:
Constantin authored and StillDeadcode committed 2026-11-03 19:08:58 +01:00
1 parent 8cb2c0a841
commit a5b2f84450
2 files changed
+18 -15

No files matched your search

+10 -9
View File
@@ -256,15 +256,16 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
RAD_STR("from", "bf16"), RAD_STR("to", "f32")), RAD_NOWEIGHTS), {m.h}, {output});
};
m.widen = widen(m.x);
/* Reducing the widened projection outputs retains the f32 residual stream. The exact f32
* wire is also needed for the embedding, where only one vocabulary shard contributes. */
auto reduce = [&](rad_buf x) {
/* Embedding shards contribute disjoint rows, so their widened sum is exact. Projection
* shards must instead reduce in bf16, rounding the merged output before it is widened and
* added to the f32 residual, as in the reference's BF16 linear layers. */
auto reduce = [&](rad_buf x, const char* dtype) {
if (g.world <= 1) return (rad_op)0;
return rw(b, RAD_OP(b, "all_reduce", RAD_PARAMS(RAD_INT("world_size", g.world),
RAD_RANGE("numel", E, T * E), RAD_STR("dtype", "fp32"), RAD_INT("exact", 1)),
RAD_RANGE("numel", E, T * E), RAD_STR("dtype", dtype), RAD_INT("exact", 1)),
RAD_NOWEIGHTS), {x}, {x});
};
m.reduce_embed = reduce(m.x);
m.reduce_embed = reduce(m.x, "fp32");
m.layers.resize((size_t)g.n_layer);
for (int l = 0; l < g.n_layer; ++l) {
@@ -345,16 +346,16 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
RAD_INT("n", g.head_dim), RAD_STR("act", "none"), RAD_STR("dtype", "bf16")),
RAD_NOWEIGHTS), {m.attn, m.gate}, {m.attn});
a.op = gemm(E, Q, a.o, m.attn, m.h);
a.reduce_attn = reduce(m.h, "bf16");
a.widen_attn = widen(m.delta);
a.reduce_attn = reduce(m.delta);
a.add_attn = add();
a.fnorm = norm(a.fn, m.x, m.h);
a.gup = gemm(2 * F, E, a.gu, m.h, m.gu);
a.act = rw(b, RAD_OP(b, "act_mul", RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n", F),
RAD_STR("act", "gelu"), RAD_STR("dtype", "bf16")), RAD_NOWEIGHTS), {m.gu}, {m.ff});
a.dp = gemm(E, F, a.down, m.ff, m.h);
a.reduce_ffn = reduce(m.h, "bf16");
a.widen_ffn = widen(m.delta);
a.reduce_ffn = reduce(m.delta);
a.add_ffn = add();
}
m.final_norm = norm(m.norm, m.x, m.h);
@@ -403,15 +404,15 @@ static void step(RadCtx* c, const RadBatch* batch) {
const RadOperand gates = RAD_P_T2(rad_buf_ptr(c, m.gate), RAD_BF16, T * g.n_head, 1);
RAD_ISSUE_N(c, a.gate_heads, T * g.n_head, heads, gates, RAD_NONE, heads);
RAD_ISSUE(c, a.op, brows(m.attn, T), RAD_W(a.o), brows(m.h, T));
if (a.reduce_attn) RAD_ISSUE_N(c, a.reduce_attn, T * g.n_embd, brows(m.h, T), RAD_NONE);
RAD_ISSUE(c, a.widen_attn, brows(m.h, T), brows(m.delta, T));
if (a.reduce_attn) RAD_ISSUE_N(c, a.reduce_attn, T * g.n_embd, brows(m.delta, T), RAD_NONE);
RAD_ISSUE(c, a.add_attn, brows(m.x, T), brows(m.delta, T), brows(m.x, T));
RAD_ISSUE(c, a.fnorm, brows(m.x, T), RAD_W(a.fn), brows(m.h, T));
RAD_ISSUE(c, a.gup, brows(m.h, T), RAD_W(a.gu), brows(m.gu, T));
RAD_ISSUE(c, a.act, bcol(m.gu, 0, g.n_ff, T), bcol(m.gu, g.n_ff, g.n_ff, T), brows(m.ff, T));
RAD_ISSUE(c, a.dp, brows(m.ff, T), RAD_W(a.down), brows(m.h, T));
if (a.reduce_ffn) RAD_ISSUE_N(c, a.reduce_ffn, T * g.n_embd, brows(m.h, T), RAD_NONE);
RAD_ISSUE(c, a.widen_ffn, brows(m.h, T), brows(m.delta, T));
if (a.reduce_ffn) RAD_ISSUE_N(c, a.reduce_ffn, T * g.n_embd, brows(m.delta, T), RAD_NONE);
RAD_ISSUE(c, a.add_ffn, brows(m.x, T), brows(m.delta, T), brows(m.x, T));
}
RAD_ISSUE(c, m.final_norm, brows(m.x, T), RAD_W(m.norm), brows(m.h, T));
+8 -6
View File
@@ -101,12 +101,14 @@ The FFN splits by channels, whole 128-column blocks where the width has them, an
`[gate | up]` keeps both parts the same width. Every split dimension is covered exactly once at
every world from 1 to 8, asserted in `spark25_arch_test`.
Column-sharded outputs are partial sums, and the residual stream is f32, so the plugin reduces the
*f32* delta rather than the bf16 projection output: one exact `all_reduce` over the widened rows
after the embedding (whose ranks hold disjoint token rows) and two per layer, all issued on the
residual's own buffer. The accumulation order then matches the single-card graph. libr4d serves
that row at 2, 3, 4 and 8 ranks; other widths resolve only on a library with a general
`all_reduce` (libref), so a card deployment is limited to those worlds.
Column-sharded projection outputs are partial sums. Each layer issues two exact bf16
`all_reduce`s on the projection buffer, rounding the merged output to bf16 **before** widening it
and adding it to the f32 residual. This preserves the reference's BF16 projection-output boundary
without rounding away the residual's low bits. Sharding still changes the dot-product reduction
order, so it does not promise bitwise parity with one card. The embedding uses one exact f32
`all_reduce` over its widened, disjoint token rows. libr4d serves these collectives at 2, 3, 4 and 8
ranks; other widths resolve only on a library with a general `all_reduce` (libref), so a card
deployment is limited to those worlds.
The initial implementation serves BF16 with tied embeddings, headwise sigmoid gates and the
checkpoint's own rotary parameters. It does not implement DSpark, quantisation recipes, other gate