mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #19] [MERGED] serve: per-request temperature/top_p/top_k/min_p/penalties sampling #2421
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#2421
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/19
Author: @j-luwierski
Created: 9/27/2026
Status: ✅ Merged
Merged: 9/27/2026
Merged by: @Niko1221
Base:
main← Head:temperature-pr📝 Commits (10+)
111c58aserve: per-request temperature sampling for the verify window's head8586f94verify: a persistent per-request draw counter; sampled requests default top_k=20a02ee82docs: temperature sampling + reproducibility note (static residency)4d39f7aperf: sample the verify window's head with a block per token012992fserve: forward temperature/top_p/top_k/seed to the engine4fe1c9edocs: the v1 limits line still said temperature was ignored968512cserve: sampling defaults from the run config2072ac0generate: min_p and the penalties ride the GEN linee1d9e99serve: the sampling block supports min_p and the penalties3e0836fsampler: the penalty count as a shared bitmap📊 Changes
8 files changed (+626 additions, -144 deletions)
View changed files
📝
docs/DETAILS.md(+9 -2)📝
include/strata/core/verify.hpp(+26 -0)📝
include/strata/kernels/sampler.hpp(+5 -6)📝
serve/server.py(+112 -5)📝
src/core/verify.cpp(+17 -4)📝
src/kernels/cuda/sampler.cu(+142 -127)📝
src/kernels/sampler_parity.cpp(+244 -0)📝
src/program/generate.cpp(+71 -0)📄 Description
Problem
Temperature (and the rest of the sampling block) was ignored end to end:
verify.cpphardcodedsp.greedy = true) - and a captured sampler would replay the same draws forever;StrataEngine.generateaccepted the request's sampling parameters and dropped them, so every served request decoded greedy whatever the client asked - the README's "temperature / top_p / top_k / seed are honored per request" was not true over the HTTP API.What changed
Per-request temperature / top_p / top_k / min_p / seed, plus the llama.cpp penalty set. The sampled chain's options are live end to end: an OpenAI-style request (
temperature,top_p,top_k,min_p,presence_penalty,frequency_penalty,repetition_penalty,penalty_last_n,seed) or the config'ssamplingblock reaches the engine's per-request sampler. Absent / neutral keys cost nothing and stay greedy, exactly as before.The sampling moves out of the captured graph. The graph computes the head logits;
run()appliessample_tokenshost-side after the replay, with that request's own parameters and a fresh Philox counter per window. The GEN protocol accepts optionalkey=valuepairs before the ids (temperature=,top_p=,top_k=,min_p=,penalty_repeat=,penalty_freq=,penalty_present=,penalty_last_n=,seed=); absent keys stay greedy. Emitted tokens are always the TARGET's own samples (the drafts only fill the verified window), so the output distribution is the target's regardless of what the drafts propose.A persistent per-request draw counter; sampled requests default top_k=20. The sampled path requires top_k in 1..64 (the selection loop is O(k·n_vocab) by design); with the engine-default top_k=0 it returned without writing the token, leaking stale buffer contents as output - sampled requests now default to the sampler's own top_k=20. The Philox draw counter also persists across a request's verify windows (it was run-local before: every window redrew the same numbers, so identical requests returned identical sampled output).
min_p and the penalties.
min_pcuts the top_k survivors to the prefix with p >= min_p · p_max - in logits,sel_logit >= sel_logit[0] + logf(min_p)- before top_p, so the two cuts compose; 0 disables, and the head always survives, so the kept count never reaches zero. The penalties (repeat/freq/present) count over a history window: the serve loop uploads the request's lastpenalty_last_ntokens - the consumed tail plus the fed-back head - into one device row before every window; a request without penalties hands the sampler a null row and takes the byte-for-byte neutral path. The counts come from a shared per-token bitmap: each block builds it from the history row in oneatomicOrpass, and a candidate pays the O(hlen) count scan only when its bit is set - the naive per-candidate recount cost O(k·n_vocab·hlen) integer compares per token (~318M at top_k=20 on the 248,320 vocabulary) and dragged sampled decode 45.2 → 30.8 tok/s on a real chat workload. The repeat penalty multiplies for non-positive logits and divides for positive ones (llama.cpp's chain; dividing unconditionally inverts the penalty on half the vocabulary), and the presence penalty is a boolean, not the count.The sampled chain is one block per token. It ran in ONE thread per token: top_k alone was k sequential full-vocabulary scans and a verify window measured ~1.6 s in the sampler - sampled requests decoded at 1.5 tok/s against 54 greedy. The selection is k argmax rounds, so it parallelises exactly like the greedy kernel: one block per token, the rounds back to back inside it, each a two-level (warp, then block) reduction. Ties resolve to the lowest index (the serial scan's strict
>), so the kept sequence - set and order - is unchanged; the penalty, top_p cut, temperature and Philox draw read that order as before.server.py forwards the block to the engine.
sampling_keysforwards each field only when it is not neutral, so a client that leaves them out costs nothing; an absent temperature keeps the engine's greedy default and temperature=0 is not forwarded (it means the same thing); top_k outside the sampled path's 1..64 is dropped rather than sending a value the engine would refuse; a penalty without a window sends the engine's defaultpenalty_last_n=64. The run config's optionalsamplingblock sets the defaults for fields a request leaves out - the request's own fields always win, a null field falls back to the default, and a value the engine cannot honour refuses to start the server (a typo'd config should not quietly change sampling).Docs. DETAILS.md: the sampling + reproducibility note (static residency), the config defaults, and the v1 limits line no longer claims temperature is ignored.
Verification
sampler_paritypasses, grown to a full-chain host reference (the Philox draw transcribed host-side) plus fixtures for the sampled chain with penalties, for min_p at 0/0.5/0.9, and for the penalty-window clamp - each fixture first asserts it can SEE the feature before asserting the kernel matches; a 248,320-vocab GPU-vs-CPU draw check matches the serial reference on five parameter sets including injected duplicate logits🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.