Skip to content

Gemma TP prefill: one shared expert dispatch per prompt chunk (1.87×, same tokens) - #1842

Merged
jonathan308 merged 5 commits into
devfrom
perf/gemma-tp-prefill-shared-experts
Nov 6, 2026
Merged

jonathan308 merged 5 commits into
devfrom
perf/gemma-tp-prefill-shared-experts

Conversation

@jonathan308

@jonathan308 jonathan308 commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

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 reviewed 6c5e1b70).

Why

Gemma TP prefill measured ~10× below one Mac. Root cause (full write-up in the tp-perf REPORT, "Gemma TP prefill"):

  1. This PR: Gemma4Experts.tensorParallelCall looped 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.
  2. The registered M=1 prompt policy itself (one Mac under the same policy: 1,162 tok/s vs 5,064 for the single-Mac product). Not changed here.
  3. The 9-row FP32 exchange: 3.38 MB per token, ~41 % of the remaining time. Not changed here.

Evidence

  • M5 Ultra pair, JACCL (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. Same tokenIDsSHA256 at all three lengths.
  • Tests (CPU, nice -n 19): scopedSlicesPackIntoOneCollective now 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.
  • Gemma4TensorParallel suites: 43 tests pass at this head; all Gemma4 suites, 62 tests, pass with the fix.

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with 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]
  end
Loading

Observable behaviour

flowchart LR
  B["Before: 9,651-token prompt on two M5 Ultras<br/>prefill 569 tok/s"] --> A["After: 1,066 tok/s<br/>same tokens (tokenIDsSHA256), same 60 collectives per forward"]
Loading

Validation

  • Pair, M5 Ultras over JACCL (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. Same tokenIDsSHA256 at all three lengths. floatingReductions are equal (4,204 / 4,504 / 4,984), with reductionsPerForward 60. One rank's compute (--mode shard, 9.7K): 12.58 → 5.37 s.
  • CPU, this head: 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 be false (an observing gate projection). Red before the fix (S dispatches); the reviewer's sorted-hint mutant fails it.
  • GPU (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.
  • Combined with cluster: Gemma 4 tensor-parallel speculative rounds with the assistant (runtime and tp-check) #1818 (CPU, local scratch merge at 2b6b7360cc): 16 tests in 3 suites pass. They are Gemma4TensorParallelTests (all CPU tests), cluster: Gemma 4 tensor-parallel speculative rounds with the assistant (runtime and tp-check) #1818's aWindowsRowsAreThePlainStepsOnTheWholeModel and 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-impact passes. The inference.md Gemma TP "Prompt" bullet names the shared dispatch.

Limitations

Refactor pass (separate subagent, refactor-1842.md)

Test-only, at head ac6cd473d5:

  • one explicit scope → (rows, tokens) table;
  • the doc comment updated for the shared dispatch;
  • a named rankTopK.

No assertion was removed or loosened; 14/14 Gemma TP CPU tests pass. The fork side needed no change.

…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>
@vercel

vercel Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
d-inference Error Error Nov 6, 2026 6:47am UTC
d-inference-console-ui-dev Error Error Nov 6, 2026 6:47am UTC
d-inference-landing Ready Ready Preview Nov 6, 2026 6:47am UTC
darkbloom-status Ready Ready Preview Nov 6, 2026 6:47am UTC
eigen-homepages-darkbloom Ready Ready Preview Nov 6, 2026 6:47am UTC

Request Review

…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

2 failed and 3 active deployments
Preview – eigen-homepages-darkbloom — ca5dc62d Deployed Nov 6, 2026 by vercel[bot]
Preview – d-inference-landing — ca5dc62d Deployed Nov 6, 2026 by vercel[bot]
Preview – d-inference — ca5dc62d Deployed Nov 6, 2026 by vercel[bot]
Preview – darkbloom-status — ca5dc62d Deployed Nov 6, 2026 by vercel[bot]
Preview – d-inference-console-ui-dev — ca5dc62d Deployed Nov 6, 2026 by vercel[bot]
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.

1 participant