Files
Strata/include/strata/program/batch_groups.hpp
T
SuperDistance a3bb38ec45 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.
2026-10-26 19:03:48 +01:00

105 lines
5.8 KiB
C++

// include/strata/program/batch_groups.hpp - how many pipelined slot groups `--batch` runs with.
//
// `--batch-groups N` pipelines N groups of slots through the stages of a layer split; `--batch-groups auto` picks the
// most groups (at most one per stage) that divide the slots. Since 0.1.41 a layer split with `--batch` >= 2 and no
// `--batch-groups` at all means `auto`. The pipelined path does not run `--batch-mtp`'s drafts or the batch
// adaptive tier (`--adapt-async 1` between batch windows). `--adapt-async 1` with the number of groups NOT given still
// makes `auto` resolve to one group (and says why). `--batch-mtp` does not any more (0.1.43.1): on 2x A4000 and 2x RTX
// 3090 four concurrent streams lost 25-30% with the serial MTP windows against the pipelined default, so the default
// pipelines and `--batch-mtp` is left off there (said at start); STRATA_BATCH_MTP_SERIAL=1 or `--batch-groups 1` keeps
// the serial windows with the slots' drafts (the 0.1.42/0.1.43 behaviour). A number or `auto` given on the command line
// is honoured as before.
//
// The pipelined path starts no round of the adaptive VRAM tier on its own (the serial batch windows start one every
// `--adapt-every` windows): opt-in, STRATA_PIPE_ADAPT_EVERY=N lets it run one after every N group windows, once the groups
// in flight have finished (pipe_adapt_every / pipe_adapt_due below).
//
// Pure (no GPU, no environment read): generate.cpp passes in what it knows.
#pragma once
#include <cstdlib>
namespace strata::program::batch_groups {
/// STRATA_PIPE_ADAPT_EVERY's value -> the interval in group windows; 0 (off: the pipelined path adapts nothing, as before)
/// when unset, empty, not a number, 0 or negative.
inline int pipe_adapt_every(const char* value) {
if (value == nullptr) return 0;
const int n = std::atoi(value);
return n > 0 ? n : 0;
}
/// What the pipelined loop knows when it decides on a round.
struct PipeAdapt {
int every = 0; ///< pipe_adapt_every()
bool tier_on = false; ///< the adaptive tier counts the routing here (it is on and not the whole model)
bool async_tier = false; ///< --adapt-async 1 runs its own rounds
bool prompt_read = false; ///< a prompt is read beside the slots (it may hold a loan of cache slots)
bool swaps_pending = false; ///< the last round's copies have not landed yet
long long windows = 0; ///< group windows completed so far
long long last = 0; ///< `windows` when the last round ran
};
/// A round is due: the loop starts no new group, and runs the round once nothing is in flight.
inline bool pipe_adapt_due(const PipeAdapt& p) {
return p.every > 0 && p.tier_on && !p.async_tier && !p.prompt_read && !p.swaps_pending && p.windows - p.last >= p.every;
}
/// What kept `auto` from pipelining.
enum class Serial { None, BatchMtp, AdaptAsync, TwoCardsSerialFaster };
struct Input {
bool set = false; ///< --batch-groups was given (a number or `auto`)
bool auto_given = false; ///< ... as `auto`
int given = 1; ///< the number given (1 when none)
int batch = 0; ///< the slots
int later_stages = 0; ///< the layer split's stages after CUDA0 (0: no layer split)
bool batch_mtp = false; ///< --batch-mtp is on (it passed the gates that do not concern the groups)
bool adapt_async = false; ///< --adapt-async 1 was asked for
bool mtp_serial = false; ///< STRATA_BATCH_MTP_SERIAL=1: --batch-mtp with no --batch-groups runs one serial group (0.1.43)
bool nv_serial_faster = false; ///< PROPOSED (0.2.1, owner decides): NVIDIA build, STRATA_BATCH_GROUPS_NV_SERIAL not 0 - two cards run faster in serial windows (z440 2x A4000, aggregate tok/s: +16% at 2 slots, +12% at 4, +15% at 8)
};
struct Result {
int groups = 1;
bool from_auto = false; ///< the number came from `auto` (given or the 0.1.41 default)
Serial serial = Serial::None; ///< `auto` would have pipelined, but this feature needs the serial windows
int would_be = 1; ///< what `auto` would have picked without it (== groups unless `serial`)
bool mtp_dropped = false; ///< --batch-mtp met the default pipeline: the pipeline wins, --batch-mtp is off
};
/// The most groups, at most one per stage of the layer split (`later_stages` + 1 GPUs), that divide `batch`.
inline int auto_groups(int later_stages, int batch) {
int best = 1;
if (later_stages <= 0) return best;
for (int d = 2; d <= later_stages + 1 && d <= batch; ++d)
if (batch % d == 0) best = d;
return best;
}
inline Result resolve(const Input& in) {
Result r;
r.groups = in.given;
r.would_be = in.given;
const bool defaulted = !in.set && in.later_stages > 0 && in.batch > 1; // the 0.1.41 default
if (!in.auto_given && !defaulted) return r;
r.from_auto = true;
r.would_be = auto_groups(in.later_stages, in.batch);
r.groups = r.would_be;
if (!in.set && r.would_be > 1) { // only the default gives way; `--batch-groups auto` asked for the pipeline
if (in.batch_mtp && in.mtp_serial) r.serial = Serial::BatchMtp;
else if (in.nv_serial_faster && in.later_stages == 1 && !in.batch_mtp && !in.adapt_async) r.serial = Serial::TwoCardsSerialFaster;
else if (in.adapt_async) r.serial = Serial::AdaptAsync;
r.mtp_dropped = in.batch_mtp && r.serial == Serial::None;
if (r.serial != Serial::None) r.groups = 1;
}
return r;
}
inline const char* serial_what(Serial s) {
return s == Serial::BatchMtp ? "--batch-mtp" : s == Serial::AdaptAsync ? "--adapt-async 1"
: s == Serial::TwoCardsSerialFaster ? "serial windows (measured faster than pipelined groups on two NVIDIA cards; STRATA_BATCH_GROUPS_NV_SERIAL=0 keeps the pipeline)" : "";
}
} // namespace strata::program::batch_groups