mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[GH-ISSUE #2028] [Bug]: STRATA_SPEC_PROB: --spec-min-p gates on the drawn draft's q, which skews sampled tokens toward the draft head's favourite #806
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#806
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?
Originally created by @KarlGrier on GitHub (Oct 12, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/2028
What happened
With
STRATA_SPEC_PROB=1and--spec-min-p 0.5, a near-tie inside a speculative window comes out skewed toward one token. With--spec-min-p 0, or withSTRATA_SPEC_PROB_GATE=top, the same position matches speculation off again, so the gate causes the skew, not the acceptance rule.Stock v0.1.43 (
7695859a), the arithmetic case below, temperature 0.6, top_k 20, top_p 0.95, min_p 0.05, no penalties, no seed sent (so the server's clock seed). The counts are of=among the samples that took the 3-token path to the position:=--spec-min-p 2: no draft passes)STRATA_SPEC_PROB=1,--spec-min-p 0.5STRATA_SPEC_PROB=1,--spec-min-p 0STRATA_SPEC_PROB=1,--spec-min-p 0.5,STRATA_SPEC_PROB_GATE=top--spec-min-p 0.5--spec-min-p 0.5(not in Strata)The last row is our own build (v0.1.43 plus our patches, including an exact-sampling path of ours), shown for comparison only. All three
STRATA_SPEC_PROBruns drafted in every request (3.9 drafts per request with the gate on the drawn token, 4.2 with the other two).Strata's own probabilities at the position, forced through the
STRATA_SPEC_PROBserver, are=0.5018,0.4902,=0.0076. After top_k 20, top_p 0.95, min_p 0.05 and temperature 0.6 only=andare left, at 51.0 % / 49.0 %. Against that, the gated run gives χ² 28.3 (p < 1e-6);GATE=topgives 0.16 (p 0.69). The speculation-off server forces slightly different values at the same position (=0.5219,0.4707, so 54.3 % after the chain), so I compare the runs with each other rather than with one fixed number.Why (where the two samplers differ)
In probabilistic mode the drafter writes the drawn token's own probability under q to
*out_prob:src/kernels/cuda/sampler.culine 980,spec_draft_merge_kernel:prob[0] = n_keep > 0 ? (float) ex[gate_pick ? pi : 0] : 1.0f;. Heregate_pick = spec_gate_pick(), which is on by default (include/strata/core/spec_prob.hppline 45,STRATA_SPEC_PROB_GATEunset).The serve loop then adds draft j to the window only while that value passes
--spec-min-p:src/program/generate.cppline 11815 (the serve loop):while (T < S_mtp && dprob[(size_t) T - 1] >= (float) req_spec_min_p) ++T;. The CLI loop does the same at line 13012 and also hands q to the verifier.So whether a row is judged by
min(1, p(d)/q(d))depends on which token the drafter drew. A draw withq(d) < --spec-min-pis dropped, and that row becomes a plain sample of p. Rejection sampling gives p only averaged over every draw from q. With the draws selected by their own q, a row's output isP(x) = Σ_d q(d)·[q(d) ≥ θ]·P(x | d judged) + Σ_d q(d)·[q(d) < θ]·p(x)For two tokens A and B with
q(A) ≥ p(A)andq(A) ≥ θ > q(B), this isP(A) = p(A)·(2 − q(A)). For example, with p = (0.51, 0.49), a draft head at q(A) = 0.88 would give 57 % instead of 51 %. The skew always goes toward the draft head's favourite token. It disappears when no draw passes the gate, so it varies with temperature: on v0.1.42, at this position (our build, runningSTRATA_SPEC_PROB), we saw +8 to +10 points at temperature 0.6 and no detectable skew at 1.0. A Monte Carlo of the kernel's rule (accept ifu·q(d) < p(d), otherwise sample the residualmax(0, p − q)) gives the same numbers, and p itself for every q with θ = 0.The comment at
spec_gate_picksays it gates "as the coupled drafter does". For the coupled drafter and plain exact-match drafting the gate is harmless: the row's output is the target's own draw whether or not a draft was proposed. Rejection sampling is different. Before this I also checked the Philox draws behind the pick, the draft, the accept and the residual (the bit-61/62/63 counters). In a port ofphilox_uniform, 400K random seeds and positions showed no correlation (|r| ≤ 0.003).Suggested fix: in
STRATA_SPEC_PROBmode, gate on something that does not depend on the draw, such as q's top probability, which is whatSTRATA_SPEC_PROB_GATE=topdoes. The gate on each draft then depends only on the context before it, so each row still samples exactly from p. The comment there says top gating decoded slower on an RTX 3060; I have not measured its speed here. Another option is to fix the window length before drawing.How to rebuild the case
The chat request sends
reasoning_effort: "high". The model's chat template maps "high" to its default "xhigh", so the server renders this prompt (76 ids):That is followed by the first 32 tokens of the greedy answer,
We need answer simple arithmetic. Need final brief steps. Compute 17*23=391. 144/12=1:Each request generates 4 tokens from there (
max_tokens4). I count the samples whose first three tokens are2. Sum(17, 13, 7860), then whether the fourth is=(28) or(220). The position is the 4th generated token, so it falls inside a speculative window (--spec 4), not in a request's first one-token window. Stock's server has no field for appending token ids to a chat prompt, so I appended the 32 ids with a small server hook that is only for measurement and changes nothing else.Flags (served, two GPUs; the measured build is stock v0.1.43 plus that hook):
Strata version, GPU, OS
v0.1.43 (
7695859a) · RTX 5080 16 GB + RTX 4060 Ti 8 GB, driver 615.71.09, CUDA 13.3 · Linux 7.0 (Ubuntu 24.04-based)Engine log
Measured and drafted with Claude Code.
@Niko1221 commented on GitHub (Oct 12, 2026):
Thank you, this is a careful report and the maths holds. In
STRATA_SPEC_PROBmode the window length is decided from the drawn token's own q (prob[0] = ex[pi]inspec_draft_merge_kernel, thendprob[T-1] >= spec_min_pin the serve loop at generate.cpp ~11871, and the same at ~13068 in the CLI loop). Whether draft j is judged at all therefore depends on d_j, and the rows that skip the judge are plain samples of p. Rejection sampling gives p only when the sum runs over every draw of q, so selecting draws by their own q gives P(A) = p(A)(2 - q(A)) in your two-token example, always toward the draft head's favourite, and it fades as the temperature rises because fewer draws fall under the gate. Your table agrees:--spec-min-p 0andSTRATA_SPEC_PROB_GATE=topboth match speculation off (z 0.2 and -1.1) while the default gate is +4.1 points (z 3.0). Default exact-match drafting (noSTRATA_SPEC_PROB) is not affected: there the output is the target's own draw whether or not a draft was proposed.Scope: only
STRATA_SPEC_PROB=1with--spec-min-pabove 0, which is opt-in, so default answers are unchanged. Proposed fix, not applied yet because it changes sampled answers in that mode and the comment atspec_gate_pickrecords thattopdecoded slower on the 3060: (1) withSTRATA_SPEC_PROB=1, default the gate totop, which depends only on the context before the draft and keeps every row exact; keep the drawn-token gate as an explicitSTRATA_SPEC_PROB_GATE=pick, documented as approximate. (2) Before changing the default, an interleaved A/B (>= 5 pairs, medians, pairs-faster) of pick vs top on the 3060, 5070 and a 16 GB card at temperature 0.6 and 1.0, plus a chi-square check at your position (topmust stay within noise of speculation off), and a note in the docs and the startup line that prints 'probabilistic draft acceptance on'. (3) Optionally decide the window length before drawing (a fixed T per round) as the alternative iftopis too slow. We will report the speed numbers here before any default moves. Could you share the small server hook for appending token ids to a chat prompt, so we can reproduce your position with the same harness?@KarlGrier commented on GitHub (Oct 13, 2026):
Thanks for confirming it, and for the plan. Here is the hook. It is for measurement only: it adds two request fields to the OpenAI chat endpoint (
/v1/chat/completions) and does nothing when they are absent.strata_prefix_ids: a list of token ids appended after the rendered chat prompt (here, after the template's<think>\n), so every sample starts from the same point inside the answer.strata_return_ids: true: the response carries the generated token ids asstrata_ids, so the counting is done on ids, not on detokenized text.The diff against v0.1.43 (
serve/server.pyonly, 9 lines added)On v0.1.43.1 the first three hunks apply with an offset. The fourth doesn't, because #1999 moved
cancelahead ofsvc.prepare(...); the three lines go right after the OpenAI handler'ssvc.prepare(...)line there as well.The request, one per sample, exactly as we sent it (
POST /v1/chat/completions):min_p0.05 wasn't in the body: it came from the server config's default sampling ("sampling": {"temperature": 0.6, "top_p": 0.95, "top_k": 20, "min_p": 0.05}). With a config that doesn't set it, add"min_p": 0.05to the body. No seed was sent, so each request used the server's clock seed.The counting rule:
strata_ids(4 ids) from each response.[17, 13, 7860](2. Sum). The others took a different path and are left out.28(=) or220(). The share in the issue's table is the=count divided by the kept samples.The kept samples were 3,830 of 4,000 requests with speculation off and 1,914 to 1,934 of 2,000 in the other runs. Every kept sample ended in
=or.The probabilities in the issue (
=0.5018 and so on) came from a second, larger measurement hook that teacher-forces tokens through the verify window (it also touchesgenerate.cpp). Say if you'd like that one too.Measured and drafted with Claude Code.
@Niko1221 commented on GitHub (Oct 29, 2026):
Thank you. Fixed in 0.2.7: with
STRATA_SPEC_PROB=1the confidence gate (--spec-min-p) now stays inside the proposal law, so it no longer skews sampled tokens toward the draft head's favourite. The change is by KeyanHu-git (#3416).Release: https://github.com/Niko1221/Strata/releases/tag/v0.2.7