Skip to content

Gemma 4 TP: one shared expert dispatch per registered prompt chunk (prefill 1.87×, same bytes) - #387

Merged
jonathan308 merged 3 commits into
mainfrom
perf/gemma4-tp-prefill-shared-experts
Nov 6, 2026
Merged

jonathan308 merged 3 commits into
mainfrom
perf/gemma4-tp-prefill-shared-experts

Conversation

@jonathan308

@jonathan308 jonathan308 commented Nov 6, 2026 •

Copy link
Copy Markdown

What this does

Under the registered prompt policy, a Gemma 4 tensor-parallel rank ran one SwitchGLU forward per token of each prompt chunk (Gemma4Experts.tensorParallelCall, byToken: 512 expert forwards per layer per 512-token chunk), then one collective. The unsharded model under the same policy makes one shared call (callAndWeightedReduceRows).

The rank now computes every token's expert rows in one shared dispatch per projection (SwitchGLU.expertRowsByRow): the gathers are not told the rows are sorted, so each (token, expert) pair stays the native M=1 product its own call computes. The rows go into the same single packed collective with the dense partial and are weighted as before. Every row's bytes are unchanged; only the dispatch count changes (S → 1 per layer).

TensorParallelSwitchLinear.forwards (a task-local counter, nil by default) lets tests count the projection's forwards.

Evidence

  • M5 Ultra pair over JACCL (darkbloom-cluster-tp-check gemma run --mode tp --path paged, real QAT 4-bit weights, real text, chunk 512, warm): prefill 561 / 564 / 569 → 1,060 / 1,044 / 1,066 tok/s at 2,586 / 5,188 / 9,651 tokens. Tokens are identical (same tokenIDsSHA256) at all three lengths.
  • One rank's compute alone (--mode shard, 9,651 tokens): 12.59 s → 5.37 s.
  • Test (in d-inference, Gemma4TensorParallelTests.scopedSlicesPackIntoOneCollective): the TP expert output and dense sum equal the split-sum oracle bit for bit for a 6-token chunk and a 24-token chunk (96 routes, past the gather's sort threshold of 64). A new check requires one expert down-projection forward per prompt chunk: it fails without this change (6 and 24 forwards) and passes with it.

Root-cause write-up (Gemma TP prefill ~10× slower than one Mac): the per-token expert loop, the registered M=1 prompt policy (4.4× on one Mac too), and the 9-row exchange (3.38 MB per token). This PR fixes the first.

Review fixes (1068c346, 03:31Z)

  • Sorted hint, checked on the GPU:
    • the d-inference test now observes the gate projection's sortedIndices (must be false) on CPU chunks of 2, 6, 16 (exactly 64 routes) and 24 tokens;
    • a new GPU test holds a rank's prompt chunk bit for bit against its one-token calls at a 16-expert geometry and the registered rank geometry (2–512 tokens);
    • the reviewer's mutant (hintsSorted: true in expertRowsByRow) now fails both, with 46 issues including the GPU bytes.
  • Dead branches removed: after the prompt chunk's early return, tensorParallelCall's loop serves only batch decode rows (count = B, row slices, axis 0), with the same ops for those rows.
  • One rows path: callAndWeightedReduceRows is now weightedExpertSum(expertRowsByRow(...)), the identical graph.
  • Public test counter dropped: TensorParallelSwitchLinear.forwards was read on every expert down forward; the test observes the gate instead.

AI usage

Written with Claude (Co-Authored-By trailer on each commit). Reviewed by an independent review pass; every claim above was re-run.

Checklist

  • fork.yaml section updated (Gemma 4)
  • changelog.d updated in place
  • Exact pin: d-inference#1842 repins to this PR's merge commit after merge

Under the registered prompt policy a tensor-parallel rank ran one
SwitchGLU forward per token of the chunk (512 per layer per 512-token
chunk), then one collective. It now computes every token's expert rows
in one shared dispatch per projection (SwitchGLU.expertRowsByRow: gathers
not told the rows are sorted, so each (token, expert) pair stays the native
M=1 product its own call computes, as callAndWeightedReduceRows does for
the unsharded model), packs them with the dense partial into the same
single collective, and weights them as before.

M5 Ultra pair over JACCL, 9,651-token prompt: 569 -> 1,066 tok/s, same
tokens at 2.6K/5.2K/9.7K. TensorParallelSwitchLinear.forwards counts the
projection's forwards (tests).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jonathan308 and others added 2 commits November 5, 2026 19:31
…ne rows path

- TensorParallelSwitchLinear.forwards (public, read on every expert down
  forward) is removed: d-inference's test observes the gate projection
  instead and also checks every gather's sorted hint (false).
- tensorParallelCall's remaining loop serves only scoped batch decode rows:
  the byToken branches after the prompt chunk's early return are gone (count
  = B, row slices, axis 0); same ops for those rows.
- callAndWeightedReduceRows is weightedExpertSum(expertRowsByRow(...)): the
  identical graph, one rows path for the unsharded and the TP prompt chunk.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonathan308
jonathan308 merged commit 79a1ecc into main Nov 6, 2026
22 checks passed
jonathan308 added a commit that referenced this pull request Nov 6, 2026
The changelog keeps both entries (the parts entry now states the measured
best: 4 parts, +13 %). Two new lines over 100 columns are wrapped (a local
Part typealias).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jonathan308 added a commit that referenced this pull request Nov 6, 2026
The changelog keeps main's entries, then this branch's; fork.yaml merged
without conflict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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