From 6149efd3a4a7c2203adefb4ecf73435a842c9dbe Mon Sep 17 00:00:00 2001 From: JeanP00l <181703563+JeanP00l@users.noreply.github.com> Date: Sat, 3 Oct 2026 15:43:00 +0300 Subject: [PATCH] STRATA_ARENA_MMAP=1: the expert arena as a read-only mapped file (Linux), for small-RAM machines The host expert arena is normally read into pinned (locked) RAM. On a machine whose GPUs already hold most experts, that locks memory the CPU never reads: with the Coder (25 GB arena) on 2x 16 GB GPUs and 32 GB of RAM, ~1 GB stayed available at 128K context, the kernel swapped, and a multi-turn checkpoint took 25-80 s. With STRATA_ARENA_MMAP=1 (opt-in, Linux; off by default, nothing changes without it): - the first start writes the arena to the pack's experts.bin (padded by one blob so a whole-slot copy may start at any expert); later starts map it read-only, not locked, not pinned. The page cache keeps what the CPU pool and the prompt path read and gives the rest back under pressure; a dropped page is re-read from the file, so no result depends on what is resident. The GPUs get no mapped alias of it: run with --pcie-frac 0; - after the VRAM caches are filled, the pages of experts a GPU holds are handed back (MADV_DONTNEED, STRATA_ARENA_RELEASE=0 keeps them) and the rest are prefetched (MADV_WILLNEED); an adaptive swap releases the expert that went to VRAM and prefetches the one that came back; - HIP: ROCclr copies a pageable source of 1 MiB or more by locking its pages in place and keeps them locked, which undid every release. The engine raises GPU_PINNED_MIN_XFER_SIZE (an explicit setting wins) so such copies stage through ROCclr's own buffers, and locks an adaptive swap's source pages itself only until the copy lands (the page ranges are merged first: a copy from a range only part of which is registered fails); - STRATA_RSS_TRACE=1 prints the process's RssFile at the startup steps (madvise reports success on locked pages and frees nothing; RssFile is the honest check). The existing --mmap-experts refuses native packs (the Coder); this path is for them. Measured on 2x AMD Instinct MI50 16 GB + 32 GB DDR4 (gfx906 port, Coder IQ1_M, 128K context, layer split): RAM available while serving ~1 GB -> 25 GB; multi-turn re-read of a checkpoint 25-80 s -> <= 3.2 s; decode unchanged (49.06 vs 49.16 ms per verify window). Not measured on NVIDIA (pin_blobs is a no-op there). --- include/strata/core/expert_source.hpp | 9 ++ include/strata/hip_compat/cuda_runtime.h | 1 + src/core/expert_source.cpp | 92 +++++++++++++++- src/program/generate.cpp | 128 ++++++++++++++++++++++- 4 files changed, 226 insertions(+), 4 deletions(-) diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp index 4c4d12f5..f555efe8 100644 --- a/include/strata/core/expert_source.hpp +++ b/include/strata/core/expert_source.hpp @@ -122,6 +122,11 @@ public: virtual void begin_layer(int64_t layer, const int32_t* ids, int64_t k) { (void) layer; (void) ids; (void) k; } /// Plan v0.3 P6: the DEVICE address of a pinned, mapped blob (the GPU can read it over PCIe), or null. virtual const uint8_t* device_alias(int64_t layer, int64_t expert) const { (void) layer; (void) expert; return nullptr; } + /// A file-backed source: start reading this expert's pages now (it will be needed by the CPU); no-op elsewhere. + virtual void prefetch(int64_t layer, int64_t expert) { (void) layer; (void) expert; } + /// A file-backed source: this expert lives in VRAM, so its pages need not stay in RAM - hand them back to the + /// kernel (a later read re-reads the file; no result depends on it). Returns the bytes released; 0 elsewhere. + virtual uint64_t release(int64_t layer, int64_t expert) { (void) layer; (void) expert; return 0; } /// Whether the verify window may give the GPU a PCIe share of this layer's misses at all (each expert is still /// checked with `pinned`). The arena answers per layer through its expert 0; the resident RAM mode's compact /// copy has no expert 0 when the GPU cache holds it, so it answers for the whole copy. @@ -641,6 +646,8 @@ public: int64_t reads() const { return reads_; } bool pinned(int64_t layer, int64_t expert) const override; const uint8_t* device_alias(int64_t layer, int64_t expert) const override; + void prefetch(int64_t layer, int64_t expert) override; + uint64_t release(int64_t layer, int64_t expert) override; /// What backing was obtained and why, for the startup print. "The engine adapts to the machine it is on" is /// only true if the engine says what it got. @@ -655,6 +662,8 @@ public: private: void* arena_ = nullptr; ///< the PinnedArena, owned + void* map_ = nullptr; ///< STRATA_ARENA_MMAP: the arena file, mapped read-only (not the PinnedArena) + uint64_t map_bytes_ = 0; std::vector dev_slice_; ///< device alias of each registered slice (or of the whole range) uint64_t slice_bytes_ = 0; const uint8_t* base_ = nullptr; diff --git a/include/strata/hip_compat/cuda_runtime.h b/include/strata/hip_compat/cuda_runtime.h index da2c4f4c..ccf9a913 100644 --- a/include/strata/hip_compat/cuda_runtime.h +++ b/include/strata/hip_compat/cuda_runtime.h @@ -61,6 +61,7 @@ #define cudaHostRegister hipHostRegister #define cudaHostRegisterMapped hipHostRegisterMapped #define cudaHostRegisterPortable hipHostRegisterPortable +#define cudaHostRegisterReadOnly hipHostRegisterReadOnly #define cudaHostUnregister hipHostUnregister #define cudaLaunchHostFunc hipLaunchHostFunc #define cudaMalloc hipMalloc diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index c64909f5..b68779e5 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -2569,7 +2569,7 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6 std::ifstream f(path, std::ios::binary | std::ios::ate); if (!f) { err = "ArenaExpertSource: cannot open " + path; return false; } const uint64_t got = (uint64_t) f.tellg(); - if (got != want) { + if (got != want && got != want + (uint64_t) blob) { // + one blob: STRATA_ARENA_MMAP's padded file char buf[400]; std::snprintf(buf, sizeof buf, "ArenaExpertSource: %s is %llu B but %lld layers x %lld experts (blobs up to %lld B) " @@ -2581,6 +2581,44 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6 } } + // STRATA_ARENA_MMAP=1 (a small-RAM machine whose GPUs hold most experts): the arena is the pack's experts.bin + // mapped READ-ONLY - not locked, not pinned. The page cache keeps what the CPU pool and the prompt path + // actually read (the experts no VRAM cache holds) and gives back the rest under memory pressure; a page + // dropped is re-read from the file, so a result never depends on what is resident. The GPUs then get no + // mapped alias: run with --pcie-frac 0. The first start writes experts.bin (arena layout, padded by one blob + // so a whole-slot copy may start at any expert), later ones map it. +#if defined(_WIN32) + static const bool arena_mmap = false; // POSIX mmap/madvise: Linux only for now +#else + static const bool arena_mmap = [] { const char* v = std::getenv("STRATA_ARENA_MMAP"); return v && v[0] == '1'; }(); + if (arena_mmap) { + const uint64_t file_bytes = want + (uint64_t) blob; + uint64_t have = 0; + { + std::ifstream f(path, std::ios::binary | std::ios::ate); + if (f) have = (uint64_t) f.tellg(); + } + if (have == file_bytes) { + const int fd = ::open(path.c_str(), O_RDONLY); + void* v = fd >= 0 ? mmap(nullptr, (size_t) file_bytes, PROT_READ, MAP_SHARED, fd, 0) : MAP_FAILED; + if (fd >= 0) ::close(fd); + if (v == MAP_FAILED) { err = "ArenaExpertSource: mmap of " + path + " failed"; return false; } + map_ = v; + map_bytes_ = file_bytes; + base_ = (const uint8_t*) v; + pinned_bytes_ = 0; + dev_slice_.clear(); + slice_bytes_ = 0; + blobs_ = n_layers * n_expert; + n_expert_ = n_expert; + reads_ = 0; + note_ = "mapped read-only from " + path + " (STRATA_ARENA_MMAP: not locked, not pinned)"; + gib_per_s_ = 0.0; + load_seconds_ = load_read_s_ = load_copy_s_ = 0.0; + return true; + } + } +#endif uint64_t pack_hash = 0; if (!shared_arena_file.empty() && !shared_arena_pack_hash(pack_dir, path, gguf_, lay, pack_hash, err)) return false; @@ -2639,6 +2677,20 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6 err = "ArenaExpertSource: the load read " + std::to_string(st.bytes) + " B of " + std::to_string(want); return false; } + if (arena_mmap && from_gguf) { // the first start: write experts.bin for the mapped starts after this one + const std::string tmp = path + ".tmp"; + std::FILE* f = std::fopen(tmp.c_str(), "wb"); + bool ok = f != nullptr && std::fwrite(a->data(), 1, (size_t) want, f) == (size_t) want; + if (ok) { + std::vector pad((size_t) blob, 0); + ok = std::fwrite(pad.data(), 1, pad.size(), f) == pad.size(); + } + if (f) ok = std::fclose(f) == 0 && ok; + if (ok) ok = std::rename(tmp.c_str(), path.c_str()) == 0; + if (!ok) std::remove(tmp.c_str()); + std::fprintf(stderr, "strata generate: STRATA_ARENA_MMAP: %s %s (the next start maps it)\n", + ok ? "wrote" : "could NOT write", path.c_str()); + } arena_ = a; base_ = a->data(); pinned_bytes_ = a->registered_bytes; @@ -2669,6 +2721,11 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6 } void ArenaExpertSource::close() { +#if !defined(_WIN32) + if (map_ != nullptr) munmap(map_, (size_t) map_bytes_); +#endif + map_ = nullptr; + map_bytes_ = 0; if (arena_ != nullptr) { delete (PinnedArena*) arena_; arena_ = nullptr; @@ -2678,6 +2735,39 @@ void ArenaExpertSource::close() { n_expert_ = 0; } +void ArenaExpertSource::prefetch(int64_t layer, int64_t expert) { +#if defined(_WIN32) + (void) layer; (void) expert; +#else + if (map_ == nullptr || layer < 0 || expert < 0 || expert >= n_expert_) return; + const auto& lay = strata::kernels::cpu::expert_layout(); + const uint64_t off = lay.blob_offset(layer, expert) & ~(uint64_t) 4095; + const uint64_t end = lay.blob_offset(layer, expert) + lay.blob_bytes(layer); + madvise((uint8_t*) map_ + off, (size_t) (end - off), MADV_WILLNEED); +#endif +} + +// Only the pages wholly inside the blob: a page shared with a neighbour the CPU may still read stays. MADV_DONTNEED +// unmaps them from this process; they stay in the page cache as clean, unmapped pages - the first the kernel takes +// back under pressure, and a minor fault away when the CPU needs the expert again (an adaptive swap evicts it). +// Dropping them from the cache too (POSIX_FADV_DONTNEED) made every eviction a re-read from the disk: ~200 major +// faults a second in decode, +8 ms a window. The memory the arena used to hold was never the cache itself but +// ROCclr's pin-in-place locks on it (see main) - locked pages are the ones the kernel cannot take back. +uint64_t ArenaExpertSource::release(int64_t layer, int64_t expert) { +#if defined(_WIN32) + (void) layer; (void) expert; + return 0; +#else + if (map_ == nullptr || layer < 0 || expert < 0 || expert >= n_expert_) return 0; + const auto& lay = strata::kernels::cpu::expert_layout(); + const uint64_t off = (lay.blob_offset(layer, expert) + 4095) & ~(uint64_t) 4095; + const uint64_t end = (lay.blob_offset(layer, expert) + lay.blob_bytes(layer)) & ~(uint64_t) 4095; + if (end <= off) return 0; + if (madvise((uint8_t*) map_ + off, (size_t) (end - off), MADV_DONTNEED) != 0) return 0; + return end - off; +#endif +} + bool ArenaExpertSource::pinned(int64_t layer, int64_t expert) const { if (base_ == nullptr || layer < 0 || expert < 0 || expert >= n_expert_) return false; const auto& lay = strata::kernels::cpu::expert_layout(); diff --git a/src/program/generate.cpp b/src/program/generate.cpp index b7bc46e0..f621754c 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -114,6 +114,61 @@ bool under_wddm() { } // perf-review D-4: the lent slots are refilled with queued copies and one wait; STRATA_REFILL_BLOCKING=1 waits on each +// A file-backed arena on AMD (STRATA_ARENA_MMAP; the engine raises ROCclr's pin-in-place threshold, see main): an +// adaptive swap copies one expert straight from the mapped file. Through ROCclr's staging that copy blocks the host +// (decode windows of 150+ ms); instead the batch's pages are locked here for the async copies and unlocked when they +// have landed (unpin_blobs), so the release that follows can hand them back. Neighbouring experts share a page and +// a copy from a range only PART of which is registered fails ("invalid argument"), so the page ranges are merged +// first and each merged span is registered once. A span that does not register is copied through the staging +// path. Portable: a layer split copies to either card. +int pin_blobs(std::vector> r, std::vector& live) { + int failed = 0; // ranges left pageable (a copy from them still works, through the driver's staging) +#if (defined(STRATA_USE_HIP) || defined(STRATA_HIP_GFX906)) && !defined(_WIN32) + static const bool on = std::getenv("STRATA_ARENA_MMAP") && std::getenv("STRATA_ARENA_MMAP")[0] == '1'; + if (!on || r.empty()) return 0; + for (auto& [a, e] : r) { a &= ~(uintptr_t) 4095; e = (e + 4095) & ~(uintptr_t) 4095; } + std::sort(r.begin(), r.end()); + size_t k = 0; + for (size_t i = 1; i < r.size(); ++i) { + if (r[i].first <= r[k].second) r[k].second = std::max(r[k].second, r[i].second); + else r[++k] = r[i]; + } + r.resize(k + 1); + for (const auto& [a, e] : r) { + if (cudaHostRegister((void*) a, (size_t) (e - a), cudaHostRegisterReadOnly | cudaHostRegisterPortable) == + cudaSuccess) + live.push_back((void*) a); + else { + (void) cudaGetLastError(); + ++failed; + } + } +#else + (void) r; (void) live; +#endif + return failed; +} +void unpin_blobs(std::vector& v) { + for (void* p : v) (void) cudaHostUnregister(p); + (void) cudaGetLastError(); + v.clear(); +} + +// STRATA_RSS_TRACE=1: the process's mapped file pages (the arena) at a startup step - who reads the arena back +void rss_probe(const char* where) { + static const bool on = std::getenv("STRATA_RSS_TRACE") != nullptr; + if (!on) return; + FILE* f = std::fopen("/proc/self/status", "r"); + if (!f) return; + char line[256]; + long long kb = -1; + while (std::fgets(line, sizeof line, f)) + if (std::strncmp(line, "RssFile:", 8) == 0) { kb = std::atoll(line + 8); break; } + std::fclose(f); + std::fprintf(stderr, "strata rss: %s: RssFile %.2f GiB\n", where, (double) kb / (1024.0 * 1024.0)); + std::fflush(stderr); +} + bool refill_blocking() { static const bool v = std::getenv("STRATA_REFILL_BLOCKING") != nullptr; return v; @@ -1066,6 +1121,15 @@ int main(int argc, char** argv) { // pipe or a file is block-buffered, so a program that dies loses every line it had already printed - which // turns "it crashed at step 7" into "it crashed somewhere", and the difference is a debugging session. std::setvbuf(stdout, nullptr, _IONBF, 0); +#if (defined(STRATA_USE_HIP) || defined(STRATA_HIP_GFX906)) && !defined(_WIN32) + // AMD, a file-backed arena (STRATA_ARENA_MMAP): ROCclr copies a pageable source of 1 MiB or more by locking its + // pages in place (a GPU userptr), and keeps them - so every expert the VRAM fill copied from the mapped + // experts.bin stayed resident, the release of the VRAM-held ones was undone by the driver at once, and a + // 32 GB PC ran with ~0.3 GB free: the kernel swapped, and a turn's checkpoint took 25-80 s. Above this size + // ROCclr stages through its own pinned buffers instead; decode is unchanged (49.06 vs 49.16 ms/window). + if (std::getenv("STRATA_ARENA_MMAP") && std::getenv("STRATA_ARENA_MMAP")[0] == '1') + setenv("GPU_PINNED_MIN_XFER_SIZE", "1000000", 0); // MiB; an explicit setting wins +#endif // Load every CUDA kernel when the context is created, before the expert cache takes the free VRAM. With the // default lazy loading, a kernel first used mid-prompt (MMQ for IQ3_XXS at 64K+ on a 12 GB card) found no VRAM // left for its code and the engine ended ("out of memory: cudaFuncSetAttribute"). Costs ~30 MB of VRAM. @@ -3938,6 +4002,31 @@ int main(int argc, char** argv) { } thits.d_res = d_res; thits.n_expert = g.n_expert; + // a file-backed arena (STRATA_ARENA_MMAP): the experts no GPU holds are the ones the CPU pool and the + // prompt path will read - start reading them now instead of faulting them in 4 KB at a time mid-request + // ... and the ones a GPU holds are handed back first (STRATA_ARENA_RELEASE=0 keeps them): the fill read the + // whole arena, and left mapped and referenced it crowds every other allocation into swap + { + static const bool keep = std::getenv("STRATA_ARENA_RELEASE") && std::getenv("STRATA_ARENA_RELEASE")[0] == '0'; + rss_probe("before the release"); + uint64_t released = 0; + if (!keep) + for (size_t i = 0; i < host_res.size(); ++i) + if (host_res[i] != strata::core::kNotResident) + released += srcp->release((int64_t) i / g.n_expert, (int64_t) i % g.n_expert); + if (released > 0) + std::fprintf(stderr, "strata generate: expert arena: %.2f GiB of VRAM-held experts handed back to the OS\n", + (double) released / (1024.0 * 1024.0 * 1024.0)); + rss_probe("after the release"); + int64_t pf = 0; + for (size_t i = 0; i < host_res.size(); ++i) + if (host_res[i] == strata::core::kNotResident) { + srcp->prefetch((int64_t) i / g.n_expert, (int64_t) i % g.n_expert); + ++pf; + } + (void) pf; + rss_probe("after the prefetch hints"); + } for (auto& st : stages) { // layer split across GPUs: the same table on every device const strata::core::OnDevice on(st->dev); if (cudaMalloc((void**) &st->d_res, host_res.size() * sizeof(int32_t)) != cudaSuccess || @@ -4339,6 +4428,7 @@ int main(int argc, char** argv) { } else { std::fprintf(stderr, "strata serve: the prompt path allocates its own buffers (too few cache slots to borrow)\n"); } + rss_probe("the prompt path set up"); // layer split across GPUs: a prompt path per stage, each handing its chunk's rows to the next. // THE CHUNK STEPS DOWN INSTEAD OF EXITING. A split's stage caches are sized after the arena is registered, and // the prompt path's own buffers (no loan) are not priced into them: with the whole arena pinned (#253) a @@ -4813,6 +4903,7 @@ int main(int argc, char** argv) { } // plan v0.3 P6: swaps in flight - (residency index, slot) admitted when adapt_ev has completed std::vector> pending; + std::vector pin_live; // the swaps' locked arena pages (pin_blob), unlocked once they have landed cudaEvent_t adapt_ev = nullptr; cudaEventCreateWithFlags(&adapt_ev, cudaEventDisableTiming); // a layer split's later stages keep a copy of the residency table on their devices, and swap on their own @@ -4835,8 +4926,12 @@ int main(int argc, char** argv) { else if (cudaEventQuery(st->adapt_ev) != cudaSuccess) return; } for (auto& st : stages) st->adapt_live = false; + unpin_blobs(pin_live); src.commit_exchanges(); // the resident RAM mode: the evicted experts take their places in RAM - for (const auto& [i, slot] : pending) host_res[(size_t) i] = slot; + for (const auto& [i, slot] : pending) { + host_res[(size_t) i] = slot; + srcp->release((int64_t) i / g.n_expert, (int64_t) i % g.n_expert); // in VRAM now: RAM not needed + } pending.clear(); res_upload(); }; @@ -4868,6 +4963,14 @@ int main(int argc, char** argv) { std::sort(swaps.begin(), swaps.end(), [](const Swap& a, const Swap& b) { return a.gain > b.gain; }); if ((int) swaps.size() > o.adapt_swaps) swaps.resize((size_t) o.adapt_swaps); if (!resident_stage_swaps(src, xcache, host_res, g.n_expert, swaps, adapt_stream)) return false; + { // lock the batch's source pages (a file-backed arena on AMD; see pin_blobs) + std::vector> spans; + for (const Swap& s : swaps) + if (const uint8_t* b = srcp->blob(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); + } bool main_live = false; 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; @@ -4879,11 +4982,15 @@ int main(int argc, char** argv) { if (slot < 0 || b == nullptr || cudaMemcpyAsync(gs ? gs->cache.device_slot(slot) : xcache.device_slot(slot), b, (size_t) strata::kernels::cpu::expert_layout().blob_bytes(s.layer), - cudaMemcpyHostToDevice, gs ? gs->adapt_stream : adapt_stream) != cudaSuccess) + cudaMemcpyHostToDevice, gs ? gs->adapt_stream : adapt_stream) != cudaSuccess) { + std::fprintf(stderr, "strata serve: adaptive swap copy failed (layer %d, slot %d, pinned %d): %s\n", + (int) s.layer, (int) slot, pin_live.empty() ? 0 : 1, cudaGetErrorString(cudaGetLastError())); return false; + } if (gs) gs->adapt_live = true; else main_live = true; host_res[out] = strata::core::kNotResident; // evicted now: the CPU computes it meanwhile + srcp->prefetch(s.layer, s.out); // a file-backed arena released its pages: read them back ahead pending.emplace_back((int32_t) in, slot); // resident once the copy has landed } if (!swaps.empty()) cudaEventRecord(adapt_ev, adapt_stream); @@ -5000,6 +5107,7 @@ int main(int argc, char** argv) { const int64_t free_mib = (int64_t) (free_b >> 20); if (free_mib >= 256) { std::fprintf(stderr, "strata serve: %lld MiB of VRAM free with everything loaded\n", (long long) free_mib); + rss_probe("serving"); } else if (reserve_adapted) { // #496: the reserve was already lowered to make the cache fit - a bigger one would leave it no room std::fprintf(stderr, "strata serve: WARNING: %lld MiB of VRAM free with everything loaded - this card " @@ -6481,6 +6589,7 @@ int main(int argc, char** argv) { } // plan v0.3 P6: swaps in flight - (residency index, slot) admitted when adapt_ev has completed std::vector> pending; + std::vector pin_live; // the swaps' locked arena pages (pin_blob), unlocked once they have landed cudaEvent_t adapt_ev = nullptr; cudaEventCreateWithFlags(&adapt_ev, cudaEventDisableTiming); int64_t adapt_rounds = 0; // counted here: `rounds` is declared below the adapt lambda @@ -6497,8 +6606,12 @@ int main(int argc, char** argv) { } if (trace_pending) std::fprintf(stderr, "strata: PENDING landed, %zu experts become resident\n", pending.size()); + unpin_blobs(pin_live); src.commit_exchanges(); // the resident RAM mode: the evicted experts take their places in RAM - for (const auto& [i, slot] : pending) host_res[(size_t) i] = slot; + for (const auto& [i, slot] : pending) { + host_res[(size_t) i] = slot; + srcp->release((int64_t) i / g.n_expert, (int64_t) i % g.n_expert); // in VRAM now: RAM not needed + } pending.clear(); if (d_res != nullptr) cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice); @@ -6548,6 +6661,14 @@ int main(int argc, char** argv) { std::fprintf(stderr, "strata generate: an adaptive refill failed (copying evicted experts back)\n"); return false; } + { // lock the batch's source pages (a file-backed arena on AMD; see pin_blobs) + std::vector> spans; + for (const Swap& s : swaps) + if (const uint8_t* b = srcp->blob(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); + } 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]; @@ -6560,6 +6681,7 @@ int main(int argc, char** argv) { return false; } host_res[out] = strata::core::kNotResident; // evicted now: the CPU computes it meanwhile + srcp->prefetch(s.layer, s.out); // a file-backed arena released its pages: read them back ahead pending.emplace_back((int32_t) in, slot); // resident once the copy has landed } if (!swaps.empty()) cudaEventRecord(adapt_ev, adapt_stream);