batch-groups: opt-in adaptive-tier rounds in the pipelined loop, STRATA_PIPE_ADAPT_EVERY (#4010)

With --batch-groups above 1 on a layer split the pipelined batch loop never starts a round of the adaptive VRAM tier (the
serial batch windows start one every --adapt-every windows). While the slots are busy the tier only moves when they go back
to the solo path, so a fresh engine keeps its start-up profile for minutes.

STRATA_PIPE_ADAPT_EVERY=N (opt-in; unset, 0 or not a number is off): after N group windows pump starts no new group on the
first stage, lets the groups in flight finish, then runs one round as batch_step does between serial windows, adapt() and
apply_pending(true), so nothing in flight reads a cache slot while experts swap and the new experts have landed before the
next launch. Not while a prompt is read beside the slots (in_prompt_read, its loan of cache slots), not with --adapt-async,
not while the last round's copies are in flight, not with the tier off. The rule is two pure functions in
include/strata/program/batch_groups.hpp (pipe_adapt_every, pipe_adapt_due) with 12 new cases in batch_groups_test. With the
switch set the engine says so once at start and the "strata batch (pipelined ...)" line adds the rounds, the experts
swapped in and the time spent. docs/BATCHING.md documents the switch with the author's table (2x RTX 5090, six streams,
309 to 410 rows/s over eight bursts after a fresh start, two engines per setting).

Reviewed: unset costs one counter increment per group window and one false check where a group starts, and prints no new
line. A due round cannot wait forever: no new group starts, the groups in flight finish, then the round runs. A prompt read
sets in_prompt_read before the reader thread starts (including STRATA_PIPED_CONCURRENT) and clears it after the reader
joined; the drain after the read runs the round if it is due.

Tests: batch_groups_test (the new cases). Not covered and still the author's to show (the PR is a draft): a soak with
prompts read beside the slots, a Linux run, another GPU pair, more than two stages, and exactness settings with the switch
set. The model gate is written down.
This commit is contained in:
SuperDistance authored and Niko1221 committed 2026-10-26 19:03:48 +01:00
1 parent 2b13a44933
commit a3bb38ec45
4 files changed
+148 -5

No files matched your search

