diff --git a/src/dsv41/ds4_main.cpp b/src/dsv41/ds4_main.cpp index c4354749..47214cfe 100644 --- a/src/dsv41/ds4_main.cpp +++ b/src/dsv41/ds4_main.cpp @@ -713,7 +713,30 @@ int serve(strata::ds4::Model& m, const std::string& eos_list, bool spec, float c std::signal(SIGPIPE, SIG_IGN); #endif } - while (in.next(line)) { + // WP-8's option (1): the post-prompt cache settle runs in the gaps between requests and gives way the moment one arrives + // (DS4_SETTLE_IDLE=0 turns it off). An agent's turn ends, its tool runs, and the swaps the next answer wants go in during + // that idle - which is what the 800 ms prompt-side budget cannot do for a run that has no next request. + std::atomic settle_cancel{false}; + std::thread settle_th; + const bool settle_idle_on = [] { + const char* v = std::getenv("DS4_SETTLE_IDLE"); + return v == nullptr || std::atoi(v) != 0; + }(); + const auto settle_stop = [&] { + if (!settle_th.joinable()) return; + settle_cancel.store(true, std::memory_order_release); + settle_th.join(); + }; + const auto settle_start = [&] { + if (!settle_idle_on || settle_th.joinable()) return; + settle_cancel.store(false, std::memory_order_release); + settle_th = std::thread([&m, &settle_cancel] { m.settle_idle(settle_cancel); }); + }; + for (;;) { + settle_start(); // (nothing in flight: this is the idle) + const bool have_line = in.next(line); + settle_stop(); // (a line arrived, or the input closed) + if (!have_line) break; if (line == "QUIT") break; const bool geni = line.rfind("GENI ", 0) == 0; if (!geni && line.rfind("GEN ", 0) != 0) { @@ -1024,6 +1047,7 @@ int serve(strata::ds4::Model& m, const std::string& eos_list, bool spec, float c note(msg); } } + settle_stop(); // (the idle thread must be off the cache before the profile is saved and the engine ends) m.save_profile(); return 0; } diff --git a/src/dsv41/ds4_model.cu b/src/dsv41/ds4_model.cu index dadc0bf1..dd95fc73 100644 --- a/src/dsv41/ds4_model.cu +++ b/src/dsv41/ds4_model.cu @@ -6985,6 +6985,31 @@ struct Model::Impl { std::fprintf(stderr, "ds4: expert cache: %lld swaps after the prompt (%.0f ms)\n", (long long) (st.swaps - s0), now_ms() - t0); } + // WP-8's option (1): the same swap rounds, run on a thread while the server has no request in flight, stopped by `cancel`. + // The prompt path keeps its 800 ms (a run with no server has no idle to use), so this is pure addition: an agent's turn + // ends, the swaps go in while it runs its tool, and the next turn's answer finds them. Interrupting costs one round (a + // swap of ~9 MB, a millisecond or two) because cancel is tested between rounds, and cache_tick_swaps is the same call the + // prompt path makes, so the state it leaves is the state a decode window already handles. + void cache_settle_idle(const std::atomic& cancel) { + if (!o.expert_cache || cache_ths.empty()) return; + static const double budget = env_f("DS4_SETTLE_IDLE_MS", 6000.0f); + const double t0 = now_ms(); + const int64_t s0 = st.swaps; + while (!cancel.load(std::memory_order_acquire) && now_ms() - t0 < budget) { + for (Layer& l : L) { + if (l.mtp || l.spare_vram < 0 || l.swap_pending) continue; + l.cpu_hits.clear(); + for (int e = 0; e < l.n_exp; ++e) + if (l.slot[(size_t) e] < 0 && l.heat[(size_t) e] >= 2.0f) l.cpu_hits.push_back(e); + } + cache_tick_swaps(0); + if (sw_inflight == 0) break; // (nothing queued: no layer's best CPU-lane expert is hotter than its coldest VRAM one) + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + if (o.verbose) + std::fprintf(stderr, "ds4: expert cache: %lld swaps in the idle (%.0f ms, cancelled %d)\n", + (long long) (st.swaps - s0), now_ms() - t0, cancel.load() ? 1 : 0); + } void cache_tick(int T) { cache_tick_swaps(T); store_sync(); @@ -10497,6 +10522,7 @@ bool Model::slot_load(const uint8_t* data, size_t bytes, int64_t n_pos, std::str return p_->slot_load(data, bytes, n_pos, err); } std::string Model::slot_identity() const { return p_->slot_identity(); } +void Model::settle_idle(const std::atomic& cancel) { p_->cache_settle_idle(cancel); } void Model::save_profile() const { p_->save_profile(); } const Stats& Model::stats() const { Stats& st = p_->st; diff --git a/src/dsv41/ds4_model.hpp b/src/dsv41/ds4_model.hpp index 6cf65dba..20fdf519 100644 --- a/src/dsv41/ds4_model.hpp +++ b/src/dsv41/ds4_model.hpp @@ -157,6 +157,10 @@ public: const std::vector& tokens() const; // what the state has consumed, in order void reset(); + /// The post-prompt cache settle run on a thread of its own while the server waits for its next request (WP-8's option 1): + /// the swap rounds of cache_settle without the store preload, stopped early when `cancel` goes true. `done` is set when + /// the thread has nothing left to do. Only one such run at a time; the caller joins the thread after setting cancel. + void settle_idle(const std::atomic& cancel); /// Keep the state at the current position in checkpoint slot 0 or 1 (the end of a prompt, the position before it); /// restore(limit) goes back to the latest one at or before limit (-1: none). void checkpoint(int slot = 0);