From 7d7da881779a2e9abe6838a929617ef139c06de8 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Tue, 13 Oct 2026 04:14:29 +0200 Subject: [PATCH] Opt-in STRATA_PROMPT_DIRECT=1 (Linux): with the file tier read through the file cache, the prompt path's batched expert reads go unbuffered (merged O_DIRECT requests); the same bytes --- include/strata/core/expert_source.hpp | 5 +++++ src/core/expert_source.cpp | 28 +++++++++++++++++++++++++-- src/program/generate.cpp | 9 +++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp index 5d16d997..c25c79d5 100644 --- a/include/strata/core/expert_source.hpp +++ b/include/strata/core/expert_source.hpp @@ -656,6 +656,10 @@ public: /// expert bytes outside it; switches either way (startup only, nothing reading). Returns whether unbuffered. bool recheck_unbuffered(std::string& why); bool unbuffered() const { return !direct_.empty(); } + /// MODEL-UPDATE round 2 (Linux, opt-in STRATA_PROMPT_DIRECT=1): with the file tier read through the file cache (the + /// decode's choice: re-routed experts come back from it), the prompt path's batched stager reads (copy_blobs) go + /// unbuffered instead - the same bytes, read in merged O_DIRECT requests. False when not opened (and why). + bool open_prompt_direct(std::string& why); /// pp-opt (Windows, experts.bin, unbuffered): close the mapped view once startup no longer needs it. NTFS runs /// the unbuffered reads of a mapped file one at a time - on a PCIe 4 NVMe (WD SN580) 2 MiB reads at queue depth /// 32 measured 2.35 GB/s with experts.bin mapped and 3.57 GB/s without. After this every file read goes through @@ -761,6 +765,7 @@ private: /// Windows: experts.bin mapped again as `open` maps it (begin/end_startup_unbuffered). bool map_view(std::string& why); std::vector paths_; ///< the mapped files, as maps_ + std::vector pdirect_; ///< open_prompt_direct: O_DIRECT fds for copy_blobs only std::vector direct_; ///< #286: per file, an unbuffered handle (Windows) or O_DIRECT fd (Linux) std::vector role_file_; ///< 3 x n_layers: index into maps_ / direct_ /// blobs assembled in the stage buffers (`blob` hands those out): the GGUF in place, or any unbuffered source diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index ad28f56f..126d65ef 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -1033,6 +1033,8 @@ void FileExpertSource::close() { paths_.clear(); for (void* h : direct_) close_direct(h); direct_.clear(); + for (void* h : pdirect_) close_direct(h); + pdirect_.clear(); { std::lock_guard lk(stage_mu_); // the buffers' last copies end before they are freed (#1237) @@ -1631,6 +1633,28 @@ bool FileExpertSource::open_direct(std::string& why) { #endif } +bool FileExpertSource::open_prompt_direct(std::string& why) { +#if defined(__linux__) + if (base_ == nullptr || paths_.empty()) { why = "no expert files open"; return false; } + if (!direct_.empty()) { why = "the file tier reads unbuffered already"; return false; } + if (!pdirect_.empty()) return true; + for (const std::string& path : paths_) { + const int fd = ::open(path.c_str(), O_RDONLY | O_DIRECT | O_CLOEXEC); + if (fd < 0) { + why = "cannot open " + path + " with O_DIRECT (" + std::strerror(errno) + ")"; + for (void* d : pdirect_) close_direct(d); + pdirect_.clear(); + return false; + } + pdirect_.push_back((void*) (intptr_t) fd); + } + return true; +#else + why = "Linux only (NTFS serves unbuffered reads of a mapped file one at a time)"; + return false; +#endif +} + bool FileExpertSource::set_unbuffered(uint64_t ram_bytes, std::string& why) { #if defined(_WIN32) || defined(__linux__) if (base_ == nullptr || paths_.empty() || !direct_.empty()) { @@ -2041,7 +2065,7 @@ bool FileExpertSource::read_direct(const Fill* fills, size_t n) const { continue; } } - sc.req.push_back({(int) (intptr_t) direct_[(size_t) x.file], x.a0, x.size, total}); + sc.req.push_back({(int) (intptr_t) (direct_.empty() ? pdirect_ : direct_)[(size_t) x.file], x.a0, x.size, total}); total += x.size; last_file = x.file; x.req = sc.req.size() - 1; @@ -2499,7 +2523,7 @@ bool FileExpertSource::copy_blob(int64_t layer, int64_t expert, uint8_t* dst) { // copy_blob made one ~2 MB request each - and NTFS serves the unbuffered reads of a mapped file one at a time, so the // request size is what sets the rate (see read_direct). The blobs the RAM copy holds are copied as before. bool FileExpertSource::copy_blobs(const int32_t* layers, const int32_t* experts, uint8_t* const* dst, size_t n) { - if (direct_.empty() || n <= 1) return ExpertSource::copy_blobs(layers, experts, dst, n); + if ((direct_.empty() && pdirect_.empty()) || n <= 1) return ExpertSource::copy_blobs(layers, experts, dst, n); thread_local std::vector fills; fills.clear(); uint64_t bytes = 0; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 68cef95e..2c4c8f4d 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -6936,6 +6936,15 @@ int main(int argc, char** argv) { const bool ub = src.recheck_unbuffered(why); std::fprintf(stderr, "strata generate: the file tier reads %s%s (%s)\n", ub ? "unbuffered" : "through the file cache", ub == was ? "" : " (changed)", why.c_str()); + // MODEL-UPDATE round 2 (opt-in STRATA_PROMPT_DIRECT=1): the prompt path's batched reads unbuffered while + // decode keeps the file cache (z440, UD-Q4_K_XL, 39 GiB budget: all-unbuffered doubled the 6K prompt but + // cost decode 9-13%). The same bytes either way. + if (!ub && [] { const char* v = std::getenv("STRATA_PROMPT_DIRECT"); return v != nullptr && v[0] == '1'; }()) { + std::string pw; + const bool pd = src.open_prompt_direct(pw); + std::fprintf(stderr, "strata generate: the prompt path's expert reads %s\n", + pd ? "go unbuffered (STRATA_PROMPT_DIRECT=1)" : ("stay buffered: " + pw).c_str()); + } } #endif }