Repository navigation
The saved activation stream is bf16, and the projections read it where it lies - #108
Merged
Merged
Conversation
…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.
This was referenced 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.
This was referenced Aug 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every
[N, D]activation a block saved for its backward pass was fp32, whileevery consumer of one was a bf16 tcgen05 operand. The gap was paid for twice:
once in the
convert_f32_to_bf16_pairseach projection ran to stage itsoperand, and once in the fp32 copy that quantize had to read.
Storing the stream packed closes it.
Bf16Acts<N, W>carries the K-major andMN-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):
GpuGroupedLinear::forward_into— quantize ofattention_normalized(qkv_proj)GpuGroupedLinear::backward_into— quantize ofattention_normalizedGpuLinear::forward_into— quantize ofattended(o_proj)GpuLinear::backward_into— quantize ofattendedforward.lm_head.quantize— quantize offinal_normalizedforward.attention.pack(the flash epilogue is fp32)Each was an
[N, D]pass: 384 MiB read + 192 MiB written. Net traffic removedby 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 binsare 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 bothbf16, so the lookup neither widens nor rounds.
swiglu_intonow uses the packed storeswiglu_forward_bf16that already existed, so its
activatedis never fp32 at all.rms_norm_forward_fast_bf16has no tile arm on purpose: the tile kernel'sadvantage 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
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.
design.
d_model_*, the expert gradient panels, the wgrads.gate_upandbin_output. See below — blocked, not skipped.Not in this PR
GpuExpertActs::gate_up(fp32[E, C, 2, FF], 2.0 GiB/block) andbin_output(fp32[E, C, D], 0.75 GiB/block) are the two largest fp32 savedactivations left — 16.5 GiB at B=16, more than this PR saves. Both are
written by an expert GEMM's
f32_store_atepilogue, and there is no packedstore_at:launch_tcgen05takes no output offset and derivesmfromoutput.len(), so a sub-region store is not merely a missing argument. Addingone means changing
gemm_tcgen05_bf16_optimized's signature the waygemm_tcgen05_f32_optimizedalready carries it, which puts the hot GEMM'sregister count in play. That is its own change with its own occupancy check.
Measured
bin/train.rsitself, B=16, B200, same shard, same seed (42), step 1 discardedfrom the timing window. Three independent bf16 runs against two of main.
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 blockover 12 blocks (11.25 GiB), the tail where
final_normalizedand the lm head'soperand 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:
+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).
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 sothe two that accumulate 24 roundings over 12 blocks) is the first thing to put
back in fp32: a two-field change to
GpuBlockActscosting 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:
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 outputbuffer. 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_bf16dropped the last element of an odd-length input: itbailed on any word whose second half fell past the end rather than storing
the first half.