mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
serve: repeatable output also fixes the decode PCIe share: PcieAuto's settled value is stored with the draft cost table
PcieAuto refines the decode PCIe share (which of a layer's missed experts the GPU computes after a PCIe copy, the rest on the CPU, which rounds differently) at fixed window counts, but the value it picks comes from the measured CPU pool time. On an RTX A6000 (IQ3_S, DFlash2 Auto, frozen table, prompt share 0.5) two of 18 starts settled one reading later (0.10 -> 0.15 at window 144 instead of 48) and gave other tokens from the second request on; with the share fixed at 0.15 12 of 12 traced starts were identical, and identical to the 16 other starts. - The table file gets an optional "pcie" line (0..1). The start that writes the table stores the share PcieAuto has then (its third reading, or what it reached by that request's end) and fixes it from the next request; a start that loads a file with the line runs with that share and PcieAuto off. - A file written before this has no pcie line: it loads as before, PcieAuto refines as today, and once it has settled (three readings, or it gave up) the share is written into the same file and fixed; the next start loads it. - One GPU only (a layer split keeps PcieAuto's per-stage refinement). Same key, same ignore-and-relearn rules; without --draft-costs nothing changes. - draft_policy_test: the pcie round trip, an old file without it, out-of-range and malformed values refused.
This commit is contained in:
1 parent
eb92671d60
commit
6e4afbbc9d
4 files changed
+71
-2
No files matched your search
@@ -74,11 +74,13 @@ private:
|
||||
/// key <one line>
|
||||
/// bucket <0..2> <ms of T1>,<ms of T2>,...
|
||||
/// share <0..1> (optional: the CPU prompt share learned on this machine, src/prefill/prefill.cpp)
|
||||
/// pcie <0..1> (optional: the decode PCIe share PcieAuto settled on, src/program/generate.cpp)
|
||||
/// end
|
||||
struct CostFile {
|
||||
std::string key;
|
||||
std::array<std::vector<double>, CostLearner::kCtxBuckets> tables{};
|
||||
double cpu_share = -1.0; ///< < 0: none stored
|
||||
double pcie_frac = -1.0; ///< the decode PCIe share PcieAuto settled on; < 0: none stored (a file from before it: refined, then written)
|
||||
};
|
||||
/// 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.
|
||||
|
||||
@@ -12922,6 +12922,18 @@ int main(int argc, char** argv) {
|
||||
gs.link_gbps, gs.pcie_frac);
|
||||
}
|
||||
};
|
||||
// --draft-costs (repeatable output): PcieAuto's value comes from measured pool time, and a start that settles one reading later decodes
|
||||
// its windows in between with another share (one more or one fewer missed expert of a layer computed on the GPU instead of the CPU,
|
||||
// which round differently: on an RTX A6000 2 of 18 starts gave other tokens, 0 of 12 with the share fixed). So the settled share is
|
||||
// stored with the table and a later start runs with it fixed. One GPU only (a layer split keeps PcieAuto's per-stage refinement).
|
||||
const bool dc_pcie_ok = pcie_auto.on && n_stages == 1;
|
||||
auto fix_pcie_share = [&](double f) {
|
||||
pcie_auto.on = false;
|
||||
o.pcie_frac = f;
|
||||
drive.d.pcie_num = std::max(0, std::min(256, (int) (f * 256.0 + 0.5)));
|
||||
split_drive.pcie_num[0] = drive.d.pcie_num;
|
||||
split_drive_b.pcie_num[0] = drive.d.pcie_num;
|
||||
};
|
||||
// the serial windows of one GPU, where the pool can admit (expert_pool_dispatch_multi's fetch_on)
|
||||
const char* fetch_off = d_res == nullptr ? "no residency table" : o.spec_split ? "--spec-split"
|
||||
: multi_gpu || !stages.empty() ? "a layer split" : peer.valid() ? "--peer-device"
|
||||
@@ -14183,6 +14195,8 @@ int main(int argc, char** argv) {
|
||||
constexpr int kDraftCostMinN = 20; // a window size needs this many rounds to be measured (fewer: filled in)
|
||||
std::string dc_key;
|
||||
bool dc_learning = false;
|
||||
bool dc_pcie_pending = false; // a loaded table without a PCIe share: written into it once PcieAuto has settled
|
||||
strata::spec::CostFile dc_file; // that table, as loaded
|
||||
if (!o.draft_costs.empty() && !policy.fixed()) {
|
||||
dc_key = draft_costs_key(argc, argv, o.draft_costs_key, spec_cost);
|
||||
strata::spec::CostFile cf;
|
||||
@@ -14195,9 +14209,17 @@ int main(int argc, char** argv) {
|
||||
sizes += (sizes.empty() ? "" : ", ") + std::to_string(cf.tables[(size_t) k].size());
|
||||
}
|
||||
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) : "";
|
||||
if (dc_pcie_ok && cf.pcie_frac >= 0.0) { // PcieAuto's settled share: fixed, no refinement in this run
|
||||
fix_pcie_share(cf.pcie_frac);
|
||||
extra += "; PCIe share " + std::to_string(cf.pcie_frac).substr(0, 4);
|
||||
} else if (dc_pcie_ok) { // a file from before the stored PCIe share: refined as before, then written into the file
|
||||
dc_pcie_pending = true;
|
||||
dc_file = cf;
|
||||
extra += "; PCIe share: refined in this run, then stored";
|
||||
}
|
||||
std::fprintf(stderr, "strata serve: draft costs: this machine's table is used (%s; window sizes %s%s): the same request "
|
||||
"gives the same tokens on every start\n", o.draft_costs.c_str(), sizes.c_str(),
|
||||
cf.cpu_share >= 0.0 ? ("; CPU prompt share " + std::to_string(cf.cpu_share).substr(0, 4)).c_str() : "");
|
||||
"gives the same tokens on every start\n", o.draft_costs.c_str(), sizes.c_str(), extra.c_str());
|
||||
} else {
|
||||
policy.set_learning(true);
|
||||
dc_learning = true;
|
||||
@@ -19561,9 +19583,13 @@ int main(int argc, char** argv) {
|
||||
const double s = pst.cpu_share_readings > 0 ? pst.cpu_share_balanced : 0.5;
|
||||
cf.cpu_share = std::clamp(std::round(s * 20.0) / 20.0, 0.05, 0.9);
|
||||
}
|
||||
// the decode PCIe share as PcieAuto has it now (its third reading, or what it had reached by this request's end);
|
||||
// fixed from now on like the table
|
||||
if (dc_pcie_ok) cf.pcie_frac = o.pcie_frac;
|
||||
std::string why;
|
||||
const bool saved = strata::spec::write_cost_file(o.draft_costs, cf, why);
|
||||
if (cf.cpu_share >= 0.0) strata::prefill::Prefill::fix_cpu_share(cf.cpu_share);
|
||||
if (cf.pcie_frac >= 0.0) fix_pcie_share(cf.pcie_frac);
|
||||
for (int k = 0; k < strata::spec::CostLearner::kCtxBuckets; ++k)
|
||||
if (!cf.tables[(size_t) k].empty())
|
||||
policy.set_fixed_costs(cf.tables[(size_t) k].data(), (int) cf.tables[(size_t) k].size(), k);
|
||||
@@ -19580,10 +19606,27 @@ int main(int argc, char** argv) {
|
||||
std::snprintf(b, sizeof b, "; CPU prompt share %.2f", cf.cpu_share);
|
||||
t0 += b;
|
||||
}
|
||||
if (cf.pcie_frac >= 0.0) {
|
||||
std::snprintf(b, sizeof b, "; PCIe share %.2f", cf.pcie_frac);
|
||||
t0 += b;
|
||||
}
|
||||
std::fprintf(stderr, "strata serve: draft costs: this machine's table is learned (%s); it is fixed from the next request "
|
||||
"on%s%s\n", t0.c_str(), saved ? "" : " (not saved: ", saved ? "" : (why + "; the next start learns again)").c_str());
|
||||
}
|
||||
}
|
||||
// --draft-costs with a table from before the stored PCIe share: once PcieAuto has settled (three readings, or it gave up), its
|
||||
// share goes into the file beside the table and is fixed from the next request; the next start loads it
|
||||
if (dc_pcie_pending && !pcie_auto.on) {
|
||||
dc_pcie_pending = false;
|
||||
strata::spec::CostFile cf = dc_file;
|
||||
cf.key = dc_key;
|
||||
cf.pcie_frac = o.pcie_frac;
|
||||
std::string why;
|
||||
const bool saved = strata::spec::write_cost_file(o.draft_costs, cf, why);
|
||||
fix_pcie_share(cf.pcie_frac);
|
||||
std::fprintf(stderr, "strata serve: draft costs: the PCIe share settled at %.2f; it is %s and fixed from the next request on\n",
|
||||
cf.pcie_frac, saved ? "stored with this machine's table" : ("not stored (" + why + ")").c_str());
|
||||
}
|
||||
// the last commit (set_commit_async): the session is complete before anything reads or copies it
|
||||
if (!ver.wait_commit(err)) {
|
||||
exit_if_gpu_fatal(err);
|
||||
|
||||
@@ -210,6 +210,13 @@ bool read_cost_file(const std::string& path, const std::string& key, CostFile& o
|
||||
out.cpu_share = s;
|
||||
continue;
|
||||
}
|
||||
if (line.rfind("pcie ", 0) == 0) {
|
||||
char* e = nullptr;
|
||||
const double s = std::strtod(line.c_str() + 5, &e);
|
||||
if (e == line.c_str() + 5 || *e != '\0' || !(s >= 0.0) || !(s <= 1.0)) { why = "a malformed PCIe share"; return false; }
|
||||
out.pcie_frac = s;
|
||||
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";
|
||||
@@ -261,6 +268,10 @@ bool write_cost_file(const std::string& path, const CostFile& c, std::string& wh
|
||||
std::snprintf(b, sizeof b, "share %.3f\n", std::min(c.cpu_share, 1.0));
|
||||
f << b;
|
||||
}
|
||||
if (c.pcie_frac >= 0.0) {
|
||||
std::snprintf(b, sizeof b, "pcie %.3f\n", std::min(c.pcie_frac, 1.0));
|
||||
f << b;
|
||||
}
|
||||
f << "end\n";
|
||||
f.flush();
|
||||
if (!f) { why = "could not write the file"; fs::remove(tmp, ec); return false; }
|
||||
|
||||
@@ -310,6 +310,19 @@ int main() {
|
||||
"file: the CPU prompt share is stored and read back with the table");
|
||||
{ std::ofstream f(path, std::ios::binary | std::ios::trunc); f << "strata-draft-costs 1\nkey x\nbucket 0 15.1,18\nshare 1.7\nend\n"; }
|
||||
check(!strata::spec::read_cost_file(path, "x", r, why), "file: a share outside 0..1 is not loaded");
|
||||
// the decode PCIe share PcieAuto settled on (repeatable output): stored and read back; a file written before it has none, and
|
||||
// still loads (the engine then refines the share as before and writes it)
|
||||
{ std::ofstream f(path, std::ios::binary | std::ios::trunc); f << "strata-draft-costs 1\nkey x\nbucket 0 15.1,18\nshare 0.900\nend\n"; }
|
||||
check(strata::spec::read_cost_file(path, "x", r, why) && r.cpu_share == 0.9 && r.pcie_frac < 0.0,
|
||||
"file: a file from before the PCIe share loads, with no PCIe share");
|
||||
c.pcie_frac = 0.15;
|
||||
check(strata::spec::write_cost_file(path, c, why) && strata::spec::read_cost_file(path, c.key, r, why) && r.pcie_frac == 0.15 &&
|
||||
r.cpu_share == 0.45 && r.tables[0].size() == 4,
|
||||
"file: the PCIe share is stored and read back with the table and the prompt share");
|
||||
{ std::ofstream f(path, std::ios::binary | std::ios::trunc); f << "strata-draft-costs 1\nkey x\nbucket 0 15.1,18\npcie -0.2\nend\n"; }
|
||||
check(!strata::spec::read_cost_file(path, "x", r, why), "file: a PCIe share outside 0..1 is not loaded");
|
||||
{ std::ofstream f(path, std::ios::binary | std::ios::trunc); f << "strata-draft-costs 1\nkey x\nbucket 0 15.1,18\npcie 0.15x\nend\n"; }
|
||||
check(!strata::spec::read_cost_file(path, "x", r, why), "file: a malformed PCIe share is not loaded");
|
||||
fs::remove_all(dir, ec);
|
||||
}
|
||||
std::printf(g_fail ? "FAIL\n" : "PASS\n");
|
||||
|
||||
Reference in new issue
Block a user