Repository navigation
Report the model's log probabilities before the logit processors on request - #864
Merged
davidkoski merged 1 commit intoDec 3, 2026
Merged
Conversation
…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
approved these changes
Dec 3, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Change looks good, thank you!
This was referenced Dec 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)afterlogits_processorsingenerate.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:The transformer takes the same choice:
.processedstays the default, so current values and parity with Python MLX-LM do not change.TokenIteratornormalizes the model's logits a second time only when a processor is active. Without one, both sources are the same array.SpeculativeTokenIteratorandMTPSpeculativeTokenIteratoralready normalize the reported rows in their own batch, so.modelonly changes which rows they read. Every emission path reads the chosen source: prefill, verify, speculative sampling and passthrough.recording(_:sampledFrom:topK:)label becomesfrom:, because with.modelthe rows are no longer the ones the token was sampled from.vLLM makes the same distinction with
logprobs_mode(raw_logprobs, its default, againstprocessed_logprobs).Measured results
The same change applied to
TokenIteratorat 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:
.processed.model250 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
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.