Expand description
Token sampling — temperature, top-p, top-k, min-p, repetition penalty.
Randomness comes from an explicit SplitMix64 PRNG carried by the
caller: reproducible with a seed, unbiased across the whole CDF
(the v1 subsec_nanos source could never pick past ~23% of it).
Structs§
- Sampler
Config - Sampling configuration.
- Sampler
Scratch - Reusable per-pipeline sampling workspace. The epoch table lets the repetition penalty visit each token id once without allocating a HashSet or clearing a vocab-sized boolean vector on every decode step.
- Split
Mix64 - SplitMix64 — tiny, fast, statistically solid for sampling.
Constants§
- SPARSE_
TOPK_ MAX - Largest top-k the sparse chain serves. Past this the dense chain is the better tool anyway.
Functions§
- argmax
- Greedy: index of the maximum value.
- argmax_
penalized - Greedy over the PENALIZED logits without the working copy: one pass
that applies the repetition / presence penalty and the suppress list
on the fly (a membership table over the vocab, built from the past
tokens) and keeps the argmax with the same tie rule as
argmax(highest index among equal maxima). Bit-identical tochain+argmaxfor temperature 0 — the values compared are the same expressions — and it is what a greedy decode with penalties pays per token, and what a speculative round pays per draft and per verified row (nine such passes a round at k=4). - distribution_
into - The distribution the sampler would draw from — the whole chain minus
the draw — as a normalized vector over the vocab, in
out. Greedy configs (and the filtered-out fallback) come back as a one-hot, so a caller can treat every configuration uniformly. This is what speculative SAMPLING needs from both the draft head and the verify: accept-with-min(1, p/q), correct from max(0, p − q). - draw
- Draw from a normalized distribution with the caller’s RNG.
- draw_
sparse - Draw from a sparse distribution: inverse CDF in id order — the same
walk the dense
categorical_samplemakes over the vocab, so a seed lands on the same token when the survivor set and probs agree. - sample
- Sample next token from logits. Chain order is fixed: rep-penalty → temperature → softmax → min-p → top-k → top-p → sample.
- sample_
with_ scratch - Sampling entry point for hot decode loops with reusable scratch storage.
- sample_
with_ scratch_ pool - The same chain with the whole-vocab passes spread over the CPU pool.
- sparse_
distribution_ into - The sampler chain’s distribution as a SPARSE list — the same
distribution
chainbuilds over the whole vocab, for configs with a top-k, at a fraction of the cost. The dense chain copies the vocab, exponentiates it, selects, filters and normalises it — six or seven passes over 248k floats — and every one of them past the selection touches only the k survivors. Here: penalties on a copy ONLY when there are penalties, one pooled pass that selects the top-k penalized logits, one pooled pass for the vocab-wide softmax denominator (top-p is defined against the FULL normalisation, so the denominator must see every token), and the rest over k entries. - sparse_
ok - Whether
configcan go through the sparse chain: a real temperature and a top-k in 1..=256. Qwen’s recommended instruct settings (0.7 / top-p 0.8 / top-k 20 / presence 1.5) do. - spec_
accept_ or_ correct - One step of speculative sampling (Leviathan et al. / Chen et al.):
the draft
dwas drawn fromq; the target distribution at the same position isp. ReturnsNonewhendis accepted (with probability min(1, p[d]/q[d])) andSome(c)when it is rejected,cdrawn from the residual max(0, p − q) renormalized — which is exactly what makes the emitted token stream distributed asp, draft or no draft. When the residual is empty (p ⊆ q, so p == q on the support) the correction falls back to a draw frompitself.scratchholds the residual; the pool spreads the vocab-wide pass. - spec_
accept_ or_ correct_ sparse spec_accept_or_correctover sparse distributions: accept the draftdwith min(1, p[d]/q[d]); on rejection draw the correction from the residual max(0, p − q) over p’s support (q’s support outside p contributes nothing to the residual). Empty residual → a draw from p.- top1_
prob_ pool - Top-1 probability of
idunder a softmax at temperaturetemp— the per-token confidence — with the exp pass over the pool and the sum sequential in index order (bit-identical to the serial fold). Uses the scratch’s partition buffer, idle now that top-k streams.
Type Aliases§
- Sparse
- A distribution over at most
SPARSE_TOPK_MAXtokens:(id, prob)sorted by id, probs summing to 1.