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.
168 lines
9.4 KiB
C++
168 lines
9.4 KiB
C++
// src/spec/draft_policy_test.cpp - DraftPolicy: when does a lookup window beat the MTP's?
|
|
//
|
|
// Simulated rounds with the costs measured on the RTX 5070 (window-cost: ~10 ms more per token) check that
|
|
// 1. with no lookup proposal the MTP window is kept;
|
|
// 2. lookup drafts that are mostly rejected stop being taken (their bucket's rate falls);
|
|
// 3. lookup drafts that are always accepted are taken, and the window grows with them;
|
|
// 4. match-length buckets learn separately (short matches failing does not stop long ones);
|
|
// 5. the policy never proposes a window beyond its cap.
|
|
#include "strata/spec/draft_policy.hpp"
|
|
|
|
#include <cstdio>
|
|
|
|
using strata::spec::DraftPolicy;
|
|
|
|
namespace {
|
|
int g_fail = 0;
|
|
void check(bool ok, const char* what) {
|
|
std::printf(" %-66s %s\n", what, ok ? "ok" : "FAIL");
|
|
if (!ok) ++g_fail;
|
|
}
|
|
double cost(int t) { return 19.0 + 10.5 * (t - 1); } // ms per round, measured shape
|
|
} // namespace
|
|
|
|
int main() {
|
|
std::printf("draft_policy_test\n");
|
|
{
|
|
DraftPolicy p(6);
|
|
for (int i = 0; i < 50; ++i) p.observe(false, 4, 2, 0, cost(4)); // MTP windows of 4: 3 tokens each
|
|
const DraftPolicy::Pick k = p.choose(4, 0, 0);
|
|
check(!k.lookup && k.t == 4, "no proposal: the MTP window");
|
|
}
|
|
{
|
|
DraftPolicy p(6);
|
|
for (int i = 0; i < 50; ++i) p.observe(false, 4, 2, 0, cost(4));
|
|
for (int t = 2; t <= 6; ++t) p.observe(false, t, 0, 0, cost(t));
|
|
for (int i = 0; i < 40; ++i) p.observe(true, 6, 0, 4, cost(6)); // short matches, all rejected
|
|
check(p.lookup_rate(4) < 0.15, "rejected short-match drafts: their rate falls below 0.15");
|
|
check(!p.choose(4, 5, 4).lookup, "rejected short-match drafts: no longer taken");
|
|
for (int i = 0; i < 40; ++i) p.observe(true, 6, 5, 30, cost(6)); // long matches, all accepted
|
|
check(p.lookup_rate(30) > 0.9, "accepted long-match drafts: their rate rises above 0.9");
|
|
const DraftPolicy::Pick k = p.choose(4, 5, 30);
|
|
check(k.lookup && k.t == 6, "accepted long matches: the full lookup window is taken");
|
|
check(!p.choose(4, 5, 4).lookup, "buckets are separate: short matches still not taken");
|
|
check(p.choose(4, 20, 30).t <= 6, "never beyond the window cap");
|
|
}
|
|
{
|
|
DraftPolicy p(8);
|
|
for (int i = 0; i < 50; ++i) p.observe(false, 3, 2, 0, cost(3)); // a very good MTP: 3 of 3 tokens
|
|
for (int t = 2; t <= 8; ++t) p.observe(false, t, t - 1, 0, cost(t));
|
|
for (int i = 0; i < 40; ++i) p.observe(true, 4, 2, 8, cost(4)); // lookup at q ~ 0.67
|
|
check(!p.choose(3, 7, 8).lookup, "a mediocre lookup does not replace a strong MTP window");
|
|
}
|
|
{
|
|
DraftPolicy p(6);
|
|
for (int i = 0; i < 50; ++i) p.observe(false, 4, 3, 0, cost(4)); // a near-perfect MTP, only size 4 seen
|
|
const DraftPolicy::Pick k = p.choose(4, 5, 40);
|
|
check(k.lookup && k.t == 6, "an unmeasured size is probed for a confident lookup");
|
|
for (int i = 0; i < 3; ++i) p.observe(true, 6, 5, 40, 3.0 * cost(6)); // it turns out very expensive
|
|
check(!p.choose(4, 5, 40).lookup, "after the probes, the measured cost decides");
|
|
}
|
|
{
|
|
// #1252: a wide size priced high in one slow stretch is measured again after a while
|
|
DraftPolicy p(6);
|
|
for (int i = 0; i < 50; ++i) p.observe(false, 4, 3, 0, cost(4)); // a strong MTP
|
|
for (int t = 2; t <= 6; ++t) for (int i = 0; i < 4; ++i) p.observe(false, t, t - 1, 0, cost(t));
|
|
for (int i = 0; i < 6; ++i) p.observe(true, 6, 5, 40, 4.0 * cost(6)); // the wide size, in a slow stretch
|
|
for (int i = 0; i < 30; ++i) p.observe(true, 4, 3, 40, cost(4)); // and the short window's rate is fine
|
|
bool saw6 = false;
|
|
for (int i = 0; i < 400 && !saw6; ++i) {
|
|
const DraftPolicy::Pick k = p.choose(4, 5, 40);
|
|
if (k.lookup && k.t == 6) { saw6 = true; p.observe(true, 6, 5, 40, cost(6)); }
|
|
else p.observe(k.lookup, k.t, k.t - 1, 40, cost(k.t));
|
|
}
|
|
check(saw6, "a stale wide size is tried again within a few hundred rounds");
|
|
}
|
|
{
|
|
// --lookup-chain: rows cheap (UMA-like) and the chain always accepted -> chained; rows dear or the chain
|
|
// always rejected -> not
|
|
auto cheap = [](int t) { return 40.0 + 2.0 * (t - 1); };
|
|
DraftPolicy p(8);
|
|
for (int t = 2; t <= 8; ++t)
|
|
for (int i = 0; i < 5; ++i) p.observe(false, t, 1, 0, cheap(t));
|
|
for (int i = 0; i < 30; ++i) p.observe(false, 4, 2, 0, cheap(4));
|
|
for (int i = 0; i < 30; ++i) p.observe_chain(4, 3, 6, 12, cheap(7)); // every chained token accepted
|
|
check(p.chain(4, 0.9, 3, 12) == 3, "chain: cheap rows, always accepted -> all 3 chained");
|
|
DraftPolicy q(8);
|
|
for (int t = 2; t <= 8; ++t)
|
|
for (int i = 0; i < 5; ++i) q.observe(false, t, 1, 0, cost(t));
|
|
for (int i = 0; i < 30; ++i) q.observe_chain(4, 3, 3, 4, cost(7)); // reached, never accepted
|
|
check(q.chain(4, 0.9, 3, 4) == 0, "chain: dear rows, never accepted -> none");
|
|
check(q.chain(4, 0.9, 0, 4) == 0 && q.chain(8, 1.0, 3, 40) == 0, "chain: nothing proposed / no room -> none");
|
|
}
|
|
{
|
|
// The stale-size check must also run when a shorter chain already pays. Synthetic costs isolate the
|
|
// selection policy: even a size that stays expensive is retried, but only once per stale interval.
|
|
DraftPolicy p(6);
|
|
for (int i = 0; i < 3; ++i) p.observe(false, 4, 3, 0, 40.0);
|
|
for (int i = 0; i < 3; ++i) p.observe_chain(4, 1, 4, 40, 42.0);
|
|
for (int i = 0; i < 3; ++i) p.observe_chain(4, 2, 5, 40, 1000.0);
|
|
check(p.chain(4, 1.0, 2, 40) == 1, "chain: the profitable shorter chain initially wins");
|
|
int probes = 0, last_probe = -1;
|
|
bool spaced = true, confident = true;
|
|
for (int i = 0; i < 1000; ++i) {
|
|
const int k = p.chain(4, 1.0, 2, 40);
|
|
if (k == 2) {
|
|
++probes;
|
|
spaced &= last_probe < 0 ? i >= 300 : i - last_probe > 300;
|
|
last_probe = i;
|
|
confident &= p.chain(4, 0.5, 2, 40) < 2;
|
|
}
|
|
p.observe_chain(4, k, 3 + k, 40, k == 2 ? 1000.0 : 42.0);
|
|
}
|
|
check(probes > 0 && probes <= 3 && spaced,
|
|
"chain: stale full size is re-probed despite a profitable shorter chain");
|
|
check(probes > 0 && confident, "chain: stale re-probes retain the MTP confidence gate");
|
|
}
|
|
{
|
|
// Gated proposals: within one generation the policy copy learns from committed outcomes only. A size
|
|
// whose cost is still guessed is probed until kProbes rounds of it have run, timed or not, not every round.
|
|
DraftPolicy live(6);
|
|
for (int i = 0; i < 50; ++i) live.observe(false, 4, 3, 0, cost(4)); // a strong MTP
|
|
live.observe(true, 6, 5, 8, 3.0 * cost(6)); // size 6 timed once, dear
|
|
DraftPolicy frozen = live, copy = live;
|
|
int frozen_probes = 0, copy_probes = 0;
|
|
for (int i = 0; i < 20; ++i) {
|
|
const DraftPolicy::Pick f = frozen.choose(4, 5, 8);
|
|
frozen_probes += f.lookup && f.t == 6;
|
|
const DraftPolicy::Pick c = copy.choose(4, 5, 8);
|
|
copy_probes += c.lookup && c.t == 6;
|
|
copy.observe_committed(c.lookup, c.lookup ? c.t : 4, c.lookup ? 5 : 3, 8);
|
|
}
|
|
check(frozen_probes == 20, "gated: a frozen copy probes the guessed size every round");
|
|
check(copy_probes == 2, "gated: committed outcomes end the probes after kProbes rounds of the size");
|
|
check(copy.cost_ms(6) == live.cost_ms(6), "gated: committed outcomes leave the measured costs alone");
|
|
}
|
|
{
|
|
// Gated proposals: a round's time reaches the generation's copy only once the committed position has passed
|
|
// every counter it depends on. Cheap costs from earlier text stop steering long lookups once the dear
|
|
// repetitive-text times are applied; a held time is not a new probe.
|
|
DraftPolicy live(6);
|
|
for (int t = 2; t <= 6; ++t) for (int i = 0; i < 5; ++i) live.observe(false, t, 1, 0, cost(t) * 0.2); // cheap stretch
|
|
DraftPolicy copy = live;
|
|
check(copy.choose(4, 5, 40).lookup && copy.choose(4, 5, 40).t == 6, "delayed: cheap inherited costs take the full lookup");
|
|
const double before = copy.cost_ms(6);
|
|
copy.observe_committed(true, 6, 5, 40);
|
|
copy.defer_cost(110, 6, 40.0 * cost(6));
|
|
copy.apply_costs(110);
|
|
check(copy.cost_ms(6) == before, "delayed: a time is held while its counters can still be drawn");
|
|
copy.apply_costs(111);
|
|
check(copy.cost_ms(6) > before, "delayed: applied once the committed position passes its last counter");
|
|
for (int i = 0; i < 40; ++i) {
|
|
copy.observe_committed(true, 6, 5, 40);
|
|
copy.defer_cost(120 + i, 6, 40.0 * cost(6));
|
|
copy.apply_costs(121 + i);
|
|
}
|
|
check(copy.choose(4, 5, 40).t < 6, "delayed: dear measured times shorten the lookup window");
|
|
DraftPolicy fresh(6);
|
|
fresh.observe_committed(true, 6, 5, 40);
|
|
fresh.defer_cost(5, 6, 30.0);
|
|
fresh.apply_costs(6);
|
|
fresh.observe_committed(true, 6, 5, 40);
|
|
check(fresh.choose(4, 5, 40).lookup && fresh.choose(4, 5, 40).t == 6,
|
|
"delayed: a held time does not count as another probe");
|
|
}
|
|
std::printf(g_fail ? "FAIL\n" : "PASS\n");
|
|
return g_fail ? 1 : 0;
|
|
}
|