Skip to content

The router weight gradient's loads share a basic block, and 64 partitions buy what 256 did - #111

Merged
feitreim merged 1 commit into
mainfrom
fuse-router-dweight-gemm
Aug 6, 2026
Merged

feitreim merged 1 commit into
mainfrom
fuse-router-dweight-gemm

Conversation

@feitreim

@feitreim feitreim commented Aug 6, 2026 •

Copy link
Copy Markdown
Owner

Issue #107 item A4, and an account of why the rest of A4 is not reachable.

What A4 asked for, and what measuring it first found

A4 priced router_backward_weight_split + router_backward_weight_merge at
0.59 GiB/step and 12 launches — the [SPLITS, E, D] partial buffer written
by one and read straight back by the other — and proposed replacing the pair
with one accumulating GEMM against the MN-major view of ffn_normalized.

Measuring the pair first (./run.sh model profile, B=12, at dc6f533) found
something the shape arithmetic could not:

span main (dc6f533)
backward.router.weight 5.9083 ms — 1.86% of step
backward.router.weight_merge 0.8352 ms — 0.26%
full step 316.9499 ms

5.9083 ms to read 144 MiB per layer is 292 GB/s. rms_norm_backward_weight_fast
sits three rows below it doing the same kind of work over three times the bytes
(backward.ffn_norm.weight, 2.2156 ms) at about 2.4 TB/s. The PTX says why:
in the token walk every load sits in a basic block of its own, fenced by a
slice bounds check and by the expert < e guard on the gate row, so a warp never
has more than one load in flight and the kernel is pure memory latency.

The change

gpu/ops/src/lib.rs and the parity call in gpu/ops/src/main.rs. gpu/model
is untouched
— the partial buffer's declared shape follows
ROUTER_WGRAD_SPLITS, so it resizes on its own.

  • The shape is checked once, up front, and the walk then indexes unchecked,
    so the bounds-check traps leave the loop.
  • ROUTER_WGRAD_TOKENS = 8 loads are issued before any is multiplied. The
    token loop's trip count is a runtime value NVVM will not unroll, which is the
    same fact MOE_PROBABILITY_SUMS_THREADS already records for #99.
  • A lane owns both halves of one packed word (ROUTER_WGRAD_ROWS 4 → 2)
    instead of four strided rows, so a warp's load is a full 128-byte sector
    rather than half of one.
  • The tail clamps the token index and multiplies by zero rather than branching,
    so nothing re-enters the loop's control flow.

That makes the partition count worth only what it buys. Memory parallelism is
D * SPLITS * TOKENS loads in flight, already past memory rate at 64, so
ROUTER_WGRAD_SPLITS drops 256 → 64. This is A4's traffic, bought a
different way: the partial buffer goes 25.2 MiB → 6.3 MiB and the merge
reads a quarter as much.

Why A4's single accumulating GEMM is not reachable

Two independent blockers, both measured or structural rather than assumed.

1. The tcgen05 path cannot take this output. dweight is [D, E] = 3072 x
8 columns against a TC_N_TILE of 256; tcgen05_launch_config asserts
n % TC_N_TILE == 0. The GEMM would have to pad dlogits to 256 columns — 32x
the FLOPs, on 12 clusters of the device's 74 — and it would need dlogits
quantized to bf16, which is exactly the fp32 the router is deliberately kept in
(SPEC §7.1).

2. Folding the partitions in directly needs atomics, and a gate here forbids
them.
I implemented that version — one relaxed atomicAdd per output a
partition owns, the shape rms_norm_backward_weight_fast already uses for every
norm gradient in the model — and ran the gates. Every numerical check passed:
./run.sh ops, the CPU-reference MoE block{n} router.dw check, the MoE
overfits. The byte-identical checkpoint-resume gate did not
(gpu/model/src/main.rs:1526, "checkpoint resume changed the continued
trajectory"
). The norm gradients get away with atomics because at that binary's
shape their grid.y is 1 and there is no contention; 64 partitions per router
output is real contention. So the fixed ascending-partition order stays, and A4
is delivered as a smaller buffer rather than no buffer.

Fidelity

No tolerance touched, no gate weakened, no assertion removed.

  • ./run.sh ops — green (✓ ops forward/backward parity checks passed),
    including split MoE router weight gradient must be bit-identical across launches and the 2e-6/2e-6 comparison against the serial GPU oracle.
  • ./run.sh model — green, all 14 checks, including checkpoint v5 (bf16 masters) resumes bit-identically.

Numbers

Per span — ./run.sh model profile, B=12, quiet B200 containers

