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++
loader: read the expert arena in bulk instead of std::ifstream
Denis Chirkovedeb92848c2c
diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hppindex c64bf4e7d..4864eb036 100644--- a/include/strata/core/expert_source.hpp+++ b/include/strata/core/expert_source.hpp@@ -336,6 +336,12 @@ public:/// only true if the engine says what it got.const std::string& note() const { return note_; }double load_gib_per_second() const { return gib_per_s_; }+ // Loader fix: the load, split. `load_seconds()` is the wall clock of the load loop; the other two are+ // sums over the reader threads (see LoadStats), so on their own they say how much of that wall was spent+ // waiting for the disk and how much in memcpy + FNV-1a.+ double load_seconds() const { return load_seconds_; }+ double load_read_seconds() const { return load_read_s_; }+ double load_copy_seconds() const { return load_copy_s_; }private:void* arena_ = nullptr; ///< the PinnedArena, owned@@ -347,6 +353,9 @@ private:int64_t reads_ = 0;std::string note_;double gib_per_s_ = 0.0;+ double load_seconds_ = 0.0;+ double load_read_s_ = 0.0;+ double load_copy_s_ = 0.0;uint64_t pinned_bytes_ = 0;std::string gguf_;};diff --git a/include/strata/core/pinned.hpp b/include/strata/core/pinned.hppindex af704691e..36c9fc893 100644--- a/include/strata/core/pinned.hpp+++ b/include/strata/core/pinned.hpp@@ -51,15 +51,40 @@ struct PinnedArena {struct LoadStats {double seconds = 0.0;+ // Loader fix: the two halves of `seconds`, both SUMMED OVER THE READER THREADS rather than wall clock, so+ // either can exceed `seconds` when the threads overlap. They exist to separate the read from the copy and+ // the hash: an 8 MiB chunk that reaches the disk as ~2048 4 KiB reads spends its time in the first one.+ double read_seconds = 0.0; // inside the read call only: no seek, no copy, no hash+ double copy_seconds = 0.0; // memcpy + FNV-1a+ // A short read is not a slow load, it is a WRONG one: the caller must refuse the pack rather than run+ // on an arena whose tail was never written. `seconds` alone cannot say that (it is -1.0 on failure, but+ // the caller's own check is on the REQUESTED byte count, which a refused read does not change).+ bool ok = true; // false: the load failed, see `error`+ std::string error; // why it failed, for the caller's messageuint64_t bytes = 0;uint64_t layers = 0;std::vector<uint64_t> layer_checksums; // one FNV-1a per layerdouble gib_per_second() const { return seconds > 0 ? (double) bytes / (1024.0 * 1024 * 1024) / seconds : 0.0; }+ // Aggregate rate of the readers WHILE THEY WERE INSIDE THE READ CALL: `bytes` over the mean per-thread+ // read time. `gib_per_second()` above divides the same bytes by the wall clock of the whole loop, which+ // also contains the copy and the hash, so the two differ by how much of the loop was not I/O.+ double read_gib_per_second(int threads) const {+ if (read_seconds <= 0.0 || threads < 1) return 0.0;+ return (double) bytes / (1024.0 * 1024 * 1024) / (read_seconds / (double) threads);+ }};// Load `layers` layers of the expert arena into `dst` with `threads` readers, `chunk` bytes at a time.// Each thread opens its OWN handle and seeks, which is the portable form of parallel pread: a shared handle// needs a lock around the seek and defeats the parallelism on Windows.+//+// The reads go through `fread()` on a per-thread `FILE*`, NOT through `std::ifstream`. MSVC's+// `basic_filebuf::xsgetn` splits every request larger than `_INTERNAL_BUFSIZ - 1` into 4095-byte `fread()`+// calls, so an 8 MiB `chunk` reached the disk as ~2048 4 KiB operations and the chunking did nothing at all+// (measured on a 42.9 GB pack: 1222 s, 4096 bytes per operation, 0.03 GiB/s). `fread()` hands a request+// larger than the stream buffer straight to `_read()`/`ReadFile()`, which is what `chunk` is for. The+// control flow, the copy, the per-layer FNV-1a and therefore the checksums are unchanged, so a pack loaded+// by the old and the new reader must produce identical `layer_checksums`.LoadStats load_experts(const std::string& path, uint8_t* dst, uint64_t blob_bytes, uint64_t blobs_per_layer,uint64_t layers, int threads, uint64_t chunk);/// Plan v0.3 P6: the same with one byte range per layer (`layer_off[L]`, `layer_bytes[L]`).diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cppindex 0ab1ce13c..11ef6f5b8 100644--- a/src/core/expert_source.cpp+++ b/src/core/expert_source.cpp@@ -606,6 +606,8 @@ LoadStats load_experts_gguf(const std::string& gguf, uint8_t* dst, const strata:for (auto& t : pool) t.join();if (bad) {st.seconds = -1.0;+ st.ok = false;+ st.error = "short read or unreadable shard while reading the experts from the GGUF";return st;}st.bytes = lay.total;@@ -665,6 +667,11 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6}const LoadStats st = from_gguf ? load_experts_gguf(gguf_, a->data(), lay, threads): load_experts_ranges(path, a->data(), loff, lbytes, threads, /*chunk=*/8u << 20);+ if (!st.ok) {+ delete a;+ err = "ArenaExpertSource: the expert load was refused: " + (st.error.empty() ? std::string("unknown") : st.error);+ return false;+ }if (st.bytes != want) {delete a;err = "ArenaExpertSource: the load read " + std::to_string(st.bytes) + " B of " + std::to_string(want);@@ -693,6 +700,9 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6reads_ = 0;note_ = a->note;gib_per_s_ = st.gib_per_second();+ load_seconds_ = st.seconds;+ load_read_s_ = st.read_seconds;+ load_copy_s_ = st.copy_seconds;return true;}diff --git a/src/core/load_main.cpp b/src/core/load_main.cppindex a508ca2fc..0885b766b 100644--- a/src/core/load_main.cpp+++ b/src/core/load_main.cpp@@ -65,7 +65,10 @@ int main(int argc, char** argv) {const strata::core::LoadStats st =strata::core::load_experts(path, dst, blob_bytes, blobs_per_layer, layers, threads, chunk);- if (st.seconds < 0) return 1;+ if (!st.ok) {+ std::fprintf(stderr, "loading the experts failed: %s\n", st.error.empty() ? "unknown" : st.error.c_str());+ return 1;+ }std::printf("loaded %.3f GiB in %.3f s with %d threads and %llu MiB chunks -> %.2f GiB/s\n",(double) st.bytes / (1024.0 * 1024 * 1024), st.seconds, threads,diff --git a/src/core/pinned.cu b/src/core/pinned.cuindex 90c22d17c..cb0cd1820 100644--- a/src/core/pinned.cu+++ b/src/core/pinned.cu@@ -12,6 +12,14 @@#include <mutex>#include <thread>+// Loader fix: `fseek`/`ftell` are 32-bit on Windows by default (and the pack is 42.9 GB), and the 64-bit+// spelling is not the same on the two platforms the engine builds for.+#ifdef _WIN32+#define STRATA_FSEEK64(f, o) _fseeki64((f), (long long) (o), SEEK_SET)+#else+#define STRATA_FSEEK64(f, o) fseeko((f), (off_t) (o), SEEK_SET)+#endif+#ifdef _WIN32#include <windows.h>#else@@ -228,11 +236,33 @@ LoadStats load_experts_ranges(const std::string& path, uint8_t* dst, const std::const auto t0 = std::chrono::steady_clock::now();std::vector<uint64_t> layer_hash((size_t) layers, 1469598103934665603ull);std::atomic<uint64_t> next_layer{0};+ std::atomic<uint64_t> read_ns_sum{0}; // summed over the threads: see LoadStats::read_seconds+ std::atomic<uint64_t> copy_ns_sum{0};std::mutex err_mu;std::string err;auto worker = [&]() {std::vector<uint8_t> buf((size_t) chunk);+ // One handle per thread, seeked once per layer: a shared handle would need a lock around the seek and+ // would serialise the very thing the threads are here to parallelise. `fread` on a `FILE*` rather than+ // `std::ifstream`: see the header - MSVC's `basic_filebuf::xsgetn` splits any request larger than+ // `_INTERNAL_BUFSIZ - 1` into 4095-byte freads, which turned one 8 MiB chunk into ~2048 4 KiB reads.+ // `fread` sees a request bigger than the stream buffer and passes it to `_read()`/+ // `ReadFile()` unchanged, so the chunk size reaches the disk. Buffered, not `FILE_FLAG_NO_BUFFERING`:+ // the cache should still hold what it can.+ FILE* f = std::fopen(path.c_str(), "rb");+ if (f == nullptr) {+ std::lock_guard<std::mutex> g(err_mu);+ err = "cannot open " + path;+ return;+ }+ // A `FILE*` has no destructor that closes it, and this function has early returns below (open, seek and+ // short-read failures), so the guard is what keeps the closing correct on every path.+ struct Closer {+ FILE* f;+ ~Closer() { if (f != nullptr) std::fclose(f); }+ } closer{f};+ uint64_t read_ns = 0, copy_ns = 0;for (;;) {const uint64_t L = next_layer.fetch_add(1);if (L >= layers) break;@@ -240,30 +270,39 @@ LoadStats load_experts_ranges(const std::string& path, uint8_t* dst, const std::uint64_t remaining = layer_bytes[(size_t) L];uint64_t pos = 0;uint64_t h = 1469598103934665603ull;- // one handle per thread, seeked once per layer: a shared handle would need a lock around the seek- // and would serialise the very thing the threads are here to parallelise- std::ifstream f(path, std::ios::binary);- if (!f) {+ // 64-bit seek: the pack is 42.9 GB, so the 32-bit `fseek` would wrap past 4 GiB+ if (STRATA_FSEEK64(f, off) != 0) {std::lock_guard<std::mutex> g(err_mu);- err = "cannot open " + path;+ err = "seek to " + std::to_string(off) + " B failed in layer " + std::to_string(L);return;}- f.seekg((std::streamoff) off);while (remaining > 0) {const uint64_t n = remaining < chunk ? remaining : chunk;- f.read((char*) buf.data(), (std::streamsize) n);- if ((uint64_t) f.gcount() != n) {+ const auto t_read = std::chrono::steady_clock::now();+ const size_t got = std::fread(buf.data(), 1, (size_t) n, f);+ read_ns += (uint64_t) std::chrono::duration_cast<std::chrono::nanoseconds>(+ std::chrono::steady_clock::now() - t_read).count();+ // A short read is EOF or an I/O error, never a silent zero fill: say WHERE and HOW SHORT, and+ // keep going no further - the caller turns this into a refused load, not a wrong answer.+ if (got != (size_t) n) {std::lock_guard<std::mutex> g(err_mu);- err = "short read in layer " + std::to_string(L);+ err = "short read in layer " + std::to_string(L) + ": got " + std::to_string(got) + " of "+ + std::to_string(n) + " B at offset " + std::to_string(off + pos)+ + (std::ferror(f) != 0 ? " (ferror set)" : "");return;}+ const auto t_copy = std::chrono::steady_clock::now();std::memcpy(dst + off + pos, buf.data(), (size_t) n);h = fnv1a64(buf.data(), n, h);+ copy_ns += (uint64_t) std::chrono::duration_cast<std::chrono::nanoseconds>(+ std::chrono::steady_clock::now() - t_copy).count();pos += n;remaining -= n;}layer_hash[(size_t) L] = h;}+ read_ns_sum.fetch_add(read_ns);+ copy_ns_sum.fetch_add(copy_ns);};std::vector<std::thread> pool;@@ -274,8 +313,12 @@ LoadStats load_experts_ranges(const std::string& path, uint8_t* dst, const std::if (!err.empty()) {std::fprintf(stderr, "load_experts: %s\n", err.c_str());st.seconds = -1.0;+ st.ok = false;+ st.error = err;return st;}+ st.read_seconds = (double) read_ns_sum.load() / 1e9;+ st.copy_seconds = (double) copy_ns_sum.load() / 1e9;st.layer_checksums = std::move(layer_hash);st.seconds = std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();return st;