prefill: batched unbuffered expert reads in the stager; close the experts.bin view once reads are unbuffered

NTFS serves unbuffered reads of a mapped file one at a time (WD SN580: 2 MiB reads at QD32
2.35 GB/s mapped vs 3.57 GB/s unmapped). With the file tier unbuffered (--resident-budget-gib
or STRATA_UNBUFFERED_LOAD=1) the mapped view is only a fallback after startup, so it is closed
(STRATA_KEEP_MAPPING=1 keeps it). The prompt path's stager claims runs of consecutive jobs and
reads them with one read_direct batch (STRATA_STAGER_BATCH, default 8 for file-backed blobs).

RTX 5070 Ti, 32 GB RAM, IQ3_XXS, 47.6K prompt, --resident-budget-gib 18 --prefill auto:32768:
1260 -> 1370 tok/s (batch 8) -> 1669 tok/s (view closed).

Ported from #833 onto the rewritten main. Two fixes on the way: unmapped_ is declared on every
platform (mapped_blob reads it outside the Windows block, so Linux did not compile), and
copy_blobs asks resident_blob, which follows the exchange rotation (STRATA_EXCHANGE_ROTATE).
This commit is contained in:
adonizm authored and Niko1221 committed 2026-10-07 21:18:06 +02:00
1 parent d9621d1e23
commit 0b030b96fc
4 files changed
+127 -22

No files matched your search

+61 -1
View File
@@ -980,7 +980,8 @@ void FileExpertSource::close() {
io_pf_jobs_.store(0); io_pf_blobs_.store(0); io_pf_skips_.store(0); io_pf_dropped_.store(0);
io_pf_used_.store(0); io_pf_unused_.store(0); io_crit_us_.store(0); io_crit_n_.store(0);
#if defined(_WIN32)
if (base_ != nullptr) UnmapViewOfFile((LPCVOID) base_);
if (base_ != nullptr && !unmapped_) UnmapViewOfFile((LPCVOID) base_);
unmapped_ = false;
if (mapping_ != nullptr) CloseHandle((HANDLE) mapping_);
if (file_ != nullptr) CloseHandle((HANDLE) file_);
mapping_ = nullptr;
@@ -1008,6 +1009,12 @@ bool ExpertSource::copy_blob(int64_t layer, int64_t expert, uint8_t* dst) {
return true;
}
bool ExpertSource::copy_blobs(const int32_t* layers, const int32_t* experts, uint8_t* const* dst, size_t n) {
for (size_t i = 0; i < n; ++i)
if (!copy_blob(layers[i], experts[i], dst[i])) return false;
return true;
}
// ================================ CS-T: THE GGUF SHARDS IN PLACE ================================
//
// A native pack without experts.bin: every file native_experts.txt names is mapped (MapViewOfFile / mmap, no
@@ -1509,6 +1516,26 @@ bool FileExpertSource::recheck_unbuffered(std::string& why) {
#endif
}
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 (base_ == nullptr || unmapped_) { why = base_ == nullptr ? "not open" : "already closed"; return unmapped_; }
if (!UnmapViewOfFile((LPCVOID) base_)) { why = "UnmapViewOfFile failed"; return false; }
unmapped_ = true;
if (mapping_ != nullptr) CloseHandle((HANDLE) mapping_);
mapping_ = nullptr;
// the cached handle too: only the unbuffered handles (direct_) read the file from here on
if (file_ != nullptr) CloseHandle((HANDLE) file_);
file_ = nullptr;
why = "every expert outside RAM and VRAM is read unbuffered";
return true;
#else
why = "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
@@ -1999,8 +2026,41 @@ bool FileExpertSource::copy_blob(int64_t layer, int64_t expert, uint8_t* dst) {
return true;
}
// pp-opt: the prompt path streams a chunk's experts in file order, so a stager thread's run of jobs is mostly
// neighbouring blobs of experts.bin. One read_direct batch merges them into requests of up to 32 MiB (kMerge) where
// 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);
thread_local std::vector<Fill> fills;
fills.clear();
uint64_t bytes = 0;
for (size_t i = 0; i < n; ++i) {
const int64_t l = layers[i], e = experts[i];
if (base_ == nullptr || dst[i] == nullptr || l < 0 || e < 0 || l >= n_layers_ || e >= n_expert_) return false;
const size_t index = (size_t) l * (size_t) n_expert_ + (size_t) e;
if (complement_ready_ &&
(resident_blob(index) != nullptr || (!override_.empty() && override_[index] != nullptr))) {
if (!copy_blob(l, e, dst[i])) return false;
continue;
}
fills.push_back({0, l, e, dst[i]});
bytes += layer_blob_bytes_[(size_t) l];
}
if (fills.empty()) return true;
const auto t0 = std::chrono::steady_clock::now();
if (!read_direct(fills.data(), fills.size()))
for (const Fill& f : fills) // a failed batch: each blob on its own (copy_from_files falls back to the mapping)
if (!copy_from_files(f.layer, f.e, f.dst)) return false;
file_us_.fetch_add((uint64_t) std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count(),
std::memory_order_relaxed);
file_read_bytes_.fetch_add(bytes, std::memory_order_relaxed);
return true;
}
const uint8_t* FileExpertSource::mapped_blob(int64_t layer, int64_t expert) const {
if (!role_ptr_.empty()) return nullptr; // the GGUF in place: no contiguous blob in any file
if (unmapped_) return nullptr; // pp-opt: drop_mapping closed the view; read_direct reads them
if (base_ == nullptr || layer < 0 || expert < 0 || layer >= n_layers_ || expert >= n_expert_) return nullptr;
const size_t i = (size_t) layer;
if (i >= layer_offsets_.size() || i >= layer_blob_bytes_.size()) return nullptr;