[MoE] Count tokens per expert with scatter_add on MPS (torch.histc is ~45 ms per call there) - #49027
Open
nassersala wants to merge 1 commit into
Open
nassersala wants to merge 1 commit into
nassersala wants to merge 1 commit into
Conversation
torch.histc costs ~0.17 ms per bin on MPS (45 ms at 256 experts), once per MoE layer per token. Count with scatter_add instead; out-of-range ids (EP sentinels) go to an extra bin that is dropped, as histc drops them. CPU/CUDA unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Contributor
CI recapDashboard: View test results in Grafana |
Isalia20
suggested changes
Sep 23, 2026
Isalia20
left a comment
Contributor
There was a problem hiding this comment.
Please hold off from merging before i check the numbers here:
pytorch/pytorch#198304
Contributor
|
Let's try to make histc better in pytorch rather than add a workaround, I'll take a look |
This branch has not been deployed
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.
What
grouped_mm_experts_forward(integrations/moe.py) computestokens_per_expertwithtorch.histc. On MPS,histccosts about 0.17 ms per bin regardless of input size: 11 ms at 64 experts, 22 ms at 128, 45 ms at 256, once per MoE
layer, so once per layer per generated token.
grouped_mmis the default experts implementation, so this hits everyMoE model run on Apple Silicon.
This PR counts with
scatter_add_on MPS instead. Ids at or abovenum_experts(EP sentinels) go to an extra bin that isdropped, which is what
histc(max=num_experts - 1)does with them. CPU and CUDA paths are unchanged, and there is nodata-dependent shape, so nothing changes for graph capture.
Measurements (Apple M5 Max, macOS 26.5.2, torch 2.14.0, transformers 5.17.0)
histcvsscatter_addalone (bench_mps.pybelow):Greedy decoding, random-weight Qwen3-MoE with Qwen3-30B-A3B's expert layout (48 layers, 128 experts, top 8), bf16
(
bench_moe_decode.pybelow):Same tokens in all three. On a real model (Mapika/decider-35b-a3b, Qwen3.5-35B-A3B architecture, 256 experts), prefill of
a 96-token input goes from 2.8 s to 1.0 s with this change alone, with bit-identical output probabilities.
Checked on this branch (current
main): the same model decodes at 46.8 -> 13.0 ms/token against 5.17.0, with bit-identicallogits and tokens.
Correctness: the cumulative offsets match
histcexactly on 50 random id sets of up to 5,000 ids that include sentinels.Related PyTorch issue: pytorch/pytorch#198304.
Follow-up (separate PR or issue, happy to open it)
The gated-delta-rule reference path (
torch_chunk_gated_delta_rule, used by qwen3_5, qwen3_5_moe, qwen3_next, olmo_hybrid,qwen4_exp) calls
torch.linalg.solve_triangulartwice per layer. On MPS that is 18 ms for 32 heads x 10 chunks and118 ms for 32 x 64, against 1.4 ms and 8.8 ms on CPU. Two MPS options, both checked against the MPS solver:
is_torchdynamo_exporting()forward-substitution loop: 3.3 ms / 11.8 ms, no new code;[[A11,0],[A21,A22]]^-1 = [[X11,0],[-X22 A21 X11, X22]], all batched matmuls: 0.40 ms /1.4 ms, max abs difference 6e-7 on realistic systems.
On decider-35b-a3b both changes together take a 600-token prefill from 3.9 s to 0.47 s. Output probabilities move by at
most 0.033, less than switching to the exact CPU solver moves them (0.042); no argmax changed on six items, and the
model's JevBench public numbers are reproduced exactly (48/48, 70/72, 75/111).
(A Neumann-series inverse
(I - N)(I + N^2)...looks tempting but gives NaNs on real inputs, whose entries are close to 1.)bench_mps.py
bench_moe_decode.py
馃 Generated with Claude Code