batch: tick the asynchronous adaptive tier (--adapt-async 1) between batch windows; --adapt-min-gain

batch_step advances the async round one step per window (never between a prompt's chunks), commands drain the round in
flight before they run, a round with nothing to swap ends at its first step, and --adapt-min-gain sets the bar a swap
must clear (default 1.5, as before) for both tiers. --adapt-async is no longer refused with --batch slots (still with
--batch-groups).
This commit is contained in:
noon-at-cgn authored and Niko1221 committed 2026-10-09 15:16:22 +02:00
1 parent e989c67194
commit 495dc43690
3 files changed
+65 -19

No files matched your search

+51 -14
View File
@@ -573,6 +573,9 @@ struct Options {
/// the VRAM tier in place of the least-routed resident ones (decayed counts). 0 = static residency.
int adapt_every = 4;
float adapt_decay = 0.7f; ///< the usage counts are multiplied by this after each adaptation (--adapt-decay)
/// an expert is swapped into the VRAM tier only when its decayed routing count beats the one it replaces by more than
/// this (--adapt-min-gain; both the blocking and the asynchronous tier)
float adapt_min_gain = 1.5f;
/// --adapt-async 1 (--serve with the resident RAM mode; opt-in): the adaptive tier's rounds advance between verify
/// windows on a helper thread instead of one window waiting for a whole round (see the tier in the --serve block).
/// Not bit-exact run to run. 0 = the blocking tier.
@@ -905,6 +908,8 @@ void usage() {
" --resident-experts the low-RAM PC's resident mode (setup): --mmap-experts --resident-cpu-experts\n"
" with the copy page-locked when possible, 4 GiB headroom, plain mmap if it\n"
" does not fit. Same answers as --mmap-experts for the same placement.\n"
" --adapt-min-gain F the adaptive tier swaps an expert in only when its decayed routing count beats\n"
" the one it replaces by more than F (default 1.5; higher = fewer, surer swaps)\n"
" --adapt-async 1 --serve with the resident RAM mode (opt-in): the adaptive tier's swaps\n"
" advance between verify windows instead of a window waiting for a whole\n"
" round. Not bit-exact run to run. 0 (default) = the blocking tier.\n");
@@ -1899,6 +1904,7 @@ int main(int argc, char** argv) {
else if (a == "--pcie-frac") o.pcie_frac = std::atof(next("--pcie-frac"));
else if (a == "--adapt-every") o.adapt_every = std::atoi(next("--adapt-every"));
else if (a == "--adapt-decay") o.adapt_decay = (float) std::atof(next("--adapt-decay"));
else if (a == "--adapt-min-gain") o.adapt_min_gain = (float) std::atof(next("--adapt-min-gain"));
else if (a == "--adapt-async") o.adapt_async = std::atoi(next("--adapt-async")) != 0 ? 1 : 0;
else if (a == "--spec-min-p") o.spec_min_p = std::atof(next("--spec-min-p"));
else if (a == "--stop-eos") o.stop_eos = true;
@@ -6365,11 +6371,11 @@ int main(int argc, char** argv) {
// share-of-pinned figure above (which sizes the prompt path) is left as the mmap mode's for the same reason.
// --adapt-async (opt-in): where the asynchronous adaptive tier does not apply, the blocking tier runs, said once.
// It exchanges experts between the GPU caches and the resident RAM copy only (the copy is checked once it is
// built, below). Not beside the --batch slots (their windows run between a request's prompt chunks), a peer
// tier or the helper GPUs' tier, which pick their swaps from the same table. The pipelined decode loop
// (--pipeline-windows 2) has windows in flight at every tick: there the steps that overwrite something a window
// may still read wait for those windows (see adapt_tick); STRATA_PIPELINE_ADAPT_ASYNC=0 keeps the blocking tier
// beside it instead.
// built, below). Beside the --batch slots it advances between their windows, never between a prompt's chunks;
// not with --batch-groups (pipelined slot groups), a peer tier or the helper GPUs' tier, which pick their swaps
// from the same table. The pipelined decode loop (--pipeline-windows 2) has windows in flight at every tick:
// there the steps that overwrite something a window may still read wait for those windows (see adapt_tick);
// STRATA_PIPELINE_ADAPT_ASYNC=0 keeps the blocking tier beside it instead.
auto adapt_async_off = [&](const char* why) {
std::fprintf(stderr, "strata generate: --adapt-async 1 is off (%s): the blocking adaptive tier\n", why);
o.adapt_async = 0;
@@ -6382,7 +6388,7 @@ int main(int argc, char** argv) {
const char* why = !o.serve ? "it needs --serve"
: o.adapt_every <= 0 || o.adapt_swaps <= 0 ? "the adaptive tier is off"
: !o.resident_cpu_experts ? "it needs the resident RAM mode, --resident-experts"
: o.batch > 0 ? "not with --batch slots"
: o.batch > 0 && o.batch_groups > 1 ? "not with --batch-groups (the pipelined slot groups)"
: peer.valid() ? "not with --peer-device"
: remote_opt ? "not with --remote-expert-opt"
: o.pipeline_windows >= 2 && !pl_async ? "not with --pipeline-windows 2 (STRATA_PIPELINE_ADAPT_ASYNC=0)"
@@ -7987,7 +7993,7 @@ int main(int argc, char** argv) {
std::partial_sort(vict.begin(), vict.begin() + (ptrdiff_t) nc, vict.end(),
[](auto& a, auto& b) { return a.first < b.first; });
for (size_t i = 0; i < nc; ++i) {
if (cand[i].first < vict[i].first + 1.5f) break;
if (cand[i].first < vict[i].first + o.adapt_min_gain) break;
swaps.push_back({cand[i].first - vict[i].first, (int32_t) l, cand[i].second, vict[i].second});
}
}
@@ -8186,7 +8192,7 @@ int main(int argc, char** argv) {
std::partial_sort(vict.begin(), vict.begin() + (ptrdiff_t) nc, vict.end(),
[](auto& a, auto& b) { return a.first < b.first; });
for (size_t i = 0; i < nc; ++i) {
if (cand[i].first < vict[i].first + 1.5f) break;
if (cand[i].first < vict[i].first + o.adapt_min_gain) break;
all.push_back({cand[i].first - vict[i].first, (int32_t) l, cand[i].second, vict[i].second});
}
}
@@ -8361,6 +8367,11 @@ int main(int argc, char** argv) {
aswaps[k++] = w;
}
aswaps.resize(k);
if (aswaps.empty()) { // nothing to move (converged, or the table moved since the snapshot): the round ends here
a_ms += std::chrono::duration<double, std::milli>(Clock::now() - a_t0).count();
astate = AState::Idle;
continue;
}
for (AHome& h : ahomes) h.used = false;
if (pl) { // the copies in at each card's next gap: after this step, so fenced (see above)
a_res_dirty = true;
@@ -8825,6 +8836,8 @@ int main(int argc, char** argv) {
// since the last timing line, its rounds, the experts it swapped in and the ms a window waited for it
int64_t ba_window = 0, bt_adapt_rounds = 0, bt_adapt_swaps = 0;
double bt_adapt_wait = 0;
bool in_prompt_read = false; // batch_step runs between a prompt's chunks: the adaptive tier stands still then
int64_t bt_a_rounds0 = 0, bt_a_swapped0 = 0; // the asynchronous tier's counters when the timed windows began
Clock::time_point bt_start = Clock::now();
int admit_slot = -1; ///< the slot the request being read will continue in (BGEN)
long long admit_max_new = 0;
@@ -8961,6 +8974,18 @@ int main(int argc, char** argv) {
drive.d.experts = 0;
drive.d.failed = false;
apply_pending(false);
// --adapt-async: the round in flight moves on a step when its job and copies are done (or a due round starts).
// Never between a prompt's chunks (its loan of cache slots and its chunks read the tables); a request drains
// the round before its prompt starts (adapt_tick(true) where the request begins). The main thread's part is
// small (the table changes and uploads); the copies are on the helper thread and the cards' refill streams.
if (ajob && !in_prompt_read) {
const Clock::time_point ta = Clock::now();
if (!adapt_tick(false)) {
std::printf("ERR an adaptive refill failed\n");
return false;
}
bt_adapt_wait += std::chrono::duration<double, std::milli>(Clock::now() - ta).count();
}
if (bt_windows == 0) {
bt_start = Clock::now();
bt_wait0 = ver.ms_wait; bt_pool0 = ver.ms_pool;
@@ -8972,6 +8997,7 @@ int main(int argc, char** argv) {
bt_stage_pool0[k] = stage_verifier((int) k).ms_pool;
}
bt_cpu_ent0 = drive.d.multi_entries; bt_off_ent0 = drive.d.offload_entries;
bt_a_rounds0 = a_rounds; bt_a_swapped0 = a_swapped;
}
const Clock::time_point w0 = Clock::now();
if (!ver.run_slot_rows(rows, S, tok, pos, win_pool_fn, win_pool_user, outb, err) || drive.d.failed) {
@@ -8983,15 +9009,14 @@ int main(int argc, char** argv) {
// the drafts. Every stage's window has finished here (run_slot_rows synchronised each stage's stream), and
// nothing in flight reads the cache slots a swap overwrites; the swapped-in experts are landed (apply_pending)
// before this window ends, so no other path (a prompt chunk read between windows, a loan of cache slots, a KV
// growth) ever meets swaps in flight. Not while a prompt holds a loan of cache slots: its lent experts are
// marked missing and are put back when the loan ends.
// growth) ever meets swaps in flight. Not between a prompt's chunks: a loan of cache slots marks its lent
// experts missing, and they are put back when the loan ends. (With --adapt-async the tier is ticked above.)
struct AdaptRun {
std::thread th;
bool ok = true;
~AdaptRun() { if (th.joinable()) th.join(); } // an error return below still joins it
} adapt_run;
if (!drive.d.usage.empty() && !ajob && (++ba_window % o.adapt_every) == 0 && pending.empty() &&
std::none_of(pf_parts.begin(), pf_parts.end(), [](const PfPart& p) { return !p.lent.empty(); }))
if (!drive.d.usage.empty() && !ajob && !in_prompt_read && (++ba_window % o.adapt_every) == 0 && pending.empty())
adapt_run.th = std::thread([&] { adapt_run.ok = adapt(); });
// Accept the proposal only when the target picked it and there is room to emit both tokens.
std::vector<int> keep(bs.size(), 0);
@@ -9084,7 +9109,11 @@ int main(int argc, char** argv) {
per_stage += sb;
}
char adapt_txt[160] = "";
if (!drive.d.usage.empty())
if (!drive.d.usage.empty() && ajob)
std::snprintf(adapt_txt, sizeof adapt_txt,
"; adaptive tier (async): %lld rounds started, %lld experts swapped in",
(long long) (a_rounds - bt_a_rounds0), (long long) (a_swapped - bt_a_swapped0));
else if (!drive.d.usage.empty())
std::snprintf(adapt_txt, sizeof adapt_txt, "; adaptive tier: %lld rounds, %lld experts swapped in",
(long long) bt_adapt_rounds, (long long) bt_adapt_swaps);
std::fprintf(stderr, "strata batch: %lld windows, avg %.2f rows, %.2f ms/window = run %.2f (CUDA0 GPU-reach "
@@ -9272,6 +9301,12 @@ int main(int argc, char** argv) {
std::fflush(stdout);
continue;
}
// a command may touch the expert cache (a request lends slots, a K/V growth moves them, a session restore
// quiesces it): a round of the asynchronous tier that the batch windows left in flight lands first
if (!adapt_tick(true)) {
std::printf("ERR an adaptive refill failed\n");
return 1;
}
admit_slot = -1;
if (line.rfind("BGEN ", 0) == 0 || line.rfind("BGENI ", 0) == 0) {
const bool bimg = line.rfind("BGENI ", 0) == 0;
@@ -10404,9 +10439,11 @@ int main(int argc, char** argv) {
if (decode_share <= 0.0 || !batch_on()) continue;
const double budget = decode_share * std::chrono::duration<double, std::milli>(Clock::now() - tq).count();
const auto td = Clock::now();
in_prompt_read = true;
do {
if (!batch_step()) { batch_fatal = true; e = "a batch window failed"; return false; }
if (!batch_step()) { in_prompt_read = false; batch_fatal = true; e = "a batch window failed"; return false; }
} while (batch_on() && std::chrono::duration<double, std::milli>(Clock::now() - td).count() < budget);
in_prompt_read = false;
++il_parts;
il_ms += std::chrono::duration<double, std::milli>(Clock::now() - td).count();
if (trace) {