Skip to main content

Module sampling

Module sampling 

Source
Expand description

Sampling controls for decode loops (ROADMAP row 133, slice 1: the pure host sampler).

sample_with_params picks the next token from one logits row under a SamplingParams and the tokens seen so far. It is host-side and deterministic: the same logits, params, history and RNG state give the same token. With only a positive temperature set it reduces exactly to crate::lazy::sample_logits (same RNG stream, same probabilities), which a test pins.

Order of operations, so a caller can predict the result:

  1. penalties on the raw logits, from history: repetition (HF form: a seen token’s positive logit is divided by the penalty, a negative one multiplied), then logit -= frequency * count + presence * (count > 0);
  2. temperature <= 0 or NaN: the argmax of the penalised logits (greedy), lowest index on a tie;
  3. otherwise scale by 1 / temperature, softmax, then top_k, then top_p (nucleus: the smallest prefix of descending probabilities reaching top_p, at least one token), then min_p (drop tokens below min_p times the largest remaining probability), renormalise, and draw with crate::lazy::sample_multinomial.

Logprobs are of the model’s own distribution: log_softmax of the INPUT logits, before penalties, temperature and filters (vLLM’s “raw logprobs”). They describe what the model said, not what was sampled from, and they do not depend on the sampling controls.

history is whatever the caller wants penalised. OpenAI penalises generated tokens only; HF’s repetition penalty covers the prompt too. The scheduler slice decides which it passes.

Structs§

Sampled
One sampled step.
SamplingParams
Every control for one sampling step. Default is greedy with no penalties and no logprobs.

Functions§

sample_with_params
Pick the next token. Errors on empty or non-finite logits, a history token outside the vocabulary, or invalid params; never panics.