diff --git a/include/strata/kernels/ngram.hpp b/include/strata/kernels/ngram.hpp index d15491de..8e6702a2 100644 --- a/include/strata/kernels/ngram.hpp +++ b/include/strata/kernels/ngram.hpp @@ -110,6 +110,9 @@ struct PleIoOptions { uint32_t max_inflight = 64; ///< outstanding SSD reads (decode needs 16; prefill chunks use more) uint64_t cache_rows = 1u << 20; ///< bounded row cache: 1,048,576 rows x 90 B ~ 95 MB; 0 disables bool io_thread = true; ///< reads submitted by a worker thread, not the caller + /// dual-3090 (--ple-io ram): mmap mode with the whole table locked in RAM at open (a box with RAM to spare: + /// no SSD read ever sits on the prompt or token path) + bool lock = false; }; /// The PLE table. Held by pointer-to-impl so this header does not drag `` into every diff --git a/src/kernels/ngram.cpp b/src/kernels/ngram.cpp index b1bd98b2..d9076888 100644 --- a/src/kernels/ngram.cpp +++ b/src/kernels/ngram.cpp @@ -6,8 +6,12 @@ #include "strata/kernels/f16_bits.hpp" #include "strata/ngram/ple_reader.hpp" +#include #include #include +#if !defined(_WIN32) +#include +#endif #include #include @@ -195,6 +199,20 @@ 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) + 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; + madvise((void*) a0, a1 - a0, MADV_WILLNEED); + if (mlock((const void*) a0, a1 - a0) != 0) { + std::fprintf(stderr, "strata: PLE table mlock failed (%s): touching its pages instead\n", std::strerror(errno)); + volatile uint8_t sink = 0; + for (uintptr_t p = a0; p < a1; p += page) sink = sink + *(const volatile uint8_t*) p; + (void) sink; + } +#endif + } impl_->mode = io.mode; return true; } diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 3b091eb2..0bc03bc6 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -319,7 +319,7 @@ void usage() { " --tokens-file PATH pretokenized prompt, commas or whitespace (alternative to --tokens)\n" " --ple-gguf PATH required PLE table (original second GGUF shard)\n" " --no-ple explicit diagnostic ablation of the PLE layer\n" - " --ple-io direct|mmap n-gram table reads (plan v0.3 P2). direct (default): unbuffered SSD\n" + " --ple-io direct|mmap|ram n-gram table reads (ram = mmap, locked in RAM at start); (plan v0.3 P2). direct (default): unbuffered SSD\n" " reads, the table never enters RAM or the file cache; mmap: A/B arm\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 64)\n" @@ -1195,7 +1195,7 @@ int main(int argc, char** argv) { return 2; } - if ((o.ple_io != "direct" && o.ple_io != "mmap") || o.ple_row_cache < 0 || o.ple_inflight < 1 || + if ((o.ple_io != "direct" && o.ple_io != "mmap" && o.ple_io != "ram") || o.ple_row_cache < 0 || o.ple_inflight < 1 || o.ple_inflight > 1024 || !(o.ple_delay_us >= 0)) { std::fprintf(stderr, "strata generate: invalid --ple-io/--ple-row-cache/--ple-inflight/--ple-delay-us\n"); return 2; @@ -1564,7 +1564,9 @@ int main(int argc, char** argv) { float* ple_scratch = nullptr; if (!o.ple_gguf.empty()) { strata::kernels::PleIoOptions pio; - pio.mode = o.ple_io == "mmap" ? strata::kernels::PleIo::Mmap : strata::kernels::PleIo::Direct; + pio.mode = o.ple_io == "mmap" || o.ple_io == "ram" ? strata::kernels::PleIo::Mmap : strata::kernels::PleIo::Direct; + pio.lock = o.ple_io == "ram"; // dual-3090: the whole table locked in RAM + const auto tpl = Clock::now(); pio.max_inflight = (uint32_t) o.ple_inflight; pio.cache_rows = (uint64_t) o.ple_row_cache; pio.io_thread = !o.ple_sync_submit; @@ -1572,6 +1574,9 @@ int main(int argc, char** argv) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } + if (pio.lock) + std::fprintf(stderr, "strata generate: PLE table locked in RAM (--ple-io ram) in %.1f s\n", + std::chrono::duration(Clock::now() - tpl).count()); const strata::core::WeightRef* wk = wt.find("blk.1.ple_key.weight"); const strata::core::WeightRef* wv = wt.find("blk.1.ple_value.weight"); const strata::core::WeightRef* wnk = wt.find("blk.1.ple_norm_key.weight");