Skip to content

perf: block-per-pair moe_scatter_dy (30.8 -> 3.7 ms, closes #45) - #49

Merged
feitreim merged 2 commits into
mainfrom
perf/issue-45-scatter-dy
Jul 23, 2026
Merged

feitreim merged 2 commits into
mainfrom
perf/issue-45-scatter-dy

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Closes #45.

What changed

moe_scatter_dy gave each (token, slot) pair a single thread that serially
walked the whole D=3072 gradient row: an uncoalesced gate·dy bin copy fused
with a serial gate dot Σ_d expert_output·dy. Measured at ~1% of HBM and the
top of the backward-router profile.

Restructured to one block per pair (grid = N·K, block = 256):

  • lanes stride the D row so the bin copy is fully coalesced;
  • the gate dot reduces in shared memory (fixed-order tree, no atomics);
  • dropped slots write gate_gradient = 0 and skip the row; all writes stay
    disjoint per surviving slot (semantics preserved exactly).

Launch sites updated in the model backward path and the gpu/ops parity harness.
zero_dy_bins fold-in left as a follow-up (see SPEC §13.9) to keep the diff
confined to the kernel.

Profile — BASELINE_REF=main ./run.sh model profile (B200, same container, B=12 T=2048)

span main this PR change
backward.router.scatter_dy 30.7679 ms (4.11%) 3.6565 ms (0.51%) 8.4×
backward.router.zero_dy_bins 3.7575 ms 3.7419 ms unchanged (not folded)
full step 748.8096 ms 721.1883 ms -3.7%

Parity

./run.sh ops:

  • ✓ ops forward/backward parity checks passed

./run.sh model:

  • ✓ 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.944996 -> 0.022638)
  • ✓ full GPU Dense forward/backward and AdamW (bf16 lm-head) match CPU

Follow-ups

  • float4-vectorize the row copy for the sub-1 ms floor the issue targets.
  • fold backward.router.zero_dy_bins into a dead-slot zeroing pass using the
    routing assignment_counts.

@feitreim
feitreim merged commit a60b65c into main Jul 23, 2026
feitreim added a commit that referenced this pull request Jul 25, 2026
Phase 2 of the backward MoE scatter, following #45/#49.

float4 row copy: `moe_scatter_dy` walked the `D` row one `f32` per lane
per step. When `dim` is a multiple of four every row base `row * dim` is
16-byte aligned, so each lane now moves one `F32x4` (`ld/st.global.v4.f32`)
instead; other `dim` keep the scalar walk. The `gate * dy` bin writes stay
bit-identical -- only the fixed order the gate dot accumulates in changes,
as it already did in #49.

Dead-slot zeroing: capacity assignment hands expert `e` the slots
`0..min(count[e], C)` and the scatter overwrites exactly those, so the
`E*C*D` pre-fill (`backward.router.zero_dy_bins`) rewrote the whole buffer
to no effect. `moe_zero_dead_bins` takes the routing `assignment_counts`
and clears only the unassigned tail `min(count[e], C)..C`, one block per
`(expert, slot)`, also float4 where `dim` allows. The two passes partition
the buffer, so the separate `fill_zero` launch is gone.

The ops parity harness now poisons the bin gradients rather than zeroing
them, so a bin neither pass touches fails the compare, and adds a case
pair over a routing with a dropped pair, a half-dead expert, and an
entirely unassigned expert at a `D` on the float4 path and one off it.
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.

moe_scatter_dy: one thread per slot serializes a 3072-float row copy + dot

1 participant