Repository navigation
MoE routing fusion: interleaved SwiGLU kills split/join, gathers fold their adds (-46 ms/step) - #81
Merged
Merged
Conversation
… 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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·Dbin 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 sharedscratch.gate_upstaging buffer it read.Fused SwiGLU backward — three passes become one.
swiglu_backward_interleaved_bf16reads the interleaved activation andd_activatedonce and writes both halves of the packedd_gate_uppanel directly.backward.experts.swiglu_gate+swiglu_up+gate_up_join(26.6 ms) collapse into one 7.6 msbackward.experts.swiglurow, and thed_gate/d_upfp32 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 bf16d_gate).Gathers fold their adds and move quads.
moe_gather_combine_addstarts each output element at the ffn residual, so the forward gather writes the block output directly —forward.ffn_residualand theprojection_outputstaging round trip are gone.moe_gather_dx_adddoes the same withrouter_dx(the gather now runs afterrouter_backward_input), deletingbackward.router.combine_dxand thed_model_3round 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):forward.experts.gate_up_splitbackward.experts.swiglu_gate+swiglu_up+gate_up_joinswiglu)backward.router.gather_dx+combine_dxforward.router.gather+forward.ffn_residualforward.experts.swigluforward.router.scatterforward.router.zero_bins54.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 opsgreen, 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 modelgreen: 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_deviceprimitives.