Skip to content

The E=8 expert loop leaves the host: one batched tcgen05 launch a GEMM (#107 A9) - #112

Merged
feitreim merged 2 commits into
mainfrom
a9-batch-expert-gemm
Aug 6, 2026
Merged

feitreim merged 2 commits into
mainfrom
a9-batch-expert-gemm

Conversation

@feitreim

@feitreim feitreim commented Aug 6, 2026

Copy link
Copy Markdown
Owner

Closes item A9 of #107.

What was there

expert_linear_forward and expert_linear_backward dispatched E = 8
identically shaped tcgen05 GEMMs from a host loop — 16 forward and 32
backward launches a layer, 576 a step, 48% of the trainer's 1206 — and all
eight sat inside one profiler.measure span, so the profile never showed them.
Inductor issues the same work as one bmm apiece (2 forward, 4 backward a
layer = 72).

What it is now

The expert becomes the kernel's outermost item axis. The descriptors move
from one allocation each into one allocation of experts descriptors
(Bf16PairsTmaMaps), which is all the kernel needs to reach expert e's
operands — their base addresses and row strides were already in the
descriptors, so an item's tile coordinates, its operand walk and the transaction
bytes it charges are a single GEMM's, unshifted:

let (expert, tile_m, tile_n) = self.place(item);
let (a_map, b_map) = (self.a_map.add(expert), self.b_map.add(expert));

The only geometry that differs is C, one allocation holding every expert's
matrix, where the expert is tiles_m tile-rows down. That is derived, not
passed — which is why experts is the single new kernel parameter and the
producer/MMA/epilogue loops are otherwise byte-for-byte what they were. The
alternative sketched in the issue — one strided descriptor per operand — needs
four per-operand expert strides instead of one parameter, because the expert
sits on the MN axis of three of the six operands and on the K axis of the other
three (the globally transposed weight is [columns, E * rows]).

Launches: 576 → 72 a step (−504). Trainer total 1206 → 702.

What it buys, and what it does not

Not launch latency. The grid is capped at MAX_CLUSTERS = 74 clusters either
way, so batching never widens a launch that already filled the device. What it
removes is the tail of one — the same last-wave quantization the kernel's
own module doc names when it explains why 192 tiles over 74 clusters is 86.5%.

./run.sh model profile vs dc6f533 (same container, B=12, C=6144), ms:

span main batched ratio
forward.experts.gate_up_gemm 109.1323 104.6789 0.959
forward.experts.down_gemm 42.0100 41.1054 0.979
backward.experts.down_weight_gemm 34.9424 29.4138 0.842
backward.experts.down_input_gemm 61.5945 54.4133 0.883
backward.experts.gate_up_weight_gemm 68.0173 58.6630 0.863
backward.experts.gate_up_input_gemm 49.2270 46.5597 0.946
six expert GEMMs 364.92 334.83 0.918
forward.experts.swiglu 4.1121 4.1156 1.001
backward.experts.swiglu 7.4809 7.3727 0.986
optimizer.experts.*.adamw 17.582 17.638 1.003
full step 761.3430 731.5607 0.961

The three that move most are the three whose per-expert tile count put them in
the 86.5% regime (192 or 384 tiles over 74 clusters); the two that barely move
were already at 94–97%. Shape arithmetic alone predicts 0.925 for the group
against the 0.918 measured, so the wave tail is nearly all of it and the
remainder is per-launch fill.

A/B on the shipping binary

bin/train, B=16, TRAIN_STEPS=12 (first discarded, 11 measured), both arms
built and run back to back in one container against dc6f533, two rounds:

round main (dc6f533) batched
0 82 353.8 tok/s 84 031.0 tok/s
1 82 297.7 tok/s 84 055.9 tok/s
mean 82 325.8 84 043.5

+2.09% (36.55% → 37.32% MFU). Within-arm spread is 0.07% and 0.03%, so the
effect is thirty times the noise. The profile's −3.9% on the step is the larger
number because that binary synchronizes every span; the trainer's +2.1% is the
one that counts.

Registers

ptxas budgets, unchanged in every digit — the expert index is a divide and a
pointer stride an item, not liveness carried through the pipeline:

kernel main batched
gemm_tcgen05_bf16_optimized 134 regs, 0 spill 134 regs, 0 spill
gemm_tcgen05_f32_optimized 168 regs, 0 spill 168 regs, 0 spill
gemm_tcgen05_f32_accumulate 146 regs, 0 spill 146 regs, 0 spill

Fidelity

No tolerance touched, no gate weakened.

  • ./run.sh gemm — fp32 and tcgen05 bf16 GEMM store/accumulate parity passed.
  • ./run.sh model — every gate green, including aligned tcgen05 expert forward/backward, zero rows, accumulation, and compute sync, both substituted
    MoE model gates, the aux-loss overfit, and the byte-identical checkpoint
    resume.

The fp32 oracle path (non-aligned expert shapes) keeps its host loop and is
covered by its own gate unchanged.

Interaction with the concurrent expert-panel bf16 work

That branch changes the store dtype of the expert bin buffers; this one
changes launch structure. They meet in expert_linear_forward /
expert_linear_backward and in the f32_* adapters' output type, so expect
conflicts in exactly those places. The resolution is mechanical: whatever the
output buffer becomes, this side only requires it to be the experts stacked
[m, n] matrices assert_batched_output checks for, and a packed-bf16 output
would want a store_batched beside f32_store_batched built the same way.

…pert GEMM

`expert_linear_forward` and `expert_linear_backward` dispatched E=8
identically shaped GEMMs from a host loop — 16 forward and 32 backward
launches a layer, 576 a step, 48% of the trainer's launch count, and all
eight inside one `profiler.measure` span so the profile never showed them.
PyTorch issues the same work as one `bmm` apiece.

The expert becomes the kernel's outermost item axis. Descriptors move from
one allocation each into one allocation of `experts` descriptors
(`Bf16PairsTmaMaps`), which is all the kernel needs to reach expert `e`'s
operands: their base addresses and row strides were already in the
descriptors, so an item's tile coordinates, its operand walk and its `C`
band are a single GEMM's, unshifted. The only geometry that differs is
`C`, one allocation holding every expert's matrix, where the expert is
`tiles_m` tile-rows down — derived rather than passed, so `experts` is the
single new kernel parameter.

What that buys is the tail, not the launches. The grid is capped at
MAX_CLUSTERS either way, so batching does not widen a launch that already
filled the device; what it removes is the quantization at the end of one.
Six of the eight expert GEMM shapes ran 192 or 384 tiles over 74 clusters
— 86.5% last-wave efficiency — and eight experts' worth of the same tiles
is 98.8%.
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