Skip to content

common : keep the fast top-k path when penalties only lower logits - #444

Open
sb32445 wants to merge 1 commit into
PrismML-Eng:prismfrom
sb32445:pr/sampler-topk-penalties
Open

sb32445 wants to merge 1 commit into
PrismML-Eng:prismfrom
sb32445:pr/sampler-topk-penalties

Conversation

@sb32445

@sb32445 sb32445 commented Nov 16, 2026 •

Copy link
Copy Markdown

Overview

#313 (merged) lets the sampler take the top-k straight from the logits when top-k starts the sampler chain. With a presence, frequency or repeat penalty the chain starts with the penalties sampler, so that fast path was switched off (common_sampler_fast_topk returned 0) and the full-vocabulary array (248k tokens for Qwen3.5 / Bonsai) was built and sorted for every token.

Penalties that only lower logits (penalty_repeat >= 1, penalty_freq >= 0, penalty_present >= 0) change at most penalty_last_n distinct tokens, and only downwards. So the top k + 1 after the penalties lies inside the top k + penalty_last_n + 1 of the raw logits, unless there is a tie at the border. The fast path now builds a shortlist of that size when the penalties sampler comes first. Everything else keeps the old path: penalties that can raise a logit (negative values), penalty_last_n < 0 or > 128 (and top_k > 128 as before).

Ties (review). The shortlist has to give the same selection as the full array, including ties. common_topk_shortlist_exact() applies the penalties sampler to a copy of the shortlist and accepts it only if (a) the k + 1 largest penalized values are pairwise different, so neither the order of the top k nor the membership at the border depends on the order in which the tokens are given, and (b) the (k + 1)-th is larger than the smallest raw logit in the shortlist, so no token outside it can reach or equal it. Otherwise common_sampler_sample falls back to the full array, as before the PR. With real-valued logits ties are rare, so the fast path is still taken almost always. Your example (logits [0, 1, 0, 0, 0, 0], top_k = 1, history [1], presence penalty 1) is refused and selects token 0 as the full path does.

RTX 4070, Bonsai 2 27B PTQ1_0 + MTP head (n-max 2), q4_0 K/V, llama-server, presence_penalty 0.5, temperature 0.6, top_k 20, top_p 0.95, penalty_last_n 64, against plain prism d12ca13bb, 4 interleaved pairs (4 prompts x 2 seeds, 256 tokens each, fixed seeds): 83.83 -> 92.11 tok/s, +9.87 % (95 % CI [+9.84, +9.90] %). The generated texts are identical to the full path in all runs. Without a penalty the speed is unchanged (94.02 -> 94.05 tok/s, +0.04 %, 2 pairs).

Additional information

  • One commit: common/sampling.cpp/.h (one new function and the check in common_sampler_sample), a new test and its line in tests/CMakeLists.txt. No new options, no change for requests without penalties or with penalty values that can raise logits.
  • Speed with a penalty is now about the speed without one (92.2 against 94.0 tok/s); before it was 11 % lower.
  • The result does not depend on the model; it removes a per-token sort of the whole vocabulary, so the gain is larger the faster the model decodes and the larger the vocabulary is. On a Qwen3.5 9B (Q8_0) the same change gave +3 to +4 % (earlier measurement on an older base); on a 35B-A3B MoE +10.7 % (separate fork).
  • Tests: new tests/test-sampling-topk-penalties.cpp (links llama-common). It builds the shortlist of the n largest raw logits (equal logits in arbitrary order), applies the penalties sampler and top-k to it, and compares with the same chain on the full array. Cases: the example above (shortlist refused, full path gives token 0); a case without ties (shortlist accepted, equal to the full path); 20000 random cases, half with few distinct logit values (many ties), half with near-continuous values and a single planted tie, random penalty values and histories: in 9660 the shortlist is accepted and its result equals the full array in entries and order, in 10340 it is refused. With the tie check removed the test fails on the first case, so it does cover the review. test-sampling passes as before.
  • The generated text of the 8 requests per run (fixed seeds) is identical with and without the change, in all runs (4 pairs with a penalty, 2 without).
  • test-backend-ops does not use common/sampling.cpp; I did not repeat the full suite for this change.
  • Not tested: other models, backend_sampling, several slots, penalty_last_n between 64 and 128 with large top_k.

