Windows GGUF-in-place: close every GGUF shard view once the file tier reads unbuffered (NTFS serializes unbuffered reads of a mapped file)
This commit is contained in:
1 parent
cdbf458684
commit
52a2dd10ac
3 files changed
+55
-11
No files matched your search
+48
-11
@@ -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<int32_t, int32_t>* pairs,
|
||||
}
|
||||
|
||||
bool FileExpertSource::advise_pairs(const std::pair<int32_t, int32_t>* 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] == ' | ||||