Opt-in: keep what the expert cache learned across restarts (--expert-profile-save, #477)
`--expert-profile-save P` (--serve) writes the adaptive tier's learning as a profile in tools/make_profile.py's format, byte for byte (read_expert_profile reads it back): every pair ranked - the experts resident in VRAM now first, then the routing counted since the start (the decayed usage summed at each adapt step, before its decay), then the profile the engine started from. On QUIT / stdin EOF and every --expert-profile-save-every minutes (default 10, 0 = exit only) between requests, through <P>.tmp and a rename. Without the flag nothing is counted or written: `heat` stays empty and every added loop is over it. Server: "expert_profile_save" (and "expert_profile_save_every") in the config pass the flags, and the next start begins from the saved file instead of the config's --expert-profile when its header matches that profile's layers x experts. tests/core/expert_profile_save_test.cpp (CPU only) checks the order and the bytes.
This commit is contained in:
1 parent
4ba7c94d4d
commit
31da8f5dca
8 files changed
+324
-1
No files matched your search
@@ -303,6 +303,11 @@ struct Options {
|
||||
/// trace is what the plan's `h = 0.6447` refers to, and compulsory-miss measured 0.4864 because it fills
|
||||
/// with whatever the prompt touched FIRST. Empty means no profile.
|
||||
std::string expert_profile;
|
||||
/// #477 (--serve, opt-in): where to save what the adaptive tier learned, as a profile `--expert-profile` reads
|
||||
/// (the resident experts first, then the routing counted since the start); on QUIT and every
|
||||
/// `expert_profile_save_min` minutes between requests. Empty (the default): nothing is counted or written.
|
||||
std::string expert_profile_save;
|
||||
double expert_profile_save_min = 10.0;
|
||||
/// R4.2d: **ON by default**, because the measurement is unambiguous and the alternative is known-broken.
|
||||
/// Without it, 17 of 10,562 layers had the hit work done when the pool returned; with it, 9,190. The
|
||||
/// A/B arm is `--no-hit-poke`.
|
||||
@@ -530,6 +535,10 @@ void usage() {
|
||||
" real graph that --stage-timing cannot give. Prints and exits.\n"
|
||||
" --expert-profile P R4.2e: pre-load the VRAM tier from a `profile.bin` (see\n"
|
||||
" tools/make_profile.py) instead of admitting on first use.\n"
|
||||
" --expert-profile-save P --serve, #477: save what the adaptive tier learned (the experts in\n"
|
||||
" VRAM, then the routing counted since the start) as a profile at P, on\n"
|
||||
" QUIT and every --expert-profile-save-every MIN minutes (default 10;\n"
|
||||
" 0 = on QUIT only) between requests; start from it with --expert-profile P\n"
|
||||
" --no-hit-poke R4.2d's A/B arm. The hit path pokes the driver once right after its\n"
|
||||
" launch so the GPU starts while the CPU pool runs; without it the work\n"
|
||||
" waits for the next driver entry and does not overlap at all.\n"
|
||||
@@ -1210,6 +1219,9 @@ int main(int argc, char** argv) {
|
||||
else if (a == "--expert-cache-per-layer") o.expert_cache_per_layer = true;
|
||||
else if (a == "--no-hit-poke") o.no_hit_poke = true;
|
||||
else if (a == "--expert-profile") o.expert_profile = next("--expert-profile");
|
||||
else if (a == "--expert-profile-save") o.expert_profile_save = next("--expert-profile-save");
|
||||
else if (a == "--expert-profile-save-every")
|
||||
o.expert_profile_save_min = std::atof(next("--expert-profile-save-every"));
|
||||
else if (a == "--gpu-stages") o.gpu_stages = true;
|
||||
else if (a == "--mmap-experts") o.mmap_experts = true;
|
||||
else if (a == "--shared-expert-arena") o.shared_expert_arena = next("--shared-expert-arena");
|
||||
@@ -2258,6 +2270,10 @@ int main(int argc, char** argv) {
|
||||
std::fprintf(stderr, "strata generate: profile %s: %zu ranked pairs, built for %lld slots\n",
|
||||
o.expert_profile.c_str(), profile.size(), (long long) pslots);
|
||||
}
|
||||
// #477: the whole ranking as loaded, the prior of --expert-profile-save's order (a layer split keeps only
|
||||
// CUDA0's pairs in `profile` below). Empty without --expert-profile-save.
|
||||
std::vector<std::pair<int32_t, int32_t>> profile_loaded;
|
||||
if (!o.expert_profile_save.empty()) profile_loaded = profile;
|
||||
// ---- layer split across GPUs: "auto" places the split points by a cost model of one decode window, measured on
|
||||
// the 5080 + 3090 rig (bench/results/2026-09-29-layer-split):
|
||||
// - every layer costs its GPU a time inversely proportional to SMs x clock (0.33 ms on an RTX 5080, 0.50 on a
|
||||
@@ -4490,6 +4506,17 @@ int main(int argc, char** argv) {
|
||||
drive.d.plan = ver.plan_sink();
|
||||
drive.d.pcie_num = std::max(0, std::min(256, (int) (o.pcie_frac * 256.0 + 0.5)));
|
||||
if (o.adapt_every > 0 && o.adapt_swaps > 0) drive.d.usage.assign((size_t) (g.n_layers * g.n_expert), 0.0f);
|
||||
// #477 --expert-profile-save: what the adaptive tier learned, kept across restarts (opt-in; off: `heat` stays
|
||||
// empty and nothing below runs). It needs the adaptive tier's counts and the residency table.
|
||||
std::vector<double> heat;
|
||||
if (!o.expert_profile_save.empty()) {
|
||||
if (drive.d.usage.empty() || host_res.empty())
|
||||
std::fprintf(stderr, "strata serve: --expert-profile-save needs the adaptive tier (--adapt-every and "
|
||||
"--adapt-swaps above 0) and --expert-profile: nothing will be saved\n");
|
||||
else
|
||||
heat.assign(drive.d.usage.size(), 0.0);
|
||||
}
|
||||
Clock::time_point profile_saved_at = Clock::now();
|
||||
cudaStream_t adapt_stream = nullptr;
|
||||
if (cudaStreamCreateWithFlags(&adapt_stream, cudaStreamNonBlocking) != cudaSuccess) {
|
||||
std::fprintf(stderr, "strata serve: cannot create the refill stream\n");
|
||||
@@ -4576,9 +4603,29 @@ int main(int argc, char** argv) {
|
||||
const strata::core::OnDevice on(st->dev);
|
||||
cudaEventRecord(st->adapt_ev, st->adapt_stream);
|
||||
}
|
||||
// #477: the routing counted since the start (each count adds up to 1 / (1 - 0.7) over its decays: the
|
||||
// sum is proportional to the routing itself) - only with --expert-profile-save, else `heat` is empty
|
||||
for (size_t i = 0; i < heat.size(); ++i) heat[i] += (double) drive.d.usage[i];
|
||||
for (float& v : drive.d.usage) v *= 0.7f;
|
||||
return true;
|
||||
};
|
||||
// #477: write the learned profile (between requests and at QUIT: a prompt's lent slots are back by then).
|
||||
// A swap still in flight counts as done - its expert is resident once the copy lands. `why`: for the log.
|
||||
auto save_profile = [&](const char* why) {
|
||||
if (heat.empty()) return;
|
||||
std::vector<uint8_t> resident(host_res.size(), 0);
|
||||
for (size_t i = 0; i < host_res.size(); ++i) resident[i] = host_res[i] >= 0;
|
||||
for (const auto& p : pending) resident[(size_t) p.first] = 1;
|
||||
std::string e;
|
||||
const auto ranked = strata::core::rank_learned_profile(g.n_layers, g.n_expert, resident, heat,
|
||||
profile_loaded);
|
||||
if (strata::core::write_expert_profile(o.expert_profile_save, g.n_layers, g.n_expert, ranked, e))
|
||||
std::fprintf(stderr, "strata serve: expert profile saved to %s (%s)\n", o.expert_profile_save.c_str(),
|
||||
why);
|
||||
else
|
||||
std::fprintf(stderr, "strata serve: the expert profile was not saved: %s\n", e.c_str());
|
||||
profile_saved_at = Clock::now();
|
||||
};
|
||||
// stdin is read on its own thread, so a STOP line reaches a request that is still running (the client went
|
||||
// away, or pressed Esc): the flag is checked between prompt chunks and between verify windows.
|
||||
std::atomic<bool> stop_req{false};
|
||||
@@ -4741,6 +4788,10 @@ int main(int argc, char** argv) {
|
||||
std::vector<float> img_rows;
|
||||
std::vector<const float*> row_ptr;
|
||||
while (next_line(line)) {
|
||||
// #477: every --expert-profile-save-every minutes, before the next request (at QUIT: after the loop)
|
||||
if (!heat.empty() && line != "QUIT" && o.expert_profile_save_min > 0 &&
|
||||
Clock::now() - profile_saved_at >= std::chrono::duration<double>(o.expert_profile_save_min * 60.0))
|
||||
save_profile("periodic");
|
||||
if (line == "QUIT") break;
|
||||
// the watchdog watches a request from here until this iteration ends, whichever way it ends
|
||||
struct BusyScope {
|
||||
@@ -5719,6 +5770,7 @@ int main(int argc, char** argv) {
|
||||
remote_experts[(size_t) r].ms_begin() - begin_before[(size_t) r],
|
||||
remote_experts[(size_t) r].ms_wait() - wait_before[(size_t) r]);
|
||||
}
|
||||
save_profile("exit"); // #477: QUIT, or the server closed stdin
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user