#465 engine: batch slots for one GPU - warn instead of refuse, windows over the active slots, slot conversation cache, prompts read beside decoding slots, BYIELD

- --slots N is the same as --batch N; a count the engine cannot run (no --serve, 1, more than 8, a split on one
  GPU, sessions that do not fit, --batch-groups that do not divide) is a warning and fewer slots or none, never a
  refusal; INFO says batch_slots=N when slots run
- a batch window runs over the active slots only (Verifier::run_slot_rows, graphs per row list): an idle slot is
  not written, so it keeps the conversation it served
- a request whose prompt starts with what an idle slot holds (all of it, or its turn checkpoint) continues from
  the slot's sessions instead of reading the history again
- with slots, a prompt is read one prompt chunk at a time (the same chunks as one run) and the slots decode
  between two chunks for half as long as the chunk took (STRATA_BATCH_DECODE_SHARE)
- BYIELD <slot>: the prompt being read stops at its next chunk boundary, the part read is copied into the slot,
  YIELDED <slot> <tokens> + DONE cancel; the same request sent again goes on from there with the same chunks
  and checkpoints (#656's cooperative preemption, a slot as the park)

Default (no --batch) unchanged.
This commit is contained in:
Niko1221 committed 2026-10-04 01:18:02 +02:00
1 parent 89351a799d
commit cab62c933f
3 files changed
+384 -83

No files matched your search

+304 -36
View File
@@ -617,6 +617,10 @@ void usage() {
" --vram-elastic --serve (#533, opt-in, NVIDIA): the expert cache in segments (--vram-segment-mib,\n"
" default 512), so the command `VRAM <reserve_mib>` (the server's POST /v1/vram) can\n"
" give VRAM back to other programs between requests and take it back later\n"
" --batch N / --slots N --serve (opt-in): up to N requests decode together in batch slots (2..8),\n"
" each slot with its own session (VRAM like the main one); docs/BATCHING.md\n"
" --batch-groups G --batch with a layer split: the slots in G groups pipelined through the GPUs\n"
" --trim-stage-weights an explicit --layer-split: each GPU loads only its own layers' dense weights\n"
" --prompt-cache-every N --serve: also checkpoint every N fresh prompt tokens (default 16384, 0 = off)\n"
" --turn-token ID --serve: the token that opens a chat turn (default 248045, <|im_start|>)\n"
" --tail-role-token ID --serve: a turn of this role right before the last turn is left out of\n"
@@ -1382,6 +1386,7 @@ int main(int argc, char** argv) {
else if (a == "--dump-final-r") o.dump_final_r = next("--dump-final-r");
else if (a == "--spec") o.spec = std::atoi(next("--spec"));
else if (a == "--batch") o.batch = std::atoi(next("--batch"));
else if (a == "--slots") o.batch = std::atoi(next("--slots")); // the same as --batch
else if (a == "--trim-stage-weights") o.trim_stage_weights = true;
else if (a == "--batch-groups") o.batch_groups = std::atoi(next("--batch-groups"));
else if (a == "--spec-oracle") o.spec_oracle = next("--spec-oracle");
@@ -2889,26 +2894,51 @@ int main(int argc, char** argv) {
// --batch: every stage carves o.batch more sessions like its own (same layer range and context), one per
// slot of the batch windows. Before the expert cache is sized, so `--expert-cache auto` leaves them room.
// Recommend, never force: a count the engine cannot run is said and adjusted (or batching left off) - the
// engine still starts and serves one request at a time.
std::vector<std::vector<std::unique_ptr<strata::core::SessionState>>> bslot_ss;
if (o.batch > 0) {
if (!o.serve || o.batch < 2 || o.batch > strata::kernels::kVerifyMaxT) {
std::fprintf(stderr, "strata generate: --batch N needs --serve and 2 <= N <= %d\n", strata::kernels::kVerifyMaxT);
return 2;
if (o.batch != 0) {
const int cap = strata::kernels::kVerifyMaxT;
const char* off = !o.serve ? "it needs --serve" : o.batch < 2 ? "it needs 2 or more slots"
: split_same ? "it needs each stage of a layer split on its own GPU" : nullptr;
if (off != nullptr) {
std::fprintf(stderr, "strata generate: WARNING: --batch %d is off: %s\n", o.batch, off);
o.batch = 0;
} else if (o.batch > cap) {
std::fprintf(stderr, "strata generate: WARNING: --batch %d: a batch window holds at most %d rows, so %d "
"slots\n", o.batch, cap, cap);
o.batch = cap;
}
}
if (o.batch > 0 && o.batch_groups > 1 && (stages.empty() || o.batch % o.batch_groups != 0)) {
std::fprintf(stderr, "strata generate: WARNING: --batch-groups %d needs a layer split and to divide --batch %d; "
"one group\n", o.batch_groups, o.batch);
o.batch_groups = 1;
}
if (o.batch > 0) {
bslot_ss.resize(1 + stages.size());
int fit = o.batch; // the slots every stage could carve
uint64_t bytes0 = 0;
for (size_t k = 0; k < bslot_ss.size(); ++k) {
const int dev = k == 0 ? 0 : stages[k - 1]->dev;
const int64_t lo = k == 0 ? 0 : stages[k - 1]->lb;
const int64_t hi = k == 0 ? (multi_gpu ? split_at[0] : -1) : stages[k - 1]->le;
const strata::core::OnDevice on_k(dev);
const uint64_t bytes = strata::core::session_bytes(g, o.max_context, K, lo, hi);
for (int b = 0; b < o.batch; ++b) {
if (k == 0) bytes0 = bytes;
for (int b = 0; b < fit; ++b) {
auto u = std::make_unique<strata::core::SessionState>();
void* buf = nullptr;
if (cudaMalloc(&buf, bytes) != cudaSuccess || strata::core::session_init(g, o.max_context, K, buf, *u, lo, hi) == 0) {
std::fprintf(stderr, "strata generate: --batch: slot %d's session on CUDA%d does not fit (%.2f GiB each)\n",
b, dev, (double) bytes / 1073741824.0);
return 1;
cudaGetLastError();
if (buf != nullptr) cudaFree(buf);
size_t fb = 0, tb = 0;
cudaMemGetInfo(&fb, &tb);
std::fprintf(stderr, "strata generate: WARNING: --batch %d: slot %d's session does not fit on CUDA%d "
"(%.2f GiB each, %.2f GiB free); %d slot%s\n", o.batch, b, dev,
(double) bytes / 1073741824.0, (double) fb / 1073741824.0, b, b == 1 ? "" : "s");
fit = b;
break;
}
strata::core::session_zero(*u, g, nullptr, nullptr);
bslot_ss[k].push_back(std::move(u));
@@ -2917,7 +2947,21 @@ int main(int argc, char** argv) {
size_t fb = 0, tb = 0;
cudaMemGetInfo(&fb, &tb);
std::fprintf(stderr, "strata generate: --batch: %d slot sessions on CUDA%d (%.2f GiB each); %.2f GiB free\n",
o.batch, dev, (double) bytes / 1073741824.0, (double) fb / 1073741824.0);
(int) bslot_ss[k].size(), dev, (double) bytes / 1073741824.0, (double) fb / 1073741824.0);
}
for (auto& v : bslot_ss) if ((int) v.size() > fit) v.resize((size_t) fit); // the same count on every stage
if (fit < 2) {
std::fprintf(stderr, "strata generate: WARNING: --batch is off: not two slot sessions fit (a smaller "
"--max-context or KV streaming, --kv-resident, makes them smaller)\n");
bslot_ss.clear();
o.batch = 0;
} else {
o.batch = fit;
if (o.batch_groups > 1 && o.batch % o.batch_groups != 0) o.batch_groups = 1;
// the sessions' VRAM is the expert cache's: say what it costs (docs/BATCHING.md has the measured trade)
std::fprintf(stderr, "strata generate: --batch %d: the slot sessions take %.2f GiB of VRAM on CUDA0 that the "
"expert cache would otherwise hold\n", o.batch,
(double) bytes0 * o.batch / 1073741824.0);
}
}
@@ -4876,10 +4920,6 @@ int main(int argc, char** argv) {
}
// each stage's verifier gets its stage's slot sessions
if (o.batch > 0) {
if (split_same) {
std::fprintf(stderr, "strata serve: --batch needs each stage on its own GPU\n");
return 1;
}
for (size_t k = 0; k < bslot_ss.size(); ++k) {
std::vector<strata::core::SessionState*> ptrs;
for (auto& u : bslot_ss[k]) ptrs.push_back(u.get());
@@ -5435,7 +5475,7 @@ int main(int argc, char** argv) {
"expert_slots_primary=%lld expert_cache_primary_mib=%lld spec=%d "
"mtp_max=%d lookup=%d vram_free_mib=%lld cvec=%s arena_mib=%lld pool_workers=%d pcie_frac=%.2f "
"spec_min_p=%.2f conversation_cache_mib=%lld conversation_cache_slots=%d "
"conversation_cache_min_free_mib=%lld tail_role_token=%lld vram_elastic=%d engine=" STRATA_VERSION "\n",
"conversation_cache_min_free_mib=%lld tail_role_token=%lld vram_elastic=%d%s engine=" STRATA_VERSION "\n",
(long long) o.max_context, o.kv.c_str(),
(long long) (g.n_qsa_layers() > 0 && ss.qsa_states[ss.qsa_primary()].kv_mode == 1
? ss.qsa_states[ss.qsa_primary()].n_slots * 4 : 0),
@@ -5446,7 +5486,8 @@ int main(int argc, char** argv) {
(long long) ((o.mmap_experts ? src.resident_bytes() : strata::kernels::cpu::expert_layout().total) >> 20),
pool.workers(), o.pcie_frac,
o.spec_min_p, (long long) o.conversation_cache_mib, o.conversation_cache_slots,
(long long) o.conversation_cache_min_free_mib, (long long) o.tail_role_token, xcache.segmented() ? 1 : 0);
(long long) o.conversation_cache_min_free_mib, (long long) o.tail_role_token, xcache.segmented() ? 1 : 0,
o.batch > 0 ? (" batch_slots=" + std::to_string(o.batch)).c_str() : "");
}
// issue #29: a request whose heartbeat (tokens, prompt chunks, verify windows) stops for this long is stuck on
// a flag nobody will raise - end the engine with where it was, so the server starts it again instead of the
@@ -5619,6 +5660,20 @@ int main(int argc, char** argv) {
int64_t p = 0; ///< its position
int64_t produced = 0, max_new = 0;
Clock::time_point t0;
// the slot's own conversation cache: the tokens its sessions hold (the prompt, then every token a window
// fed). Kept when the request ends (`cached`), so the next turn of that conversation continues from it
// instead of reading its history again (see `slot_source` in the request path).
std::vector<int32_t> ids;
bool cached = false; ///< idle, and its sessions still hold `ids`
bool cvec = true; ///< the control vector setting `ids` were read with
bool img = false; ///< the conversation has pictures (never reused from the slot)
// the checkpoint its prompt read took at the last turn boundary (the history before the new turn's
// header): a client's next turn renders the history again WITHOUT this reply's thinking, so it matches
// the slot's tokens only up to there - the slot's K/V up to it plus this state continue from it
std::vector<ConvCheckpoint> checks;
// a prompt read that gave way to a waiting request (BYIELD): `ids` is the part read so far, and the same
// request continues from it (the read goes on with the same chunks; `from0`: it had started at token 0)
bool partial = false, partial_from0 = false;
};
std::vector<BSlot> bs((size_t) std::max(o.batch, 0));
// timing of the batch windows since the slots were last all idle (one stderr line then)
@@ -5662,15 +5717,55 @@ int main(int argc, char** argv) {
}
return true;
};
// one batch window over slots [0, highest active], inactive slots below it fed a pad token at position 0
// slot b's sessions -> the main ones (the reverse of copy_to_slot): a request that continues the
// conversation an idle slot holds reads on from there. Every stage, the same calls.
// `at`: one of the slot's checkpoints - only the K/V up to it is copied and its state restored instead
auto copy_from_slot = [&](int b, const ConvCheckpoint* at, std::string& e) -> bool {
const int64_t upto = at != nullptr ? (int64_t) at->ids.size() : (int64_t) bs[(size_t) b].ids.size();
if (at != nullptr && at->stage_parts.size() != stages.size()) { e = "a checkpoint without its stage parts"; return false; }
for (size_t k = 0; k < bslot_ss.size(); ++k) {
strata::core::SessionState& from = *bslot_ss[k][(size_t) b];
strata::core::SessionState& to = k == 0 ? ss : stages[k - 1]->ss;
const strata::core::OnDevice on_k(k == 0 ? 0 : stages[k - 1]->dev);
if (cudaDeviceSynchronize() != cudaSuccess) { e = "batch slot restore: device sync failed"; return false; }
if (at != nullptr) {
if (!strata::core::conversation_checkpoint_restore(k == 0 ? *at : at->stage_parts[k - 1], to, g, e))
return false;
} else {
strata::core::ConversationCheckpoint ck;
ck.ids = bs[(size_t) b].ids;
if (!strata::core::conversation_checkpoint_save(ck, from, g, e) ||
!strata::core::conversation_checkpoint_restore(ck, to, g, e))
return false;
}
for (int64_t j = 0; j < from.qsa_alloc; ++j) {
strata::core::ConversationKv img;
if (!strata::core::conversation_kv_save(img, from.qsa_states[from.qsa_ord0 + j], g, upto, true, e))
return false;
if (cudaDeviceSynchronize() != cudaSuccess) { e = "batch slot restore: device sync failed"; return false; }
if (!strata::core::conversation_kv_restore(img, to.qsa_states[to.qsa_ord0 + j], g, upto, true, e))
return false;
}
if (cudaDeviceSynchronize() != cudaSuccess) { e = "batch slot restore: device sync failed"; return false; }
}
return true;
};
// one batch window over the active slots only (row t is the t-th active slot): an idle slot is not touched,
// so it keeps the conversation it holds
auto batch_step = [&]() -> bool {
int S = 0;
for (int b = 0; b < (int) bs.size(); ++b) if (bs[(size_t) b].active) S = b + 1;
if (S == 0) return true;
int rows[strata::kernels::kVerifyMaxT] = {};
int32_t tok[strata::kernels::kVerifyMaxT] = {}, outb[strata::kernels::kVerifyMaxT] = {};
int64_t pos[strata::kernels::kVerifyMaxT] = {};
for (int b = 0; b < S; ++b)
if (bs[(size_t) b].active) { tok[b] = bs[(size_t) b].x; pos[b] = bs[(size_t) b].p; }
for (int b = 0; b < (int) bs.size() && S < strata::kernels::kVerifyMaxT; ++b)
if (bs[(size_t) b].active) {
rows[S] = b;
tok[S] = bs[(size_t) b].x;
pos[S] = bs[(size_t) b].p;
++S;
}
if (S == 0) return true;
const bool was_busy = strata::core::progress().busy.load();
strata::core::progress().busy.store(true);
drive.d.layers = 0;
drive.d.experts = 0;
@@ -5682,7 +5777,7 @@ int main(int argc, char** argv) {
bt_miss0 = drive.d.multi_misses; bt_hits0 = drive.d.cache_hits; bt_pcie0 = drive.d.pcie_experts;
}
const Clock::time_point w0 = Clock::now();
if (!ver.run_slots(S, tok, pos, win_pool_fn, win_pool_user, outb, err) || drive.d.failed) {
if (!ver.run_slot_rows(rows, S, tok, pos, win_pool_fn, win_pool_user, outb, err) || drive.d.failed) {
std::printf("ERR %s\n", drive.d.failed && drive.d.fail ? drive.d.fail : err.c_str());
return false;
}
@@ -5697,10 +5792,11 @@ int main(int argc, char** argv) {
bt_commit += msd(w1, w2);
++bt_windows;
bt_rows += S;
for (int b = 0; b < S; ++b) {
for (int t = 0; t < S; ++t) {
const int b = rows[t];
BSlot& sl = bs[(size_t) b];
if (!sl.active) continue;
const int32_t y = outb[b];
const int32_t y = outb[t];
sl.ids.push_back(sl.x); // the window fed it: the slot's sessions hold it now
std::printf("BT %d %d\n", b, (int) y);
++sl.produced;
const bool eos = std::find(o.eos_ids.begin(), o.eos_ids.end(), (int64_t) y) != o.eos_ids.end();
@@ -5710,6 +5806,7 @@ int main(int argc, char** argv) {
const double ms = std::chrono::duration<double, std::milli>(Clock::now() - sl.t0).count();
std::printf("BDONE %d %lld %s %.1f\n", b, (long long) sl.produced, fin, ms);
sl.active = false;
sl.cached = o.prompt_cache > 0 && !sl.img; // its sessions hold sl.ids for the next turn
} else {
sl.x = y;
sl.p += 1;
@@ -5717,7 +5814,7 @@ int main(int argc, char** argv) {
}
std::fflush(stdout);
bt_emit += msd(w2, Clock::now());
strata::core::progress().busy.store(false);
strata::core::progress().busy.store(was_busy);
if (!batch_on() && bt_windows > 0) {
const double w = (double) bt_windows, wall = msd(bt_start, Clock::now());
const double L = (double) g.n_layers;
@@ -5741,10 +5838,6 @@ int main(int argc, char** argv) {
// ---- --batch-groups: the slot groups pipelined through the stages (--batch-groups G > 1 with a layer split)
const int n_pipe = (int) stages.size() + 1;
const bool piped = o.batch > 0 && o.batch_groups > 1 && n_pipe > 1;
if (o.batch > 0 && o.batch_groups > 1 && (o.batch % o.batch_groups != 0 || !piped)) {
std::fprintf(stderr, "strata serve: --batch-groups needs a layer split and to divide --batch\n");
return 1;
}
const int GS = piped ? o.batch / o.batch_groups : o.batch;
struct PGroup {
bool inflight = false;
@@ -5792,10 +5885,12 @@ int main(int argc, char** argv) {
const bool eos = std::find(o.eos_ids.begin(), o.eos_ids.end(), (int64_t) y) != o.eos_ids.end();
const char* fin = eos ? "stop" : sl.stop ? "cancel" : sl.produced >= sl.max_new ? "length"
: sl.p + 2 > o.max_context ? "length" : nullptr;
sl.ids.push_back(sl.x);
if (fin != nullptr) {
const double ms = std::chrono::duration<double, std::milli>(Clock::now() - sl.t0).count();
std::printf("BDONE %d %lld %s %.1f\n", gi * GS + t, (long long) sl.produced, fin, ms);
sl.active = false;
sl.cached = false; // the pipeline's pad rows: a pipelined slot is not reused as a cache
} else {
sl.x = y;
sl.p += 1;
@@ -5825,9 +5920,10 @@ int main(int argc, char** argv) {
rr = pick + 1;
PGroup& G = pg[(size_t) pick];
for (int t = 0; t < GS; ++t) {
const BSlot& sl = bs[(size_t) (pick * GS + t)];
BSlot& sl = bs[(size_t) (pick * GS + t)];
G.tok[t] = sl.active ? sl.x : 0;
G.pos[t] = sl.active ? sl.p : 0;
if (!sl.active) sl.cached = false; // its pad row writes its state
}
G.inflight = true;
G.stage = 0;
@@ -5880,6 +5976,7 @@ int main(int argc, char** argv) {
if (b >= 0 && b < (int) bs.size()) bs[(size_t) b].stop = true;
continue;
}
if (line.rfind("BYIELD", 0) == 0) continue; // for a prompt read that has ended meanwhile
if (line.rfind("VRAM", 0) == 0) { // #533 (above): between requests, not a request
std::string verr;
if (batch_on()) verr = "VRAM: not while batch slots are decoding";
@@ -6119,9 +6216,32 @@ int main(int argc, char** argv) {
from_live = false;
}
}
// --batch: an idle slot that holds the start of this prompt (the conversation it served last) is a
// source too - its sessions are copied back below, so only the new part is read
int slot_source = -1;
int64_t slot_tokens = 0;
bool resumed_from0 = false; // a prompt read parked in a slot (BYIELD) that had started at token 0
const ConvCheckpoint* slot_ck = nullptr; // the slot's checkpoint the prompt continues from (else its end)
if (o.prompt_cache > 0 && req_imgs.empty())
for (int b = 0; b < (int) bs.size(); ++b) {
const BSlot& sl = bs[(size_t) b];
if (sl.active || !sl.cached || sl.cvec != want_cvec) continue;
if ((int64_t) sl.ids.size() > std::max(resume, slot_tokens) && starts_with(sl.ids, {})) {
slot_source = b;
slot_tokens = (int64_t) sl.ids.size();
slot_ck = nullptr;
}
for (const ConvCheckpoint& c : sl.checks)
if ((int64_t) c.ids.size() > std::max(resume, slot_tokens) && starts_with(c.ids, c.imgs)) {
slot_source = b;
slot_tokens = (int64_t) c.ids.size();
slot_ck = &c;
}
}
const auto parked = conversations.best(ids, req_imgs, want_cvec);
std::optional<strata::core::SavedConversation> incoming;
if (parked.tokens > resume) incoming.emplace(conversations.take(parked.index));
if (parked.tokens > std::max(resume, slot_tokens)) incoming.emplace(conversations.take(parked.index));
if (incoming) slot_source = -1;
// Reject the entire image before parking/overwriting the outgoing
// state. Invalid entries can safely fall back to its existing prefix.
if (incoming && incoming->stage_images.size() != stages.size()) {
@@ -6147,10 +6267,47 @@ int main(int argc, char** argv) {
}
// Preserve the outgoing branch before any checkpoint rewind, reset,
// or incoming restore overwrites the positional state it requires.
if ((!from_live || incoming) && !park_current(incoming ? incoming->bytes() : 0)) {
if ((!from_live || incoming || slot_source >= 0) && !park_current(incoming ? incoming->bytes() : 0)) {
std::printf("ERR %s\n", err.c_str());
return 1;
}
if (slot_source >= 0) {
const auto t0 = Clock::now();
if (!copy_from_slot(slot_source, slot_ck, err)) {
// the main session may be half written: read this prompt from the start
std::fprintf(stderr, "strata batch: restoring slot %d failed (%s); reading the prompt\n",
slot_source, err.c_str());
err.clear();
live.clear();
live_imgs.clear();
checks.clear();
resume = 0;
from_live = false;
} else {
checks.clear(); // the main session's checkpoints were of the conversation it held before
// a read that gave way, the same request again (into its own slot): on with it - its segments and
// checkpoints as if it had not stopped
if (slot_ck == nullptr && bs[(size_t) slot_source].partial && admit_slot == slot_source) {
resumed_from0 = bs[(size_t) slot_source].partial_from0;
for (const ConvCheckpoint& c : bs[(size_t) slot_source].checks) checks.push_back(c);
bs[(size_t) slot_source].partial = false;
}
if (slot_ck != nullptr) {
live = slot_ck->ids;
checks.push_back(*slot_ck); // the main session's chain has it now
checks.back().used = ++check_clock;
} else {
live = bs[(size_t) slot_source].ids;
}
live_imgs.clear();
resume = slot_tokens;
from_live = true;
std::fprintf(stderr, "strata batch: slot %d gave back %lld tokens of this conversation (%s) in "
"%.1f ms\n", slot_source, (long long) slot_tokens,
slot_ck != nullptr ? "its turn checkpoint" : "all it holds",
std::chrono::duration<double, std::milli>(Clock::now() - t0).count());
}
}
if (incoming) {
const auto t0 = Clock::now();
if (strata::core::conversation_snapshot_restore(*incoming, ss, g,
@@ -6462,6 +6619,99 @@ int main(int argc, char** argv) {
}
return true;
};
// --batch: a prompt read while slots are decoding goes one prompt chunk at a time (the same chunks as one
// run: each run starts where the last ended, at a multiple of the chunk), and between two chunks the slots
// decode for a share of the time the chunk took (STRATA_BATCH_DECODE_SHARE, default 0.5), so a long
// prompt does not stop the others. The slots' windows then see the cache without the slots this prompt
// borrowed (marked missing: those experts run on the CPU), never a prompt buffer.
static const double decode_share = [] {
const char* v = std::getenv("STRATA_BATCH_DECODE_SHARE");
return v != nullptr ? std::max(0.0, std::atof(v)) : 0.5;
}();
bool batch_fatal = false;
int64_t il_parts = 0; // the slots' windows run between this prompt's chunks
double il_ms = 0;
int64_t yielded_at = -1; // BYIELD: the read stopped here and its slot holds it
int yielded_slot = -1;
// `BYIELD <slot>` (the server: a shorter request is waiting): at the next chunk boundary the part read so
// far is copied into <slot> (this admission's own, or a free one the server reserved for a solo request),
// 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);
const int64_t C = std::max<int64_t>(sp.chunk(), 1);
for (int64_t q = a0; q < b0;) {
const int64_t r = std::min(b0, q + C);
const auto tq = Clock::now();
if (!sp.run(ids.data() + q, r - q, q, e)) return false;
q = r;
if (q >= b0) continue;
int ys = -1;
{ // a BSTOP that came meanwhile ends its slot at its next window; a BYIELD is for this read
std::lock_guard<std::mutex> lk(in_mu);
for (auto it = in_lines.begin(); it != in_lines.end();) {
if (it->rfind("BSTOP ", 0) == 0) {
const int b = std::atoi(it->c_str() + 6);
if (b >= 0 && b < (int) bs.size()) bs[(size_t) b].stop = true;
it = in_lines.erase(it);
} else if (it->rfind("BYIELD ", 0) == 0) {
ys = std::atoi(it->c_str() + 7);
it = in_lines.erase(it);
} else {
++it;
}
}
}
if (ys >= 0) {
// only where the rest is read the same way after it (more than a chunk and more than a short
// read left), text only, and into a slot that is not decoding
const bool can = ys < (int) bs.size() && !bs[(size_t) ys].active &&
(admit_slot < 0 || ys == admit_slot) && req_imgs.empty() && o.prompt_cache > 0 &&
b0 - q > std::max<int64_t>(C, o.short_read);
std::string ye;
const auto ty = Clock::now();
std::vector<int32_t> pre(ids.begin(), ids.begin() + q);
if (can && copy_to_slot(ys, pre, ye)) {
BSlot& sl = bs[(size_t) ys];
sl = BSlot{};
sl.ids = std::move(pre);
sl.cached = true;
sl.partial = true;
sl.partial_from0 = read_from == 0 || resumed_from0;
sl.cvec = cvec_cached;
for (const ConvCheckpoint& c : checks) // the root / periodic checkpoints of this read
if ((int64_t) c.ids.size() <= q && std::equal(c.ids.begin(), c.ids.end(), ids.begin(),
[](int32_t x, int64_t y) { return (int64_t) x == y; }))
sl.checks.push_back(c);
yielded_at = q;
yielded_slot = ys;
std::fprintf(stderr, "strata batch: the prompt read gives way at %lld of %lld tokens; slot %d "
"holds it (copied in %.1f ms)\n", (long long) q, (long long) n, ys,
std::chrono::duration<double, std::milli>(Clock::now() - ty).count());
e = "yield";
return false;
}
if (can) bs[(size_t) ys].cached = false; // half written
std::fprintf(stderr, "strata batch: BYIELD %d not taken at %lld of %lld%s%s\n", ys, (long long) q,
(long long) n, ye.empty() ? "" : ": ", ye.c_str());
}
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();
do {
if (!batch_step()) { batch_fatal = true; e = "a batch window failed"; return false; }
} while (batch_on() && std::chrono::duration<double, std::milli>(Clock::now() - td).count() < budget);
++il_parts;
il_ms += std::chrono::duration<double, std::milli>(Clock::now() - td).count();
if (trace) {
std::fprintf(stderr, "strata trace: prompt chunk to %lld, then the slots decoded %.0f ms\n",
(long long) q, std::chrono::duration<double, std::milli>(Clock::now() - td).count());
std::fflush(stderr);
}
}
return true;
};
apply_pending(true);
// per-request sampling for the verify window's head (greedy when temperature is absent)
strata::kernels::SamplerParams req_sp;
@@ -6511,7 +6761,7 @@ int main(int argc, char** argv) {
// (PR #65, code-martin.) Only for a system prompt of --prompt-cache-root tokens or more: a small one
// is cheaper to read again than the extra part costs (~0.3 s).
int64_t root_at = -1;
if (o.prompt_cache > 0 && o.turn_token >= 0 && o.prompt_cache_root > 0 && read_from == 0)
if (o.prompt_cache > 0 && o.turn_token >= 0 && o.prompt_cache_root > 0 && (read_from == 0 || resumed_from0))
for (int64_t i = 1; i < turn_at; ++i)
if (ids[(size_t) i] == o.turn_token) {
if (i >= o.prompt_cache_root) root_at = i;
@@ -6531,7 +6781,7 @@ int main(int argc, char** argv) {
return 1;
}
const auto tsp = Clock::now();
const bool sp_ok = win ? read_windows(at, to, err) : sp.run(ids.data() + at, to - at, at, err);
const bool sp_ok = win ? read_windows(at, to, err) : read_part(at, to, err);
if (trace) {
std::fprintf(stderr, "strata trace: read %lld tokens (%s) in %.1f ms\n", (long long) (to - at),
win ? "windows" : "batched",
@@ -6539,7 +6789,7 @@ int main(int argc, char** argv) {
std::fflush(stderr);
}
if (!sp_ok) {
if (!stop_req.load()) {
if (yielded_at < 0 && (batch_fatal || !stop_req.load())) {
std::fprintf(stderr, "strata serve: %s\n", err.c_str());
std::printf("ERR %s\n", err.c_str());
// #224: a CUDA fault (an illegal address) poisons the context for the whole process, and
@@ -6565,6 +6815,9 @@ int main(int argc, char** argv) {
return 1;
}
tr("prompt done (slots refilled)");
if (il_parts > 0)
std::fprintf(stderr, "strata batch: the prompt was read in %lld parts, the slots decoding %.0f ms between "
"them\n", (long long) il_parts + 1, il_ms);
const double prompt_ms = std::chrono::duration<double, std::milli>(Clock::now() - r0).count();
std::printf("REUSED %lld\n", (long long) resume); // the prompt is read; the first window comes next
std::fflush(stdout);
@@ -6864,6 +7117,7 @@ int main(int argc, char** argv) {
// [prompt tokens read] (#471: fewer than <prompt> - <reused> when a cancel stopped the read)
// [offloaded] (#588: the decode's routed experts the GPU read over PCIe or another GPU computed;
// not in [lookups])
if (yielded_at >= 0) std::printf("YIELDED %d %lld\n", yielded_slot, (long long) yielded_at);
std::printf("DONE %lld %lld %.1f %.1f %s %lld %lld %lld %lld %lld %lld %lld %.1f %lld %lld\n",
(long long) produced_n,
(long long) n, prompt_ms, decode_ms, finish, (long long) draft_accepted, (long long) draft_offered,
@@ -6873,9 +7127,12 @@ int main(int argc, char** argv) {
(long long) req_offload);
std::fflush(stdout);
if (admit_slot >= 0) { // --batch: BADM <slot> <1 = continues in the batch windows | 0 = done>
bool cont = !cancelled && produced_n == 1 && admit_max_new > 1 && std::strcmp(finish, "length") == 0;
bool cont = !cancelled && produced_n == 1 && admit_max_new > 1 && std::strcmp(finish, "length") == 0 &&
(int64_t) live.size() == p;
const auto tc0 = Clock::now();
if (cont && !copy_to_slot(admit_slot, live, err)) {
std::fprintf(stderr, "strata serve: batch admission failed: %s\n", err.c_str());
bs[(size_t) admit_slot].cached = false; // its sessions may be half written
cont = false;
}
if (cont) {
@@ -6888,6 +7145,17 @@ int main(int argc, char** argv) {
sl.produced = 1;
sl.max_new = admit_max_new;
sl.t0 = Clock::now();
sl.ids = live;
sl.cvec = cvec_cached;
sl.img = !live_imgs.empty(); // pictures: not matched again by tokens alone, so not cached
const ConvCheckpoint* best = nullptr;
for (const ConvCheckpoint& c : checks)
if (c.ids.size() < live.size() && (best == nullptr || c.ids.size() > best->ids.size()) &&
std::equal(c.ids.begin(), c.ids.end(), live.begin()))
best = &c;
if (best != nullptr && !sl.img) sl.checks.push_back(*best);
std::fprintf(stderr, "strata batch: slot %d takes %lld tokens (copied in %.1f ms)\n", admit_slot,
(long long) live.size(), std::chrono::duration<double, std::milli>(Clock::now() - tc0).count());
}
std::printf("BADM %d %d\n", admit_slot, cont ? 1 : 0);
std::fflush(stdout);