Skip to content

MoE routing fusion: interleaved SwiGLU kills split/join, gathers fold their adds (-46 ms/step) - #81

Merged
feitreim merged 1 commit into
mainfrom
feat/moe-routing-fusion
Aug 4, 2026
Merged

feitreim merged 1 commit into
mainfrom
feat/moe-routing-fusion

Conversation

@feitreim

@feitreim feitreim commented Aug 4, 2026

Copy link
Copy Markdown
Owner

The MoE routing/dispatch plumbing was pure data movement stacked in separate passes: a GEMM epilogue staged into shared scratch that a split pass copied into per-block gate/up buffers, two SwiGLU backward passes whose outputs a join pass re-read and merged, gathers that staged into intermediates a separate add pass re-read, and a forward pre-fill that rewrote the whole E·C·D bin panel every layer. This PR removes the passes instead of speeding them up: five profiler rows are gone outright and the SwiGLU backward's three rows become one.

What changed

Interleaved gate/up activation — the split pass no longer exists. The gate/up GEMM already produces the interleaved [E, C, 2, FF] layout; it now writes the per-block activation directly and SwiGLU reads it in place (swiglu_forward_interleaved_bf16, one 16-byte quad per thread). forward.experts.gate_up_split (13.9 ms of pure copying) is deleted, along with the shared scratch.gate_up staging buffer it read.

Fused SwiGLU backward — three passes become one. swiglu_backward_interleaved_bf16 reads the interleaved activation and d_activated once and writes both halves of the packed d_gate_up panel directly. backward.experts.swiglu_gate + swiglu_up + gate_up_join (26.6 ms) collapse into one 7.6 ms backward.experts.swiglu row, and the d_gate/d_up fp32 scratch buffers are gone. The bf16 round still happens exactly once (SPEC §7.1 had flagged this fusion as "the better win" when rejecting a bf16 d_gate).

Gathers fold their adds and move quads. moe_gather_combine_add starts each output element at the ffn residual, so the forward gather writes the block output directly — forward.ffn_residual and the projection_output staging round trip are gone. moe_gather_dx_add does the same with router_dx (the gather now runs after router_backward_input), deleting backward.router.combine_dx and the d_model_3 round trip. Both gathers and the packed forward scatter now move 16-byte quads, which alone roughly halves the scatter/gather rows on top of the folded adds.

Forward dead-bin zeroing. The forward scatter overwrites every assigned bin row, so the forward now mirrors the backward's dead-bin pass (moe_zero_dead_bins* over the capacity tail) instead of pre-filling the whole panel.

Measured — same-container A/B on B200

modal run modal_app.py::main --kernel model --bin profile --baseline-ref cebec1c, §13.9 shape (L=12, B=12, T=2048, D=3072, FF=4096, E=8, K=2, C=6144). Retained back-to-back pair (ms, summed over the step):

row cebec1c this PR Δ
forward.experts.gate_up_split 13.94 — −13.94
backward.experts.swiglu_gate + swiglu_up + gate_up_join 26.59 7.59 (swiglu) −19.00
backward.router.gather_dx + combine_dx 9.99 2.48 −7.52
forward.router.gather + forward.ffn_residual 9.99 2.52 −7.47
forward.experts.swiglu 7.02 4.18 −2.84
forward.router.scatter 4.97 2.85 −2.12
forward.router.zero_bins 1.87 0.40 −1.47
full step 467.78 421.82 −45.97 (−9.8%)

54.4 ms comes off the targeted rows; the expert GEMM rows drift +0.7–0.8 ms each in the candidate on both A/B passes (down_weight 15.88→16.71, down_input 12.76→13.48, gate_up_weight 27.43→28.17 — plausibly L2 residency, since the gate/up epilogue now lands in 12 per-block buffers instead of one hot scratch buffer), which nets to the −45.97 ms full step. Throughput 52.5k → 58.3k tokens/s at this shape. Workspace VRAM 137.8 → 134.8 GiB (the deleted scratch.gate_up/d_gate/d_up).

The first (build-order) pair on the same container agrees: 467.28 → 420.14 ms.

Correctness

  • --kernel ops green, with new gates: interleaved SwiGLU forward/backward vs the flat kernels bit for bit (fp32) and as exact bf16 roundings (packed); fused gathers vs unfused + host add, quad vs scalar bit-identical; quad packed scatter vs the pair walk bit-identical; and the dead-bin + scatter composition rewriting every input bin over poisoned fp32 and packed panels.
  • --kernel model green: both expert parity configs, both MoE model parities (drops + underfull experts), the overfit gates, and checkpoint resume.

SPEC §7.1's buffer audit is updated to the new layout. No ferro-kittens changes needed — everything composes from existing cuda_device primitives.

… fold their adds, forward zero-bins goes dead-bin-only

- The gate/up GEMM writes the per-block [E,C,2,FF] activation directly and
  SwiGLU reads it interleaved: forward.experts.gate_up_split is gone, and the
  fused SwiGLU backward writes the packed d_gate_up in one pass, deleting
  backward.experts.swiglu_up, gate_up_join, and the d_gate/d_up buffers.
- moe_gather_combine_add folds the ffn residual into the forward gather;
  moe_gather_dx_add folds router_dx into the backward gather (which now runs
  after router_backward_input), deleting forward.ffn_residual and
  backward.router.combine_dx plus each one's intermediate round trip.
- The forward zero_bins pass mirrors the backward's dead-bin zeroing instead
  of pre-filling the whole E*C*D panel, and the packed scatter moves quads.
- Ops parity: interleaved swiglu vs flat (bit-for-bit, plus bf16 rounding),
  fused gathers vs unfused + host add, quad vs pair scatter, and the
  dead-bin+scatter composition over poisoned panels.
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