From aea9dc3ed83622be5b653b2f2d0e715ad4c02f9c Mon Sep 17 00:00:00 2001 From: Evan Date: Tue, 6 Oct 2026 15:41:07 +0200 Subject: [PATCH] resident split: pin_cache_complement keeps each stage's lend region in the RAM copy too (stage_lend_regions, append_stage_lend_regions) - the experts the prompt path borrows under a split streamed from the mapped files through page faults (3.7 GiB read for one 73K prompt, 1.5 s of a 3.9 s chunk on host staging, 2026-10-06 rig); one budget caps complement and regions, experts past it keep the file fallback --- include/strata/core/expert_source.hpp | 22 ++++++++ src/core/expert_source.cpp | 79 +++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp index dfc249d8..c1b1ed02 100644 --- a/include/strata/core/expert_source.hpp +++ b/include/strata/core/expert_source.hpp @@ -124,6 +124,18 @@ int64_t choose_resident_keep_from(const std::vector& slot_bytes, uint6 /// (the caller copies out's bytes there). False, and nothing changed, unless `in` is in the copy and `out` is not. bool exchange_cache_complement(std::vector& offsets, size_t in, size_t out); +/// A layer split: add each stage's lend region to a plan built from the complement plus every stage's +/// cache (the `additional_gpu_pairs` shape). Each region lists one stage's (layer, expert) pairs, the +/// pairs in its cache's highest slots first - the slots the prompt path borrows, from the tail in. A pair +/// already in the plan is passed over; a stage's walk stops at the first pair that does not fit `cap_bytes` +/// and the NEXT stage's walk still runs (the pairs past the cap keep the mapped-file fallback they had). +/// Returns the pairs added, or -1 - leaving the plan untouched - when a pair is outside the geometry or a +/// region repeats one. +int64_t append_stage_lend_regions(int64_t n_layers, int64_t n_expert, const std::vector& layer_blob_bytes, + std::vector& offsets, uint64_t& bytes, uint64_t cap_bytes, + const std::vector>>& regions, + std::string& err); + } // namespace detail @@ -516,6 +528,15 @@ public: const std::vector>& additional_gpu_pairs = {}, int64_t lend_from_slot = -1, uint64_t headroom_bytes = 8ull << 30, uint64_t budget_bytes = 0, const std::vector>* rank = nullptr); + /// A layer split: the prompt path borrows the TAIL SLOTS of every stage's cache - CUDA0's own part + /// first, then every stage in order. Set these BEFORE `pin_cache_complement` to keep those slots' experts + /// in the RAM copy too: each region lists its stage's (layer, expert) pairs, the highest cache slot first, + /// as the borrowing takes them. Pairs past the budget keep the mapped-file fallback. Empty (the + /// default) keeps #848's shape: with `additional_gpu_pairs` the single lend region is off and the copy + /// holds only the complement. + void stage_lend_regions(std::vector>> regions) { + stage_lend_regions_ = std::move(regions); + } void close(); bool mapped() const { return base_ != nullptr; } @@ -776,6 +797,7 @@ private: bool complement_ready_ = false; uint64_t complement_locked_ = 0; ///< bytes held in the working set (pin refused) int64_t complement_lent_slots_ = 0; + std::vector>> stage_lend_regions_; ///< a split: each stage's lend region std::vector override_; ///< staged exchanges: an evicted expert read from its exchange buffer struct Exchange { size_t in, out; int64_t q; uint64_t bytes; }; std::vector staged_; diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index e4b46490..f92994fd 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -210,6 +210,46 @@ int64_t choose_resident_keep_from(const std::vector& slot_bytes, uint6 return keep; } +int64_t append_stage_lend_regions(int64_t n_layers, int64_t n_expert, const std::vector& layer_blob_bytes, + std::vector& offsets, uint64_t& bytes, uint64_t cap_bytes, + const std::vector>>& regions, + std::string& err) { + const size_t blobs = (size_t) (n_layers * n_expert); + if (n_layers <= 0 || n_expert <= 0 || layer_blob_bytes.size() != (size_t) n_layers || offsets.size() != blobs) { + err = "append_stage_lend_regions: the plan does not match the geometry"; + return -1; + } + std::vector> sorted; + for (const auto& region : regions) { // validate the whole request before touching the plan + for (const auto& pair : region) + if (pair.first < 0 || pair.first >= n_layers || pair.second < 0 || pair.second >= n_expert) { + err = "append_stage_lend_regions: a lend-region expert is outside the model"; + return -1; + } + sorted = region; + std::sort(sorted.begin(), sorted.end()); + for (size_t i = 1; i < sorted.size(); ++i) + if (sorted[i] == sorted[i - 1]) { + err = "append_stage_lend_regions: a stage's lend region repeats an expert"; + return -1; + } + } + int64_t kept = 0; + for (const auto& region : regions) { + for (const auto& pair : region) { + const size_t index = (size_t) pair.first * (size_t) n_expert + (size_t) pair.second; + if (offsets[index] != kNoCacheComplement) continue; // already in the copy: the complement, or a + // CUDA0 lend slot the region is asked to skip + const uint64_t b = layer_blob_bytes[(size_t) pair.first]; + if (b > cap_bytes - bytes) break; // this stage's rest keeps the file fallback + offsets[index] = bytes; + bytes += b; + ++kept; + } + } + return kept; +} + bool exchange_cache_complement(std::vector& offsets, size_t in, size_t out) { if (in == out || in >= offsets.size() || out >= offsets.size() || offsets[in] == kNoCacheComplement || offsets[out] != kNoCacheComplement) return false; @@ -2332,6 +2372,36 @@ bool FileExpertSource::pin_cache_complement( } } + // A layer split: generate hands the copy every stage's lend region (`stage_lend_regions`, set before the + // call). The prompt path borrows the tail slots of EVERY part's cache, and with `additional_gpu_pairs` + // non-empty the single lend region above is off - so without this the borrowed experts sat in neither + // VRAM nor RAM during a prompt and came back through page faults from the mapped shards: measured + // 2026-10-06 on an RTX 2000 Ada + 5060 Ti rig (17/48 split, IQ3_S mapped from the GGUF, --prefill auto, + // one 73K-token prompt) 3.7 GiB read from the NVMe for the one prompt (arena mode reads 1.1 GiB), and one + // 8192-token chunk spent 1.5 s of its 3.9 s on host staging (arena: 0.37 s). The regions walk each + // stage's highest slots first, as the borrowing does; the whole copy - complement and regions + // together - stays within one budget, and experts past it keep the file fallback they had. + int64_t stage_lent = 0; + uint64_t stage_lent_bytes = 0; + if (!stage_lend_regions_.empty()) { + if (budget == std::numeric_limits::max()) { // #403: the one reading, taken now + uint64_t physical = budget_physical, commit = budget_commit; + if (physical == 0 && !available_memory_bytes(physical, &commit)) { + err = "FileExpertSource: cannot determine available memory for the stage lend regions"; + return false; + } + const uint64_t available = std::min(physical, commit); + budget = available > headroom_bytes ? available - headroom_bytes : 0; + } + uint64_t cap = budget; // an explicit --resident-budget-gib caps the whole copy, regions included + if (budget_bytes > 0 && budget_bytes != kResidentWhatFits && budget_bytes < cap) cap = budget_bytes; + const uint64_t before = bytes; + stage_lent = detail::append_stage_lend_regions(n_layers_, n_expert_, layer_blob_bytes_, offsets, bytes, cap, + stage_lend_regions_, err); + if (stage_lent < 0) return false; + stage_lent_bytes = bytes - before; + } + void* arena = nullptr; const uint8_t* host = nullptr; const uint8_t* device = nullptr; @@ -2701,6 +2771,7 @@ bool FileExpertSource::pin_cache_complement( complement_locked_ = locked; complement_lock_off_ = lock_off; complement_lent_slots_ = lend ? n_slots - keep_from : 0; + complement_lent_slots_ += stage_lent; // and the split stages' regions: pairs the copy kept of them complement_ready_ = true; std::fprintf(stderr, "FileExpertSource: %s cache complement ready: resident %.2f GiB, pinned %.2f GiB in %.0f s%s%s\n", complement_pinned_ ? "mapped pinned" : pin ? "locked resident" : "pageable resident", @@ -2723,6 +2794,14 @@ bool FileExpertSource::pin_cache_complement( "cost 26%%). Free RAM or lower STRATA_RESIDENT_HEADROOM_GIB (now the RAM left free at start) and restart; " "the line above should read N of N.\n", missing, lendable, 100.0 * (double) missing / (double) std::max(1, lendable)); } + if (!stage_lend_regions_.empty()) { + int64_t offered = 0; + for (const auto& region : stage_lend_regions_) offered += (int64_t) region.size(); + std::fprintf(stderr, "FileExpertSource: the split stages lend %lld slots to the prompt path: %lld keep " + "their experts in RAM too (%.2f GiB)%s\n", + (long long) offered, (long long) stage_lent, (double) stage_lent_bytes / 1073741824.0, + stage_lent < offered ? " (the rest are read from the file when lent: not enough RAM for them)" : ""); + } if (!additional_gpu_pairs.empty()) { std::fprintf(stderr, "FileExpertSource: %zu verified additional-GPU experts remain on the mmap fallback\n", additional_gpu_pairs.size());