Repository navigation
feat(server): report logprobs when the request asks for them - #777
Open
manueldomke wants to merge 2 commits into
Open
manueldomke wants to merge 2 commits into
manueldomke wants to merge 2 commits into
Conversation
Qwen3_5VL::insert() and Qwen3_6_MoE::insert() call _shared_insert(), which
prefills and draws one token into `last_token`. Their generate() then
force-feeds the think block and samples again; the drawn token is never used,
and the only reference to it is an assert.
sample() records every drawn token in the repetition and frequency windows
first. So a token that was thrown away counts as generated text. For a model
that answers without a thinking block, the token predicted after `assistant`
IS the answer letter -- it is pushed down by count * freq_penalty +
pre_penalty at exactly the position where it is needed, which for a single
occurrence is 1.0 + 1.5 = 2.5 logits (Qwen3.5-VL's defaults; its rep_penalty
is 1.0 and does nothing).
Measured on Qwen3.5-2B, temperature 0, default penalties, before and after,
nothing else changed:
prompt Q before B after A
prompt R before B after A
A is the argmax of the unpenalised distribution (0.339 against 0.299 for B),
so the answer is no longer the penalised one.
Sampler::sample() and sample_greedy() take a record_in_history flag,
defaulting to true, so no other model changes behaviour. sample() forwards it
to sample_greedy() on the top_k == 1 path, and _shared_insert() passes it
through -- both paths have to honour it, or the same prompt answers
differently depending on a sampling knob:
top_k=1 B (greedy path)
top_k=40 A (sampled path) before both flags were in place
Co-Authored-By: DeepSeek V4 Flash (Default Thinking)
/v1/chat/completions and /v1/completions wrote a hard-coded
`{"logprobs", nullptr}` into their responses. This fills the field from the
sampler, in OpenAI's shape, and only when the request asks for it.
For a model that answers with a single token -- a classifier, a router, a
decision model -- the letter alone says nothing about how sure the model was.
sampler.hpp/.cpp capture_if_requested(), log_sum_exp_all() and a table of
exp() for every bf16 value; the sampling paths capture
once per draw while a request has the sampler armed
automodel.hpp/.cpp top_logprob_t, logprobs_entry_t,
_request_logprobs_capture() and _record_top_logprobs()
modeling_qwen3_5vl records where a token reaches the caller's text, not
where it was drawn
rest_handler.cpp reads the request, builds both response shapes
One entry per token of message.content, in the same order, so a client that
pairs entries with tokens stays aligned. Recording happens where the token is
written to the response, which is what keeps the two in step: a token the
engine filters out gets no entry, and neither does EOS. A 17-token answer
comes back with 16 entries. An answer that carries a tool call gets null,
because message.content is empty and there are no tokens to describe.
logprob is a real log-probability -- logit minus logsumexp over the WHOLE
vocabulary, after the penalties and before the temperature. It is NOT
renormalised over the entries listed, so those do not sum to 1; the rest of
the vocabulary lies outside the list.
The logsumexp is exact and cheap: the logits arrive as bf16 from the engine,
so there are only 65536 possible values, and a table of exp() for each of them
(512 KB, fits in L2) replaces a quarter of a million exp() calls with as many
reads. Values the penalties moved off the bf16 grid take the exact call. The
result differs from the shifted computation by about one float ulp, which is
rounding, not approximation. For the alternatives, N <= top_k needs no sort at
all: top_k_logits is already sorted by logit with the same tie-break, so its
beginning is the top-n.
Cost, wall clock on Qwen3.5-2B, 300 tokens, nothing else running,
platform_profile=balanced. The runs drift by about 2 % on their own, so this
is an upper bound rather than a figure:
without logprobs 10.55 10.46 10.35 s
with logprobs 10.57 s at most about 1 %
The sampler is disarmed at the start of every request, before anything that
can return early; it is armed only in Qwen3.5-VL's generate(), and only when
the request asks for logprobs.
The two request shapes differ, as they do upstream: chat takes `logprobs` as a
boolean plus `top_logprobs` (0-20), completions takes `logprobs` as an integer
0-5 and has no `top_logprobs`. For completions 0 is a valid request: it asks
for the drawn token's own logprob with no alternatives.
Limitations, unchanged by this patch:
- only Qwen3.5-VL reports; every other model still returns null
- streaming responses carry no logprobs
- with think:true the answer gets null: the tokens of the reasoning block
land in reasoning_content, not in content
Seen while working on this, not touched here: the AVX2 argmax in
sample_greedy() breaks ties within a lane by the lowest index but merges lanes
by the lowest lane, so an exact tie at the top can pick a higher id than the
scalar path would.
Co-Authored-By: DeepSeek V4 Flash (Default Thinking)
This branch has not been deployed
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.
Motivation
/v1/chat/completionsand/v1/completionswrite a hard-coded{"logprobs", nullptr}into their responses. The sampler already keeps thetop-k of every sampled token -- token id, logit and probability -- but nothing
carries them to the response.
A model that answers with a single token -- a classifier, a router, a decision
model -- is unreadable without that distribution. The caller gets the chosen
option and cannot tell whether the model was certain or guessing. Three
Qwen3.5 fine-tunes converted for the NPU hit exactly this, and each of them
was built and measured against this patch:
Their model cards carry the flm commit they were tested with and the
configuration that produced the numbers below.
Technical Details
Builds on #776. Until that one is merged this PR shows both commits; it exists
because without the fix the reported distribution would include the penalty of
a sample the model never used.
One entry per token of
message.content, in the same order, so a clientthat pairs entries with tokens stays aligned. Recording happens where the token
is written to the response, which is what keeps the two in step: a token the
engine filters out gets no entry, and neither does EOS. A 17-token answer comes
back with 16 entries. An answer that carries a tool call gets
null, becausemessage.contentis empty and there are no tokens to describe.logprobis a real log-probability -- logit minus logsumexp over the wholevocabulary, after the penalties and before the temperature. It is not
renormalised over the entries listed, so those do not sum to 1; the rest of the
vocabulary lies outside the list.
The logsumexp is exact and cheap. The logits arrive as bf16 from the
engine, so there are only 65536 possible values, and a table of
exp()foreach of them (512 KB, fits in L2) replaces a quarter of a million
exp()callswith as many reads. Values the penalties moved off the bf16 grid take the exact
call. The result differs from the shifted computation by about one float ulp,
which is rounding, not approximation. For the alternatives,
N <= top_kneedsno sort at all:
top_k_logitsis already sorted by logit with the sametie-break, so its beginning is the top-n.
The two request shapes differ, as they do upstream: chat takes
logprobsas a boolean plus
top_logprobs(0-20), completions takeslogprobsas aninteger 0-5 and has no
top_logprobs. For completions 0 is a valid request: itasks for the drawn token's own logprob with no alternatives.
A request that does not ask pays nothing. The sampler is disarmed at the
start of every request, before anything that can return early; it is armed only
in Qwen3.5-VL's
generate(), and only when the request asks for logprobs.Limitations, unchanged by this patch:
nullthink: truethe answer getsnull: the tokens of the reasoning blockland in
reasoning_content, not incontentTest Plan
count; a tool-call answer;
think: truetop_k=2,temperature=1, 50 runs -- the drawn token mustappear in its own entry every time
from its
bytesand comparinglogprobs, text comparedcharacter by character; then a run of without -> with -> without, to show the
sampler is disarmed again
Test Result
The cost is inside the run-to-run spread -- the three runs without logprobs
differ by 0.05 s on their own, and the run with logprobs was faster than the
first. The honest figure is: at most about 1 %, and only for requests that ask.
Submission Checklist
🤖 Generated with Claude Code