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

+176 -31
View File
@@ -68,6 +68,7 @@
#include "strata/core/native_head.hpp"
#include "strata/core/verify.hpp"
#include "strata/core/mtp.hpp"
#include "strata/core/gated_proposal.hpp"
#if defined(STRATA_HAVE_DFLASH2)
#include "strata/core/dflash2.hpp"
#include "strata/core/draft_cap.hpp"
@@ -2340,6 +2341,89 @@ static void prime_hw_queues() {
}
#endif
// STRATA_SPEC_PROB=1 with the default (pick) gate: a draft that fails the confidence gate is still a proposal attempt, and the
// verifier gets the gated measure (core/gated_proposal.hpp). That is exact only where the window choice cannot see a draw or a
// timing that the next rounds draw again. Where it cannot be kept, say why and return false: the caller keeps the older rule
// (a warning, not a refusal).
static bool gated_proposal_usable(bool serial_single_device, int lookup_chain, int adapt_swaps, bool pcie_share, bool own_window_rule,
int window, double min_p) {
const char* why = nullptr;
#if defined(STRATA_GPU_METAL)
why = "the Metal engine has no gated verifier";
#endif
if (why == nullptr && !serial_single_device) why = "it needs serial decode on one device";
if (why == nullptr && (lookup_chain != 0 || adapt_swaps != 0 || pcie_share))
why = "it needs --lookup-chain 0, --adapt-swaps 0 and --pcie-frac 0";
if (why == nullptr && own_window_rule) why = "STRATA_SPEC_COST, STRATA_SPEC_T_CYCLE, DFlash2 and --spec-corrupt change the window by their own rule";
if (why == nullptr && window > strata::core::gated::kRows) why = "the window is wider than 8 rows";
if (why == nullptr && (!std::isfinite(min_p) || min_p < 0.0)) why = "--spec-min-p must be a finite number >= 0";
if (why == nullptr) return true;
static bool warned = false;
if (!warned) {
warned = true;
std::fprintf(stderr, "strata: STRATA_SPEC_PROB=1 keeps the older acceptance rule in this run (%s). That rule's output "
"distribution is off when the confidence gate fails; STRATA_SPEC_PROB_GATE=top is exact here.\n", why);
}
return false;
}
// A draft at MTP cell c is sampled at counter c+1. The next target starts
// there and needs one final cell for its bonus. Cap BEFORE any proposal draw;
// a zero budget therefore means no attempt, never a discarded failed NULL.
class GatedDraftLimit {
public:
GatedDraftLimit(strata::core::MtpDrafter& mtp, bool active, int configured_mtp,
int64_t next_target_pos, int64_t context_end)
: mtp_(mtp), active_(active), configured_(configured_mtp - 1) {
if (active_) {
const int64_t room = std::max<int64_t>(0, context_end - next_target_pos);
mtp_.set_max_drafts((int) std::min<int64_t>(configured_, std::max<int64_t>(0, room - 1)));
}
}
~GatedDraftLimit() { if (active_) mtp_.set_max_drafts(configured_); }
GatedDraftLimit(const GatedDraftLimit&) = delete;
GatedDraftLimit& operator=(const GatedDraftLimit&) = delete;
private:
strata::core::MtpDrafter& mtp_;
bool active_;
int configured_;
};
// Prepare one gated window from the last successful producer result. Context room
// is fixed before consulting D; the producer was already capped before drawing.
// No max_new/output-cap shortening: the ordinary emission loop owns that limit.
static bool prepare_gated_window(strata::core::gated::Packet& packet,
const strata::core::MtpDrafter::DraftResult& generation,
strata::spec::SuffixDrafter& suffix, const strata::spec::DraftPolicy& policy,
bool suffix_enabled, bool initial, int room, int configured_mtp,
int vocab, float threshold, const int32_t* q, const int32_t* drafts,
const float* scores, int32_t* suffix_tokens, int& match, std::string& err) {
if (room < 1 || generation.budget < 0 || generation.generated < 0 ||
generation.generated > generation.budget || generation.budget >= room) {
err = "gated proposal: invalid context room or producer result";
return false;
}
strata::core::gated::RootChoice root;
match = 0;
if (suffix_enabled && !initial && room > 1) {
const int k = suffix.propose(room - 1, suffix_tokens);
match = suffix.last_match();
if (k > 0) {
// Prefix/configuration only: never inspect a draw or its gated width.
const auto pick = policy.choose(std::min(configured_mtp, room), k, match);
root.eligible = pick.lookup;
root.rescue_id = suffix_tokens[0];
root.window = pick.t;
}
}
if (!strata::core::gated::make_packet(packet, q, drafts, scores, generation.generated, generation.budget,
room, vocab, threshold, initial, root)) {
err = std::string("gated proposal: ") + packet.error;
return false;
}
return true;
}
int main(int argc, char** argv) {
// **UNBUFFERED, BECAUSE THE INTERESTING OUTPUT IS THE OUTPUT BEFORE A CRASH.** `stdout` redirected to a
// pipe or a file is block-buffered, so a program that dies loses every line it had already printed - which
@@ -14132,6 +14216,10 @@ int main(int argc, char** argv) {
ver.set_sampling(req_sp);
if (pipe) ver_b.set_sampling(req_sp); // (reaches the later stage's odd verifier)
if (use_mtp) mtp.set_draft_sampling(req_sp); // STRATA_SPEC_COUPLED=1: sampled drafts (a no-op otherwise)
const bool gated_proposal = use_mtp && mtp.prob() && strata::core::spec_gate_pick() &&
gated_proposal_usable(o.pipeline_windows < 2 && o.batch == 0 && n_stages == 1 && !multi_gpu && drive.d.remote_count == 0,
o.lookup_chain, o.adapt_swaps, req_pcie_frac != 0.0 || o.pcie_frac_auto, spec_cost, S,
req_spec_min_p);
{ // aux_cpus=0|1: where the non-pool, non-host threads run for this request (-1 = the start-up --aux-cpus); moves
// threads only. An enabled placement is swept at every request: it catches threads created since.
static const bool aux_start = strata::aux_cpus::enabled();
@@ -14366,6 +14454,11 @@ int main(int argc, char** argv) {
extra_sources_reset();
for (int64_t t : ids) { const int32_t t32 = (int32_t) t; extra_sources_append(&t32, 1); }
}
// This request's source planner: the live policy as it stands at the start (the choice must not depend on the
// request's own draws, so it is not updated from them).
const auto source_policy = policy;
strata::core::MtpDrafter::DraftResult generation;
strata::core::gated::Packet proposal; // immutable through the synchronous verifier call
bool first_window = true;
int64_t produced_n = 0, sfx_windows = 0, sfx_drafts = 0, sfx_ok = 0;
int64_t chain_windows = 0, chain_drafts = 0, chain_ok = 0; // --lookup-chain's own counts
@@ -15063,29 +15156,45 @@ int main(int argc, char** argv) {
}
}
while (!pl_ran && !cancelled && produced_n < max_new) {
int T = use_mtp ? S_mtp : 1; // no --mtp: one token a round unless a lookup draft fires
int T = 1;
policy.set_context(p);
if (use_mtp && spec_cost && !first_window) {
T = std::clamp(chain_rows, 1, S_mtp);
} else if (use_mtp && req_spec_min_p > 0.0) {
T = 1;
while (T < S_mtp && dprob[(size_t) T - 1] >= (float) req_spec_min_p) ++T;
}
if (first_window) T = merge_T > 0 ? merge_T : 1;
std::copy(dprob.begin(), dprob.begin() + (std::ptrdiff_t) std::min<size_t>(dprob.size(), 8), dwin_s.begin());
const bool merged = first_window && merge_T > 0; // the prompt's last tokens and the first token
// a repeat of earlier context (prompt lookup) where the MTP's own first guess agrees: the policy takes it
// when its expected tokens per ms, from the measured acceptance and window costs, beat the MTP window's
bool from_sfx = false;
int sfx_match = 0;
if (o.suffix_draft > 0 && !first_window) {
const int k = sfx.propose(S - 1, sbuf.data());
sfx_match = sfx.last_match();
if (k > 0 && (!use_mtp || sbuf[0] == drafts[0])) {
const strata::spec::DraftPolicy::Pick pk = policy.choose(T, k, sfx_match);
if (pk.lookup) { T = pk.t; from_sfx = true; }
if (gated_proposal) {
const int room = (int) std::min<int64_t>(S, o.max_context - p);
if (!prepare_gated_window(proposal, generation, sfx, source_policy, o.suffix_draft > 0,
first_window, room, S_mtp, (int) ver.vocab(), (float) req_spec_min_p,
mtp.spec_q(), drafts.data(), dprob.data(), sbuf.data(), sfx_match, err)) {
std::printf("ERR %s\n", err.c_str()); return 1;
}
T = proposal.T; from_sfx = proposal.from_sfx;
if (first_window && merge_T > 0) T = merge_T; // STRATA_ADMIT_MERGE_FIRST: the prompt's last tokens ride in this window
} else {
T = use_mtp ? S_mtp : 1; // no --mtp: one token a round unless a lookup draft fires
if (use_mtp && spec_cost && !first_window) {
T = std::clamp(chain_rows, 1, S_mtp);
} else if (use_mtp && req_spec_min_p > 0.0) {
T = 1;
while (T < S_mtp && dprob[(size_t) T - 1] >= (float) req_spec_min_p) ++T;
}
if (first_window) T = merge_T > 0 ? merge_T : 1;
// a repeat of earlier context (prompt lookup) where the MTP's own first guess agrees: the policy takes it
// when its expected tokens per ms, from the measured acceptance and window costs, beat the MTP window's
if (o.suffix_draft > 0 && !first_window) {
const int k = sfx.propose(S - 1, sbuf.data());
sfx_match = sfx.last_match();
// a probabilistic window must not condition the source on the draw: the policy copy fixed at the request's start
// chooses, and the lookup is taken without asking whether the MTP's first guess agrees
const bool probabilistic = use_mtp && mtp.prob();
if (k > 0 && (probabilistic || !use_mtp || sbuf[0] == drafts[0])) {
const strata::spec::DraftPolicy::Pick pk = probabilistic
? source_policy.choose(S_mtp, k, sfx_match) : policy.choose(T, k, sfx_match);
if (pk.lookup) { T = pk.t; from_sfx = true; }
}
}
}
std::copy(dprob.begin(), dprob.begin() + (std::ptrdiff_t) std::min<size_t>(dprob.size(), 8), dwin_s.begin());
// --lookup-chain: what followed an earlier occurrence of the context + the MTP's drafts, after them
int chain_n = 0, cm = 0;
if (o.lookup_chain > 0 && !first_window && !from_sfx && T < S) {
@@ -15273,7 +15382,8 @@ int main(int argc, char** argv) {
// STRATA_SPEC_PROB: the MTP drafts' distributions q, judged by rejection sampling (core/spec_prob.hpp);
// a suffix window and the lookup chain's tail are point masses and keep the exact-match rule
// (never on a first window: a merged one holds prompt tokens, not drafts)
if (use_mtp && mtp.prob() && !from_sfx && !first_window && T_mtp > 1) ver.set_spec_q(mtp.spec_q(), T_mtp - 1);
if (gated_proposal) ver.set_spec_proposal(proposal.n_q ? proposal.mu.data() : nullptr, proposal.n_q);
else if (use_mtp && mtp.prob() && !from_sfx && !first_window && T_mtp > 1) ver.set_spec_q(mtp.spec_q(), T_mtp - 1);
if (!ver.run(T, window.data(), p, win_pool_fn, win_pool_user, outv.data(), err) || drive.d.failed) {
std::printf("ERR %s\n", drive.d.failed && drive.d.fail ? drive.d.fail : err.c_str());
return 1;
@@ -15353,8 +15463,11 @@ int main(int argc, char** argv) {
// commit, outv[a] is its row 0) - the drafts extend it on the device as the verify rows will
if (use_mtp && hist_n > 0 && mtp.coupled() && !eos && produced_n < max_new)
mtp.set_draft_history(consumed.data(), (int64_t) consumed.size(), outv[(size_t) a]);
generation = {}; // a skipped or failed production must not expose an older q view
const GatedDraftLimit draft_limit(mtp, gated_proposal, S_mtp, p + a + 1, o.max_context);
const bool drafted = !use_mtp || eos || produced_n >= max_new ||
mtp.draft(T, outv.data(), p, a, drafts.data(), err, dprob.data(), (float) req_spec_min_p);
mtp.draft(T, outv.data(), p, a, drafts.data(), err, dprob.data(), (float) req_spec_min_p,
nullptr, gated_proposal ? &generation : nullptr);
{
const Clock::time_point tw3 = Clock::now();
auto msd = [](Clock::time_point a0, Clock::time_point b0) { return std::chrono::duration<double, std::milli>(b0 - a0).count(); };
@@ -16621,10 +16734,23 @@ int main(int argc, char** argv) {
// plan v0.3 P6: with a native pack the first window is the last prompt token alone (it produces the first
// generated token and the MTP's first cell); otherwise the token loop already did that.
bool first_window = native_pack;
if (use_mtp && !first_window &&
!mtp.draft_first(o.spec, ss.R, x, p - 1, drafts.data(), err, dprob.data(), (float) o.spec_min_p)) {
std::fprintf(stderr, "strata generate: %s\n", err.c_str());
return 1;
const int S_mtp = o.mtp_max_t > 0 ? std::min(o.mtp_max_t, o.spec) : o.spec;
const bool gated_proposal = use_mtp && mtp.prob() && strata::core::spec_gate_pick() &&
gated_proposal_usable(o.pipeline_windows < 2 && o.batch == 0 && !multi_gpu && drive.d.remote_count == 0,
o.lookup_chain, o.adapt_swaps, o.pcie_frac != 0.0 || o.pcie_frac_auto,
spec_cost || use_df2 || !t_cycle.empty() || o.spec_corrupt > 0, o.spec, o.spec_min_p);
if (gated_proposal && !follow.empty())
std::fprintf(stderr, "strata generate: --spec-follow replaces outputs before acceptance (teacher-forced diagnostic, not sampling-law evidence)\n");
strata::core::MtpDrafter::DraftResult generation;
strata::core::gated::Packet proposal;
{ // Non-native prefill already emitted x; draft_first(cell=p-1) samples at p.
const GatedDraftLimit draft_limit(mtp, gated_proposal, S_mtp, p, o.max_context);
if (use_mtp && !first_window &&
!mtp.draft_first(o.spec, ss.R, x, p - 1, drafts.data(), err, dprob.data(), (float) o.spec_min_p,
nullptr, gated_proposal ? &generation : nullptr)) {
std::fprintf(stderr, "strata generate: %s\n", err.c_str());
return 1;
}
}
std::vector<int32_t> window((size_t) o.spec), outv((size_t) o.spec);
std::vector<uint8_t> fdiff((size_t) o.spec, 0);
@@ -16636,10 +16762,10 @@ int main(int argc, char** argv) {
std::vector<float> hash_rows;
std::vector<int64_t> accepted_hist((size_t) o.spec, 0);
int64_t rounds = 0, drafts_total = 0, drafts_ok = 0, corrupt_counter = 0;
const int S_mtp = o.mtp_max_t > 0 ? std::min(o.mtp_max_t, o.spec) : o.spec;
if (use_mtp && S_mtp < o.spec) mtp.set_max_drafts(S_mtp - 1);
strata::spec::SuffixDrafter sfx(std::max(1, o.suffix_draft), 64, (size_t) o.max_context + 4096);
strata::spec::DraftPolicy policy(o.spec); // MTP or lookup window (see draft_policy.hpp)
const auto source_policy = policy; // the lookup choice of a probabilistic window: fixed at the start, never fed by this run's draws
strata::spec::DraftPolicy::ChainState chain_st;
int chain_rows = 1; // STRATA_SPEC_COST: the window the cost-aware rule kept in the MTP chain just drafted
if (const char* tab = std::getenv("STRATA_SPEC_COST_TABLE"); spec_cost && tab != nullptr && tab[0] != '\0') {
@@ -16739,7 +16865,8 @@ int main(int argc, char** argv) {
return true;
}
#endif
return mtp.draft(T_, outv.data(), p, a_, drafts.data(), err, dprob.data(), (float) o.spec_min_p);
return mtp.draft(T_, outv.data(), p, a_, drafts.data(), err, dprob.data(), (float) o.spec_min_p,
nullptr, gated_proposal ? &generation : nullptr);
};
int win_cap = o.spec; // the widest window the VRAM allows (lowered on an out-of-memory capture, see below)
while ((int64_t) produced.size() < max_new) {
@@ -16748,6 +16875,18 @@ int main(int argc, char** argv) {
strata::core::progress_at("spec loop: choosing the window", (int64_t) produced.size());
int T = S_mtp;
policy.set_context(p);
bool from_sfx = false;
int sfx_match = 0;
if (gated_proposal) {
if (!prepare_gated_window(proposal, generation, sfx, source_policy, o.suffix_draft > 0, first_window,
(int) std::min<int64_t>(win_cap, o.max_context - p), S_mtp, (int) ver.vocab(),
(float) o.spec_min_p, mtp.spec_q(), drafts.data(), dprob.data(), sbuf.data(),
sfx_match, err)) {
std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1;
}
T = proposal.T; from_sfx = proposal.from_sfx;
std::copy(dprob.begin(), dprob.begin() + (std::ptrdiff_t) o.spec, dwin.begin());
} else {
if (use_mtp && spec_cost && !first_window) {
T = std::clamp(chain_rows, 1, S_mtp);
} else if (use_mtp && o.spec_min_p > 0.0) {
@@ -16764,16 +16903,19 @@ int main(int argc, char** argv) {
if (first_window) T = 1;
std::copy(dprob.begin(), dprob.begin() + (std::ptrdiff_t) o.spec, dwin.begin());
if (!t_cycle.empty() && !first_window) T = t_cycle[(size_t) (rounds % (int64_t) t_cycle.size())];
bool from_sfx = false;
int sfx_match = 0;
if (o.suffix_draft > 0 && !first_window) {
const int k = sfx.propose(o.spec - 1, sbuf.data());
sfx_match = sfx.last_match();
if (k > 0 && (!use_mtp || sbuf[0] == drafts[0])) {
const strata::spec::DraftPolicy::Pick pk = policy.choose(T, k, sfx_match);
// a probabilistic window must not condition the source on the draw: the policy copy fixed at the start chooses, and the
// lookup is taken without asking whether the MTP's first guess agrees
const bool probabilistic = use_mtp && mtp.prob();
if (k > 0 && (probabilistic || !use_mtp || sbuf[0] == drafts[0])) {
const strata::spec::DraftPolicy::Pick pk = probabilistic
? source_policy.choose(S_mtp, k, sfx_match) : policy.choose(T, k, sfx_match);
if (pk.lookup) { T = pk.t; from_sfx = true; }
}
}
}
// The window's rows sit at p .. p + T - 1, and past the context there is no cell to verify them in: near
// its end the window takes only the rows that fit (the admission checked the prompt and max_new, which a
// full window can overrun)
@@ -16818,11 +16960,12 @@ int main(int argc, char** argv) {
// (STRATA_ADAPT_NOWAIT=1: 0.1.37's non-blocking query, the A/B)
if (adapt_nowait()) apply_pending(false);
else if (pending.empty() || ++pending_age >= adapt_lag()) apply_pending(true);
if (use_mtp && mtp.prob() && !from_sfx && T_mtp > 1 && o.spec_corrupt <= 0) ver.set_spec_q(mtp.spec_q(), T_mtp - 1);
if (gated_proposal) ver.set_spec_proposal(proposal.n_q ? proposal.mu.data() : nullptr, proposal.n_q);
else if (use_mtp && mtp.prob() && !from_sfx && T_mtp > 1 && o.spec_corrupt <= 0) ver.set_spec_q(mtp.spec_q(), T_mtp - 1);
if (!ver.run(T, window.data(), p, &drive_pool_multi, &drive, outv.data(), err)) {
// The graph of a window size is captured the first time it is used, and the draft source took VRAM: when the capture runs
// out of memory the window gets one row narrower (the widths below it are already captured) instead of the run failing.
if (T > 2 && !first_window && err.find("out of memory") != std::string::npos) {
if (T > 2 && !first_window && !gated_proposal && err.find("out of memory") != std::string::npos) {
win_cap = T - 1;
--window_hist[(size_t) T];
std::fprintf(stderr, "strata generate: not enough VRAM left for a %d-token verify window: windows are %d tokens at most from here\n", T, win_cap);
@@ -16903,6 +17046,8 @@ int main(int argc, char** argv) {
total_ms += std::chrono::duration<double, std::milli>(Clock::now() - t0).count();
break;
}
generation = {};
const GatedDraftLimit draft_limit(mtp, gated_proposal, S_mtp, p + a + 1, o.max_context);
const bool drafted = !use_mtp || (int64_t) produced.size() >= max_new || draft_next(T, a);
if (adapt_thr.joinable()) adapt_thr.join();
if (!adapt_ok) return 1;