Skip to content

The saved activation stream is bf16, and the projections read it where it lies - #108

Merged
feitreim merged 8 commits into
mainfrom
bf16-activations
Aug 6, 2026
Merged

feitreim merged 8 commits into
mainfrom
bf16-activations

Conversation

@feitreim

@feitreim feitreim commented Aug 6, 2026 •

Copy link
Copy Markdown
Owner

Every [N, D] activation a block saved for its backward pass was fp32, while
every consumer of one was a bf16 tcgen05 operand. The gap was paid for twice:
once in the convert_f32_to_bf16_pairs each projection ran to stage its
operand, and once in the fp32 copy that quantize had to read.

Storing the stream packed closes it. Bf16Acts<N, W> carries the K-major and
MN-major descriptors a linear's forward and weight-gradient GEMMs address it
through, so both read the activation in place — the staging buffer and the
kernel that filled it are gone together.

What the dtype change deletes

Conversion launches removed outright (per step, L=12, B=16):

deleted call site launches/step
GpuGroupedLinear::forward_into — quantize of attention_normalized (qkv_proj) 12
GpuGroupedLinear::backward_into — quantize of attention_normalized 12
GpuLinear::forward_into — quantize of attended (o_proj) 12
GpuLinear::backward_into — quantize of attended 12
forward.lm_head.quantize — quantize of final_normalized 1
total deleted 49
added: forward.attention.pack (the flash epilogue is fp32) 12
net −37

Each was an [N, D] pass: 384 MiB read + 192 MiB written. Net traffic removed
by the launch count alone is 20.8 GiB/step, before counting the halving on
every producer and consumer of the five streams.

Conversions that survive as kernels but stop converting:

  • moe_scatter_bf16_quad → moe_scatter_packed_quad: stream and expert bins
    are now the same dtype, so the scatter is an 8-byte copy that rounds nothing
    and reads half the bytes.
  • embedding_forward → embedding_forward_bf16: master and stream are both
    bf16, so the lookup neither widens nor rounds.
  • the dense FFN's swiglu_into now uses the packed store swiglu_forward_bf16
    that already existed, so its activated is never fp32 at all.

rms_norm_forward_fast_bf16 has no tile arm on purpose: the tile kernel's
advantage over the block-per-row one was loads in flight, and packing the
stream doubles the elements each of those lanes already carries.

What stays fp32, and why

  • Every reduction and statistic inside a kernel. Norm sums of squares, the
    norm scale, the residual add, the MoE gate combine — all still accumulate in
    fp32 registers. Only the store rounds, so the stream carries one rounding per
    layer boundary rather than one per operand.
  • attention_logsumexp. The backward recreates the probabilities from it;
    8 mantissa bits are not an exponent argument.
  • Router probabilities, gate weights and logits. The router is fp32 by
    design.
  • Losses.
  • Every gradient. d_model_*, the expert gradient panels, the wgrads.
  • The parameter plane. Untouched.
  • Expert gate_up and bin_output. See below — blocked, not skipped.

Not in this PR

GpuExpertActs::gate_up (fp32 [E, C, 2, FF], 2.0 GiB/block) and
bin_output (fp32 [E, C, D], 0.75 GiB/block) are the two largest fp32 saved
activations left — 16.5 GiB at B=16, more than this PR saves. Both are
written by an expert GEMM's f32_store_at epilogue, and there is no packed
store_at: launch_tcgen05 takes no output offset and derives m from
output.len(), so a sub-region store is not merely a missing argument. Adding
one means changing gemm_tcgen05_bf16_optimized's signature the way
gemm_tcgen05_f32_optimized already carries it, which puts the hot GEMM's
register count in play. That is its own change with its own occupancy check.

Measured

bin/train.rs itself, B=16, B200, same shard, same seed (42), step 1 discarded
from the timing window. Three independent bf16 runs against two of main.

main (fp32 acts) this branch delta
tokens/s, 100 steps 78 618 79 051 +0.55%
tokens/s, 300 steps 77 731 78 704 +1.25%
tokens/s, 300 steps (shipped tree) 77 731 78 329 +0.77%
MFU (300 steps) 34.52% 34.78% +0.26 pt
peak VRAM 150.77 GiB 138.28 GiB −12.49 GiB
GiB per unit B 5.30 4.52 −0.78

