From 88f4146c53ed3cf56e42c29c59becdd33c6b8663 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Thu, 1 Oct 2026 09:22:58 +0200 Subject: [PATCH] #340 prototype: a split's streamed ring is 96 slots when most experts are resident (STRATA_SPLIT_RING); STRATA_SPLIT_SMALL_OWN=S keeps an S-token chunk's buffers per lending stage for the session; --no-prefill-borrow on a split books the ring (it no longer starves the draft head) One GPU: unchanged (all three rules are for layer splits only). --- include/strata/prefill/prefill.hpp | 3 ++ src/prefill/prefill.cpp | 4 ++- src/program/generate.cpp | 58 +++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/include/strata/prefill/prefill.hpp b/include/strata/prefill/prefill.hpp index 38f64a91..da225d3b 100644 --- a/include/strata/prefill/prefill.hpp +++ b/include/strata/prefill/prefill.hpp @@ -73,6 +73,9 @@ public: /// ring (a big one only pays when the copy engine, not the host copies, is the limit); set before bytes_needed. static void set_pinned_share(double share); static double pinned_share(); + /// #340: the streamed ring's slot count for chunks that stream every expert, instead of the pinned-share rule + /// (0 = that rule). Set before any `bytes_needed`/`init` (both count the ring); STRATA_PREFILL_RING still wins. + static void set_ring_override(int slots); /// Device bytes `init` needs for a chunk of `chunk` tokens (what a borrowed region must hold). static uint64_t bytes_needed(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk); diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 117cae95..278f92e6 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -86,9 +86,10 @@ double g_pinned_share = 1.0; // 8192-token chunks: 96 slots 1153 tok/s, 384 1294 (the next layer's experts arrive during its attention half) - // and 96 when a large share goes through host copies (IQ3_S on 64 GB, a third unpinned: 96 slots 1216, 256 1070 - // the host copies are the limit and the bigger ring only takes cache slots). STRATA_PREFILL_RING overrides. +int g_ring_override = 0; // #340: set by a layer split (Prefill::set_ring_override); 0 = the rule below inline int ring_slots(size_t T) { const char* v = std::getenv("STRATA_PREFILL_RING"); - const int r = v ? std::atoi(v) : (g_pinned_share >= 0.9 ? 384 : 96); + const int r = v ? std::atoi(v) : g_ring_override > 0 ? g_ring_override : (g_pinned_share >= 0.9 ? 384 : 96); if (v && r == STAGE) return STAGE; // Explicit opt-in to routed-only staging, including large chunks. const int big = r < 16 ? 16 : r > RING_MAX ? RING_MAX : r; return (int64_t) T >= stream_all_min() ? big : STAGE; @@ -843,6 +844,7 @@ bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t return true; } void Prefill::set_pinned_share(double share) { g_pinned_share = share; } +void Prefill::set_ring_override(int slots) { g_ring_override = slots > 0 ? slots : 0; } double Prefill::pinned_share() { return g_pinned_share; } uint64_t Prefill::bytes_needed(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk) { diff --git a/src/program/generate.cpp b/src/program/generate.cpp index e55595ab..8a513a1e 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -2144,7 +2144,16 @@ int main(int argc, char** argv) { // 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; + // (#340: the estimate predates the streamed ring: from 1024-token chunks the prompt path also holds a ring of + // whole expert blobs, which a split without borrowing sizes at 96 (Prefill::set_ring_override below) and books + // here - without it a `--no-prefill-borrow` split filled the cards and the draft head no longer fit) + const int64_t split_ring_mib = + (multi_gpu && !pf_borrow && o.prefill_chunk >= 1024) + ? (int64_t) ((96ull * (uint64_t) strata::kernels::cpu::expert_layout().max_blob + (1ull << 20) - 1) >> 20) + : 0; + if (split_ring_mib > 0) strata::prefill::Prefill::set_ring_override(96); + const int64_t split_pf_mib = + (o.prefill_chunk > 0 && !pf_borrow) ? 160 + (o.prefill_chunk * 680) / 1024 + split_ring_mib : 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 @@ -3657,6 +3666,22 @@ int main(int argc, char** argv) { : (uint64_t) (xc.slots() - first) * (uint64_t) strata::kernels::cpu::expert_layout().max_blob; }; + // #340: a layer split whose caches already hold most experts streams few of them through the prompt path, so + // the 384-slot ring (sized for a card that streams nearly every expert of a chunk) only makes every stage's + // loan bigger: 96 slots (0.1.30's ring here) when >= 75% of the (layer, expert) pairs are resident. One GPU + // keeps the pinned-share rule. STRATA_SPLIT_RING=N: N slots on a split; 0: the pinned-share rule. + if (multi_gpu && !host_res.empty()) { + int64_t res_n = 0; + for (const int32_t r : host_res) res_n += r >= 0; + const double res_share = (double) res_n / (double) host_res.size(); + const char* v = std::getenv("STRATA_SPLIT_RING"); + const int ring = v ? std::atoi(v) : (res_share >= 0.75 ? 96 : 0); + if (ring > 0) { + strata::prefill::Prefill::set_ring_override(ring); + std::fprintf(stderr, "strata serve: layer split: %.0f%% of the experts resident, the prompt path's " + "streamed ring %d slots\n", 100.0 * res_share, ring); + } + } std::vector 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 @@ -3863,6 +3888,37 @@ int main(int argc, char** argv) { return 1; } mem_mark("the head and the prompt path"); + // #340: STRATA_SPLIT_SMALL_OWN=S (tokens): on a layer split, every stage that borrows keeps the slots for an + // S-token chunk's buffers for the whole session (0.1.29's own buffers, carved from the tail of its cache): + // a request of at most S prompt tokens then lends, streams and refills nothing, a longer one lends (and + // refills) only the slots above them. Their experts stay non-resident (the CPU / PCIe path computes them). + if (multi_gpu && !pf_parts.empty() && !host_res.empty()) { + const char* v = std::getenv("STRATA_SPLIT_SMALL_OWN"); + const int64_t S = v ? request_chunk(std::atoll(v), o.prefill_chunk) : 0; + int64_t evicted = 0; + for (PfPart& p : pf_parts) { + if (S <= 0 || p.first < 0) continue; + const int32_t first_s = std::max(p.first, (int32_t) (p.cache->slots() - part_slots(p, S))); + int64_t n = 0; + for (int64_t l = p.lb; l < p.le; ++l) + for (int64_t ex = 0; ex < g.n_expert; ++ex) { + int32_t& r = host_res[(size_t) (l * g.n_expert + ex)]; + if (r >= first_s) { r = strata::core::kNotResident; ++n; } + } + evicted += n; + std::fprintf(stderr, "strata serve: CUDA%d keeps %lld slots for %lld-token prompts (%lld experts " + "no longer resident)\n", p.dev < 0 ? 0 : p.dev, + (long long) (p.cache->slots() - first_s), (long long) S, (long long) n); + } + if (evicted > 0) { + if (d_res != nullptr) + cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice); + for (auto& st : stages) { + const strata::core::OnDevice on(st->dev); + cudaMemcpy(st->d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice); + } + } + } // the penalty-history buffer: one row per verify-window row (`penalty_rows`), each the last // `penalty_last_n` tokens that row's pick follows, -1 padded in front. Allocated once at the cap for // the widest window; a request without penalties gets a null buffer and takes the byte-for-byte