batch-groups: decode and give way between the pieces of a prompt read (#2050, opt-in STRATA_PIPED_READ_PIECE), on top of #2326's concurrent read

Squash of SuperDistance's three commits of PR #2050 (decode between the pieces of a read on the pipelined groups, the
piece lines counting the rows the slots decoded meanwhile, the stall watchdog staying on for a read in pieces),
applied after #2326 here: the BYIELD give-way is #2326's give_way lambda, the concurrent block runs first and the
piece path serves a read that STRATA_PIPED_CONCURRENT leaves to it (pictures, configurations where it is off).

Co-authored-by: SuperDistance <43229543+SuperDistance@users.noreply.github.com>
This commit is contained in:
Niko1221andSuperDistance committed 2026-10-14 12:26:10 +02:00
1 parent 073d079d27
commit 5d6bf8d414
4 files changed
+214 -2

No files matched your search

+69 -1
View File
@@ -237,6 +237,74 @@ at its first chunk (`YIELDED 2 2048`), E admitted meanwhile, D going on and A go
which main does not do on this path (D never gives way there); the third run had B off at token 74, a near tie that
flips on main as well. Greedy only; other GPU pairs, three or more stages and sampled decoding not measured.
## A prompt read beside pipelined groups (opt-in)
With a layer split and `--batch-groups` above 1 a prompt is read in one run: every decoding slot stops until it is read,
however long it is, `STRATA_BATCH_DECODE_SHARE` does not apply and no `BYIELD` is taken (`batch_read_piece` returns 0
for the pipelined groups; #1906). One GPU does not have this problem: there the slots decode between the prompt's chunks
for `STRATA_BATCH_DECODE_SHARE` x the chunk's time. These variables give the pipelined groups the same choice:
- `STRATA_PIPED_READ_PIECE=N`: a prompt read on the pipelined groups goes in pieces of N tokens, rounded down to whole
chunks (at least one, at most 32), so a piece reads exactly the chunks a single run would, the two stages stay
overlapped inside it and only the piece's end drains the pipeline. Between two pieces the slot groups decode through
the pipeline for `STRATA_BATCH_DECODE_SHARE` x the piece's time, at most `STRATA_PIPED_DECODE_MAX_MS` and at least
`STRATA_PIPED_DECODE_MIN_MS`, and every group in flight finishes before the next piece, as before any prompt read.
- At a piece's end the pipeline is drained, so a waiting request with a prompt under half as long can take over there
(`BYIELD`, as between the chunks of a read off the pipeline): the part read waits in a slot and the read goes on from
it after the short request is in. The pieces are also taken when no slot decodes, for this reason (off the pipeline
the same read goes in `STRATA_BATCH_YIELD_CHUNKS` pieces then).
- A read that starts within a second (plus the decode budget) of the previous such read's end - prompts arriving back to
back - first gives the slots one decode budget (the same rule, from the previous read's last full piece); otherwise
the slots would stop for both reads.
With the variables unset the read is one run as before. One log line per piece (`strata batch: piped read N tokens in X
ms (to Q of T)`, with `, then the slots decoded R rows in Y ms` when slots decode), per give-way (`the prompt read gives
way at Q of T tokens; slot S holds it`) and per lead-in (`a read right after a read: the slots decoded R rows in Y ms
first`).
**What it trades.** Every piece ends by draining the pipeline, so a long read takes longer - also on an idle server,
since the pieces are what lets a short request in - and the decoding streams get tokens where they had none. It is a
latency choice, not a throughput gain: leave it off where throughput or the long prompt's own TTFT matters most, use it
where a stream that stops for several seconds, or a short question stuck behind a long read, is worse than a long prompt
that waits a little longer.
Measured on 2x RTX 5090 (Swift 1.5 IQ3_S, `--layer-split 25 --trim-stage-weights --batch 6 --batch-groups 2
--resident-experts`, k8v4 KV, 262K context, `--kv-resident 32768`, `STRATA_ARENA_PIN_GIB=24`, Windows 11; a CUDA 13.0
sm_120 build of this change on 0.1.43, "release" = 0.1.43 built the same way; a fresh engine for every run, all from one
window, `tools/piped_read_join_bench.py`). Four streams decode greedy coding answers (thinking off, up to 2048 tokens)
when cold prompts of this repository's own source text join. "Longest stop": the longest time any stream received no
token, from the first prompt's arrival to the last one's first token + 1 s (client-side, SSE); "stream tokens": what the
four streams received in that window; "token rate": the streams' tokens over the run's wall time.
Two cold prompts, 64K then 32K, arriving 6 s apart (`--scenarios two_joiners`; release and 32K pieces: median of 5 runs
each, interleaved; 16K pieces: one run):
| arm | prompts' TTFT | longest stop of a stream | stream tokens | token rate |
| --- | ---: | ---: | ---: | ---: |
| release (one run per prompt) | 6.9 s / 6.1 s | 12.2 s | 289 | 173 tok/s |
| `PIECE=32768 DECODE_MAX_MS=400` | 8.1 s / 8.5 s | 4.2 s | 527 | 166 tok/s |
| `PIECE=16384 DECODE_MAX_MS=400` | 10.5 s / 12.0 s | 2.6 s | 793 | 154 tok/s |
The longest stop was 12.16-12.23 s for the release in all five runs and 4.19-4.22 s for 32K pieces in four; in the fifth
the first 32K piece of the 64K prompt took 9.3 s instead of 3.95 s (one uninterrupted run before any slot decoded),
which made that run's stop 9.5 s. The token rate varied 169-180 tok/s (release) and 136-174 tok/s (32K) between runs.
A short request behind a long read, nothing decoding (#1504's test, `--scenarios followup,read_alone --repeat 3`, median
of 3): a one-line question sent 1 s after a cold 45K prompt, and the same 45K prompt read alone:
| arm | the question's TTFT | the 45K prompt's TTFT | the 45K prompt alone |
| --- | ---: | ---: | ---: |
| release | 5.4 s | 6.0 s | 5.7 s |
| `PIECE=32768 DECODE_MAX_MS=400` | 3.6 s | 7.3 s | 6.4 s |
| `PIECE=16384 DECODE_MAX_MS=400` | 1.8 s | 8.0 s | 7.1 s |
With the variables unset this change ran as the release in the same window (the 45K read alone 5.66 vs 5.69 s, the two
joiners' longest stop 12.2 s on both) and gave the same SHA-256 for six greedy answers under `--prompt-cache 0
--adapt-swaps 0 --pcie-frac 0`. For reference, one RTX 5090 (the same model and flags without the split, 6 slots) with
one cold 64K prompt joining four streams: `STRATA_BATCH_DECODE_SHARE=2.0` 2.4 s longest stop and 37.2 s TTFT, `=0` 14.0
s and 13.8 s. Pieces of one chunk (`PIECE=8192`) cost the long prompt the most. Not measured: other GPU pairs, three or
more stages, HIP and SYCL (built for CUDA only), sampled decoding, prompts with pictures.
## The VRAM expert tier keeps adapting in batch windows
Batch windows count which experts they route to, and every `--adapt-every` windows (default 4) the engine swaps the
@@ -405,7 +473,7 @@ tests the server's side with a scripted engine (no GPU).
| Script | What it checks |
| --- | --- |
| `tools/batch_test.py` | the same prompts alone (`GEN`) and together in the batch slots (`BGEN`): every slot's greedy tokens equal its solo tokens; prints the aggregate rate. `--batch-groups` in `--extra` tests the pipeline, `--keys "temperature=0.7"` the sampled rows. |
| `tools/batch_interleave_test.py` | a long prompt read while two slots decode, a prompt that gives way (`BYIELD`) and goes on, and a next turn continued from its slot: each equal to its solo tokens. |
| `tools/batch_interleave_test.py` | a long prompt read while two slots decode, a prompt that gives way (`BYIELD`) and goes on, and a next turn continued from its slot: each equal to its solo tokens. On a two-stage layer split with pipelined groups add `--batch 4 --batch-groups 2` to `--extra` (its 3 slots do not divide into two groups) and set `STRATA_PIPED_READ_PIECE`; run it without a conversation cache, or step 4 restores the prompt instead of reading it. |
| `tools/parking_test.py` | a follow-up to a conversation decodes the same tokens whether its state stayed live or came back from the parking cache (with a layer split: every stage's image). |
| `tools/early_close_test.py` | a client that stops reading a streamed answer early (alone, and with a second request running) does not leave its tokens to the next request (server). |
+31
View File
@@ -53,4 +53,35 @@ inline const char* concurrent_read_off(const ConcurrentReadSetup& s) {
return nullptr;
}
/// Tokens of the next piece of a prompt read on the pipelined slot groups (--batch-groups > 1 on a layer split, #1906),
/// or 0 for "the whole rest in one run" (the opt-in is off).
/// piece_tokens: STRATA_PIPED_READ_PIECE, rounded down to whole chunks (at least one, at most kPipedMaxChunks)
/// A piece is one pipelined run over its chunks, so the stages stay overlapped inside it and only its end drains the
/// pipeline. At a piece's end the decoding slots decode, and a waiting shorter request can take over (BYIELD), as
/// between the pieces of a read off the pipeline.
inline constexpr int64_t kPipedMaxChunks = 32;
inline int64_t piped_read_piece(int64_t piece_tokens, int64_t chunk) {
if (piece_tokens <= 0) return 0;
chunk = std::max<int64_t>(chunk, 1);
return std::clamp<int64_t>(piece_tokens / chunk, 1, kPipedMaxChunks) * chunk;
}
/// The slots' decode time between two pieces: share x the piece's time (STRATA_BATCH_DECODE_SHARE, the rule the
/// one-GPU path uses between chunks), at most cap_ms (STRATA_PIPED_DECODE_MAX_MS; <= 0: no cap), at least floor_ms
/// (STRATA_PIPED_DECODE_MIN_MS).
inline double piped_decode_budget_ms(double piece_ms, double share, double cap_ms, double floor_ms) {
double b = std::max(0.0, share) * std::max(0.0, piece_ms);
if (cap_ms > 0.0) b = std::min(b, cap_ms);
return std::max(b, std::max(0.0, floor_ms));
}
/// Prompts arriving back to back: a piecewise read that starts within kPipedLeadInWindowMs + budget_ms of the
/// previous piecewise read's end first gives the slots one decode budget (otherwise they stop for both reads).
/// ms_since_prev_end: < 0 when there was no piecewise read yet
inline constexpr double kPipedLeadInWindowMs = 1000.0;
inline bool piped_read_lead_in(bool enabled, bool beside_slots, double ms_since_prev_end, double budget_ms) {
return enabled && beside_slots && ms_since_prev_end >= 0.0 &&
ms_since_prev_end <= kPipedLeadInWindowMs + std::max(0.0, budget_ms);
}
} // namespace strata::program
+88 -1
View File
@@ -11898,12 +11898,79 @@ int main(int argc, char** argv) {
}
return true;
}
const int64_t piece = strata::program::batch_read_piece(o.batch > 0, piped, !stages.empty(), batch_on(), C, yield_chunks);
// Opt-in (#1906): on the pipelined slot groups a read goes in pieces of STRATA_PIPED_READ_PIECE tokens
// (whole chunks). A piece's end has the pipeline drained, so a waiting shorter request can take
// over there (BYIELD); with slots decoding, the groups then decode through the pipeline (pump) for
// STRATA_BATCH_DECODE_SHARE x the piece's time, capped / floored by STRATA_PIPED_DECODE_MAX_MS /
// _MIN_MS, and are drained (pipe_drain) before the next piece, as before any read. Unset: one run.
static const int64_t pr_piece = [] {
const char* v = std::getenv("STRATA_PIPED_READ_PIECE");
return v != nullptr ? (int64_t) std::max(0, std::atoi(v)) : (int64_t) 0;
}();
static const double pr_cap = [] {
const char* v = std::getenv("STRATA_PIPED_DECODE_MAX_MS");
return v != nullptr ? std::max(0.0, std::atof(v)) : 0.0;
}();
static const double pr_floor = [] {
const char* v = std::getenv("STRATA_PIPED_DECODE_MIN_MS");
return v != nullptr ? std::max(0.0, std::atof(v)) : 0.0;
}();
const bool pr_on = piped && pr_piece > 0;
auto piece_now = [&]() -> int64_t {
return pr_on ? strata::program::piped_read_piece(pr_piece, C)
: strata::program::batch_read_piece(o.batch > 0, piped, !stages.empty(), batch_on(), C,
yield_chunks);
};
// the slots decode through the pipeline for budget_ms, drained before the next read
auto piped_decode = [&](double budget_ms, std::string& err) -> bool {
const auto td = Clock::now();
in_prompt_read = true;
do {
if (!pump(true)) {
in_prompt_read = false; batch_fatal = true; err = "a pipelined batch window failed";
return false;
}
} while (batch_on() &&
std::chrono::duration<double, std::milli>(Clock::now() - td).count() < budget_ms);
if (!pipe_drain()) {
in_prompt_read = false; batch_fatal = true; err = "a pipelined batch window failed";
return false;
}
in_prompt_read = false;
++il_parts;
il_ms += std::chrono::duration<double, std::milli>(Clock::now() - td).count();
return true;
};
// prompts arriving back to back: one decode budget before a read that follows a piecewise read
static Clock::time_point pr_prev_end{};
static bool pr_have_prev = false;
static double pr_prev_piece_ms = 0.0; // the previous read's last full piece
if (pr_on) {
const double lead =
pr_prev_piece_ms > 0.0
? strata::program::piped_decode_budget_ms(pr_prev_piece_ms, decode_share, pr_cap, pr_floor)
: (pr_cap > 0.0 ? pr_cap : std::max(pr_floor, 200.0));
const double since =
pr_have_prev ? std::chrono::duration<double, std::milli>(Clock::now() - pr_prev_end).count()
: -1.0;
if (strata::program::piped_read_lead_in(true, batch_on(), since, lead)) {
const int64_t rows0 = pipe_rows;
const auto tl = Clock::now();
if (!piped_decode(lead, e)) return false;
std::fprintf(stderr, "strata batch: a read right after a read: the slots decoded %lld rows in "
"%.0f ms first\n", (long long) (pipe_rows - rows0),
std::chrono::duration<double, std::milli>(Clock::now() - tl).count());
}
}
int64_t piece = piece_now();
if (piece <= 0) return sp.run(ids.data() + a0, b0 - a0, a0, e);
for (int64_t q = a0; q < b0;) {
const int64_t r = std::min(b0, q + piece);
const auto tq = Clock::now();
if (!sp.run(ids.data() + q, r - q, q, e)) return false;
const double piece_ms = std::chrono::duration<double, std::milli>(Clock::now() - tq).count();
if (pr_on && r < b0) pr_prev_piece_ms = piece_ms;
const int64_t piece_tokens = r - q;
q = r;
if (q >= b0) continue;
int ys = -1;
@@ -11923,6 +11990,25 @@ int main(int argc, char** argv) {
}
}
if (ys >= 0 && give_way(ys, q, e)) return false;
// (on the pipelined path a piece's end has the pipeline drained - nothing runs during a piece and
// every decode budget ends in pipe_drain - so the part read can give way there as off the pipeline)
if (pr_on) { // the pipelined pieces: the slots decode through the pipeline, then it is drained
if (!batch_on()) {
std::fprintf(stderr, "strata batch: piped read %lld tokens in %.0f ms (to %lld of %lld)\n",
(long long) piece_tokens, piece_ms, (long long) q, (long long) n);
continue;
}
const double budget =
strata::program::piped_decode_budget_ms(piece_ms, decode_share, pr_cap, pr_floor);
const int64_t rows0 = pipe_rows;
const auto td = Clock::now();
if (!piped_decode(budget, e)) return false;
std::fprintf(stderr, "strata batch: piped read %lld tokens in %.0f ms (to %lld of %lld), "
"then the slots decoded %lld rows in %.0f ms\n", (long long) piece_tokens,
piece_ms, (long long) q, (long long) n, (long long) (pipe_rows - rows0),
std::chrono::duration<double, std::milli>(Clock::now() - td).count());
continue;
}
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();
@@ -11939,6 +12025,7 @@ int main(int argc, char** argv) {
std::fflush(stderr);
}
}
if (pr_on) { pr_prev_end = Clock::now(); pr_have_prev = true; } // for the next read's lead-in
return true;
};
apply_pending(true);
+26
View File
@@ -8,6 +8,9 @@
using strata::program::batch_read_piece;
using strata::program::concurrent_read_off;
using strata::program::ConcurrentReadSetup;
using strata::program::piped_decode_budget_ms;
using strata::program::piped_read_lead_in;
using strata::program::piped_read_piece;
static int fails = 0;
#define CHECK(c) do { if (!(c)) { std::printf("FAIL %s:%d %s\n", __FILE__, __LINE__, #c); ++fails; } } while (0)
@@ -54,6 +57,29 @@ int main() {
for (size_t j = i; j < std::size(each); ++j) s.*each[j] = true;
CHECK(same(concurrent_read_off(s), named[i]));
}
// piped_read_piece (#1906): off -> one run; whole chunks, at least one, at most 32
CHECK(piped_read_piece(0, 8192) == 0);
CHECK(piped_read_piece(-5, 8192) == 0);
CHECK(piped_read_piece(32768, 8192) == 32768);
CHECK(piped_read_piece(20000, 8192) == 16384); // rounded down to whole chunks
CHECK(piped_read_piece(4096, 8192) == 8192); // never below one chunk
CHECK(piped_read_piece(1 << 30, 8192) == 32 * 8192);
CHECK(piped_read_piece(5000, 0) == 32); // chunk 0 guarded as 1 (and the cap applies)
for (int64_t c : {1024, 2048, 6144, 8192})
for (int64_t p : {1, 1000, 9000, 50000, 300000}) CHECK(piped_read_piece(p, c) % c == 0); // the header's rule
// piped_decode_budget_ms: share x piece, capped, floored; negatives are 0
CHECK(piped_decode_budget_ms(2000.0, 2.0, 400.0, 0.0) == 400.0);
CHECK(piped_decode_budget_ms(2000.0, 0.35, 3000.0, 0.0) == 700.0);
CHECK(piped_decode_budget_ms(2000.0, 0.0, 400.0, 150.0) == 150.0);
CHECK(piped_decode_budget_ms(2000.0, 2.0, 0.0, 0.0) == 4000.0); // no cap
CHECK(piped_decode_budget_ms(-5.0, -1.0, 400.0, -3.0) == 0.0);
// piped_read_lead_in: on, beside decoding slots, within 1000 ms + the budget of the previous piecewise read's end
CHECK(piped_read_lead_in(true, true, 0.0, 400.0));
CHECK(piped_read_lead_in(true, true, 1400.0, 400.0));
CHECK(!piped_read_lead_in(true, true, 1401.0, 400.0));
CHECK(!piped_read_lead_in(true, true, -1.0, 400.0));
CHECK(!piped_read_lead_in(false, true, 0.0, 400.0));
CHECK(!piped_read_lead_in(true, false, 0.0, 400.0));
if (fails == 0) std::printf("batch_read_test: ok\n");
return fails == 0 ? 0 : 1;
}