+53 -3
View File
@@ -8223,6 +8223,15 @@ int main(int argc, char** argv) {
: nullptr;
if (why != nullptr) adapt_async_off(why);
}
// STRATA_PIPE_ADAPT_EVERY (batch_groups.hpp; the rounds run in the pipelined loop below): said once here, with the
// other start-up lines, before READY
if (o.serve && o.batch > 0 && o.batch_groups > 1 && multi_gpu) {
const int every = strata::program::batch_groups::pipe_adapt_every(std::getenv("STRATA_PIPE_ADAPT_EVERY"));
if (every > 0)
std::fprintf(stderr, "strata batch: STRATA_PIPE_ADAPT_EVERY=%d: the pipelined groups run an adaptive-tier round "
"every %d group windows (the pipeline empties for it)%s\n", every, every,
o.adapt_every <= 0 || o.adapt_swaps <= 0 ? " - but the adaptive tier is off" : "");
}
// #340 / STRATA_SPLIT_RING: a layer split's streamed-ring override, decided from the stage caches' residency. It
// has to be in place BEFORE the split's lend regions are sized (Prefill::bytes_needed counts the ring): they were
// sized with the default ring and the override applied afterwards, so a ring of 384 borrowed 5396 slots and the RAM
@@ -12037,6 +12046,28 @@ int main(int argc, char** argv) {
std::mutex bell_mu;
std::condition_variable bell_cv;
std::atomic<bool> bell_waiting{false};
// STRATA_PIPE_ADAPT_EVERY=N (opt-in; unset or 0: off, as before): the pipelined groups start no adaptive-tier round
// of their own (batch_step starts one every --adapt-every windows; pump below never does), so under steady parallel
// load the VRAM tier only moves when the slots go back to the solo path, and a fresh engine keeps its start-up
// profile for minutes. With N > 0, after N group windows the pipeline starts no new group, lets the ones in flight
// finish, and runs one round there as batch_step runs it between serial windows (nothing in flight reads a cache
// slot; the swapped-in experts land before the next launch), then goes on. Never while a prompt is read beside
// the slots (its loan of cache slots), nor with --adapt-async (its own ticks).
// (the rule: batch_groups.hpp pipe_adapt_every / pipe_adapt_due, tested in batch_groups_test)
const int pipe_adapt_every = strata::program::batch_groups::pipe_adapt_every(std::getenv("STRATA_PIPE_ADAPT_EVERY"));
int64_t pa_windows = 0, pa_last = 0, pa_rounds = 0, pa_swaps = 0;
double pa_ms = 0;
auto pa_due = [&] {
strata::program::batch_groups::PipeAdapt p;
p.every = pipe_adapt_every;
p.tier_on = !drive.d.usage.empty();
p.async_tier = (bool) ajob;
p.prompt_read = in_prompt_read;
p.swaps_pending = !pending.empty();
p.windows = pa_windows;
p.last = pa_last;
return strata::program::batch_groups::pipe_adapt_due(p);
};
// one turn: serve every running stage, hand finished groups on, start what can start
auto pump = [&](bool may_start) -> bool {
++pipe_tick;
@@ -12102,6 +12133,7 @@ int main(int argc, char** argv) {
rows_wait = nullptr;
}
++bt_windows;
++pa_windows;
G.inflight = false;
report_batch_window(G.S, G.active, !pipe_inflight() && !batch_on(), G.started);
}
@@ -12116,7 +12148,7 @@ int main(int argc, char** argv) {
pick = gi;
if (pick < 0) continue;
} else {
if (!may_start) continue;
if (!may_start || pa_due()) continue; // STRATA_PIPE_ADAPT_EVERY: a round is due, the pipeline empties first
for (int j = 0; j < (int) pg.size() && pick < 0; ++j) {
const int gi = (int) ((rr + j) % (int64_t) pg.size());
if (!pg[(size_t) gi].inflight && group_active(gi)) pick = gi;
@@ -12165,6 +12197,18 @@ int main(int argc, char** argv) {
}
}
if (drive.d.failed) { std::printf("ERR %s\n", drive.d.fail ? drive.d.fail : "the expert pool failed"); return false; }
if (pa_due() && !pipe_inflight()) { // STRATA_PIPE_ADAPT_EVERY: the pipeline is empty - one round, as batch_step
const Clock::time_point ta = Clock::now();
pa_last = pa_windows;
if (!adapt()) {
std::printf("ERR an adaptive refill failed\n");
return false;
}
++pa_rounds;
pa_swaps += (int64_t) pending.size();
apply_pending(true); // the copies land and the tables follow (the next launch plans from them)
pa_ms += std::chrono::duration<double, std::milli>(Clock::now() - ta).count();
}
if (batch_telemetry_enabled) {
const auto sample = stage_in_flight.sample(Clock::now(), !pipe_inflight() && !batch_on());
if (sample) {
@@ -12176,9 +12220,15 @@ int main(int argc, char** argv) {
}
if (!pipe_inflight() && !batch_on() && bt_windows > 0) { // all idle: the timing line, as batch_step
const double w = (double) bt_windows, wall = std::chrono::duration<double, std::milli>(Clock::now() - bt_start).count();
char pa_txt[160] = "";
if (pipe_adapt_every > 0)
std::snprintf(pa_txt, sizeof pa_txt, "; adaptive tier (STRATA_PIPE_ADAPT_EVERY): %lld rounds, %lld experts "
"swapped in, %.0f ms", (long long) pa_rounds, (long long) pa_swaps, pa_ms);
std::fprintf(stderr, "strata batch (pipelined, %d groups of %d): %lld group-steps, %lld rows in %.0f ms = "
"%.1f rows/s (admissions included)\n", o.batch_groups, GS, (long long) bt_windows,
(long long) bt_rows, wall, 1000.0 * bt_rows / std::max(wall, 1e-9));
"%.1f rows/s (admissions included)%s\n", o.batch_groups, GS, (long long) bt_windows,
(long long) bt_rows, wall, 1000.0 * bt_rows / std::max(wall, 1e-9), pa_txt);
pa_rounds = pa_swaps = 0;
pa_ms = 0;
for (int k = 0; k < n_pipe; ++k) {
const std::string pr = stage_verifier(k).profile_report();
if (!pr.empty()) std::fprintf(stderr, "strata batch GPU stages, stage %d (ms/window):%s\n", k + 1, pr.c_str());