Skip to main content

Module sampling

Module sampling 

Source
Expand description

Wasm-safe CPU sampling: penalties, logit bias, truncation (top-k / top-p / min-p), and per-token logprobs — pure functions over a logits slice with no GPU and no server-feature dependency, so the browser/wasm build and the native server share exactly this code and every rule is unit-tested on synthetic logits.

Application order (fixed, mirrors vLLM): penalties → logit bias → temperature → truncation (top-k → top-p → min-p) → draw. Penalties themselves apply repetition (multiplicative, over prompt ∪ output) before frequency and presence (subtractive, over output), matching HF/vLLM.

Logprobs, when requested, are the log_softmax of the RAW read-back column — the model’s own probabilities — independent of the penalty/truncation used to pick the token, which is what the OpenAI logprobs field reports.

Determinism: the draw uses the same xorshift64 stream keyed only on the request seed as the original greedy-or-nucleus sampler, and at default knobs (top_k = None, min_p = None) the truncation + draw is bit-identical to it — so the seeded-replay serving contract is preserved.

Structs§

TokenLogprobs
The chosen token’s logprob plus the top_n highest-logprob alternatives, from log_softmax of the RAW logits column.

Functions§

apply_logit_bias
Add per-token logit bias in place, clamped to ±100 (OpenAI’s range; −100 effectively bans a token, +100 all but forces it).
apply_penalties
Apply presence / frequency / repetition penalties in place.
argmax
The lowest-id argmax of logits (greedy pick after penalties/bias, when temperature == 0).
log_softmax_at
log_softmax(logits) evaluated at chosen and at the top_n highest-logprob tokens.
prompt_lookup_drafts
Prompt-lookup draft source: propose the k tokens that followed the longest matching suffix n-gram (n ≤ 3) of all earlier in all — pure token-id matching, no linguistic data. The most recent earlier occurrence wins (locality: generations repeat their own recent structure), and a short found continuation is padded by cycling. Returns empty when no n-gram recurs. Wrong drafts only cost speed under greedy exact-match verify, never correctness. Shared by serving (PLD replace-form) and the app-tier constrained drafter.
sample
Temperature + truncation (top-k → top-p → min-p) nucleus draw with the deterministic per-request RNG. At top_k = None, min_p = None this reduces exactly to plain nucleus sampling.
xorshift64
Per-request xorshift64 RNG advance. The stream is a pure function of the seed, so a sampled request replays identically regardless of what traffic it shared batches with.