[Engine] Linux file tier: opt-in sequential read-ahead for the mapped expert files (STRATA_EXPERTS_SEQ_READAHEAD)

STRATA_EXPERTS_SEQ_READAHEAD=1 (Linux, off by default): FileExpertSource marks each
mapped expert file (a pack's experts.bin, or each GGUF shard in place) MADV_SEQUENTIAL
right after mmap. Unset, nothing runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
njy1991andClaude Opus 5.5 committed 2026-11-08 00:34:50 -05:00
1 parent 146df3c468
commit 7c28d4e61c
2 files changed
+53

No files matched your search

+22
View File
@@ -301,6 +301,28 @@ The weights and the MTP draft files are asked for ahead of their reads at start
steps), which matters on a slow drive: `STRATA_READ_AHEAD=0` turns it off, `STRATA_FILL_AHEAD=N` sets how many fill pairs
are requested ahead (default 256).
**Sequential read-ahead on the mapped experts (Linux, opt-in).** `STRATA_EXPERTS_SEQ_READAHEAD=1` marks the mapped expert
files (`experts.bin`, or the GGUF shards in place) `MADV_SEQUENTIAL` when they are mapped. When the experts outside the GPU
are much larger than the file cache, the kernel can stop reading ahead on the mapping: each page fault then reads one 4 KiB
page, and a prompt waits on hundreds of thousands of small reads. With the variable each fault reads the drive's read-ahead
window. The answers are the same. Measured on one PC: RX 6900 XT 16 GB, Ryzen 9 9950X, 32 GB RAM, Gyro-M (a 29.9 GiB
`experts.bin` on two NVMe SSDs in RAID0), Linux 7.2, `--adapt-swaps 0`, a cold page cache before each run, the same binary
with and without the variable (2 runs without, 4 with):
| | Without | With |
| --- | ---: | ---: |
| Five follow-up turns of 650 to 970 new tokens | 65.2 / 76.2 s | 23.5 to 28.4 s |
| One follow-up turn | 5.7 to 28.6 s | 4.4 to 6.3 s |
| Major page faults in one follow-up turn | 15,000 to 1,350,000 | 73,000 to 115,000 |
| A 20K-token first prompt | 31.0 / 31.9 s | 23.9 to 32.4 s |
| An 800-token answer at 24K context | 32.8 / 43.9 tok/s | 42.0 to 52.2 tok/s |
Without the variable a turn took about 6 s while the kernel kept reading ahead (about 15,000 faults) and 15 to 29 s once it
stopped (0.5 to 1.35 million); in two more runs without it the first prompt took 28.6 s with 60,000 faults and 120.5 s with
7.6 million. With adaptive swaps on (the default) that PC stopped long sessions with and without the variable (#4566), so
these runs had them off. Not measured: a PC whose file cache holds the experts, a slower drive (the decode's scattered
misses also read a whole window there).
The prompt chunk follows the host's memory (Linux, file tier, on by default). When the page cache is smaller than the file
set, a planner picks the chunk and ring with the fewest predicted bytes read per prompt from the RAM copy, the page cache that
is left, the drive class and the PCIe link. It prints `chunk 8192 -> 3840, ring 96 -> 96: ...` when it changes the pick and
+31
View File
@@ -993,6 +993,35 @@ bool file_tier_unbuffered(const std::vector<std::string>& paths, uint64_t arena_
FileExpertSource::~FileExpertSource() { close(); }
#if !defined(_WIN32)
namespace {
// STRATA_EXPERTS_SEQ_READAHEAD=1 (Linux, opt-in, read once): the mapped expert files - a pack's experts.bin, or each
// GGUF shard in place - are marked MADV_SEQUENTIAL when they are mapped. Seen on a 32 GB Linux PC whose experts do not
// fit the file cache (RX 6900 XT, Gyro-M's 29.9 GiB experts.bin on NVMe RAID0, kernel 7.2): with no hint the mapping's
// faults read one 4 KiB page each once the file cache thrashed - about 2 million major faults for a 700-token follow-up
// turn, the drive at a few hundred MB/s of the ~25 GB/s it can read - and with the hint each fault reads the device's
// read-ahead window. Only the kernel's read-ahead changes: the bytes read, and so the products, are the same. Set once
// here, not per prompt: switching the hint on for each prompt read and off after stalled the batched prompt path (#29)
// in 3 of 5 runs on that PC (with --adapt-swaps 0). Unset: nothing here runs.
void advise_mapped_experts(void* view, size_t bytes, const std::string& path) {
#if defined(__linux__)
static const bool on = [] {
const char* v = std::getenv("STRATA_EXPERTS_SEQ_READAHEAD");
return v != nullptr && v[0] == '1';
}();
if (!on) return;
const int rc = madvise(view, bytes, MADV_SEQUENTIAL);
std::fprintf(stderr, "FileExpertSource: STRATA_EXPERTS_SEQ_READAHEAD=1: %s is read with sequential read-ahead%s\n",
path.c_str(), rc == 0 ? "" : " (madvise failed: the default read-ahead stays)");
#else
(void) view;
(void) bytes;
(void) path;
#endif
}
} // namespace
#endif
#if defined(__linux__)
namespace {
/// Bytes of [p, p + n) whose pages are in the page cache (mincore, page-granular); 0 when the call fails.
@@ -1564,6 +1593,7 @@ bool FileExpertSource::open(const std::string& pack_dir, int64_t n_layers, int64
}
void* view = mmap(nullptr, (size_t) want, PROT_READ, MAP_SHARED, fd, 0);
if (view == MAP_FAILED) { ::close(fd); err = "FileExpertSource: mmap failed on " + path; return false; }
advise_mapped_experts(view, (size_t) want, path);
fd_ = fd;
base_ = (const uint8_t*) view;
paths_.assign(1, path); // #286: open_direct opens it again with O_DIRECT
@@ -1765,6 +1795,7 @@ bool FileExpertSource::open_gguf(std::string& err) {
}
void* view = mmap(nullptr, (size_t) st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (view == MAP_FAILED) { ::close(fd); err = "FileExpertSource: cannot map " + path; return false; }
advise_mapped_experts(view, (size_t) st.st_size, path);
m.fd = fd;
m.bytes = (uint64_t) st.st_size;
m.base = (const uint8_t*) view;