span main (dc6f533) this PR Δ
backward.router.weight 5.9083 (1.86%) 3.1397 (0.98%) −46.9%
backward.router.weight_merge 0.8352 (0.26%) 0.2445 (0.08%) −70.7%
the pair 6.7435 3.3842 −3.3593 ms
backward.experts.gate_up_weight_gemm (control) 23.1810 23.3908 +0.9%
backward.ffn_norm.weight (control) 2.2156 2.2400 +1.1%
full step 316.9499 319.8559 (see below)

Different containers, so the full-step column is not the claim. Summed over the
68 spans that are not the router pair the two runs differ by +2.02%, and
the pair's −50% is an order of magnitude outside that drift while both control
spans sit inside it. A same-container BASELINE_REF=dc6f533 ./run.sh model profile agrees on direction (pair 5.9799 → 4.1375) but landed on a container
whose GEMMs were running 5x slow under concurrent load, so it is corroboration
rather than the number.

Registers — ptxas -v, sm_100a

kernel main this PR
router_backward_weight_split_bf16 72 32
router_backward_weight_split (fp32 twin) 72 42
router_backward_weight_merge 30 30

0 bytes stack frame, 0 bytes spill stores, 0 bytes spill loads on both sides.
At 256 threads a block, 72 → 32 registers is 3 blocks/SM → 8: those are the warps
that hide the loads, and they are half the win. (ncu cannot initialize its
counter library on this driver — "Failed to initialize the profiler:
LibraryNotLoaded"
— so this comes from ptxas -v, like compute-sanitizer
being unavailable on B200.)

Throughput — bin/train.rs, B=16, 30 steps, both arms in one container

tokens/s MFU
baseline dc6f533 33,183.9 14.74%
this PR 33,644.8 14.94%
+1.39%

Caveat, stated plainly: that container was badly contended — five agents were
holding B200s — and 33.2k tok/s is far under the ~78,300 this machine gives when
it has the GPU to itself. Both arms ran back to back inside it, and +1.39% is the
right order for a 3.36 ms saving against a 316.95 ms step (1.06%), but it wants
re-taking on a quiet pool before it is quoted anywhere.

One ablation worth keeping

The staged token values land in a .local depot, because the multiply loop that
reads them stays rolled. Folding the depot away with #[unroll] — 128
straight-line FMAs, no depot at all — measured 4.3205 ms against 3.1397 over
the step's twelve launches. The depot is L1-resident, and the registers it saves
are what keep the warps. It is recorded on ROUTER_WGRAD_TOKENS so the next
person does not have to spend the run.

Interaction with the concurrent RMSNorm work

The RMSNorm PR (#107 A1/A2/A5) changes where ffn_normalized comes from — it
recomputes it in the backward instead of loading a saved copy — at the same
buffer and descriptor interface. This PR consumes it through that interface
unchanged and does not touch gpu/model/src/lib.rs at all, so the two should
compose without a conflict.

…ions buy what 256 did

`backward.router.weight` was 5.9083 ms of a 316.9499 ms step, reading 144
MiB per layer at 292 GB/s — a tenth of what `rms_norm_backward_weight_fast`
achieves beside it on three times the bytes. The PTX says why: every load
in the token walk sits in a basic block of its own, fenced by a slice
bounds check and by the `expert < e` guard on the gate row, so a warp never
has more than one in flight and the kernel is pure latency.

The shape is now checked once up front and the walk indexes unchecked,
`ROUTER_WGRAD_TOKENS` loads are issued before any of them is multiplied,
and a lane owns both halves of one packed word instead of four strided
rows, so a warp's load is a full 128-byte sector rather than half of one.
5.9083 ms -> 3.1397.

That makes the partition count worth only what it buys. Memory parallelism
is `D * SPLITS * TOKENS` loads in flight, which is already past memory rate
at 64 partitions; 256 of them bought nothing and cost four times the
`[SPLITS, E, D]` partial buffer — 25.2 MiB down to 6.3, and the merge that
reads it back 0.8352 ms down to 0.2445.

Issue #107's A4 asked for the partial buffer to go entirely, as one
accumulating GEMM. It cannot: a `[D, E]` output is 8 columns against a
`TC_N_TILE` of 256, so the tcgen05 path would pad 32x and run 12 clusters
of the device's 74 against a `dlogits` the router deliberately keeps fp32,
and folding the partitions in directly needs atomics — which the
byte-identical checkpoint-resume gate in `gpu/model`'s parity binary
rejects (measured: every numerical gate passed, that one did not). The
fixed ascending-partition order stays, and the item's traffic is bought by
shrinking the buffer instead.
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