diff --git a/include/strata/kernels/ngram.hpp b/include/strata/kernels/ngram.hpp index 84291476..207707d0 100644 --- a/include/strata/kernels/ngram.hpp +++ b/include/strata/kernels/ngram.hpp @@ -153,7 +153,8 @@ struct PleIoOptions { ///< ~335 MB (BF16); 0 disables bool io_thread = true; ///< reads submitted by a worker thread, not the caller /// Mmap mode only (`--ple-io ram`): lock the whole mapped table in RAM at open, so no SSD read ever sits on - /// the prompt or token path. Needs RAM for the full table. POSIX only (mlock); `locked()` reports the outcome. + /// the prompt or token path. Needs RAM for the full table (28.8 GB IQ4_NL, 51.2 GB FP8). mlock on POSIX, + /// the process working-set minimum plus VirtualLock on Windows; `locked()` reports the outcome. bool lock = false; /// Direct mode with the I/O worker only: keep the SSD awake while rows are asked for - one page of the table /// after this long without a read (0 = off), until `keepalive_window_s` after the last request for rows @@ -197,7 +198,7 @@ public: bool open(const std::string& gguf_path, std::string& err); void close(); bool is_open() const; - /// True when `PleIoOptions::lock` was asked for and mlock succeeded (false: pages only pre-touched). + /// True when `PleIoOptions::lock` was asked for and the OS lock succeeded (false: pages only pre-touched). bool locked() const; uint64_t rows() const; /// "IQ4_NL" or "F8_E4M3" (a GGUF from tools/ple_fp8_pack.py: type I8, strata.ple.format = f8_e4m3). diff --git a/src/kernels/ngram.cpp b/src/kernels/ngram.cpp index 4667745f..90631b4e 100644 --- a/src/kernels/ngram.cpp +++ b/src/kernels/ngram.cpp @@ -6,6 +6,7 @@ #include "strata/artifact/dequant.hpp" #include "strata/kernels/f16_bits.hpp" #include "strata/ngram/ple_reader.hpp" +#include "strata/platform/memory.hpp" #include #include @@ -327,7 +328,31 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoO impl_->n_rows = n_rows; } if (io.mode == PleIo::Mmap && io.lock && impl_->data != nullptr) { -#if !defined(_WIN32) +#if defined(_WIN32) + // Windows has no mlock: the platform helper raises the process's minimum working set and + // VirtualLocks the mapped table instead (ordinary accounts hold the privilege). A failure + // falls back to touching the pages, i.e. plain mmap behaviour, with a warning - same as POSIX. + // A table that is more than half of the RAM leaves the rest of the process little (the locked pages cannot be + // paged out): say so, and go on (recommend, never force). + MEMORYSTATUSEX ms; + ms.dwLength = sizeof(ms); + if (GlobalMemoryStatusEx(&ms) && need > ms.ullTotalPhys / 2) + std::fprintf(stderr, "strata: warning: --ple-io ram locks %.1f GiB, more than half of this PC's %.1f GiB of RAM\n", + (double) need / (1ull << 30), (double) ms.ullTotalPhys / (1ull << 30)); + const strata::platform::LockResult lr = + strata::platform::lock_resident((void*) impl_->data, need); + if (lr.ok && lr.locked_bytes >= need) { + impl_->locked = true; + } else { + // a partial lock is not a lock: the unlocked rest would fault on the token path, so give it back + if (lr.locked_bytes > 0) strata::platform::unlock_resident((void*) impl_->data, lr.locked_bytes); + std::fprintf(stderr, "strata: PLE table lock failed (%s): touching its pages instead\n", + lr.note.c_str()); + volatile uint8_t sink = 0; + for (uint64_t off = 0; off < need; off += 4096) sink = sink + impl_->data[off]; + (void) sink; + } +#else const uint64_t page = 4096; const uintptr_t a0 = (uintptr_t) impl_->data & ~(uintptr_t) (page - 1); const uintptr_t a1 = (uintptr_t) impl_->data + (uintptr_t) need; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 003bf891..8818a026 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -651,7 +651,8 @@ void usage() { " --no-ple explicit diagnostic ablation of the PLE layer\n" " --ple-io direct|mmap|ram n-gram table reads (plan v0.3 P2). direct (default): unbuffered SSD\n" " reads, the table never enters RAM or the file cache; mmap: A/B arm;\n" - " ram: mmap with the whole table locked in RAM at start (Linux/macOS)\n" + " ram: the whole table locked in RAM at start (VirtualLock on Windows,\n" + " mlock elsewhere), so no SSD read sits on the prompt or token path\n" " --ple-row-cache N bounded cache of fetched rows, 90 B each (default 1048576; 0 = off)\n" " --ple-inflight N outstanding SSD reads (default 256)\n" " --ple-delay-us U fault injection: each row read completes no earlier than U us\n" @@ -2058,12 +2059,6 @@ int main(int argc, char** argv) { std::fprintf(stderr, "strata generate: invalid --ple-io/--ple-row-cache/--ple-inflight/--ple-delay-us\n"); return 2; } -#if defined(_WIN32) - if (o.ple_io == "ram") { - std::fprintf(stderr, "strata generate: --ple-io ram is not available on Windows (no mlock); use --ple-io mmap\n"); - return 2; - } -#endif if (o.kv == "q4") o.kv = "q4_0"; if (o.kv != "fp16" && o.kv != "int8" && o.kv != "q4_0" && o.kv != "k8v4") { std::fprintf(stderr, "strata generate: --kv must be fp16, int8, q4_0 or k8v4\n");