perf: speed up top-p sampling for large vocabularies#1232
Open
NorbertKlockiewicz wants to merge 4 commits into
Open
perf: speed up top-p sampling for large vocabularies#1232NorbertKlockiewicz wants to merge 4 commits into
NorbertKlockiewicz wants to merge 4 commits into
Conversation
Replace the full-vocabulary sort in mask_topp with a logit-space histogram that finds the nucleus threshold in two O(n) passes, and skip exp() on masked logits in softmax. For Gemma's 262k vocab this cuts per-token sampling from ~45ms to ~10ms on device. The histogram approximates the exact sort-based nucleus; the sampled distribution is statistically equivalent. Authored with Claude. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
mkopcins
reviewed
Jun 15, 2026
mkopcins
reviewed
Jun 15, 2026
mkopcins
reviewed
Jun 15, 2026
mkopcins
reviewed
Jun 15, 2026
mkopcins
requested changes
Jun 15, 2026
msluszniak
reviewed
Jun 15, 2026
Use std::ranges::max_element over a std::span for the max scan, std::clamp for bin clamping, and explicit int32_t for bin indices. Authored with Claude. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
mkopcins
approved these changes
Jun 15, 2026
float is sufficient for the exp() accumulation over the vocab (error well below the histogram bin granularity); double bought no real precision. Accumulating in T directly is still avoided since T may be bf16, which saturates when summing many small terms. Authored with Claude. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Use acc_t = double when T is double, float otherwise. Every logit conversion is then a widening (or no-op), never a narrowing, regardless of which logit dtype instantiates the sampler. Accumulating in T itself stays avoided because bf16 saturates when summing exp() over the vocab. Authored with Claude. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Member
|
Code is ok, now testing |
msluszniak
approved these changes
Jun 15, 2026
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.
Description
Optimizes token sampling for large-vocabulary models (e.g. Gemma 4 E2B, 262k vocab), where the previous full-vocabulary sort in top-p dominated per-token latency.
Two changes in
sampler.cpp:mask_topp: replaces theO(n log n)sort over all logits with a logit-space histogram (kBins=2048) that locates the nucleus threshold in twoO(n)passes — no sort, no per-token vocab-sized allocation. Binning in logit space (rather than probability space) keeps uniform resolution for both peaked and flat distributions.softmax: skipsexp()on logits already masked tolowest()by top-k/top-p. The result underflows to zero anyway, and the call is slow on device.On an iPhone 17 Pro with Gemma 4 E2B (int4), per-token sampling drops from ~45 ms to ~10 ms. The histogram approximates the exact sort-based nucleus; the resulting sampled distribution is statistically equivalent (verified the kept-mass fraction stays within <1% of the exact nucleus across peaked, flat, and sharp distributions).
Introduces a breaking change?
Type of change
Tested on
Testing instructions
topPset (e.g. Gemma 4 E2B withtemperature: 0.8,topP: 0.9).Greedy decoding (
temperature: 0) is unaffected — it bypasses this path entirely.Screenshots
Related issues
Checklist
Additional notes
The histogram is an approximation bounded by bin granularity (
kBins=2048over akRange=40logit span). This is intentional: exact top-p over a 262k vocab where the nucleus can exceed 100k tokens is inherently expensive, and the sampling outcome is statistically indistinguishable from the exact version.