spec: STRATA_SPEC_PROB=1 keeps the confidence gate in the proposal law, opt-in (#3416)

Problem (KeyanHu-git, refs #2028, #2591): with STRATA_SPEC_PROB=1 the default
--spec-min-p gate tests the token drawn from the draft head's distribution q, but the
verifier still judged the draft against the unfiltered q and treated a failed gate as a
plain sample from p. That shifts the output distribution: with p = (0.5, 0.5),
q = (0.875, 0.125) and threshold 0.5 it emits (0.5625, 0.4375).

Change: the gate is part of the proposal. Keep q's mass on the tokens that pass the gate
without renormalising (mu); a draw that fails the gate becomes NULL. An offered token D
is accepted with min(1, p(D)/mu(D)); a rejection and NULL draw from the normalised
positive part of p - mu; a position with no attempted proposal is a plain sample from p.
Serve and the CLI build the same packet (core/gated_proposal.hpp) and give it to the
verifier explicitly (Verifier::set_spec_proposal, sample_tokens_proposal). The explicit
call has no plain-p fallback: an invalid packet row is reported, not sampled around.
STRATA_SPEC_PROB_GATE=top keeps its normalised-q rule.

Default path (STRATA_SPEC_PROB unset): unchanged. mtp.prob() is false, so gated_proposal
is false, the new draft-result argument is null, GatedDraftLimit does nothing,
Verifier::run takes the old branch (set_spec_q clears the measure flag), and the
sampler kernel's old instance is the old code with the new branches compiled out.
Read hunk by hunk; no hunk runs a new branch or a new allocation per token. The
default-identity model run is written as a gate script and not run yet.

Port notes against the PR as submitted:
- Unsupported combinations (parallel or multi-device decode, --lookup-chain,
  --adapt-swaps, PCIe share, --spec-min-p not finite, STRATA_SPEC_COST, DFlash2,
  STRATA_SPEC_T_CYCLE, --spec-corrupt, Metal) used to be an error that ended the
  request or the server. They now warn once and keep the older rule, so setting the
  variable never stops a run. --adapt-swaps 96 and the PCIe share are on by default, so
  the exact rule needs --lookup-chain 0 --adapt-swaps 0 --pcie-frac 0.
- The lookup source of a probabilistic window is chosen by a copy of the policy fixed at
  the request start, without asking whether the MTP's first guess agrees. Learning from
  committed rounds inside a request is the policy commit of this series.
- The CLI loop on the current tree has the out-of-memory window retry, DFlash2 and
  STRATA_SPEC_T_CYCLE; the gated path does not use the retry (a narrower window would
  change the room the packet was built for). Serve keeps merged first windows
  (STRATA_ADMIT_MERGE_FIRST) at their length.
- docs/DETAILS.md: two short bullets instead of the PR's long paragraph (that paragraph
  no longer exists on this tree). bench/results and the PNGs are not ported.

Tests: gated_proposal_test and spec_admission_test (default, pick, top, exact-top-only)
pass on z440 (CPU, g++ 13) and on the owner PC (MSVC). On the owner PC's RTX 5070
(sm_120, MSVC build, synthetic data, no model) spec_proposal_parity, spec_verify_parity and
sampler_parity (default, one-block and old-kernel configurations) pass. Also built:
CUDA sm_86 on z440 and boston, SYCL on P1006 (engine and host tests). The Metal path is
not built here.
Not covered: a model-level distribution test (chi-square of the first tokens against
plain sampling) and the identity run with the variable unset. Both are written as gate
scripts (3416-spec-prob-dist.sh, 3416-default-identity.sh) and not run.
This commit is contained in:
Keyan Hu authored and Niko1221 committed 2026-10-27 01:45:35 +01:00
1 parent a8c1d4b7f8
commit f4612b1add
18 files changed
+963 -57

No files matched your search

+12 -6
View File
@@ -599,8 +599,10 @@ bool MtpDrafter::setup_coupled(std::string& err) {
coupled_ok_ = true;
if (spec_prob_env())
std::fprintf(stderr, "strata mtp: probabilistic draft acceptance on (STRATA_SPEC_PROB): sampled requests draft by "
"sampling the draft head's distribution over %lld tokens; the verifier accepts with "
"min(1, p/q) and resamples the residual\n", (long long) nv);
"sampling the draft head's distribution over %lld tokens; %s\n", (long long) nv,
spec_gate_pick()
? "pick gate: masked mu offers, min(1, p/mu) acceptance and (p-mu)+ rejection/NULL residuals"
: "top gate (compatibility): q offers, min(1, p/q) acceptance and (p-q)+ rejection residuals");
else
std::fprintf(stderr, "strata mtp: coupled draft sampling on (STRATA_SPEC_COUPLED): sampled requests draft with the "
"target's chain and Philox draw over %lld tokens\n", (long long) nv);
@@ -1555,7 +1557,8 @@ DraftTiming g_dt;
} // namespace
bool MtpDrafter::draft(int T, const int32_t* tokens, int64_t p, int a, int32_t* drafts, std::string& err,
float* probs, float min_p, int* n_drafts) {
float* probs, float min_p, int* n_drafts, DraftResult* result) {
if (result) { *result = {}; if (n_drafts) *n_drafts = 0; }
#if defined(STRATA_GPU_METAL)
{
if (T < 1 || T > max_t_ || a < 0 || a >= T) { err = "mtp: draft arguments out of range"; return false; }
@@ -1565,6 +1568,7 @@ bool MtpDrafter::draft(int T, const int32_t* tokens, int64_t p, int a, int32_t*
if (!strata::metal_engine::mtp_draft(T, tokens, p, a, max_steps, drafts, probs, min_p, &n, err)) return false;
for (int j = n; j < max_t_ - 1; ++j) { drafts[j] = 0; if (probs) probs[j] = 0.0f; }
if (n_drafts) *n_drafts = n;
if (result) *result = {n, max_steps};
ms_draft += ms_since(t0);
++rounds;
return true;
@@ -1736,6 +1740,7 @@ bool MtpDrafter::draft(int T, const int32_t* tokens, int64_t p, int a, int32_t*
}
for (int j = n; j < max_t_ - 1; ++j) { drafts[j] = 0; if (probs) probs[j] = 0.0f; }
if (n_drafts) *n_drafts = n;
if (result) *result = {n, max_steps};
ms_draft += ms_since(t0);
++rounds;
return true;
@@ -1855,12 +1860,13 @@ int MtpDrafter::chain_poll(std::string& err) {
}
bool MtpDrafter::draft_first(int T, const float* R_row, int32_t token, int64_t cell, int32_t* drafts, std::string& err,
float* probs, float min_p, int* n_drafts) {
float* probs, float min_p, int* n_drafts, DraftResult* result) {
if (result) { *result = {}; if (n_drafts) *n_drafts = 0; }
#if defined(STRATA_GPU_METAL)
{ // the decode window's row 0 is the residual (the window that just ran)
(void) R_row;
std::vector<int32_t> toks((size_t) T, token);
return draft(1, toks.data(), cell, 0, drafts, err, probs, min_p, n_drafts);
return draft(1, toks.data(), cell, 0, drafts, err, probs, min_p, n_drafts, result);
}
#endif
const OnDevice on_device(device_);
@@ -1873,7 +1879,7 @@ bool MtpDrafter::draft_first(int T, const float* R_row, int32_t token, int64_t c
return false;
}
std::vector<int32_t> toks((size_t) T, token);
return draft(T, toks.data(), cell, 0, drafts, err, probs, min_p, n_drafts);
return draft(T, toks.data(), cell, 0, drafts, err, probs, min_p, n_drafts, result);
}
} // namespace strata::core