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++
Multi-GPU: the session carve and the per-stage prompt loans
Gopi915f74fefb0f
diff --git a/include/strata/core/session.hpp b/include/strata/core/session.hppindex e9c39ced2..c05d7e991 100644--- a/include/strata/core/session.hpp+++ b/include/strata/core/session.hpp@@ -45,6 +45,20 @@ struct SessionState {float* R = nullptr; ///< alias of `block.R`, named for what it means at this levelint64_t k = 10; ///< experts per token++ // **THE LAYER-RANGE CARVE (multi-GPU).** A split stage's session used to carve state for ALL 48 layers+ // (every QSA KV pool at max_cells, every GDN recurrence) while running only its slice - the same+ // whole-model-sized-buffer disease the QSA chunked-prefill PR cured upstream. A session now owns+ // [layer_lo, layer_hi): `qsa_states`/`gdn_state` keep GLOBAL ordinal indexing, the arrays simply hold+ // fewer rows, so per-layer consumers subtract `gdn_ord0`, and every old model-level `qsa_states[0]` use+ // (the shared RoPE table, the prefill staging identity, the MTP rope borrow) reads `qsa_states[primary]`.+ // A range with no QSA layer still carves ONE primary state so those model-level uses stay valid.+ int64_t layer_lo = 0, layer_hi = 0; ///< resolved by `session_init` (hi = n_layers for the full range)+ int64_t qsa_ord0 = 0; ///< global QSA ordinal of the first allocated state+ int64_t qsa_alloc = 0; ///< allocated QSA states (>= 1 whenever the model has any)+ int64_t gdn_ord0 = 0; ///< global GDN ordinal of `gdn_state` row 0+ int64_t gdn_alloc = 0; ///< allocated GDN rows+ int qsa_primary() const { return (int) qsa_ord0; }/// The doorbell the host loop polls. Null until a caller provides one - the engine runs without it, and/// neither `session_token` nor `session_replay` looks at it.const Doorbell* db = nullptr;@@ -76,9 +90,15 @@ struct SessionState {/// Bytes for a whole session at `max_cells` of context. Every layer's state is sized at once, because P2.T10/// requires ZERO token-path allocations - a `cudaMalloc` that happened on the first token of a longer sequence/// would satisfy every test here and fail that one.-uint64_t session_bytes(const ModelGeometry& g, int64_t max_cells, int64_t k);-/// Carves `base` (DEVICE memory) into `s`. Returns the bytes used.-uint64_t session_init(const ModelGeometry& g, int64_t max_cells, int64_t k, void* base, SessionState& s);+///+/// [layer_lo, layer_hi) carves only that range's per-layer state (a split stage runs a slice of the model);+/// the default full range is byte-identical to the old whole-model carve. Pure arithmetic - safe to call for+/// a candidate range before anything is allocated, which is how the layer-split search prices a placement.+uint64_t session_bytes(const ModelGeometry& g, int64_t max_cells, int64_t k, int64_t layer_lo = 0,+ int64_t layer_hi = -1);+/// Carves `base` (DEVICE memory) into `s`. Returns the bytes used. Same range convention as `session_bytes`.+uint64_t session_init(const ModelGeometry& g, int64_t max_cells, int64_t k, void* base, SessionState& s,+ int64_t layer_lo = 0, int64_t layer_hi = -1);/// Zeroes every layer's state - the residual to `R_init`, everything else to zero, so a fresh sequence starts/// from the reference's own `zeros()`.void session_zero(SessionState& s, const ModelGeometry& g, const float* R_init, void* stream);@@ -207,7 +227,8 @@ struct SessionGraphs {/// replay re-reads every token - so the position is data, not an argument. That is the whole reason rounds/// 211-214 moved the per-token counts into device buffers.bool session_capture(const WeightTable& tables, const ModelGeometry& g, SessionState& s, const float* parts,- SessionGraphs& gr, std::string& err, bool split = false);+ SessionGraphs& gr, std::string& err, bool split = false, int64_t layer_lo = 0,+ int64_t layer_hi = -1);/// One token by REPLAYING the captured graphs. Identical arithmetic to `session_token`; the only difference is/// that ~2,000 kernel launches become 48 graph launches.diff --git a/src/core/mtp.cpp b/src/core/mtp.cppindex 2b1d79ef8..e73b58b13 100644--- a/src/core/mtp.cpp+++ b/src/core/mtp.cpp@@ -196,7 +196,7 @@ bool MtpDrafter::load(const std::string& rt_dir, const ModelGeometry& g, Session// ---- the layer's own K/V (dense attention: no indexer state is read)const strata::kernels::QsaShapes s = shapes_of(g);- const int64_t max_cells = ss.qsa_states[0].max_cells;+ const int64_t max_cells = ss.qsa_states[ss.qsa_primary()].max_cells;// KV streaming: the drafter only reads its last `window` cells, so with streaming on its K/V is a ring of the// window (plus the cells a round writes ahead of its queries) over a host copy, refilled on a resume. The host copy// is pinned after the expert arena has pinned what it could: if it does not fit, the K/V stays whole in VRAM.@@ -209,7 +209,7 @@ bool MtpDrafter::load(const std::string& rt_dir, const ModelGeometry& g, Sessionif (kv_hybrid_was) qsa_set_kv_int8(true); // the drafter under --kv k8v4: plain INT8uint64_t sb = qsa_state_bytes(g, max_cells, false, ring);if (cudaMalloc(&state_arena_, sb) != cudaSuccess) { err = "mtp: the K/V state does not fit"; return false; }- if (qsa_state_init(g, max_cells, state_arena_, st_, &ss.qsa_states[0], ring) == 0) {+ if (qsa_state_init(g, max_cells, state_arena_, st_, &ss.qsa_states[ss.qsa_primary()], ring) == 0) {if (st_.kv_mode == 0) { err = "mtp: state init failed"; return false; }std::fprintf(stderr, "strata mtp: no pinned RAM left for the draft layer's K/V copy; keeping it in VRAM\n");cudaGetLastError();@@ -218,7 +218,7 @@ bool MtpDrafter::load(const std::string& rt_dir, const ModelGeometry& g, Sessionring = -1; // fully residentsb = qsa_state_bytes(g, max_cells, false, ring);if (cudaMalloc(&state_arena_, sb) != cudaSuccess) { err = "mtp: the K/V state does not fit"; return false; }- if (qsa_state_init(g, max_cells, state_arena_, st_, &ss.qsa_states[0], ring) == 0) { err = "mtp: state init failed"; return false; }+ if (qsa_state_init(g, max_cells, state_arena_, st_, &ss.qsa_states[ss.qsa_primary()], ring) == 0) { err = "mtp: state init failed"; return false; }}qsa_set_kv_int8(kv_int8_was);qsa_set_kv_hybrid(kv_hybrid_was);diff --git a/src/core/session.cpp b/src/core/session.cppindex d18a5ec38..2211e9ba5 100644--- a/src/core/session.cpp+++ b/src/core/session.cpp@@ -50,13 +50,20 @@ static uint64_t ple_hist_bytes() {return (uint64_t) strata::kernels::NG_HIST * strata::kernels::NG_HC_DIM * sizeof(float);}-uint64_t session_bytes(const ModelGeometry& g, int64_t max_cells, int64_t k) {+uint64_t session_bytes(const ModelGeometry& g, int64_t max_cells, int64_t k, int64_t layer_lo, int64_t layer_hi) {+ if (layer_hi < 0 || layer_hi > g.n_layers) layer_hi = g.n_layers;+ if (layer_lo < 0) layer_lo = 0;+ // QSA layers are `l % interval == interval-1`, so exactly `bound / interval` of them live below `bound`+ const int64_t I = std::max<int64_t>(g.qsa_interval, 1);+ const int64_t q_lo = layer_lo / I, q_hi = layer_hi / I;+ const int64_t q_n = std::max<int64_t>(q_hi - q_lo, g.n_qsa_layers() > 0 ? 1 : 0);+ const int64_t gdn_n = std::max<int64_t>((layer_hi - layer_lo) - std::max<int64_t>(q_hi - q_lo, 0), 0);uint64_t n = 0;n += gdn_buffers_bytes(g);- n += (uint64_t) g.n_gdn_layers() * gdn_state_floats(g) * 4;+ n += (uint64_t) gdn_n * gdn_state_floats(g) * 4;// One QSA state carries the RoPE table; the others borrow it (P7: 64 MiB per layer at 262K).if (g.n_qsa_layers() > 0)- n += qsa_state_bytes(g, max_cells, true) + (uint64_t) (g.n_qsa_layers() - 1) * qsa_state_bytes(g, max_cells, false);+ n += qsa_state_bytes(g, max_cells, true) + (uint64_t) (q_n - 1) * qsa_state_bytes(g, max_cells, false);n += qsa_buffers_bytes(g, max_cells);n += moe_buffers_bytes(g, k);n += block_buffers_bytes(g);@@ -64,7 +71,10 @@ uint64_t session_bytes(const ModelGeometry& g, int64_t max_cells, int64_t k) {return align_up(n, SESSION_STATE_ALIGN) + 4096;}-uint64_t session_init(const ModelGeometry& g, int64_t max_cells, int64_t k, void* base, SessionState& s) {+uint64_t session_init(const ModelGeometry& g, int64_t max_cells, int64_t k, void* base, SessionState& s,+ int64_t layer_lo, int64_t layer_hi) {+ if (layer_hi < 0 || layer_hi > g.n_layers) layer_hi = g.n_layers;+ if (layer_lo < 0) layer_lo = 0;uint8_t* p = (uint8_t*) base;uint64_t used = 0;auto take = [&](uint64_t bytes) {@@ -75,24 +85,34 @@ uint64_t session_init(const ModelGeometry& g, int64_t max_cells, int64_t k, voids.max_cells = max_cells;s.k = k;+ s.layer_lo = layer_lo;+ s.layer_hi = layer_hi;+ const int64_t I = std::max<int64_t>(g.qsa_interval, 1);+ const int64_t q_n_range = layer_hi / I - layer_lo / I;+ s.qsa_ord0 = layer_lo / I;+ s.qsa_alloc = std::max<int64_t>(q_n_range, g.n_qsa_layers() > 0 ? 1 : 0);+ s.gdn_ord0 = layer_lo - layer_lo / I;+ s.gdn_alloc = std::max<int64_t>((layer_hi - layer_lo) - q_n_range, 0);gdn_buffers_init(g, take(gdn_buffers_bytes(g)), s.gdn);- s.gdn_state = (float*) take((uint64_t) g.n_gdn_layers() * gdn_state_floats(g) * 4);+ s.gdn_state = (float*) take((uint64_t) s.gdn_alloc * gdn_state_floats(g) * 4);- // the 12 QSA states are separate allocations carved from one arena, because `QsaState` is a struct of+ // the QSA states are separate allocations carved from one arena, because `QsaState` is a struct of// pointers and `qsa_state_init` writes them - a contiguous array would need the arena to be laid out the- // same way, which is a coupling with nothing to gain.+ // same way, which is a coupling with nothing to gain. Only the range's ordinals are initialized; the+ // rest stay value-initialized nulls. The FIRST ALLOCATED one (the session's primary, ordinal qsa_ord0)+ // owns the RoPE table that the others - and the prefill staging identity, and the MTP drafter - borrow.const uint64_t first = qsa_state_bytes(g, max_cells, true), rest = qsa_state_bytes(g, max_cells, false);- s.qsa_state_arena = take(g.n_qsa_layers() > 0 ? first + (uint64_t) (g.n_qsa_layers() - 1) * rest : 0);- s.qsa_states = new QsaState[(size_t) g.n_qsa_layers()];+ s.qsa_state_arena = take(g.n_qsa_layers() > 0 ? first + (uint64_t) (s.qsa_alloc - 1) * rest : 0);+ s.qsa_states = new QsaState[(size_t) g.n_qsa_layers()]();s.qsa_buf_arena = take(qsa_buffers_bytes(g, max_cells));uint8_t* qp = (uint8_t*) s.qsa_state_arena;// a layer whose pinned RAM could not be had (KV streaming's host copy) is half-built: going on would have the// attention read null host pointers at the first request ("illegal memory access"), so the session fails here- for (int64_t i = 0; i < g.n_qsa_layers(); ++i)- if (qsa_state_init(g, max_cells, qp + (i == 0 ? 0 : first + (uint64_t) (i - 1) * rest), s.qsa_states[i],- i == 0 ? nullptr : &s.qsa_states[0]) == 0)+ for (int64_t j = 0; j < s.qsa_alloc; ++j)+ if (qsa_state_init(g, max_cells, qp + (j == 0 ? 0 : first + (uint64_t) (j - 1) * rest),+ s.qsa_states[s.qsa_ord0 + j], j == 0 ? nullptr : &s.qsa_states[s.qsa_ord0]) == 0)return 0;qsa_buffers_init(g, max_cells, s.qsa_buf_arena, s.qsa_bufs);@@ -118,10 +138,10 @@ void session_zero(SessionState& s, const ModelGeometry& g, const float* R_init,} else {cudaMemsetAsync(s.block.R, 0, (size_t) g.hc * g.n_embd * 4, cs);}- // every GDN layer's recurrence and conv history- cudaMemsetAsync(s.gdn_state, 0, (size_t) g.n_gdn_layers() * gdn_state_floats(g) * 4, cs);- // and every QSA layer's cache and indexer- for (int64_t i = 0; i < g.n_qsa_layers(); ++i) qsa_state_zero(s.qsa_states[i], g, stream);+ // every owned GDN layer's recurrence and conv history+ cudaMemsetAsync(s.gdn_state, 0, (size_t) s.gdn_alloc * gdn_state_floats(g) * 4, cs);+ // and every owned QSA layer's cache and indexer+ for (int64_t j = 0; j < s.qsa_alloc; ++j) qsa_state_zero(s.qsa_states[s.qsa_ord0 + j], g, stream);// **AND THE PLE'S CONV HISTORY AND TOKEN WINDOW.** A sequence that started with a warm history would// convolve over rows belonging to a different sequence - the conv reads NG_HIST previous NORMALIZED rows,// so a stale one is a real contribution and not a zero. The token window resets to `NG_HIST`-many nulls@@ -139,7 +159,7 @@ void gdn_point_at(const ModelGeometry& g, int64_t layer, SessionState& s) {if (is_qsa_layer(g, layer)) return;int64_t gdn_index = 0;for (int64_t l = 0; l < layer; ++l) if (!is_qsa_layer(g, l)) ++gdn_index;- s.gdn.state = s.gdn_state + (size_t) gdn_index * gdn_state_floats(g);+ s.gdn.state = s.gdn_state + (size_t) (gdn_index - s.gdn_ord0) * gdn_state_floats(g);s.gdn.conv_state = s.gdn.state + (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size;}@@ -152,36 +172,38 @@ void stage_token(const ModelGeometry& g, int64_t pos, int32_t pos_base, SessionSsh.head_dim = g.head_dim;sh.idx_n_head = g.idx_q_heads;sh.idx_dim = g.idx_key_dim;- for (int64_t i = 0; i < g.n_qsa_layers(); ++i) {- QsaState& q = s.qsa_states[i];+ for (int64_t j = 0; j < s.qsa_alloc; ++j) {+ QsaState& q = s.qsa_states[s.qsa_ord0 + j];qsa_step_fill(q.host_step, pos, sh);for (int64_t h = 0; h < g.n_head; ++h) q.host_pos[h] = (int32_t) (pos_base + pos);}}bool session_capture(const WeightTable& tables, const ModelGeometry& g, SessionState& s, const float* parts,- SessionGraphs& gr, std::string& err, bool split) {+ SessionGraphs& gr, std::string& err, bool split, int64_t layer_lo, int64_t layer_hi) {if (gr.captured) return true;- gr.execs = new cudaGraphExec_t[(size_t) g.n_layers];- gr.posts = new cudaGraphExec_t[(size_t) g.n_layers];+ if (layer_hi < 0 || layer_hi > g.n_layers) layer_hi = g.n_layers;+ if (layer_lo < 0) layer_lo = 0;+ gr.execs = new cudaGraphExec_t[(size_t) g.n_layers]();+ gr.posts = new cudaGraphExec_t[(size_t) g.n_layers]();for (int64_t i = 0; i < g.n_layers; ++i) gr.posts[i] = nullptr;if (split) {- gr.preA = new cudaGraphExec_t[(size_t) g.n_layers];- gr.preB = new cudaGraphExec_t[(size_t) g.n_layers];+ gr.preA = new cudaGraphExec_t[(size_t) g.n_layers]();+ gr.preB = new cudaGraphExec_t[(size_t) g.n_layers]();for (int64_t i = 0; i < g.n_layers; ++i) { gr.preA[i] = nullptr; gr.preB[i] = nullptr; }for (int k = 0; k < 5; ++k) {- gr.preP[k] = new cudaGraphExec_t[(size_t) g.n_layers];+ gr.preP[k] = new cudaGraphExec_t[(size_t) g.n_layers]();for (int64_t i = 0; i < g.n_layers; ++i) gr.preP[k][i] = nullptr;}}gr.split_captured = split;gr.n = 0;- int64_t qsa_index = 0;- for (int64_t l = 0; l < g.n_layers; ++l) {+ int64_t qsa_index = s.qsa_ord0;+ for (int64_t l = layer_lo; l < layer_hi; ++l) {gdn_point_at(g, l, s);const bool qsa = is_qsa_layer(g, l);- QsaState& qst = qsa ? s.qsa_states[qsa_index] : s.qsa_states[0];+ QsaState& qst = qsa ? s.qsa_states[qsa_index] : s.qsa_states[s.qsa_primary()];// **TWO GRAPHS PER LAYER, SPLIT AT THE ROUTER.** `pre` ends with the doorbell rung; the host then runs// the CPU pool on what it published; `post` combines those experts with THIS layer's weights. Capturingdiff --git a/src/core/verify.cpp b/src/core/verify.cppindex 085135a95..223d1753e 100644--- a/src/core/verify.cpp+++ b/src/core/verify.cpp@@ -174,7 +174,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&const strata::kernels::QsaShapes s = shapes_of(g);cap_ = strata::kernels::qsa_selection_width(strata::kernels::kTopkMaxCells, s);- max_blocks_ = ss.qsa_states[0].max_cells / s.idx_block + 2;+ max_blocks_ = ss.qsa_states[ss.qsa_primary()].max_cells / s.idx_block + 2;attn_scratch_floats_ = (int64_t) strata::kernels::qsa_decode_attn_scratch_floats(cap_, s);const uint64_t T = (uint64_t) max_t, N = (uint64_t) g.n_embd, HC = (uint64_t) g.hc, K = (uint64_t) ss.k;@@ -453,7 +453,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {!native_of(wout, v.name("ssm_out.weight"), err))return false;const int64_t gi = gdn_idx[(size_t) l];- float* state = ss.gdn_state + (size_t) gi * gdn_floats;+ float* state = ss.gdn_state + (size_t) (gi - ss.gdn_ord0) * gdn_floats;float* conv = state + (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size;float* qkv = qkv_L_ + (size_t) gi * MT * C;float* hb = h_L_ + (size_t) gi * MT * C;@@ -906,7 +906,7 @@ bool Verifier::capture_commit(std::string& err) {if (!is_qsa_layer(g, l)) {const WeightRef* wnm = need(v, "ssm_norm.weight", err);if (!wnm) { ok = false; break; }- float* state = ss.gdn_state + (size_t) gdn_index * gdn_floats;+ float* state = ss.gdn_state + (size_t) (gdn_index - ss.gdn_ord0) * gdn_floats;float* conv = state + (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size;const float* qkv = qkv_L_ + (size_t) gdn_index * MT * C;gdn_conv_commit(conv, qkv, (int) C, commit_, cs_);@@ -954,7 +954,7 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,if (T < 1 || T > max_t_) { err = "verify: window size out of range"; return false; }const ModelGeometry& g = *g_;SessionState& ss = *ss_;- if (pos0 + T > ss.qsa_states[0].max_cells) { err = "verify: the window runs past the context"; return false; }+ if (pos0 + T > ss.qsa_states[ss.qsa_primary()].max_cells) { err = "verify: the window runs past the context"; return false; }if (!capture(T, err) || !capture_commit(err)) return false;VDBG("captured; staging\n");const Clock::time_point t0 = Clock::now();diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cppindex 93e258a57..e2468f00d 100644--- a/src/prefill/prefill.cpp+++ b/src/prefill/prefill.cpp@@ -342,7 +342,7 @@ namespace {bool stage_own() { static const bool v = std::getenv("STRATA_KV_STAGE_OWN") != nullptr; return v; }void take_stage(Alloc& o_borrowed, const core::SessionState& ss, const strata::kernels::QsaShapes& s,strata::kernels::KvHostPools& st, bool& ok) {- const core::QsaState& q0 = ss.qsa_states[0];+ const core::QsaState& q0 = ss.qsa_states[ss.qsa_primary()];if (q0.kv_mode != 1) return;if (stage_own() && o_borrowed.count_only) return;Alloc own;@@ -546,8 +546,8 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, cook = false;m.ple_rows[b].resize(T * strata::kernels::PLE_N_HEADS);}- if (ss.qsa_states[0].kv_mode == 1) { // KV streaming: the staging pool's identity page table- const int64_t pages = ss.qsa_states[0].n_pages;+ if (ss.qsa_states[ss.qsa_primary()].kv_mode == 1) { // KV streaming: the staging pool's identity page table+ const int64_t pages = ss.qsa_states[ss.qsa_primary()].n_pages;std::vector<int32_t> ident((size_t) pages);for (int64_t i = 0; i < pages; ++i) ident[(size_t) i] = (int32_t) i;if (cudaMalloc((void**) &m.ident_table, ident.size() * 4) != cudaSuccess ||@@ -594,7 +594,7 @@ bool Prefill::carve(size_t T, void* alloc) {s.n_head = g.n_head; s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_n_head = g.idx_q_heads;s.idx_dim = g.idx_key_dim;m.cap = strata::kernels::qsa_selection_width(strata::kernels::kTopkMaxCells, s);- m.max_blocks = ss.qsa_states[0].max_cells / s.idx_block + 2;+ m.max_blocks = ss.qsa_states[ss.qsa_primary()].max_cells / s.idx_block + 2;{// one region for the attention half's and the MoE half's scratch (see gdn_set_bytes)const uint64_t region = std::max({gdn_set_bytes(T), qsa_set_bytes(T, m.cap, m.max_blocks, m.sel_batch,@@ -846,7 +846,7 @@ uint64_t Prefill::bytes_needed(const core::ModelGeometry& g, const core::Sessions.n_head = g.n_head; s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_n_head = g.idx_q_heads;s.idx_dim = g.idx_key_dim;const int64_t cap = strata::kernels::qsa_selection_width(strata::kernels::kTopkMaxCells, s);- const int64_t max_blocks = ss.qsa_states[0].max_cells / s.idx_block + 2;+ const int64_t max_blocks = ss.qsa_states[ss.qsa_primary()].max_cells / s.idx_block + 2;o.take<uint8_t>((size_t) std::max({gdn_set_bytes(T), qsa_set_bytes(T, cap, max_blocks, 256, 32, s),moe_set_bytes(T, g.n_expert)}), ok);for (int i = 0; i < DQ; ++i) { o.take<uint16_t>(1280 * 2560, ok); o.take<uint16_t>(2560 * 640, ok); }@@ -1279,7 +1279,7 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e*wsa = need(v, "ssm_a", err);if (!wqkv || !wg || !wo || !wa || !wb || !wc || !wnm || !wdt || !wsa) return false;pt.mark(kPfGdn, cs);- float* state = ss.gdn_state + (size_t) gdn_index * gdn_floats;+ float* state = ss.gdn_state + (size_t) (gdn_index - ss.gdn_ord0) * gdn_floats;float* conv = state + (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size;if (!native_proj(m.gemm, wqkv, m.mixed_h, m.qkv, T, v.name("attn_qkv.weight"), err)) return false;if (!native_proj(m.gemm, wg, m.mixed_h, m.z, T, v.name("attn_gate.weight"), err)) return false;@@ -1882,7 +1882,7 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& estd::vector<uint8_t> b((size_t) gdn_floats * 4);std::string line;char h[8];- for (int64_t i = 0; i < g.n_gdn_layers(); ++i) {+ for (int64_t i = 0; i < ss.gdn_alloc; ++i) {cudaMemcpy(b.data(), ss.gdn_state + (size_t) i * gdn_floats, b.size(), cudaMemcpyDeviceToHost);uint64_t x = 1469598103934665603ull;for (uint8_t c : b) x = (x ^ c) * 1099511628211ull;diff --git a/src/program/generate.cpp b/src/program/generate.cppindex d811728bf..1412652b3 100644--- a/src/program/generate.cpp+++ b/src/program/generate.cpp@@ -736,9 +736,10 @@ struct ConvStateSizes {size_t gdn = 0, ple = 0, tail = 0;};-ConvStateSizes conv_state_sizes(const strata::core::ModelGeometry& g) {+ConvStateSizes conv_state_sizes(const strata::core::ModelGeometry& g, const strata::core::SessionState& ss) {ConvStateSizes z;- z.gdn = (size_t) g.n_gdn_layers() *+ // a split stage's session owns only its layer range's state (see SessionState's carve note)+ z.gdn = (size_t) ss.gdn_alloc *((size_t) g.ssm_state_size * (size_t) g.ssm_v_heads * (size_t) g.ssm_state_size +(size_t) g.ssm_conv_channels * (size_t) (g.ssm_d_conv - 1)) * sizeof(float);z.ple = (size_t) strata::kernels::NG_HIST * (size_t) strata::kernels::NG_HC_DIM * sizeof(float);@@ -748,30 +749,30 @@ ConvStateSizes conv_state_sizes(const strata::core::ModelGeometry& g) {/// Copies the running state out. The caller has synchronized the device.bool checkpoint_save(ConvCheckpoint& c, const strata::core::SessionState& ss, const strata::core::ModelGeometry& g) {- const ConvStateSizes z = conv_state_sizes(g);+ const ConvStateSizes z = conv_state_sizes(g, ss);c.gdn.resize(z.gdn);c.ple.resize(ss.ple_hist != nullptr ? z.ple : 0);- c.tails.resize(z.tail * (size_t) g.n_qsa_layers());+ c.tails.resize(z.tail * (size_t) ss.qsa_alloc);if (cudaMemcpy(c.gdn.data(), ss.gdn_state, z.gdn, cudaMemcpyDeviceToHost) != cudaSuccess) return false;if (!c.ple.empty() && cudaMemcpy(c.ple.data(), ss.ple_hist, z.ple, cudaMemcpyDeviceToHost) != cudaSuccess)return false;- for (int64_t i = 0; i < g.n_qsa_layers(); ++i)- if (cudaMemcpy(c.tails.data() + (size_t) i * z.tail, ss.qsa_states[i].idx_tail, z.tail, cudaMemcpyDeviceToHost) !=- cudaSuccess)+ for (int64_t j = 0; j < ss.qsa_alloc; ++j)+ if (cudaMemcpy(c.tails.data() + (size_t) j * z.tail, ss.qsa_states[ss.qsa_ord0 + j].idx_tail, z.tail,+ cudaMemcpyDeviceToHost) != cudaSuccess)return false;return true;}/// Puts a checkpoint's running state back; the positional cells below it are the caller's to guarantee.bool checkpoint_restore(const ConvCheckpoint& c, strata::core::SessionState& ss, const strata::core::ModelGeometry& g) {- const ConvStateSizes z = conv_state_sizes(g);- if (c.gdn.size() != z.gdn || c.tails.size() != z.tail * (size_t) g.n_qsa_layers()) return false;+ const ConvStateSizes z = conv_state_sizes(g, ss);+ if (c.gdn.size() != z.gdn || c.tails.size() != z.tail * (size_t) ss.qsa_alloc) return false;if (cudaMemcpy(ss.gdn_state, c.gdn.data(), z.gdn, cudaMemcpyHostToDevice) != cudaSuccess) return false;if (!c.ple.empty() && cudaMemcpy(ss.ple_hist, c.ple.data(), z.ple, cudaMemcpyHostToDevice) != cudaSuccess)return false;- for (int64_t i = 0; i < g.n_qsa_layers(); ++i)- if (cudaMemcpy(ss.qsa_states[i].idx_tail, c.tails.data() + (size_t) i * z.tail, z.tail, cudaMemcpyHostToDevice) !=- cudaSuccess)+ for (int64_t j = 0; j < ss.qsa_alloc; ++j)+ if (cudaMemcpy(ss.qsa_states[ss.qsa_ord0 + j].idx_tail, c.tails.data() + (size_t) j * z.tail, z.tail,+ cudaMemcpyHostToDevice) != cudaSuccess)return false;// the PLE's token window is the last two tokens, OLDEST FIRST, -1 where there is none (as session_zero leaves it)const size_t L = c.ids.size();@@ -1167,7 +1168,15 @@ int main(int argc, char** argv) {std::fprintf(stderr, "strata generate: a layer split across GPUs needs --expert-profile\n");return 2;}- o.no_prefill_borrow = true; // each stage's prompt path has its own buffers+ // A STAGE'S PROMPT PATH BORROWS FROM THAT STAGE'S OWN EXPERT CACHE. This used to set+ // `no_prefill_borrow = true` - "each stage's prompt path has its own buffers" - which is true, but it is+ // a reason to give each stage its own LOAN, not a reason to make every stage withhold a chunk-sized+ // reserve from its cache for the whole session. Forced on, it also collapsed `--prefill auto` to 2048+ // (below) and skipped the lend arm, so a stage paid for its prompt buffers twice over: once in VRAM it+ // never got back, once in the smaller chunk. At `--prefill 5524` that reserve is 3.8 GiB per stage,+ // more than either 8 GB card had - which is how adding two GPUs to the two 12 GB ones lost 260K.+ // The loan is the tail of the stage's own cache (see `PfPart` in the serve block); outside the prompt+ // that tail is expert cache, so a large chunk costs a stage nothing permanent.int n_vis = 1;if (cudaGetDeviceCount(&n_vis) != cudaSuccess || n_vis < 1) n_vis = 1;cudaGetLastError();@@ -1310,6 +1319,14 @@ int main(int argc, char** argv) {std::fprintf(stderr, "strata generate: --native-flash-attn-short requires the normal decode loop for status validation\n");return 2;}+ // The whole-model graph measurements replay every layer through CUDA0's session, which a layer split carves to+ // CUDA0's own range - the answer would read another stage's state. Refused here rather than at the call, so+ // the reason is visible before 55 GB is loaded.+ if (multi_gpu && (o.gpu_only_full || o.gpu_stages)) {+ std::fprintf(stderr, "strata generate: --gpu-only-full and --gpu-stages replay the whole model through one "+ "session, which a layer split does not have; run them without --layer-split\n");+ return 2;+ }if (o.native_ple_key && (o.native_dense_gguf.empty() || o.no_ple)) {std::fprintf(stderr, "strata generate: --native-ple-key requires PLE and --native-dense-gguf\n");return 2;@@ -1514,12 +1531,8 @@ int main(int argc, char** argv) {return 2;}- void* sbuf = nullptr;- if (cudaMalloc(&sbuf, strata::core::session_bytes(g, o.max_context, K)) != cudaSuccess) {- std::fprintf(stderr, "strata generate: session state allocation failed\n");- return 1;- }strata::core::SessionState ss;+ void* sbuf = nullptr; // allocated after the layer-split search, sized to CUDA0's own layer range (the carve)// **THE ENGINE RAN ON THE LEGACY DEFAULT STREAM, WHICH ON WDDM IS THE SLOW PATH.** All four session// calls - `session_capture`, `session_replay`, `session_token` and `session_loop` - were handed `nullptr`,// i.e. stream 0. `bench/micro/kernel_costs.cu` measures what that costs: EVERY kernel it launches through@@ -1533,29 +1546,6 @@ int main(int argc, char** argv) {return 1;}void* const main_cs = (void*) main_stream;- if (strata::core::session_init(g, o.max_context, K, sbuf, ss) == 0) {- std::fprintf(stderr, "strata generate: session_init failed\n");- return 1;- }- // **A GENERATE RUN ON A NATIVE PACK NEVER ZEROED THE SESSION STATE.** The canonical path zeroes it from- // `put_input` at position 0 (`session_zero`, with the embedding as `R`), but a native pack breaks out of that- // loop before the first `put_input` - it runs verify windows instead - and neither the batched prompt path- // nor the verifier zeroes anything. `sbuf` is `cudaMalloc`'d, so the GDN recurrence, the QSA KV/indexer- // state, the PLE history and `R` all started from whatever the allocator last held: finite on a fresh- // allocation and overflow/NaN after reuse, which surfaced as the whole layer stack saturating and every- // prompt decoding to the same token. `--serve` zeroes exactly this state when `resume == 0`; generate mode- // always starts from an empty sequence, so it must too.- // (#167) Every pack, not only a native one: a canonical pack whose prompt goes through the batched prompt path- // (--prefill) starts at position 0 without a put_input, so it never zeroed the state either.- strata::core::session_zero(ss, g, nullptr, main_cs);- if (cudaDeviceSynchronize() != cudaSuccess) {- std::fprintf(stderr, "strata generate: zeroing the session state failed\n");- return 1;- }- if (g.n_qsa_layers() > 0 && ss.qsa_states[0].kv_mode == 1)- std::fprintf(stderr, "strata generate: KV streaming: %lld of %lld cells per QSA layer in VRAM, the K/V in "- "%.2f GiB of pinned RAM\n", (long long) (ss.qsa_states[0].n_slots * 4),- (long long) o.max_context, (double) strata::core::qsa_kv_host_bytes() / 1073741824.0);// ---- **THE HALF-LEVEL DUMP HAS TO BE ARMED BEFORE `session_capture`, AND THE LADDER MUST NOT BE.** The// half copies are issued from inside `block_layer_pre`/`block_layer_post`, so they are only ever enqueued@@ -1653,7 +1643,8 @@ int main(int argc, char** argv) {ss.ple.table = &ple_table;ss.ple.token = &ss.ple_token;ss.ple.prev = ss.ple_prev;- ss.ple.hist = ss.ple_hist;+ // `ss.ple.hist` and the ready() check wait for `session_init`, which carves the history - the session is+ // now allocated after the layer-split search (see the carve), and the wiring lands theress.ple.emb_host = ple_emb_host.data();if (cudaMalloc((void**) &ple_emb_dev, (size_t) strata::kernels::NG_N_EMBD * 4) != cudaSuccess ||cudaMalloc((void**) &ple_scratch, strata::core::ple_run_scratch_bytes()) != cudaSuccess) {@@ -1662,12 +1653,6 @@ int main(int argc, char** argv) {}ss.ple.emb_dev = ple_emb_dev;ss.ple.scratch = ple_scratch;- if (!ss.ple.ready()) {- std::fprintf(stderr, "strata generate: the PLE run is not ready after construction\n");- return 1;- }- std::fprintf(stderr, "strata generate: PLE on, table %llu rows of %s\n",- (unsigned long long) ple_table.rows(), o.ple_gguf.c_str());} else {std::fprintf(stderr,"strata generate: PLE OFF by explicit --no-ple diagnostic request.\n"@@ -1688,7 +1673,6 @@ int main(int argc, char** argv) {stages.push_back(std::make_unique<GpuStage>());GpuStage& st = *stages.back();st.dev = split_devs[i];- const bool last = i + 1 == split_devs.size();double free_gib = 0;if (!strata::core::RemoteExperts::preflight(st.dev, free_gib, err)) {std::fprintf(stderr, "strata generate: %s\n", err.c_str());@@ -1707,20 +1691,13 @@ int main(int argc, char** argv) {err.c_str());return 1;}- void* sbuf_s = nullptr;- if (cudaMalloc(&sbuf_s, strata::core::session_bytes(g, o.max_context, K)) != cudaSuccess ||- strata::core::session_init(g, o.max_context, K, sbuf_s, st.ss) == 0 ||- cudaStreamCreateWithFlags(&st.stream, cudaStreamNonBlocking) != cudaSuccess ||+ // THE SESSION AND THE HEAD WAIT FOR THE SPLIT SEARCH. `session_bytes` prices a stage's session by its+ // LAYER RANGE (the carve - every stage used to hold all 48 layers' state whatever it ran), so the+ // sessions are allocated after the search below has set `st.lb`/`st.le`; the last stage's head follows.+ if (cudaStreamCreateWithFlags(&st.stream, cudaStreamNonBlocking) != cudaSuccess ||cudaStreamCreateWithFlags(&st.adapt_stream, cudaStreamNonBlocking) != cudaSuccess ||cudaEventCreateWithFlags(&st.adapt_ev, cudaEventDisableTiming) != cudaSuccess) {- std::fprintf(stderr, "strata generate: layer split, CUDA%d: the session state failed\n", st.dev);- return 1;- }- const strata::core::WeightRef* wo_s = st.wt.find("output.weight");- if (wo_s == nullptr ||- (last && !o.native_head_gguf.empty() && !st.head.load(o.native_head_gguf, g.n_embd, wo_s->ne1, err))) {- std::fprintf(stderr, "strata generate: layer split, CUDA%d head: %s\n", st.dev,- wo_s == nullptr ? "output.weight is missing" : err.c_str());+ std::fprintf(stderr, "strata generate: layer split, CUDA%d: its streams failed\n", st.dev);return 1;}// a control vector (the speed projection): its tables on this device too - the stage's layers apply it here@@ -1749,119 +1726,11 @@ int main(int argc, char** argv) {}size_t fb = 0, tb = 0;cudaMemGetInfo(&fb, &tb);- std::fprintf(stderr, "strata generate: layer split: CUDA%d holds its weights, session%s; %.2f GiB free\n",- st.dev, last ? " and head" : "", (double) fb / 1073741824.0);+ std::fprintf(stderr, "strata generate: layer split: CUDA%d holds its weights; %.2f GiB free (its session "+ "follows the split search)\n", st.dev, (double) fb / 1073741824.0);}GpuStage* const last_st = stages.empty() ? nullptr : stages.back().get();- // Secure MTP's CUDA0 allocations before the large host arena is registered with both CUDA contexts.- // In particular WDDM can refuse the draft weights after mapping tens of GiB of host pages.- strata::core::MtpDrafter mtp;- if (!o.mtp.empty()) {- if (o.spec < 2) {- std::fprintf(stderr, "strata generate: --mtp is ignored without --spec T (T >= 2)\n");- o.mtp.clear();- }- if (!o.mtp.empty()) mtp.set_prompt_len((int64_t) o.tokens.size());- // the draft layer is the canonical model's MTP head (512 experts) even when the target is pruned,- // so it always sees the canonical geometry; `static` because MtpDrafter keeps a reference- static const strata::core::ModelGeometry draft_geometry{};- // with a layer split across GPUs the drafter reads the last stage's residual: it lives on that device- const strata::core::OnDevice on_mtp(last_st ? last_st->dev : -1);- if (!o.mtp.empty() && !mtp.load(o.mtp, draft_geometry, last_st ? last_st->ss : ss, o.spec, err, o.mtp_window)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; }- }- // Create the additional contexts after MTP has secured CUDA0 memory, but- // before the host arena maps its expert pages into their address spaces.- for (int r = 1; r < 3; ++r) if (o.expert_cache_remote[(size_t) r] > 0 && !early_remote) {- double free_gib = 0;- if (!strata::core::RemoteExperts::preflight(remote_dev[r], free_gib, err)) {- std::fprintf(stderr, "strata generate: %s\n", err.c_str());- return 1;- }- std::fprintf(stderr, "strata generate: CUDA%d context ready, %.2f GiB free before expert arena registration\n",- remote_dev[r], free_gib);- }-- // ---- the CPU expert pool- //- // R2.1: the experts are loaded into a RESIDENT ARENA by default. The mmap path is kept behind- // `--mmap-experts` because it is the A/B arm, not because it is competitive.- //- // The reasoning is the review's C1 and it is now measured on both sides. `FileExpertSource` maps the 34 GB- // file, and mapped file pages are the first thing the OS reclaims; the engine's rate then depends on whether- // the standby list happens to hold `experts.bin`, which is why two consecutive runs of the SAME BINARY with- // the SAME FLAGS measured 71.97 and 34.78 ms/token in the pool (7.54 vs 12.18 tok/s). The arena is- // anonymous memory the engine owns, and the pool runs at 19.41 ms/token - 1.79x better than the warm mmap- // and 3.7x better than the cold one.- //- // IT IS NOT PINNED, and that is reported rather than hidden: `cudaHostRegister` on 31.64 GiB fails with- // "out of memory" (you cannot pin 34 of 63 GB) and the arena falls back to 4 KB anonymous pages. That is- // fine for the CPU pool - which is all that exists today - and NOT fine for Phase 3, whose cache fills and- // CPU/PCIe miss split need the GPU to DMA out of this arena. Read `note()` when that lands.- //- // The earlier "the arena does not fit" conclusion was WRONG and is worth recording: the failure was a stale- // CUDA error left set by the failed `cudaHostRegister` and read later by `gr_read`'s launch check. See the- // note in `pinned.cu`.- strata::core::FileExpertSource src;- strata::core::ArenaExpertSource arena_src;- strata::core::ExpertSource* srcp = nullptr;- if (o.mmap_experts) {- // FileExpertSource maps the pack's experts.bin: a canonical pack has it; a native (IQ) pack has it when- // built with `tools/iq_pack.py --experts-bin` (the per-layer blob sizes of its layout, PR #121). The low-RAM- // mode: the experts come from the file through the OS cache instead of a pinned copy in RAM, for a PC whose- // GPU holds most of them but whose RAM cannot hold them all.- if (native_pack && !std::filesystem::exists(std::filesystem::path(o.pack) / "experts.bin")) {- std::fprintf(stderr, "strata generate: --mmap-experts needs the pack's experts.bin; %s is a native (IQ) pack "- "built without it: python tools/iq_pack.py --gguf <shard 1> --out %s --experts-bin\n",- o.pack.c_str(), o.pack.c_str());- return 2;- }- if (!src.open(o.pack, g.n_layers, g.n_expert, err)) {- std::fprintf(stderr, "strata generate: %s\n", err.c_str());- return 1;- }- std::fprintf(stderr, "strata generate: experts via mmap (--mmap-experts; the A/B arm of R2.1)\n");- srcp = &src;- } else {- arena_src.set_gguf(o.native_preset); // plan v0.3 P6: a native pack may take its experts from shard 1- // On the multi-GPU Windows experiment, start with at most 8 GiB of mapped host pages.- // Unregistered layers remain in the resident arena and use the CPU expert path.- // (a layer split across GPUs too: pinning all of it into two contexts leaves WDDM refusing every later- // allocation - measured on the 5080 + 3090 rig: cudaMemGetInfo and the next cudaMalloc fail)- const uint64_t pin_limit = (o.expert_cache_remote[0] > 0 || multi_gpu) ? (8ull << 30) : 0;- if (!arena_src.open(o.pack, g.n_layers, g.n_expert, /*threads=*/6, err, pin_limit,- o.shared_expert_arena)) {- std::fprintf(stderr, "strata generate: %s\n", err.c_str());- return 1;- }- std::fprintf(stderr, "strata generate: expert arena: %s\n", arena_src.note().c_str());- std::fprintf(stderr, "strata generate: loaded %.2f GiB at %.2f GiB/s\n",- (double) strata::kernels::cpu::expert_layout().total / (1024.0 * 1024 * 1024),- arena_src.load_gib_per_second());- // A rate under ~0.2 GiB/s is not the hardware. Task Scheduler / service contexts throttle this- // read+fill about 24x (measured 0.05 vs 1.42 GiB/s for the same binary, args and cache state; the- // scheduler's defaults - Below normal priority and a least-privilege token - were the only- // difference between the runs). Say so instead of letting the user blame the disk; see- // docs/DETAILS.md, "Running it at startup (Task Scheduler)".-#ifdef _WIN32 // a Windows launch context; elsewhere a load this slow is the disk- if (arena_src.load_gib_per_second() > 0.0 && arena_src.load_gib_per_second() < 0.2) {- std::fprintf(stderr,- "strata generate: hint: ~24x below what this hardware streams from a normal "- "launch. If Strata is started by Task Scheduler or a service, register the task "- "with Priority 4 (Normal) and 'Run with highest privileges' - the scheduler's "- "defaults (Below normal + a least-privilege token) throttle the load. See "- "docs/DETAILS.md ('Running it at startup').\n");- }-#endif- srcp = &arena_src;- }- strata::kernels::cpu::ExpertPool pool(o.pool_workers, /*pin=*/true, /*host_works=*/!o.no_host_worker);- if (o.no_ple_prefetch) strata::kernels::ple_prefetch_enable(false);- // ---- R4's slot storage. Allocated AFTER the weights and the session, so `cudaMemGetInfo` inside `open`- // sees the memory this process actually has left rather than the card's idle figure - and refuses with both- // numbers if the slots do not fit, instead of handing back a cache smaller than it was asked for.- mem_mark("the weights, the session and the drafter");- strata::core::ExpertCache xcache;std::vector<std::pair<int32_t, int32_t>> profile;if (!o.expert_profile.empty()) {int64_t pslots = 0;@@ -1889,14 +1758,31 @@ int main(int argc, char** argv) {// 99.53/99.34/99.15%, measured 99.5/99.4/99.0%).// Up to 3 GPUs every placement is tried; beyond, the layers are shared in proportion to speed.// STRATA_SPLIT_MISS_MS tunes the miss cost (a slower CPU: higher).- const int64_t split_pf_mib = o.prefill_chunk > 0 ? 160 + (o.prefill_chunk * 680) / 1024 : 0;- auto stage_room = [&](int dev, bool later) -> int64_t {+ // THE PROMPT PATH'S BUFFERS ARE BORROWED FROM THE CACHE, NOT WITHHELD BESIDE IT. With borrowing the cache+ // is sized first and at full size, and the prompt path is laid out in the tail of it (`Prefill::relayout`),+ // so it withholds no VRAM of its own and this reserve is zero. Only without borrowing - no profile to fill+ // a cache from, or --no-prefill-borrow - do the buffers take a reserve, and then this estimate stands in+ // for buffers that cannot be priced exactly yet because the sessions do not exist. `plan_lend` uses the+ // exact `Prefill::bytes_needed` as soon as it can.+ const bool pf_borrow = !o.no_prefill_borrow && !o.expert_profile.empty();+ const int64_t split_pf_mib = (o.prefill_chunk > 0 && !pf_borrow) ? 160 + (o.prefill_chunk * 680) / 1024 : 0;+ // ---- WHAT A STAGE RESERVES, AND ON WHICH STAGE. The flat 1 GiB this used to withhold from EVERY stage+ // after the first was booked "for its windows and the drafter", but the windows measure 75 MiB ("window up+ // to 6 tokens, 74.1 MiB of device buffers", on every boot) and the drafter is loaded on ONE stage - the+ // last one, which is also the only one that holds the head. On the two identical 8 GB cards that GiB was+ // the entire difference between CUDA0's cache and CUDA1's: 814 slots against 188, 2026-09-30. Both of+ // those allocations are already made before a stage's cache is sized, so what has to be held back here is+ // the windows and - only on the stage that carries them - the drafter and the head.+ const int64_t kWindowMib = 96; // the verify windows; 75 MiB measured, rounded up+ const int64_t kDrafterMib = 1000; // the MTP drafter (839 MiB) + the head, on the last stage only+ auto stage_room = [&](int dev, bool later, bool drafter) -> int64_t {const strata::core::OnDevice on(dev);size_t fb = 0, tb = 0;if (const cudaError_t e = cudaMemGetInfo(&fb, &tb); e != cudaSuccess)std::fprintf(stderr, "strata generate: layer split: CUDA%d free memory: %s\n", dev < 0 ? 0 : dev,cudaGetErrorString(e));- const int64_t reserve = ((int64_t) o.vram_reserve_mib + split_pf_mib + (later ? 1024 : 0)) << 20;+ const int64_t reserve = ((int64_t) o.vram_reserve_mib + split_pf_mib + (later ? kWindowMib : 0) ++ (drafter ? kDrafterMib : 0)) << 20;return std::max<int64_t>((int64_t) fb - reserve, 0);};if (multi_gpu && split_auto) {@@ -1906,7 +1792,7 @@ int main(int argc, char** argv) {std::vector<double> layer_ms((size_t) ns);for (int i = 0; i < ns; ++i) {const int dev = i == 0 ? 0 : stages[(size_t) i - 1]->dev;- cap[(size_t) i] = stage_room(i == 0 ? -1 : dev, i > 0);+ cap[(size_t) i] = stage_room(i == 0 ? -1 : dev, i > 0, i + 1 == ns);int sms = 0, khz = 0;cudaDeviceGetAttribute(&sms, cudaDevAttrMultiProcessorCount, dev);if (cudaDeviceGetAttribute(&khz, cudaDevAttrClockRate, dev) != cudaSuccess || khz <= 0) khz = 1800000;@@ -1914,7 +1800,7 @@ int main(int argc, char** argv) {const double speed = std::max(1.0, (double) sms * (double) khz / 1e6); // SMs x GHzlayer_ms[(size_t) i] = 0.33 * (84.0 * 2.617) / speed;std::fprintf(stderr, "strata generate: layer split auto: CUDA%d %d SMs at %.2f GHz -> %.2f ms per layer, "- "%.2f GiB for experts\n", dev, sms, khz / 1e6, layer_ms[(size_t) i],+ "%.2f GiB free before its session carve\n", dev, sms, khz / 1e6, layer_ms[(size_t) i],(double) cap[(size_t) i] / 1073741824.0);}const double miss_ms = std::getenv("STRATA_SPLIT_MISS_MS") ? std::atof(std::getenv("STRATA_SPLIT_MISS_MS")) : 190.0;@@ -1926,6 +1812,15 @@ int main(int argc, char** argv) {};// the predicted window time (ms) of a placement, and the routed mass its caches holdauto predict = [&](const std::vector<int64_t>& at, double& held_mass, int64_t& held) -> double {+ // THE CARVE, PRICED: a placement gives stage i the layers [lb, le), and that range's session is a+ // real cost on its device - subtracted here so the search knows what it leaves for experts. This+ // is why the sessions are allocated after the search: `session_bytes` is pure arithmetic.+ std::vector<int64_t> capr((size_t) ns);+ for (int i = 0; i < ns; ++i) {+ const int64_t lb = i == 0 ? 0 : at[(size_t) i - 1];+ const int64_t le = i + 1 < ns ? at[(size_t) i] : g.n_layers;+ capr[(size_t) i] = cap[(size_t) i] - (int64_t) strata::core::session_bytes(g, o.max_context, K, lb, le);+ }std::fill(used.begin(), used.end(), 0);held_mass = 0;held = 0;@@ -1935,7 +1830,7 @@ int main(int argc, char** argv) {int st = 0;while (st + 1 < ns && l >= at[(size_t) st]) ++st;if (full[(size_t) st]) continue;- if (used[(size_t) st] + cost(l) > cap[(size_t) st]) { full[(size_t) st] = true; continue; } // as the fill+ if (used[(size_t) st] + cost(l) > capr[(size_t) st]) { full[(size_t) st] = true; continue; } // as the fillused[(size_t) st] += cost(l);held_mass += mass[r];++held;@@ -2005,6 +1900,177 @@ int main(int argc, char** argv) {stages[i]->le = i + 1 < stages.size() ? split_at[i + 1] : g.n_layers;}}++ // ---- CUDA0's session, and the stages' sessions: sized to each device's own layer range (the carve). A+ // stage that runs [lb, le) carves only those layers' GDN rows and QSA pools - before the carve every stage+ // held all 48 layers' state whatever layers it ran, which is the same disease the chunked-QSA-prefill PR+ // fixed in llama.cpp: allocation sized by the whole model instead of the device's own work.+ {+ const strata::core::OnDevice on0(0);+ const int64_t hi0 = multi_gpu ? split_at[0] : -1;+ if (cudaMalloc(&sbuf, strata::core::session_bytes(g, o.max_context, K, 0, hi0)) != cudaSuccess) {+ std::fprintf(stderr, "strata generate: session state allocation failed\n");+ return 1;+ }+ if (strata::core::session_init(g, o.max_context, K, sbuf, ss, 0, hi0) == 0) {+ std::fprintf(stderr, "strata generate: session_init failed\n");+ return 1;+ }+ if (g.n_qsa_layers() > 0 && ss.qsa_states[ss.qsa_primary()].kv_mode == 1)+ std::fprintf(stderr, "strata generate: KV streaming: %lld of %lld cells per QSA layer in VRAM, the K/V in "+ "%.2f GiB of pinned RAM\n",+ (long long) (ss.qsa_states[ss.qsa_primary()].n_slots * 4),+ (long long) o.max_context, (double) strata::core::qsa_kv_host_bytes() / 1073741824.0);+ // the PLE block above built everything but the history, which session_init has just carved+ ss.ple.hist = ss.ple_hist;+ // (#167) generate mode starts from an empty sequence, and nothing else zeroes this state before the prompt+ // path or the verifier reads it (--serve zeroes it per request when nothing is reused)+ strata::core::session_zero(ss, g, nullptr, main_cs);+ if (cudaDeviceSynchronize() != cudaSuccess) {+ std::fprintf(stderr, "strata generate: zeroing the session state failed\n");+ return 1;+ }+ if (!o.ple_gguf.empty()) {+ if (!ss.ple.ready()) {+ std::fprintf(stderr, "strata generate: the PLE run is not ready after construction\n");+ return 1;+ }+ std::fprintf(stderr, "strata generate: PLE on, table %llu rows of %s\n",+ (unsigned long long) ple_table.rows(), o.ple_gguf.c_str());+ }+ }+ for (size_t i = 0; i < stages.size(); ++i) {+ GpuStage& st = *stages[i];+ const strata::core::OnDevice on(st.dev);+ void* sbuf_s = nullptr;+ if (cudaMalloc(&sbuf_s, strata::core::session_bytes(g, o.max_context, K, st.lb, st.le)) != cudaSuccess ||+ strata::core::session_init(g, o.max_context, K, sbuf_s, st.ss, st.lb, st.le) == 0) {+ std::fprintf(stderr, "strata generate: layer split, CUDA%d: the session state failed\n", st.dev);+ return 1;+ }+ const bool last = i + 1 == stages.size();+ const strata::core::WeightRef* wo_s = st.wt.find("output.weight");+ if (wo_s == nullptr ||+ (last && !o.native_head_gguf.empty() && !st.head.load(o.native_head_gguf, g.n_embd, wo_s->ne1, err))) {+ std::fprintf(stderr, "strata generate: layer split, CUDA%d head: %s\n", st.dev,+ wo_s == nullptr ? "output.weight is missing" : err.c_str());+ return 1;+ }+ size_t fb = 0, tb = 0;+ cudaMemGetInfo(&fb, &tb);+ std::fprintf(stderr, "strata generate: layer split: CUDA%d holds its weights, session [%lld, %lld)%s; "+ "%.2f GiB free\n", st.dev, (long long) st.lb, (long long) st.le,+ last ? " and the head" : "", (double) fb / 1073741824.0);+ }++ // Secure MTP's CUDA0 allocations before the large host arena is registered with both CUDA contexts.+ // In particular WDDM can refuse the draft weights after mapping tens of GiB of host pages.+ strata::core::MtpDrafter mtp;+ if (!o.mtp.empty()) {+ if (o.spec < 2) {+ std::fprintf(stderr, "strata generate: --mtp is ignored without --spec T (T >= 2)\n");+ o.mtp.clear();+ }+ if (!o.mtp.empty()) mtp.set_prompt_len((int64_t) o.tokens.size());+ // the draft layer is the canonical model's MTP head (512 experts) even when the target is pruned,+ // so it always sees the canonical geometry; `static` because MtpDrafter keeps a reference+ static const strata::core::ModelGeometry draft_geometry{};+ // with a layer split across GPUs the drafter reads the last stage's residual: it lives on that device+ const strata::core::OnDevice on_mtp(last_st ? last_st->dev : -1);+ if (!o.mtp.empty() && !mtp.load(o.mtp, draft_geometry, last_st ? last_st->ss : ss, o.spec, err, o.mtp_window)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; }+ }+ // Create the additional contexts after MTP has secured CUDA0 memory, but+ // before the host arena maps its expert pages into their address spaces.+ for (int r = 1; r < 3; ++r) if (o.expert_cache_remote[(size_t) r] > 0 && !early_remote) {+ double free_gib = 0;+ if (!strata::core::RemoteExperts::preflight(remote_dev[r], free_gib, err)) {+ std::fprintf(stderr, "strata generate: %s\n", err.c_str());+ return 1;+ }+ std::fprintf(stderr, "strata generate: CUDA%d context ready, %.2f GiB free before expert arena registration\n",+ remote_dev[r], free_gib);+ }++ // ---- the CPU expert pool+ //+ // R2.1: the experts are loaded into a RESIDENT ARENA by default. The mmap path is kept behind+ // `--mmap-experts` because it is the A/B arm, not because it is competitive.+ //+ // The reasoning is the review's C1 and it is now measured on both sides. `FileExpertSource` maps the 34 GB+ // file, and mapped file pages are the first thing the OS reclaims; the engine's rate then depends on whether+ // the standby list happens to hold `experts.bin`, which is why two consecutive runs of the SAME BINARY with+ // the SAME FLAGS measured 71.97 and 34.78 ms/token in the pool (7.54 vs 12.18 tok/s). The arena is+ // anonymous memory the engine owns, and the pool runs at 19.41 ms/token - 1.79x better than the warm mmap+ // and 3.7x better than the cold one.+ //+ // IT IS NOT PINNED, and that is reported rather than hidden: `cudaHostRegister` on 31.64 GiB fails with+ // "out of memory" (you cannot pin 34 of 63 GB) and the arena falls back to 4 KB anonymous pages. That is+ // fine for the CPU pool - which is all that exists today - and NOT fine for Phase 3, whose cache fills and+ // CPU/PCIe miss split need the GPU to DMA out of this arena. Read `note()` when that lands.+ //+ // The earlier "the arena does not fit" conclusion was WRONG and is worth recording: the failure was a stale+ // CUDA error left set by the failed `cudaHostRegister` and read later by `gr_read`'s launch check. See the+ // note in `pinned.cu`.+ strata::core::FileExpertSource src;+ strata::core::ArenaExpertSource arena_src;+ strata::core::ExpertSource* srcp = nullptr;+ if (o.mmap_experts) {+ // FileExpertSource maps the pack's experts.bin: a canonical pack has it; a native (IQ) pack has it when+ // built with `tools/iq_pack.py --experts-bin` (the per-layer blob sizes of its layout, PR #121). The low-RAM+ // mode: the experts come from the file through the OS cache instead of a pinned copy in RAM, for a PC whose+ // GPU holds most of them but whose RAM cannot hold them all.+ if (native_pack && !std::filesystem::exists(std::filesystem::path(o.pack) / "experts.bin")) {+ std::fprintf(stderr, "strata generate: --mmap-experts needs the pack's experts.bin; %s is a native (IQ) pack "+ "built without it: python tools/iq_pack.py --gguf <shard 1> --out %s --experts-bin\n",+ o.pack.c_str(), o.pack.c_str());+ return 2;+ }+ if (!src.open(o.pack, g.n_layers, g.n_expert, err)) {+ std::fprintf(stderr, "strata generate: %s\n", err.c_str());+ return 1;+ }+ std::fprintf(stderr, "strata generate: experts via mmap (--mmap-experts; the A/B arm of R2.1)\n");+ srcp = &src;+ } else {+ arena_src.set_gguf(o.native_preset); // plan v0.3 P6: a native pack may take its experts from shard 1+ // On the multi-GPU Windows experiment, start with at most 8 GiB of mapped host pages.+ // Unregistered layers remain in the resident arena and use the CPU expert path.+ // (a layer split across GPUs too: pinning all of it into two contexts leaves WDDM refusing every later+ // allocation - measured on the 5080 + 3090 rig: cudaMemGetInfo and the next cudaMalloc fail)+ const uint64_t pin_limit = (o.expert_cache_remote[0] > 0 || multi_gpu) ? (8ull << 30) : 0;+ if (!arena_src.open(o.pack, g.n_layers, g.n_expert, /*threads=*/6, err, pin_limit,+ o.shared_expert_arena)) {+ std::fprintf(stderr, "strata generate: %s\n", err.c_str());+ return 1;+ }+ std::fprintf(stderr, "strata generate: expert arena: %s\n", arena_src.note().c_str());+ std::fprintf(stderr, "strata generate: loaded %.2f GiB at %.2f GiB/s\n",+ (double) strata::kernels::cpu::expert_layout().total / (1024.0 * 1024 * 1024),+ arena_src.load_gib_per_second());+ // A rate under ~0.2 GiB/s is not the hardware. Task Scheduler / service contexts throttle this+ // read+fill about 24x (measured 0.05 vs 1.42 GiB/s for the same binary, args and cache state; the+ // scheduler's defaults - Below normal priority and a least-privilege token - were the only+ // difference between the runs). Say so instead of letting the user blame the disk; see+ // docs/DETAILS.md, "Running it at startup (Task Scheduler)".+#ifdef _WIN32 // a Windows launch context; elsewhere a load this slow is the disk+ if (arena_src.load_gib_per_second() > 0.0 && arena_src.load_gib_per_second() < 0.2) {+ std::fprintf(stderr,+ "strata generate: hint: ~24x below what this hardware streams from a normal "+ "launch. If Strata is started by Task Scheduler or a service, register the task "+ "with Priority 4 (Normal) and 'Run with highest privileges' - the scheduler's "+ "defaults (Below normal + a least-privilege token) throttle the load. See "+ "docs/DETAILS.md ('Running it at startup').\n");+ }+#endif+ srcp = &arena_src;+ }+ strata::kernels::cpu::ExpertPool pool(o.pool_workers, /*pin=*/true, /*host_works=*/!o.no_host_worker);+ if (o.no_ple_prefetch) strata::kernels::ple_prefetch_enable(false);+ // ---- R4's slot storage. Allocated AFTER the weights and the session, so `cudaMemGetInfo` inside `open`+ // sees the memory this process actually has left rather than the card's idle figure - and refuses with both+ // numbers if the slots do not fit, instead of handing back a cache smaller than it was asked for.+ mem_mark("the weights, the session and the drafter");+ strata::core::ExpertCache xcache;// THE HEAD BEFORE THE CACHE. The expert cache takes what is free minus the reserve, so everything allocated// after it comes out of the reserve. The native head (~0.5 GB with IQ3_S) was loaded after it and ate most of// the 700 MiB: 128K IQ3_S ended with 30 MiB free, the driver paged, and a request stalled for good at its first@@ -2033,9 +2099,9 @@ int main(int argc, char** argv) {cudaMemGetInfo(&free_b, &total_b);// Plan v0.3 P5: the batched prompt path's chunk buffers are allocated later, so they are reserved here -// under WDDM an over-subscribed allocation does not fail, it pages to system memory and crawls.- // (with borrowing - the default with a profile - the prompt path lends cache slots instead)- const bool borrow = !o.no_prefill_borrow && !o.expert_profile.empty();- const int64_t prefill_mib = (o.prefill_chunk > 0 && !borrow) ? 160 + (o.prefill_chunk * 680) / 1024 : 0;+ // (with borrowing - the default with a profile - the prompt path lends cache slots instead; `pf_borrow` is+ // the predicate a local `borrow` was here, hoisted above so both cache-size branches read the same one)+ const int64_t prefill_mib = (o.prefill_chunk > 0 && !pf_borrow) ? 160 + (o.prefill_chunk * 680) / 1024 : 0;// the draft layer's head and logits are allocated when it binds, after this: 0.1.27's CJK subset made them// ~110-180 MiB larger, and out of the reserve they left 16 GB cards below the stall line (#199)const int64_t mtp_bind = (!o.mtp.empty() && native_head.loaded())@@ -2051,11 +2117,12 @@ int main(int argc, char** argv) {std::fprintf(stderr, "strata generate: no VRAM is left for the expert cache: lower --max-context, use ""--kv k8v4, run images on the CPU, or close other programs that use the GPU\n");} else if (multi_gpu && o.expert_cache > 0) {- // a layer split's prompt path has its own buffers (it borrows no slots): an explicit cache size leaves room- // for them and the reserve, or the first prompt fails with "device buffers ... do not fit"+ // an explicit cache size leaves room for the prompt path's buffers and the reserve, or the first prompt+ // fails with "device buffers ... do not fit" (with borrowing - the default with a profile - the path lends+ // slots instead and `prefill_mib` is 0, so only the reserve is checked)size_t free_b = 0, total_b = 0;cudaMemGetInfo(&free_b, &total_b);- const int64_t prefill_mib = o.prefill_chunk > 0 ? 160 + (o.prefill_chunk * 680) / 1024 : 0;+ const int64_t prefill_mib = (o.prefill_chunk > 0 && !pf_borrow) ? 160 + (o.prefill_chunk * 680) / 1024 : 0;const int64_t reserve = ((int64_t) o.vram_reserve_mib + prefill_mib) << 20;const int64_t fit = std::max<int64_t>(((int64_t) free_b - reserve) / (int64_t) strata::kernels::cpu::expert_layout().max_blob, 0);if (o.expert_cache > fit) {@@ -2245,7 +2312,9 @@ int main(int argc, char** argv) {for (auto& stp : stages) {GpuStage& st = *stp;const auto& lay = strata::kernels::cpu::expert_layout();- const int64_t room = stage_room(st.dev, true);+ // the drafter and the head are already allocated by now (they load above, before this), so what is left+ // to hold back is the windows - and `free_b` has already lost the drafter.+ const int64_t room = stage_room(st.dev, true, false);const strata::core::OnDevice on(st.dev);std::vector<int64_t> sized;int64_t used = 0;@@ -2508,7 +2577,11 @@ int main(int argc, char** argv) {// ---- the graphsstrata::core::SessionGraphs gr;if (!o.no_capture && !native_pack) { // plan v0.3 P6: a native pack runs verify windows only- if (!strata::core::session_capture(wt, g, ss, d_parts, gr, err, /*split=*/o.gpu_stages)) {+ // a layer split's CUDA0 session owns only [0, split_at[0]), so its graphs cover that range; the+ // whole-model replay paths (`session_loop`, the plain generate loop) refuse rather than read another+ // stage's state - a split runs its layers on the stages' verifiers (serve) or prefill stage chain+ if (!strata::core::session_capture(wt, g, ss, d_parts, gr, err, /*split=*/o.gpu_stages, 0,+ multi_gpu ? split_at[0] : -1)) {std::fprintf(stderr, "strata generate: session_capture: %s\n", err.c_str());return 1;}@@ -2985,7 +3058,7 @@ int main(int argc, char** argv) {(long long) resident);}if (!o.no_capture && !o.no_token_graph && layer_dump == nullptr && half_dump == nullptr &&- (hit_fn == nullptr || thits.on()) && !native_pack) {+ (hit_fn == nullptr || thits.on()) && !native_pack && !multi_gpu) { // a split's token graph cannot span stagesif (!strata::core::session_capture_token(wt, g, ss, d_parts, loop_scratch.y_miss, loop_scratch.parts_bytes,tgraph, err, thits.on() ? &thits : nullptr)) {std::fprintf(stderr, "strata generate: %s\n", err.c_str());@@ -3052,11 +3125,8 @@ int main(int argc, char** argv) {}return k;};- auto lend_bytes = [&](int32_t first) -> uint64_t {- return xcache.slot_offsets() ? (uint64_t) (xcache.bytes() - (int64_t) xcache.slot_offsets()[first])- : (uint64_t) (xcache.slots() - first) *- (uint64_t) strata::kernels::cpu::expert_layout().max_blob;- };+ // `lend_bytes` went with the single-cache serve loan: a participant's loan is priced by `part_bytes` from its+ // OWN cache, and the only other user of the old helper was the serve path's own relayout.auto request_chunk = [](int64_t tokens, int64_t max_chunk) -> int64_t {if (tokens <= 0 || max_chunk <= 0) return 0;const int64_t rounded = tokens > std::numeric_limits<int64_t>::max() - 255@@ -3069,16 +3139,17 @@ int main(int argc, char** argv) {// lent slot's expert is streamed during the prompt and refilled after it; measured on a 12 GB card, 32K Q2_0// prompt: 4096 791 tok/s, 6144 878, 8192 973 with 69% of the slots lent). A request lends only what its own// prompt needs (Prefill::relayout), so a big chunk costs short prompts nothing. 0 = none fits.+ // at 8192-token chunks nearly every expert streams anyway, so a lent slot costs little: 90% when the+ // copies are DMA from pinned RAM (Q2_0 8192 + a 384-slot ring: 1283 tok/s), 85% when host copies are the+ // limit (lending more only streams more through them). STRATA_PREFILL_LEND_PCT overrides (tuning).+ // Hoisted out of plan_lend: the serve path's per-stage loans obey the same cap, one participant at a time.+ const int64_t kAutoLendPct = [] {+ const char* v = std::getenv("STRATA_PREFILL_LEND_PCT");+ return v ? (int64_t) std::atoi(v)+ : (int64_t) (strata::prefill::Prefill::pinned_share() >= 0.9 ? 90 : 85);+ }();auto plan_lend = [&](int64_t& chunk) -> int64_t {static constexpr int64_t kAutoChunks[] = {8192, 6144, 4096, 3072, 2048, 1024, 512, 256};- // at 8192-token chunks nearly every expert streams anyway, so a lent slot costs little: 90% when the- // copies are DMA from pinned RAM (Q2_0 8192 + a 384-slot ring: 1283 tok/s), 85% when host copies are the- // limit (lending more only streams more through them). STRATA_PREFILL_LEND_PCT overrides (tuning).- const int64_t kAutoLendPct = [] {- const char* v = std::getenv("STRATA_PREFILL_LEND_PCT");- return v ? (int64_t) std::atoi(v)- : (int64_t) (strata::prefill::Prefill::pinned_share() >= 0.9 ? 90 : 85);- }();auto slots_for = lend_slots;if (o.prefill_auto) {for (const int64_t c : kAutoChunks) {@@ -3104,42 +3175,131 @@ int main(int argc, char** argv) {void* borrow = nullptr;uint64_t borrow_bytes = 0;int32_t lend_first = -1; // the first slot the prompt path may borrow (its largest chunk)- int32_t lend_first_now = -1; // where its buffers are laid out now- // a cache too small to lend the prompt path its buffers would make it allocate them on top - on a card the- // cache already filled to its reserve, that is the over-subscription the auto sizing avoids - so the- // prompt chunk is halved until its buffers fit in the lendable slots (a smaller chunk only reads slower)- if (!o.no_prefill_borrow && d_res != nullptr) {- int64_t chunk = o.prefill_chunk;- if (const int64_t k = plan_lend(chunk); k > 0) {+ // ---- WHO BORROWS, AND FROM WHOSE CACHE. One entry per prompt path: CUDA0's (layers [0, split_at[0]),+ // which is the whole model without a split) borrowing the tail of CUDA0's cache, then one per stage+ // borrowing the tail of ITS OWN cache. A loan is sized by the exact `Prefill::bytes_needed` for the+ // chunk, is laid out by `Prefill::relayout`, and is refilled before any window reads - so outside the+ // prompt the whole cache is expert cache. THIS IS THE POINT OF THE STRUCT: the loan used to exist only+ // for CUDA0, and `no_prefill_borrow` made every stage instead withhold a chunk-sized reserve from its+ // cache for the entire session, which is what cost the 4-way its context (see the note at the top of the+ // layer-split block). A stage may only lend the rows for ITS OWN layers: `host_res` is one table whose+ // slot values are indices into whichever cache owns the layer, so a loan that marked rows by slot number+ // alone would hand CUDA0 a slot belonging to another stage's cache.+ struct PfPart {+ strata::core::ExpertCache* cache = nullptr;+ const strata::core::SessionState* ses = nullptr;+ strata::prefill::Prefill* sp = nullptr;+ int dev = -1; // -1: leave the device alone (CUDA0)+ int64_t lb = 0, le = 0; // the layers whose rows this cache holds - the only rows it may lend+ int32_t first = -1; // the first slot it may lend, for the chunk that was chosen+ int32_t first_now = -1; // where its buffers are laid out now+ int64_t lent_chunk = 0;+ std::vector<std::pair<int32_t, int32_t>> lent;+ };+ auto part_slots = [&](const PfPart& p, int64_t c) -> int64_t {+ const uint64_t need = strata::prefill::Prefill::bytes_needed(g, *p.ses, c);+ strata::core::ExpertCache& xc = *p.cache;+ if (xc.slot_offsets() != nullptr) { // sized slots: from the end until they hold `need`+ int64_t k = 0;+ while (k < xc.slots() &&+ (uint64_t) (xc.bytes() - (int64_t) xc.slot_offsets()[xc.slots() - k]) < need) ++k;+ return k;+ }+ const int64_t blob = (int64_t) strata::kernels::cpu::expert_layout().max_blob;+ return (int64_t) ((need + (uint64_t) blob - 1) / (uint64_t) blob);+ };+ auto part_bytes = [&](const PfPart& p, int32_t first) -> uint64_t {+ strata::core::ExpertCache& xc = *p.cache;+ return xc.slot_offsets() ? (uint64_t) (xc.bytes() - (int64_t) xc.slot_offsets()[first])+ : (uint64_t) (xc.slots() - first) *+ (uint64_t) strata::kernels::cpu::expert_layout().max_blob;+ };+ std::vector<PfPart> pf_parts;+ // a cache too small to lend the prompt path its buffers would make it allocate them on top - on a card+ // whose cache already filled its reserve, that is the over-subscription the auto sizing avoids - so the+ // chunk is the largest one EVERY participant can lend (a smaller chunk only reads slower)+ if (pf_borrow && d_res != nullptr) {+ pf_parts.push_back({&xcache, &ss, &sp, -1, 0, multi_gpu ? split_at[0] : g.n_layers, -1, -1, 0, {}});+ for (auto& st : stages)+ pf_parts.push_back({&st->cache, &st->ss, &st->sp, st->dev, st->lb, st->le, -1, -1, 0, {}});+ // The two tests plan_lend makes for CUDA0 alone, one participant at a time: a loan must leave the+ // 128-slot floor. The percentage cap is an AUTO-chunk rule and only the auto scan applies it - an+ // explicit --prefill is the operator's number, and a loan of it only has to fit. With one participant+ // (no split) this reduces to plan_lend exactly, so the single-GPU loan is unchanged from main.+ auto fits = [&](int64_t c, bool cap) -> bool {+ for (const PfPart& p : pf_parts) {+ const int64_t k = part_slots(p, c);+ if (k <= 0 || k + 128 > p.cache->slots()) return false;+ if (cap && k * 100 > kAutoLendPct * p.cache->slots()) return false;+ }+ return true;+ };+ static constexpr int64_t kAutoChunks[] = {8192, 6144, 4096, 3072, 2048, 1024, 512, 256};+ int64_t chunk = 0;+ if (o.prefill_auto) {+ for (const int64_t c : kAutoChunks)+ if (fits(c, true)) { chunk = c; break; }+ } else {+ for (int64_t c = o.prefill_chunk; c >= 256; c /= 2)+ if (fits(c, false)) { chunk = c; break; }+ }+ if (chunk > 0) {if (o.prefill_auto)std::fprintf(stderr, "strata serve: prompt chunk auto: %lld tokens\n", (long long) chunk);else if (chunk != o.prefill_chunk)- std::fprintf(stderr, "strata serve: prompt chunk %lld -> %lld tokens so its buffers fit in the "- "expert cache\n", (long long) o.prefill_chunk, (long long) chunk);+ std::fprintf(stderr, "strata serve: prompt chunk %lld -> %lld tokens so its buffers fit in "+ "every expert cache\n", (long long) o.prefill_chunk, (long long) chunk);o.prefill_chunk = chunk;- lend_first = (int32_t) (xcache.slots() - k);- lend_first_now = lend_first;+ for (PfPart& p : pf_parts) {+ p.first = (int32_t) (p.cache->slots() - part_slots(p, chunk));+ p.first_now = p.first;+ }+ lend_first = pf_parts[0].first;borrow = xcache.device_slot(lend_first);- borrow_bytes = xcache.slot_offsets()- ? (uint64_t) (xcache.bytes() - (int64_t) xcache.slot_offsets()[lend_first])- : (uint64_t) k * (uint64_t) strata::kernels::cpu::expert_layout().max_blob;+ borrow_bytes = part_bytes(pf_parts[0], lend_first);} else if (o.prefill_auto) {o.prefill_chunk = 1024; // nothing lendable: small buffers of its own+ } else if (pf_parts.size() > 1) {+ // An explicit chunk no stage can lend in full. main falls back to the prompt path's own buffers+ // here and so do we, rather than refusing to start - but say what every stage has, because a split+ // stage that has to allocate these on top of a cache that already filled its VRAM will not fit,+ // and `init` would otherwise report only that the buffers do not fit.+ std::fprintf(stderr, "strata serve: no stage can lend the prompt path its %lld-token buffers, so "+ "each stage allocates its own:\n", (long long) o.prefill_chunk);+ for (const PfPart& p : pf_parts)+ std::fprintf(stderr, "strata serve: CUDA%d has %lld slots, and a %lld-token chunk needs "+ "the last %lld of them\n", p.dev < 0 ? 0 : p.dev,+ (long long) p.cache->slots(), (long long) o.prefill_chunk,+ (long long) part_slots(p, o.prefill_chunk));}} else if (o.prefill_auto && d_res == nullptr) {o.prefill_chunk = 1024; // #85: no expert cache at all (a full 8 GB card): small buffers of its own}- if (borrow != nullptr)- std::fprintf(stderr, "strata serve: the prompt path borrows %lld cache slots (%.2f GiB)\n",+ if (borrow != nullptr) {+ std::fprintf(stderr, "strata serve: the prompt path borrows %lld CUDA0 cache slots (%.2f GiB)\n",(long long) (xcache.slots() - lend_first), (double) borrow_bytes / 1073741824.0);- else+ for (size_t i = 1; i < pf_parts.size(); ++i) // one loan per stage, from that stage's own cache+ std::fprintf(stderr, "strata serve: CUDA%d prompt path borrows %lld of its %lld slots (%.2f GiB)\n",+ pf_parts[i].dev, (long long) (pf_parts[i].cache->slots() - pf_parts[i].first),+ (long long) pf_parts[i].cache->slots(),+ (double) part_bytes(pf_parts[i], pf_parts[i].first) / 1073741824.0);+ } else {std::fprintf(stderr, "strata serve: the prompt path allocates its own buffers (too few cache slots to borrow)\n");+ }// layer split across GPUs: a prompt path per stage, each handing its chunk's rows to the nextfor (size_t i = 0; i < stages.size(); ++i) {GpuStage& st = *stages[i];st.sp.set_stage(st.lb, i + 1 < stages.size() ? st.le : -1, i + 1 < stages.size() ? &stages[i + 1]->sp : nullptr);const strata::core::OnDevice on(st.dev);- if (!st.sp.init(st.wt, g, st.ss, srcp, &st.cache, host_res.data(), o.prefill_chunk, (void*) st.stream, err)) {+ void* sb = nullptr; // this stage's own loan, out of its own cache+ uint64_t sbb = 0;+ // `first < 0`: no loan was taken (nothing was lendable), so this stage allocates its own buffers+ if (i + 1 < pf_parts.size() && pf_parts[i + 1].first >= 0) {+ sb = st.cache.device_slot(pf_parts[i + 1].first);+ sbb = part_bytes(pf_parts[i + 1], pf_parts[i + 1].first);+ }+ if (!st.sp.init(st.wt, g, st.ss, srcp, &st.cache, host_res.data(), o.prefill_chunk, (void*) st.stream, err,+ sb, sbb)) {std::fprintf(stderr, "strata serve: layer split, CUDA%d prompt path: %s\n", st.dev, err.c_str());return 1;}@@ -3550,13 +3710,32 @@ int main(int argc, char** argv) {{size_t free_b = 0, total_b = 0;cudaMemGetInfo(&free_b, &total_b);- std::printf("INFO context=%lld kv=%s kv_resident=%lld expert_slots=%lld expert_cache_mib=%lld spec=%d "+ // "Experts in VRAM" is every tier, not one card's. This used to report `xcache` alone, so a layer split+ // showed CUDA0's cache as if it were the whole GPU's: a 4-GPU 256K run read 3327 experts when the four+ // cards held 13320, and the Monitor tab was wrong by 4x for every multi-GPU config. The tiers are+ // disjoint by construction (remote_experts.cpp skips any pair a stage already claimed), so they add.+ const int64_t slots_primary = (int64_t) xcache.slots();+ const int64_t mib_primary = (int64_t) (xcache.bytes() >> 20);+ int64_t slots_all = slots_primary, mib_all = mib_primary;+ for (const auto& st : stages) {+ slots_all += (int64_t) st->cache.slots();+ mib_all += (int64_t) (st->cache.bytes() >> 20);+ }+ for (int r = 0; r < 3; ++r)+ if (o.expert_cache_remote[(size_t) r] > 0) {+ slots_all += remote_experts[(size_t) r].resident();+ mib_all += (int64_t) (remote_experts[(size_t) r].gib() * 1024.0);+ }+ std::printf("INFO context=%lld kv=%s kv_resident=%lld expert_slots=%lld expert_cache_mib=%lld "+ "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 engine=" STRATA_VERSION "\n",(long long) o.max_context, o.kv.c_str(),- (long long) (g.n_qsa_layers() > 0 && ss.qsa_states[0].kv_mode == 1- ? ss.qsa_states[0].n_slots * 4 : 0),- (long long) xcache.slots(), (long long) (xcache.bytes() >> 20), o.spec, o.mtp_max_t,+ (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),+ (long long) slots_all, (long long) mib_all,+ (long long) slots_primary, (long long) mib_primary,+ o.spec, o.mtp_max_t,o.suffix_draft, (long long) (free_b >> 20), cvec_summary.c_str(),(long long) (strata::kernels::cpu::expert_layout().total >> 20), pool.workers(), o.pcie_frac,o.spec_min_p);@@ -3776,12 +3955,17 @@ int main(int argc, char** argv) {std::array<int64_t, 3> remote_before{};std::array<int64_t, 3> launches_before{};std::array<uint64_t, 3> compact_before{}, full_before{};+ // ms_begin/ms_wait are cumulative since boot; the log line used to print them next to per-request deltas,+ // so the host time read as if it belonged to this request. Take deltas here like every other column.+ std::array<double, 3> begin_before{}, wait_before{};for (int r = 0; r < 3; ++r) if (o.expert_cache_remote[(size_t) r] > 0){remote_before[(size_t) r] = remote_experts[(size_t) r].computed();launches_before[(size_t) r] = remote_experts[(size_t) r].launched_layers();compact_before[(size_t) r] = remote_experts[(size_t) r].returned_bytes();full_before[(size_t) r] = remote_experts[(size_t) r].full_row_bytes();+ begin_before[(size_t) r] = remote_experts[(size_t) r].ms_begin();+ wait_before[(size_t) r] = remote_experts[(size_t) r].ms_wait();}cur = ids;const Clock::time_point r0 = Clock::now();@@ -3933,55 +4117,74 @@ int main(int argc, char** argv) {};// the batched path's slots are lent just before its first run and given back (refilled) before a window// reads - so the windows always see the whole expert cache - or once the prompt is read- std::vector<std::pair<int32_t, int32_t>> lent_now;- int64_t lent_chunk = 0; // the chunk the lent slots hold the prompt path's buffers for- auto refill = [&](std::string& e) -> bool {- if (lent_now.empty()) return true;- tr("refill start", (long long) lent_now.size());- for (const auto& [i, slot] : lent_now) { // D-4: queued, one wait (STRATA_REFILL_BLOCKING=1: each)+ // Every participant gives its loan back here: the rows it lent are refilled into the SAME slots from+ // the arena, the residency table is restored, and one upload puts it on every device. A stage refills+ // through its own cache and its own device - a slot refilled into the wrong cache would leave that+ // stage's cache holding an expert it does not own, which is silent and produces plausible tokens.+ auto refill_one = [&](PfPart& p, std::string& e) -> bool {+ if (p.lent.empty()) return true;+ tr("refill start", (long long) p.lent.size());+ const strata::core::OnDevice on(p.dev);+ for (const auto& [i, slot] : p.lent) { // D-4: queued, one wait (STRATA_REFILL_BLOCKING=1: each)const uint8_t* b = srcp->blob(i / g.n_expert, i % g.n_expert);const int64_t nb = (int64_t) strata::kernels::cpu::expert_layout().blob_bytes(i / g.n_expert);- if (b == nullptr || !(refill_blocking() ? xcache.fill_slot_blocking(slot, b, e, nb)- : xcache.fill_slot_queued(slot, b, e, nb)))+ if (b == nullptr || !(refill_blocking() ? p.cache->fill_slot_blocking(slot, b, e, nb)+ : p.cache->fill_slot_queued(slot, b, e, nb)))return false;host_res[(size_t) i] = slot;}- if (!xcache.sync_queued(e)) return false;- cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice);- lent_now.clear();- lent_chunk = 0;+ if (!p.cache->sync_queued(e)) return false;+ p.lent.clear();+ p.lent_chunk = 0;+ return true;+ };+ auto refill = [&](std::string& e) -> bool {+ bool any = false;+ for (PfPart& p : pf_parts)+ if (!p.lent.empty()) { any = true; if (!refill_one(p, e)) return false; }+ if (any) res_upload();return true;};// lend the slots `tokens` batched prompt tokens need: the prompt path's buffers for min(chunk, tokens- // rounded up to 256), laid out in the last of the slots it may borrow+ // rounded up to 256), laid out in the last of the slots it may borrow - per participant, out of that+ // participant's own cache, and marking only that participant's own layersauto lend = [&](int64_t tokens, std::string& e) -> bool {- if (lend_first < 0) return true; // its own buffers: nothing to lend+ if (pf_parts.empty()) return true; // its own buffers: nothing to lend+ // what this segment needs, capped by the configured chunk: a request lends only what its own+ // prompt needs, so a large chunk costs a short prompt nothingconst int64_t want = request_chunk(tokens, o.prefill_chunk);if (want <= 0) {e = "prefill: cannot lend buffers for an empty request segment";return false;}- if (!lent_now.empty()) {- if (want <= lent_chunk) return true;- if (!refill(e)) return false;- }- const int64_t slots = lend_slots(want);- if (slots <= 0 || slots > xcache.slots() - lend_first) {- e = "prefill: request-sized buffers exceed the configured lend region";- return false;- }- const int32_t first = std::max<int32_t>(lend_first, (int32_t) (xcache.slots() - slots));- if (want != sp.chunk() || first != lend_first_now) {- if (!sp.relayout(want, xcache.device_slot(first), lend_bytes(first), e)) return false;- lend_first_now = first;- }- for (size_t i = 0; i < host_res.size(); ++i)- if (host_res[i] >= first) {- lent_now.emplace_back((int32_t) i, host_res[i]);- host_res[i] = strata::core::kNotResident;+ bool any = false;+ for (PfPart& p : pf_parts) {+ if (p.first < 0) continue;+ if (!p.lent.empty()) {+ if (want <= p.lent_chunk) continue; // its current loan already covers this+ // ONLY this participant's loan goes back: `refill` would return the other participants'+ // loans too, and their buffers are still laid out in their caches - marking those slots+ // resident again would hand the next window a prompt buffer in place of an expert+ if (!refill_one(p, e)) return false;}- cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice);- lent_chunk = want;+ const strata::core::OnDevice on(p.dev);+ const int32_t first = std::max<int32_t>(p.first, (int32_t) (p.cache->slots() - part_slots(p, want)));+ if (want != p.sp->chunk() || first != p.first_now) {+ if (!p.sp->relayout(want, p.cache->device_slot(first), part_bytes(p, first), e)) return false;+ p.first_now = first;+ }+ for (int64_t l = p.lb; l < p.le; ++l) // THIS participant's layers only+ for (int64_t ex = 0; ex < g.n_expert; ++ex) {+ const size_t i = (size_t) (l * g.n_expert + ex);+ if (host_res[i] >= first) {+ p.lent.emplace_back((int32_t) i, host_res[i]);+ host_res[i] = strata::core::kNotResident;+ any = true;+ }+ }+ p.lent_chunk = want;+ }+ if (any) res_upload();return true;};apply_pending(true);@@ -4265,13 +4468,13 @@ int main(int argc, char** argv) {}return h;};- const ConvStateSizes z = conv_state_sizes(g);+ const ConvStateSizes z = conv_state_sizes(g, ss);uint64_t h_gdn = hash_dev(ss.gdn_state, z.gdn, 1469598103934665603ull);if (std::getenv("STRATA_STATE_HASH_GDN") != nullptr) { // per GDN layer: which one differs first- const size_t per = z.gdn / (size_t) g.n_gdn_layers();+ const size_t per = z.gdn / (size_t) ss.gdn_alloc;std::string s;char b[8];- for (int64_t i = 0; i < g.n_gdn_layers(); ++i) {+ for (int64_t i = 0; i < ss.gdn_alloc; ++i) {std::snprintf(b, sizeof(b), "%04llx ", (unsigned long long) (hash_dev((const uint8_t*) ss.gdn_state + i * per, per, 1469598103934665603ull) & 0xffff));s += b;}@@ -4298,9 +4501,9 @@ int main(int argc, char** argv) {return a;};const int64_t end_cell = std::min<int64_t>(((L + qs.page_size - 1) / qs.page_size) * qs.page_size,- ss.qsa_states[0].max_cells);- for (int64_t i = 0; i < g.n_qsa_layers(); ++i) {- const strata::core::QsaState& st = ss.qsa_states[i];+ ss.qsa_states[ss.qsa_primary()].max_cells);+ for (int64_t j = 0; j < ss.qsa_alloc; ++j) { // this session's owned QSA ordinals only+ const strata::core::QsaState& st = ss.qsa_states[ss.qsa_ord0 + j];h_tail = hash_dev(st.idx_tail, z.tail, h_tail);h_pool = hash_dev(st.idx_pooled, (size_t) (L / qs.idx_block) * qs.idx_dim * 4, h_pool);// KV streaming: the host copy is the identity layout and holds every cell@@ -4352,12 +4555,14 @@ int main(int argc, char** argv) {"pool + plan %.3f ms, host staging %.3f ms, commit %.3f ms\n", st,(long long) v.windows, v.ms_wait / w, v.ms_pool / w, v.ms_host / w, v.ms_commit / w);}- if (g.n_qsa_layers() > 0 && ss.qsa_states[0].kv_mode == 1) {+ if (g.n_qsa_layers() > 0 && ss.qsa_states[ss.qsa_primary()].kv_mode == 1) {// KV streaming, cumulative over the process: blocks the selections named vs blocks read from RAM+ // (this device's owned ordinals; a split's other stages hold theirs)uint64_t miss = 0, look = 0;bool over = false;- for (int64_t i = 0; i < g.n_qsa_layers(); ++i) {- const strata::kernels::KvStreamCounters c = strata::kernels::kv_stream_counters(ss.qsa_states[i].map);+ for (int64_t j = 0; j < ss.qsa_alloc; ++j) {+ const strata::kernels::KvStreamCounters c =+ strata::kernels::kv_stream_counters(ss.qsa_states[ss.qsa_ord0 + j].map);miss += c.misses; look += c.lookups; over = over || c.overflow;}std::fprintf(stderr, "strata serve: KV streaming: %.2f%% of %llu block reads hit VRAM, %.1f MiB read "@@ -4371,12 +4576,13 @@ int main(int argc, char** argv) {for (int r = 0; r < 3; ++r) if (o.expert_cache_remote[(size_t) r] > 0)std::fprintf(stderr, "strata serve: CUDA%d: %lld expert entries, %lld active layer launches, %.1f MiB returned ""(%.1f MiB with full rows) in this request; host %.0f ms staging+launching, %.0f ms "- "waiting for it (since start)\n", r + 1,+ "waiting for it in this request\n", r + 1,(long long) (remote_experts[(size_t) r].computed() - remote_before[(size_t) r]),(long long) (remote_experts[(size_t) r].launched_layers() - launches_before[(size_t) r]),(double) (remote_experts[(size_t) r].returned_bytes() - compact_before[(size_t) r]) / 1048576.0,(double) (remote_experts[(size_t) r].full_row_bytes() - full_before[(size_t) r]) / 1048576.0,- remote_experts[(size_t) r].ms_begin(), remote_experts[(size_t) r].ms_wait());+ remote_experts[(size_t) r].ms_begin() - begin_before[(size_t) r],+ remote_experts[(size_t) r].ms_wait() - wait_before[(size_t) r]);}return 0;}@@ -4657,7 +4863,8 @@ int main(int argc, char** argv) {// The sampled-token synchronization also completes every captured QSA// status readback. Retain one status per layer so a later layer cannot// hide an earlier failure; no extra synchronization or token allocation.- if (o.native_flash_attn_short) for (int64_t i = 0; i < g.n_qsa_layers(); ++i) {+ if (o.native_flash_attn_short) for (int64_t j = 0; j < ss.qsa_alloc; ++j) {+ const int64_t i = ss.qsa_ord0 + j; // the global ordinal, as the session's carve names itconst int32_t status = ss.qsa_states[i].host_step[strata::kernels::kStepCount];if (status != 0) {std::fprintf(stderr, "strata generate: native attention status %d at QSA layer %lld, position %lld\n",