Skip to content

The expert bins are bf16, and the packed GEMM stores one where it lies - #110

Merged
feitreim merged 1 commit into
mainfrom
bf16-expert-buffers
Aug 6, 2026
Merged

feitreim merged 1 commit into
mainfrom
bf16-expert-buffers

Conversation

@feitreim

@feitreim feitreim commented Aug 6, 2026 •

Copy link
Copy Markdown
Owner

GpuExpertActs::gate_up (fp32 [E, C, 2, FF]) and bin_output (fp32
[E, C, D]) were the two largest fp32 saved activations left after #108 —
16.5 GiB at B=16. Both are written by an expert GEMM's epilogue, and the
packed GEMM had no way to write one bin of a stacked allocation, so they were
blocked rather than skipped.

Rebased onto main after today's six-PR campaign (now 14e5d18). #112
landed the batched expert GEMM in the meantime and changes what this PR needs;
the composition is described below and is the one the two PRs pre-negotiated.

The store form, after #112

#112 made the expert the tcgen05 kernel's outermost item axis: one launch
covers all E GEMMs, a_map/b_map are experts descriptors, and C's
per-expert band is tiles_m tile-rows down — derived on device by
TileState::origin, not passed.

That dissolves the original form of this PR. The first version added a
host-side c_offset to the packed kernel plus a store_at entry point, so an
E-iteration host loop could aim the epilogue at one bin. Under #112 there is
no host loop and nothing to aim: a batched launch writes the whole stack.
So the packed kernel gets the one parameter its fp32 twin already has —

sweep(&tile, experts * tiles_m * tiles_n);

— and one host entry point, store_batched, built exactly like
f32_store_batched. No c_offset, no store_at, no output_offset
plumbing: every packed caller writes its whole allocation, so an offset
parameter could only ever be zero.

The epilogue itself is unchanged. Batching and store width are independent
— the expert reaches the epilogue only through the row origin derives — which
is why a bin panel that is bf16 and batched needs no new epilogue, only the
entry point. expert_linear_forward is one launch whose C the bins' dtype
picks:

match output {
    ExpertBins::Packed(words) => tcgen05.store_batched(..., words, ...),
    ExpertBins::Wide(values)  => tcgen05.f32_store_batched(..., values, ...),
}

The buffers

gate_up and bin_output become ExpertBins — Packed where the experts are
tcgen05-aligned, Wide on the non-aligned 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.

Panel and bins share one alignment gate, so "both packed" and "both wide" are
the only reachable pairings and each match arm says so:

  • swiglu_forward_interleaved_packed / swiglu_backward_interleaved_packed
    read the gate/up panel packed, replacing the _bf16 forms — once gate_up
    is packed, an fp32-in/packed-out SwiGLU has no caller.
  • moe_gather_combine_add_quad_packed reads the bins packed. A packed bin
    panel exists only at tcgen05-aligned shapes, which puts D on TC_N_TILE
    and therefore on QUAD_LANES, so no shape can reach a word-wise packed twin.
  • moe_scatter_dy_packed replaces moe_scatter_dy_bf16.

Net kernel count: +1. Accumulation is untouched: the GEMM still accumulates
in fp32 TMEM, SwiGLU still computes in fp32 registers, and the gate-gradient
dot still sums in fp32. Only the stores round.

expert_linear_backward needed no change — its outputs (weight_gradient,
d_bin_input) are fp32 decision-#20 epilogue targets that this PR never
touched.

Registers and occupancy

--kernel gemm on a B200, against main measured in the same session:

kernel main this branch Δ spill pin
gemm_tcgen05_bf16_optimized 134 132 −2 0 B 134 → 132
gemm_tcgen05_f32_optimized 168 168 0 0 B 168
gemm_tcgen05_f32_accumulate 146 146 0 0 B 146

The packed kernel comes out two registers cheaper than main, and the pin is
ratcheted down to the measured 132 rather than left slack — no budget was
loosened. The original version of this PR reported 134/134/0; dropping the
c_offset the batched form made unnecessary is what moved it.

Occupancy is unmoved and was never close. CTAS_PER_SM is 1, set by shared
memory and tensor memory, not registers.

Memory — bin/train.rs, B200, sweep_batch 12/16/20

B main this branch saving #110's original prediction
12 118.52 106.14 12.38 12.38
16 136.20 119.70 16.50 16.50
20 153.88 133.26 20.62 20.62

The accounting closes to 0.01 GiB against 12 blocks × ½ × (E·C·2FF + E·C·D) × 4 B, on the new main, at three independent batch sizes.

main this branch
GiB per unit B 4.420 3.390
B-independent plane 65.48 65.46
batch ceiling 25 33

Both slopes are exactly linear across all three points, and the plane agrees to
0.02 GiB — parameters and optimizer state are untouched, and the fit says so.
Ceiling is ⌊(178.35 − plane) / slope⌋, computed from the slope, not verified
with an OOM probe. The whole curve sits one batch higher than the original
measurement (24 → 32 became 25 → 33) because the campaign's other PRs trimmed
main's slope from 4.55 to 4.42; the +8 step is this PR's.

Throughput — train_ab, same container, B=16, 30 steps, 2 rounds

mean tokens/s spread MFU
baseline main 87 720.6 0.40% 38.95%
this branch 88 833.2 0.21% 39.45%

+1.27%, MFU +0.50 pt. Three to six times the within-arm spread, so the
effect is real, but it is smaller than the +1.91% the original version
measured against the pre-campaign main
— and that is the expected direction.
This PR buys throughput by halving the bytes three touches a step move; #112's
batching and #113/#114's fusions already removed part of the pressure those
touches were creating, so the same bytes saved are worth less on top of them.
The memory saving, which is structural rather than a traffic effect, is
unchanged to the 0.01 GiB.

Fidelity

No tolerance touched, no gate weakened.

  • --kernel model: all 15 gates green at the existing tolerances
    (BF16_ATOL = 3e-3, BF16_RTOL = 3e-2), including both substituted-MoE
    gates, the aligned expert forward/backward gate, the byte-identical
    checkpoint resume, and the convergence gates (aligned MoE overfit
    2.949841 → 0.000050; block linears 3.105546 → 0.000075).
  • --kernel gemm: store/accumulate parity green, budgets green against the
    tightened pin.
  • --kernel ops: all 13 green, including the two gates this PR extends —
    check_swiglu_interleaved feeds the packed arms a packed panel with gate and
    up pre-rounded so the comparison stays exact, and
    check_moe_scatter_dy_rows gains a packed arm asserting the bin gradient is
    the wide result rounded bit-for-bit.

Caveat carried forward

The original 300-step loss-tracking study against a same-seed control still
stands as the evidence that this tracks: mean Δ +0.007 against the control's
+0.021 and a balanced 32/29 sign split, but a wider max excursion (0.288 vs
0.174) and a +1.75% endpoint offset against a +0.70% floor. The trainer is not
bitwise reproducible, so two runs of main at the same seed differ. That study
was not re-run on the new main. If a longer run shows drift, the first
thing to put back in fp32 is gate_up — the larger buffer, and the one whose
rounding lands before a nonlinearity rather than after a linear map.

`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.
@feitreim
feitreim force-pushed the bf16-expert-buffers branch from bfecd61 to 1e455df Compare August 6, 2026 18:46
@feitreim
feitreim merged commit 85eb7a6 into main Aug 6, 2026
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