Test results

  • Hardware / software: RTX 4070 12 GB (AD104, cc 8.9), Linux 6.18, NVIDIA driver 620.70, CUDA 13.4, GCC 16.2; Release build, -DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=89.
  • Base: prism at d12ca13bb.
  • Model: Ternary-Bonsai-2-27B (PTQ1_0) with a community MTP draft head (Q8_0), --spec-type draft-mtp --spec-draft-n-max 2, q4_0 K/V cache, one slot, --cache-ram 0, thinking off.
  • Method: separate builds of prism and the branch, one server start per run and arm, alternating A B B A, one warm-up request, speed = predicted tokens / predicted time of the server timings; paired differences with a 95 % bootstrap interval (script tools/sampler_ab.py of my notes repository, available on request).

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: The patches were developed with Claude Code (Anthropic's coding agent): it wrote the code, the measurement scripts and the first drafts of the commit messages and PR texts. I decided what to work on (which kernels and host paths to optimise, based on profiles of my own decode setup). The measurements and checks listed in the PR texts were run in the Claude Code sessions; I did not re-run them independently. I will maintain the changes. Commits where Claude Code was used carry a Co-Authored-By trailer.

🤖 Generated with Claude Code

@bri-prism bri-prism left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The shortlist preserves the top-k scores when penalties only lower logits, but it changes the candidate order seen by the final partial sort. That can change the selected token when scores tie. On Linux/libstdc++, with logits [0, 1, 0, 0, 0, 0], top_k=1, history [1], and a presence penalty of 1, the full path selects token 0 and the proposed fast path selects token 1. Please preserve the existing tie behavior or fall back when a tie can affect selection. Add this case to the sampling regression tests.

With presence/frequency/repeat penalties the chain started with the
penalties sampler, so the fast top-k path was off and the full-vocabulary
array was built and sorted per token. Penalties that only lower logits
(repeat >= 1, frequency >= 0, presence >= 0) touch at most penalty_last_n
distinct tokens, so the top k after them is inside the top k + penalty_last_n
of the raw logits. Use that as k. Other penalty values, penalty_last_n < 0 or
> 128 keep the old path.

The shortlist is the top k + penalty_last_n + 1 of the raw logits. After the
penalties the k + 1 largest values must be pairwise different, and the (k + 1)-th
must be larger than the smallest raw logit in the shortlist (no token outside it
can reach or tie it); otherwise the full array is used, so tie order and selection
are the same as before. test-sampling-topk-penalties compares both paths on random
logits with many ties and covers the case where the shortlist alone would pick
token 1 instead of 0 (logits [0, 1, 0, 0, 0, 0], top_k 1, history [1], presence 1).

Ternary-Bonsai-2-27B PTQ1_0 + MTP head, RTX 4070, llama-server, presence_penalty 0.5,
temperature 0.6, top_k 20, top_p 0.95, penalty_last_n 64, against prism 6447906
(4 interleaved pairs, 8 generations per run): 83.95 -> 92.18 tok/s (+9.8 %, 95 % CI
[+9.7, +9.9] %). The texts with a fixed seed are identical to the full path in all runs.
Without penalties the speed is unchanged (-0.15 %, 2 pairs).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@sb32445
sb32445 force-pushed the pr/sampler-topk-penalties branch from d43e399 to 38e87bc Compare November 21, 2026 09:25
@sb32445

sb32445 commented Nov 21, 2026

Copy link
Copy Markdown
Author

Thanks, you are right: the shortlist keeps the top-k scores but not the order in which equal scores reach the final sort, so ties could change the selection. I reproduced your case (logits [0, 1, 0, 0, 0, 0], top_k = 1, history [1], presence penalty 1: full path selects token 0, the shortlist token 1).

Changed (one commit, rebased on current prism d12ca13bb, was 6447906bf):

  • The shortlist is now the top k + penalty_last_n + 1 of the raw logits, and common_topk_shortlist_exact() decides whether it may be used. It applies the penalties sampler to a copy and accepts the shortlist only if the k + 1 largest penalized values are pairwise different and the (k + 1)-th is larger than the smallest raw logit in the shortlist (so no token outside it can reach or equal it). Otherwise common_sampler_sample uses the full array exactly as before the PR, so the tie behaviour is the existing one.
  • Your case is in the tests: new tests/test-sampling-topk-penalties.cpp runs the chain with and without the shortlist. It covers your example (shortlist refused, token 0), a case without ties (shortlist accepted and equal to the full path), and 20000 random cases with many equal logits and with near-continuous logits plus a planted tie: in 9660 the shortlist is accepted and gives the same entries and order as the full array, in 10340 it is refused. With the tie check removed the test fails on the first case. test-sampling passes as before.

Re-measured against plain prism d12ca13bb (RTX 4070, Bonsai 2 27B PTQ1_0 + MTP head, q4_0 K/V, llama-server, presence_penalty 0.5, temperature 0.6, top_k 20, penalty_last_n 64, 4 interleaved pairs, fixed seeds): 83.83 -> 92.11 tok/s, +9.87 % (95 % CI [+9.84, +9.90] %), generated texts identical in all runs, so the tie check does not cost the fast path in practice. Without a penalty: +0.04 % (2 pairs), texts identical. I did not repeat the full test-backend-ops suite (it does not use common/sampling.cpp).

The description has the new text and numbers.

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.

2 participants