Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

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

noon-at-cgn495dc43690c6
diff --git a/docs/BATCHING.md b/docs/BATCHING.md
index 8779937d3..4217c60b2 100644
--- a/docs/BATCHING.md
+++ b/docs/BATCHING.md
@@ -144,14 +144,22 @@ about 10-25% speed per request on this card". `--parallel N` is honoured as aske
Batch windows count which experts they route to, and every `--adapt-every` windows (default 4) the engine swaps the
most-routed experts that are not in VRAM into the cache in place of the least-routed resident ones, the same rule the
solo path uses (`--adapt-swaps` and `--adapt-decay` apply too; on a layer split every GPU's cache adapts). It runs when the
-tier is not the whole model and both `--adapt-every` and `--adapt-swaps` are above 0. The copies run beside the
-window's commit and drafts and have landed before the next window starts. It pauses while a prompt holds a loan of cache
-slots (a long prompt read beside decoding slots). The `strata batch:` log line shows the rounds and swaps, each stage's
+tier is not the whole model and both `--adapt-every` and `--adapt-swaps` are above 0. By default the round runs beside the
+window's commit and drafts and has landed before the next window starts. It stands still while a long prompt is read
+between decoding windows (the prompt may hold a loan of cache slots). The `strata batch:` log line shows the rounds and swaps, each stage's
GPU-reach wait and pool time, and how many routed entries the VRAM tier, the PCIe share and the CPU served per window.
Swaps move experts between the GPU and the CPU, which round differently, so greedy outputs can differ from run to run;
`--adapt-every 1000000` keeps the tier fixed (as in the exactness settings below). The pipelined `--batch-groups` path
does not adapt.
+With `--adapt-async 1` (needs the resident RAM mode) the round does not hold a window up: each batch window moves the
+round on one step (table changes and uploads on the main thread, the copies on a helper thread and the cards' refill
+streams), as on the solo path. A request that arrives finishes the round in flight before it starts, and a prompt read
+between decoding windows does not move it. `--adapt-min-gain F` (default 1.5) raises the bar a swap must clear (the
+routing count of the expert coming in against the one going out), so fewer experts move per round; both tiers use it.
+The log line's `adapt wait` is the time the main thread spent on the tier per window (the blocking tier: waiting for
+its round).
+
## Exactness
A batch row's arithmetic is the single-token window's, so with greedy decoding **every conversation of a batch
diff --git a/docs/DETAILS.md b/docs/DETAILS.md
index 9547e1126..41e2dc6ed 100644
--- a/docs/DETAILS.md
+++ b/docs/DETAILS.md
@@ -167,8 +167,9 @@ other ~18 GB), a 32 GB PC with a 12-16 GB GPU the Coder; IQ3_XXS on a 32 GB PC s
- The cache still follows the conversation (`--adapt-every`): a swap copies the evicted expert back from VRAM into the
RAM place of the one that replaces it, so the RAM copy keeps holding exactly what the GPU does not.
- `--adapt-async 1` (opt-in, `--serve`): the swaps of a round advance between decode windows on a helper thread
- (copy back, copy in, move into RAM) instead of one window waiting for the whole round. Not with `--batch` or
- `--peer-device` (the blocking tier runs there). With `--pipeline-windows 2` (two windows in flight, see
+ (copy back, copy in, move into RAM) instead of one window waiting for the whole round. With `--batch` slots it
+ advances between their windows too (docs/BATCHING.md); not with `--batch-groups` or `--peer-device` (the blocking
+ tier runs there). With `--pipeline-windows 2` (two windows in flight, see
[MULTI_GPU.md](MULTI_GPU.md)) the copies into the evicted slots and the moves into RAM also wait until the windows
that were in flight when they were decided have completed (`STRATA_PIPELINE_ADAPT_ASYNC=0`: the blocking tier
there). It is not bit-exact from run to run: which window first computes a swapped-in expert on the GPU (which
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index 4c4b492d5..7b89e141e 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -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) {