From b437b8ea13e54cf65644ee6b9a21e289c769a855 Mon Sep 17 00:00:00 2001 From: Francesco Albano <119073350+Hardin22@users.noreply.github.com> Date: Mon, 5 Oct 2026 02:22:14 +0200 Subject: [PATCH] Asynchronous adaptive expert tier (--adapt-async 1, opt-in) In the resident RAM mode (--resident-experts) an adaptive round runs on a thread the decode loop joins after the draft: choose, copy the evicted slots back with a stream sync, copy the new experts in from the RAM copy (through the driver's staging when it is pageable). With ~90 swaps that was ~24 ms every 4th window (measured on our fork). --adapt-async 1 splits a round into four steps that advance between windows on a helper thread, so no window waits for one: 1. choose (adapt()'s rule, over snapshots of the counts and the residency) and copy the evicted slots back (D2H), each from the cache of the card that owns its layer 2. stage the exchanges, mark the evicted experts non-resident (the CPU reads them from the exchange buffers), copy the new experts in: straight from a page-locked RAM copy, else through a pinned bounce buffer 3. mark the new experts resident, move the evicted blobs into the RAM places the new ones left 4. flip the copy's offsets A slot is overwritten only after its old expert stopped being planned for the GPU, and a RAM place is reused only once its expert is resident. The residency tables on the devices are uploaded at steps 2 and 3, as apply_pending does (the verify graph's doorbell reads them). A request starts with the round drained, before its prompt lends any slot. --expert-profile-save counts the routing at the start of each round, as the blocking tier does. Swaps that need no exchange (the evicted expert is in the RAM copy already, or the new one is not: a RAM budget) take the same steps without a copy back; a new expert outside the RAM copy is read from the files on the helper thread. Default off: which window first computes a swapped-in expert on the GPU depends on when its copy lands, so the tier is not bit-exact from run to run. Without the flag nothing changes. With it, the blocking tier still runs outside --serve, without the resident RAM copy, and with --batch slots, --peer-device or --remote-expert-opt (said once at startup). The exchange buffers are twice as many with it (the bounce half): 96 more blob-sized pinned buffers, ~130 MiB with 1.38 MB blobs. Layer split (with the resident mode on a split, #848): each swap's copies run on the owning card's refill stream under that card's device; the homes are looked up the way #848's resident_stage_swaps does. Measured: Swift 1.5 IQ3_XXS at 160K (q4_0 KV), i9-14900KF, 32 GB of RAM, Windows 11, greedy, decode tok/s, blocking -> --adapt-async 1: - RTX 4060 Ti + RTX 5080 layer split with the resident RAM mode (#848), --trim-stage-weights, the stock draft layer, five prompts x two rounds, two interleaved pairs: 79.7 / 78.8 -> 87.6 / 86.2 (+10%). - the same split with a fine-tuned draft layer and --spec-min-p 0.65, four prompts x three rounds: 81.0 / 81.8 -> 108.1 / 107.0 (+32%). - RTX 5080 alone with --resident-experts: 26.7 / 26.6 -> 29.5 / 28.5 (noisy). Without the flag the greedy output is the same as main's, byte for byte (4/4 prompts, 500 tokens, STRATA_IQ_MT_MIN=1 --pcie-frac 0 --adapt-every 0). With it, 22 varied requests at temperature 0.6 on the split (43,825 experts swapped in over 622 rounds) gave no degenerate answer. --- docs/DETAILS.md | 4 + include/strata/core/expert_source.hpp | 8 + src/core/expert_source.cpp | 34 +++ src/program/generate.cpp | 327 +++++++++++++++++++++++++- 4 files changed, 371 insertions(+), 2 deletions(-) diff --git a/docs/DETAILS.md b/docs/DETAILS.md index 20e93343..f9d6d29f 100644 --- a/docs/DETAILS.md +++ b/docs/DETAILS.md @@ -136,6 +136,10 @@ other ~18 GB), a 32 GB PC with a 12-16 GB GPU the Coder; IQ3_XXS on a 32 GB PC s as far as the RAM allows, their experts are kept in RAM too (so a prompt reads nothing from the SSD either). - The cache still follows the conversation (`--adapt-every`): a swap copies the evicted expert back from VRAM into the RAM place of the one that replaces it, so the RAM copy keeps holding exactly what the GPU does not. +- `--adapt-async 1` (opt-in, `--serve`): the swaps of a round advance between decode windows on a helper thread + (copy back, copy in, move into RAM) instead of one window waiting for the whole round. Not with `--batch` or + `--peer-device` (the blocking tier runs there). It is not bit-exact from run to run: which window first computes a + swapped-in expert on the GPU (which rounds differently from the CPU) depends on when its copy lands. - The answers are the plain mapped mode's for the same expert placement: the bytes are the file's. With a page-locked copy the GPU also takes its usual share of the misses over PCIe (`--pcie-frac`), as with enough RAM; `--pcie-frac 0` (or `STRATA_RESIDENT_PIN=0`) gives the mapped mode's exact tokens. diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp index 8fbc4a58..e4627141 100644 --- a/include/strata/core/expert_source.hpp +++ b/include/strata/core/expert_source.hpp @@ -485,6 +485,14 @@ public: bool stage_exchange(int64_t layer, int64_t in, int64_t out, int64_t q); /// After the GPU copies of every staged swap have landed. Returns how many exchanges were applied. int64_t 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` + /// (the caller's thread, between windows) points `out` there and drops the staging. Returns how many were applied. + void commit_copies(); + int64_t commit_flip(); + /// The compact copy's blob of `(layer, expert)`, or null; not counted as a read (any thread). + const uint8_t* resident_blob(int64_t layer, int64_t expert) const; int64_t exchanges() const { return exchanges_; } /// With the compact copy ready: blobs read from the mapped file since (what the plain mmap mode may read from /// the SSD). 0 in a steady resident mode; lend-region experts that did not fit the RAM count here. diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index c145db45..b5b67e41 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -1796,6 +1796,40 @@ int64_t FileExpertSource::commit_exchanges() { return n; } +void FileExpertSource::commit_copies() { + for (const Exchange& x : staged_) { + const uint8_t* src = override_.empty() ? nullptr : override_[x.out]; + const uint64_t at = complement_offsets_[x.in]; + if (src != nullptr && at != kNoComplement && at <= complement_bytes_ && x.bytes <= complement_bytes_ - at && + complement_host_ != nullptr) + std::memcpy((uint8_t*) complement_host_ + (size_t) at, src, (size_t) x.bytes); + } +} + +int64_t FileExpertSource::commit_flip() { + int64_t n = 0; + for (const Exchange& x : staged_) { + const uint8_t* src = override_[x.out]; + const uint64_t at = complement_offsets_[x.in]; + // the same test as commit_copies: an exchange whose bytes were copied is the one that flips + if (src != nullptr && at != kNoComplement && at <= complement_bytes_ && x.bytes <= complement_bytes_ - at && + complement_host_ != nullptr && detail::exchange_cache_complement(complement_offsets_, x.in, x.out)) + ++n; + override_[x.out] = nullptr; + } + staged_.clear(); + exchanges_ += n; + return n; +} + +const uint8_t* FileExpertSource::resident_blob(int64_t layer, int64_t expert) const { + if (!complement_ready_ || complement_host_ == nullptr || layer < 0 || expert < 0 || layer >= n_layers_ || + expert >= n_expert_) return nullptr; + const size_t index = (size_t) layer * (size_t) n_expert_ + (size_t) expert; + if (index >= complement_offsets_.size() || complement_offsets_[index] == kNoComplement) return nullptr; + return complement_host_ + (size_t) complement_offsets_[index]; +} + const uint8_t* FileExpertSource::blob(int64_t layer, int64_t expert) { if (base_ == nullptr || layer < 0 || expert < 0 || layer >= n_layers_ || expert >= n_expert_) return nullptr; const size_t index = (size_t) layer * (size_t) n_expert_ + (size_t) expert; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index e6aa66ef..02ef8e21 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -81,6 +81,7 @@ #include #include #include +#include #include #include #include @@ -505,6 +506,10 @@ struct Options { /// the VRAM tier in place of the least-routed resident ones (decayed counts). 0 = static residency. int adapt_every = 4; float adapt_decay = 0.7f; ///< the usage counts are multiplied by this after each adaptation (--adapt-decay) + /// --adapt-async 1 (--serve with the resident RAM mode; opt-in): the adaptive tier's rounds advance between verify + /// windows on a helper thread instead of one window waiting for a whole round (see the tier in the --serve block). + /// Not bit-exact run to run. 0 = the blocking tier. + int adapt_async = 0; /// Plan v0.3 P6: a draft enters the verify window only while every draft before it (and itself) has at least /// this probability under the draft layer; 0 = always --spec-1 drafts. double spec_min_p = 0.0; @@ -750,7 +755,10 @@ void usage() { " RAM allows); adaptive swaps exchange them, so none is read from the file again.\n" " --resident-experts the low-RAM PC's resident mode (setup): --mmap-experts --resident-cpu-experts\n" " with the copy page-locked when possible, 4 GiB headroom, plain mmap if it\n" - " does not fit. Same answers as --mmap-experts for the same placement.\n"); + " does not fit. Same answers as --mmap-experts for the same placement.\n" + " --adapt-async 1 --serve with the resident RAM mode (opt-in): the adaptive tier's swaps\n" + " advance between verify windows instead of a window waiting for a whole\n" + " round. Not bit-exact run to run. 0 (default) = the blocking tier.\n"); } bool parse_i64_list(const char* s, std::vector& out, std::string& err) { @@ -1051,6 +1059,82 @@ bool adapt_nowait() { return v; } +/// --adapt-async: one background job at a time (the asynchronous adaptive tier's copies and memcpys). `post` hands +/// it over, `idle` says whether it has finished; the owner never posts while a job runs. +class JobThread { +public: + JobThread() : th_([this] { loop(); }) {} + ~JobThread() { + { std::lock_guard lk(mu_); quit_ = true; } + cv_.notify_one(); + th_.join(); + } + JobThread(const JobThread&) = delete; + JobThread& operator=(const JobThread&) = delete; + void post(std::function f) { + { + std::lock_guard lk(mu_); + job_ = std::move(f); + has_ = true; + done_.store(false, std::memory_order_release); + } + cv_.notify_one(); + } + bool idle() const { return done_.load(std::memory_order_acquire); } + void wait() const { while (!idle()) std::this_thread::yield(); } +private: + // The second logical processor of the last physical core that has two: an SMT sibling no pool worker or host loop + // is pinned to (the pool takes the first of every core but the first, the host the first core), or -1. + static int smt_spare_cpu() { +#if defined(_WIN32) + DWORD len = 0; + GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &len); + if (len == 0) return -1; + std::vector buf(len); + if (!GetLogicalProcessorInformationEx(RelationProcessorCore, (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX) buf.data(), + &len)) + return -1; + int best = -1, core = 0; + for (const char* p = buf.data(); p < buf.data() + len; + p += ((const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*) p)->Size, ++core) { + const auto* e = (const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*) p; + const GROUP_AFFINITY& ga = e->Processor.GroupMask[0]; + int seen = 0; + for (int bit = 0; bit < 64; ++bit) + if ((ga.Mask & (1ull << bit)) && ++seen == 2 && core > 0) best = ga.Group * 64 + bit; + } + return best; +#else + return -1; +#endif + } + void loop() { + // the job's memcpys stay off the host's core and the pool's: by default the spare SMT sibling of the last + // two-thread core (Windows); STRATA_ADAPT_JOB_CPU=, or -1 for no pinning + const char* c = std::getenv("STRATA_ADAPT_JOB_CPU"); + const int cpu = c != nullptr ? std::atoi(c) : smt_spare_cpu(); + if (cpu >= 0) (void) strata::kernels::cpu::pin_current_thread(cpu); + for (;;) { + std::function f; + { + std::unique_lock lk(mu_); + cv_.wait(lk, [&] { return has_ || quit_; }); + if (!has_) return; + f = std::move(job_); + has_ = false; + } + f(); + done_.store(true, std::memory_order_release); + } + } + std::mutex mu_; + std::condition_variable cv_; + std::function job_; + bool has_ = false, quit_ = false; + std::atomic done_{true}; + std::thread th_; // last: started once the members above exist +}; + int argmax(const std::vector& v) { int best = 0; for (size_t i = 1; i < v.size(); ++i) @@ -1442,6 +1526,7 @@ int main(int argc, char** argv) { else if (a == "--pcie-frac") o.pcie_frac = std::atof(next("--pcie-frac")); else if (a == "--adapt-every") o.adapt_every = std::atoi(next("--adapt-every")); else if (a == "--adapt-decay") o.adapt_decay = (float) std::atof(next("--adapt-decay")); + else if (a == "--adapt-async") o.adapt_async = std::atoi(next("--adapt-async")) != 0 ? 1 : 0; else if (a == "--spec-min-p") o.spec_min_p = std::atof(next("--spec-min-p")); else if (a == "--stop-eos") o.stop_eos = true; else if (a == "--spec-split") o.spec_split = true; @@ -4657,6 +4742,25 @@ int main(int argc, char** argv) { // streamed during a prompt and copied back after it, so those are kept in RAM too as far as RAM allows. The // bytes are the file's bytes and the placement is the same, so the answers are the plain mmap mode's; the // share-of-pinned figure above (which sizes the prompt path) is left as the mmap mode's for the same reason. + // --adapt-async (opt-in): where the asynchronous adaptive tier does not apply, the blocking tier runs, said once. + // It exchanges experts between the GPU caches and the resident RAM copy only (the copy is checked once it is + // built, below). Not beside the --batch slots (their windows run between a request's prompt chunks), a peer + // tier or the helper GPUs' tier, which pick their swaps from the same table. (A pipelined decode loop that + // adapts beside windows in flight needs the blocking tier too: such a mode belongs on this list.) + auto adapt_async_off = [&](const char* why) { + std::fprintf(stderr, "strata generate: --adapt-async 1 is off (%s): the blocking adaptive tier\n", why); + o.adapt_async = 0; + }; + if (o.adapt_async) { + const char* why = !o.serve ? "it needs --serve" + : o.adapt_every <= 0 || o.adapt_swaps <= 0 ? "the adaptive tier is off" + : !o.resident_cpu_experts ? "it needs the resident RAM mode, --resident-experts" + : o.batch > 0 ? "not with --batch slots" + : peer.valid() ? "not with --peer-device" + : remote_opt ? "not with --remote-expert-opt" + : nullptr; + if (why != nullptr) adapt_async_off(why); + } if (o.resident_cpu_experts) { int64_t lend_from = -1; if (o.prefill_chunk > 0 && !o.no_prefill_borrow && d_res != nullptr && xcache.slots() > 0) { @@ -4692,6 +4796,10 @@ int main(int argc, char** argv) { err = whole_err + "; " + err; } if (resident_ok) { + // --adapt-async: twice the exchange buffers - the second half bounces the copies in whose source is not + // 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()); 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()); @@ -4732,6 +4840,7 @@ int main(int argc, char** argv) { } #endif } + if (o.adapt_async && !src.complement_ready()) adapt_async_off("the resident RAM mode is not running"); if (o.serve) { if (o.spec < 2 || o.mtp.empty() || o.prefill_chunk <= 0 || (graph_hits && (thits.d_res == nullptr || host_res.empty()))) { @@ -5737,6 +5846,206 @@ int main(int argc, char** argv) { for (float& v : drive.d.usage) v *= o.adapt_decay; return true; }; + // ---- --adapt-async 1: the asynchronous adaptive tier (opt-in; the resident RAM mode, see the checks where + // the RAM copy is built). adapt() runs a whole round - choose, copy the evicted slots back with a stream sync, + // copy the new experts in from the RAM copy - on a thread the decode loop joins after the draft: ~24 ms every + // 4th window with ~90 swaps. Here a round moves through four steps, each started by `adapt_tick` between + // windows once the previous step's copies (events) and host work (the job thread) are done: + // 1. job: choose (adapt()'s rule, over snapshots of the routing counts and the residency) and copy the + // evicted slots back into exchange buffers (D2H), each from the cache of the card that owns its layer; + // 2. stage the exchanges and mark the evicted experts non-resident (the CPU reads them from the exchange + // buffers from the next window on); job: copy the new experts in (straight from a page-locked RAM copy, + // else through a pinned bounce buffer), on the owning card's refill stream; + // 3. mark the new experts resident; job: move the evicted blobs into the RAM places the new ones left; + // 4. flip the copy's offsets. + // A slot is overwritten only after its old expert stopped being planned for the GPU (step 2, between windows, + // with the device copies of the residency table uploaded as apply_pending does), and a RAM place only once its + // expert is resident (step 3), so no window computes from memory that is changing. A request starts with the + // round drained (adapt_tick(true)), before its prompt lends any slot. Not bit-exact run to run: which window + // first computes a swapped-in expert on the GPU (which rounds differently from the CPU) depends on when its + // copy lands. `j`: the swap's exchange buffer (its copy back) and bounce buffer (a_cap + j). + struct ASwap { int32_t layer, in, out, slot; int home; int64_t j; bool exchange; }; + struct AHome { strata::core::ExpertCache* cache; cudaStream_t stream; cudaEvent_t ev; int dev; bool used; }; + enum class AState { Idle, CopyBack, SwapIn, Commit }; + AState astate = AState::Idle; + std::vector aswaps; + std::vector a_usage; + std::vector a_res; + int64_t a_win = 0, a_last = 0, a_rounds = 0, a_swapped = 0; + std::atomic a_err{false}; + std::vector ahomes; // [0] = CUDA0's cache, [k] = layer split stage k's + std::unique_ptr ajob; + const int64_t a_cap = std::min(o.adapt_swaps, 96); // as the blocking tier's exchange buffers + if (o.adapt_async && !drive.d.usage.empty() && src.complement_ready() && src.exchange_capacity() >= 2 * a_cap) { + ahomes.push_back({&xcache, adapt_stream, adapt_ev, 0, false}); + for (auto& st : stages) ahomes.push_back({&st->cache, st->adapt_stream, st->adapt_ev, st->dev, false}); + ajob = std::make_unique(); + std::fprintf(stderr, "strata serve: adaptive tier asynchronous (--adapt-async 1): up to %lld swaps per round, " + "a round every %d windows\n", (long long) a_cap, o.adapt_every); + } + auto a_flush = [&]() { // the job's copies: an event after them on every stream used, submitted now (WDDM) + for (AHome& h : ahomes) + if (h.used) { + const strata::core::OnDevice on(h.dev); + if (cudaEventRecord(h.ev, h.stream) != cudaSuccess) a_err = true; + (void) cudaStreamQuery(h.stream); + } + }; + auto a_choose = [&]() { // step 1, on the job thread + struct C { float gain; int32_t layer, in, out; }; + std::vector all; + std::vector> cand, vict; + for (int64_t l = 0; l < g.n_layers; ++l) { + cand.clear(); + vict.clear(); + const float* u = a_usage.data() + l * g.n_expert; + const int32_t* r = a_res.data() + l * g.n_expert; + for (int32_t e = 0; e < (int32_t) g.n_expert; ++e) { + if (r[e] < 0) { if (u[e] >= 2.0f) cand.emplace_back(u[e], e); } + else vict.emplace_back(u[e], e); + } + if (cand.empty() || vict.empty()) continue; + std::sort(cand.begin(), cand.end(), [](auto& a, auto& b) { return a.first > b.first; }); + const size_t nc = std::min(cand.size(), vict.size()); + std::partial_sort(vict.begin(), vict.begin() + (ptrdiff_t) nc, vict.end(), + [](auto& a, auto& b) { return a.first < b.first; }); + for (size_t i = 0; i < nc; ++i) { + if (cand[i].first < vict[i].first + 1.5f) break; + all.push_back({cand[i].first - vict[i].first, (int32_t) l, cand[i].second, vict[i].second}); + } + } + std::sort(all.begin(), all.end(), [](const C& a, const C& b) { return a.gain > b.gain; }); + aswaps.clear(); + for (const C& c : all) { + if ((int64_t) aswaps.size() >= a_cap) break; + const int32_t slot = a_res[(size_t) c.layer * g.n_expert + c.out]; + if (slot < 0) continue; + // an exchange (as resident_stage_swaps): `in` from the RAM copy, `out` held by its slot alone takes its + // place there; otherwise `out` is in the copy already (the lend region) or `in` is not (a RAM budget) + const bool x = src.has_resident(c.layer, c.in) && !src.has_resident(c.layer, c.out); + aswaps.push_back({c.layer, c.in, c.out, slot, multi_gpu ? stage_of(c.layer) : 0, (int64_t) aswaps.size(), x}); + } + for (AHome& h : ahomes) h.used = false; + for (const ASwap& w : aswaps) { + if (!w.exchange) continue; + AHome& h = ahomes[(size_t) w.home]; + const strata::core::OnDevice on(h.dev); + if (const cudaError_t e = cudaMemcpyAsync(src.exchange_buffer(w.j), h.cache->device_slot(w.slot), + (size_t) strata::kernels::cpu::expert_layout().blob_bytes(w.layer), + cudaMemcpyDeviceToHost, h.stream); e != cudaSuccess) { + std::fprintf(stderr, "strata serve: adaptive tier: copy back of layer %d slot %d (CUDA%d) failed: %s\n", + (int) w.layer, (int) w.slot, h.dev, cudaGetErrorString(e)); + a_err = true; + } + h.used = true; + } + a_flush(); + }; + auto a_copy_in = [&]() { // step 2, on the job thread + for (AHome& h : ahomes) h.used = false; + for (const ASwap& w : aswaps) { + AHome& h = ahomes[(size_t) w.home]; + const size_t nb = (size_t) strata::kernels::cpu::expert_layout().blob_bytes(w.layer); + const uint8_t* b = src.pinned(w.layer, w.in) ? src.resident_blob(w.layer, w.in) : nullptr; + if (b == nullptr) { // pageable, or not in the RAM copy: through a bounce buffer (an async copy) + uint8_t* bb = src.exchange_buffer(a_cap + w.j); + if (bb == nullptr || !src.copy_blob(w.layer, w.in, bb)) { + std::fprintf(stderr, "strata serve: adaptive tier: layer %d expert %d could not be read\n", + (int) w.layer, (int) w.in); + a_err = true; + continue; + } + b = bb; + } + const strata::core::OnDevice on(h.dev); + if (const cudaError_t e = cudaMemcpyAsync(h.cache->device_slot(w.slot), b, nb, cudaMemcpyHostToDevice, + h.stream); e != cudaSuccess) { + std::fprintf(stderr, "strata serve: adaptive tier: copy of layer %d into slot %d (CUDA%d) failed: %s\n", + (int) w.layer, (int) w.slot, h.dev, cudaGetErrorString(e)); + a_err = true; + } + h.used = true; + } + a_flush(); + }; + auto a_ready = [&](bool wait) -> bool { // the job and its copies are done + if (wait) ajob->wait(); + else if (!ajob->idle()) return false; + for (AHome& h : ahomes) + if (h.used) { + const strata::core::OnDevice on(h.dev); + if (wait) { if (cudaEventSynchronize(h.ev) != cudaSuccess) a_err = true; } + else if (const cudaError_t q = cudaEventQuery(h.ev); q == cudaErrorNotReady) return false; + else if (q != cudaSuccess) a_err = true; + } + return true; + }; + // between windows: `drain` finishes the round in flight and starts none. False when a copy failed. + auto adapt_tick = [&](bool drain) -> bool { + if (!ajob) return true; + if (!drain) ++a_win; // one tick per window + for (;;) { + if (a_err.load()) return false; + switch (astate) { + case AState::Idle: + if (drain || a_win - a_last < (int64_t) o.adapt_every) return true; + a_last = a_win; + ++a_rounds; + a_usage = drive.d.usage; + a_res = host_res; + // #477: the routing counted, as adapt() counts it (--expert-profile-save; else `heat` is empty) + for (size_t i = 0; i < heat.size(); ++i) heat[i] += (double) drive.d.usage[i]; + for (float& v : drive.d.usage) v *= o.adapt_decay; + ajob->post(a_choose); + astate = AState::CopyBack; + return true; + case AState::CopyBack: { // step 2: the evicted experts leave the GPU's plan + if (!a_ready(drain)) return true; + if (a_err.load()) return false; + size_t k = 0; + for (size_t i = 0; i < aswaps.size(); ++i) { + const ASwap w = aswaps[i]; + const size_t out = (size_t) w.layer * g.n_expert + w.out; + if (host_res[out] != w.slot) continue; // the table moved since the snapshot: not this one + if (w.exchange && !src.stage_exchange(w.layer, w.in, w.out, w.j)) { + std::fprintf(stderr, "strata serve: adaptive tier: stage_exchange refused layer %d in %d out %d\n", + (int) w.layer, (int) w.in, (int) w.out); + return false; + } + host_res[out] = strata::core::kNotResident; // the CPU computes it from the next window on + srcp->prefetch(w.layer, w.out); + aswaps[k++] = w; + } + aswaps.resize(k); + res_upload(); + ajob->post(a_copy_in); + astate = AState::SwapIn; + if (!drain) return true; + continue; + } + case AState::SwapIn: // step 3: the new experts are in their slots + if (!a_ready(drain)) return true; + if (a_err.load()) return false; + for (const ASwap& w : aswaps) { + host_res[(size_t) w.layer * g.n_expert + w.in] = w.slot; + srcp->release(w.layer, w.in); + } + res_upload(); + a_swapped += (int64_t) aswaps.size(); + ajob->post([&] { src.commit_copies(); }); + astate = AState::Commit; + if (!drain) return true; + continue; + case AState::Commit: // step 4 + if (!a_ready(drain)) return true; + if (a_err.load()) return false; + src.commit_flip(); + aswaps.clear(); + astate = AState::Idle; + continue; // a round due now starts at this same boundary + } + } + }; // #477: write the learned profile (between requests and at QUIT: a prompt's lent slots are back by then). // A swap still in flight counts as done - its expert is resident once the copy lands. `why`: for the log. auto save_profile = [&](const char* why) { @@ -5744,6 +6053,8 @@ int main(int argc, char** argv) { std::vector resident(host_res.size(), 0); for (size_t i = 0; i < host_res.size(); ++i) resident[i] = host_res[i] >= 0; for (const auto& p : pending) resident[(size_t) p.first] = 1; + if (astate == AState::SwapIn) // --adapt-async: the same for a round past its step 2 + for (const ASwap& w : aswaps) resident[(size_t) w.layer * g.n_expert + w.in] = 1; std::string e; const auto ranked = strata::core::rank_learned_profile(g.n_layers, g.n_expert, resident, heat, profile_loaded); @@ -7175,6 +7486,10 @@ int main(int argc, char** argv) { return true; }; apply_pending(true); + if (!adapt_tick(true)) { // --adapt-async: the round in flight lands before the prompt lends any slot + std::printf("ERR an adaptive refill failed\n"); + return 1; + } // per-request sampling for the verify window's head (greedy when temperature is absent) strata::kernels::SamplerParams req_sp; req_sp.greedy = req_temperature <= 0.0f; @@ -7356,6 +7671,10 @@ int main(int argc, char** argv) { // expert ran on the GPU or the CPU (they round differently) depended on the copy's timing // (STRATA_ADAPT_NOWAIT=1: 0.1.37's non-blocking query, the A/B) apply_pending(!adapt_nowait()); + if (!adapt_tick(false)) { // --adapt-async: the round in flight moves on a step when it can + std::printf("ERR an adaptive refill failed\n"); + return 1; + } if (hist_n > 0) { // the tails the penalties count over, ONE PER ROW: the tokens the state has consumed, the // fed-back head `x` (it joins `consumed` only after this window commits), then the drafts @@ -7379,7 +7698,8 @@ int main(int argc, char** argv) { const Clock::time_point tw1 = Clock::now(); std::thread adapt_thr; // the adaptive tier beside the commit and the draft (as in generate) bool adapt_ok = true; - if (!drive.d.usage.empty() && ((rounds + 1) % o.adapt_every) == 0) + // (--adapt-async 1: the asynchronous tier above instead, ticked before the window) + if (!drive.d.usage.empty() && !ajob && ((rounds + 1) % o.adapt_every) == 0) adapt_thr = std::thread([&] { adapt_ok = adapt(); }); if (!ver.commit(a + 1, err)) { if (adapt_thr.joinable()) adapt_thr.join(); @@ -7672,6 +7992,9 @@ int main(int argc, char** argv) { "VRAM tier, %lld blob reads from the file\n", (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); // 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 "