Files
strata/src/spec/draft_policy_test.cpp
T
Niko1221 48d2e6cabc spec: DraftPolicy measures a lookup window size again when its cost is stale (#1252)
A window size's cost was updated only when that size was chosen, and the re-probe covered only sizes seen fewer than
three times. A size priced high during one slow stretch (a prompt's cache refill) could stay priced out for the life
of the process. A size not measured for 300 rounds is now tried again when a confident lookup could use it, one
round per stretch. Output is unchanged (the policy only picks which drafts to verify); draft_policy_test gains a case
that fails without the change.
2026-10-07 03:02:55 +02:00

96 lines
5.2 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");
}
std::printf(g_fail ? "FAIL\n" : "PASS\n");
return g_fail ? 1 : 0;
}