From cbf9f166ef92f0ecf38c6de34c4e8e3b0218428c Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Fri, 6 Nov 2026 15:42:56 +0100 Subject: [PATCH] STRA-338 size: engine verify_commit within limits (commit_parts) --- .../src/backend_original_serve/runner.rs | 2 +- .../runner/command_loop.rs | 10 ++ .../backend_original_serve/runner/run_fn.rs | 8 +- .../impl_originalspeccontroller.rs | 1 + .../commit_parts.rs | 113 ++++++++++++++++++ .../impl_originalspeccontroller_2_2.rs | 73 +---------- 6 files changed, 131 insertions(+), 76 deletions(-) create mode 100644 crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/impl_originalspeccontroller/commit_parts.rs diff --git a/crates/engine-qwen35-cuda/src/backend_original_serve/runner.rs b/crates/engine-qwen35-cuda/src/backend_original_serve/runner.rs index 3023e4e8..df6bf6c1 100644 --- a/crates/engine-qwen35-cuda/src/backend_original_serve/runner.rs +++ b/crates/engine-qwen35-cuda/src/backend_original_serve/runner.rs @@ -124,7 +124,7 @@ use run_parts::{ #[path = "runner/command_loop.rs"] mod command_loop; #[allow(unused_imports)] -use command_loop::{LoopCtx, announce, command_loop}; +use command_loop::{LoopCtx, announce, answer_unload, command_loop}; #[path = "runner/reconfigure_parts.rs"] mod reconfigure_parts; #[allow(unused_imports)] diff --git a/crates/engine-qwen35-cuda/src/backend_original_serve/runner/command_loop.rs b/crates/engine-qwen35-cuda/src/backend_original_serve/runner/command_loop.rs index c1798900..ca83fabd 100644 --- a/crates/engine-qwen35-cuda/src/backend_original_serve/runner/command_loop.rs +++ b/crates/engine-qwen35-cuda/src/backend_original_serve/runner/command_loop.rs @@ -89,3 +89,13 @@ fn handle_command<'a, 'ctx>( } Ok(None) } + +/// A failed close stops the engine; the unload (if that ended the loop) is answered with the close's result. +pub(super) fn answer_unload(shared: &Shared, closed: Result<(), BackendError>, unload_reply: Option>) { + if closed.is_err() { + shared.shutdown.store(true, Ordering::Release); + } + if let Some(reply) = unload_reply { + let _ = reply.send(closed); + } +} diff --git a/crates/engine-qwen35-cuda/src/backend_original_serve/runner/run_fn.rs b/crates/engine-qwen35-cuda/src/backend_original_serve/runner/run_fn.rs index 022acb1f..5a0f946f 100644 --- a/crates/engine-qwen35-cuda/src/backend_original_serve/runner/run_fn.rs +++ b/crates/engine-qwen35-cuda/src/backend_original_serve/runner/run_fn.rs @@ -32,12 +32,6 @@ pub(in super::super::super) fn run(args: Run<'_, '_>) -> Result<(), BackendError let stack_closed = close_stack(stack.take()); // the stack's borrow of the context ends with it drop(stack); - let closed = close_context(context)?.and(stack_closed); - if closed.is_err() { - shared.shutdown.store(true, Ordering::Release); - } - if let Some(reply) = unload_reply { - let _ = reply.send(closed); - } + answer_unload(shared, close_context(context)?.and(stack_closed), unload_reply); Ok(()) } diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/impl_originalspeccontroller.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/impl_originalspeccontroller.rs index 0961a473..921080c6 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/impl_originalspeccontroller.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/impl_originalspeccontroller.rs @@ -3,6 +3,7 @@ #[allow(unused_imports)] use super::*; +mod commit_parts; mod verify_parts; use verify_parts::{Round, window_inputs}; mod impl_originalspeccontroller_2; diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/impl_originalspeccontroller/commit_parts.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/impl_originalspeccontroller/commit_parts.rs new file mode 100644 index 00000000..768d8aaf --- /dev/null +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/impl_originalspeccontroller/commit_parts.rs @@ -0,0 +1,113 @@ +//! The commit half of `OriginalSpecController::verify_commit`, moved out of it by STRA-338 (size): the round's verified +//! window is accepted, its prefix committed and the receipt handed back, in the order `verify_commit` did it. + +#[allow(unused_imports)] +use super::*; +use super::verify_parts::{Round, WindowPlan}; + +/// The verified window of a round with the plan it ran under and the number of the round it completes. +pub(super) type Judged<'p, V> = (V, &'p WindowPlan, u64); + +/// The window's outputs and how many of its rows the outputs confirm. +fn accepted_prefix( + engine: &E, + round: &Round<'_, E>, + verified: &E::Verified, +) -> ([i32; MAX_T], usize) { + let mut outputs = [0; MAX_T]; + outputs[..round.t].copy_from_slice(engine.outputs(verified)); + let mut accepted = 0; + while accepted < round.t - 1 && round.inputs[accepted + 1] == outputs[accepted] { + accepted += 1; + } + (outputs, accepted) +} + +fn note_round_profile(t: usize, accepted: usize, lookup: bool) { + super::super::super::original_serve_profile::add("round.T", t as f64); + super::super::super::original_serve_profile::add("round.acc", accepted as f64); + if lookup { + super::super::super::original_serve_profile::add("round.lookupT", t as f64); + } +} + +impl OriginalSpecController { + /// The round may start: the controller is healthy, the request unfinished and not owing a draft, nothing cancelled, + /// and the engine stands where the request does. `false`: the request is finished. + pub(super) fn round_ready( + &mut self, + engine: &mut E, + cancel: &mut impl FnMut() -> bool, + ) -> SpecResult { + if self.failed { + return Err(Error::IdentityMismatch.into()); + } + let r = self.request.as_ref().ok_or(Error::IdentityMismatch)?; + if r.finished { + return Ok(false); + } + if r.pending_draft { + return Err(Error::IdentityMismatch.into()); + } + if cancel() { + return Err(SpecError::Cancelled); + } + engine.validate_current(&r.consumed, r.input, r.position)?; + Ok(true) + } + + /// Commit the accepted prefix of the verified window and hand back the receipt. Every failure drains the window, + /// joins the adaptation and restores the checkpoints through `abort`. + pub(super) fn commit_round( + &mut self, + engine: &mut E, + round: &Round<'_, E>, + judged: Judged<'_, E::Verified>, + cancel: &mut impl FnMut() -> bool, + ) -> SpecResult>> { + let (verified, plan, next_round) = judged; + if cancel() { + let _ = engine.drain_verified(verified); + return Err(self.abort(engine, round.target, round.draft, SpecError::Cancelled)); + } + let (outputs, accepted) = accepted_prefix(engine, round, &verified); + let next = round.p.checked_add(accepted as i64 + 1).ok_or(Error::Overflow)?; + note_round_profile(round.t, accepted, plan.lookup); + let (verified, work) = self.commit_prefix(engine, round, verified, (accepted, next_round))?; + // Actual prefix commit is now authoritative. Adaptation remains beside draft via a receipt owned by the engine; + // finish_round joins it. No borrowed stack worker or unobserved async boolean cert is retained. + let books = self.book_round((round.inputs, &outputs), accepted, next, next_round)?; + // Store the actual pending adaptive owner with the committed receipt. + #[rustfmt::skip] + let receipt = CommittedRound { verified, outputs, emitted: books.emitted, t: round.t, accepted, position: round.p, + from_lookup: plan.lookup, matched: plan.matched, timed: books.timed, eos: books.eos, choice: plan.choice, + next_position: next, round_number: next_round, work: Some(work), owner: Rc::clone(&self.owner), + bookkeeping_error: books.bookkeeping_error }; + Ok(Some(receipt)) + } + + /// Start the adaptation beside the commit, then commit the first `accepted + 1` rows. + fn commit_prefix( + &mut self, + engine: &mut E, + round: &Round<'_, E>, + verified: E::Verified, + progress: (usize, u64), + ) -> SpecResult<(E::Verified, E::AdaptiveWork)> { + let (accepted, next_round) = progress; + let work = match engine.begin_adaptation(next_round) { + Ok(v) => v, + Err(e) => { + let _ = engine.drain_verified(verified); + return Err(self.abort(engine, round.target, round.draft, e.into())); + } + }; + let mut verified = verified; + if let Err(e) = engine.commit_verified(&mut verified, accepted + 1) { + let _ = engine.join_adaptation(work); + let _ = engine.drain_verified(verified); + return Err(self.abort(engine, round.target, round.draft, e.into())); + } + Ok((verified, work)) + } +} diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/impl_originalspeccontroller/impl_originalspeccontroller_2_2.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/impl_originalspeccontroller/impl_originalspeccontroller_2_2.rs index a518c543..c6b66cc3 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/impl_originalspeccontroller/impl_originalspeccontroller_2_2.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/impl_originalspeccontroller/impl_originalspeccontroller_2_2.rs @@ -9,83 +9,20 @@ impl OriginalSpecController { engine: &mut E, mut cancel: impl FnMut() -> bool, ) -> SpecResult>> { - if self.failed { - return Err(Error::IdentityMismatch.into()); - } - let r = self.request.as_ref().ok_or(Error::IdentityMismatch)?; - if r.finished { + if !self.round_ready(engine, &mut cancel)? { return Ok(None); } - if r.pending_draft { - return Err(Error::IdentityMismatch.into()); - } - if cancel() { - return Err(SpecError::Cancelled); - } - engine.validate_current(&r.consumed, r.input, r.position)?; let mut plan = self.plan_window()?; if !self.fit_window(&mut plan)? { return Ok(None); } let r = self.request.as_ref().ok_or(Error::IdentityMismatch)?; - let p = r.position; - let inputs = window_inputs(r, &plan); - let t = plan.t; + let (p, inputs) = (r.position, window_inputs(r, &plan)); let next_round = self.rounds.checked_add(1).ok_or(Error::Overflow)?; let target = engine.checkpoint_target()?; let draft = engine.checkpoint_draft()?; - let round = Round:: { p, t, inputs: &inputs, target: &target, draft: &draft }; - let mut verified = self.verify_window(engine, &round, &mut cancel)?; - if cancel() { - let _ = engine.drain_verified(verified); - return Err(self.abort(engine, &target, &draft, SpecError::Cancelled)); - } - let mut outputs = [0; MAX_T]; - outputs[..t].copy_from_slice(engine.outputs(&verified)); - let mut accepted = 0; - while accepted < t - 1 && inputs[accepted + 1] == outputs[accepted] { - accepted += 1; - } - let next = p.checked_add(accepted as i64 + 1).ok_or(Error::Overflow)?; - super::super::super::original_serve_profile::add("round.T", t as f64); - super::super::super::original_serve_profile::add("round.acc", accepted as f64); - if plan.lookup { - super::super::super::original_serve_profile::add("round.lookupT", t as f64); - } - let work = match engine.begin_adaptation(next_round) { - Ok(v) => v, - Err(e) => { - let _ = engine.drain_verified(verified); - return Err(self.abort(engine, &target, &draft, e.into())); - } - }; - if let Err(e) = engine.commit_verified(&mut verified, accepted + 1) { - let _ = engine.join_adaptation(work); - let _ = engine.drain_verified(verified); - return Err(self.abort(engine, &target, &draft, e.into())); - } - // Actual prefix commit is now authoritative. Adaptation remains beside - // draft via a receipt owned by the engine; finish_round joins it. No - // borrowed stack worker or unobserved async boolean cert is retained. - let books = self.book_round((&inputs, &outputs), accepted, next, next_round)?; - // Store the actual pending adaptive owner with the committed receipt. - Ok(Some(CommittedRound { - verified, - outputs, - emitted: books.emitted, - t, - accepted, - position: p, - from_lookup: plan.lookup, - matched: plan.matched, - timed: books.timed, - eos: books.eos, - choice: plan.choice, - next_position: next, - round_number: next_round, - work: Some(work), - owner: Rc::clone(&self.owner), - bookkeeping_error: books.bookkeeping_error, - })) + let round = Round:: { p, t: plan.t, inputs: &inputs, target: &target, draft: &draft }; + let verified = self.verify_window(engine, &round, &mut cancel)?; + self.commit_round(engine, &round, (verified, &plan, next_round), &mut cancel) } }