Skip to content

server: kept_probs, each token's post-filter probabilities beside its logprobs - #3207

Merged
ashhart merged 4 commits into
mainfrom
lead/kept-probs
Nov 28, 2026
Merged

ashhart merged 4 commits into
mainfrom
lead/kept-probs

Conversation

@ashhart

@ashhart ashhart commented Nov 13, 2026

Copy link
Copy Markdown
Owner

What this changes

Adds an opt-in kept_probs: true to /v1/chat/completions (with logprobs: true) and /v1/completions (with a logprobs count). Each reply token also carries the probabilities the sampler drew it from: the tokens left after temperature, top_k, top_p and min_p, in the row's order, renormalized to sum to 1. Chat adds a kept_probs list of {token, prob} to each logprobs.content entry; completions add a kept_probs list beside top_logprobs, one list per token. A greedy request reports its one token at probability 1.

The server computes the set on the host from the logprob row it already receives, with the sampler's own cut: the top_p and min_p step moves unchanged out of chooseOrdered into sampling.kept, which both the draw and kept_probs call. With the option on, the engine is asked for all 20 alternatives; top_logprobs still prints the count the client asked for.

A sampled request needs top_k from 1 to 20, so the kept set always fits inside the 20-alternative row. Any other top_k (including none), a nonzero presence_penalty or frequency_penalty, or kept_probs without logprobs returns HTTP 400 with the reason. The set comes from the rows' 32-bit values, so a token within about 1e-6 of a cut can land on the other side of it; docs/api.md says so.

Why replies are unchanged

The option changes only what the server prints. The engine sees the same request apart from a larger logprobs count, which only sizes the logprob row; the sampler reads the same logits with the same seed, so tokens are untouched. With the option off, the request and every printed field are as before.

Tests (host, no GPU)

  • kept_probs.zig: the kept set by hand (top_k, top_p's cumulative cut, min_p's floor, temperature); a top_k the row can't hold is an error, never a truncated set; 20,000 host draws per configuration land in the set at their reported frequencies; from rows built off full-vocabulary logits, every draw of the host sampler and of both GPU sampler references lands in the set, a top_k cut between tied logits keeps the lower id, and the full-row GPU rule keeps exactly as many tokens; refusals for top_k 0, 21 and 50 and for penalties.
  • logprobs_test.zig: chat and completions, whole and streamed, option on and off: with ids, clocks and the runtime block fixed and kept_probs removed, every body and stream event is byte-identical; the streamed kept sets joined over the chunks equal the whole reply's; greedy keeps one token at 1; a completion with no tokens carries an empty list; the HTTP 400 cases.
  • The two new HTTP tests fail on main, and a variant that skips the sampler's cut fails four tests.
  • zig build test-server-cpu, test-logprob-offers-host, test-shared-lane-regressions-host (with the sampler identity tests), test-request-seeds-host and test-lane-api-host pass; zig fmt --check is clean; the lean checker's count equals main's.

Still owed

A served check: the same sampled chat with and without kept_probs (whole, streamed, and on /v1/completions) must give equal token_sha256 and top_logprobs; each token must be in its own kept set with probabilities summing to 1; decode speed with the option on and off should be within noise.

Fixes #3182

  • Synthena (Ash's Agent)

…m its logprob row

An opt-in `kept_probs: true` on chat (with logprobs) and completions (with a
logprobs count) adds, for each reply token, the probabilities over the tokens
the sampler keeps after temperature, top_k, top_p and min_p, renormalized. The
server computes them on the host with the sampler's own cut (`sampling.kept`,
moved out of `chooseOrdered` unchanged) and asks the engine for all 20
alternatives, while `top_logprobs` still prints the count asked for. A sampled
request needs top_k from 1 to 20 so the kept set fits the row; other top_k
values and nonzero penalties are refused with HTTP 400. Tokens and every other
field are unchanged with the option on or off.

Fixes #3182
@ashhart
ashhart merged commit 738281b into main Nov 28, 2026
2 of 12 checks passed
@ashhart
ashhart deleted the lead/kept-probs branch November 28, 2026 05:30
ashhart added a commit to joshuaswarren/TensorFold that referenced this pull request Nov 28, 2026
ashhart added a commit that referenced this pull request Nov 28, 2026
ashhart added a commit to Bizuayeu/TensorFold that referenced this pull request Nov 28, 2026
ashhart added a commit that referenced this pull request Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Post-filter probabilities: report the distribution a sampled token was drawn from

1 participant