Skip to content

Report the model's log probabilities before the logit processors on request - #864

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:alessio/log-probability-source
Dec 3, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:alessio/log-probability-source

Conversation

@aleroot

@aleroot aleroot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

Token log probabilities normalize the logits after the logit processors and before temperature, as Python MLX-LM does (logprobs = logits - mx.logsumexp(logits) after logits_processors in generate.py). A repetition or presence penalty is a logit processor, so it lowers the reported probability of every token the reply already used. The values then describe the penalty as much as the model.

Digits show it clearly: Qwen tokenizes numbers one digit at a time, and the recommended non-thinking sampling for Qwen 3.5–3.8 uses presence_penalty: 1.5. Asked for the population of Nauru, Qwen3.8-27B is 83% sure that "10,084" starts with "1". "1" is already in "2011" earlier in the reply, so the reported value is 52%, and the step looks like a near coin flip between "1" and "9". The generated text is correct and the same in both cases. Only the report changes.

This adds an opt-in, GenerateParameters.logProbabilitySource:

public enum LogProbabilitySource: Sendable, Hashable {
    case processed  // after the logit processors (default, unchanged)
    case model      // before any logit processor
}

The transformer takes the same choice:

session.observe(to: prompt, using: .logProbabilities(topCount: 5, source: .model))
  • .processed stays the default, so current values and parity with Python MLX-LM do not change.
  • The sampled tokens do not change: the processors still shape the distribution the sampler draws from.
  • TokenIterator normalizes the model's logits a second time only when a processor is active. Without one, both sources are the same array.
  • SpeculativeTokenIterator and MTPSpeculativeTokenIterator already normalize the reported rows in their own batch, so .model only changes which rows they read. Every emission path reads the chosen source: prefill, verify, speculative sampling and passthrough.
  • The internal recording(_:sampledFrom:topK:) label becomes from:, because with .model the rows are no longer the ones the token was sampled from.

vLLM makes the same distinction with logprobs_mode (raw_logprobs, its default, against processed_logprobs).

Measured results

The same change applied to TokenIterator at a390d50 and run from an app on an M5 Max 64 GB. Debug build, Qwen3.8-27B-4bit, greedy decoding, presence penalty 1.5, 6 top candidates.

Decode speed, 512 tokens, 5 measured rounds after 1 warm-up, median tok/s:

Log probabilities Launch 1 Launch 2
off 29.81
.processed 29.13 29.24
.model 29.36 29.36

250 short factual questions (150 from SimpleQA, 100 well known): the replies are identical token for token under both sources. Steps whose entropy is at least ln 2 drop from 4,914 to 4,679. Those are the steps where the penalty spread probability that the model had concentrated.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • I have read this PR description in full and approve it as my own, and it
    accurately describes the code changes.
  • AI usage disclosure:

…equest

Log probabilities normalize the logits after the logit processors, as
Python MLX-LM does. A repetition or presence penalty therefore lowers
the reported probability of every token the reply already used, so the
values stop describing the model's confidence. With a presence penalty
of 1.5 a model that is 83% sure of a digit is reported at 52%.

GenerateParameters.logProbabilitySource selects the logits:
`.processed` keeps the current values and stays the default, `.model`
reports the logits before any processor. The transformer takes the same
choice: `.logProbabilities(topCount: 5, source: .model)`.

The sampled tokens do not change. TokenIterator normalizes the model's
logits a second time only when a processor is active; the speculative
iterators already normalize the reported rows in their own batch, so
`.model` only changes which rows they read.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good, thank you!

@davidkoski
davidkoski merged commit 4ad95e8 into ml-explore:main Dec 3, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants