diff --git a/crates/leleka-runtime/src/qwen_forward/original_serve_native.rs b/crates/leleka-runtime/src/qwen_forward/original_serve_native.rs index f9c144df..5d1cf878 100644 --- a/crates/leleka-runtime/src/qwen_forward/original_serve_native.rs +++ b/crates/leleka-runtime/src/qwen_forward/original_serve_native.rs @@ -118,12 +118,27 @@ fn trace_residual()->Option<&'static std::ffi::OsString> { } /// `LELEKA_SERVE_OVERLAP=off`: settle the commit (wait + publish) before the draft launches (the earlier, serialized -/// order; A/B control). Default: generate.cpp's order, the draft enqueued behind the commit event. +/// order; A/B control). Default: the draft is enqueued while the commit runs (`draft_waits_for_commit` says whether it also waits for +/// the commit event on the device), and the host publishes the session while the draft's first graph runs. fn overlap()->bool { static ON:std::sync::OnceLock=std::sync::OnceLock::new(); *ON.get_or_init(||!std::env::var("LELEKA_SERVE_OVERLAP").is_ok_and(|v|v=="off"||v=="0")) } +/// Whether the draft stream waits for the verifier's commit event on the device before its first command. +/// Upstream's serve loop does not: `Verifier::commit` (core/verify.cpp, `set_commit_async`) enqueues the commit graph on the verifier's +/// stream and returns, and `MtpDrafter::draft` launches the round graph on the drafter's own stream with no dependency on it ("the drafter +/// (its own stream) reads only this window's final rows and its own K/V"), so the two graphs run side by side. The earlier port enqueued +/// the draft behind the commit event, which put the commit graph (~0.36 ms: 36 GDN layers, 12 QSA appends, the PLE history) in front of +/// every draft on the critical path. `LELEKA_SERVE_DRAFT_AFTER_COMMIT=on` restores that order (A/B control). +fn draft_waits_for_commit()->bool { + static ON:std::sync::OnceLock=std::sync::OnceLock::new(); + *ON.get_or_init(||draft_waits_for_commit_from(std::env::var("LELEKA_SERVE_DRAFT_AFTER_COMMIT").ok().as_deref())) +} +fn draft_waits_for_commit_from(value:Option<&str>)->bool { + value.is_some_and(|v|v=="on"||v=="1") +} + impl<'ctx,R,H> OriginalServePort for NativeServePort<'ctx,R,H> where R:OriginalVerifyGraphResources<'ctx>+OriginalNativeHeadGraphResources<'ctx>,H:NativeServeHost<'ctx> { type Window=NativeWindow; @@ -254,7 +269,8 @@ where R:OriginalVerifyGraphResources<'ctx>+OriginalNativeHeadGraphResources<'ctx let selection=window.selection.borrow_mut().take().ok_or(Error::IdentityMismatch); let (completed,selection)=match (completed,selection) {(Ok(c),Ok(s))=>(c,s),(Err(e),_)|(_,Err(e))=>return Err(e)}; 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())}; + let after=if draft_waits_for_commit() {self.verifier.pending_commit_event()} else {None}; + self.host.launch_draft(completed,selection,position,accepted_row,max_drafts,min_p,after)}; 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);} @@ -281,3 +297,21 @@ where R:OriginalVerifyGraphResources<'ctx>+OriginalNativeHeadGraphResources<'ctx } fn drain_first(&mut self)->Result<()> {self.host.drain_first()} } + +#[cfg(test)] +mod tests { + use super::draft_waits_for_commit_from; + + #[test] + fn the_draft_does_not_wait_for_the_commit_unless_asked() { + // default (variable unset), empty and unknown values: upstream's order, draft and commit side by side + assert!(!draft_waits_for_commit_from(None)); + assert!(!draft_waits_for_commit_from(Some(""))); + assert!(!draft_waits_for_commit_from(Some("off"))); + assert!(!draft_waits_for_commit_from(Some("0"))); + assert!(!draft_waits_for_commit_from(Some("yes please"))); + // the A/B control restores the earlier order + assert!(draft_waits_for_commit_from(Some("on"))); + assert!(draft_waits_for_commit_from(Some("1"))); + } +}