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 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-10-16 20:54:11 +02:00
co-authored by Claude Opus 5.5
parent 6fa92c13e4
commit 3eb74054e0
@@ -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<bool> = 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