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++

#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

Niko1221cab62c933f4f
diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hpp
index f84a9d0a0..9e0f35686 100644
--- a/include/strata/core/verify.hpp
+++ b/include/strata/core/verify.hpp
@@ -142,6 +142,10 @@ public:
/// One batch window over slots [0, S): tokens[s] at positions pos[s]; out[s] = the greedy pick after it.
bool run_slots(int S, const int32_t* tokens, const int64_t* pos, PoolMultiFn pool, void* user, int32_t* out,
std::string& err);
+ /// The same over the S slots `rows` (row t is slot rows[t], any distinct slots in any order): the slots not
+ /// listed are not touched, so an idle slot keeps its state (a finished conversation it may continue later).
+ bool run_slot_rows(const int* rows, int S, const int32_t* tokens, const int64_t* pos, PoolMultiFn pool, void* user,
+ int32_t* out, std::string& err);
/// Keep every row of the last batch window: each slot's state advances by its one token.
bool commit_slots(std::string& err);
@@ -210,26 +214,32 @@ private:
// batch windows (see init_slots)
std::vector<SessionState*> slots_;
bool batch_rec_ = false; ///< record_window is capturing a batch window
- int row_base_ = 0; ///< ... over slots [row_base_, row_base_ + T)
+ int row_base_ = 0; ///< ... its hand-off rows start here (a pipeline group's own rows)
+ int brow_[8] = {}; ///< ... and row t is slot brow_[t]
bool last_batch_ = false; ///< the last run was a batch window (set_plan_slot: one group)
- std::map<int, cudaGraphExec_t> exec_bm_, commit_bm_; ///< key base * 16 + S
- int last_base_ = 0;
+ std::map<uint64_t, cudaGraphExec_t> exec_bm_, commit_bm_; ///< key: batch_key(rows, S, hand-off base)
+ int last_rows_[8] = {}; ///< the slots of the last batch window's rows
+ static uint64_t batch_key(const int* rows, int S, int hbase) {
+ uint64_t k = (uint64_t) hbase << 40 | (uint64_t) S << 32;
+ for (int t = 0; t < S; ++t) k |= (uint64_t) (rows[t] & 15) << (4 * t);
+ return k;
+ }
// batch_launch / batch_poll
bool b_running_ = false;
int64_t b_k_ = 0, b_steps_ = 0;
std::chrono::steady_clock::time_point b_last_;
int32_t b_out_[8] = {};
std::vector<strata::kernels::SamplerParams> slot_sp_;
- bool sample_rows(int base, int S, std::string& err); ///< the sampled slots' rows of the last batch window
+ bool sample_rows(int S, std::string& err); ///< the sampled slots' rows of the last batch window
int32_t* h_commitb_ = nullptr; int32_t* m_commitb_ = nullptr; // per slot [1, 0, pos, -1 ..], stride 2 + max_t
int32_t* commitb_ = nullptr;
float* tail_snap_b_ = nullptr; ///< per (slot, QSA layer) indexer tail snapshot
void* arena_b_ = nullptr;
int64_t last_pos_b_[8] = {};
- bool capture_batch(int base, int S, std::string& err);
+ bool capture_batch(const int* rows, int S, int hbase, std::string& err);
void collect_profile(); ///< STRATA_VERIFY_PROFILE: add the last window's stamps to prof_sum_
- bool capture_commit_batch(int base, int S, std::string& err);
- bool stage_batch(int base, int S, const int32_t* tokens, const int64_t* pos, std::string& err);
+ bool capture_commit_batch(const int* rows, int S, int hbase, std::string& err);
+ bool stage_batch(const int* rows, int S, int hbase, const int32_t* tokens, const int64_t* pos, std::string& err);
strata::kernels::SamplerParams sampling_ = [] {
strata::kernels::SamplerParams s;
s.greedy = true;
diff --git a/src/core/verify.cpp b/src/core/verify.cpp
index 868e32b18..5ba7c93bf 100644
--- a/src/core/verify.cpp
+++ b/src/core/verify.cpp
@@ -537,7 +537,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
if (!batch_rec_) groups_[T] = G;
const int64_t nQall = g.n_qsa_layers();
// a batch window: row t is slot t, whose state lives in its own session
- auto slot_ss = [&](int t) -> SessionState& { return batch_rec_ ? *slots_[(size_t) (row_base_ + t)] : ss; };
+ auto slot_ss = [&](int t) -> SessionState& { return batch_rec_ ? *slots_[(size_t) brow_[t]] : ss; };
const int hrow0 = batch_rec_ ? row_base_ : 0; // a slot group's own hand-off rows
// ---- the window's inputs, from mapped staging
@@ -736,7 +736,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
stamp(l, 8, grp);
if (batch_rec_) { // every slot's indexer tail, restored by its commit
for (int t = tb; t < te; ++t)
- copy_from_mapped(tail_snap_b_ + ((size_t) (row_base_ + t) * nQall + qi) * TS, slot_ss(t).qsa_states[qi].idx_tail,
+ copy_from_mapped(tail_snap_b_ + ((size_t) brow_[t] * nQall + qi) * TS, slot_ss(t).qsa_states[qi].idx_tail,
TS, cs);
} else
if (grp == 0) copy_from_mapped(tail_snap_ + (size_t) qi * TS, st.idx_tail, TS, cs);
@@ -1606,15 +1606,16 @@ bool Verifier::init_slots(const std::vector<SessionState*>& slots, std::string&
return true;
}
-bool Verifier::capture_batch(int base, int S, std::string& err) {
- cudaGraphExec_t& ex = exec_bm_[base * 16 + S];
+bool Verifier::capture_batch(const int* rows, int S, int hbase, std::string& err) {
+ cudaGraphExec_t& ex = exec_bm_[batch_key(rows, S, hbase)];
if (ex != nullptr) return true;
if (cudaStreamBeginCapture(cs_, cudaStreamCaptureModeThreadLocal) != cudaSuccess) {
err = "verify: begin batch capture failed";
return false;
}
batch_rec_ = true;
- row_base_ = base;
+ row_base_ = hbase;
+ for (int t = 0; t < S; ++t) brow_[t] = rows[t];
std::string rerr;
const bool ok = record_window(S, cs_, rerr);
batch_rec_ = false;
@@ -1631,12 +1632,14 @@ bool Verifier::capture_batch(int base, int S, std::string& err) {
if (ie != cudaSuccess) { err = std::string("verify: batch instantiate: ") + cudaGetErrorString(ie); return false; }
cudaGraphUpload(ex, cs_);
cudaStreamSynchronize(cs_);
- std::fprintf(stderr, "strata verify: captured the batch window over slots [%d, %d)\n", base, base + S);
+ std::string list;
+ for (int t = 0; t < S; ++t) list += (t ? "," : "") + std::to_string(rows[t]);
+ std::fprintf(stderr, "strata verify: captured the batch window over slots %s\n", list.c_str());
return true;
}
-bool Verifier::capture_commit_batch(int base, int S, std::string& err) {
- cudaGraphExec_t& cex = commit_bm_[base * 16 + S];
+bool Verifier::capture_commit_batch(const int* rows, int S, int hbase, std::string& err) {
+ cudaGraphExec_t& cex = commit_bm_[batch_key(rows, S, hbase)];
if (cex != nullptr) return true;
using namespace strata::kernels;
const ModelGeometry& g = *g_;
@@ -1653,7 +1656,8 @@ bool Verifier::capture_commit_batch(int base, int S, std::string& err) {
}
bool ok = true;
try {
- copy_i32_from_mapped(commitb_ + (size_t) base * CB, m_commitb_ + (size_t) base * CB, S * CB, cs_);
+ for (int t = 0; t < S; ++t)
+ copy_i32_from_mapped(commitb_ + (size_t) rows[t] * CB, m_commitb_ + (size_t) rows[t] * CB, CB, cs_);
int64_t qsa_index = 0, gdn_index = 0;
for (int64_t l = 0; l < lb_; ++l) (is_qsa_layer(g, l) ? qsa_index : gdn_index) += 1;
for (int64_t l = lb_; l < le_ && ok; ++l) {
@@ -1662,8 +1666,8 @@ bool Verifier::capture_commit_batch(int base, int S, std::string& err) {
const WeightRef* wnm = need(v, "ssm_norm.weight", err);
if (!wnm) { ok = false; break; }
for (int t = 0; t < S; ++t) {
- SessionState& sx = *slots_[(size_t) (base + t)];
- const int32_t* keep = commitb_ + (size_t) (base + t) * CB;
+ SessionState& sx = *slots_[(size_t) rows[t]];
+ const int32_t* keep = commitb_ + (size_t) rows[t] * CB;
float* state = sx.gdn_state + (size_t) (gdn_index - sx.gdn_ord0) * gdn_floats;
float* conv = state + (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size;
gdn_conv_commit(conv, qkv_L_ + (size_t) gdn_index * MT * C + (size_t) t * C, (int) C, keep, cs_);
@@ -1678,10 +1682,10 @@ bool Verifier::capture_commit_batch(int base, int S, std::string& err) {
const WeightRef* wikn = need(v, "indexer.k_norm.weight", err);
if (!wikn) { ok = false; break; }
for (int t = 0; t < S; ++t) {
- const QsaState& st = slots_[(size_t) (base + t)]->qsa_states[qsa_index];
- copy_from_mapped(st.idx_tail, tail_snap_b_ + ((size_t) (base + t) * nQ + qsa_index) * TS, TS, cs_);
+ const QsaState& st = slots_[(size_t) rows[t]]->qsa_states[qsa_index];
+ copy_from_mapped(st.idx_tail, tail_snap_b_ + ((size_t) rows[t] * nQ + qsa_index) * TS, TS, cs_);
const QsaIndexerBuffers ib{st.idx_tail, st.idx_dead, st.idx_pooled, st.idx_block_pos};
- native_qsa_indexer_append(idx_raw_L_ + (size_t) (qsa_index * MT + t) * ID, commitb_ + (size_t) (base + t) * CB + 2,
+ native_qsa_indexer_append(idx_raw_L_ + (size_t) (qsa_index * MT + t) * ID, commitb_ + (size_t) rows[t] * CB + 2,
0, (const float*) wikn->data, EPS, ib, s, st.max_cells, rope_scaling(), cs_);
}
++qsa_index;
@@ -1689,8 +1693,8 @@ bool Verifier::capture_commit_batch(int base, int S, std::string& err) {
}
if (ok && ss_->ple.ready() && ple_stage())
for (int t = 0; t < S; ++t)
- copy_indexed(slots_[(size_t) (base + t)]->ple_hist, hist_snap_ + (size_t) t * HS, HS,
- commitb_ + (size_t) (base + t) * CB + 1, HS, cs_);
+ copy_indexed(slots_[(size_t) rows[t]]->ple_hist, hist_snap_ + (size_t) t * HS, HS,
+ commitb_ + (size_t) rows[t] * CB + 1, HS, cs_);
} catch (const std::exception& e) {
err = std::string("verify batch commit: ") + e.what();
ok = false;
@@ -1710,17 +1714,26 @@ bool Verifier::capture_commit_batch(int base, int S, std::string& err) {
return true;
}
-bool Verifier::stage_batch(int base, int S, const int32_t* tokens, const int64_t* pos, std::string& err) {
+bool Verifier::stage_batch(const int* rows, int S, int hbase, const int32_t* tokens, const int64_t* pos,
+ std::string& err) {
using namespace strata::kernels;
- if (S < 1 || base < 0 || base + S > (int) slots_.size()) { err = "verify: batch rows out of range (init_slots)"; return false; }
+ if (S < 1 || S > max_t_ || hbase < 0 || hbase + S > (int) slots_.size()) {
+ err = "verify: batch rows out of range (init_slots)";
+ return false;
+ }
+ for (int t = 0; t < S; ++t) {
+ bool dup = false;
+ for (int u = 0; u < t; ++u) dup = dup || rows[u] == rows[t];
+ if (rows[t] < 0 || rows[t] >= (int) slots_.size() || dup) { err = "verify: a batch row's slot is out of range or twice"; return false; }
+ }
if (released_.load()) { err = "verify: an earlier window never finished on the GPU (#267); restart the engine"; return false; }
const ModelGeometry& g = *g_;
for (int t = 0; t < S; ++t)
- if (pos[t] < 0 || pos[t] + 1 > slots_[(size_t) (base + t)]->max_cells) {
- err = "verify: slot " + std::to_string(base + t) + " runs past its context";
+ if (pos[t] < 0 || pos[t] + 1 > slots_[(size_t) rows[t]]->max_cells) {
+ err = "verify: slot " + std::to_string(rows[t]) + " runs past its context";
return false;
}
- if (!capture_batch(base, S, err) || !capture_commit_batch(base, S, err)) return false;
+ if (!capture_batch(rows, S, hbase, err) || !capture_commit_batch(rows, S, hbase, err)) return false;
const Clock::time_point t0 = Clock::now();
const QsaShapes s = shapes_of(g);
for (int t = 0; t < S; ++t) {
@@ -1733,18 +1746,18 @@ bool Verifier::stage_batch(int base, int S, const int32_t* tokens, const int64_t
for (int64_t h = 0; h < g.idx_q_heads; ++h) pi[t * g.idx_q_heads + h] = (int32_t) pos[t];
}
if (ss_->ple.ready() && ple_stage()) {
- uint32_t rows[kVerifyMaxT * PLE_N_HEADS];
+ uint32_t ple_rows[kVerifyMaxT * PLE_N_HEADS]; // (not `rows`: that is the slots of the window's rows)
for (int t = 0; t < S; ++t) {
- const SessionState& sx = *slots_[(size_t) (base + t)];
+ const SessionState& sx = *slots_[(size_t) rows[t]];
int32_t prev[2] = {sx.ple_prev[0], sx.ple_prev[1]};
- ngram_rows(&tokens[t], prev, 1, ss_->ple.consts, rows + t * PLE_N_HEADS);
+ ngram_rows(&tokens[t], prev, 1, ss_->ple.consts, ple_rows + t * PLE_N_HEADS);
}
- if (!ss_->ple.table->gather_batch(rows, (size_t) S, h_ple_, err)) return false;
+ if (!ss_->ple.table->gather_batch(ple_rows, (size_t) S, h_ple_, err)) return false;
}
// the commit's rows (a batch window keeps every row): [1, 0, position, -1 ..] per slot
const int64_t CB = 2 + max_t_;
for (int t = 0; t < S; ++t) {
- int32_t* c = h_commitb_ + (size_t) (base + t) * CB;
+ int32_t* c = h_commitb_ + (size_t) rows[t] * CB;
c[0] = 1;
c[1] = 0;
c[2] = (int32_t) pos[t];
@@ -1756,7 +1769,8 @@ bool Verifier::stage_batch(int base, int S, const int32_t* tokens, const int64_t
*(volatile uint32_t*) h_flagB_ = 0;
std::atomic_thread_fence(std::memory_order_seq_cst);
last_t_ = S;
- last_base_ = base;
+ for (int t = 0; t < S; ++t) last_rows_[t] = rows[t];
+ row_base_ = hbase; // the graphs' key (and nothing else) reads it until the next stage_batch
last_batch_ = true;
for (int t = 0; t < S; ++t) { last_tokens_[t] = tokens[t]; last_pos_b_[t] = pos[t]; }
ms_host += ms_since(t0);
@@ -1765,11 +1779,18 @@ bool Verifier::stage_batch(int base, int S, const int32_t* tokens, const int64_t
bool Verifier::run_slots(int S, const int32_t* tokens, const int64_t* pos, PoolMultiFn pool, void* user, int32_t* out,
std::string& err) {
+ int rows[8] = {};
+ for (int t = 0; t < S && t < 8; ++t) rows[t] = t;
+ return run_slot_rows(rows, S, tokens, pos, pool, user, out, err);
+}
+
+bool Verifier::run_slot_rows(const int* rows, int S, const int32_t* tokens, const int64_t* pos, PoolMultiFn pool,
+ void* user, int32_t* out, std::string& err) {
using namespace strata::kernels;
const OnDevice on_device(device_);
const ModelGeometry& g = *g_;
- if (!stage_batch(0, S, tokens, pos, err)) return false;
- const cudaError_t le = cudaGraphLaunch(exec_bm_[0 * 16 + S], cs_);
+ if (!stage_batch(rows, S, 0, tokens, pos, err)) return false;
+ const cudaError_t le = cudaGraphLaunch(exec_bm_[batch_key(rows, S, 0)], cs_);
if (le != cudaSuccess) { err = std::string("verify: batch launch: ") + cudaGetErrorString(le); return false; }
(void) cudaStreamQuery(cs_);
volatile uint32_t* const seq = h_seq_;
@@ -1827,8 +1848,8 @@ bool Verifier::run_slots(int S, const int32_t* tokens, const int64_t* pos, PoolM
cudaStreamSynchronize(copy_);
if (prof_on_) collect_profile();
++windows;
- if (le_ < g.n_layers) return next_ == nullptr || next_->run_slots(S, tokens, pos, pool, next_user_, out, err);
- if (!sample_rows(0, S, err)) return false;
+ if (le_ < g.n_layers) return next_ == nullptr || next_->run_slot_rows(rows, S, tokens, pos, pool, next_user_, out, err);
+ if (!sample_rows(S, err)) return false;
for (int t = 0; t < S; ++t) out[t] = ((volatile int32_t*) h_out_)[t];
progress_at("decode");
progress_beat();
@@ -1841,13 +1862,13 @@ bool Verifier::commit_slots(std::string& err) {
const int S = last_t_;
const Clock::time_point t0 = Clock::now();
std::atomic_thread_fence(std::memory_order_seq_cst);
- const cudaError_t le = cudaGraphLaunch(commit_bm_[last_base_ * 16 + S], cs_);
+ const cudaError_t le = cudaGraphLaunch(commit_bm_[batch_key(last_rows_, S, row_base_)], cs_);
if (le != cudaSuccess) { err = std::string("verify: batch commit launch: ") + cudaGetErrorString(le); return false; }
const cudaError_t se = cudaStreamSynchronize(cs_);
if (se != cudaSuccess) { err = std::string("verify: batch commit: ") + cudaGetErrorString(se); return false; }
if (ple_stage())
for (int t = 0; t < S; ++t) {
- SessionState& sx = *slots_[(size_t) (last_base_ + t)];
+ SessionState& sx = *slots_[(size_t) last_rows_[t]];
sx.ple_prev[0] = sx.ple_prev[1];
sx.ple_prev[1] = last_tokens_[t];
}
@@ -1855,10 +1876,10 @@ bool Verifier::commit_slots(std::string& err) {
return next_ == nullptr || next_->commit_slots(err);
}
-bool Verifier::sample_rows(int base, int S, std::string& err) {
+bool Verifier::sample_rows(int S, std::string& err) {
bool any = false;
for (int t = 0; t < S; ++t) {
- strata::kernels::SamplerParams sp = slot_sp_[(size_t) (base + t)];
+ strata::kernels::SamplerParams sp = slot_sp_[(size_t) last_rows_[t]];
if (sp.greedy || sp.temperature <= 0.0f) continue;
sp.counter = (uint64_t) last_pos_b_[t]; // Philox(seed, position): the solo window's draw for this position
sp.penalty_last_n = 0;
@@ -1873,14 +1894,16 @@ bool Verifier::sample_rows(int base, int S, std::string& err) {
bool Verifier::batch_launch(int base, int S, const int32_t* tokens, const int64_t* pos, std::string& err) {
const OnDevice on_device(device_);
if (b_running_) { err = "verify: batch_launch while this stage is busy"; return false; }
- if (!stage_batch(base, S, tokens, pos, err)) return false;
- cudaError_t le = cudaGraphLaunch(exec_bm_[base * 16 + S], cs_);
- if (le == cudaSuccess) le = cudaGraphLaunch(commit_bm_[base * 16 + S], cs_); // right behind it: every row is kept
+ int rows[8] = {};
+ for (int t = 0; t < S && t < 8; ++t) rows[t] = base + t;
+ if (!stage_batch(rows, S, base, tokens, pos, err)) return false;
+ cudaError_t le = cudaGraphLaunch(exec_bm_[batch_key(rows, S, base)], cs_);
+ if (le == cudaSuccess) le = cudaGraphLaunch(commit_bm_[batch_key(rows, S, base)], cs_); // right behind it: every row is kept
if (le != cudaSuccess) { err = std::string("verify: batch launch: ") + cudaGetErrorString(le); return false; }
(void) cudaStreamQuery(cs_);
if (ple_stage()) // the host's side of the commit (the hash's last two tokens)
for (int t = 0; t < S; ++t) {
- SessionState& sx = *slots_[(size_t) (base + t)];
+ SessionState& sx = *slots_[(size_t) rows[t]];
sx.ple_prev[0] = sx.ple_prev[1];
sx.ple_prev[1] = tokens[t];
}
@@ -1945,7 +1968,7 @@ int Verifier::batch_poll(PoolMultiFn pool, void* user, std::string& err) {
if (qc == cudaErrorNotReady) return 0;
if (prof_on_) collect_profile();
if (last_stage()) {
- if (!sample_rows(last_base_, S, err)) { b_running_ = false; return -1; }
+ if (!sample_rows(S, err)) { b_running_ = false; return -1; }
for (int t = 0; t < S; ++t) b_out_[t] = ((volatile int32_t*) h_out_)[t];
}
++windows;
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index 9978a45e4..270a73583 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -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) {
+ 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) {
- 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;
- }
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);