serve: plan and queue the adaptive swaps while the GPU runs the draft, not before the commit (LELEKA_SERVE_ADAPT_DEFER=off restores the earlier order)

begin_adaptation (choose_swaps over 24,576 counts, up to 96 span lookups and swap queueings, the count decay) ran on the host between the window and the commit/draft launch:
0.6 ms on an adaptation round, 0.15 ms per round on average, during which the GPU sat idle. The host now records the round at begin_adaptation and runs the same work right after
launch_draft (span round.adapt_deferred), at the latest at join_adaptation, always before the next round's apply_pending. The decision inputs (counts, residency) cannot change in between,
so the swaps are the same ones in the same order; only the host time they take moves into the draft's shadow. Not yet measured on the 5090.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-10-29 02:24:02 +01:00
co-authored by Claude Sonnet 5.5
parent 359b4b5a6a
commit bb47b296c0
2 changed files with 910 additions and 879 deletions
@@ -97,6 +97,9 @@ pub(in crate::qwen_forward) struct OriginalServeHost<'a, 'ctx> {
window_origin: Option<TargetStepOrigin>, window_origin: Option<TargetStepOrigin>,
/// A draft launched behind the enqueued commit, completed after the publication (`launch_draft`). /// A draft launched behind the enqueued commit, completed after the publication (`launch_draft`).
launched: Option<OriginalLaunchedMtpDraft>, launched: Option<OriginalLaunchedMtpDraft>,
/// The round whose adaptive swaps are still to be planned and queued (`LELEKA_SERVE_ADAPT_DEFER`, default on): the host does that
/// work while the GPU runs the draft instead of before the commit and the draft are enqueued.
deferred_adaptation: Option<u64>,
} }
/// Everything a host borrows, in one place so the stack can build it in one expression. /// Everything a host borrows, in one place so the stack can build it in one expression.
@@ -166,6 +169,7 @@ impl<'a, 'ctx> OriginalServeHost<'a, 'ctx> {
ledger, ledger,
window_origin: None, window_origin: None,
launched: None, launched: None,
deferred_adaptation: None,
}) })
} }
@@ -243,6 +247,12 @@ impl<'a, 'ctx> OriginalServeHost<'a, 'ctx> {
} }
} }
/// `LELEKA_SERVE_ADAPT_DEFER=off`: plan and queue the adaptive swaps before the commit (the earlier order; the A/B control).
fn adapt_defer() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| !std::env::var("LELEKA_SERVE_ADAPT_DEFER").is_ok_and(|v| v == "off" || v == "0"))
}
impl<'a, 'ctx> OriginalServeHost<'a, 'ctx> { impl<'a, 'ctx> OriginalServeHost<'a, 'ctx> {
/// The MTP actor drafts as many chain steps as it is configured for (`set_max_drafts`); an adaptive round's plan is /// The MTP actor drafts as many chain steps as it is configured for (`set_max_drafts`); an adaptive round's plan is
/// applied here (the engine has already bounded it by the request's cap). Equal values cost one comparison. /// applied here (the engine has already bounded it by the request's cap). Equal values cost one comparison.
@@ -444,10 +454,26 @@ impl<'a, 'ctx> NativeServeHost<'ctx> for OriginalServeHost<'a, 'ctx> {
fn begin_adaptation(&mut self, next_round: u64) -> Result<()> { fn begin_adaptation(&mut self, next_round: u64) -> Result<()> {
// generate.cpp `adapt()` when `(rounds + 1) % adapt_every == 0`, issued before the commit; its copies // generate.cpp `adapt()` when `(rounds + 1) % adapt_every == 0`, issued before the commit; its copies
// run on the tier's own stream beside the commit and the draft (no work for the all-CPU tier). // run on the tier's own stream beside the commit and the draft (no work for the all-CPU tier).
// The planning and the queueing are host work (0.6 ms on an adaptation round) that sat between the window and the commit/draft
// launch; with the deferral they run once the draft is launched (`run_deferred_adaptation`) or, at the latest, at the join,
// always before the next round's `apply_pending`. LELEKA_SERVE_ADAPT_DEFER=off runs them here as before.
if adapt_defer() {
if self.deferred_adaptation.is_some() {
return Err(Error::IdentityMismatch);
}
self.deferred_adaptation = Some(next_round);
return Ok(());
}
self.pool.begin_adaptation(next_round) self.pool.begin_adaptation(next_round)
} }
fn run_deferred_adaptation(&mut self) -> Result<()> {
match self.deferred_adaptation.take() {
Some(round) => self.pool.begin_adaptation(round),
None => Ok(()),
}
}
fn join_adaptation(&mut self, _work: ()) -> Result<()> { fn join_adaptation(&mut self, _work: ()) -> Result<()> {
Ok(()) self.run_deferred_adaptation()
} }
fn stage_draft_history(&mut self, consumed: &[i32], next: i32) -> Result<()> { fn stage_draft_history(&mut self, consumed: &[i32], next: i32) -> Result<()> {
self.executor.set_mtp_draft_history(self.binding, consumed, next) self.executor.set_mtp_draft_history(self.binding, consumed, next)
@@ -43,6 +43,9 @@ pub(in crate::qwen_forward) trait NativeServeHost<'ctx> {
&mut self,verifier:&mut OriginalVerifier<'ctx,R>,position:i64,committed:&[i32], &mut self,verifier:&mut OriginalVerifier<'ctx,R>,position:i64,committed:&[i32],
)->Result<()>; )->Result<()>;
fn begin_adaptation(&mut self,next_round:u64)->Result<Self::Adaptive>; fn begin_adaptation(&mut self,next_round:u64)->Result<Self::Adaptive>;
/// Adaptive work a host deferred at `begin_adaptation`: run it now (the draft is launched, the GPU is busy). A host that does its work
/// at `begin_adaptation` has nothing to run. `join_adaptation` must run whatever is still deferred.
fn run_deferred_adaptation(&mut self)->Result<()> {Ok(())}
fn join_adaptation(&mut self,work:Self::Adaptive)->Result<()>; fn join_adaptation(&mut self,work:Self::Adaptive)->Result<()>;
fn stage_draft_history(&mut self,consumed:&[i32],next:i32)->Result<()>; fn stage_draft_history(&mut self,consumed:&[i32],next:i32)->Result<()>;
/// Literal `OriginalMtpDrafter::draft_from_verifier` over the same ring/snapshot lineage. /// Literal `OriginalMtpDrafter::draft_from_verifier` over the same ring/snapshot lineage.
@@ -253,6 +256,8 @@ where R:OriginalVerifyGraphResources<'ctx>+OriginalNativeHeadGraphResources<'ctx
let launched={let _p=profile::span("draft.launch"); let launched={let _p=profile::span("draft.launch");
self.host.launch_draft(completed,selection,position,accepted_row,max_drafts,min_p,self.verifier.pending_commit_event())}; self.host.launch_draft(completed,selection,position,accepted_row,max_drafts,min_p,self.verifier.pending_commit_event())};
if let Err(e)=launched {self.failed=true;return Err(e);} if let Err(e)=launched {self.failed=true;return Err(e);}
// the GPU is on the draft now: the adaptive planning/queueing the host deferred runs in its shadow
if let Err(e)={let _p=profile::span("round.adapt_deferred");self.host.run_deferred_adaptation()} {self.failed=true;return Err(e);}
self.settle_commit()?; self.settle_commit()?;
let finished={let _p=profile::span("draft.finish");self.host.finish_draft()}; let finished={let _p=profile::span("draft.finish");self.host.finish_draft()};
if finished.is_err() {self.failed=true;} if finished.is_err() {self.failed=true;}