Skip to content

Gather routed rows before Inkling 2 MLX expert GEMMs - #72

Merged
ekzhang merged 1 commit into
ekzhang:mainfrom
samiamjidkhan:mlx-inkling2-expert-prefill
Nov 23, 2026
Merged

ekzhang merged 1 commit into
ekzhang:mainfrom
samiamjidkhan:mlx-inkling2-expert-prefill

Conversation

@samiamjidkhan

Copy link
Copy Markdown
Contributor

Short Inkling prompts on MLX spent most of their prefill in the expert MLP. Below 512 tokens it called gather_qmm with lhs_indices, and MLX ignores sorted_indices in 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_qmm per 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):

Tokens 128 256 512 1K 2K 3K 4K 8K–32K
Speedup 1.51× 1.91× 2.53× 1.81× 1.35× 1.27× 1.16× tie

8K–32K are within run-to-run noise in interleaved scripts/bench.py runs. 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_precision fails on this M4 Max on main too. The BF16 row max flushes subnormals to zero here, while CI's runner keeps them.

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 ekzhang left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great improvement and simplification on both ends

@ekzhang
ekzhang merged commit a36ddfd into ekzhang:main Nov 23, 2026
4 checks passed
@samiamjidkhan
samiamjidkhan deleted the mlx-inkling2-expert-prefill branch November 24, 2026 02:44
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.

2 participants