Repository navigation
Gather routed rows before Inkling 2 MLX expert GEMMs - #72
Merged
Merged
Conversation
MLX ignores sorted_indices when gather_qmm is given lhs_indices, so prefill under 512 tokens missed the grouped kernel, and longer chunks used per-expert FP32 GEMMs over dequantized weights with a host readback. Gathering routed rows first and running one sorted gather_qmm matches or beats both paths at every prefill size. Activations stay FP32, so only summation order changes. The real-checkpoint test now uses 80 tokens to cover the grouped kernel and excludes routes whose top-8 boundary is an FP32 tie. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ekzhang
approved these changes
Nov 23, 2026
ekzhang
left a comment
Owner
There was a problem hiding this comment.
Great improvement and simplification on both ends
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.
Short Inkling prompts on MLX spent most of their prefill in the expert MLP. Below 512 tokens it called
gather_qmmwithlhs_indices, and MLX ignoressorted_indicesin that case (right_sorted = sorted_indices && !lhs_indices), so the gate/up projection never reached the grouped kernel. From 512 tokens it switched to the per-expert FP32 "segmented" path from #67, which dequantizes all 128 experts on every visit and reads the routing back to the host. Its 2.3–3.5× advantage in #67 was measured against the slow form.This gathers routed rows first and runs one sorted
gather_qmmper projection at every size, and removes the segmented path. Activations stay FP32 (the checkpoint's quantized activations), so the products are the same and only the summation order changes.Prefill speedup on an M4 Max, old and new alternated prompt by prompt in one process (median of three):
8K–32K are within run-to-run noise in interleaved
scripts/bench.pyruns. Decode is unchanged at batch 1–8.The real-checkpoint test now uses 80 tokens so the grouped kernel runs against the CPU reference; 24 tokens stayed on the per-route kernel. At 80 tokens three of the 3,840 token visits have eighth and ninth biased router scores within 1e-5; one (2.5e-7 apart) picks a different expert than the reference under main's paths too. The test excludes such tied routes and allows up to 0.2% of token visits. Visits agree within 0.56% (attention) and 0.31% (experts), teacher-forced logits within 5e-5. Rerunning the 16-, 1,100- and 2,100-token comparisons against the CPU reference agrees on all 15 top-1 tokens.
Against main, greedy output (64 tokens) is identical at 4K and 8K but splits on three shorter prompts after 2, 15 and 36 tokens. At each split the top two logprobs are 0.003–0.48 apart, and at each one main or this branch itself picks a different token when the same tokens are prefilled instead of decoded.
For reference, the open mlx-lm port (ml-explore/mlx-lm#2086 at
e2b5bbd) on the same prompts and laptop prefills at 857, 760, 703 and 643 tokens/s (medians) for 4K–32K, against 880, 797, 740 and 624 for this branch in one non-interleaved run. It runs the experts with BF16 activations instead of the checkpoint's quantized ones.Unrelated:
test_nvfp4_row_max_precisionfails on this M4 Max on main too. The BF16 row max flushes subnormals to zero here, while CI's runner keeps them.