Resident RAM mode on Windows: the GPU cache's fill and the RAM copy read experts.bin unbuffered, the view closed meanwhile
With --resident-experts (setup's low-RAM mode when the experts the GPU does not hold fit the RAM) the start copies
the profile fill of the GPU cache and the RAM copy (pin_cache_complement) through the mapped view of experts.bin.
Every page it touches sits in the process working set beside the page-locked copy until #467's trim after the fill
and the VirtualUnlock after the copy: on a PC short of RAM that pages other memory out, and the copies run at the
page-fault rate (~0.85 GB/s here).
FileExpertSource::begin_startup_unbuffered closes the view, the section and the buffered handle, opens the
unbuffered handles (open_direct) and reads the first blob both ways; if the open fails or the two differ, the view is
mapped again and the start copies through it as before. The fill (prefetch_pairs) and the copy (pin_cache_complement)
then take their existing unbuffered paths (read_direct: requests merged up to 32 MiB). end_startup_unbuffered maps
the view again (map_view, the size checked) and closes the unbuffered handles and the stage buffers, so the file tier
is chosen as before. Same bytes in the same places.
Windows and experts.bin only: the call is under _WIN32, and the GGUF in place, an unbuffered file tier and runs
without a RAM copy are unchanged. STRATA_STARTUP_UNBUFFERED=0 gives the mapped copy back; STRATA_VERIFY_COMPLEMENT=1
prints checksums of the filled cache slots and of the RAM copy. pin_cache_complement no longer VirtualUnlocks the
range of a closed view.
IQ2_XS (33 GiB experts.bin), RTX 5090, Samsung 9100 PRO, --resident-experts --expert-cache auto (~15,200 slots, a
16.4 GiB RAM copy), interleaved against fb58e0db:
- 21 GiB available (a RAM ballast; #467's 32 GB PC had 20.7 GiB), 6 pairs: ready 42.5 -> 9.6 s (chat), 40.7 -> 10.2 s
(32K); the fill 842 -> 6,914 MB/s, the copy 13.7 -> 3.3 s; lowest available 0.25 -> 4.5 GiB; pages written out
~60,000 -> 579-3,340; 3 of 12 base starts held available RAM under 1 GiB for 2 s, none of 12 here.
- 128 GB: peak working set 32.6 -> 18.3 GiB; a restart with the file cached 13.6 -> 8.3 s; the 32K prompt and decode
unchanged.
- Same bits: p2k 200 tokens and first-token logits, 32K 64 / 256 tokens, as fb58e0db; STRATA_VERIFY_COMPLEMENT=1
checksums identical to STRATA_STARTUP_UNBUFFERED=0 (12,595 and 15,438 slots).
This commit is contained in:
1 parent
fbc60ee157
commit
f04ab568fe
3 files changed
+207
-2
No files matched your search
+135
-1
@@ -1666,6 +1666,120 @@ bool FileExpertSource::drop_mapping(std::string& why) {
|
||||
#endif
|
||||
}
|
||||
|
||||
bool FileExpertSource::begin_startup_unbuffered(std::string& why) {
|
||||
#if defined(_WIN32)
|
||||
if (const char* v = std::getenv("STRATA_STARTUP_UNBUFFERED"); v != nullptr && v[0] != '\0' && std::atoi(v) == 0) {
|
||||
why = "STRATA_STARTUP_UNBUFFERED=0";
|
||||
return false;
|
||||
}
|
||||
if (base_ == nullptr || unmapped_ || startup_ub_ || !role_ptr_.empty() || !maps_.empty() || paths_.size() != 1 ||
|
||||
layer_blob_bytes_.empty()) {
|
||||
why = "experts.bin only";
|
||||
return false;
|
||||
}
|
||||
if (!direct_.empty()) { why = "the file tier reads unbuffered already"; return false; }
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
const uint8_t* m = mapped_blob(0, 0); // the first blob as the mapping reads it, for the check below
|
||||
if (m == nullptr) { why = "the first blob is not mapped"; return false; }
|
||||
const std::vector<uint8_t> ref(m, m + (size_t) layer_blob_bytes_[0]);
|
||||
// NTFS runs the unbuffered reads of a file one at a time while it is mapped or open buffered (drop_mapping), so
|
||||
// the view, the section and the handle all go. The first unbuffered open with no buffered handle left purges the
|
||||
// file's cached pages: 1.2-2.5 s after a mapped start had left IQ2_XS's 33 GiB experts.bin on the standby list,
|
||||
// 3 ms after an unbuffered start. A buffered handle held open instead keeps the cache and serializes the reads
|
||||
UnmapViewOfFile((LPCVOID) base_);
|
||||
unmapped_ = true;
|
||||
if (mapping_ != nullptr) CloseHandle((HANDLE) mapping_);
|
||||
mapping_ = nullptr;
|
||||
if (file_ != nullptr) CloseHandle((HANDLE) file_);
|
||||
file_ = nullptr;
|
||||
std::string fail;
|
||||
if (open_direct(fail)) {
|
||||
// the first blob both ways: a drive that does not read it unbuffered keeps the mapped copy
|
||||
std::vector<uint8_t> probe(ref.size());
|
||||
const Fill f{0, 0, 0, probe.data()};
|
||||
if (read_direct(&f, 1) && std::memcmp(probe.data(), ref.data(), ref.size()) == 0) {
|
||||
startup_ub_ = true;
|
||||
startup_blob_bytes_ = file_blob_bytes_.load();
|
||||
startup_us_ = file_us_.load();
|
||||
char note[96];
|
||||
std::snprintf(note, sizeof note, "the view closed until the RAM copy is built; %.0f ms",
|
||||
std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count());
|
||||
why = note;
|
||||
return true;
|
||||
}
|
||||
fail = "an unbuffered read of the first blob failed or differed from the mapping";
|
||||
for (void* d : direct_) close_direct(d);
|
||||
direct_.clear();
|
||||
std::lock_guard<std::mutex> lk(stage_mu_);
|
||||
stage_blob_ = 0;
|
||||
}
|
||||
std::string again;
|
||||
why = map_view(again) ? fail : fail + "; " + again;
|
||||
return false;
|
||||
#else
|
||||
why = "Windows only";
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool FileExpertSource::end_startup_unbuffered(std::string& why) {
|
||||
if (!startup_ub_) return true;
|
||||
startup_ub_ = false;
|
||||
// the view first: closing the unbuffered handles beside a buffered one purges nothing
|
||||
if (!map_view(why)) {
|
||||
why += ": the file tier reads it unbuffered";
|
||||
return false; // as after drop_mapping: every read goes through read_direct
|
||||
}
|
||||
for (void* d : direct_) close_direct(d);
|
||||
direct_.clear();
|
||||
{ // the mapped tier hands out the mapping: the startup's stage buffers go
|
||||
std::lock_guard<std::mutex> lk(stage_mu_);
|
||||
stage_buf_.clear();
|
||||
stage_key_.clear();
|
||||
stage_epoch_.clear();
|
||||
stage_used_.clear();
|
||||
stage_busy_.clear();
|
||||
stage_pf_.clear();
|
||||
stage_of_.clear();
|
||||
stage_blob_ = 0;
|
||||
}
|
||||
file_blob_bytes_.store(startup_blob_bytes_);
|
||||
file_us_.store(startup_us_);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileExpertSource::map_view(std::string& why) {
|
||||
#if defined(_WIN32)
|
||||
// open's mapping of experts.bin, again
|
||||
const std::string& path = paths_.front();
|
||||
const int wide = MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, nullptr, 0);
|
||||
std::vector<wchar_t> wpath((size_t) (wide > 0 ? wide : 1), L'\0');
|
||||
if (wide > 0) MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, wpath.data(), wide);
|
||||
HANDLE f = CreateFileW(wpath.data(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
|
||||
nullptr);
|
||||
LARGE_INTEGER sz{};
|
||||
const bool same = f != INVALID_HANDLE_VALUE && GetFileSizeEx(f, &sz) && (uint64_t) sz.QuadPart == mapped_bytes_;
|
||||
HANDLE m = same ? CreateFileMappingW(f, nullptr, PAGE_READONLY, 0, 0, nullptr) : nullptr;
|
||||
void* view = m != nullptr ? MapViewOfFile(m, FILE_MAP_READ, 0, 0, 0) : nullptr;
|
||||
if (view == nullptr) {
|
||||
why = f != INVALID_HANDLE_VALUE && !same
|
||||
? std::string("experts.bin changed size since it was opened")
|
||||
: "experts.bin could not be mapped again (error " + std::to_string((unsigned long long) GetLastError()) + ")";
|
||||
if (m != nullptr) CloseHandle(m);
|
||||
if (f != INVALID_HANDLE_VALUE) CloseHandle(f);
|
||||
return false;
|
||||
}
|
||||
file_ = f;
|
||||
mapping_ = m;
|
||||
base_ = (const uint8_t*) view;
|
||||
unmapped_ = false;
|
||||
return true;
|
||||
#else
|
||||
why = "Windows only";
|
||||
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
|
||||
@@ -2809,9 +2923,29 @@ bool FileExpertSource::pin_cache_complement(
|
||||
#if defined(_WIN32)
|
||||
// The mapped pages this process touched (the GPU cache's fill and this copy) leave its working set for the
|
||||
// standby list: VirtualUnlock on pages that are not locked does exactly that (it then reports ERROR_NOT_LOCKED).
|
||||
if (maps_.empty()) (void) VirtualUnlock((LPVOID) base_, (SIZE_T) mapped_bytes_);
|
||||
// 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);
|
||||
#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
|
||||
constexpr uint64_t kPiece = 1ull << 30;
|
||||
const size_t pieces = (size_t) ((bytes + kPiece - 1) / kPiece);
|
||||
std::vector<uint64_t> ph(pieces, 0);
|
||||
std::atomic<size_t> next_piece{0};
|
||||
auto hash_work = [&] {
|
||||
for (size_t i; (i = next_piece.fetch_add(1)) < pieces;)
|
||||
ph[i] = fnv1a64(host + (size_t) (i * kPiece), std::min<uint64_t>(kPiece, bytes - i * kPiece));
|
||||
};
|
||||
std::vector<std::thread> hth;
|
||||
for (int t = 1; t < 8; ++t) hth.emplace_back(hash_work);
|
||||
hash_work();
|
||||
for (auto& t : hth) t.join();
|
||||
uint64_t h = fnv1a64((const uint8_t*) offsets.data(), (uint64_t) (offsets.size() * sizeof(uint64_t)));
|
||||
h = fnv1a64((const uint8_t*) ph.data(), (uint64_t) (ph.size() * sizeof(uint64_t)), h);
|
||||
std::fprintf(stderr, "FileExpertSource: cache complement checksum %016llx (%.2f GiB, its offsets included)\n",
|
||||
(unsigned long long) h, (double) bytes / 1073741824.0);
|
||||
}
|
||||
|
||||
complement_arena_ = arena;
|
||||
complement_host_ = host;
|
||||
|
||||
Reference in new issue
Block a user