spec: a gated request's lookup policy learns from committed rounds only, DraftPolicy choose() unchanged otherwise (#3416)

Problem (KeyanHu-git): the gated proposal needs the choice between the MTP window and a
lookup window to be fixed by the history, not by the draws of the rounds it will verify.
Freezing the policy for the whole request (the first commit) does that, but also freezes
the cost table, so a repeat-heavy request kept the window sizes it inherited. In the
author's diagnostic workload the frozen policy chose mean lookup windows of 5.87 and 3.38
on two repeat requests; with this change 4.31 and 4.05 (from the PR text; not
reproduced here).

Change: the request's copy of the DraftPolicy (source_policy) gets three calls, used only
when gated_proposal is on:
- observe_committed(): the round's accept and probe counts, applied at once. A lookup
  window counts as a probe of its size (tried_), so a size whose cost is only guessed is
  tried kProbes times, not every round.
- defer_cost(end, t, ms): the round's time is held. A time covers the window and the next
  MTP draft, so it depends on every Philox counter up to
  max(p + T - 1, p + a + 1 + S_mtp).
- apply_costs(committed): applies, in order, the held times whose end is behind the
  committed position, with the same update observe() makes (including the STRATA_SPEC_COST
  clamp), in the context bucket the round ran in; delayed_n_ keeps a held time from
  counting as a second probe.
choose() counts the probes as cost_n_ - delayed_n_ + tried_.

Port note: the DraftPolicy on the current tree keeps its costs per context bucket
(STRATA_SPEC_COST), so tried_, delayed_n_ and the held times carry the bucket. A gated
request does not run with STRATA_SPEC_COST (it falls back to the older rule), so its
copy stays in bucket 0.

Default path: unchanged. choose() reads two new terms; on any path that never calls the new
functions tried_ and delayed_n_ are +0.0 and cost_n_ - 0.0 + 0.0 == cost_n_ exactly.
Checked by driving the old DraftPolicy (namespace-renamed copy of f-027-gh-eng) and the
new one with the same random call sequences (observe, observe_chain, observe_probs,
set_context, seed_costs, set_robust, choose, chain, keep_draft; 400 sequences of 3000
steps): 25,983,538 bitwise comparisons of picks, costs and rates, 0 mismatches; one
observe_committed call makes the same harness report 60,528 mismatches. generate.cpp:
nothing calls the new functions when gated_proposal is false.

Tests: draft_policy_test (the PR's added assertions, on the bucketed class) and
spec_admission_test pass on z440 and on the owner PC.
Not covered: the sampling argument for the delay boundary (that a held time cannot steer
a window through a counter that is drawn again) is the author's; a unit test cannot show
it, only the distribution run (gate script 3416-spec-prob-dist.sh, arm NEWLK) can.
This commit is contained in:
Keyan Hu authored and Niko1221 committed 2026-10-27 01:45:36 +01:00
1 parent 499e963593
commit ca17d0f5cd
4 files changed
+124 -5

No files matched your search

+17 -4
View File
@@ -14470,9 +14470,9 @@ 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;
// This request's source planner: the live policy at its start, then only committed outcomes (no round
// times, no rows past the accepted ones: their Philox counters are drawn again by the next rounds).
auto source_policy = policy;
strata::core::MtpDrafter::DraftResult generation;
strata::core::gated::Packet proposal; // immutable through the synchronous verifier call
bool first_window = true;
@@ -15179,6 +15179,7 @@ int main(int argc, char** argv) {
int sfx_match = 0;
if (gated_proposal) {
const int room = (int) std::min<int64_t>(S, o.max_context - p);
source_policy.apply_costs(p); // held round times whose draws are all committed now
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)) {
@@ -15462,6 +15463,7 @@ int main(int argc, char** argv) {
draft_offered += T - 1;
draft_accepted += a;
}
const bool initial_round = first_window; // a single-row window: no proposal to learn from
first_window = false;
bool eos = false;
for (int i = merged ? a : 0; i <= a && produced_n < max_new && !eos; ++i) { // (merged: row T-1 only)
@@ -15529,6 +15531,12 @@ int main(int argc, char** argv) {
std::printf("ERR %s\n", err.c_str());
return 1;
}
if (gated_proposal && !eos && !initial_round)
source_policy.observe_committed(from_sfx, from_sfx ? T : std::min<int>(S_mtp, (int) std::min<int64_t>(S, o.max_context - p)),
a, sfx_match);
if (gated_proposal && !eos && !initial_round && timed_round) // after the next draft: its counters too
source_policy.defer_cost(std::max<int64_t>(p + T - 1, p + a + 1 + S_mtp), T,
std::chrono::duration<double, std::milli>(Clock::now() - round0).count());
if (spec_cost && use_mtp && timed_round && !from_sfx) policy.observe_probs(dwin_s.data(), T_mtp - 1, a);
if (timed_round && !eos && chain_n == 0)
policy.observe(from_sfx, T, a, sfx_match,
@@ -16782,7 +16790,7 @@ int main(int argc, char** argv) {
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
auto source_policy = policy; // this generation's: committed outcomes only (observe_committed)
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') {
@@ -16895,6 +16903,7 @@ int main(int argc, char** argv) {
bool from_sfx = false;
int sfx_match = 0;
if (gated_proposal) {
source_policy.apply_costs(p); // held round times whose draws are all committed now
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(),
@@ -17019,6 +17028,7 @@ int main(int argc, char** argv) {
}
int a = 0;
while (a < T - 1 && window[(size_t) a + 1] == outv[(size_t) a]) ++a;
const bool initial_round = first_window; // a single-row window: no proposal to learn from
if (first_window) {
first_window = false;
ttft_ms = std::chrono::duration<double, std::milli>(Clock::now() - t_start).count();
@@ -17077,6 +17087,9 @@ int main(int argc, char** argv) {
p += a + 1;
const double round_ms = std::chrono::duration<double, std::milli>(Clock::now() - t0).count();
total_ms += round_ms;
if (gated_proposal && !initial_round) source_policy.observe_committed(from_sfx, from_sfx ? T : std::min(S_mtp, room), a, sfx_match);
if (gated_proposal && !initial_round && timed_round) // p has advanced: the window was p - a - 1 ..
source_policy.defer_cost(std::max<int64_t>(p - a - 2 + T, p + S_mtp), T, round_ms);
if (spec_trace != nullptr && timed_round) {
std::fprintf(spec_trace, "%d %d %d %.3f", T, a, from_sfx ? 1 : 0, round_ms);
for (int i = 0; i < o.spec - 1; ++i) std::fprintf(spec_trace, " %.4f", (double) dwin[(size_t) i]);