Merge branch 'pr-89' into next-023

This commit is contained in:
Niko1221 committed 2026-09-29 11:34:45 +02:00
commit 762daf658a
6 files changed
+140 -20

No files matched your search

+9
View File
@@ -341,6 +341,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
@@ -352,6 +358,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_;
};
+25
View File
@@ -52,15 +52,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 message
uint64_t bytes = 0;
uint64_t layers = 0;
std::vector<uint64_t> layer_checksums; // one FNV-1a per layer
double 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]`).
+10
View File
@@ -653,6 +653,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;
@@ -712,6 +714,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);
@@ -740,6 +747,9 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6
reads_ = 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;
}
+4 -1
View File
@@ -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,
+40 -10
View File
@@ -69,13 +69,31 @@ strata::kernels::QsaShapes shapes_of(const ModelGeometry& g) {
return s;
}
// 64-bit seek/tell on a `FILE*`: `fseek`/`ftell` take a 32-bit `long` on Windows and would wrap past 2 GiB.
#if defined(_WIN32)
#define STRATA_FILE_SEEK64(f, o, w) _fseeki64((f), (long long) (o), (w))
#define STRATA_FILE_TELL64(f) _ftelli64(f)
#else
#define STRATA_FILE_SEEK64(f, o, w) fseeko((f), (off_t) (o), (w))
#define STRATA_FILE_TELL64(f) ftello(f)
#endif
bool read_file(const std::string& path, std::vector<uint8_t>& out) {
std::ifstream f(path, std::ios::binary | std::ios::ate);
if (!f) return false;
const std::streamsize n = f.tellg();
f.seekg(0);
// Loader fix (0.1.15+loaderfix.2): `ifstream::read` reaches the disk as 4095-byte reads on MSVC - the same
// split `load_experts_ranges` had - so the drafter's 111 MiB dense blob paid 4 KiB per operation on a cold
// start. `fread` passes a request bigger than the stream buffer straight to `ReadFile()`.
FILE* f = std::fopen(path.c_str(), "rb");
if (f == nullptr) return false;
// A `FILE*` has no destructor and the short-read path below returns early: the guard closes on every path.
struct Closer {
FILE* f;
~Closer() { if (f != nullptr) std::fclose(f); }
} closer{f};
if (STRATA_FILE_SEEK64(f, 0, SEEK_END) != 0) return false;
const long long n = (long long) STRATA_FILE_TELL64(f);
if (n < 0 || STRATA_FILE_SEEK64(f, 0, SEEK_SET) != 0) return false;
out.resize((size_t) n);
return (bool) f.read((char*) out.data(), n);
return n == 0 || std::fread(out.data(), 1, (size_t) n, f) == (size_t) n;
}
} // namespace
@@ -120,6 +138,9 @@ bool MtpDrafter::load(const std::string& rt_dir, const ModelGeometry& g, Session
max_t_ = max_t;
rt_dir_ = rt_dir;
if (max_t < 1 || max_t > strata::kernels::kVerifyMaxT) { err = "mtp: max_t out of range"; return false; }
// Loader fix (0.1.15+loaderfix.2): the two reads below are the whole “drafter files” cost; reporting
// them apart from the rest of the stage is what makes the next regression visible.
const auto t_files = std::chrono::steady_clock::now();
// ---- the index and the dense weights
{
std::ifstream idx(rt_dir + "/dense.txt");
@@ -150,13 +171,19 @@ bool MtpDrafter::load(const std::string& rt_dir, const ModelGeometry& g, Session
// ---- the 512 routed experts, one blob each
{
const uint64_t bytes = (uint64_t) g.n_expert * strata::kernels::cpu::BLOB;
std::ifstream f(rt_dir + "/experts.bin", std::ios::binary);
if (!f) { err = "mtp: cannot open experts.bin"; return false; }
// Loader fix (0.1.15+loaderfix.2): each 64 MiB read below reached the disk as ~16k 4095-byte reads under
// MSVC's `basic_filebuf::xsgetn`, which is what made 675 MiB of drafter experts take minutes.
FILE* f = std::fopen((rt_dir + "/experts.bin").c_str(), "rb");
if (f == nullptr) { err = "mtp: cannot open experts.bin"; return false; }
struct Closer {
FILE* f;
~Closer() { if (f != nullptr) std::fclose(f); }
} closer{f};
if (cudaMalloc((void**) &experts_, bytes) != cudaSuccess) { err = "mtp: the 512 experts do not fit in VRAM"; return false; }
std::vector<uint8_t> chunk(64u << 20);
for (uint64_t off = 0; off < bytes;) {
const uint64_t n = std::min<uint64_t>(chunk.size(), bytes - off);
if (!f.read((char*) chunk.data(), (std::streamsize) n)) { err = "mtp: experts.bin is truncated"; return false; }
if (std::fread(chunk.data(), 1, (size_t) n, f) != (size_t) n) { err = "mtp: experts.bin is truncated"; return false; }
cudaMemcpy(experts_ + off, chunk.data(), n, cudaMemcpyHostToDevice);
off += n;
}
@@ -248,9 +275,12 @@ bool MtpDrafter::load(const std::string& rt_dir, const ModelGeometry& g, Session
cudaMemcpy(ident_, id.data(), id.size() * 4, cudaMemcpyHostToDevice);
}
if (cudaStreamCreateWithFlags(&cs_, cudaStreamNonBlocking) != cudaSuccess) { err = "mtp: stream"; return false; }
std::fprintf(stderr, "strata mtp: draft layer loaded, %.0f MiB of VRAM (experts %.0f, dense %.0f)\n",
const double files_s = std::chrono::duration<double>(std::chrono::steady_clock::now() - t_files).count();
std::fprintf(stderr, "strata mtp: draft layer loaded, %.0f MiB of VRAM (experts %.0f, dense %.0f), files read in %.2f s (%.0f MiB/s)\n",
(double) vram_ / 1048576.0, (double) g.n_expert * strata::kernels::cpu::BLOB / 1048576.0,
(double) tensors_.back().off / 1048576.0);
(double) tensors_.back().off / 1048576.0, files_s,
files_s > 0 ? ((double) g.n_expert * strata::kernels::cpu::BLOB + (double) tensors_.back().off) /
1048576.0 / files_s : 0.0);
return true;
}
+52 -9
View File
@@ -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
@@ -234,11 +242,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;
@@ -246,30 +276,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;
@@ -280,8 +319,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;