Skip to content

Gemma TP: build sampled steps one ahead too (stacked on #1822) - #1824

Merged
jonathan308 merged 4 commits into
devfrom
perf/tp-gemma-pipelined-sampled
Nov 5, 2026
Merged

jonathan308 merged 4 commits into
devfrom
perf/tp-gemma-pipelined-sampled

Conversation

@jonathan308

@jonathan308 jonathan308 commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #1822 (base perf/tp-gemma-pipelined-steps). Retarget to dev once #1822 merges. I stacked it rather than folding it in so #1822's review stays as it is; the diff here is only the sampled part.

What

Gemma tensor parallel now builds sampled steps one ahead as well (#1822 did greedy only). Most real traffic is sampled.

  • Keyed draws are unchanged: the same token for the same key and position. Rank 0's draw is QwenStageTokenSampler.sample, i.e. the product's CBv2DefaultSampler:

    • its penalty and bias state advances on the device with each drawn token (LogitsPipelineV2.commit(sampledTokens:) inside the draw);
    • its keyed RNG step index advances with each draw (SamplerV2.commit());
    • the host history (selected) only configures it at the first draw, and the stepIndex argument is telemetry only.

    So step k+1's draw, built before step k's token is read, makes exactly the calls in exactly the order the read-then-build loop makes. This is the same property the product's chained single-Mac decode relies on (pendingSampledTokens).

  • Logprobs (sampled): they come from rank 0's own draw, as graph nodes with no collective. Each step now carries its own gather: takePendingStepLogprobs() right after the step is built, read with tokenLogprobs once its token is agreed. Before, a single slot would have been overwritten by the step built ahead.

  • Greedy requests that ask for logprobs keep reading each step first. Their logprobs come from a vocabulary-split top-k gather, which is a collective evaluated on the host after the token is agreed. Built ahead, it would queue behind the next step and gain nothing. Building that gather lazily into the step's own graph is a possible follow-up.

Tests

  • Red → green: Gemma4TensorParallelRuntimeTests.greedyRequestsBuildTheirStepsAhead now expects sampled requests, with and without logprobs, to build ahead, and greedy-with-logprobs not to. With Gemma TP: build greedy steps one ahead (GLM's step order) #1822's rule it records 2 issues (pipelines(temperature: 1, …)); with this change it passes. Run locally (CPU, swift test).
  • Keyed-draw parity, new: SamplingSelectionTests.drawingOneStepAheadTakesTheStepByStepTokensAndLogprobs. It uses temperature 0.9, top-p 0.95, repetition/frequency/presence penalties, logit bias and 3 logprobs. Drawing each step before reading the previous one gives the same 12 tokens, logprobs, top IDs and history as draw-read-commit. Run locally on the CPU device: passes.
  • Two-rank parity, new, GPU: Gemma4TensorParallelRuntimeTests.sampledStepsBuiltAheadTakeTheReadThenBuildTokens. On the synthetic checkpoint over two in-process ranks, sampled build-ahead and read-then-build give equal tokens, finish, published tokens and logprobs, both plainly and with an early owner stop. It uses Gemma4TensorParallelRequestLoop.buildsAhead, a test-only @TaskLocal. Queued for the next GPU slot, along with Gemma TP: build greedy steps one ahead (GLM's step order) #1822's suites.
  • Gemma TP: build greedy steps one ahead (GLM's step order) #1822's model-free two-rank loop checks still pass (the loop is unchanged).

Docs: docs/architecture/inference.md (Gemma 4 tensor parallel). Changelog: changelog.d/gemma-tensor-parallel-builds-sampled-steps-ahead.md.

Gain: not measured; the Gemma pair needs a hardware slot. The upper bound is the host share of a step. On GLM, building ahead took the step from 13.4 to 12.0 ms.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Hardware results (M5 Ultra, 2026-11-05 19:55Z, Mac A load 5.7–6.1)

PASS 4/4 at e9e60ed102: sampledStepsBuiltAheadTakeTheReadThenBuildTokens (tokens, finish, published, logprobs, owner stop equal to read-then-build), theRequestLoopEndsBothRanksAfterTheSameStep, greedyRequestsBuildTheirStepsAhead, theRequestLoopContinuesARestoredPrefixWithTheColdTokens.

Rank 0's draw is the product's CBv2DefaultSampler, which advances its
penalty counts with each drawn token on the device and its keyed step index
with each draw; QwenStageTokenSampler.selected only configures it at the
first draw. A draw built before the previous token is read is therefore the
draw at its output index with the history up to it. Each step keeps its own
lazy logprob gather (takePendingStepLogprobs), read after its token is
agreed. Greedy requests asking for logprobs still read each step first
(their top-k gather is a host-evaluated collective).

Tests: pipelines() covers sampled requests (red on #1822: 2 failures);
SamplingSelectionTests draws one step ahead and matches step-by-step tokens
and logprobs (CPU); a GPU suite compares sampled build-ahead with
read-then-build on two ranks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@blacksmith-sh

This comment has been minimized.

@vercel

vercel Bot commented Nov 5, 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 5, 2026 8:27pm UTC
d-inference-console-ui-dev Error Error Nov 5, 2026 8:27pm UTC
d-inference-landing Ready Ready Preview Nov 5, 2026 8:27pm UTC
darkbloom-status Ready Ready Preview Nov 5, 2026 8:27pm UTC
eigen-homepages-darkbloom Ready Ready Preview Nov 5, 2026 8:27pm UTC

Request Review

jonathan308 and others added 3 commits November 5, 2026 12:21
…in the loop's step

decode and submitDecode built the same forward (stride, decode frame, last
row) twice; it is now decodeRow, so the built-ahead step is the
read-then-build step's graph by construction. The request loop's step
closure builds its select once for both orders. No behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The docs paragraph keeps #1822's late-stop exception and this PR's sampled
part (now naming greedy requests with logit bias or penalties).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review of #1824 (nits 3 and 5): the changelog names greedy requests with
logit bias or penalties (they draw through the sampler and are built
ahead) and the one late-stop stream edge; the buildsAhead TaskLocal is
marked tests-only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonathan308
jonathan308 changed the base branch from perf/tp-gemma-pipelined-steps to dev November 5, 2026 22:27
@jonathan308
jonathan308 merged commit 1840617 into dev Nov 5, 2026
18 of 22 checks passed
jonathan308 added a commit that referenced this pull request Nov 5, 2026
QwenStageTokenSampler.takePendingStepLogprobs came in from #1824 too: one
copy kept, its doc naming both users (Gemma's one-token loop, Qwen's
build-ahead). #1820's stride, #1822/#1824's CBv2OwnedRequestState.submit and
#1830 are kept as on dev.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jonathan308 added a commit that referenced this pull request Nov 5, 2026
Brings in the Gemma TP build-ahead steps (#1822, #1824) and the bench layer
stride (#1827). Conflict in tp-check's `gemma run` resolved by keeping both:
`--mtp-depth`/`--assistant-dir` beside `--layer-stride`, both bound into the
ranks' workload digest. Speculative rounds keep their own loop (runRounds),
which reads each round's agreement before it builds the next round; the plain
loop's build-ahead is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch had an error being deployed

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