diff --git a/include/strata/spec/draft_policy.hpp b/include/strata/spec/draft_policy.hpp index 2408de59..0254fb71 100644 --- a/include/strata/spec/draft_policy.hpp +++ b/include/strata/spec/draft_policy.hpp @@ -51,6 +51,9 @@ public: void seed_costs(const double* ms, int n, double weight); /// The measured round cost of every window size seen: "T1:39.8(n=120) T2:..." for the log / the next process's table. std::string cost_table_text() const; + /// STRATA_SPEC_COST: a round time far above what is known for that width (the first use of a width captures its graph; a stalled + /// round) counts as a tenth of a sample, so one capture cannot price a width out of use. Off: the cost EMA as it always was. + void set_robust(bool on) { robust_ = on; } /// COST-AWARE MTP WINDOW (STRATA_SPEC_COST=1): the MTP chain is drafted one step at a time; after draft j (probability p_j) /// E_j = 1 + q_0 + .. + q_j, q_i = prod_{k<=i} a(p_k) a() = the acceptance measured for MTP drafts of that probability @@ -83,6 +86,8 @@ private: std::array ok_{}, bad_{}; // lookup drafts accepted / windows cut short, decayed std::array cok_{}, cbad_{}; // chained lookup drafts, the same (reached rounds only) double rounds_ = 0; // rounds observed so far + bool robust_ = false; + double clamp_sample(int t, double ms) const; static constexpr int kPBuckets = 8; std::array pa_ok_{}, pa_n_{}; // MTP drafts reached / accepted by probability bucket (decayed) std::array last_{}; // the round at which each size was last measured diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 3f2cda8f..fc83195b 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -11034,6 +11034,7 @@ int main(int argc, char** argv) { const bool spec_cost = [] { const char* v = std::getenv("STRATA_SPEC_COST"); return v != nullptr && v[0] == '1'; }(); // cost-aware MTP window (A/B arm) strata::spec::DraftPolicy::ChainState chain_st; int chain_rows = 1; // the window the cost-aware rule kept in the MTP chain just drafted + policy.set_robust(spec_cost); if (spec_cost && use_mtp) mtp.set_chain_rule([&](int idx, float pj) { const bool k = policy.keep_draft(chain_st, idx, pj, S_mtp); @@ -16257,6 +16258,7 @@ int main(int argc, char** argv) { } policy.seed_costs(ms, n, 6.0); } + policy.set_robust(spec_cost); if (spec_cost && use_mtp) mtp.set_chain_rule([&](int idx, float pj) { const bool k = policy.keep_draft(chain_st, idx, pj, S_mtp); diff --git a/src/spec/draft_policy.cpp b/src/spec/draft_policy.cpp index 16d997b1..1f845edf 100644 --- a/src/spec/draft_policy.cpp +++ b/src/spec/draft_policy.cpp @@ -52,6 +52,13 @@ double DraftPolicy::cost_ms(int t) const { return den > 0 ? num / den : kShape[t]; } +double DraftPolicy::clamp_sample(int t, double ms) const { + if (!robust_) return ms; + const double known = cost_ms(t); // the measured value, or the scaled guess from the other widths + if (cost_n_[t] > 0 || known > 0.0) return std::min(ms, 2.0 * known); + return ms; +} + void DraftPolicy::seed_costs(const double* ms, int n, double weight) { for (int t = 1; t <= std::min(n, kMaxT); ++t) if (ms[t - 1] > 0.0 && cost_n_[t] == 0) { @@ -112,6 +119,7 @@ DraftPolicy::Pick DraftPolicy::choose(int t_mtp, int lookup_k, int match) const void DraftPolicy::observe(bool lookup, int t, int accepted, int match, double round_ms) { t = std::clamp(t, 1, kMaxT); if (round_ms > 0) { + round_ms = clamp_sample(t, round_ms); cost_[t] = cost_n_[t] > 0 ? (1.0 - kCostAlpha) * cost_[t] + kCostAlpha * round_ms : round_ms; cost_n_[t] += 1.0; last_[t] = rounds_; @@ -214,6 +222,7 @@ int DraftPolicy::chain(int t_mtp, double p_mtp, int k_avail, int match) const { void DraftPolicy::observe_chain(int t_mtp, int k, int accepted, int match, double round_ms) { const int t = std::clamp(t_mtp + k, 1, kMaxT); if (round_ms > 0) { + round_ms = clamp_sample(t, round_ms); cost_[t] = cost_n_[t] > 0 ? (1.0 - kCostAlpha) * cost_[t] + kCostAlpha * round_ms : round_ms; cost_n_[t] += 1.0; last_[t] = rounds_;