diff --git a/include/strata/spec/draft_policy.hpp b/include/strata/spec/draft_policy.hpp index 600b1b8408..814a1acbba 100644 --- a/include/strata/spec/draft_policy.hpp +++ b/include/strata/spec/draft_policy.hpp @@ -123,6 +123,7 @@ struct CostFile { bool state_effect = false; ///< the learning stretch showed a load-time effect: only fresh real rounds are counted std::array real{}; ///< bit t-1: the cost of size t is a median of real rounds, no longer the learned one std::array, CostLearner::kCtxBuckets> cal{}; ///< real rounds collected so far for the sizes not yet replaced + std::array, CostLearner::kCtxBuckets> real_cost{}; ///< the cost of a size marked real (the table lines keep the learned costs) }; /// false (with `why` for the log) when the file is missing, unreadable, malformed or truncated, or was learned under another key: the caller /// then learns a new table. Never throws. @@ -227,11 +228,16 @@ public: /// the machine has a load-time effect), across starts through the table file (CostFile::cal). At kCalibN rounds the size's cost becomes their mean /// (a result outside [kCalibOkLo, kCalibOkHi] x the learned cost means a disturbed stretch: the rounds are dropped and collected again) and the size /// is marked real (CostFile::real) for good: no later change, so the windows of a repeated start are the same. A size that real requests never reach - /// keeps its learned cost (nothing is guessed from other sizes). real and cal come from the file (CostFile). + /// keeps its learned cost, except for the provisional step below. real, cal and real_cost come from the file (CostFile). + /// Provisional step: a size that is not real yet and has 2 or more rows is priced at its learned cost x the mean of (real cost / learned cost) over the + /// real sizes of 2 or more rows (when there are some), so a size that real requests reach rarely (the 4-row lookup: about one round per request) is not + /// left at a price that the sizes next to it have already left. Scaling every size alike would change no choice (the policy compares costs with each + /// other); this changes the prices relative to each other, from the engine's own measurements, and only until the size has its own 16 rounds. static constexpr int kCalibN = 16; static constexpr double kCalibLo = 0.85, kCalibHi = 1.30, kCalibOkLo = 0.86, kCalibOkHi = 1.29; void start_calibration(bool state_effect, const std::array& real, - const std::array, kCtxBuckets>& cal); + const std::array, kCtxBuckets>& cal, + const std::array, kCtxBuckets>& real_cost); bool calibrating() const { return calib_on_; } bool calib_dirty() const { return calib_dirty_; } /// At a request's end: replaces the sizes that have kCalibN real rounds and puts the table, the real marks and the rounds collected so far into @@ -289,7 +295,9 @@ private: std::array fixed_n_{}; // the length of each bucket's fixed table std::array real_{}; std::array, kCtxBuckets> cal_{}; + std::array, kCtxBuckets> learned_{}, real_cost_{}; // the table as fixed (learned); the cost of each real size void calib_add(int bucket, int t, double ms, double age); + void apply_provisional(); int explore_size(int lookup_k, int pad_cap) const; // the size a learning stretch measures next, 0 = none (choose() counts it) int explore_next(int reach, int skip) const; // the smallest size 2..reach (not `skip`) that still needs rounds; counts it; 0 = none bool explore_ok() const; // a forced round counts now (fresh phase, or no state effect) diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 3c58501e69..04f52a8f92 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -14568,7 +14568,7 @@ int main(int argc, char** argv) { sizes += (sizes.empty() ? "" : ", ") + std::to_string(cf.tables[(size_t) k].size()); } dc_file = cf; - policy.start_calibration(cf.state_effect, cf.real, cf.cal); + policy.start_calibration(cf.state_effect, cf.real, cf.cal, cf.real_cost); dc_calib = policy.calibrating(); if (cf.cpu_share >= 0.0) strata::prefill::Prefill::fix_cpu_share(cf.cpu_share); std::string extra = cf.cpu_share >= 0.0 ? "; CPU prompt share " + std::to_string(cf.cpu_share).substr(0, 4) : ""; @@ -20085,7 +20085,7 @@ int main(int argc, char** argv) { policy.reset_learning(); dc_learning = false; dc_file = cf; - policy.start_calibration(cf.state_effect, cf.real, cf.cal); + policy.start_calibration(cf.state_effect, cf.real, cf.cal, cf.real_cost); dc_calib = policy.calibrating(); std::string t0; char b[24]; diff --git a/src/spec/draft_policy.cpp b/src/spec/draft_policy.cpp index 6198fc638a..79f4a8cabc 100644 --- a/src/spec/draft_policy.cpp +++ b/src/spec/draft_policy.cpp @@ -308,6 +308,16 @@ bool read_cost_file(const std::string& path, const std::string& key, CostFile& o out.cal[(size_t) cb][(size_t) ct] = CalAcc{cn, cs}; continue; } + if (line.rfind("rc ", 0) == 0) { + int rb = -1, rt = -1; + double rv = -1.0; + if (std::sscanf(line.c_str(), "rc %d %d %lf", &rb, &rt, &rv) != 3 || rb < 0 || rb >= CostLearner::kCtxBuckets || rt < 1 || rt > CostLearner::kMaxT || !(rv > 0.0) || !(rv < 1e5)) { + why = "a malformed real cost"; + return false; + } + out.real_cost[(size_t) rb][(size_t) rt] = rv; + continue; + } int b = -1, used = 0; if (std::sscanf(line.c_str(), "bucket %d %n", &b, &used) < 1 || used == 0 || b < 0 || b >= CostLearner::kCtxBuckets) { why = "a malformed line"; @@ -358,6 +368,11 @@ bool write_cost_file(const std::string& path, const CostFile& c, std::string& wh if (c.state_effect) f << "state 1\n"; for (int k = 0; k < CostLearner::kCtxBuckets; ++k) { if (c.real[(size_t) k] != 0u) f << "real " << k << " " << c.real[(size_t) k] << "\n"; + for (int t = 1; t <= CostLearner::kMaxT; ++t) + if ((c.real[(size_t) k] >> (t - 1) & 1u) && c.real_cost[(size_t) k][(size_t) t] > 0.0) { + std::snprintf(b, sizeof b, "%.4f", c.real_cost[(size_t) k][(size_t) t]); + f << "rc " << k << " " << t << " " << b << "\n"; + } for (int t = 1; t <= CostLearner::kMaxT; ++t) { const CalAcc& a = c.cal[(size_t) k][(size_t) t]; if (a.n <= 0 || (c.real[(size_t) k] >> (t - 1) & 1u)) continue; @@ -401,6 +416,7 @@ void DraftPolicy::set_fixed_costs(const double* ms, int n, int bucket) { const bool in = t <= n && ms[t - 1] > 0.0; cost_[bucket][t] = in ? ms[t - 1] : 0.0; cost_n_[bucket][t] = in ? 1e6 : 0.0; + learned_[(size_t) bucket][(size_t) t] = cost_[bucket][t]; } } @@ -623,11 +639,38 @@ void DraftPolicy::observe(bool lookup, int t, int accepted, int match, double ro // ---- calibration of a fixed table against real requests (see the header) +void DraftPolicy::apply_provisional() { + for (int k = 0; k < kCtxBuckets; ++k) { + const int n = fixed_n_[(size_t) k]; + if (n <= 0) continue; + double sum = 0.0; + int cnt = 0; + for (int t = 2; t <= n; ++t) + if ((real_[(size_t) k] >> (t - 1) & 1u) && learned_[(size_t) k][(size_t) t] > 0.0) { + sum += real_cost_[(size_t) k][(size_t) t] / learned_[(size_t) k][(size_t) t]; + ++cnt; + } + const double r = cnt > 0 ? sum / cnt : 1.0; + for (int t = 1; t <= n; ++t) { + if (real_[(size_t) k] >> (t - 1) & 1u) cost_[(size_t) k][(size_t) t] = real_cost_[(size_t) k][(size_t) t]; + else cost_[(size_t) k][(size_t) t] = learned_[(size_t) k][(size_t) t] * (t >= 2 ? r : 1.0); + } + for (int t = 2; t <= n; ++t) // more rows never cost less + if (cost_[(size_t) k][(size_t) t] < cost_[(size_t) k][(size_t) t - 1]) cost_[(size_t) k][(size_t) t] = cost_[(size_t) k][(size_t) t - 1]; + } +} + void DraftPolicy::start_calibration(bool state_effect, const std::array& real, - const std::array, kCtxBuckets>& cal) { + const std::array, kCtxBuckets>& cal, + const std::array, kCtxBuckets>& real_cost) { calib_fresh_only_ = state_effect; real_ = real; cal_ = cal; + real_cost_ = real_cost; + for (int k = 0; k < kCtxBuckets; ++k) // a mark without a cost (a damaged file) is no mark + for (int t = 1; t <= kMaxT; ++t) + if ((real_[(size_t) k] >> (t - 1) & 1u) && !(real_cost_[(size_t) k][(size_t) t] > 0.0)) real_[(size_t) k] &= ~(1u << (t - 1)); + apply_provisional(); calib_dirty_ = false; calib_on_ = false; for (int k = 0; k < kCtxBuckets; ++k) { @@ -641,7 +684,7 @@ void DraftPolicy::calib_add(int bucket, int t, double ms, double age) { if (bucket < 0 || bucket >= kCtxBuckets || t < 1 || t > fixed_n_[(size_t) bucket]) return; if (real_[(size_t) bucket] >> (t - 1) & 1u) return; if (calib_fresh_only_ && age > CostLearner::kFreshS) return; // the table is what rounds of the first seconds of a busy run cost - const double base = cost_[(size_t) bucket][(size_t) t]; + const double base = learned_[(size_t) bucket][(size_t) t]; if (!(base > 0.0)) return; CalAcc& a = cal_[(size_t) bucket][(size_t) t]; if (a.n >= kCalibN) return; @@ -654,28 +697,42 @@ bool DraftPolicy::calibrate(CostFile& cf, std::string& report) { report.clear(); if (!calib_on_ && !calib_dirty_) return false; char b[96]; - bool changed = calib_dirty_; + bool changed = calib_dirty_, replaced = false; for (int k = 0; k < kCtxBuckets; ++k) { const int n = fixed_n_[(size_t) k]; for (int t = 1; t <= n; ++t) { if (real_[(size_t) k] >> (t - 1) & 1u) continue; CalAcc& a = cal_[(size_t) k][(size_t) t]; if (a.n < kCalibN) continue; - const double v = a.sum / a.n, base = cost_[(size_t) k][(size_t) t]; + const double v = a.sum / a.n, base = learned_[(size_t) k][(size_t) t]; if (v >= kCalibOkLo * base && v <= kCalibOkHi * base) { std::snprintf(b, sizeof b, "%s%d rows %.2f -> %.2f", report.empty() ? "" : ", ", t, base, v); report += b; - cost_[(size_t) k][(size_t) t] = v; + real_cost_[(size_t) k][(size_t) t] = v; real_[(size_t) k] |= 1u << (t - 1); + replaced = true; } a = CalAcc{}; // replaced, or disturbed (rounds dropped, collected again) } - for (int t = 2; t <= n; ++t) // more rows never cost less - if (cost_[(size_t) k][(size_t) t] < cost_[(size_t) k][(size_t) t - 1]) cost_[(size_t) k][(size_t) t] = cost_[(size_t) k][(size_t) t - 1]; - if (n > 0) { - cf.tables[(size_t) k].assign(cost_[(size_t) k].begin() + 1, cost_[(size_t) k].begin() + 1 + n); - cf.real[(size_t) k] = real_[(size_t) k]; - for (int t = 0; t <= kMaxT; ++t) cf.cal[(size_t) k][(size_t) t] = cal_[(size_t) k][(size_t) t]; + } + if (replaced) { + std::array, kCtxBuckets> before = cost_; + apply_provisional(); + for (int k = 0; k < kCtxBuckets; ++k) + for (int t = 1; t <= fixed_n_[(size_t) k]; ++t) + if (!(real_[(size_t) k] >> (t - 1) & 1u) && std::fabs(cost_[(size_t) k][(size_t) t] - before[(size_t) k][(size_t) t]) > 0.005) { + std::snprintf(b, sizeof b, "%s%d rows (not real yet) %.2f -> %.2f", report.empty() ? "" : ", ", t, before[(size_t) k][(size_t) t], cost_[(size_t) k][(size_t) t]); + report += b; + } + } + for (int k = 0; k < kCtxBuckets; ++k) { + const int n = fixed_n_[(size_t) k]; + if (n <= 0) continue; + cf.tables[(size_t) k].assign(learned_[(size_t) k].begin() + 1, learned_[(size_t) k].begin() + 1 + n); // the file keeps the learned costs + cf.real[(size_t) k] = real_[(size_t) k]; + for (int t = 0; t <= kMaxT; ++t) { + cf.cal[(size_t) k][(size_t) t] = cal_[(size_t) k][(size_t) t]; + cf.real_cost[(size_t) k][(size_t) t] = real_cost_[(size_t) k][(size_t) t]; } } cf.state_effect = calib_fresh_only_; @@ -684,7 +741,7 @@ bool DraftPolicy::calibrate(CostFile& cf, std::string& report) { for (int k = 0; k < kCtxBuckets; ++k) for (int t = 1; t <= fixed_n_[(size_t) k]; ++t) if (!(real_[(size_t) k] >> (t - 1) & 1u)) calib_on_ = true; - return changed || !report.empty(); + return changed || replaced; } void DraftPolicy::observe_committed(bool lookup, int t, int accepted, int match) { diff --git a/src/spec/draft_policy_test.cpp b/src/spec/draft_policy_test.cpp index 72d99f9f8b..0f9e40c49a 100644 --- a/src/spec/draft_policy_test.cpp +++ b/src/spec/draft_policy_test.cpp @@ -550,60 +550,74 @@ int main() { using strata::spec::CostLearner; using strata::spec::read_cost_file; using strata::spec::write_cost_file; + using Cal = std::array, CostLearner::kCtxBuckets>; + using Rc = std::array, CostLearner::kCtxBuckets>; + using Real = std::array; const double tab[5] = {12.68, 14.30, 16.41, 18.57, 20.90}; + const Cal cal0{}; + const Rc rc0{}; DraftPolicy c(8); c.set_fixed_costs(tab, 5, 0); - std::array real0{}; - std::array, CostLearner::kCtxBuckets> cal0{}; - c.start_calibration(false, real0, cal0); + c.start_calibration(false, Real{}, cal0, rc0); check(c.calibrating() && !c.calib_dirty(), "calib: on after a table is fixed, nothing collected"); for (int i = 0; i < 15; ++i) c.observe(false, 3, 2, 0, 16.70); CostFile cf; std::string rep; check(c.calibrate(cf, rep) && rep.empty() && cf.cal[0][3].n == 15, "calib: 15 real rounds are kept, nothing replaced"); - check(std::fabs(c.cost_ms(3) - 16.41) < 1e-9, "calib: the cost of a size stays at the learned one under kCalibN rounds"); + check(std::fabs(c.cost_ms(3) - 16.41) < 1e-9 && std::fabs(c.cost_ms(4) - 18.57) < 1e-9, "calib: under kCalibN rounds every cost stays the learned one"); c.observe(false, 3, 2, 0, 16.70); check(c.calibrate(cf, rep) && !rep.empty(), "calib: the 16th real round replaces the cost"); - check(std::fabs(c.cost_ms(3) - 16.70) < 1e-9 && (cf.real[0] & 4u) && std::fabs(cf.tables[0][2] - 16.70) < 1e-9, "calib: the cost is the mean, the size is marked real, the file has it"); - check(std::fabs(c.cost_ms(5) - 20.90) < 1e-9 && std::fabs(c.cost_ms(4) - 18.57) < 1e-9, "calib: a size without real rounds keeps its learned cost (nothing guessed)"); + const double r3 = 16.70 / 16.41; + check(std::fabs(c.cost_ms(3) - 16.70) < 1e-9 && (cf.real[0] & 4u) && std::fabs(cf.real_cost[0][3] - 16.70) < 1e-9 && std::fabs(cf.tables[0][2] - 16.41) < 1e-9, + "calib: the size takes the mean, is marked real; the file keeps the learned cost beside it"); + check(std::fabs(c.cost_ms(4) - 18.57 * r3) < 1e-6 && std::fabs(c.cost_ms(5) - 20.90 * r3) < 1e-6 && std::fabs(c.cost_ms(2) - 14.30 * r3) < 1e-6 && std::fabs(c.cost_ms(1) - 12.68) < 1e-9, + "calib: sizes not real yet (2 rows and more) are priced at learned x the real sizes' ratio, size 1 stays"); for (int i = 0; i < 40; ++i) c.observe(false, 3, 2, 0, 15.0); check(!c.calib_dirty() && std::fabs(c.cost_ms(3) - 16.70) < 1e-9, "calib: a real size never changes again"); - // a lookup size reached once per request: rounds come across starts through the file - DraftPolicy d(8); - d.set_fixed_costs(tab, 5, 0); + // a lookup size reached once per request: its rounds come across starts through the file CostFile cd; cd.key = "k"; - cd.tables[0].assign(tab, tab + 5); - std::array, CostLearner::kCtxBuckets> cal1 = cal0; + Cal cal1 = cal0; + Real real1{}; + Rc rc1 = rc0; for (int start = 0; start < 16; ++start) { DraftPolicy e(8); e.set_fixed_costs(tab, 5, 0); - e.start_calibration(false, real0, cal1); + e.start_calibration(false, real1, cal1, rc1); e.observe(true, 4, 2, 0, 18.9); std::string r2; check(e.calibrate(cd, r2), "calib: a start with one real round of a size writes the file"); cal1 = cd.cal; - real0 = cd.real; + real1 = cd.real; + rc1 = cd.real_cost; } - check((cd.real[0] & 8u) != 0 && std::fabs(cd.tables[0][3] - 18.9) < 1e-9 && cd.cal[0][4].n == 0, "calib: 16 starts of one round each calibrate the size"); - // the file round trip: state, real, cal + check((cd.real[0] & 8u) != 0 && std::fabs(cd.real_cost[0][4] - 18.9) < 1e-9 && cd.cal[0][4].n == 0 && std::fabs(cd.tables[0][3] - 18.57) < 1e-9, + "calib: 16 starts of one round each calibrate the size"); + DraftPolicy again(8); // a start from that file: size 4 real, the others provisional at 18.9 / 18.57 + again.set_fixed_costs(tab, 5, 0); + again.start_calibration(false, real1, cal1, rc1); + check(std::fabs(again.cost_ms(4) - 18.9) < 1e-9 && std::fabs(again.cost_ms(5) - 20.90 * 18.9 / 18.57) < 1e-6, "calib: a restart gives the same costs"); + // the file round trip: state, real, real cost, cal CostFile w; w.key = "k2"; w.tables[0].assign(tab, tab + 5); w.state_effect = true; w.real[0] = 6u; + w.real_cost[0][2] = 14.6; + w.real_cost[0][3] = 16.7; w.cal[0][4] = CalAcc{7, 131.0}; const std::string path = "dp_calib_test.costs"; std::string why; check(write_cost_file(path, w, why), "calib file: written"); CostFile r; - check(read_cost_file(path, "k2", r, why) && r.state_effect && r.real[0] == 6u && r.cal[0][4].n == 7 && std::fabs(r.cal[0][4].sum - 131.0) < 1e-3 && r.cal[0][2].n == 0, - "calib file: state, real marks and collected rounds come back"); + check(read_cost_file(path, "k2", r, why) && r.state_effect && r.real[0] == 6u && r.cal[0][4].n == 7 && std::fabs(r.cal[0][4].sum - 131.0) < 1e-3 && r.cal[0][2].n == 0 && + std::fabs(r.real_cost[0][2] - 14.6) < 1e-9 && std::fabs(r.real_cost[0][3] - 16.7) < 1e-9, + "calib file: state, real marks, real costs and collected rounds come back"); std::remove(path.c_str()); // a machine with a load-time effect counts only the first kFreshS seconds of a busy run DraftPolicy f(8); f.set_fixed_costs(tab, 5, 0); - f.start_calibration(true, real0 = {}, cal0); + f.start_calibration(true, Real{}, cal0, rc0); f.set_busy_age(2.0); for (int i = 0; i < 10; ++i) f.observe(false, 2, 1, 0, 14.6); f.set_busy_age(50.0); @@ -615,12 +629,22 @@ int main() { // a disturbed stretch (a stalled machine) is dropped, not taken DraftPolicy g(8); g.set_fixed_costs(tab, 5, 0); - g.start_calibration(false, real0 = {}, cal0); - for (int i = 0; i < 16; ++i) g.observe(false, 1, 0, 0, 40.0); + g.start_calibration(false, Real{}, cal0, rc0); + for (int i = 0; i < 16; ++i) g.observe(false, 2, 0, 0, 40.0); CostFile cg; std::string rg; g.calibrate(cg, rg); - check(rg.empty() && std::fabs(g.cost_ms(1) - 12.68) < 1e-9 && cg.cal[0][1].n == 0 && (cg.real[0] & 1u) == 0, "calib: rounds far above the learned cost are dropped"); + check(rg.empty() && std::fabs(g.cost_ms(2) - 14.30) < 1e-9 && cg.cal[0][2].n == 0 && (cg.real[0] & 2u) == 0, "calib: rounds far above the learned cost are dropped"); + // a size that real requests never reach, and a machine whose real sizes are all 1 row: nothing is scaled + DraftPolicy h(8); + h.set_fixed_costs(tab, 5, 0); + h.start_calibration(false, Real{}, cal0, rc0); + for (int i = 0; i < 16; ++i) h.observe(false, 1, 0, 0, 13.0); + CostFile ch; + std::string rh; + h.calibrate(ch, rh); + check((ch.real[0] & 1u) != 0 && std::fabs(h.cost_ms(1) - 13.0) < 1e-9 && std::fabs(h.cost_ms(2) - 14.30) < 1e-9 && std::fabs(h.cost_ms(5) - 20.90) < 1e-9, + "calib: a real 1-row size scales nothing else"); } std::printf(g_fail ? "FAIL\n" : "PASS\n"); return g_fail ? 1 : 0;