Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
Resident RAM mode on Windows: the GPU cache's fill and the RAM copy read experts.bin unbuffered, the view closed meanwhile
sergiywithf04ab568fe26
diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hppindex 030ee3cc2..336cee6a4 100644--- a/include/strata/core/expert_source.hpp+++ b/include/strata/core/expert_source.hpp@@ -617,6 +617,16 @@ public:/// 32 measured 2.35 GB/s with experts.bin mapped and 3.57 GB/s without. After this every file read goes through/// read_direct (mapped_blob answers nullptr). Returns whether the view was closed; `why` says why not.bool drop_mapping(std::string& why);+ /// Windows, experts.bin, a file tier that reads through the file cache (--resident-experts): the startup reads -+ /// the GPU cache's fill from the profile and the RAM copy (pin_cache_complement) - go unbuffered (read_direct)+ /// with the view closed meanwhile. Through the mapping they put the file's pages in the working set beside the+ /// RAM copy, and on a PC short of RAM that pages other memory out. The bytes and their places are the same.+ /// end_startup_unbuffered maps the view again and closes the unbuffered handles and the stage buffers, as before+ /// the startup. STRATA_STARTUP_UNBUFFERED=0: the mapped copy (A/B). False (and `why`) when it does not apply.+ bool begin_startup_unbuffered(std::string& why);+ /// False when the view could not be mapped again: then the file tier stays unbuffered, `why` says so.+ bool end_startup_unbuffered(std::string& why);+ bool startup_unbuffered() const { return startup_ub_; }/// Every expert's bytes (n_layers x n_expert blobs).uint64_t expert_bytes() const;/// #286, unbuffered: assembles the blobs of these pairs ahead of the `blob` calls that will ask for them (the@@ -704,6 +714,8 @@ private:/// Windows, io_submit on Linux) into this thread's aligned buffer, then copied into place. False when a read fails.bool read_direct(const Fill* fills, size_t n) const;bool open_direct(std::string& why);+ /// Windows: experts.bin mapped again as `open` maps it (begin/end_startup_unbuffered).+ bool map_view(std::string& why);std::vector<std::string> paths_; ///< the mapped files, as maps_std::vector<void*> direct_; ///< #286: per file, an unbuffered handle (Windows) or O_DIRECT fd (Linux)std::vector<int> role_file_; ///< 3 x n_layers: index into maps_ / direct_@@ -828,6 +840,8 @@ private:int fd_ = -1;#endifbool unmapped_ = false; ///< pp-opt: drop_mapping closed the view (base_ stays as the "opened" mark only)+ bool startup_ub_ = false; ///< begin_startup_unbuffered: until end_startup_unbuffered+ uint64_t startup_blob_bytes_ = 0, startup_us_ = 0; ///< the staged reads' counters at its begin (put back at the end)};// ================================ THE RESIDENT ARENA (R2.1) ================================diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cppindex 3ba41e876..11dd8c0df 100644--- a/src/core/expert_source.cpp+++ b/src/core/expert_source.cpp@@ -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;diff --git a/src/program/generate.cpp b/src/program/generate.cppindex 6c6eae423..d1108091d 100644--- a/src/program/generate.cpp+++ b/src/program/generate.cpp@@ -4420,6 +4420,19 @@ int main(int argc, char** argv) {std::fprintf(stderr, "strata generate: the file tier reads through the file cache (STRATA_UNBUFFERED_LOAD=%s ""ignored: without --resident-budget-gib the file cache holds the experts)\n", env);}+#if defined(_WIN32)+ // the GPU cache's fill and the RAM copy read unbuffered even so: through the mapping their pages sat in the+ // working set beside the copy (end_startup_unbuffered below, once the copy is built). Windows, experts.bin+ if (o.resident_cpu_experts && !src.unbuffered()) {+ std::string why;+ if (src.begin_startup_unbuffered(why))+ std::fprintf(stderr, "strata generate: the GPU cache's fill and the RAM copy read unbuffered (%s)\n",+ why.c_str());+ else if (why.find("STRATA_STARTUP_UNBUFFERED") == std::string::npos)+ std::fprintf(stderr, "strata generate: the GPU cache's fill and the RAM copy read through the mapping "+ "(%s)\n", why.c_str());+ }+#endif// Linux I/O path (opt in): whole-blob preads instead of page faults, and the predicted experts of the next// layers read ahead on I/O threads while the current layer computes (see FileExpertSource::set_io_prefetch)if (const char* v = std::getenv("STRATA_IO_PREFETCH"); v != nullptr && std::atoi(v) != 0) {@@ -4953,6 +4966,39 @@ int main(int argc, char** argv) {"slot 0 verified\n",(long long) prefilled, (long long) (per_layer ? xcache.slots() : want), fill_s,fill_s > 0 ? (double) fill_bytes / 1e6 / fill_s : 0.0);+ if (std::getenv("STRATA_VERIFY_COMPLEMENT") != nullptr) {+ // tests: the filled slots' checksum (each slot's blob read back and hashed, then the hashes in slot order)+ std::vector<std::pair<int32_t, int64_t>> sl; // (slot, blob bytes)+ for (int64_t l = 0; l < g.n_layers; ++l)+ for (int64_t e = 0; e < g.n_expert; ++e)+ if (const int32_t s = xcache.slot_of(l, e); s >= 0)+ sl.emplace_back(s, (int64_t) strata::kernels::cpu::expert_layout().blob_bytes(l));+ std::sort(sl.begin(), sl.end());+ std::vector<uint64_t> sh(sl.size(), 0);+ std::atomic<size_t> next_slot{0};+ std::atomic<bool> copy_ok{true};+ auto hash_work = [&] {+ std::vector<uint8_t> hb;+ for (size_t i; (i = next_slot.fetch_add(1)) < sl.size();) {+ hb.resize((size_t) sl[i].second);+ if (cudaMemcpy(hb.data(), xcache.device_slot(sl[i].first), hb.size(), cudaMemcpyDeviceToHost) !=+ cudaSuccess) copy_ok = false;+ sh[i] = strata::core::fnv1a64(hb.data(), (uint64_t) hb.size());+ }+ };+ 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 = strata::core::fnv1a64(nullptr, 0);+ for (size_t i = 0; i < sl.size(); ++i) { // field by field: a pair's padding is not part of it+ const int64_t v[2] = {(int64_t) sl[i].first, sl[i].second};+ h = strata::core::fnv1a64((const uint8_t*) v, sizeof v, h);+ h = strata::core::fnv1a64((const uint8_t*) &sh[i], sizeof sh[i], h);+ }+ std::fprintf(stderr, "strata generate: expert cache checksum %016llx (%zu slots%s)\n", (unsigned long long) h,+ sl.size(), copy_ok ? "" : ", A READ-BACK FAILED");+ }}for (auto& stp : stages) {@@ -6452,6 +6498,17 @@ int main(int argc, char** argv) {elseerr = whole_err + "; " + err;}+ // the startup reads are done: the view of experts.bin back, the file tier as chosen above+ bool startup_kept = false;+ if (src.startup_unbuffered()) {+ std::string why;+ const auto end_t0 = std::chrono::steady_clock::now();+ startup_kept = !src.end_startup_unbuffered(why);+ if (startup_kept) std::fprintf(stderr, "strata generate: WARNING: %s\n", why.c_str());+ else+ std::fprintf(stderr, "strata generate: the startup reads are done: experts.bin mapped again (%.0f ms)\n",+ std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - end_t0).count());+ }// #669 / #765: a big chunk lends many cache slots to the prompt path, and each lent slot's expert is read back// from the SSD whenever the RAM could not keep its copy (31 GB: auto:32768 read prompts ~3x slower than auto).// Said, never capped: --prefill is the user's, and with the RAM for them it is the faster one (+21-35%).@@ -6504,7 +6561,7 @@ int main(int argc, char** argv) {// the file cache keeps those, and every refill after a prompt read the drive instead). Windows and Linux: the// unbuffered reads exist there#if defined(_WIN32) || defined(__linux__)- if (o.mmap_experts && o.resident_budget > 0) {+ if (o.mmap_experts && o.resident_budget > 0 && !startup_kept) {std::string why;const bool was = src.unbuffered();const bool ub = src.recheck_unbuffered(why);