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++
prefill: the CPU share up to 3,072-token chunks, from mapped experts too, and on a layer split's stages
architectds92bce20e4bc8
diff --git a/include/strata/prefill/prefill.hpp b/include/strata/prefill/prefill.hppindex 03c4374d4..22fba8dbf 100644--- a/include/strata/prefill/prefill.hpp+++ b/include/strata/prefill/prefill.hpp@@ -168,10 +168,16 @@ public:/// The CPU expert pool (decode's, idle while a prompt is read). With STRATA_PREFILL_CPU_SHARE set, a chunk below/// stream_all_min() tokens - an agent's tool output - hands it the non-resident experts routed by at most MAXT of/// its tokens, fewest first, up to a share of the experts it would stream (`auto`: measured, where both sides end- /// together). The CPU reads them from RAM while the rest come over PCIe; their rows go to Dm's tail, as a peer's- /// do. Not bit-identical to the GPU's rows (the CPU's own activation format). Unset (default) or null: every- /// expert on the GPU. Only for a pool no other thread runs meanwhile (no batch slots). Set before `init`.+ /// together). The CPU reads them from RAM (the arena, the page-locked copy, or the mapped experts.bin's pages)+ /// while the rest come over PCIe; their rows go to Dm's tail, as a peer's do. Not bit-identical to the GPU's rows+ /// (the CPU's own activation format). Unset (default) or null: every expert on the GPU. Only for a pool no other+ /// thread runs meanwhile (no batch slots); on a layer split every stage may have it - one stage at a time takes it+ /// for a chunk. Set before `init`.void set_cpu_pool(kernels::cpu::ExpertPool* pool);+ /// With STRATA_PREFILL_CPU_SHARE set and `applies` (a pool will be set): the chunks staged after their routing - the+ /// only ones the share applies to - go up to STRATA_PREFILL_CPU_SHARE_MAX tokens (default 3072) instead of 1024.+ /// Before any `bytes_needed` (the ring and the small-chunk buffers follow the limit); `init` also sets it.+ static void arm_cpu_share(bool applies);private:static uint64_t bytes_needed_impl(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk,diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cppindex 05009250f..ecbaa1b84 100644--- a/src/prefill/prefill.cpp+++ b/src/prefill/prefill.cpp@@ -135,10 +135,13 @@ constexpr int RING_MAX = 1024; // the arrays; the ring itself is ring_s// The chunk size from which every expert streams: 1024 since 0.1.30 (was 2048). Measured on the 5070, Q2_0 / IQ2_XS,// fixed cache: 1,500-token prompts 621 -> 785 / 612 -> 735 tok/s, 2,000 727 -> 934 / 712 -> 892, 4,000 (its last// chunk) 779 -> 912 / 766 -> 844, the same output. Below ~1,000 tokens the output changed on Q2_0 (a smaller chunk-// takes other kernels), so 1024 is the floor. STRATA_PREFILL_STREAM_MIN overrides (A/B).+// takes other kernels), so 1024 is the floor. STRATA_PREFILL_STREAM_MIN overrides (A/B). With the CPU share armed+// (Prefill::arm_cpu_share) it is the share's own limit instead: the share applies to staged chunks only.+int64_t g_stream_min_share = 0;inline int64_t stream_all_min() {- static const int64_t v = [] { const char* e = std::getenv("STRATA_PREFILL_STREAM_MIN"); return e ? (int64_t) std::atoll(e) : (int64_t) 1024; }();- return v;+ static const int64_t env = [] { const char* e = std::getenv("STRATA_PREFILL_STREAM_MIN"); return e ? (int64_t) std::atoll(e) : (int64_t) -1; }();+ if (env >= 0) return env;+ return g_stream_min_share > 0 ? g_stream_min_share : (int64_t) 1024;}// STRATA_PREFILL_CPU_SHARE (opt-in; set_cpu_pool): a chunk below stream_all_min() hands the decode CPU pool - idle// while a prompt is read - the non-resident experts few of its tokens route to, instead of streaming them over PCIe.@@ -159,6 +162,19 @@ inline double cpu_share_env() { // -1: measuredreturn v;}inline bool cpu_share_on() { return cpu_share_env() != 0.0; }+// STRATA_PREFILL_CPU_SHARE_MAX (default 3072, at least 1024): with the share on, chunks below it are staged after their+// routing - so they can hand the CPU its share - instead of streaming every expert the cards do not hold. The share pays+// on chunks up to ~3K tokens, where few tokens route to many of the streamed experts; 1024 keeps the old limit.+inline int64_t cpu_share_max() {+ static const int64_t v = [] {+ const char* e = std::getenv("STRATA_PREFILL_CPU_SHARE_MAX");+ return e ? std::max<int64_t>(1024, (int64_t) std::atoll(e)) : (int64_t) 3072;+ }();+ return v;+}+// On a layer split every stage may have the pool (set_cpu_pool), and the stages read different chunks at the same time:+// one stage at a time takes it for a chunk, the other reads its chunk with the GPU alone.+std::atomic<bool> g_cpu_pool_busy{false};double g_pinned_share = 1.0;// The streamed ring: 384 slots when (nearly) every streamed expert is DMA'd from pinned RAM - measured on Q2_0,// 8192-token chunks: 96 slots 1153 tok/s, 384 1294 (the next layer's experts arrive during its attention half) -@@ -919,6 +935,8 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, coerr = "prefill: the stage's layer range is wrong";return false;}+ // the CPU share's staged-chunk limit, before the ring is sized below (a caller that sizes loans first armed it)+ arm_cpu_share(cpu_pool_ != nullptr);for (int b = 0; next_ != nullptr && b < 2; ++b)if (!m.hand[b] && cudaHostAlloc((void**) &m.hand[b], (size_t) chunk * D * 4, cudaHostAllocPortable) != cudaSuccess) {err = "prefill: the layer split's hand-off buffers";@@ -1671,6 +1689,9 @@ int64_t Prefill::ring_max_slots() {int64_t Prefill::ring_slots_for(int64_t chunk) { return ring_slots((size_t) chunk); }void Prefill::set_cpu_pool(kernels::cpu::ExpertPool* pool) { cpu_pool_ = pool; }+void Prefill::arm_cpu_share(bool applies) {+ if (applies && cpu_share_on()) g_stream_min_share = cpu_share_max();+}bool Prefill::ring_bytes_enabled() { return ring_bytes_on(); }@@ -2019,6 +2040,16 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri// `used` event recorded), so the copy stream never waits on an event that is not queued yetconst strata::kernels::cpu::ExpertLayout& lay0 = strata::kernels::cpu::expert_layout();const bool stream_all = m.ring > STAGE && T >= stream_all_min() && m.src != nullptr;+ // the CPU share: this chunk takes the pool if no other stage of a layer split has it now (released at the end+ // of the chunk, after the last layer's CPU thread is joined)+ struct PoolHold {+ bool held = false;+ ~PoolHold() { if (held) g_cpu_pool_busy.store(false, std::memory_order_release); }+ } pool_hold;+ if (cpu_pool_ != nullptr && cpu_share_on() && !stream_all) {+ bool expect = false;+ pool_hold.held = g_cpu_pool_busy.compare_exchange_strong(expect, true, std::memory_order_acq_rel);+ }const bool ps_on = stream_all && m.pp && m.pp->ps_frac > 0.0;if (m.pp && !ps_on) m.pp->ps_flag.clear();// multi-GPU: the peer's ring - issue its copies up to `limit` / give entries back (the peer device is current)@@ -2727,7 +2758,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::striint32_t* src_h = grp_mapped ? m.grp_host + 2 * m.grp_tk : m.src_host.data();if (grp_mapped) copy_i32(m.grp_dev, m.ids, T * K, m.cs);else cudaMemcpyAsync(m.ids_host.data(), m.ids, (size_t) T * K * 4, cudaMemcpyDeviceToHost, m.cs);- const bool cpu_maybe = cpu_pool_ != nullptr && cpu_share_on() && !stream_all && m.src != nullptr &&+ const bool cpu_maybe = pool_hold.held && !stream_all && m.src != nullptr &&lay.native && !m.pp && !lay.fmt.empty();// auto shares only while the layers that share cost less per non-resident expert than their// neighbours that do not. #1282 (RX 7900 GRE + 5700X3D): every share lost, auto's 0.47 by@@ -2827,19 +2858,30 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::striif (c == 0 || (m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0))continue;++nstream;- if (c <= strata::kernels::cpu::MAXT && m.src->pinned(l, e)) cand.emplace_back(c, e);+ // a blob the CPU reads from RAM: page-locked, or any other that is not assembled into a+ // short-lived buffer (the arena's unpinned part, the mapped experts.bin's pages)+ if (c <= strata::kernels::cpu::MAXT && (m.src->pinned(l, e) || !m.src->transient(l, e)))+ cand.emplace_back(c, e);}std::sort(cand.begin(), cand.end());const double share = cpu_share_env() >= 0.0 ? cpu_share_env() : cpu_arm ? m.cpu_share_now : 0.0;const size_t take = std::min(cand.size(), (size_t) std::llround(share * (double) nstream));n_stream = nstream;- if (take > 0) {- on_cpu.assign((size_t) m.g->n_expert, 0);- for (size_t i = 0; i < take; ++i) {- on_cpu[(size_t) cand[i].second] = 1;- rows_cpu += cand[i].first;+ // The CPU thread touches no ExpertSource (its blob() counts reads without a lock, and its+ // calls race with this thread's own blob_stable()/pinned() ones): the pointers are taken+ // here, and an expert without a stable blob stays on the GPU.+ for (size_t i = 0; i < cand.size() && n_cpu < (int64_t) take; ++i) {+ const int32_t e = cand[i].second;+ const uint8_t* b = m.src->blob_stable(l, e);+ if (b == nullptr) continue;+ if (on_cpu.empty()) {+ on_cpu.assign((size_t) m.g->n_expert, 0);+ cpu_blob.assign((size_t) m.g->n_expert, nullptr);}- n_cpu = (int64_t) take;+ on_cpu[(size_t) e] = 1;+ cpu_blob[(size_t) e] = b;+ rows_cpu += cand[i].first;+ ++n_cpu;}}// multi-GPU: the rows of the experts the peer computes go last, as one block [rows_local, T*K)@@ -2933,14 +2975,6 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::striif (m.cpu_ev[0] == nullptr) { cudaEventCreate(&m.cpu_ev[0]); cudaEventCreate(&m.cpu_ev[1]); }cudaEventRecord(m.cpu_ev[0], m.cs);}- // The thread touches no ExpertSource (its blob() counts reads without a lock, and its calls- // race with this thread's own blob_stable()/pinned() ones): the pointers are taken here.- cpu_blob.assign((size_t) m.g->n_expert, nullptr);- for (int32_t e = 0; e < m.g->n_expert; ++e)- if (on_cpu[(size_t) e] && (cpu_blob[(size_t) e] = m.src->blob_stable(l, e)) == nullptr) {- err = "prefill: a CPU expert has no blob";- return false;- }cpu_fut = std::async(std::launch::async, [&m, &on_cpu, &cpu_blob, &cf, &cpu_ms, src_h, l, T, r0c, this]() -> bool {const auto t0 = std::chrono::steady_clock::now();constexpr size_t AB = strata::kernels::cpu::kNativeActBytes;diff --git a/src/program/generate.cpp b/src/program/generate.cppindex ce8e00474..464acb948 100644--- a/src/program/generate.cpp+++ b/src/program/generate.cpp@@ -6087,6 +6087,10 @@ int main(int argc, char** argv) {// confirms every emitted token against the real model regardless of where the draft came from.const bool use_mtp = !o.mtp.empty();strata::prefill::Prefill sp;+ // the pool is idle while a prompt is read unless batch slots decode between its parts; with+ // STRATA_PREFILL_CPU_SHARE the staged-chunk limit before the chunk below sizes the loans (bytes_needed reads it)+ const bool share_pool = o.batch <= 0 && !o.no_pool;+ strata::prefill::Prefill::arm_cpu_share(share_pool);void* borrow = nullptr;uint64_t borrow_bytes = 0;int32_t lend_first = -1; // the first slot the prompt path may borrow (its largest chunk)@@ -6373,6 +6377,7 @@ int main(int argc, char** argv) {sb = st.cache.device_slot(pf_parts[i + 1].first);sbb = part_bytes(pf_parts[i + 1], pf_parts[i + 1].first);}+ if (share_pool) st.sp.set_cpu_pool(&pool); // one stage at a time takes it for a chunk (prefill.cpp)if (!st.sp.init(st.wt, g, st.ss, srcp, &st.cache, host_res.data(), o.prefill_chunk, (void*) st.stream,err, sb, sbb)) {err = "layer split, CUDA" + std::to_string(st.dev) + " prompt path: " + err;@@ -6380,8 +6385,7 @@ int main(int argc, char** argv) {}}if (multi_gpu) sp.set_stage(0, split_at[0], &stages[0]->sp);- // the pool is idle while a prompt is read unless batch slots decode between its parts- if (!multi_gpu && o.batch <= 0 && !o.no_pool) sp.set_cpu_pool(&pool);+ if (share_pool) sp.set_cpu_pool(&pool);if (!sp.init(wt, g, ss, srcp, &xcache, host_res.data(), o.prefill_chunk, main_cs, err, borrow, borrow_bytes))return err.find("do not fit") != std::string::npos ? 2 : 1;return 0;@@ -10917,6 +10921,7 @@ int main(int argc, char** argv) {int64_t pos_start = 0;int64_t spec_pos = 0; // plan v0.3 P6: where the speculative loop starts (0 = not used)strata::prefill::Prefill prefill;+ strata::prefill::Prefill::arm_cpu_share(!multi_gpu && !o.no_pool); // before the chunk below sizes the loanbool kvg_started = false; // the elastic K/V took this run's cellsdouble prefill_batched_ms = 0;std::FILE* final_r = o.dump_final_r.empty() ? nullptr : std::fopen(o.dump_final_r.c_str(), "wb");