From 83a7dbc0f1ccd1fe3bb2b3443389eba131f6c134 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 7 Oct 2026 20:29:45 +0200 Subject: [PATCH] prefill: the stager takes the RAM profile when the GGUF experts' pages are in the page cache (#1353) --- include/strata/core/expert_source.hpp | 6 ++++++ src/core/expert_source.cpp | 31 +++++++++++++++++++++++++++ src/prefill/prefill.cpp | 12 +++++++++++ 3 files changed, 49 insertions(+) diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp index 1bbdb413..857d61c9 100644 --- a/include/strata/core/expert_source.hpp +++ b/include/strata/core/expert_source.hpp @@ -174,6 +174,11 @@ public: /// stays valid for the layer it was asked in and the next one or two; a consumer that keeps a blob longer (the /// prompt path's stager queues a whole chunk) copies it with `copy_blob` instead. virtual bool transient(int64_t layer, int64_t expert) const { (void) layer; (void) expert; return false; } + /// #1353: of the bytes `transient` experts would be copied from, the share whose pages are in RAM right now (a + /// GGUF read in place whose page cache is warm), measured on a sample of up to `samples` experts; -1 when this + /// source cannot tell (not a file source, or no residency query on this OS). The prompt path's stager uses it to + /// pick the SSD profile (many threads, a deep ring) only for blobs that really are page faults on a disk. + virtual double cached_share(int64_t samples) const { (void) samples; return -1.0; } /// Disk sessions: the files this source read its experts from, as (role, path), resolved by the loader itself /// - the pack's experts.bin, or every GGUF tensor native_experts.txt named, per layer and role /// ("expert blk.L.ffn_up"). Filled by open(); empty before. @@ -587,6 +592,7 @@ public: const uint8_t* device_alias(int64_t layer, int64_t expert) const override; bool pcie_layer(int64_t layer) const override; bool transient(int64_t layer, int64_t expert) const override; + double cached_share(int64_t samples) const override; bool copy_blob(int64_t layer, int64_t expert, uint8_t* dst) override; /// CS-T: advances the assembled blobs' age (see staged_blob). void begin_layer(int64_t layer, const int32_t* ids, int64_t k) override; diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index fcf8b71e..59871e6a 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -1946,6 +1946,37 @@ bool FileExpertSource::transient(int64_t layer, int64_t expert) const { return override_.empty() || override_[index] == nullptr; } +double FileExpertSource::cached_share(int64_t samples) const { +#if defined(__linux__) + if (role_ptr_.empty() || samples <= 0 || n_layers_ <= 0 || n_expert_ <= 0) return -1.0; + // an even spread over the (layer, expert) grid, every page of each of the expert's three slices through mincore + const int64_t total = (int64_t) n_layers_ * (int64_t) n_expert_; + const int64_t n = std::min(samples, total); + uint64_t pages = 0, resident = 0; + std::vector vec; + for (int64_t s = 0; s < n; ++s) { + const int64_t idx = std::min(total - 1, (s * total) / n + (s * 7919) % std::max(1, total / n)); + const int64_t layer = idx / n_expert_, expert = idx % n_expert_; + if (!transient(layer, expert)) continue; + for (int r = 0; r < 3; ++r) { + const size_t i = (size_t) (3 * layer + r); + if (role_ptr_[i] == nullptr || role_bytes_[i] == 0) continue; + const uint8_t* p = role_ptr_[i] + (size_t) ((uint64_t) expert * role_bytes_[i]); + const uintptr_t pg = 4096, a = (uintptr_t) p & ~(pg - 1); + const size_t len = (size_t) ((uintptr_t) p + role_bytes_[i] - a), np = (len + pg - 1) / pg; + vec.assign(np, 0); + if (mincore((void*) a, len, vec.data()) != 0) return -1.0; + pages += np; + for (unsigned char v : vec) resident += v & 1; + } + } + return pages ? (double) resident / (double) pages : -1.0; +#else + (void) samples; + return -1.0; // no cheap residency query for a file mapping here: the caller keeps its SSD assumption +#endif +} + bool FileExpertSource::copy_blob(int64_t layer, int64_t expert, uint8_t* dst) { if (base_ == nullptr || dst == nullptr || layer < 0 || expert < 0 || layer >= n_layers_ || expert >= n_expert_) return false; diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 23eaf1dd..e945194d 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -933,6 +933,18 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, co bool files = false; for (int64_t l = 0; src != nullptr && !files && l < g.n_layers; ++l) for (int64_t e = 0; !files && e < g.n_expert; ++e) files = src->transient(l, e); + // #1353: ... but a source answers transient() for every blob it copies out of the GGUF, including the ones whose + // pages are already in the page cache (a Q8 pack on a big-RAM box with a warm cache). Those copies are memcpy + // from RAM, where 32 threads and a 128-deep ring ran 4-7x slower than the defaults. So ask the OS: the SSD + // profile only when fewer than 90% of a sample of the blobs' pages are resident (-1: it cannot tell, keep it). + if (files && !stv) { + const char* ev = std::getenv("STRATA_STAGER_SSD"); // 1 / 0: force the profile (A/B, support) + if (ev != nullptr && ev[0] != '\0') files = ev[0] != '0'; + else if (const double warm = src->cached_share(256); warm >= 0.9) { + files = false; + std::fprintf(stderr, "prefill: the GGUF experts are %.0f%% in the page cache; the stager takes the RAM profile\n", warm * 100.0); + } + } const int threads = stv ? std::clamp(std::atoi(stv), 1, 32) : files ? 32 : std::max(2, std::min(4, hw / 4)); if (files && std::getenv("STRATA_STAGER_RING") == nullptr) m.stager->kRing = 4 * threads; if (!m.stager->init((size_t) MAXBLOB(), threads)) ok = false;