Throughput lands between +0.55% and +1.25% across three pairs; call it ~+0.9%.

The 12.49 GiB is what the accounting predicts: five [N, D] streams per block
over 12 blocks (11.25 GiB), the tail where final_normalized and the lm head's
operand became one buffer (0.56 GiB), and the activation staging buffer that
nothing writes any more (0.59 GiB).

Batch ceiling. The B-independent plane (parameters, optimizer state) is
150.77 − 16 × 5.30 = 65.97 GiB against 178.35 GiB of HBM, so the ceiling moves
from ⌊(178.35 − 65.97) / 5.30⌋ = 21 to ⌊(178.35 − 65.97) / 4.52⌋ = 24.
Computed from the slope; not verified with an OOM probe.

The throughput gain is small and it is supposed to be. The 49 deleted
conversion launches move 20.8 GiB/step net, against roughly 2 900 GiB moved by
a whole 0.42 s step — 0.7% of the traffic, which is about what the clock shows.
This is a memory change that pays a little speed, not the other way round.

Fidelity

300 steps, same shard, same seed, B=16. The loss tracks throughout; the offset
is small, reproducible across two independent bf16 runs, and does not
destabilize:

step main bf16 delta
1 11.121198 11.121437 +0.000239
50 8.042881 8.108030 +0.065149
100 7.323246 7.444330 +0.121084
150 7.451936 7.615177 +0.163241
200 7.216875 7.315125 +0.098250
250 6.819746 6.894321 +0.074575
300 6.875007 6.953365 +0.078358
  • max |delta| over 61 logged points: 0.217 (run A) / 0.241 (run B)
  • mean delta, first half → second half: +0.106 → +0.116 (run A),
    +0.063 → +0.100 (run B). Flat to mildly rising, and inside each run the
    last 100 steps trend down (+0.19 at step 165 → +0.078 at step 300).
  • relative offset settles at +1.1% to +1.6%

The control that makes this readable: the trainer is not bitwise
reproducible — the embedding and norm weight gradients accumulate through
atomics — so two runs of main itself at the same seed differ. Over 100 steps
that run-to-run noise alone reaches |delta| = 0.077 (mean +0.016). The bf16
stream's divergence is a few times the trainer's own nondeterminism, not a
different regime.

Honest caveat: a persistent ~+1.3% offset is larger than a
one-rounding-per-layer argument alone predicts, and 300 steps is a short
window — long enough to show it is not exploding, not long enough to prove it
never compounds. If a longer run shows it growing, the residual stream
(input / ffn_input, the two buffers a block both reads and rewrites, and so
the two that accumulate 24 roundings over 12 blocks) is the first thing to put
back in fp32: a two-field change to GpuBlockActs costing 4.5 GiB of the 12.5.

Gates

modal run modal_app.py --kernel model — the repo's end-to-end parity oracle —
passes at the existing tolerances (BF16_ATOL = 3e-3, BF16_RTOL = 3e-2);
nothing was loosened for this change. All fourteen gates green, including the
ones that converge rather than compare:

✓ full GPU Dense forward/backward and AdamW (bf16 lm-head) match CPU
✓ fp32-oracle substituted MoE model matches CPU with drops and underfull experts
✓ aligned tcgen05 substituted MoE model matches CPU with drops and underfull experts
✓ aligned tcgen05 MoE overfits with scheduled aux loss (2.949761 -> 0.000030)
✓ aligned tcgen05 block linears overfit (3.105546 -> 0.000080)
✓ checkpoint v5 (bf16 masters) resumes bit-identically...

Two bugs the gates caught and this branch fixes, both pre-existing hazards the
dtype change exposed rather than introduced:

  • convert_f32_to_bf16_pairs' widening twin bounds itself on the output
    buffer. The fp32 fallback's staging is sized for the widest linear a block
    owns, so a narrower one's launch tail read past the packed input and took the
    GPU with it (Xid 43). The offset/length form carries the element count.
  • swiglu_forward_bf16 dropped the last element of an odd-length input: it
    bailed on any word whose second half fell past the end rather than storing
    the first half.

