diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp index b51c8f99..1c6a6e21 100644 --- a/include/strata/core/expert_source.hpp +++ b/include/strata/core/expert_source.hpp @@ -602,6 +602,12 @@ public: void prepare_overrides() { if (override_.empty()) override_.assign((size_t) blobs_, nullptr); } /// After the GPU copies of every staged swap have landed. Returns how many exchanges were applied. int64_t commit_exchanges(); + /// 0.1.43 (STRATA_ADAPT_OVERLAP): BEFORE the GPU copies of the staged swaps have landed, make each staged `out` look to the + /// planner as it will after `commit_exchanges` (pinned, with a device alias into its exchange buffer, so the PCIe share + /// treats it exactly as a committed one) without touching `in`'s place, which the copy still reads. `commit_exchanges` + /// (after the copies have landed) ends it. False: not possible here (rotation, exchange buffers not page-locked): the + /// caller waits for the copies instead. + bool soft_commit_exchanges(); /// `commit_exchanges` in two halves, for the asynchronous adaptive tier (--adapt-async): `commit_copies` moves /// every staged evicted blob into its `in`'s place in the copy (on another thread: safe once nothing computes `in` /// from RAM - it is resident on the GPU - while `out` is still read from its exchange buffer), then `commit_flip` @@ -853,6 +859,8 @@ private: 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_; + std::vector soft_in_; ///< soft_commit_exchanges: per `out` index, the `in` whose place it will take (SIZE_MAX: none) + const uint8_t* xstage_dev_ = nullptr; ///< the exchange buffers' device alias (soft_commit_exchanges) uint8_t* xstage_ = nullptr; ///< exchange buffers, `xstage_cap_ x xstage_blob_` bool xstage_pinned_ = false; int64_t xstage_cap_ = 0; diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index 4604eb8d..5bfd855d 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -3225,8 +3225,24 @@ bool FileExpertSource::stage_exchange(int64_t layer, int64_t in, int64_t out, in return true; } +bool FileExpertSource::soft_commit_exchanges() { + if (staged_.empty()) return true; + if (exchange_storage_.active() || !xstage_pinned_ || xstage_ == nullptr || !complement_pinned_ || complement_device_ == nullptr) + return false; + if (xstage_dev_ == nullptr) { + void* d = nullptr; + if (cudaHostGetDevicePointer(&d, xstage_, 0) != cudaSuccess || d == nullptr) { (void) cudaGetLastError(); return false; } + xstage_dev_ = (const uint8_t*) d; + } + if (soft_in_.empty()) soft_in_.assign((size_t) blobs_, SIZE_MAX); + for (const Exchange& x : staged_) soft_in_[x.out] = x.in; + return true; +} + int64_t FileExpertSource::commit_exchanges() { int64_t n = 0; + for (const Exchange& x : staged_) + if (!soft_in_.empty()) soft_in_[x.out] = SIZE_MAX; for (const Exchange& x : staged_) { const uint8_t* src = override_[x.out]; if (exchange_storage_.active()) { @@ -3354,16 +3370,21 @@ bool FileExpertSource::pinned(int64_t layer, int64_t expert) const { layer >= n_layers_ || expert >= n_expert_) return false; const size_t index = (size_t) layer * (size_t) n_expert_ + (size_t) expert; if (exchange_storage_.active()) return exchange_storage_.resident(index).host != nullptr; - if (index >= complement_offsets_.size() || complement_offsets_[index] == kNoComplement) return false; + // soft_commit_exchanges: a staged `out` is judged by the place it will take (`in`'s), as after the commit + const size_t at_index = !soft_in_.empty() && soft_in_[index] != SIZE_MAX ? soft_in_[index] : index; + if (at_index >= complement_offsets_.size() || complement_offsets_[at_index] == kNoComplement) return false; // a partial pin (CS-T): only the registered prefix return !complement_partial_ || - complement_offsets_[index] + layer_blob_bytes_[(size_t) layer] <= complement_pin_limit_; + complement_offsets_[at_index] + layer_blob_bytes_[(size_t) layer] <= complement_pin_limit_; } const uint8_t* FileExpertSource::device_alias(int64_t layer, int64_t expert) const { if (!pinned(layer, expert) || complement_device_ == nullptr) return nullptr; const size_t index = (size_t) layer * (size_t) n_expert_ + (size_t) expert; if (exchange_storage_.active()) return exchange_storage_.resident(index).device; + if (!soft_in_.empty() && soft_in_[index] != SIZE_MAX) // soft_commit_exchanges: out is read from its exchange buffer + return override_.empty() || override_[index] == nullptr || xstage_dev_ == nullptr + ? nullptr : xstage_dev_ + (size_t) (override_[index] - xstage_); return complement_device_ + (size_t) complement_offsets_[index]; } diff --git a/src/program/generate.cpp b/src/program/generate.cpp index c5d392a3..0d3be8b3 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -8149,7 +8149,6 @@ int main(int argc, char** argv) { : remote_opt ? "not with --remote-expert-opt" : o.adapt_async ? "not with --adapt-async" : o.pipeline_windows > 0 ? "not with --pipeline-windows" - : src.complement_ready() ? "not with the resident RAM mode" : pin_blobs_on() ? "not with page-locked swap sources" : strata::core::dma_batch_mode() != 0 ? "not with STRATA_DMA_BATCH" : adapt_lag() != 1 || adapt_nowait() ? "not with STRATA_ADAPT_LAG / STRATA_ADAPT_NOWAIT" @@ -11650,11 +11649,17 @@ int main(int argc, char** argv) { else if (aov && !pending.empty() && !aflipped) { // 0.1.43 STRATA_ADAPT_OVERLAP: the tables take the swaps now, exactly where the wait would have ended; // the copies still landing are waited for per layer inside the window (AdaptGate) - for (const auto& [i, slot] : pending) host_res[(size_t) i] = slot; - res_upload(); - aflipped = true; - ++aov_flips; - agate.active.store(true, std::memory_order_release); + // (resident RAM mode: the staged evicted experts look committed to the planner at once, the RAM + // copy's places stay as they are until the copies have landed; see soft_commit_exchanges) + if (src.complement_ready() && !src.soft_commit_exchanges()) { + apply_pending(true); // not possible here: the plain wait + } else { + for (const auto& [i, slot] : pending) host_res[(size_t) i] = slot; + res_upload(); + aflipped = true; + ++aov_flips; + agate.active.store(true, std::memory_order_release); + } } else if (pending.empty() || ++pending_age >= adapt_lag()) apply_pending(true); if (!adapt_tick(false)) { // --adapt-async: the round in flight moves on a step when it can