From 3eb74054e0106bb6a544c159ded25d613196270c Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Fri, 16 Oct 2026 20:54:11 +0200 Subject: [PATCH] perf(serve): wait for the adaptive tier's swaps at the round start Job 803 (static vs adaptive tier, paired): with the context synchronizes gone from the publication, the adaptive swap copies (96 experts, ~0.2 GB every 4 rounds) overlap the next window and its CPU share; the CPU pool time per round rose by ~1-2 ms and the fenced publication lost against the serial one with the tier adapting (62.6/63.8 vs 63.7/64.4 tok/s), while it was neutral to positive with static residency. The swaps now land before the next window (wait_swaps at apply_pending, after the draft): what the synchronizes used to enforce, made explicit, and the resident set of a window no longer depends on when the copies finished. LELEKA_SERVE_SWAP_WAIT=off polls as generate.cpp's apply_pending(false). Co-Authored-By: Claude Opus 5.5 --- .../src/qwen_forward/original_serve_residency.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/leleka-runtime/src/qwen_forward/original_serve_residency.rs b/crates/leleka-runtime/src/qwen_forward/original_serve_residency.rs index b32a7165..4e735286 100644 --- a/crates/leleka-runtime/src/qwen_forward/original_serve_residency.rs +++ b/crates/leleka-runtime/src/qwen_forward/original_serve_residency.rs @@ -432,6 +432,13 @@ impl<'ctx> OriginalServePool<'ctx> for GpuExpertPool<'_, '_, 'ctx> { self.cpu.run_entries(rows, sink, Some(&include)) } fn apply_pending(&mut self) -> Result<()> { + // The swaps issued beside the previous commit land before this window (`apply_pending(true)`): their copies + // would otherwise read host memory while the CPU pool streams its experts (job 803: the overlap costs the CPU + // share more than the wait), and the resident set no longer depends on when the copies finished. + // LELEKA_SERVE_SWAP_WAIT=off polls instead (generate.cpp `apply_pending(false)`). + if swap_wait() { + return self.cache.wait_swaps().map_err(|e| driver_error(&e)); + } self.cache.poll_swaps().map(|_| ()).map_err(|e| driver_error(&e)) } fn begin_adaptation(&mut self, next_round: u64) -> Result<()> { @@ -468,6 +475,12 @@ impl<'ctx> OriginalServePool<'ctx> for GpuExpertPool<'_, '_, 'ctx> { } } +/// `LELEKA_SERVE_SWAP_WAIT=off`: poll the adaptive swaps at a round start instead of waiting for them. +fn swap_wait() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| !std::env::var("LELEKA_SERVE_SWAP_WAIT").is_ok_and(|v| v == "off" || v == "0")) +} + /// generate.cpp startup of the tier for the serve stack: size and open the cache after every other owner, /// fill it from the profile through the registered bank (blocking, rank order; per-layer skips a full /// layer's pairs, global stops at the first refusal) and read slot 0 back. `None`: the tier is off or no