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