From 13eee452deb50ece28603f65b60df6d9f07da2ca Mon Sep 17 00:00:00 2001 From: Francesco Albano <119073350+Hardin22@users.noreply.github.com> Date: Tue, 6 Oct 2026 16:58:49 +0200 Subject: [PATCH] pipeline-windows beside the asynchronous adaptive tier: copies queued from the decode loop at a card's gap (#905) --adapt-async 1 no longer drops to the blocking tier with --pipeline-windows 2: the steps that overwrite something a window in flight may read wait for those windows. STRATA_PIPELINE_ADAPT_ASYNC=0 keeps the blocking tier there. Opt-in; default output unchanged. --- docs/DETAILS.md | 11 +- docs/MULTI_GPU.md | 4 + src/program/generate.cpp | 297 ++++++++++++++++++++++++++++++--------- 3 files changed, 244 insertions(+), 68 deletions(-) diff --git a/docs/DETAILS.md b/docs/DETAILS.md index 10f181776f..f225d8a6a4 100644 --- a/docs/DETAILS.md +++ b/docs/DETAILS.md @@ -154,10 +154,13 @@ other ~18 GB), a 32 GB PC with a 12-16 GB GPU the Coder; IQ3_XXS on a 32 GB PC s 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. - It stays on the blocking tier (said in the log) when the exchange buffers are not page-locked, and with - `--pipeline-windows`; the stats line reports ms per round. + `--peer-device` (the blocking tier runs there). With `--pipeline-windows 2` (two windows in flight, see + [MULTI_GPU.md](MULTI_GPU.md)) the copies into the evicted slots and the moves into RAM also wait until the windows + that were in flight when they were decided have completed (`STRATA_PIPELINE_ADAPT_ASYNC=0`: the blocking tier + 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. + It stays on the blocking tier (said in the log) when the exchange buffers are not page-locked; the stats line + reports ms per round. - `STRATA_EXCHANGE_ROTATE=1` (opt-in): an adaptive swap hands buffer ownership over instead of copying the evicted blob into the RAM copy (equal-size blobs, fully page-locked copy). Same tokens, fewer host copies; it works with `--adapt-async 1` too. Details and the measurement: [EXCHANGE_ROTATION.md](EXCHANGE_ROTATION.md). diff --git a/docs/MULTI_GPU.md b/docs/MULTI_GPU.md index dc563931c5..50080d7d86 100644 --- a/docs/MULTI_GPU.md +++ b/docs/MULTI_GPU.md @@ -227,6 +227,10 @@ Two cards, exactly two stages, `--serve`. In the config: (`--expert-cache-device1..3`, `--remote-expert-opt`), a split into three or more stages or onto one GPU (`--split-device 0`), or no draft layer. A request with repetition penalties (`penalty_last_n`) or coupled draft sampling decodes serially. +- **With the resident RAM mode's asynchronous swaps** (`--adapt-async 1`, [DETAILS.md](DETAILS.md)) a round's steps + advance between the verified windows. Each card's copies are queued by the decode loop itself while that card has + no window in flight, after every window that may still read what they overwrite has finished; the moves into RAM + wait the same way. - **Measured** (Swift 1.5 IQ3_XXS, 160K context, q4_0 KV, the stock draft layer, RTX 4060 Ti (layers 0-19) + RTX 5080 (20-47), i9-14900KF, 32 GB of RAM with the resident RAM mode on the split (#848); greedy, 500 tokens, two interleaved pairs of three rounds, decode tok/s): Python code 90.4 -> 103.1, C code 76.6 -> 81.6, English prose diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 04a407c256..ccaec33df5 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -5470,20 +5470,26 @@ int main(int argc, char** argv) { // --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.) + // tier or the helper GPUs' tier, which pick their swaps from the same table. The pipelined decode loop + // (--pipeline-windows 2) has windows in flight at every tick: there the steps that overwrite something a window + // may still read wait for those windows (see adapt_tick); STRATA_PIPELINE_ADAPT_ASYNC=0 keeps the blocking tier + // beside it instead. 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) { + static const bool pl_async = [] { + const char* v = std::getenv("STRATA_PIPELINE_ADAPT_ASYNC"); + return v == nullptr || std::atoi(v) != 0; + }(); 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" - : o.pipeline_windows > 0 ? "not with --pipeline-windows (its windows are in flight while the tier adapts)" : peer.valid() ? "not with --peer-device" : remote_opt ? "not with --remote-expert-opt" + : o.pipeline_windows >= 2 && !pl_async ? "not with --pipeline-windows 2 (STRATA_PIPELINE_ADAPT_ASYNC=0)" : nullptr; if (why != nullptr) adapt_async_off(why); } @@ -6309,6 +6315,7 @@ int main(int argc, char** argv) { // INVARIANT: nothing pipelined is in flight outside read_windows_pl and the pipelined decode loop - both end // here (a checkpoint, a park, the next request and the serial paths all find both stages and the drafter idle). // Every window in flight is served to its end (its picks discarded), then both stages and the drafter idle. + // (--adapt-async: the decode loop then settles the asynchronous tier's round, a_pl_settle, see its end.) auto pl_drain = [&]() { std::string e2; for (int round = 0; round < 10000000; ++round) { @@ -6916,9 +6923,29 @@ int main(int argc, char** argv) { // 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; }; + // --pipeline-windows 2: the pipelined decode loop has windows in flight on both cards at every tick (window K+1 + // on the first while the last verifies or commits K), each one waiting on this thread at every layer (its + // doorbell) and planning every layer from the residency table as it is when that layer is served. There the + // tier keeps two rules (a_pl_idle, set by that loop alone; unset, nothing is in flight at a tick): + // - no CUDA call of the tier reaches a card while a window there waits on this thread. A copy queued to a + // card whose window spins on a host flag can block in the driver until that window ends (issue #31, where + // the host waited forever), and a thread blocked in the driver can hold up this one, which the window + // waits for. So the job thread does host work alone (the choice, the bounce copies, the RAM moves) and + // this thread queues each card's copies at that card's next gap - a moment its stage has no window in + // flight (a_gap: before a window is launched there, and at every tick) - from page-locked memory only; + // - a gap of a stage after a step is also the step's fence for that card: every window planned there before + // the step has completed. So step 2's copies into the evicted slots (queued at a gap after step 2) + // overwrite no slot a window may still read, and step 3's RAM moves wait (AState::RamFence) for a gap after + // step 3 on every card whose layers exchanged (a window planned before may still pull an incoming expert + // over PCIe from the RAM place its evicted one takes). The CPU's reads need no fence: the pool runs inside + // the loop's service calls. + // The device copies of the residency table are not uploaded meanwhile (a synchronous copy; the pipelined + // verifiers publish every row whatever they say): once, when the loop has drained. The loop ticks once per + // verdict (after the drafter's chain is launched) and, while a round is in flight, every 0.5 ms without + // counting a window. + struct ASwap { int32_t layer, in, out, slot; int home; int64_t j; bool exchange; const uint8_t* from; }; struct AHome { strata::core::ExpertCache* cache; cudaStream_t stream; cudaEvent_t ev; int dev; bool used; }; - enum class AState { Idle, CopyBack, SwapIn, Commit }; + enum class AState { Idle, Pick, CopyBack, SwapIn, RamFence, Commit }; AState astate = AState::Idle; std::vector aswaps; std::vector a_usage; @@ -6937,15 +6964,22 @@ int main(int argc, char** argv) { 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); - } + // the pipelined loop's side (see above): whether stage `st` has no window in flight; per home, the copies back + // and in to queue at its next gap, the RAM fence still to pass and the gaps seen + std::function a_pl_idle; + std::vector a_back_due(ahomes.size(), 0), a_in_due(ahomes.size(), 0), a_ram_due(ahomes.size(), 0); + std::vector a_gap_seq(ahomes.size(), 0), a_ram_mark(ahomes.size(), 0); + bool a_res_dirty = false; + auto a_due = [](const std::vector& v) { + return std::any_of(v.begin(), v.end(), [](uint8_t b) { return b != 0; }); }; - auto a_choose = [&]() { // step 1, on the job thread + auto a_mark = [&](AHome& h) { // an event after the copies just queued on h's stream, submitted now (WDDM) + const strata::core::OnDevice on(h.dev); + if (cudaEventRecord(h.ev, h.stream) != cudaSuccess) a_err = true; + (void) cudaStreamQuery(h.stream); + h.used = true; + }; + auto a_pick = [&]() { // step 1's choice (host work) struct C { float gain; int32_t layer, in, out; }; std::vector all; std::vector> cand, vict; @@ -6977,50 +7011,79 @@ int main(int argc, char** argv) { // 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}); + aswaps.push_back({c.layer, c.in, c.out, slot, multi_gpu ? stage_of(c.layer) : 0, (int64_t) aswaps.size(), x, + nullptr}); } - 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; - } + auto a_back = [&](int hi) { // step 1's copies back (D2H) from home hi's cache, then its event + AHome& h = ahomes[(size_t) hi]; + bool any = false; + { 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; + for (const ASwap& w : aswaps) { + if (w.home != hi || !w.exchange) continue; + 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; + } + any = true; } - h.used = true; } - a_flush(); + if (any) a_mark(h); + }; + auto a_from = [&](const ASwap& w) -> const uint8_t* { // step 2: where `in` is copied from (host work) + if (src.pinned(w.layer, w.in)) + if (const uint8_t* b = src.resident_blob(w.layer, w.in)) return b; + // 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; + return nullptr; + } + return bb; + }; + auto a_in_one = [&](const ASwap& w, const uint8_t* b) { // step 2: one copy in (H2D), on its home's stream + AHome& h = ahomes[(size_t) w.home]; + const strata::core::OnDevice on(h.dev); + if (const cudaError_t e = cudaMemcpyAsync(h.cache->device_slot(w.slot), b, + (size_t) strata::kernels::cpu::expert_layout().blob_bytes(w.layer), + 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; + } + }; + auto a_choose = [&]() { // step 1 on the job thread (the serial loop) + a_pick(); + for (AHome& h : ahomes) h.used = false; + for (size_t hi = 0; hi < ahomes.size(); ++hi) a_back((int) hi); + }; + auto a_copy_in = [&]() { // step 2 on the job thread (the serial loop): each source read, then copied in + std::vector used(ahomes.size(), 0); + for (const ASwap& w : aswaps) + if (const uint8_t* b = a_from(w)) { + a_in_one(w, b); + used[(size_t) w.home] = 1; + } + for (size_t hi = 0; hi < ahomes.size(); ++hi) + if (used[hi]) a_mark(ahomes[hi]); + }; + auto a_stage_in = [&]() { // step 2's host part on the job thread (the pipelined loop: a_in queues the copies) + for (ASwap& w : aswaps) w.from = a_from(w); + }; + auto a_in = [&](int hi) { // step 2's copies in to home hi (the pipelined loop, at its gap) + bool any = false; + for (const ASwap& w : aswaps) + if (w.home == hi && w.from != nullptr) { + a_in_one(w, w.from); + any = true; + } + if (any) a_mark(ahomes[(size_t) hi]); }; auto a_ready = [&](bool wait) -> bool { // the job and its copies are done if (wait) ajob->wait(); @@ -7034,10 +7097,35 @@ int main(int argc, char** argv) { } return true; }; - // between windows: `drain` finishes the round in flight and starts none. False when a copy failed. - auto adapt_tick = [&](bool drain) -> bool { + // --pipeline-windows 2: stage `st` has no window in flight (see above) - the copies due on its card are queued + // now, and the steps taken before are fenced for it. On the loop's thread. + auto a_gap = [&](int st) { + if (!a_pl_idle || st < 0 || st >= (int) ahomes.size() || !a_pl_idle(st)) return; + ++a_gap_seq[(size_t) st]; + if (a_back_due[(size_t) st]) { + a_back_due[(size_t) st] = 0; + a_back(st); + } + if (a_in_due[(size_t) st] && ajob->idle()) { // once the job has read the sources + a_in_due[(size_t) st] = 0; + a_in(st); + } + }; + auto a_picked = [&]() { // (pipelined) step 1's choice is made: each card's copies back at its next gap + for (AHome& h : ahomes) h.used = false; + for (const ASwap& w : aswaps) + if (w.exchange) a_back_due[(size_t) w.home] = 1; + astate = AState::CopyBack; + for (int st = 0; st < (int) ahomes.size(); ++st) a_gap(st); + }; + // between windows: `drain` finishes the round in flight and starts none; `count` false (the pipelined loop + // between verdicts) moves it on without counting a window. False when a copy failed. + auto adapt_tick = [&](bool drain, bool count = true) -> bool { if (!ajob) return true; - if (!drain) ++a_win; // one tick per window + if (!drain && count) ++a_win; // one tick per window + const bool pl = (bool) a_pl_idle; + if (pl) + for (int st = 0; st < (int) ahomes.size(); ++st) a_gap(st); for (;;) { if (a_err.load()) return false; switch (astate) { @@ -7051,10 +7139,23 @@ int main(int argc, char** argv) { // #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; + if (pl) { + ajob->post(a_pick); + astate = AState::Pick; + } else { + ajob->post(a_choose); + astate = AState::CopyBack; + } return true; + case AState::Pick: // (pipelined) step 1: the copies back wait for the choice, then for their card's gap + if (!ajob->idle()) { + if (!drain) return true; + ajob->wait(); + } + a_picked(); + continue; case AState::CopyBack: { // step 2: the evicted experts leave the GPU's plan + if (a_due(a_back_due)) return true; // (pipelined) a card not seen idle since the choice if (!a_ready(drain)) return true; if (a_err.load()) return false; size_t k = 0; @@ -7072,21 +7173,43 @@ int main(int argc, char** argv) { aswaps[k++] = w; } aswaps.resize(k); - res_upload(); - ajob->post(a_copy_in); + for (AHome& h : ahomes) h.used = false; + if (pl) { // the copies in at each card's next gap: after this step, so fenced (see above) + a_res_dirty = true; + for (const ASwap& w : aswaps) a_in_due[(size_t) w.home] = 1; + ajob->post(a_stage_in); + } else { + 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_due(a_in_due)) return true; // (pipelined) a card not seen idle since step 2 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(); + if (pl) a_res_dirty = true; + else res_upload(); a_swapped += (int64_t) aswaps.size(); + if (pl) // the RAM moves wait for a gap after this step on every card whose layers exchanged + for (const ASwap& w : aswaps) + if (w.exchange) { + a_ram_due[(size_t) w.home] = 1; + a_ram_mark[(size_t) w.home] = a_gap_seq[(size_t) w.home]; + } + astate = AState::RamFence; + continue; + case AState::RamFence: // step 3's RAM moves, once no window may still read the places they fill + if (pl) + for (size_t hi = 0; hi < ahomes.size(); ++hi) + if (a_ram_due[hi] && a_gap_seq[hi] <= a_ram_mark[hi]) return true; + std::fill(a_ram_due.begin(), a_ram_due.end(), (uint8_t) 0); ajob->post([&] { src.commit_copies(); }); astate = AState::Commit; if (!drain) return true; @@ -7102,6 +7225,25 @@ int main(int argc, char** argv) { } } }; + // --pipeline-windows 2: the loop has drained (nothing in flight on either card): what waits for a gap goes now, + // and the round in flight goes on unfenced (a_pl_idle unset) from the next tick, the next request's drain. The + // job's work is the host's alone, so the wait for it ends. + auto a_pl_settle = [&]() { + if (!ajob || !a_pl_idle) return; + ajob->wait(); + bool idle = true; + for (int st = 0; st < (int) ahomes.size(); ++st) idle = idle && a_pl_idle(st); + if (idle) { + if (astate == AState::Pick) a_picked(); + for (int st = 0; st < (int) ahomes.size(); ++st) a_gap(st); + if (a_res_dirty) res_upload(); + } else { // the loop did not drain (a window failed): the round cannot go on, the next tick says so + std::fprintf(stderr, "strata serve: adaptive tier: a window is still in flight after the pipelined loop\n"); + a_err = true; + } + a_pl_idle = nullptr; + a_res_dirty = false; + }; // #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) { @@ -9178,17 +9320,23 @@ int main(int argc, char** argv) { drive.d.experts = 0; drive.d.failed = false; // the adaptive tier beside the pipeline: the serial loop's rule and adapt() on a thread of its own (its - // copies wait for the windows in flight, which this loop keeps serving meanwhile) + // copies wait for the windows in flight, which this loop keeps serving meanwhile). --adapt-async: the + // asynchronous tier's steps instead, its copies queued here at each card's gaps (a_pl_idle, a_gap) std::thread pl_adapt_thr; std::atomic pl_adapt_done{true}; bool pl_adapt_ok = true; // an error ends the engine, as the serial loop's do: every GPU wait is released first (#267), so no - // window in flight keeps spinning on a flag nobody raises, and the tier's thread can finish + // window in flight keeps spinning on a flag nobody raises, and the tier's thread can finish (the + // asynchronous tier's job does host work alone: it ends) auto die = [&](const std::string& m) -> int { std::fprintf(stderr, "strata serve: pipelined decode: %s\n", m.c_str()); strata::core::diag_pipeline_fn().store(nullptr); strata::core::release_gpu_waits(stderr); if (pl_adapt_thr.joinable()) pl_adapt_thr.join(); + if (ajob) { + ajob->wait(); + a_pl_idle = nullptr; + } std::printf("ERR %s\n", m.c_str()); std::fflush(stdout); return 1; @@ -9310,7 +9458,11 @@ int main(int argc, char** argv) { stage_record(exch_ev); exch_wait = true; }; - auto pl_adapt = [&]() -> bool { + // --adapt-async: the asynchronous tier beside the windows (see adapt_tick). A stage is idle when neither + // of its verifiers has a window in flight (a commit or a snapshot copy waits on nothing of the host's) + if (ajob) a_pl_idle = [&](int st) { return !PV[st][0]->in_flight() && !PV[st][1]->in_flight(); }; + double a_poll_ms = 0.0; + auto pl_adapt = [&]() -> bool { // the blocking tier (--adapt-async 0) if (pl_adapt_thr.joinable() && pl_adapt_done.load()) { pl_adapt_thr.join(); if (!pl_adapt_ok) return false; @@ -9396,6 +9548,14 @@ int main(int argc, char** argv) { g_pl_diag.chain_live.store(mtp.chain_live() ? 1 : 0, std::memory_order_relaxed); g_pl_diag.doomed.store(doomed ? 1 : 0, std::memory_order_relaxed); g_pl_diag.ending.store(ending ? 1 : 0, std::memory_order_relaxed); + // ---- --adapt-async: the round in flight moves on between verdicts too (at most every 0.5 ms) + if (ajob && astate != AState::Idle) { + const double t = ms_now(); + if (t - a_poll_ms >= 0.5) { + a_poll_ms = t; + if (!adapt_tick(false, false)) return die("an adaptive refill failed"); + } + } // ---- every window in flight served if ((doomed && !pump0(D)) || !pump0(A) || !pump0(B)) return die(err); if (A.s1 && !A.s1_done) { @@ -9510,6 +9670,7 @@ int main(int argc, char** argv) { // ---- stage 0: A (never while a wrong window still holds stage 0's state) if (A.ready && !A.launched && !doomed) { if (A.p + A.T > o.max_context) { ending = true; continue; } + if (ajob) a_gap(0); // --adapt-async: stage 0 is idle until this launch if (!snap_take(A.seq)) return die("the GDN snapshot failed"); if (!V0(A).pl_launch(A.T, A.tok, A.p, err)) return die(err); A.launched = true; @@ -9518,6 +9679,7 @@ int main(int argc, char** argv) { // ---- stage 0: B, speculatively, right behind A if (B.ready && !B.launched && A.finished && !A.committed && !doomed) { if (B.p_on >= theta && B.p + B.T <= o.max_context) { + if (ajob) a_gap(0); // --adapt-async: stage 0 is idle until this launch { const strata::core::OnDevice on(dev0); if (pl_snap_overlap ? cudaStreamWaitEvent(s0, pl_snap_ev[A.seq & 1], 0) != cudaSuccess @@ -9543,6 +9705,7 @@ int main(int argc, char** argv) { // ---- stage 1: A, once its stage 0 is done if (A.finished && !A.s1) { if (chain_kind == 1 && !B.ready) ++pl_late; // stage 0 idles until the chain has B + if (ajob) a_gap(1); // --adapt-async: stage 1 is idle until this launch if (!V1(A).pl_launch(A.T, A.tok, A.p, err)) return die(err); A.s1 = true; tre("L1", A.seq, A.T); @@ -9600,7 +9763,7 @@ int main(int argc, char** argv) { B.launched ? "launched" : B.ready ? "ready" : "none", on ? " ON" : ""); x = outp[(size_t) a]; p = A.p + a + 1; - if (!last && !pl_adapt()) return die("an adaptive refill failed"); + if (!last && !ajob && !pl_adapt()) return die("an adaptive refill failed"); if (on && !last) { ++pl_on; // the chain over A, forced through B's drafts: B's bonus guess and the next window's drafts @@ -9642,6 +9805,8 @@ int main(int argc, char** argv) { A = nA; } } + // --adapt-async: the verdict's tick, once the drafter's chain is launched (off its critical path) + if (!last && ajob && !adapt_tick(false)) return die("an adaptive refill failed"); if (last) ending = true; } pl_drain(); // nothing in flight: both stages' commits and the drafter have landed @@ -9649,6 +9814,10 @@ int main(int argc, char** argv) { if (pl_adapt_thr.joinable()) pl_adapt_thr.join(); // its copies waited only for finished work adapt_fence = nullptr; if (exch_wait) pl_release(); // every window has completed (drained) + // --adapt-async: both stages idle now: the copies still due are queued and the round in flight goes on + // unfenced (the INVARIANT at pl_drain: a checkpoint, a park, the serial loop and the next request find + // nothing pipelined in flight) at the next tick, the next request's drain + a_pl_settle(); for (cudaEvent_t ev : fence_ev) if (ev) cudaEventDestroy(ev); for (cudaEvent_t ev : exch_ev) if (ev) cudaEventDestroy(ev); if (pl_trace_path != nullptr && !pl_trace.empty()) {