diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp index b51c8f99..3baea1aa 100644 --- a/include/strata/core/expert_source.hpp +++ b/include/strata/core/expert_source.hpp @@ -754,6 +754,7 @@ private: struct Map { const uint8_t* base = nullptr; uint64_t bytes = 0; + bool closed = false; ///< drop_mapping unmapped the view (base stays for offsets) #if defined(_WIN32) void* file = nullptr; void* mapping = nullptr; diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index 4604eb8d..7e62d619 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -545,6 +545,7 @@ bool pread_full(int fd, uint8_t* dst, uint64_t n, uint64_t off) { int FileExpertSource::spans(int64_t layer, int64_t expert, Span* out) const { if (layer < 0 || expert < 0 || layer >= n_layers_ || expert >= n_expert_) return 0; if (!role_ptr_.empty()) { + if (unmapped_) return 0; // the GGUF views are closed (drop_mapping) uint64_t at = 0; for (int r = 0; r < 3; ++r) { const size_t i = (size_t) (3 * layer + r); @@ -980,16 +981,17 @@ void FileExpertSource::close() { if (!maps_.empty()) { for (Map& m : maps_) { #if defined(_WIN32) - if (m.base != nullptr) UnmapViewOfFile((LPCVOID) m.base); + if (m.base != nullptr && !m.closed) UnmapViewOfFile((LPCVOID) m.base); if (m.mapping != nullptr) CloseHandle((HANDLE) m.mapping); if (m.file != nullptr) CloseHandle((HANDLE) m.file); #else - if (m.base != nullptr) munmap((void*) m.base, (size_t) m.bytes); + if (m.base != nullptr && !m.closed) munmap((void*) m.base, (size_t) m.bytes); if (m.fd >= 0) ::close(m.fd); #endif } maps_.clear(); base_ = nullptr; // one of the views above + unmapped_ = false; } role_ptr_.clear(); role_bytes_.clear(); @@ -1178,6 +1180,7 @@ bool FileExpertSource::copy_from_files(int64_t layer, int64_t expert, uint8_t* d } #endif if (!role_ptr_.empty()) { + if (unmapped_) return false; // drop_mapping closed the GGUF views: only the unbuffered reads remain uint64_t at = 0; for (int r = 0; r < 3; ++r) { const size_t i = (size_t) (3 * layer + r); @@ -1463,7 +1466,7 @@ void FileExpertSource::prefetch_pairs(const std::pair* pairs, } bool FileExpertSource::advise_pairs(const std::pair* pairs, int64_t n) const { - if (base_ == nullptr || !direct_.empty() || !strata::platform::read_ahead_enabled()) return false; + if (base_ == nullptr || unmapped_ || !direct_.empty() || !strata::platform::read_ahead_enabled()) return false; for (int64_t i = 0; pairs != nullptr && i < n; ++i) { const int64_t l = pairs[i].first, e = pairs[i].second; if (l < 0 || e < 0 || l >= n_layers_ || e >= n_expert_) continue; @@ -1649,7 +1652,27 @@ bool FileExpertSource::recheck_unbuffered(std::string& why) { bool FileExpertSource::drop_mapping(std::string& why) { #if defined(_WIN32) if (direct_.empty()) { why = "the reads are not unbuffered"; return false; } - if (!role_ptr_.empty() || !maps_.empty()) { why = "the GGUF in place keeps its maps (token embedding, PLE)"; return false; } + if (!role_ptr_.empty() || !maps_.empty()) { + // the GGUF in place: nothing in the engine reads the shards through these views any more (the token embedding is + // copied into pinned host memory at load, the dense weights and the head into VRAM, the PLE table has its own + // reader), so every shard goes: with one mapped, NTFS runs the unbuffered reads of that file one at a time + if (unmapped_) { why = "already closed"; return true; } + for (Map& m : maps_) { + if (m.closed) continue; + if (m.base != nullptr && !UnmapViewOfFile((LPCVOID) m.base)) { why = "UnmapViewOfFile failed"; return false; } + m.closed = true; // m.base stays: the read offsets are taken from it + if (m.mapping != nullptr) CloseHandle((HANDLE) m.mapping); + m.mapping = nullptr; + if (m.file != nullptr) CloseHandle((HANDLE) m.file); + m.file = nullptr; + } + unmapped_ = true; + char note[160]; + std::snprintf(note, sizeof note, "%zu GGUF shard%s unmapped; every expert outside RAM and VRAM is read unbuffered", + maps_.size(), maps_.size() == 1 ? "" : "s"); + why = note; + return true; + } if (base_ == nullptr || unmapped_) { why = base_ == nullptr ? "not open" : "already closed"; return unmapped_; } if (!UnmapViewOfFile((LPCVOID) base_)) { why = "UnmapViewOfFile failed"; return false; } unmapped_ = true; @@ -1661,8 +1684,22 @@ bool FileExpertSource::drop_mapping(std::string& why) { why = "every expert outside RAM and VRAM is read unbuffered"; return true; #else - why = "not Windows"; - return false; + // Linux's page cache has no such lock: only the tests close the GGUF views (STRATA_TEST_UNMAP_GGUF=1), to run the + // unmapped path the Windows file tier takes + const char* t = std::getenv("STRATA_TEST_UNMAP_GGUF"); + if (t == nullptr || t[0] == '' || std::atoi(t) == 0 || role_ptr_.empty()) { why = "not Windows"; return false; } + if (direct_.empty()) { why = "the reads are not unbuffered"; return false; } + if (unmapped_) { why = "already closed"; return true; } + for (Map& m : maps_) { + if (m.closed) continue; + if (m.base != nullptr && munmap((void*) m.base, (size_t) m.bytes) != 0) { why = "munmap failed"; return false; } + m.closed = true; + if (m.fd >= 0) ::close(m.fd); + m.fd = -1; + } + unmapped_ = true; + why = std::to_string(maps_.size()) + " GGUF shard(s) unmapped (STRATA_TEST_UNMAP_GGUF); every expert outside RAM and VRAM is read unbuffered"; + return true; #endif } @@ -2066,7 +2103,7 @@ void FileExpertSource::warm(int64_t layer, const int64_t* experts, int64_t n) { if (io_ahead_) io_enqueue(layer, e); continue; } - if (role_ptr_.empty()) continue; + if (role_ptr_.empty() || unmapped_) continue; for (int r = 0; r < 3; ++r) { const size_t i = (size_t) (3 * layer + r); const uint8_t* p = role_ptr_[i] + (size_t) ((uint64_t) e * role_bytes_[i]); @@ -2372,7 +2409,7 @@ bool FileExpertSource::transient(int64_t layer, int64_t expert) const { 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; + if (role_ptr_.empty() || unmapped_ || 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); @@ -2477,7 +2514,7 @@ uint64_t FileExpertSource::release(int64_t layer, int64_t expert) { const char* v = std::getenv("STRATA_FILE_RELEASE"); return v != nullptr && std::strcmp(v, "1") == 0; }(); - if (!enabled || base_ == nullptr || layer < 0 || expert < 0 || layer >= n_layers_ || expert >= n_expert_) + if (!enabled || base_ == nullptr || unmapped_ || layer < 0 || expert < 0 || layer >= n_layers_ || expert >= n_expert_) return 0; static const uint64_t page = [] { SYSTEM_INFO info{}; @@ -2733,7 +2770,7 @@ bool FileExpertSource::pin_cache_complement( if (fd >= 0) (void) posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED); }; if (maps_.empty()) drop(base_, mapped_bytes_, fd_); - else for (const Map& m : maps_) drop(m.base, m.bytes, m.fd); + else for (const Map& m : maps_) drop(m.closed ? nullptr : m.base, m.bytes, m.fd); }; #endif if (bytes > 0) { @@ -3060,7 +3097,7 @@ bool FileExpertSource::pin_cache_complement( // standby list: VirtualUnlock on pages that are not locked does exactly that (it then reports ERROR_NOT_LOCKED). // Not with the view closed (begin_startup_unbuffered): its range may hold other memory by now. if (maps_.empty() && !unmapped_) (void) VirtualUnlock((LPVOID) base_, (SIZE_T) mapped_bytes_); - for (const Map& m : maps_) (void) VirtualUnlock((LPVOID) m.base, (SIZE_T) m.bytes); + for (const Map& m : maps_) if (!m.closed) (void) VirtualUnlock((LPVOID) m.base, (SIZE_T) m.bytes); #endif if (std::getenv("STRATA_VERIFY_COMPLEMENT") != nullptr && host != nullptr && bytes > 0) { // tests: the copy's checksum (FNV-1a of each 1 GiB piece on its own thread, then of those), any reader diff --git a/src/program/generate.cpp b/src/program/generate.cpp index ff3f2e1a..dce91e89 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -6707,6 +6707,12 @@ int main(int argc, char** argv) { const bool dropped = src.drop_mapping(why); std::fprintf(stderr, "strata generate: the mapped view of the experts %s (%s)\n", dropped ? "is closed" : "stays open", why.c_str()); + // --ple-io mmap / ram maps the PLE shard for the whole run; when that shard also holds experts, NTFS still runs + // the unbuffered reads of that one file one at a time (the default --ple-io direct keeps no mapping) + if (dropped && src.gguf_mode() && !o.ple_gguf.empty() && o.ple_io != "direct") + std::fprintf(stderr, "strata generate: note: --ple-io %s keeps %s mapped; if that shard holds experts, their " + "reads stay serialized (--ple-io direct, the default, keeps no mapping)\n", + o.ple_io.c_str(), o.ple_gguf.c_str()); } // 0.1.42 (H11): the default CPU share is armed only when the host has the memory for it. One line says why not; // an explicit STRATA_PREFILL_CPU_SHARE (other than 0) is armed anyway, with a warning when memory is short.