From b2dbd9420d7860b79d553afd977ba2e70cff9d52 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 28 Oct 2026 16:19:15 +0100 Subject: [PATCH] dsv41 serve: the cache settle runs in the idle between requests (WP-8's option 1) The prompt-side settle is 800 ms because it sits inside the prompt's own time, and for a run with no next request (the CLI, a one-shot tool call) that is the right price. In a server it is not: an agent's turn ends, it runs its tool for a second or ten, and that time is free for the swap rounds the *next* answer needs. Measured why it matters (job d18-attrib, 2026-10-28): the same candidate engine with the settle at 6,000 ms decodes a 256-token code answer at 10.92 / 11.14 tok/s, at 800 ms at 9.82 / 9.62 - 12% paid for the prompt. The idle settle is meant to give that back without returning the 6 s to the prompt. How: Model::settle_idle(cancel) runs cache_settle's swap rounds on its own thread, without the RAM store preload, for at most DS4_SETTLE_IDLE_MS (6,000) and stopping between rounds when cancel is set. The serve loop starts it at the top of each iteration (nothing in flight), and cancels and joins it the moment a line arrives, before any request work touches the cache. DS4_SETTLE_IDLE=0 turns it off; the prompt-side budget is unchanged. Not measured yet: the benefit needs a serve-path harness (two GEN requests with an idle between them). On the CLI the thread is never started, so no CLI number moves. --- src/dsv41/ds4_main.cpp | 26 +++++++++++++++++++++++++- src/dsv41/ds4_model.cu | 26 ++++++++++++++++++++++++++ src/dsv41/ds4_model.hpp | 4 ++++ 3 files changed, 55 insertions(+), 1 deletion(-) 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);