Skip to content

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
bri-prism merged 1 commit into
prismfrom
server-nprobs-fast
Oct 24, 2026
Merged

bri-prism merged 1 commit into
prismfrom
server-nprobs-fast

Conversation

@bri-prism

Copy link
Copy Markdown
Collaborator

What

Cuts the per-token host time of llama-server in three places. Responses are byte-identical to before.

  1. n_probs (pre-sampling probabilities): get_token_probabilities no longer builds and partial-sorts a {id, logit} array over the whole vocabulary, and no longer calls expf on every entry.
  2. Top-k fast path (common : take the top-k straight from the logits when top-k starts the chain #313) with ignore_eos: it is now kept when the logit bias only masks tokens. All biases at -INFINITY covers ignore_eos and model suppress tokens.
  3. Top-k block check: the check in both top-k scans now vectorizes (32-wide int reduction instead of a 16-wide bool one).

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:

cost per token (smallest model) before after
populate_token_probs, n_probs=5 1450 µs 200 µs
CPU sampler, ignore_eos 400 µs 105 µs
CPU sampler, plain request 165 µs 51 µs

The full-vocab logits copy device->host is not a factor: it takes 23 µs, and llama-bench makes the same copy.

--backend-sampling avoids the sampler cost, but it does not apply to pre-sampling n_probs / OpenAI logprobs requests: the server turns it off for these. It also adds about 190 µs of host time per token to llama_decode. After this change, plain requests run as fast without -bs as with it.

How

  • Top n_probs. token_probs_topk repeats the libstdc++ std::partial_sort heap steps on the raw logits, the same way common : take the top-k straight from the logits when top-k starts the chain #313's topk_from_logits does. It also records the heap top that partial_sort would move into each tail position, which gives the exact element order the old softmax loop summed in.
  • Skipping negligible terms. The sum starts with expf(0) = 1, so once it is in [2^e, 2^(e+1)), any term expf(x) with x < (e-24)*ln2 - 0.01 is below half an ulp and leaves the float sum unchanged. These terms are skipped.
  • Probabilities. Only the returned entries and the sampled token are divided by the sum. The sampled token's probability is now an output of get_token_probabilities, so the caller no longer scans the full array.
  • Fallback. The old code path is kept for backend-sampled outputs and for n_probs >= n_vocab.
  • Masked top-k. In common_sampler, if every logit bias is -INFINITY, the top k is taken from a copy of the logits with those tokens set to logit + (-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.
  • Vectorizing the checks. BLOCK = 16 with bool any |= ...: GCC unrolls the 16-iteration loop completely and SLP then fails on the reduction (confirmed with -fopt-info-vec). BLOCK = 32 with an int reduction is vectorized in both translation units with the default x86-64 flags.

Validation

  • Exactness against the old functions (both extracted and compiled side by side, memcmp of 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_probs from 1 to 100, including the probability of a random sampled token. 0 mismatches.
    • topk_from_logits: 3800 rows. 0 mismatches.
  • Server JSON identity, before vs after, 24 requests (timings stripped). Every set is byte-identical: three model sizes, -bs, and speculative decoding with an MTP draft (K=2), at both the previous tip and the current tip. The requests cover:
    • greedy, with and without ignore_eos;
    • pre-sampling n_probs 1/3/5/10/50, plus post-sampling probs;
    • temperature sampling with seeds, including top_k=0 and top_k=200;
    • -inf and finite logit_bias, and repeat penalty;
    • streaming;
    • /v1/chat/completions with logprobs.
  • 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.

request S before S after M before M after L before L after
plain 566 604 (+6.7%) 376 393 (+4.5%) 309 320 (+3.6%)
ignore_eos 499 585 (+17%) 344 384 (+11%) 289 315 (+9%)
n_probs=5 308 538 (+75%) 241 362 (+50%) 213 288 (+35%)
n_probs=5, ignore_eos 286 519 (+82%) 228 353 (+55%) 203 282 (+39%)
plain, -bs 613 609 395 394 322 323
MTP K=2 (L), ignore_eos 438 528 (+20%)

For reference, llama-bench tg256 gives 653 / 412 / 332 tok/s for S / M / L.

Not changed here: under speculative decoding, completion_probabilities is 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 the common helper's interface; happy to merge the two if preferred.

…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.
@bri-prism
bri-prism merged commit ac9c5fe into prism Oct 24, 2026
8 checks passed
bri-prism added a commit that referenced this pull request Oct 25, 2026
…(fix -Werror build after #389) (#400)

#389 added fast_topk_mask and logits_masked without listing them in common_sampler_init's aggregate initializer, which fails -Werror=missing-field-initializers on arm64/macOS. No behaviour change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant