file tier: opt-in Linux I/O path (STRATA_IO_PREFETCH=1) and counters that separate drive reads from page-cache hits

The mapped file tier reads experts through page faults. With STRATA_IO_PREFETCH=1 (Linux, experts.bin or the GGUF
in place) a blob whose pages are not cached is read with one pread of the whole blob into a staging buffer, the
predicted experts of the next layer(s) are read ahead on I/O threads (STRATA_IO_PF_THREADS, STRATA_IO_PREFETCH_DEPTH,
STRATA_IO_PF_AHEAD=0 for demand reads only), and a cached blob is still handed out as the mapping. Same bytes, so the
same output; off by default.

Counters: each request now logs the OS's own view of the file tier (/proc/self/io read_bytes and major faults) beside
the engine's expert reads, and with STRATA_IO_STATS=1 or io prefetch a mincore split of cached and uncached bytes,
read-ahead used / unused / skipped, and the decode's wait in the fetches (--stats prints the same).

blob_stable(): a pointer that is not recycled, for the copies that run asynchronously (adaptive swaps, the GPU cache
fills, the prompt stager).
This commit is contained in:
Niko1221 committed 2026-10-07 02:21:11 +02:00
1 parent e2de691c7c
commit 9745567e5f
7 files changed
+566 -25

No files matched your search

+78 -13
View File
@@ -14,6 +14,7 @@
// AND IT IS PHASE 2, so hit rate is `h = 0` and the number it prints is slow on purpose
// (`phase-2-correct-engine.md:5-9`). What it is FOR is the honest tok/s figure and the logit dump.
#include "strata/platform/memory.hpp"
#include "strata/core/arch_defaults.hpp"
#include "strata/core/dma_batch.hpp"
#include "strata/core/device.hpp"
@@ -3745,6 +3746,21 @@ 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);
}
// 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) {
const char* th = std::getenv("STRATA_IO_PF_THREADS");
src.set_io_prefetch(true, th != nullptr ? std::atoi(th) : 0);
if (src.io_prefetch())
std::fprintf(stderr, "strata generate: file tier io prefetch ON (STRATA_IO_PREFETCH=1): whole-blob preads, "
"predicted experts read ahead on %s I/O threads (STRATA_IO_PF_THREADS, default 4)\n",
th != nullptr ? th : "4");
else
std::fprintf(stderr, "strata generate: STRATA_IO_PREFETCH=1 is not available here (Linux mapped file tier "
"only, not with unbuffered reads); the file tier reads as before\n");
} else if (const char* sv = std::getenv("STRATA_IO_STATS"); sv != nullptr && std::atoi(sv) != 0) {
src.set_io_stats(true);
}
srcp = &src;
} else {
arena_src.set_gguf(o.native_preset); // plan v0.3 P6: a native pack may take its experts from shard 1
@@ -4147,7 +4163,7 @@ int main(int argc, char** argv) {
if (per_layer) continue;
break;
}
const uint8_t* b = srcp->blob(profile[(size_t) i].first, profile[(size_t) i].second);
const uint8_t* b = srcp->blob_stable(profile[(size_t) i].first, profile[(size_t) i].second);
if (b == nullptr || !xcache.fill_slot_blocking(slot, b, err,
(int64_t) strata::kernels::cpu::expert_layout().blob_bytes(profile[(size_t) i].first))) {
std::fprintf(stderr, "strata generate: the profile fill failed at pair %lld: %s\n",
@@ -4162,7 +4178,7 @@ int main(int argc, char** argv) {
// about bytes produces a plausible token, which is this project's most expensive failure mode; the
// cache's own `verify_slot` is the check and it costs one 1.38 MB D2H at startup.
if (prefilled > 0 && !xcache.verify_slot(xcache.slot_of(profile[0].first, profile[0].second),
srcp->blob(profile[0].first, profile[0].second), err,
srcp->blob_stable(profile[0].first, profile[0].second), err,
(int64_t) strata::kernels::cpu::expert_layout().blob_bytes(profile[0].first))) {
std::fprintf(stderr, "strata generate: %s\n", err.c_str());
return 1;
@@ -4221,7 +4237,7 @@ int main(int argc, char** argv) {
if (filled >= st.cache.slots()) break;
const int32_t slot = st.cache.admit(pr.first, pr.second);
if (slot == strata::core::kNotResident) break;
const uint8_t* b = srcp->blob(pr.first, pr.second);
const uint8_t* b = srcp->blob_stable(pr.first, pr.second);
if (b == nullptr || !st.cache.fill_slot_blocking(slot, b, err, (int64_t) lay.blob_bytes(pr.first))) {
std::fprintf(stderr, "strata generate: layer split, CUDA%d profile fill failed at pair %lld: %s\n",
st.dev, (long long) filled, err.c_str());
@@ -4230,7 +4246,7 @@ int main(int argc, char** argv) {
++filled;
}
if (filled == 0 || !st.cache.verify_slot(st.cache.slot_of(st.profile[0].first, st.profile[0].second),
srcp->blob(st.profile[0].first, st.profile[0].second), err,
srcp->blob_stable(st.profile[0].first, st.profile[0].second), err,
(int64_t) lay.blob_bytes(st.profile[0].first))) {
std::fprintf(stderr, "strata generate: layer split, CUDA%d expert cache: %s\n", st.dev,
filled == 0 ? "nothing filled" : err.c_str());
@@ -4390,6 +4406,10 @@ int main(int argc, char** argv) {
const char* kv = std::getenv("STRATA_LOOKAHEAD_K");
if (ok && lookahead.start(std::move(routers), g.n_embd, g.n_expert, kv ? std::atoi(kv) : 10, &src, err)) {
drive.d.lookahead = &lookahead;
if (const char* dv = std::getenv("STRATA_IO_PREFETCH_DEPTH"); dv != nullptr && std::atoi(dv) > 0)
lookahead.set_depth(std::atoi(dv));
else if (src.io_prefetch())
lookahead.set_depth(2);
std::fprintf(stderr, "strata generate: routing-aware prefetch of the file tier on (the next layer's router)\n");
} else {
(void) cudaGetLastError();
@@ -5436,7 +5456,7 @@ int main(int argc, char** argv) {
const int32_t l = profile[pi].first, e = profile[pi].second;
const size_t i = (size_t) l * (size_t) g.n_expert + (size_t) e;
if (host_res[i] >= 0 || lay.blob_bytes(l) > room) continue;
const uint8_t* b = srcp->blob(l, e);
const uint8_t* b = srcp->blob_stable(l, e);
if (b == nullptr) continue;
cudaMemcpyAsync(xcache.device_slot((int32_t) s), b, (size_t) lay.blob_bytes(l), cudaMemcpyHostToDevice,
nullptr);
@@ -6818,7 +6838,7 @@ int main(int argc, char** argv) {
if (pin_blobs_on()) { // lock the batch's source pages (a file-backed arena on AMD; see pin_blobs)
std::vector<std::pair<uintptr_t, uintptr_t>> spans;
for (const Swap& s : swaps)
if (const uint8_t* b = srcp->blob(s.layer, s.in))
if (const uint8_t* b = srcp->blob_stable(s.layer, s.in))
spans.emplace_back((uintptr_t) b,
(uintptr_t) b + strata::kernels::cpu::expert_layout().blob_bytes(s.layer));
pin_blobs(std::move(spans), pin_live);
@@ -6847,7 +6867,7 @@ int main(int argc, char** argv) {
const Swap& s = swaps[si];
const size_t in = (size_t) s.layer * g.n_expert + s.in, out = (size_t) s.layer * g.n_expert + s.out;
const int32_t slot = fenced_slots.empty() ? host_res[out] : fenced_slots[si];
const uint8_t* b = srcp->blob(s.layer, s.in);
const uint8_t* b = srcp->blob_stable(s.layer, s.in);
const int stn = multi_gpu ? stage_of(s.layer) : 0; // the swap stays in the layer's own cache
GpuStage* gs = stn > 0 ? stages[(size_t) stn - 1].get() : nullptr;
const strata::core::OnDevice on(gs ? gs->dev : -1);
@@ -7537,7 +7557,7 @@ int main(int argc, char** argv) {
}
}
if (pick < 0) continue;
const uint8_t* b = srcp->blob(layer, pick % g.n_expert);
const uint8_t* b = srcp->blob_stable(layer, pick % g.n_expert);
if (b == nullptr || !xcache.fill_slot_queued(slot, b, ferr,
(int64_t) strata::kernels::cpu::expert_layout().blob_bytes(layer))) {
e = "VRAM: refilling the cache failed: " + ferr;
@@ -8864,7 +8884,7 @@ int main(int argc, char** argv) {
tr("refill start", (long long) p.lent.size());
const strata::core::OnDevice on(p.dev);
for (const auto& [i, slot] : p.lent) { // D-4: queued, one wait (STRATA_REFILL_BLOCKING=1: each)
const uint8_t* b = srcp->blob(i / g.n_expert, i % g.n_expert);
const uint8_t* b = srcp->blob_stable(i / g.n_expert, i % g.n_expert);
const int64_t nb = (int64_t) strata::kernels::cpu::expert_layout().blob_bytes(i / g.n_expert);
if (b == nullptr || !(refill_blocking() ? p.cache->fill_slot_blocking(slot, b, e, nb)
: p.cache->fill_slot_queued(slot, b, e, nb)))
@@ -10262,11 +10282,44 @@ int main(int argc, char** argv) {
(unsigned long long)src.rotated_exchanges(),
(unsigned long long)src.avoided_exchange_copy_bytes());
// CS-T: the tiers, cumulative - GPU cache hits (the decode lookups above), RAM copy, files (SSD / OS cache)
if (srcp == &src)
if (srcp == &src) {
std::fprintf(stderr, "strata serve: expert tiers: GPU %lld hits this request; since the start RAM %lld blobs, files %lld blobs "
"%.1f MB read%s\n", (long long) req_hits, (long long) src.ram_reads(),
(long long) src.file_reads(), (double) src.file_read_bytes() / 1e6,
src.gguf_mode() ? " (the GGUF in place)" : "");
// what the OS did for this request (since the previous one): the drive's share of the file tier's reads
// against the page cache's, and what the I/O path did about it
static strata::platform::ProcIo io_prev;
static strata::core::FileExpertSource::IoCounters ic_prev;
static uint64_t fb_prev = 0;
const strata::platform::ProcIo io_now = strata::platform::proc_io_sample();
const strata::core::FileExpertSource::IoCounters ic = src.io_counters();
const uint64_t fb = src.file_read_bytes();
if (io_now.valid) {
std::fprintf(stderr, "strata serve: file tier I/O this request: the OS read %.1f MB from storage (%llu major faults) for "
"%.1f MB of expert reads", (double) (io_now.read_bytes - io_prev.read_bytes) / 1e6,
(unsigned long long) (io_now.major_faults - io_prev.major_faults), (double) (fb - fb_prev) / 1e6);
if (src.io_stats())
std::fprintf(stderr, "; page cache held %.1f MB at the read, not %.1f MB",
(double) (ic.cached_bytes - ic_prev.cached_bytes) / 1e6,
(double) (ic.uncached_bytes - ic_prev.uncached_bytes) / 1e6);
if (src.io_prefetch())
std::fprintf(stderr, "; io prefetch: %llu read ahead (%.1f MB by pread), %llu used, %llu unused, %llu skipped "
"(cached), %llu dropped; decode waited %.1f ms in %llu fetches",
(unsigned long long) (ic.pf_read_blobs - ic_prev.pf_read_blobs),
(double) (ic.pread_bytes - ic_prev.pread_bytes) / 1e6,
(unsigned long long) (ic.pf_used - ic_prev.pf_used),
(unsigned long long) (ic.pf_unused - ic_prev.pf_unused),
(unsigned long long) (ic.pf_resident_skips - ic_prev.pf_resident_skips),
(unsigned long long) (ic.pf_dropped - ic_prev.pf_dropped),
(double) (ic.crit_us - ic_prev.crit_us) / 1e3,
(unsigned long long) (ic.crit_n - ic_prev.crit_n));
std::fprintf(stderr, "\n");
io_prev = io_now;
ic_prev = ic;
fb_prev = fb;
}
}
// STRATA_SPLIT_TIMING: where each verify stage's host time went, cumulative per window since the start
// (waiting for its GPU to ring a layer, the CPU pool and plan per layer, staging the window)
if (static const bool st_timing = std::getenv("STRATA_SPLIT_TIMING") != nullptr; st_timing)
@@ -10403,7 +10456,7 @@ int main(int argc, char** argv) {
if (!lent.empty()) {
const Clock::time_point tr = Clock::now();
for (const auto& [i, slot] : lent) { // D-4: queued, one wait (STRATA_REFILL_BLOCKING=1: each)
const uint8_t* b = srcp->blob(i / g.n_expert, i % g.n_expert);
const uint8_t* b = srcp->blob_stable(i / g.n_expert, i % g.n_expert);
const int64_t nb = (int64_t) strata::kernels::cpu::expert_layout().blob_bytes(i / g.n_expert);
if (b == nullptr || !(refill_blocking() ? xcache.fill_slot_blocking(slot, b, err, nb)
: xcache.fill_slot_queued(slot, b, err, nb))) {
@@ -10822,7 +10875,7 @@ int main(int argc, char** argv) {
if (pin_blobs_on()) { // lock the batch's source pages (a file-backed arena on AMD; see pin_blobs)
std::vector<std::pair<uintptr_t, uintptr_t>> spans;
for (const Swap& s : swaps)
if (const uint8_t* b = srcp->blob(s.layer, s.in))
if (const uint8_t* b = srcp->blob_stable(s.layer, s.in))
spans.emplace_back((uintptr_t) b,
(uintptr_t) b + strata::kernels::cpu::expert_layout().blob_bytes(s.layer));
pin_blobs(std::move(spans), pin_live);
@@ -10835,7 +10888,7 @@ int main(int argc, char** argv) {
for (const Swap& s : swaps) {
const size_t in = (size_t) s.layer * g.n_expert + s.in, out = (size_t) s.layer * g.n_expert + s.out;
const int32_t slot = host_res[out];
const uint8_t* b = srcp->blob(s.layer, s.in);
const uint8_t* b = srcp->blob_stable(s.layer, s.in);
const size_t blob_n = (size_t) strata::kernels::cpu::expert_layout().blob_bytes(s.layer);
// asynchronous: the copies run while the MTP drafts; the next window waits for them
if (slot < 0 || b == nullptr) {
@@ -11120,6 +11173,18 @@ int main(int argc, char** argv) {
fmb, rounds > 0 ? fmb / rounds : 0.0, rounds > 0 ? fms / rounds : 0.0,
fms > 0 ? fmb / fms : 0.0, src.gguf_mode() ? " (the GGUF in place)" : "",
(double) (fall0 - fbytes0) / 1e6);
if (src.io_stats()) {
const strata::core::FileExpertSource::IoCounters ic = src.io_counters();
std::printf("%-24s file reads: page cache held %.1f MB, not %.1f MB (mincore at the read); io prefetch %s: "
"%llu read ahead, %llu used, %llu unused, %llu skipped (cached), %llu dropped; pread %.1f MB in %llu calls "
"(%.2f ms each); decode waited %.1f ms/round in the fetches\n", "io path",
(double) ic.cached_bytes / 1e6, (double) ic.uncached_bytes / 1e6, src.io_prefetch() ? "on" : "off",
(unsigned long long) ic.pf_read_blobs, (unsigned long long) ic.pf_used,
(unsigned long long) ic.pf_unused, (unsigned long long) ic.pf_resident_skips,
(unsigned long long) ic.pf_dropped, (double) ic.pread_bytes / 1e6,
(unsigned long long) ic.pread_n, ic.pread_n > 0 ? (double) ic.pread_us / 1e3 / (double) ic.pread_n : 0.0,
rounds > 0 ? (double) ic.crit_us / 1e3 / rounds : 0.0);
}
if (drive.d.lookahead != nullptr)
std::printf("%-24s %lld experts warmed, %lld of the file tier's %lld blob reads had been warmed (%.1f%%); "
"predictor %.1f ms/round on its thread, %lld layers skipped (still busy)\n", "routing prefetch",