Repository navigation
Conversation
bri-prism
left a comment
There was a problem hiding this comment.
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>
d43e399 to
38e87bc
Compare
|
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 Changed (one commit, rebased on current
Re-measured against plain The description has the new text and numbers. |
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_topkreturned 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 mostpenalty_last_ndistinct tokens, and only downwards. So the topk + 1after the penalties lies inside the topk + penalty_last_n + 1of 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 < 0or> 128(andtop_k > 128as 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) thek + 1largest 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. Otherwisecommon_sampler_samplefalls 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_n64, against plainprismd12ca13bb, 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
common/sampling.cpp/.h(one new function and the check incommon_sampler_sample), a new test and its line intests/CMakeLists.txt. No new options, no change for requests without penalties or with penalty values that can raise logits.tests/test-sampling-topk-penalties.cpp(linksllama-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-samplingpasses as before.test-backend-opsdoes not usecommon/sampling.cpp; I did not repeat the full suite for this change.backend_sampling, several slots,penalty_last_nbetween 64 and 128 with largetop_k.Test results
-DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=89.prismatd12ca13bb.--spec-type draft-mtp --spec-draft-n-max 2, q4_0 K/V cache, one slot,--cache-ram 0, thinking off.prismand 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 servertimings; paired differences with a 95 % bootstrap interval (scripttools/sampler_ab.pyof my notes repository, available on request).Requirements
🤖 Generated with Claude Code