From 49728cea441bb7cd34cf4bf1452303ce1a73421b Mon Sep 17 00:00:00 2001 From: sergiywith Date: Thu, 1 Oct 2026 14:08:48 +0300 Subject: [PATCH] File tier: a pack's experts.bin is read unbuffered too (Windows) setup's low-RAM packs have an experts.bin, and with it 0.1.31's tier mapped it: on an emulated 32 GB PC the mapped reads took the RAM the budget was sized for, and available RAM fell below 1 GB (#286, measured yesterday). - set_unbuffered also takes experts.bin (paths_ is the one file); read_direct reads one window per blob (a blob is contiguous there) and merges near ones as for the GGUF. - blob() hands out the stage buffers for any unbuffered source (staged()), so prefetch, begin_layer and transient work as with the GGUF in place; the mapped experts.bin path is unchanged otherwise. IQ2_XS pack with experts.bin, emulated 32 GB PC + 16 GB GPU, 16 GiB budget, 8K prompt + 256 tokens, two runs: decode 100.2 / 100.6 tok/s, done at 17.2 / 15.5 s, the prompt path's host copies 2.1 s (the GGUF in place: 84-89 tok/s, 21.5-22.1 s, 4.3 s). Greedy tokens identical unbuffered, through the cache and on 0.1.31. --- include/strata/core/expert_source.hpp | 12 +++++++----- src/core/expert_source.cpp | 24 ++++++++++++++++++------ src/program/generate.cpp | 2 +- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp index 101dcb5e..fe079de8 100644 --- a/include/strata/core/expert_source.hpp +++ b/include/strata/core/expert_source.hpp @@ -471,11 +471,11 @@ public: double file_ms() const { return (double) file_us_.load(std::memory_order_relaxed) / 1000.0; } /// Threads `prefetch` reads the GGUF with (STRATA_FETCH_THREADS, default 8). void set_fetch_threads(int n) { fetch_threads_ = n < 1 ? 1 : n; } - /// #286 (Windows): with the GGUF in place, read the experts straight from the drive (FILE_FLAG_NO_BUFFERING, - /// overlapped) instead of through the mapped files, when the file cache could not keep them 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. + /// #286 (Windows): read the experts straight from the drive (FILE_FLAG_NO_BUFFERING, overlapped) instead of + /// through the mapped files - the GGUF in place, or a pack's experts.bin - when the file cache could not keep them + /// 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. bool set_unbuffered(uint64_t ram_bytes, std::string& why); bool unbuffered() const { return !direct_.empty(); } /// #286, unbuffered: assembles the blobs of these pairs ahead of the `blob` calls that will ask for them (the @@ -523,6 +523,8 @@ private: 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_ + /// blobs assembled in the stage buffers (`blob` hands those out): the GGUF in place, or any unbuffered source + bool staged() const { return !role_ptr_.empty() || !direct_.empty(); } static constexpr uint64_t kNoComplement = detail::kNoCacheComplement; // ---- CS-T: the GGUF shards in place std::string gguf_; diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index 96a79721..26c3edf9 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -426,6 +426,7 @@ bool FileExpertSource::open(const std::string& pack_dir, int64_t n_layers, int64 file_ = f; mapping_ = m; base_ = (const uint8_t*) view; + paths_.assign(1, path); #else const int fd = ::open(path.c_str(), O_RDONLY); if (fd < 0) { err = "FileExpertSource: cannot open " + path; return false; } @@ -765,7 +766,7 @@ const uint8_t* FileExpertSource::staged_blob(int64_t layer, int64_t expert) { } void FileExpertSource::prefetch(int64_t layer, const int64_t* experts, int64_t n) { - if (role_ptr_.empty() || n <= 0 || layer < 0 || layer >= n_layers_) return; + if (!staged() || n <= 0 || layer < 0 || layer >= n_layers_) return; std::vector todo; { std::lock_guard lk(stage_mu_); @@ -872,8 +873,8 @@ void FileExpertSource::fill_many(const std::vector& todo) { bool FileExpertSource::set_unbuffered(uint64_t ram_bytes, std::string& why) { #if defined(_WIN32) - if (role_ptr_.empty() || !direct_.empty()) { - why = role_ptr_.empty() ? "only for the GGUF in place" : "already unbuffered"; + 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; @@ -892,6 +893,10 @@ bool FileExpertSource::set_unbuffered(uint64_t ram_bytes, std::string& why) { } direct_.push_back(h); } + if (role_ptr_.empty()) { // experts.bin: blob() now assembles into the stage buffers, sized for the largest blob + std::lock_guard lk(stage_mu_); + for (uint64_t b : layer_blob_bytes_) stage_blob_ = std::max(stage_blob_, b); + } return true; #else (void) ram_bytes; @@ -929,6 +934,13 @@ bool FileExpertSource::read_direct(const Fill* fills, size_t n) const { for (size_t k = 0; k < n; ++k) { const Fill& f = fills[k]; if (f.dst == nullptr || f.layer < 0 || f.e < 0 || f.layer >= n_layers_ || f.e >= n_expert_) return false; + if (role_ptr_.empty()) { // experts.bin: the blob is one contiguous range + const uint64_t per = layer_blob_bytes_[(size_t) f.layer]; + const uint64_t off = layer_offsets_[(size_t) f.layer] + (uint64_t) f.e * per; + const uint64_t a0 = off / kSector * kSector, a1 = (off + per + kSector - 1) / kSector * kSector; + sc.win.push_back({0, a0, a1 - a0, off - a0, per, 0, f.dst, 0, 0}); + continue; + } uint64_t at = 0; for (int r = 0; r < 3; ++r) { const size_t i = (size_t) (3 * f.layer + r); @@ -1134,7 +1146,7 @@ void RouterLookahead::run() { void FileExpertSource::begin_layer(int64_t layer, const int32_t* ids, int64_t k) { (void) ids; (void) k; - if (role_ptr_.empty()) return; + if (!staged()) return; std::lock_guard lk(stage_mu_); if (layer != last_layer_) { ++epoch_; @@ -1143,7 +1155,7 @@ void FileExpertSource::begin_layer(int64_t layer, const int32_t* ids, int64_t k) } bool FileExpertSource::transient(int64_t layer, int64_t expert) const { - if (role_ptr_.empty() || layer < 0 || expert < 0 || layer >= n_layers_ || expert >= n_expert_) return false; + if (!staged() || layer < 0 || expert < 0 || layer >= n_layers_ || expert >= n_expert_) return false; const size_t index = (size_t) layer * (size_t) n_expert_ + (size_t) expert; if (complement_ready_ && index < complement_offsets_.size() && complement_offsets_[index] != kNoComplement) return false; @@ -1672,7 +1684,7 @@ const uint8_t* FileExpertSource::blob(int64_t layer, int64_t expert) { } } if (from_files) { - if (!role_ptr_.empty()) { + if (staged()) { result = staged_blob(layer, expert); // counts its bytes } else { result = mapped_blob(layer, expert); diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 80fd7306..05f88d8e 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -2662,7 +2662,7 @@ int main(int argc, char** argv) { // #286: with a RAM budget the hottest experts live in it, and the rest are read from the drive unbuffered // when the file cache could not keep them beside the budget anyway (a 32 GB PC) - the mapped reads' page // faults are small requests on the critical path, and their pages take the RAM the budget was sized for - if (src.gguf_mode() && (o.resident_budget > 0 || std::getenv("STRATA_UNBUFFERED_LOAD") != nullptr)) { + if (o.resident_budget > 0 || std::getenv("STRATA_UNBUFFERED_LOAD") != nullptr) { std::string why; const bool ub = src.set_unbuffered(o.resident_budget, why); std::fprintf(stderr, "strata generate: the file tier reads %s (%s)\n",