From 978d35585262b029393439a2c564938b27943e31 Mon Sep 17 00:00:00 2001 From: sergiywith Date: Wed, 7 Oct 2026 09:27:23 +0200 Subject: [PATCH 1/4] prefill: STRATA_PREFILL_CPU_SHARE=auto hands a small chunk's least-routed experts to the idle CPU pool A chunk below stream_all_min() (1,024 tokens: an agent's tool result, a test's output) streams every routed expert outside VRAM over PCIe while the decode CPU pool sits idle and the arena holds the same experts in RAM. Opt-in, STRATA_PREFILL_CPU_SHARE=auto|x: - the non-resident experts routed by at most MAXT of the chunk's tokens, fewest first, run on the pool on a thread while the GPU streams and runs the rest; their unweighted rows go to Dm's tail (as a peer GPU's do) and the combine weights them as any other row; - auto: the share measured, each layer's CPU time per expert and the GPU's per streamed expert (CUDA events around its expert work, read after the next layer's routing sync), and the next layers hand the CPU g / (c + g), where both end together - a small CPU takes a small share; - Q2_0 layers take the pool's Q2_0 kernels' activations (ActQ, as decode's), i-quant layers the native vec_dot type. Serve without batch slots and generate, one GPU. Unset (the default): no expert moves, byte-identical to v0.1.39 (IQ2_XS 600 and 2,100 tokens, fixed cache, logits and 32 tokens). RTX 5090 + 9950X3D, 600-token prompts, ms (off / auto): - UD-Q4_K_XL: 1,548 / 1,369, 1,528 / 1,296 (share 0.65; KL between 0.006); - Q2_0: 472 / 445, 491 / 448; - IQ2_XS: 493 / 481, 502 / 460, 527 / 529; 250 tokens 424 / 355, 402 / 382, 379 / 367 (KL to STRATA_PREFILL_MMQ=0 0.0053 off, 0.0080 auto); - IQ2_XS on 2 AVX2 workers (emulated): 512 / 487 (share 0.49). --- include/strata/prefill/prefill.hpp | 12 ++ src/prefill/prefill.cpp | 180 ++++++++++++++++++++++++++++- src/program/generate.cpp | 8 +- 3 files changed, 196 insertions(+), 4 deletions(-) diff --git a/include/strata/prefill/prefill.hpp b/include/strata/prefill/prefill.hpp index 037385a6..03c4374d 100644 --- a/include/strata/prefill/prefill.hpp +++ b/include/strata/prefill/prefill.hpp @@ -23,6 +23,7 @@ #include namespace strata::core { class PeerExperts; } +namespace strata::kernels::cpu { class ExpertPool; } namespace strata::prefill { @@ -34,6 +35,8 @@ struct PrefillStats { int64_t experts_streamed = 0; ///< expert blobs copied host -> device int64_t experts_dma = 0; ///< ...of which straight from the pinned arena (no CPU copy) int64_t experts_resident = 0; ///< expert-layer groups served from the VRAM tier + int64_t experts_cpu = 0; ///< ...computed on the CPU pool instead of streamed (STRATA_PREFILL_CPU_SHARE) + double cpu_share = 0; ///< ...the share of the streamed ones it took last (measured by default) double ms_ple = 0; }; @@ -162,9 +165,18 @@ public: /// run `init`. bool set_stage_helper(Prefill* helper, std::string& err); + /// 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`. + void set_cpu_pool(kernels::cpu::ExpertPool* pool); + private: static uint64_t bytes_needed_impl(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk, bool owned_pages); + kernels::cpu::ExpertPool* cpu_pool_ = nullptr; ///< set_cpu_pool // Stage-1 pipeline: intermediate stages return after handing their chunk to // the direct successor. The public run() drains the chain once at prompt end. bool run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::string& err); diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index c5cb4bac..ee3a1ae9 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -9,6 +9,8 @@ #include "strata/core/layout.hpp" #include "strata/kernels/cpu/expert.hpp" #include "strata/kernels/native_qsa.hpp" +#include "strata/kernels/cpu/native_expert.hpp" +#include "strata/kernels/cpu/pool.hpp" #include "strata/kernels/native_qsa_indexer.hpp" #include "strata/kernels/ngram.hpp" #include "strata/kernels/ple.hpp" @@ -138,6 +140,24 @@ 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; } +// 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. +// `auto`: the share measured, each layer's CPU time per expert and the GPU's per streamed expert (CUDA events around +// its expert work, read after the next layer's routing sync - the host reaches the combine long before the GPU does) +// as running means, and the next layers hand the CPU g / (c + g) of them, where both sides end together - so a small +// CPU takes a small share. x: a fixed x. Unset or 0: every expert on the GPU (the default). RTX 5090 + 9950X3D, +// 600-token prompts: UD-Q4_K_XL 1,528-1,548 -> 1,296-1,369 ms (share 0.65), Q2_0 472-491 -> 445-448, IQ2_XS 507 -> 490 +// (250 tokens 402 -> 368); IQ2_XS on 2 AVX2 workers 512 -> 487 (share 0.49). +inline double cpu_share_env() { // -1: measured + static const double v = [] { + const char* e = std::getenv("STRATA_PREFILL_CPU_SHARE"); + if (e == nullptr) return 0.0; + if (std::strcmp(e, "auto") == 0) return -1.0; + return std::clamp(std::atof(e), 0.0, 1.0); + }(); + return v; +} +inline bool cpu_share_on() { return cpu_share_env() != 0.0; } 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) - @@ -618,6 +638,20 @@ struct Prefill::Impl { std::vector bounds_host; std::unique_ptr mmq_ctx; std::vector ids_host, slot_host, src_host, cnt, off; + // set_cpu_pool: a small chunk's CPU experts. The layer's MoE input (T x N, from `mixed`) and the rows the pool + // writes (Dm's tail, in Dm's row order), both pinned; and the pool's per-token activations and jobs. + float *cpu_x = nullptr, *cpu_rows = nullptr; + size_t cpu_x_n = 0, cpu_rows_n = 0; + std::vector cpu_nact; + std::vector cpu_actq; // a Q2_0 layer's activations (the pool's Q2_0 kernels read ActQ) + std::vector cpu_jobs; + // the measured share: running means of the CPU's ms per expert and the GPU's per streamed expert, and the share + // they balance at (cpu_share_env) + double cpu_c_ms = 0, cpu_g_ms = 0, cpu_share_now = 0.5; + cudaEvent_t cpu_ev[2] = {}; // the GPU's expert work of the last CPU-sharing layer, read once the stream is done + bool cpu_pend = false; + double pend_cpu_ms = 0; + int64_t pend_n_cpu = 0, pend_n_gpu = 0; // The grouping tables in mapped pinned memory, [ids | slot | src] of T_max * K each, then the MMQ bounds: kernels // read and write them in place. A cudaMemcpyAsync of them queues behind the expert blobs the copy stream already // holds (up to `ring` of them, ~70 us each), and the GPU idles meanwhile - measured 4.2 s of a 128K prompt's @@ -734,6 +768,10 @@ void Prefill::release() { if (impl_->kv_released) cudaEventDestroy(impl_->kv_released); if (impl_->kv_ready) cudaEventDestroy(impl_->kv_ready); if (impl_->grp_host) cudaFreeHost(impl_->grp_host); + for (float* p : {impl_->cpu_x, impl_->cpu_rows}) + if (p) cudaFreeHost(p); + for (cudaEvent_t e : impl_->cpu_ev) + if (e) cudaEventDestroy(e); for (void* p : impl_->owned) cudaFree(p); } @@ -1601,6 +1639,7 @@ 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; } bool Prefill::ring_bytes_enabled() { return ring_bytes_on(); } @@ -2578,6 +2617,12 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri } size_t n_order = 0; // the routed experts (the debug report; unknown when fused) bool peer_now = false; // multi-GPU: the peer computed rows of this layer (MMQ path only) + // set_cpu_pool: this layer's CPU experts (rows [T * K - rows_cpu, T * K) of Dm), computed on a thread + // that reads on_cpu: declared first, so the future (which waits for the thread) goes first + std::vector on_cpu; + double cpu_ms = 0; // the thread's time (the measured share) + std::future cpu_fut; + int64_t rows_cpu = 0, n_cpu = 0, n_stream = 0; if (fused_l) { if (static bool said = false; !said) { said = true; @@ -2647,12 +2692,39 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri int32_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 && + lay.native && !m.pp && !lay.fmt.empty(); + if (cpu_maybe) { + const size_t want = (size_t) T * N; + if (m.cpu_x_n < want) { + if (m.cpu_x) cudaFreeHost(m.cpu_x); + m.cpu_x = nullptr; + m.cpu_x_n = 0; + if (cudaHostAlloc((void**) &m.cpu_x, want * sizeof(float), cudaHostAllocDefault) != cudaSuccess) { + err = "prefill: cannot allocate the CPU experts' activations"; + return false; + } + m.cpu_x_n = want; + } + cudaMemcpyAsync(m.cpu_x, m.mixed, want * sizeof(float), cudaMemcpyDeviceToHost, m.cs); + } // #579: a stall here is the GPU (this layer's attention and router, or the previous layer's // work), not the host: the watchdog's report says so (only its text changes) core::progress_at("reading the prompt (batched): waiting for the GPU (attention, router) at layer", l, p0); cudaStreamSynchronize(m.cs); core::progress_at("reading the prompt (batched): layer", l, p0); + if (m.cpu_pend) { // the measured share: the last CPU-sharing layer's GPU time is final now + m.cpu_pend = false; + float g_ms = 0; + if (cudaEventElapsedTime(&g_ms, m.cpu_ev[0], m.cpu_ev[1]) == cudaSuccess) { + constexpr double a = 0.25; + const double c = m.pend_cpu_ms / (double) m.pend_n_cpu, g = g_ms / (double) m.pend_n_gpu; + m.cpu_c_ms = m.cpu_c_ms > 0 ? m.cpu_c_ms + a * (c - m.cpu_c_ms) : c; + m.cpu_g_ms = m.cpu_g_ms > 0 ? m.cpu_g_ms + a * (g - m.cpu_g_ms) : g; + m.cpu_share_now = std::clamp(m.cpu_g_ms / (m.cpu_c_ms + m.cpu_g_ms), 0.05, 0.9); + } + } pt.fold(); if (pe.on) { // the peer's marks so far are done: the primary waited for its last rows int pd = 0; cudaGetDevice(&pd); cudaSetDevice(pe.dev); cudaStreamSynchronize(m.pp->s); pe.fold(); cudaSetDevice(pd); @@ -2668,6 +2740,29 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri if (e < 0 || e >= m.g->n_expert) { err = "prefill: routed id out of range"; return false; } ++m.cnt[(size_t) e]; } + if (cpu_maybe) { + std::vector> cand; // (tokens, expert) + int64_t nstream = 0; + for (int32_t e = 0; e < m.g->n_expert; ++e) { + const int32_t c = m.cnt[(size_t) e]; + if (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); + } + std::sort(cand.begin(), cand.end()); + const double share = cpu_share_env() >= 0.0 ? cpu_share_env() : m.cpu_share_now; + 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; + } + n_cpu = (int64_t) take; + } + } // multi-GPU: the rows of the experts the peer computes go last, as one block [rows_local, T*K) const bool pre_mmq = mmq_plan().any && mmq_plan().layer[(size_t) l]; std::vector on_peer; @@ -2693,12 +2788,18 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri { int32_t r = 0; for (int32_t e = 0; e < m.g->n_expert; ++e) - if (on_peer.empty() || !on_peer[(size_t) e]) { m.off[(size_t) e] = r; r += m.cnt[(size_t) e]; } + if ((on_peer.empty() || !on_peer[(size_t) e]) && (on_cpu.empty() || !on_cpu[(size_t) e])) { + m.off[(size_t) e] = r; + r += m.cnt[(size_t) e]; + } // the peer's rows in order_peer's order (peer-held, then peer-streamed): its groups need them contiguous for (int kind = 1; kind <= 2 && !on_peer.empty(); ++kind) for (int32_t e = 0; e < m.g->n_expert; ++e) if (on_peer[(size_t) e] == kind) { m.off[(size_t) e] = r; r += m.cnt[(size_t) e]; } + for (int32_t e = 0; e < m.g->n_expert && !on_cpu.empty(); ++e) // the CPU's block, last + if (on_cpu[(size_t) e]) { m.off[(size_t) e] = r; r += m.cnt[(size_t) e]; } m.off[(size_t) m.g->n_expert] = r; + rows_local -= rows_cpu; } std::vector fill(m.off.begin(), m.off.end() - 1); for (int64_t i = 0; i < T * K; ++i) { @@ -2710,7 +2811,8 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri // the experts, in id order: resident ones from VRAM, the others through the staging ring std::vector order, order_peer; for (int32_t e = 0; e < m.g->n_expert; ++e) - if (m.cnt[(size_t) e] > 0 && (on_peer.empty() || on_peer[(size_t) e] != 2)) + if (m.cnt[(size_t) e] > 0 && (on_peer.empty() || on_peer[(size_t) e] != 2) && + (on_cpu.empty() || !on_cpu[(size_t) e])) (!on_peer.empty() && on_peer[(size_t) e] ? order_peer : order).push_back(e); for (int32_t e = 0; e < m.g->n_expert && !on_peer.empty(); ++e) // the peer-streamed ones last: if (on_peer[(size_t) e] == 2) order_peer.push_back(e); // their copies get the most time @@ -2730,6 +2832,65 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri cudaMemcpyAsync(m.src_dev, m.src_host.data(), (size_t) T * K * 4, cudaMemcpyHostToDevice, m.cs); } n_order = order.size(); + // set_cpu_pool: the CPU's experts on a thread while this one streams and runs the GPU's; their + // unweighted rows land in Dm's row order, so the combine weights them as any other row + if (rows_cpu > 0) { + const size_t want = (size_t) rows_cpu * N; + if (m.cpu_rows_n < want) { + if (m.cpu_rows) cudaFreeHost(m.cpu_rows); + m.cpu_rows = nullptr; + m.cpu_rows_n = 0; + const size_t cap_rows = (size_t) rows_cpu * 2; // headroom: few reallocations + if (cudaHostAlloc((void**) &m.cpu_rows, cap_rows * N * sizeof(float), cudaHostAllocDefault) != cudaSuccess) { + err = "prefill: cannot allocate the CPU experts' rows"; + return false; + } + m.cpu_rows_n = cap_rows * N; + } + const int64_t r0c = T * K - rows_cpu; + const strata::kernels::cpu::NativeFmt& cf = lay.fmt[(size_t) l]; + if (cpu_share_env() < 0.0) { + if (m.cpu_ev[0] == nullptr) { cudaEventCreate(&m.cpu_ev[0]); cudaEventCreate(&m.cpu_ev[1]); } + cudaEventRecord(m.cpu_ev[0], m.cs); + } + cpu_fut = std::async(std::launch::async, [&m, &on_cpu, &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; + // a Q2_0 layer (gate/up on the pool's Q2_0 kernels): ActQ activations, as decode's + const bool q2 = strata::kernels::cpu::q2_native_kernels(cf.gu_type); + if (q2 && m.cpu_actq.size() < (size_t) T) m.cpu_actq.resize((size_t) T); + if (!q2 && m.cpu_nact.size() < (size_t) T * AB) m.cpu_nact.resize((size_t) T * AB); + std::vector need((size_t) T, 0); + for (int32_t e = 0; e < m.g->n_expert; ++e) // the tokens the CPU's experts read + if (on_cpu[(size_t) e]) + for (int32_t r = 0; r < m.cnt[(size_t) e]; ++r) + need[(size_t) src_h[(size_t) m.off[(size_t) e] + (size_t) r]] = 1; + for (int64_t t = 0; t < T; ++t) + if (need[(size_t) t] && q2) + strata::kernels::cpu::act_quant_any(m.cpu_x + (size_t) t * N, (int) N, m.cpu_actq[(size_t) t]); + else if (need[(size_t) t]) + strata::kernels::cpu::native_quant_act(cf, m.cpu_x + (size_t) t * N, + m.cpu_nact.data() + (size_t) t * AB); + m.cpu_jobs.clear(); + for (int32_t e = 0; e < m.g->n_expert; ++e) { + if (!on_cpu[(size_t) e]) continue; + strata::kernels::cpu::ExpertJobMulti j; + j.blob = m.src->blob(l, e); + if (j.blob == nullptr) return false; + j.nt = m.cnt[(size_t) e]; + for (int r = 0; r < j.nt; ++r) { + const int64_t p = (int64_t) m.off[(size_t) e] + r; + if (q2) j.act[r] = &m.cpu_actq[(size_t) src_h[(size_t) p]]; + else j.nact[r] = m.cpu_nact.data() + (size_t) src_h[(size_t) p] * AB; + j.out[r] = m.cpu_rows + (size_t) (p - r0c) * N; + } + m.cpu_jobs.push_back(j); + } + cpu_pool_->run_split_multi_native(cf, m.cpu_jobs.data(), (int) m.cpu_jobs.size()); + cpu_ms = std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); + return true; + }); + } const size_t mmq_gub = use_mmq ? mmq::matrix_bytes(mmq_gt, 1280, N) : 0; const size_t mmq_db = use_mmq ? mmq::matrix_bytes(mmq_dt, N, 640) : 0; pt.mark(kPfGather, cs); @@ -3183,6 +3344,21 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri } } pt.mark(kPfCombine, cs); + if (cpu_fut.valid()) { // set_cpu_pool: the CPU's rows into Dm's tail + if (cpu_share_env() < 0.0) cudaEventRecord(m.cpu_ev[1], m.cs); // after the GPU's expert work + if (!cpu_fut.get()) { err = "prefill: a CPU expert has no blob"; return false; } + if (cpu_share_env() < 0.0 && n_stream > n_cpu) { + m.cpu_pend = true; + m.pend_cpu_ms = cpu_ms; + m.pend_n_cpu = n_cpu; + m.pend_n_gpu = n_stream - n_cpu; + } + stats_.cpu_share = cpu_share_env() >= 0.0 ? cpu_share_env() : m.cpu_share_now; + const int64_t r0c = T * K - rows_cpu; + cudaMemcpyAsync(m.Dm + (size_t) r0c * N, m.cpu_rows, (size_t) rows_cpu * N * sizeof(float), + cudaMemcpyHostToDevice, m.cs); + stats_.experts_cpu += n_cpu; + } if (peer_now) { // multi-GPU: the peer's rows are in Dm (or, without P2P, in host memory) cudaStreamWaitEvent(m.cs, m.pp->ev_done, 0); if (!m.pp->p2p) diff --git a/src/program/generate.cpp b/src/program/generate.cpp index e1d3deab..a78d0886 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -6329,6 +6329,8 @@ 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 (!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; @@ -10911,6 +10913,7 @@ int main(int argc, char** argv) { } if (borrow == nullptr) std::fprintf(stderr, "strata generate: prompt path allocates its own buffers (no cache slots to borrow)\n"); + if (!multi_gpu && !o.no_pool) prefill.set_cpu_pool(&pool); if (!prefill.init(wt, g, ss, srcp, o.expert_cache > 0 ? &xcache : nullptr, host_res.empty() ? nullptr : host_res.data(), o.prefill_chunk, main_cs, err, borrow, borrow_bytes)) { @@ -10974,10 +10977,11 @@ int main(int argc, char** argv) { ss.ple_prev[1] = pos_start >= 1 ? (int32_t) o.tokens[(size_t) (pos_start - 1)] : -1; const strata::prefill::PrefillStats& ps = prefill.stats(); std::fprintf(stderr, "strata generate: prefill %lld tokens in %lld chunks, %.1f ms (%.1f tok/s); experts " - "streamed %lld (%lld by DMA, host %.1f ms), resident %lld; PLE %.1f ms\n", + "streamed %lld (%lld by DMA, host %.1f ms), resident %lld, on the CPU %lld (share %.2f); PLE %.1f ms\n", (long long) ps.tokens, (long long) ps.chunks, ps.ms_total, ps.ms_total > 0 ? 1000.0 * (double) ps.tokens / ps.ms_total : 0.0, (long long) ps.experts_streamed, - (long long) ps.experts_dma, ps.ms_experts_host, (long long) ps.experts_resident, ps.ms_ple); + (long long) ps.experts_dma, ps.ms_experts_host, (long long) ps.experts_resident, + (long long) ps.experts_cpu, ps.cpu_share, ps.ms_ple); } if (!kvg_started) { // the elastic K/V of a prompt that was not read in batches From 667f2ecadf7ea4ad04982d4e6a088adf1993ad8a Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 7 Oct 2026 09:39:30 +0200 Subject: [PATCH 2/4] prefill: the CPU-share thread takes no ExpertSource call (blobs taken on the prefill thread, blob_stable) blob() bumps a plain reads_ counter and, for a file source, its mapped/staged paths; calling it from the share's thread raced with the prefill thread's own blob_stable()/pinned() calls (the streamed experts). The blobs of the CPU's experts are now taken before the thread starts, so the thread reads only the arena bytes, the activations and the grouping tables, none of which the prefill thread writes until the thread is joined. --- src/prefill/prefill.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index ee3a1ae9..39cb544a 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -2620,6 +2620,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri // set_cpu_pool: this layer's CPU experts (rows [T * K - rows_cpu, T * K) of Dm), computed on a thread // that reads on_cpu: declared first, so the future (which waits for the thread) goes first std::vector on_cpu; + std::vector cpu_blob; // the CPU experts' blobs, taken on this thread (the source is not thread-safe) double cpu_ms = 0; // the thread's time (the measured share) std::future cpu_fut; int64_t rows_cpu = 0, n_cpu = 0, n_stream = 0; @@ -2853,7 +2854,15 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri if (m.cpu_ev[0] == nullptr) { cudaEventCreate(&m.cpu_ev[0]); cudaEventCreate(&m.cpu_ev[1]); } cudaEventRecord(m.cpu_ev[0], m.cs); } - cpu_fut = std::async(std::launch::async, [&m, &on_cpu, &cf, &cpu_ms, src_h, l, T, r0c, this]() -> bool { + // 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; // a Q2_0 layer (gate/up on the pool's Q2_0 kernels): ActQ activations, as decode's @@ -2875,8 +2884,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri for (int32_t e = 0; e < m.g->n_expert; ++e) { if (!on_cpu[(size_t) e]) continue; strata::kernels::cpu::ExpertJobMulti j; - j.blob = m.src->blob(l, e); - if (j.blob == nullptr) return false; + j.blob = cpu_blob[(size_t) e]; j.nt = m.cnt[(size_t) e]; for (int r = 0; r < j.nt; ++r) { const int64_t p = (int64_t) m.off[(size_t) e] + r; From 4322e2411f3ad5c095458654703015fa8e208574 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 7 Oct 2026 10:37:43 +0200 Subject: [PATCH 3/4] prefill: STRATA_DBG_NAN does not count the CPU-share rows of GU and H (the GPU never writes them) With STRATA_PREFILL_CPU_SHARE on, the last rows_cpu rows of GU and H hold whatever the buffer held before; the check read them and reported them as non-finite (about 1 in 256 garbage floats), with Dm and the layer output clean. --- src/prefill/prefill.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 39cb544a..23eaf1dd 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -3384,9 +3384,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri return c; }; // (fused: GU and H hold the grouping tables and int8 rows, not floats) - const int64_t bgu = fused_l ? 0 : bad(m.GU, T * K * 1280), bdm = bad(m.Dm, T * K * N), + // (the CPU's rows, the last rows_cpu of GU and H, are never written by the GPU: not checked) + const int64_t gpu_rows = T * K - rows_cpu; + const int64_t bgu = fused_l ? 0 : bad(m.GU, gpu_rows * 1280), bdm = bad(m.Dm, T * K * N), bbo = bad(m.bo, T * N); - const int64_t bh = m.H && !fused_l ? bad(m.H, T * K * 640) : -1; + const int64_t bh = m.H && !fused_l ? bad(m.H, gpu_rows * 640) : -1; static int64_t reported = -1; if ((bgu || bdm || bbo || bh > 0) && reported != stats_.chunks) { reported = stats_.chunks; From f1ed65b92ef68dbaf68f6909f90ed0f9611a8eaf Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 7 Oct 2026 11:51:27 +0200 Subject: [PATCH 4/4] docs: STRATA_PREFILL_CPU_SHARE (opt-in) with the measured prompt times and its output difference --- docs/DETAILS.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/DETAILS.md b/docs/DETAILS.md index bcca1fc6..d385aa10 100644 --- a/docs/DETAILS.md +++ b/docs/DETAILS.md @@ -1372,6 +1372,29 @@ test images. --- +## Short prompts: let the CPU share the experts (opt-in, `STRATA_PREFILL_CPU_SHARE`) + +A prompt chunk below 1,024 tokens (an agent's tool result, a test's output, a short follow-up) streams every routed +expert that is not in VRAM over PCIe, while the CPU pool that decodes sits idle and the RAM arena already holds those +experts. `STRATA_PREFILL_CPU_SHARE=auto` hands the pool the experts few of the chunk's tokens route to, measures per +layer how long each side takes and gives the CPU the share at which both end together (`STRATA_PREFILL_CPU_SHARE=0.4` +fixes a share). It is off unless you set it, and then the output is byte-identical to the build without it. + +When on, the CPU's rows are computed in the CPU's own activation format, so the output changes in the last bits (first +token KL against off: mean 0.006, max 0.026 nats over 22 prompts; about half of the 32-token greedy answers on 500 and +1,000-token prompts are identical, the rest part at a near tie after about 23 tokens). Prompts of 2K tokens and longer +read the same either way (their chunks are above the limit). Serve without `--batch`, one GPU. + +Prompt time, medians of 10 interleaved pairs (off / auto, ms, `--expert-cache 1500`): + +| machine | 512 tokens | 1,000 tokens | 2K / 4K / 16K | +|---|---|---|---| +| RTX 5070, Ryzen 5 7600, Q2_0 | 1,376 / 1,019 (-26%) | 1,788 / 1,392 (-22%) | unchanged | +| RTX 3060, Core Ultra 7 265, IQ3_XXS | 1,620 / 1,159 (-28%) | 2,196 / 1,770 (-19%) | unchanged | +| Tesla P100, Xeon E5-2690 v4, IQ3_XXS | 4,805 / 3,126 (-35%) | 6,821 / 5,085 (-25%) | unchanged | + +--- + ## Experimental speed projection (EXPERIMENTAL, off by default) **This is an experiment, not a finished feature.** It ships with Strata but stays off unless you turn it on.