spec: the table the learning stretch writes is used by the next start at once

A table made by the one-start stretch (STRATA_DRAFT_COSTS_LEARN=1) had no real-size marks, so the next start held it back for 100 requests (live costs decided meanwhile) and rewrote the file at every request, which also drops a read-only mode. The stretch measured every size of its table, so mark_learned_real() marks all of them real (cost rounded to the 3 decimals the file keeps, so the learning start and the next start run the same numbers) before the file is written. The next start then logs "this machine's table is used", applies the stored CPU prompt share and PCIe share at once, and never rewrites the file. The call sits inside the learning stretch's "ready" block only; the passive path (no STRATA_DRAFT_COSTS_LEARN) does not reach it.

Unit tests: the marks and costs survive the file, a marked table is neither held nor calibrated and asks for no rewrite, an unmarked table is still held.
This commit is contained in:
Niko1221 committed 2026-11-13 15:16:13 +01:00
1 parent 23c02a7f71
commit fc66df9866
4 files changed
+55 -2

No files matched your search

+4
View File
@@ -142,6 +142,10 @@ struct CostFile {
bool read_cost_file(const std::string& path, const std::string& key, CostFile& out, std::string& why);
/// Writes the file whole or not at all (a temporary file beside it, then renamed over it); makes the folder. Never throws.
bool write_cost_file(const std::string& path, const CostFile& f, std::string& why);
/// A table made by the one-start learning stretch (STRATA_DRAFT_COSTS_LEARN=1): every size of it was measured by that stretch, so all of them are marked real and the
/// next start uses the table at once (DraftPolicy::start_calibration holds back only a table with sizes not yet marked real, and the file is then not rewritten). The
/// costs are rounded to the 3 decimals the file keeps, so the start that learned the table and the start that reads it use the same numbers. Not used by the passive path.
void mark_learned_real(CostFile& cf);
class DraftPolicy {
public:
+3 -1
View File
@@ -20343,7 +20343,9 @@ int main(int argc, char** argv) {
cf.key = dc_key;
for (int k = 0; k < strata::spec::CostLearner::kCtxBuckets; ++k)
cf.tables[(size_t) k] = policy.learner().table(k, kDraftCostMinN, policy.top_size());
cf.state_effect = policy.learner().state_effect(0, kDraftCostMinN); // real rounds are counted by the same rule (fresh only, or all) // the CPU prompt share, when it is on and balancing from measured times: its balance as measured so far, to a 0.05
cf.state_effect = policy.learner().state_effect(0, kDraftCostMinN); // real rounds are counted by the same rule (fresh only, or all)
strata::spec::mark_learned_real(cf); // the stretch measured every size: the next start uses the table at once, and nothing rewrites the file
// the CPU prompt share, when it is on and balancing from measured times: its balance as measured so far, to a 0.05
// step (0.5 when no short prompt has measured it yet: on an RTX A6000 a fixed 0.5 read a 981-token chunk as fast as the
// balancing share, 1,001-1,009 against 995-997 ms); fixed from now on like the table
if (strata::prefill::Prefill::cpu_share_balancing()) {
+13
View File
@@ -429,6 +429,19 @@ bool write_cost_file(const std::string& path, const CostFile& c, std::string& wh
return true;
}
void mark_learned_real(CostFile& cf) {
for (int k = 0; k < CostLearner::kCtxBuckets; ++k) {
std::vector<double>& tab = cf.tables[(size_t) k];
for (size_t i = 0; i < tab.size(); ++i) {
const double v = std::round(tab[i] * 1000.0) / 1000.0;
if (!(v > 0.0)) continue; // never for a valid table; the size then stays not real (held, as before)
tab[i] = v;
cf.real[(size_t) k] |= 1u << i;
cf.real_cost[(size_t) k][i + 1] = v;
}
}
}
// ---------------------------------------------------------------------------------------------------- DraftPolicy
DraftPolicy::DraftPolicy(int max_t, double margin)
+35 -1
View File
@@ -893,8 +893,10 @@ int main() {
check((ch.real[0] & 1u) != 0 && h.table_held() && !h.fixed(), "calib: one real size does not bring the table into use");
}
{
// STRATA_DRAFT_COSTS_LEARN=1 keeps its one-start promise (f-dc-learn-fix): (a) a request of the learning stretch starts a new busy run.
// STRATA_DRAFT_COSTS_LEARN=1 keeps its one-start promise (f-dc-learn-fix): (a) a request of the learning stretch starts a new busy run; (b) the table it writes is used by the next start at once.
using strata::spec::CostFile;
using strata::spec::CostLearner;
namespace fs = std::filesystem;
static_assert(CostLearner::starts_busy_run(false, 0.0, false) && !CostLearner::starts_busy_run(true, 0.01, false) && CostLearner::starts_busy_run(true, 0.5, false),
"no learning stretch: the first request and a pause of kIdleS start a busy run, a request a few ms later does not");
check(CostLearner::starts_busy_run(true, 0.0, true) && CostLearner::starts_busy_run(true, 0.01, true), "learn (a): a request of the learning stretch starts a new busy run, however close");
@@ -917,6 +919,38 @@ int main() {
};
check(!sim(false).ready, "learn (a): back to back, one busy run: fresh rounds stay at the first seconds, the stretch never finishes (the batch 15 behaviour)");
check(sim(true).ready && !sim(true).disturbed, "learn (a): back to back, a busy run per request: the stretch finishes");
// (b) the written table is marked real: read back, it is used at once and nothing rewrites it
CostFile c;
c.key = "card A|engine 1|model m";
c.tables[0] = {12.3456, 14.2, 16.2004, 18.5};
c.state_effect = true;
strata::spec::mark_learned_real(c);
check(c.real[0] == 0xFu && c.real[1] == 0u && c.real_cost[0][1] == 12.346 && c.tables[0][0] == 12.346 && c.tables[0][2] == 16.2 && c.real_cost[0][3] == 16.2,
"learn (b): every size of the stretch's table is marked real, its cost rounded to the file's 3 decimals");
const fs::path dir = fs::temp_directory_path() / ("strata-dpt-learn-" + std::to_string((unsigned long long) (uintptr_t) &c));
const std::string path = (dir / "t.costs").string();
std::string why;
CostFile r;
check(strata::spec::write_cost_file(path, c, why) && strata::spec::read_cost_file(path, c.key, r, why) && r.real[0] == 0xFu && r.real_cost[0][4] == 18.5 && r.tables[0][1] == 14.2,
"learn (b): the marks and the costs survive the file");
DraftPolicy used(6);
used.set_fixed_costs(r.tables[0].data(), (int) r.tables[0].size(), 0);
used.start_calibration(r.state_effect, r.real, r.cal, r.real_cost, r.reqs);
check(used.fixed() && !used.table_held() && !used.calibrating(), "learn (b): the next start uses the table at once (no holding, no calibration)");
for (int i = 0; i < 30; ++i) used.observe(false, 2, 1, 0, 15.5);
CostFile again = r;
std::string rep;
check(!used.calibrate(again, rep) && rep.empty(), "learn (b): rounds of the next start never ask for the file to be rewritten");
// the same table WITHOUT the marks (what the stretch wrote before) is held back, as the passive path still does
CostFile u;
u.key = c.key;
u.tables[0] = {12.3456, 14.2, 16.2004, 18.5};
DraftPolicy held(6);
held.set_fixed_costs(u.tables[0].data(), 4, 0);
held.start_calibration(u.state_effect, u.real, u.cal, u.real_cost, u.reqs);
check(held.table_held() && held.calibrating() && !held.fixed(), "learn (b): a table without marks is still held back (the passive path is unchanged)");
std::error_code ec;
fs::remove_all(dir, ec);
}
std::printf(g_fail ? "FAIL\n" : "PASS\n");
return g_fail ? 1 : 0;