…e it lies

Every [N, D] saved activation a block keeps for its backward was fp32 while
every consumer of one was a bf16 tcgen05 operand. The gap was paid for twice:
once in the `convert_f32_to_bf16_pairs` each projection ran to stage its
operand, and once in the fp32 copy that quantize read.

Storing the stream packed closes it. `Bf16Acts` carries the K-major and
MN-major descriptors a linear's forward and weight-gradient GEMMs address it
through, so both read the activation in place and five quantizes per block
plus the lm head's disappear outright. Norm, embedding, residual add, router
and MoE scatter/gather gain packed twins; the sums, the statistics and every
gradient stay fp32, so the stream carries one rounding per layer boundary
rather than one per operand.

The attention output is the one saved activation whose producer writes fp32 --
the flash epilogue -- so it is rounded once into its saved copy from a scratch
panel every block shares, replacing the two quantizes the o_proj GEMMs ran
over a per-block fp32 panel.
A step allocates nothing after the first, so one `cuMemGetInfo` past it is
the peak, and the batch ceiling is a division rather than a bisection of OOMs.
A tiled tensor map needs the width on TC_BK and the height on the layout's
tile. The parity harness's eight token rows meet neither, and encoding one for
them aborted before the first gate ran -- while those same shapes route every
GEMM through the fp32 fallback, which never asks for a map.
The parity harness's FF=19 is the only odd width a stream carries, and it is
consumed flat rather than by row, so packing it needs nothing but the right
word count. The packed SwiGLU was dropping that element: it bailed on any word
whose second half fell past the input rather than storing the first half.
The oracle staging is sized for the widest linear a block owns, so a narrower
one leaves a tail; convert_bf16_pairs_to_f32 bounds itself on the output, and
that tail read past the packed input and took the GPU with it. The region form
carries the element count, which is the bound that was wanted all along.

The activation operand's staging buffer goes with the same change: nothing
writes it now that the GEMMs read the saved activation where it lies, and it
was N x max_width -- 604 MiB at the trainer's batch.
@feitreim
feitreim merged commit dc6f533 into main Aug 6, 2026
feitreim added a commit that referenced this pull request Aug 6, 2026
`gate_up` (`[E, C, 2, FF]`) and `bin_output` (`[E, C, D]`) were the two
largest fp32 saved activations a block still owned — 2.75 GiB a block,
16.5 GiB at B=16, more than #108 saved in total. They stayed fp32 for one
reason: both are written by an expert GEMM's `f32_store_at`, and the
packed-bf16 entry point had no offset store to write one bin of a stacked
allocation with. `launch_tcgen05` took no output offset and derived `m`
from `output.len()`.

So the packed kernel now carries the `c_offset` its fp32 twin already
did. It moves the base pointer once, before the item loop, so nothing in
the drain carries it and the epilogue is the same instruction stream it
was. `Tcgen05Gemm::store_at` is the host form, taking the offset and
element count in packed *elements* so both arms of a dtype dispatch pass
the same two numbers.

The bins themselves become `ExpertBins` — packed where the experts are
tcgen05-aligned, wide on the oracle path. Unlike `ExpertPanel` it carries
no descriptors: neither buffer is ever a GEMM operand, only an epilogue
target and a pointwise kernel's input.

Every reader follows the dtype:

- `swiglu_forward_interleaved_packed` / `_backward_interleaved_packed`
  read the gate/up panel packed (two 8-byte loads where there were two
  16-byte ones). Panel and bins share one alignment gate, so the packed
  and wide halves are the only pairings.
- `moe_gather_combine_add_quad_packed` reads the bins packed. Packed bins
  only exist at tcgen05-aligned shapes, which puts D on TC_N_TILE and so
  on QUAD_LANES — there is no word-wise twin because no shape reaches
  one.
- `moe_scatter_dy_packed` replaces `moe_scatter_dy_bf16`, whose fp32
  `expert_output` had no caller left once the saved output was packed.

Accumulation is untouched: the GEMM still accumulates in fp32 TMEM, the
SwiGLU still computes in fp32 registers, and the gate dot product still
sums in fp32. Only the stores round.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant