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 on a layer split: a BYIELD is taken even when no slot is decoding

Niko1221cec7e2b1f6a5
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6e0ce1c49..f65d86872 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 000000000..c90361380
--- /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 <algorithm>
+#include <cstdint>
+
+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<int64_t>(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 433d39576..1d817846c 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 <slot> <tokens>` + 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<int64_t>(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 000000000..d9842381d
--- /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 <cstdio>
+
+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;
+}