Skip to content

Gemma 4 TP: prompt chunks in row parts, each part's sums issued as built (off by default; GPU-exact for two ranks only) - #388

Merged
jonathan308 merged 4 commits into
mainfrom
perf/gemma4-tp-prefill-overlap
Nov 6, 2026
Merged

jonathan308 merged 4 commits into
mainfrom
perf/gemma4-tp-prefill-overlap

Conversation

@jonathan308

@jonathan308 jonathan308 commented Nov 6, 2026 •

Copy link
Copy Markdown

What this does (stacked on #387)

Gemma4TensorParallelPromptParts.parts (task-local, 1 by default): a Gemma 4 tensor-parallel rank can run a registered prompt chunk's work after attention in consecutive row parts.

  • The attention core still runs the whole chunk. The output projection's FP32 partial is taken unsummed (Gemma4TensorParallelContext.deferred).
  • Every part's attention sum is issued first, then every part's feed-forward collective (Gemma4Experts.tensorParallelPromptSum, asyncEval as built), then every part's rounding and norms. The GPU computes one part while the link carries the previous part's sum.
  • Under the registered prompt policy every op after the attention core keeps each row's one-row bytes (projections M=1, including the quantized router; norms and residuals per row; experts by row as in Gemma 4 TP: one shared expert dispatch per registered prompt chunk (prefill 1.87×, same bytes) #387). So, on the GPU for two ranks, every value is the whole chunk's (conditions below), and the same 9 rows per token cross the link.
  • Only chunks whose experts take the shared one-row dispatch (BF16, expert prefill) split; others run whole.

Evidence

d-inference test promptPartsKeepEveryBitAndSplitEachChunksCollectives(parts: 2, 4) (two in-process ranks, paged host):

  • tokens, top candidates, logprobs and final row are bit-identical to one part;
  • each part takes its own attention and feed-forward collective, with the same total rows;
  • red with the parts path disabled.

The pair speedup is not yet measured; the GPU commands are queued for the next M5 Ultra window. Estimate: 1,066 → ~1,300 (2 parts) / ~1,450 (4 parts) tok/s at 9.7K.

Measured (M5 Ultra pair, JACCL, 2026-11-06 03:21–03:25Z) — off by default

tp-check gemma run --mode tp --prompt-parts N, real QAT 4-bit weights and text, warm, sealed:

Parts 2.6K 5.2K 9.7K
1 1,055 1,038 1,061–1,064
2 960 1,023 1,040–1,045
4 1,106 1,167 1,196–1,208 (+13 %)
8 — — 1,183–1,195
  • Tokens are identical at every length and part count (same tokenIDsSHA256).
  • 4 parts is the measured best. One rank's compute rises 3.5 % with 4 parts (--mode shard), and ~1.0 s of the 3.7 s exchange is hidden. The next layer's attention needs every row, so the last part's sum stays exposed.
  • The product default stays 1 (off); d-inference#1843 carries the knob.

Update (07:40Z)

Merged main (79a1ecc4, #387) into this branch: signed merge, no rebase. The changelog keeps both entries, and two lines over 100 columns are wrapped.

AI usage

Written with Claude (Co-Authored-By trailer).

Exactness: conditions (independent review, review-388-1843.md; 10:56Z)

The earlier wording ("never a value", "every value unchanged") overstated it. Precisely:

  • Bit-identical on the GPU, for two ranks, while every op after attention computes each row alone whatever the row count (today's kernels do) and the cross-rank sum is one add per element.
  • Not on the CPU backend: RMSNorm's fallback runs vector steps across rows.
  • Not exact with more than two ranks: the collective's per-element sum order depends on its size.
  • Not enforced at production size, so the product keeps it off (promptChunkParts = 1). Only tp-check --prompt-parts sets it.

The doc comments, the changelog entry and fork.yaml now say this (cd33b911). Refactor pass: 3e74a460.

Gemma4TensorParallelPromptParts.parts (task-local, 1 by default): a
tensor-parallel rank runs a registered prompt chunk's work after attention
in consecutive row parts. The attention output projection's partial is
taken unsummed (Gemma4TensorParallelContext.deferred); every part's
attention sum is issued, then every part's feed-forward collective
(Gemma4Experts.tensorParallelPromptSum, asyncEval as built), then every
part's rounding and norms, so one part's sum crosses the link while the
next computes. The attention core still runs the whole chunk; every later
op keeps each row's one-row bytes under the registered prompt policy, so
every value is the whole chunk's. Only chunks whose experts take the
shared one-row dispatch (BF16, expert prefill) split.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
jonathan308 changed the base branch from perf/gemma4-tp-prefill-shared-experts to main November 6, 2026 07:40
jonathan308 and others added 2 commits November 6, 2026 00:34
…ctive, one down-projection guard

- Gemma4TensorParallelContext.withoutCollective(_:) runs a layer's attention
  without its output projection's collective and returns the dtype that sum
  rounds to. DeferredSum and its task-local are now private to the context,
  so the decoder layer no longer binds the task-local or checks the dtype
  itself. The name follows TensorParallelSwitchLinear.withoutCollective,
  which does the same for the experts' down projection.
- Gemma4Experts.tensorParallelDown holds the down-projection cast and its
  failure message once, for tensorParallelCall and tensorParallelPromptSum.
- minimumRows says why a part keeps more than one row: it stays a registered
  prompt frame, so its projections and experts take the whole chunk's
  one-row paths.

The same ops are built in the same order and the same arrays are issued:
no value, default or schedule changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Documentation only. The SDK doc comments, the changelog entry and fork.yaml
no longer say "never a value" / "values are unchanged": the parts are
bit-identical on the GPU for two ranks while every op after attention is
per-row; not on the CPU backend (RMSNorm's fallback); not exact with more
than two ranks (the collective's per-element sum order depends on its
size); nothing enforces it, so it stays off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonathan308 jonathan308 changed the title Gemma 4 TP: prompt chunks in row parts, each part's sums issued as built (same bytes; off by default) Gemma 4 TP: prompt chunks in row parts, each part's sums issued as built (off by default; GPU-exact for two ranks only) Nov 6, 2026
@jonathan308
jonathan308 merged commit 62b29ec into main Nov 6, 2026
22 checks passed
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