From 5288cd290278560df40aed5a931c369af18ee5ea Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 5 Oct 2026 10:05:42 +0200 Subject: [PATCH] adapt-async (#876): keep the blocking tier when the exchange buffers are pageable, and report ms per round Co-authored-by: Francesco Albano <119073350+Hardin22@users.noreply.github.com> --- include/strata/core/expert_source.hpp | 2 ++ src/program/generate.cpp | 13 +++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp index e4627141..9bc1e6b4 100644 --- a/include/strata/core/expert_source.hpp +++ b/include/strata/core/expert_source.hpp @@ -480,6 +480,8 @@ public: /// Host room for `n` evicted blobs (page-locked when possible). Idempotent for the same or a smaller `n`. bool reserve_exchanges(int64_t n, std::string& err); int64_t exchange_capacity() const { return xstage_cap_; } + /// The exchange buffers are page-locked (cudaHostAlloc): copies to and from them are asynchronous. + bool exchange_pinned() const { return xstage_pinned_; } uint8_t* exchange_buffer(int64_t q) const; /// Requires `has_resident(layer, in)`, `!has_resident(layer, out)` and `exchange_buffer(q)` holding out's blob. bool stage_exchange(int64_t layer, int64_t in, int64_t out, int64_t q); diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 02ef8e21..58a99d96 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -4800,6 +4800,10 @@ int main(int argc, char** argv) { // page-locked (an asynchronous copy instead of the driver's staged, synchronous one) if (o.adapt_async && !src.reserve_exchanges(2 * std::min(o.adapt_swaps, 96), err)) adapt_async_off(err.c_str()); + // pageable buffers would make every copy of a round synchronous (the driver stages them): that is the + // blocking tier's cost, so the asynchronous tier is not worth running - said, and the blocking tier stays + if (o.adapt_async && !src.exchange_pinned()) + adapt_async_off("the exchange buffers could not be page-locked"); if (o.adapt_every > 0 && o.adapt_swaps > 0 && !src.reserve_exchanges(std::min(o.adapt_swaps, 96), err)) { std::fprintf(stderr, "strata generate: CPU expert residency: %s\n", err.c_str()); @@ -5872,6 +5876,8 @@ int main(int argc, char** argv) { std::vector a_usage; std::vector a_res; int64_t a_win = 0, a_last = 0, a_rounds = 0, a_swapped = 0; + double a_ms = 0; // wall time from a round's start to its flip, summed (the "ms/round" stat) + Clock::time_point a_t0; std::atomic a_err{false}; std::vector ahomes; // [0] = CUDA0's cache, [k] = layer split stage k's std::unique_ptr ajob; @@ -5991,6 +5997,7 @@ int main(int argc, char** argv) { if (drain || a_win - a_last < (int64_t) o.adapt_every) return true; a_last = a_win; ++a_rounds; + a_t0 = Clock::now(); a_usage = drive.d.usage; a_res = host_res; // #477: the routing counted, as adapt() counts it (--expert-profile-save; else `heat` is empty) @@ -6040,6 +6047,7 @@ int main(int argc, char** argv) { if (!a_ready(drain)) return true; if (a_err.load()) return false; src.commit_flip(); + a_ms += std::chrono::duration(Clock::now() - a_t0).count(); aswaps.clear(); astate = AState::Idle; continue; // a round due now starts at this same boundary @@ -7993,8 +8001,9 @@ int main(int argc, char** argv) { (double) src.resident_bytes() / 1073741824.0, (long long) src.exchanges(), (long long) src.file_reads()); if (ajob) // --adapt-async, cumulative (a round may still be in flight: it lands before the next request) - std::fprintf(stderr, "strata serve: asynchronous adaptive tier: %lld rounds, %lld experts swapped in\n", - (long long) a_rounds, (long long) a_swapped); + std::fprintf(stderr, "strata serve: asynchronous adaptive tier: %lld rounds, %lld experts swapped in, " + "%.1f ms per round (start to flip, between windows)\n", + (long long) a_rounds, (long long) a_swapped, a_rounds > 0 ? a_ms / (double) a_rounds : 0.0); // CS-T: the tiers, cumulative - GPU cache hits (the decode lookups above), RAM copy, files (SSD / OS cache) if (srcp == &src) std::fprintf(stderr, "strata serve: expert tiers: GPU %lld hits this request; since the start RAM %lld blobs, files %lld blobs "