Repository navigation
Share one unsorted expert dispatch across Gemma prompt and decode rows - #384
Merged
Merged
Conversation
The registered Gemma prompt policy (gemma4-bf16-token-expert-reduction-v1) ran each prompt token's expert block as its own subgraph: about six kernel launches per token per layer. Batch decode did the same per lane. SwitchGLU.callAndWeightedReduceRows now projects all rows in one call with their assignments kept in row order. Every (row, expert) pair is still a native M=1 gathered product (gather_qmv: the kernel depends only on N, K, bits and dtype), and the size-8 top-K reduction keeps its one-row col_reduce_small layout (it depends only on the hidden width). Each row therefore keeps the bytes of its own one-row callAndWeightedReduce call, which is the existing contract. Only the launch count changes. Evidence: - CPU: the updated Gemma4PrefillExpertTests compare the shared call with the per-token oracle at 7 to 512 tokens and decode rows 2, 3 and 8, at 4 and 8 bits. They also check that the shared call is unsorted and the rectangular path still sorts. - GPU (M5 Ultra; opt-in GEMMA4_EXPERT_ROWS_GPU): the bytes are equal for 1 to 2048 rows at a geometry with the production kernel classes. They are also equal at hidden 2816, width 704 and 128 experts. - Synthetic timing of one expert block at production geometry (4-bit, debug build): 512 tokens 74.6 -> 9.8 ms; 2048 tokens 349.9 -> 36.8 ms. Not validated here: other GPU generations, full-model or pair runs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
callAndWeightedReduceRows now sorts 64 or more assignments by expert, so consecutive threadgroups read the same expert's weights. It does not pass the sorted hint to the gathers: every (row, expert) pair stays an M=1 gather_qmv product, rather than taking the sorted gather_qmm_rhs route. Rows return to their order (scatterUnsort) before the unchanged top-K reduction, so each row keeps the bytes of its own one-row call. The unused sortsAssignments switch is replaced by hintsSorted. Calls that hint sorted indices, and the MiMo NAX routes, are unchanged. Evidence: - Gemma4PrefillExpertTests on CPU: bytes equal; the one call carries every route, grouped by expert from 64 assignments, never hinted as sorted. - GPU equality at the production kernel classes and at production geometry (M5 Ultra). - Gemma4StagePagedSyntheticTests (GPU) and Gemma4BatchDecodeTests (CPU) pass. - Synthetic production-geometry expert layer, 4-bit: 2,048 tokens 37.2 -> 30.6 ms (per-token graph 369.5 ms); 512 tokens 9.8 -> 8.2 ms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- SwitchGLU.callAndWeightedReduceRows is package, not public: its only caller is Gemma4Experts. Its documentation names the profile its per-row guarantee does not cover: Qwen4's tiled expert kernels choose a route by assignment count. - Gemma4Experts takes the shared call through one branch for registered prompt frames and independent decode rows (the two bodies were identical). - Gemma4PrefillExpertTests checks a single row, and rows that all route to the same experts (every sort key tied), against one-row calls on the CPU. The opt-in GPU check adds tied rows at 9 and 513. Validation (debug darkbloom-cluster harness with this test file linked in, metallib 5b97893c from nested MLX 72dffa2d, M5 Ultra): - CPU: Gemma4PrefillExpertTests, Gemma4BatchDecodeTests, Gemma4BatchArithmeticAgreementTests, GemmaPrefillArithmeticTests, GemmaPrefixChunkTests, ResidentAdmissionGemma4Tests and Gemma4StagePagedSyntheticTests (GPU): 29 tests pass. - GEMMA4_EXPERT_ROWS_GPU=production: bytes equal at both geometries, 4 and 8 bits, tied rows included. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Proposed changes
The registered Gemma prompt policy ran each prompt token's expert block as its own subgraph, about six kernel launches per token per layer, and batch decode did the same per lane.
SwitchGLU.callAndWeightedReduceRowsnow projects all rows in one call. Pinned by Layr-Labs/d-inference#1704.callAndWeightedReducecall. Only the launch count changes.package, notpublic, because Qwen4's tiled expert kernels choose a route by assignment count; its documentation names that exception.Measured (from the commit messages; synthetic, not the full model): one expert layer at production geometry, 4-bit, 2,048 tokens 37.2 to 30.6 ms and 512 tokens 9.8 to 8.2 ms, on an M5 Ultra. Bytes equal the per-token oracle in
Gemma4PrefillExpertTestson the CPU, and on the GPU at production geometry withGEMMA4_EXPERT_ROWS_GPU.Conflict to expect:
Libraries/MLXLMCommon/SwitchLayers.swiftconflicts withfeat/mimo-v26-tensor-parallel, where both changeprojectExperts' signature and the down projection call. Land this PR last, then refresh it frommainwith the resolution already incb2fc27f, which keeps both parameters.Not validated here: other GPU generations, full-model and pair runs.
Merge with a merge commit, not a squash. The d-inference PRs pin these exact commits, and a merge commit keeps them on
main. Replaying the stack with squashes conflicts; with merge commits it is clean.The order for all six is in the 0.10.7 list:
feat/gemma4-tensor-parallel,feat/mimo-v26-tensor-parallel,feat/mimo-v26-tp-speculative,perf/qwen27-tp-row-exact,perf/qwen27-tp-prompt-wavefront, thenfeat/gemma4-batched-prompt-experts.Together with the other five branches, this one is in
integration/0.10.7-rehearsal(cb2fc27f), the SDK side of Layr-Labs/d-inference#1726.Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.
open PRs on this fork for d-inference work, so this was opened from a branch that was already pushed; no code changed
in opening it. It has had no human review.
🤖 Generated with Claude Code