perf(serve): wait for the lent-slot refill behind the decode phase's assembly (LELEKA_REFILL_DEFER=off controls)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
2f793d03c9
commit
ef6e2c54f8
@@ -675,6 +675,7 @@ pub(in crate::qwen_forward) fn refill_lent<'ctx>(
|
||||
moe: &MoeWeightCache<'ctx>,
|
||||
cache: &mut OriginalExpertCache<'ctx>,
|
||||
known: &mut SpanCache,
|
||||
defer_wait: bool,
|
||||
) -> Result<usize> {
|
||||
let cached = span_cache_enabled();
|
||||
let bank = moe.original_expert_bank().ok_or(Error::IdentityMismatch)?;
|
||||
@@ -697,11 +698,28 @@ pub(in crate::qwen_forward) fn refill_lent<'ctx>(
|
||||
cache.fill_slot_queued(slot, Rc::clone(&bank), spans).map_err(|e| driver_error(&e))?;
|
||||
refilled += 1;
|
||||
}
|
||||
let _span_sync = super::original_serve_profile::span("refill.sync");
|
||||
cache.sync_queued().map_err(|e| driver_error(&e))?;
|
||||
if defer_wait {
|
||||
// the copies run behind the host's assembly of the decode phase; `settle_refill` waits for them before any
|
||||
// window reads the cache
|
||||
return Ok(refilled);
|
||||
}
|
||||
settle_refill(cache)?;
|
||||
Ok(refilled)
|
||||
}
|
||||
|
||||
/// Wait for the refill copies queued by `refill_lent(.., defer_wait = true)` (a no-op when none are queued).
|
||||
pub(in crate::qwen_forward) fn settle_refill(cache: &mut OriginalExpertCache<'_>) -> Result<()> {
|
||||
let _span_sync = super::original_serve_profile::span("refill.sync");
|
||||
cache.sync_queued().map_err(|e| driver_error(&e))
|
||||
}
|
||||
|
||||
/// `LELEKA_REFILL_DEFER=off`: wait for the lent-slot refill at the end of the prompt phase (the earlier behaviour; A/B control)
|
||||
/// instead of behind the decode phase's assembly.
|
||||
pub(in crate::qwen_forward) fn refill_defer_enabled() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| !std::env::var("LELEKA_REFILL_DEFER").is_ok_and(|v| v == "off" || v == "0"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -186,6 +186,9 @@ pub(crate) struct OriginalServeStack<'a, 'ctx> {
|
||||
expert_cache: Option<OriginalExpertCache<'ctx>>,
|
||||
loan: Option<usize>,
|
||||
residency: ResidencyState,
|
||||
/// Lent-slot refill copies are queued and not yet waited (`LELEKA_REFILL_DEFER`): the decode phase waits before any
|
||||
/// window reads the cache; every other user of the cache waits inside its own entry points.
|
||||
refill_pending: bool,
|
||||
/// Wall-clock milliseconds of the last request's prompt and decode phases (operator measurements).
|
||||
timing: (f64, f64),
|
||||
executor: OriginalPromptExecutor<'ctx>,
|
||||
@@ -346,6 +349,7 @@ impl<'a, 'ctx> OriginalServeStack<'a, 'ctx> {
|
||||
expert_cache,
|
||||
loan,
|
||||
residency,
|
||||
refill_pending: false,
|
||||
timing: (0.0, 0.0),
|
||||
executor,
|
||||
draft,
|
||||
@@ -479,6 +483,7 @@ impl<'a, 'ctx> OriginalServeStack<'a, 'ctx> {
|
||||
conversation,
|
||||
expert_cache,
|
||||
residency,
|
||||
refill_pending,
|
||||
..
|
||||
} = self;
|
||||
let binding: &ModelBinding = *binding;
|
||||
@@ -521,6 +526,19 @@ impl<'a, 'ctx> OriginalServeStack<'a, 'ctx> {
|
||||
pcie_mode,
|
||||
);
|
||||
drop(p);
|
||||
// the lent slots' refill copies (queued at the end of the prompt phase) have run behind the assembly: wait for them
|
||||
// before a window or the tier's own bookkeeping touches the cache
|
||||
if std::mem::take(refill_pending) {
|
||||
if let Some(cache) = expert_cache.as_mut() {
|
||||
if let Err(error) = original_serve_residency::settle_refill(cache) {
|
||||
broken.get_or_insert(error);
|
||||
if let Ok(resources) = assembled {
|
||||
let _ = disassemble(resources).map(|owners| executor.adopt_qsa_owners(binding, &*dense, &mut *session, owners));
|
||||
}
|
||||
return Err(SpecError::Forward(error));
|
||||
}
|
||||
}
|
||||
}
|
||||
crate::bringup_trace!("DBG[{:.0}s] decode: assemble -> {}", dbg_el(), if assembled.is_ok() { "ok" } else { "ERR" });
|
||||
let resources = match assembled {
|
||||
Ok(resources) => resources,
|
||||
@@ -689,10 +707,16 @@ impl<'a, 'ctx> OriginalServeStack<'a, 'ctx> {
|
||||
let read = self.prefill_actions(request, resume, cancel);
|
||||
drop(p);
|
||||
let _p = profile::span("prefill.refill");
|
||||
// a failed or cancelled read still refills at once (generate.cpp `refill`); a good one overlaps the copies with the
|
||||
// decode phase's host-side assembly
|
||||
let defer = read.is_ok() && original_serve_residency::refill_defer_enabled();
|
||||
let refilled = match (self.loan, self.expert_cache.as_mut()) {
|
||||
(Some(_), Some(cache)) => original_serve_residency::refill_lent(self.binding, &self.moe, cache, &mut self.residency.spans).map(|_| ()),
|
||||
(Some(_), Some(cache)) => {
|
||||
original_serve_residency::refill_lent(self.binding, &self.moe, cache, &mut self.residency.spans, defer).map(|_| ())
|
||||
}
|
||||
_ => Ok(()),
|
||||
};
|
||||
self.refill_pending = defer && refilled.is_ok() && self.loan.is_some() && self.expert_cache.is_some();
|
||||
match (read, refilled) {
|
||||
(Ok(()), Err(error)) => {
|
||||
self.mark_broken(error);
|
||||
@@ -1015,6 +1039,14 @@ impl<'a, 'ctx> RequestOwners for OriginalServeStack<'a, 'ctx> {
|
||||
) -> std::result::Result<ServeOutcome, SpecError> {
|
||||
let started = Instant::now();
|
||||
let result = self.decode_phase(request, emit, cancel);
|
||||
// an exit before the decode phase reached its wait still leaves the refill copies to be waited
|
||||
if std::mem::take(&mut self.refill_pending) {
|
||||
if let Some(cache) = self.expert_cache.as_mut() {
|
||||
if let Err(error) = original_serve_residency::settle_refill(cache) {
|
||||
self.mark_broken(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.timing.1 = started.elapsed().as_secs_f64() * 1e3;
|
||||
profile::add("decode.phase", self.timing.1);
|
||||
// graphs captured after the window/commit graphs: the MTP round programs of this request's drafts
|
||||
|
||||
Reference in New Issue
Block a user