Repository navigation
Gemma TP prefill: one shared expert dispatch per prompt chunk (1.87×, same tokens) - #1842
Merged
Merged
Conversation
…ift-lm 670c2a45) Pins mlx-swift-lm#387: a tensor-parallel rank's registered prompt chunk runs every token's experts in one shared dispatch per projection instead of one expert forward per token; every row's bytes are unchanged. scopedSlicesPackIntoOneCollective: a 24-token prompt chunk (96 routes, past the gather's sort at 64) joins the bitwise check against the split-sum oracle, and a prompt chunk must take one expert down-projection forward (red before: 6 and 24; green after). The helper now scopes every prompt case under the registered prompt policy. M5 Ultra pair, 9,651-token prompt: 569 -> 1,066 tok/s, same tokens. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…in 1068c346 - scopedSlicesPackIntoOneCollective observes layer 0's expert gate (ObservedGate) instead of mlx-swift-lm's removed public counter: one gate dispatch per prompt chunk, never hinted as sorted; new cases at 2 tokens and at the gather's sort threshold (16 x 4 = 64 routes). - promptChunkExpertsEqualOneTokenCallsOnGPU(bits: 4, 8): on the GPU, a rank's prompt chunk equals its one-token calls bit for bit (expert output, dense rows) at a 16-expert geometry and the registered rank geometry, 2-512 tokens. A sorted hint in expertRowsByRow fails both tests (46 issues). - Changelog: "nearly twice as fast". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Same tree as 6c5e1b70 (the reviewed head plus its formatting commit). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…op-K Test-only, behaviour-preserving: scopedSlicesPackIntoOneCollective takes each scope's rows and tokens from one table (no `?? 1` fallback), its doc no longer describes the old per-token slicing, and the GPU leg's top-K is one constant shared with its synthetic expert config. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
inference.md, Gemma tensor parallel, Prompt: SwitchGLU.expertRowsByRow, gathers never hinted as sorted, the same packed collective (docs-impact). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch had an error being 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 this does
Pins Layr-Labs/mlx-swift-lm#387 (
670c2a45): a Gemma 4 tensor-parallel rank's registered prompt chunk runs every token's experts in one shared dispatch per projection instead of one expert forward per token, then the same single packed collective (all 9 rows per token per layer, unchanged). Every row's bytes are unchanged.Merge order: mlx-swift-lm#387 is merged (
79a1ecc442, 06:15Z), and this PR is repinned to it (55aa36fc00; same tree as the reviewed6c5e1b70).Why
Gemma TP prefill measured ~10× below one Mac. Root cause (full write-up in the tp-perf REPORT, "Gemma TP prefill"):
Gemma4Experts.tensorParallelCalllooped over the chunk's tokens under the registered prompt policy: 512 expert forwards per layer per 512-token chunk. At 9,651 tokens this was 7.2 s of a 17.0 s prefill.Evidence
tp-check gemma run --mode tp --path paged, real weights and text, warm), prefill at 2,586 / 5,188 / 9,651 tokens: 561 / 564 / 569 → 1,060 / 1,044 / 1,066 tok/s. SametokenIDsSHA256at all three lengths.nice -n 19):scopedSlicesPackIntoOneCollectivenow also runs a 24-token prompt chunk (96 routes, past the gather's sort threshold of 64) bitwise against the split-sum oracle, and requires one expert down-projection forward per prompt chunk: red before (6 and 24 forwards), green after. The test helper had scoped only the exact name"prompt"under the prompt policy; it now covers every prompt case.Gemma4TensorParallelsuites: 43 tests pass at this head; allGemma4suites, 62 tests, pass with the fix.Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.Before / After
Code flow (one TP rank, one layer, one S-token prompt chunk)
flowchart LR subgraph Before A1[h2: 1×S×2816] --> L{for each of S tokens} L --> G1[SwitchGLU forward: gate, up, GeGLU, down<br/>K=8 routes, unsorted M=1 gathers] G1 --> L L --> P1[concat S×8 FP32 rows + S dense rows] P1 --> C1[one sealed all-sum] C1 --> R1[round BF16, router-weight, post-norm] end subgraph After A2[h2: 1×S×2816] --> G2[SwitchGLU.expertRowsByRow: one dispatch per projection<br/>rows grouped by expert, gathers never hinted sorted, each row M=1] G2 --> P2[S×8 FP32 rows in token order + S dense rows] P2 --> C2[the same one sealed all-sum] C2 --> R2[round BF16, router-weight, post-norm] endObservable behaviour
Validation
tp-check gemma run --mode tp --path paged, real QAT 4-bit weights, real text, chunk 512, warm, sealed): prefill 561 / 564 / 569 → 1,060 / 1,044 / 1,066 tok/s at 2,586 / 5,188 / 9,651 tokens. SametokenIDsSHA256at all three lengths.floatingReductionsare equal (4,204 / 4,504 / 4,984), withreductionsPerForward60. One rank's compute (--mode shard, 9.7K): 12.58 → 5.37 s.scopedSlicesPackIntoOneCollective(5 scopes: 6, 2, 16 (= 64 routes), 24 tokens and batch rows). Bitwise against the split-sum oracle, and each gate dispatch's sorted hint must befalse(an observing gate projection). Red before the fix (S dispatches); the reviewer's sorted-hint mutant fails it.promptChunkExpertsEqualOneTokenCallsOnGPU, bits 4 and 8, 16-expert and registered rank geometry, 2–512 tokens): bit for bit against one-token calls, one unsorted gate dispatch. It ran green on Mac A at 03:30Z, and the mutant fails it (46 issues). Not rerun since: no GPU on the M5 Ultras while production serves.2b6b7360cc): 16 tests in 3 suites pass. They areGemma4TensorParallelTests(all CPU tests), cluster: Gemma 4 tensor-parallel speculative rounds with the assistant (runtime and tp-check) #1818'saWindowsRowsAreThePlainStepsOnTheWholeModeland its rounds-request suite. cluster: Gemma 4 tensor-parallel speculative rounds with the assistant (runtime and tp-check) #1818's GPU tests (aWindowsRowsAreThePlainStepsOnBothRanks, greedy/sampled rounds) need the GPU paged host and wait for a window.docs-impactpasses. Theinference.mdGemma TP "Prompt" bullet names the shared dispatch.Limitations
Refactor pass (separate subagent,
refactor-1842.md)Test-only, at head
ac6cd473d5:rankTopK.No assertion was removed or loosened; 14/14 Gemma TP CPU tests pass. The fork side needed no change.