Reduce CPU-side per-token overhead in GenerateNextToken and SampleTopP - #2085
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR targets CPU-side per-token overhead reductions in the generation loop by (1) hoisting repeated per-token decisions (model/EP checks + sampling dispatch) into Generator construction, and (2) reducing Top-P sampling cost by avoiding an O(V) exp() pass in the nucleus-finding logic in search.cpp.
Changes:
- Cache per-token decisions in
Generator(Nemotron speech state handling, Phi-3 ROPE rewind threshold, and sampling method selection/validation) to reduce per-token branching/dynamic checks. - Update CPU Top-P sampling to use an adaptive partial-sort nucleus search with a log-space tail bound for early termination, eliminating the full-vocab softmax partition scan in common cases.
- Add disabled benchmarks to measure orchestration overhead and Top-P scaling across vocab sizes/distributions.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/generators.cpp |
Adds per-generator initialization (InitPerTokenCache) and uses cached sampling dispatch + Phi-3 ROPE rewind threshold during GenerateNextToken. |
src/generators.h |
Introduces cached fields (SamplingMethod, ROPE threshold, EP/model flags) used to avoid per-token checks. |
src/search.cpp |
Reworks FindNucleus/SampleTopP to use a tail-bound early-termination strategy, removing the prior full-vocab exp-sum step in typical cases. |
test/sampling_benchmark.cpp |
Adds disabled benchmarks intended to isolate GenerateNextToken overhead and characterize Top-P scaling behavior. |
baijumeswani
previously approved these changes
Apr 16, 2026
…zePhi3RopeThreshold
baijumeswani
approved these changes
Apr 16, 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.
This PR is to reduce CPU-side per-token costs in two areas:
1. Cache per-token decisions at construction (generators.cpp, generators.h)
dynamic_cast<NemotronSpeechState*>with a one-time check at construction (is_nemotron_speech_model_)SamplingMethodenum +switch), replacing per-token conditional chain and moving parameter validation to construction2. Eliminate O(V) exp() pass in SampleTopP (search.cpp)
FindNucleus: after partial-sorting K elements, bound the unsorted tail as(V-i-1) * exp(score_i)and terminate early whenprefix_sum >= p * (prefix_sum + tail_bound)std::exp()per token (128K-200K for production models)Benchmarks (Batch=1, CPU, Release build)
SampleTopP (peaked distribution, p=0.95):
Consistent 65-81% reduction across distribution shapes and top_p values (0.80-0.99).
Orchestration overhead (V=1000, greedy — isolates dispatch path):
Adds two
DISABLED_benchmark tests for reproducibility.