From cec7e2b1f6a5a5036477859b90f8b65dd221c811 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 9 Oct 2026 03:17:31 +0200 Subject: [PATCH] --batch on a layer split: a BYIELD is taken even when no slot is decoding A prompt read on a layer split with no slot decoding was one run, so the BYIELD of the batch_interleave_test step 4 was dropped at the end of the read; the long prompt kept its slot and the next BGEN failed with 'ERR BGEN: no such free slot' (pre-existing since 0.1.40.4). That read now goes in pieces of STRATA_BATCH_YIELD_CHUNKS chunks (default 8, 0 = one run), the same chunks a single run reads, with the yield check between pieces. batch_read_piece holds the arithmetic; batch_read_test covers it. --- CMakeLists.txt | 4 ++++ include/strata/program/batch_read.hpp | 30 +++++++++++++++++++++++++ src/program/generate.cpp | 13 ++++++++--- tests/program/batch_read_test.cpp | 32 +++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 include/strata/program/batch_read.hpp create mode 100644 tests/program/batch_read_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e0ce1c4..f65d8687 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -244,6 +244,10 @@ if(STRATA_BUILD_TESTS) add_executable(gguf_split_test tests/core/gguf_split_test.cpp) target_link_libraries(gguf_split_test PRIVATE strata_artifact) add_test(NAME gguf_split_test COMMAND gguf_split_test) + # how --batch cuts a prompt read into pieces (a layer split with no slot decoding must still reach a BYIELD) + add_executable(batch_read_test tests/program/batch_read_test.cpp) + target_include_directories(batch_read_test PRIVATE include) + add_test(NAME batch_read_test COMMAND batch_read_test) # the hipBLASLt tuning header's architecture gate: RDNA4 siblings share solution IDs, RDNA3 siblings share # theirs, cross-family pairs and version mismatches stay rejected (synthetic tables: no model, no GPU) diff --git a/include/strata/program/batch_read.hpp b/include/strata/program/batch_read.hpp new file mode 100644 index 00000000..c9036138 --- /dev/null +++ b/include/strata/program/batch_read.hpp @@ -0,0 +1,30 @@ +// include/strata/program/batch_read.hpp - how --batch reads a prompt in pieces (header-only: it is arithmetic). +// +// A prompt read with --batch stops between two pieces to (a) let the slots decode and (b) look for a BYIELD (a +// shorter request is waiting). The pieces always end on a multiple of the prompt chunk, so they read exactly the +// chunks a single run would. +#pragma once + +#include +#include + +namespace strata::program { + +/// Tokens of the next piece of a --batch prompt read, or 0 for "the whole rest in one run". +/// beside_slots: some slot is decoding (its windows run between the pieces) +/// layer_split / piped: the read goes through a layer split's stages / the pipelined groups +/// chunks_per_piece: for a layer split with no slot decoding, the pieces' length in chunks (0: one run) +/// A layer split reads its stages as a pipeline over one run's chunks, so a piece costs a pipeline fill; with slots +/// decoding the pieces are single chunks anyway. With no slot decoding a piece is `chunks_per_piece` chunks: long +/// enough to keep the pipeline full, short enough that a BYIELD is taken within a few chunks (it used to wait for the +/// whole prompt, and the long prompt then held its slot: "ERR BGEN: no such free slot" for the request that should +/// have taken over). +inline int64_t batch_read_piece(bool batch, bool piped, bool layer_split, bool beside_slots, int64_t chunk, + int64_t chunks_per_piece) { + if (!batch || piped) return 0; + chunk = std::max(chunk, 1); + if (!layer_split || beside_slots) return chunk; + return chunks_per_piece > 0 ? chunk * chunks_per_piece : 0; +} + +} // namespace strata::program diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 433d3957..1d817846 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -58,6 +58,7 @@ #include "strata/core/mtp.hpp" #include "strata/prefill/prefill.hpp" #include "strata/core/native_dense.hpp" +#include "strata/program/batch_read.hpp" #include "strata/program/logits_selection.hpp" #include "strata/program/conv_cache.hpp" #include "strata/program/message_boundary.hpp" @@ -9702,11 +9703,17 @@ int main(int argc, char** argv) { // the request ends with `YIELDED ` + DONE cancel, and the server sends it again later: it // continues from the slot with the same chunks. #656's cooperative preemption, with a slot as the park. auto read_part = [&](int64_t a0, int64_t b0, std::string& e) -> bool { - // (a layer split reads its stages as a pipeline over one run's chunks: in pieces only beside slots) - if (o.batch <= 0 || piped || (!stages.empty() && !batch_on())) return sp.run(ids.data() + a0, b0 - a0, a0, e); + // (a layer split reads its stages as a pipeline over one run's chunks: beside slots a chunk at a time, with + // none decoding in pieces of STRATA_BATCH_YIELD_CHUNKS chunks (default 8; 0 = one run) so a BYIELD is taken) + static const int64_t yield_chunks = [] { + const char* v = std::getenv("STRATA_BATCH_YIELD_CHUNKS"); + return v != nullptr ? (int64_t) std::max(0, std::atoi(v)) : (int64_t) 8; + }(); const int64_t C = std::max(sp.chunk(), 1); + const int64_t piece = strata::program::batch_read_piece(o.batch > 0, piped, !stages.empty(), batch_on(), C, yield_chunks); + 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 + C); + 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; q = r; diff --git a/tests/program/batch_read_test.cpp b/tests/program/batch_read_test.cpp new file mode 100644 index 00000000..d9842381 --- /dev/null +++ b/tests/program/batch_read_test.cpp @@ -0,0 +1,32 @@ +// batch_read_piece: the pieces of a --batch prompt read (synthetic: no model, no GPU). +#include "strata/program/batch_read.hpp" + +#include + +using strata::program::batch_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) + +int main() { + // no --batch, or the pipelined groups: one run + CHECK(batch_read_piece(false, false, false, false, 2048, 8) == 0); + CHECK(batch_read_piece(true, true, true, false, 2048, 8) == 0); + // one GPU: a chunk per piece, whether or not a slot decodes (the yield point is the chunk boundary) + CHECK(batch_read_piece(true, false, false, false, 2048, 8) == 2048); + CHECK(batch_read_piece(true, false, false, true, 2048, 8) == 2048); + // layer split with slots decoding: a chunk per piece as before + CHECK(batch_read_piece(true, false, true, true, 2048, 8) == 2048); + // layer split, nobody decoding: pieces of 8 chunks - the BYIELD of a shorter request can be taken (it was one run) + CHECK(batch_read_piece(true, false, true, false, 2048, 8) == 8 * 2048); + CHECK(batch_read_piece(true, false, true, false, 1024, 4) == 4096); + // STRATA_BATCH_YIELD_CHUNKS=0 keeps the old single run + CHECK(batch_read_piece(true, false, true, false, 2048, 0) == 0); + // a degenerate chunk never gives a zero piece + CHECK(batch_read_piece(true, false, false, false, 0, 8) == 1); + // a piece is always a multiple of the chunk + for (int64_t c : {256, 1000, 2048, 4096}) + for (int64_t k : {1, 3, 8, 16}) CHECK(batch_read_piece(true, false, true, false, c, k) % c == 0); + if (fails == 0) std::printf("batch_read_test: ok\n"); + return fails == 0 ? 0 : 1; +}