From d161e25ff6b0a11336ed0ae18a47b0c05b406d95 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 3 Oct 2026 18:12:53 +0200 Subject: [PATCH] File tier: decide unbuffered reads from the expert bytes it reads, again once the RAM copy is built (#577) 0.1.38 made the choice before the resident set existed, from the budget asked for, against every shard's bytes (dense weights and the PLE table included). On a 96 GB PC with UD-Q4_K_XL (budget 72 GiB, 48.94 GiB really resident) that read 83.0 - 72 - 4 = 7 GiB of room against 103.7 GiB of files, so every refill after a prompt read the drive: prompts 15-40% slower than 0.1.34. - platform::file_cache_keeps(avail, arena, read): the decision, unit-tested in platform_memory_test with the reporter's numbers and the 32/64 GB PCs of #357/#362. - set_unbuffered: the arena is at most every expert's bytes, and the cache has to keep only the experts outside it. - recheck_unbuffered, after pin_cache_complement: the same decision from the RAM the copy really holds and the expert bytes outside it; switches either way (startup, nothing reading) and logs the numbers. - The 32 and 64 GB PCs still read unbuffered (the profile fill and the copy too, as before). Same tokens either way: only how the same bytes are read changes. --- include/strata/core/expert_source.hpp | 7 +++ include/strata/core/pinned.hpp | 5 ++- include/strata/platform/memory.hpp | 11 +++++ src/core/expert_source.cpp | 64 ++++++++++++++++++++++++--- src/core/pinned.cu | 20 ++++++--- src/platform/memory_test.cpp | 39 +++++++++++++++- src/program/generate.cpp | 10 +++++ 7 files changed, 141 insertions(+), 15 deletions(-) diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp index 4c4d12f5..3fe753f2 100644 --- a/include/strata/core/expert_source.hpp +++ b/include/strata/core/expert_source.hpp @@ -480,8 +480,14 @@ public: /// beside `ram_bytes` (the RAM budget), cached now or not (their mapped pages would land in the working set); see /// experts_unbuffered. The mapped reads' page faults are one small request each, and the pages they bring in /// take the RAM the budget was sized for. `why` says what decided. + /// #577: `ram_bytes` counts up to every expert, and only the experts outside it are what the cache must keep. bool set_unbuffered(uint64_t ram_bytes, std::string& why); + /// #577: the same decision once the RAM copy is built (pin_cache_complement), from the RAM it really holds and the + /// 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(); } + /// Every expert's bytes (n_layers x n_expert blobs). + uint64_t expert_bytes() const; /// #286, unbuffered: assembles the blobs of these pairs ahead of the `blob` calls that will ask for them (the /// GPU cache's fill from the profile) - one batch of reads instead of one blob at a time. At most 64 pairs. void prefetch_pairs(const std::pair* pairs, int64_t n); @@ -524,6 +530,7 @@ private: /// #286: the blobs from the drive, unbuffered: every role's 4 KiB-aligned window is read at once (overlapped) /// into this thread's aligned buffer, then copied into place. False when a read fails. bool read_direct(const Fill* fills, size_t n) const; + bool open_direct(std::string& why); std::vector paths_; ///< the mapped files, as maps_ std::vector direct_; ///< #286: per file, an unbuffered overlapped handle (Windows) std::vector role_file_; ///< 3 x n_layers: index into maps_ / direct_ diff --git a/include/strata/core/pinned.hpp b/include/strata/core/pinned.hpp index 9c9c3805..28a6f987 100644 --- a/include/strata/core/pinned.hpp +++ b/include/strata/core/pinned.hpp @@ -117,8 +117,11 @@ LoadStats load_experts_direct(const std::string& path, uint8_t* dst, const std:: /// STRATA_UNBUFFERED_LOAD=1 / 0 forces it. `why` says what decided. /// `cache_counts` false (the file tier with a RAM budget): only whether the files could be kept decides - their mapped /// pages land in the process's working set, so a partly cached file would still take the RAM the budget was sized for. +/// `read_bytes` (#577): the bytes the files are read for, when that is less than the files (the file tier reads only +/// the experts outside its RAM copy); `kAllFileBytes` = every byte of `files`. See platform::file_cache_keeps. +constexpr uint64_t kAllFileBytes = ~0ull; bool experts_unbuffered(const std::vector& files, uint64_t arena_bytes, std::string& why, - bool cache_counts = true); + bool cache_counts = true, uint64_t read_bytes = kAllFileBytes); // FNV-1a 64. Per layer, so a corrupt or short read names WHICH layer rather than just failing a whole-file // comparison - the same reason the Phase 1 tools report the first differing element. diff --git a/include/strata/platform/memory.hpp b/include/strata/platform/memory.hpp index ece6c9e4..202a3bf2 100644 --- a/include/strata/platform/memory.hpp +++ b/include/strata/platform/memory.hpp @@ -32,4 +32,15 @@ bool gpu_shared_memory_budget(const void* luid, uint64_t& budget, uint64_t& usag /// The machine's physical RAM in bytes (0 when unknown). uint64_t total_physical_memory(); +/// #357/#577: whether the OS file cache could keep the `read_bytes` the expert files are read for, beside +/// `arena_bytes` of RAM held by the engine's own copy of the experts and `margin` for everything else, with `avail` +/// bytes of RAM available. The file tier passes only the expert bytes it really reads from the files (the experts +/// outside its resident RAM copy) and the RAM that copy really holds - not every shard's bytes and the requested +/// budget, which on a 96 GB PC (#577) made the file tier read unbuffered when the cache could keep its reads. +inline bool file_cache_keeps(uint64_t avail, uint64_t arena_bytes, uint64_t read_bytes, + uint64_t margin = 4ull << 30) { + const uint64_t room = avail > arena_bytes + margin ? avail - arena_bytes - margin : 0; + return room >= read_bytes; +} + } // namespace strata::platform diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index c64909f5..bb3badfc 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -872,13 +872,14 @@ void FileExpertSource::fill_many(const std::vector& todo) { for (auto& t : th) t.join(); } -bool FileExpertSource::set_unbuffered(uint64_t ram_bytes, std::string& why) { +uint64_t FileExpertSource::expert_bytes() const { + uint64_t total = 0; + for (uint64_t b : layer_blob_bytes_) total += b * (uint64_t) n_expert_; + return total; +} + +bool FileExpertSource::open_direct(std::string& why) { #if defined(_WIN32) - if (base_ == nullptr || paths_.empty() || !direct_.empty()) { - why = !direct_.empty() ? "already unbuffered" : "no expert files open"; - return !direct_.empty(); - } - if (!experts_unbuffered(paths_, ram_bytes, why, /*cache_counts=*/false)) return false; for (const std::string& path : paths_) { const int wide = MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, nullptr, 0); std::vector w((size_t) (wide > 0 ? wide : 1), L'\0'); @@ -899,6 +900,24 @@ bool FileExpertSource::set_unbuffered(uint64_t ram_bytes, std::string& why) { for (uint64_t b : layer_blob_bytes_) stage_blob_ = std::max(stage_blob_, b); } return true; +#else + (void) why; + return false; +#endif +} + +bool FileExpertSource::set_unbuffered(uint64_t ram_bytes, std::string& why) { +#if defined(_WIN32) + if (base_ == nullptr || paths_.empty() || !direct_.empty()) { + why = !direct_.empty() ? "already unbuffered" : "no expert files open"; + return !direct_.empty(); + } + // #577: the file tier reads the experts outside the RAM copy, not every byte of the shards (dense weights, the + // PLE table), and the copy holds at most every expert - the budget asked for can be more than that + const uint64_t experts = expert_bytes(); + const uint64_t arena = std::min(ram_bytes, experts); + if (!experts_unbuffered(paths_, arena, why, /*cache_counts=*/false, experts - arena)) return false; + return open_direct(why); #else (void) ram_bytes; why = "through the file cache (not Windows)"; @@ -906,6 +925,39 @@ bool FileExpertSource::set_unbuffered(uint64_t ram_bytes, std::string& why) { #endif } +bool FileExpertSource::recheck_unbuffered(std::string& why) { +#if defined(_WIN32) + if (const char* env = std::getenv("STRATA_UNBUFFERED_LOAD"); env != nullptr && env[0] != '\0') { + why = std::string("STRATA_UNBUFFERED_LOAD=") + env; + return !direct_.empty(); + } + if (base_ == nullptr || paths_.empty()) { + why = "no expert files open"; + return false; + } + // #577: the RAM copy is built (and already out of the available RAM), so what the file cache would have to keep + // is exactly the experts outside it - the GPU cache's (refilled after a prompt borrowed their slots) and the + // ones neither holds + const uint64_t experts = expert_bytes(); + const uint64_t read = experts > complement_bytes_ ? experts - complement_bytes_ : 0; + std::string w; + const bool ub = experts_unbuffered(paths_, 0, w, /*cache_counts=*/false, read); + char head[96]; + std::snprintf(head, sizeof head, "re-checked with the RAM copy built (%.2f GiB): ", + (double) complement_bytes_ / 1073741824.0); + why = head + w; + if (ub == !direct_.empty()) return ub; + if (ub) return open_direct(why); + // through the file cache after all: the mapped reads take over (staged() stays true for the GGUF in place) + for (void* d : direct_) CloseHandle((HANDLE) d); + direct_.clear(); + return false; +#else + why = "through the file cache (not Windows)"; + return false; +#endif +} + bool FileExpertSource::read_direct(const Fill* fills, size_t n) const { #if defined(_WIN32) // NTFS runs the unbuffered reads of a file one at a time while the file is mapped or cached anywhere (the engine diff --git a/src/core/pinned.cu b/src/core/pinned.cu index e28307e0..780d93fb 100644 --- a/src/core/pinned.cu +++ b/src/core/pinned.cu @@ -503,7 +503,7 @@ LoadStats load_experts_direct(const std::string& path, uint8_t* dst, const std:: } bool experts_unbuffered(const std::vector& files, uint64_t arena_bytes, std::string& why, - bool cache_counts) { + bool cache_counts, uint64_t read_bytes) { const char* env = std::getenv("STRATA_UNBUFFERED_LOAD"); if (env != nullptr && env[0] != '\0') { why = std::string("STRATA_UNBUFFERED_LOAD=") + env; @@ -551,15 +551,21 @@ bool experts_unbuffered(const std::vector& files, uint64_t arena_by GlobalMemoryStatusEx(&ms); const uint64_t avail = ms.ullAvailPhys; // what the cache could keep beside the arena (~4 GiB for everything else) - const uint64_t room = avail > arena_bytes + (4ull << 30) ? avail - arena_bytes - (4ull << 30) : 0; - const bool keepable = room >= total_bytes; - char msg[200]; - std::snprintf(msg, sizeof msg, "%d of %d probe reads from the file cache; %.1f GiB available, %.1f GiB of files", - fast, n, (double) avail / (1ull << 30), (double) total_bytes / (1ull << 30)); + const uint64_t need = read_bytes == kAllFileBytes ? total_bytes : read_bytes; + const bool keepable = strata::platform::file_cache_keeps(avail, arena_bytes, need); + char msg[256]; + if (read_bytes == kAllFileBytes) + std::snprintf(msg, sizeof msg, "%d of %d probe reads from the file cache; %.1f GiB available, %.1f GiB of files", + fast, n, (double) avail / (1ull << 30), (double) total_bytes / (1ull << 30)); + else + std::snprintf(msg, sizeof msg, "%.1f GiB available, %.1f GiB of it still to be taken by the RAM copy, %.1f GiB " + "of experts read from the files: the file cache %s keep them", + (double) avail / (1ull << 30), (double) arena_bytes / (1ull << 30), (double) need / (1ull << 30), + keepable ? "can" : "cannot"); why = msg; return (!cached || !cache_counts) && !keepable; #else - (void) files; (void) arena_bytes; (void) cache_counts; + (void) files; (void) arena_bytes; (void) cache_counts; (void) read_bytes; why = "buffered (not Windows)"; return false; #endif diff --git a/src/platform/memory_test.cpp b/src/platform/memory_test.cpp index e5b4cde7..7346189d 100644 --- a/src/platform/memory_test.cpp +++ b/src/platform/memory_test.cpp @@ -13,7 +13,44 @@ #pragma comment(lib, "psapi.lib") #endif +namespace { +uint64_t gib(double g) { return (uint64_t) (g * 1073741824.0); } + +// #577: the file tier's unbuffered choice (true = the file cache keeps its reads, so they stay buffered) +int file_cache_keeps_cases() { + using strata::platform::file_cache_keeps; + struct Case { const char* what; double avail, arena, read; bool keeps; }; + const double experts = 71.7; // UD-Q4_K_XL's routed experts + const Case cases[] = { + // the reporter's 96 GB PC (83.0 GiB available at start): 0.1.38 passed the budget asked for and every + // shard's bytes (103.7 GiB) and went unbuffered + {"96 GB, 0.1.38's inputs (budget 72, all shards)", 83.0, 72.0, 103.7, false}, + {"96 GB, budget 72, before the copy (arena = all experts)", 83.0, experts, 0.0, true}, + {"96 GB, budget 72, copy built (48.94 resident)", 83.0 - 48.94, 0.0, experts - 48.94, true}, + {"96 GB, budget 40, before the copy", 83.0, 40.0, experts - 40.0, true}, + {"96 GB, budget 40, copy built", 83.0 - 40.0, 0.0, experts - 40.0, true}, + // #357/#362's low-RAM PCs stay unbuffered + {"64 GB, budget 40, before the copy", 55.0, 40.0, experts - 40.0, false}, + {"64 GB, budget 40, copy built", 15.0, 0.0, experts - 40.0, false}, + {"32 GB, budget 16, before the copy", 26.0, 16.0, experts - 16.0, false}, + {"32 GB, budget 16, copy built", 10.0, 0.0, experts - 16.0, false}, + {"32 GB, Q2_0 pack (34 GiB experts), budget 8", 26.0, 8.0, 34.0 - 8.0, false}, + {"nothing available", 0.0, 0.0, 1.0, false}, + {"arena larger than the RAM", 10.0, 20.0, 0.0, true}, + }; + int fail = 0; + for (const Case& c : cases) { + const bool got = file_cache_keeps(gib(c.avail), gib(c.arena), gib(c.read)); + std::printf("file_cache_keeps %-58s -> %s%s\n", c.what, got ? "buffered" : "unbuffered", + got == c.keeps ? "" : " <-- WRONG"); + fail |= got != c.keeps; + } + return fail; +} +} // namespace + int main() { + int fail_keeps = file_cache_keeps_cases(); const uint64_t bytes = 256ull << 20; void* p = std::malloc(bytes); if (p == nullptr) return 2; @@ -21,7 +58,7 @@ int main() { const strata::platform::LockResult r = strata::platform::lock_resident(p, bytes); std::printf("lock_resident: ok=%d locked=%llu MiB (%s)\n", (int) r.ok, (unsigned long long) (r.locked_bytes >> 20), r.note.c_str()); - int fail = !(r.ok && r.locked_bytes == bytes); + int fail = !(r.ok && r.locked_bytes == bytes) | fail_keeps; #if defined(_WIN32) PROCESS_MEMORY_COUNTERS pmc{}; GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof pmc); diff --git a/src/program/generate.cpp b/src/program/generate.cpp index a7c77d0e..1e5988a3 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -4082,6 +4082,16 @@ int main(int argc, char** argv) { std::fprintf(stderr, "strata generate: CPU expert residency: %s\n", err.c_str()); return 1; } + // #577: the unbuffered choice above was made before the RAM copy existed, from the budget asked for; now the + // copy is built, decide again from the RAM it really holds and the expert bytes outside it (on a 96 GB PC + // the file cache keeps those, and every refill after a prompt read the drive instead) + if (o.mmap_experts && o.resident_budget > 0) { + std::string why; + const bool was = src.unbuffered(); + 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()); + } } if (o.serve) { if (o.spec < 2 || o.mtp.empty() || o.prefill_chunk <= 0 ||