Repository navigation
server, common : cut per-token host time of n_probs and the top-k sampler (n_probs x1.35-1.8, ignore_eos +9-17%, plain +4-7% on H200; byte-identical output) - #389
Merged
Conversation
…sampler
Three host-side costs per generated token, all output-preserving:
- n_probs (pre-sampling probs): get_token_probabilities built a
{id, logit} array over the whole vocabulary, partial-sorted it and
took expf of every entry. It now takes the top n_probs straight from
the logits, repeating the libstdc++ partial_sort heap steps and
recording what partial_sort would move into the tail, so the softmax
sum is accumulated in exactly the old order. Terms below half an ulp
of the running sum (which starts at expf(0) = 1 for the maximum) are
skipped, since adding them leaves the float sum unchanged. Only the
returned entries and the sampled token are normalized.
- top-k fast path (#313): a logit bias whose biases are all -INFINITY
(ignore_eos, model suppress tokens) disabled it. The top k is now
taken from the logits with these tokens masked; the logit-bias sampler
then finds none of them in the top k, so the chain result is the same.
- the block checks of both top-k scans used 16-wide bool reductions,
which GCC unrolls completely and does not vectorize; 32-wide int
reductions vectorize.
Server responses (tokens, content, probs, logprobs) are byte-identical
to before.
This was referenced Oct 24, 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.
What
Cuts the per-token host time of
llama-serverin three places. Responses are byte-identical to before.n_probs(pre-sampling probabilities):get_token_probabilitiesno longer builds and partial-sorts a{id, logit}array over the whole vocabulary, and no longer callsexpfon every entry.ignore_eos: it is now kept when the logit bias only masks tokens. All biases at-INFINITYcoversignore_eosand model suppress tokens.intreduction instead of a 16-wideboolone).Why
On a 248k-vocabulary model, decode on an H200 runs at about 1.5 ms/token, so host work between tokens is a large share. Per token, measured with timers around each phase of
update_slots:populate_token_probs,n_probs=5ignore_eosThe full-vocab logits copy device->host is not a factor: it takes 23 µs, and
llama-benchmakes the same copy.--backend-samplingavoids the sampler cost, but it does not apply to pre-samplingn_probs/ OpenAIlogprobsrequests: the server turns it off for these. It also adds about 190 µs of host time per token tollama_decode. After this change, plain requests run as fast without-bsas with it.How
token_probs_topkrepeats the libstdc++std::partial_sortheap steps on the raw logits, the same way common : take the top-k straight from the logits when top-k starts the chain #313'stopk_from_logitsdoes. It also records the heap top thatpartial_sortwould move into each tail position, which gives the exact element order the old softmax loop summed in.expf(0) = 1, so once it is in[2^e, 2^(e+1)), any termexpf(x)withx < (e-24)*ln2 - 0.01is below half an ulp and leaves the float sum unchanged. These terms are skipped.get_token_probabilities, so the caller no longer scans the full array.n_probs >= n_vocab.common_sampler, if every logit bias is-INFINITY, the top k is taken from a copy of the logits with those tokens set tologit + (-INF), the same values the logit-bias sampler produces. The bias sampler, applied again by the chain to the k-array, finds none of these tokens (or only ones already at-INF), so it changes nothing. Finite biases still disable the fast path.BLOCK = 16withbool any |= ...: GCC unrolls the 16-iteration loop completely and SLP then fails on the reduction (confirmed with-fopt-info-vec).BLOCK = 32with anintreduction is vectorized in both translation units with the default x86-64 flags.Validation
memcmpof the outputs):get_token_probabilities: 6000 synthetic rows (normal, heavy ties, ascending input that evicts at every position, flat input whose sum reaches about 2^18, NaN/inf) and 800 real logit rows,n_probsfrom 1 to 100, including the probability of a random sampled token. 0 mismatches.topk_from_logits: 3800 rows. 0 mismatches.-bs, and speculative decoding with an MTP draft (K=2), at both the previous tip and the current tip. The requests cover:ignore_eos;n_probs1/3/5/10/50, plus post-sampling probs;top_k=0andtop_k=200;-infand finitelogit_bias, and repeat penalty;/v1/chat/completionswithlogprobs.tools/server/tests(test_completion.py,test_chat_completion.py,test_ignore_eos.py), run against a local model because the test models could not be downloaded: identical pass/fail sets before and after (61 passed, 32 failed on model-specific content asserts, 1 skipped).Numbers
Quantized hybrid model with a 248k vocabulary at three sizes (S/M/L), H200, greedy, 256 tokens,
-c 4096 -np 1,/completion. Values are tok/s, median of an ABBA run.ignore_eosn_probs=5n_probs=5,ignore_eos-bsignore_eosFor reference,
llama-benchtg256 gives 653 / 412 / 332 tok/s for S / M / L.Not changed here: under speculative decoding,
completion_probabilitiesis still not filled (the existing// TODO: set result.probs).The heap-step helper repeats #313's
topk_from_logits, with the eviction record added. I kept it local to the server rather than changing thecommonhelper's interface; happy to merge the two if preferred.