From 3cf185a34aa928e88d9683dc3dc03c939f6e539a Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Thu, 29 Oct 2026 10:20:17 +0100 Subject: [PATCH] serve: the MTP draft stream no longer waits for the verifier's commit event (upstream's order); LELEKA_SERVE_DRAFT_AFTER_COMMIT=on restores the wait Upstream enqueues the commit graph on the verifier's stream and launches the draft round graph on the drafter's own stream with no dependency between them (core/verify.cpp set_commit_async: "the drafter (its own stream) reads only this window's final rows and its own K/V"). The port made the draft stream wait the commit event on the device, so the commit graph (36 GDN layers, 12 QSA appends, the PLE history; the host sees ~0.36 ms from enqueue to event) ran in front of every draft's first graph. With the 5090 spans of speedtrain-ab (draft_committed 1.66 ms = launch 0.03 + deferred adapt 0.08 + commit.wait 0.25 + publish 0.12 + finish 1.18) the commit is on the critical path of every round; running it beside the draft is worth up to its length per round (estimate 0.2-0.35 ms of a 15.0 ms round, not measured). Output identity argument: the draft reads the completed window's final residual rows (verifier arena span "R"), the head ids staged in its own mapped block, its own MTP ring and weights; it writes its own scratch, ring and mapped outputs. The commit graph reads the window intermediates (qkv_L, h_L, gate_L, beta_L, z), writes the canonical GDN state, the QSA index images, the PLE history and the "commit" span (verify_gdn_conv_commit_kernel, verify_gdn_step_norm_multi_kernel, verify_qsa_append_*, verify_copy_*). No span is written by one graph and read or written by the other; neither module declares a device global. Host-side publication already ran beside the draft's first graph, and the next window still starts only after both the commit event and the draft read-back. The decoded ids cannot depend on which of two independent graphs ends first. Not run on a GPU. Gate: replay logprob identity (rt-speedtrain against rt-draft-tail) and the paired A/B of scripts/e2e/decode/drafttail-ab.sh. Co-Authored-By: Claude Sonnet 5.5 --- .../src/qwen_forward/original_serve_native.rs | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) 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"))); + } +}