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§
- Token
Logprobs - The chosen token’s logprob plus the
top_nhighest-logprob alternatives, fromlog_softmaxof 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, whentemperature == 0). - log_
softmax_ at log_softmax(logits)evaluated atchosenand at thetop_nhighest-logprob tokens.- prompt_
lookup_ drafts - Prompt-lookup draft source: propose the
ktokens that followed the longest matching suffix n-gram (n ≤ 3) ofallearlier inall— 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 = Nonethis 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.