diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/executor_window_numeric.rs b/crates/engine-qwen35-cuda/src/qwen_forward/executor_window_numeric.rs index 6ae90e3a..3f7db81e 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/executor_window_numeric.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/executor_window_numeric.rs @@ -42,9 +42,7 @@ pub(crate) struct WindowProofReport { } impl WindowProofReport { - /// The root-owned wrapper calls this only AFTER closing its actual - /// caches/output/session/context and verifying actual zero allocations. - pub(crate) fn assert_success_and_print(self) { + fn assert_candidate(&self) { assert_eq!(self.logit_words, 248320); assert_eq!(self.residual_words, 10240); assert_eq!(self.logit_differences, 0); @@ -57,6 +55,9 @@ impl WindowProofReport { for (layer, pair) in pairs.iter().enumerate() { assert_eq!(pair, &[(layer as u32, 0, 2), (layer as u32, 2, 4)]); } + } + + fn assert_partials(&self) { assert_eq!(self.partials.len(), 3); for (p, expected) in self.partials.iter().zip([65, 193, 256]) { assert_eq!(p.rows, expected); @@ -68,11 +69,26 @@ impl WindowProofReport { assert!(p.mtp_refused && p.publication_refused); assert!(p.target_unchanged && p.draft_unchanged); } + } + + /// The root-owned wrapper calls this only AFTER closing its actual + /// caches/output/session/context and verifying actual zero allocations. + pub(crate) fn assert_success_and_print(self) { + self.assert_candidate(); + self.assert_partials(); assert_eq!(self.cancel_observation, Some((193, 192))); assert!(self.cancelled && self.cancel_target_unchanged && self.cancel_draft_unchanged); assert!(self.checkpoint_usable && self.healthy); eprintln!( - "window boundary PASS A_rows=4 A_layers=48 micro=2 full_head_words=248320 residual_words=10240 target_draft_continuation_exact=true B_rows=65,193,256 B_layers=1 B_real_finite_words=5263360 B_micro=64 B_incomplete_publish_and_mtp_refused=true C_rows=256 C_cancel_global_row=193 C_completed_rows=192 target_draft_checkpoint_unchanged=true whole256_full48_numeric=false long_context_quality=false speed_claim=false" + "{}", + concat!( + "window boundary PASS A_rows=4 A_layers=48 micro=2 full_head_words=248320 ", + "residual_words=10240 target_draft_continuation_exact=true B_rows=65,193,256 B_layers=1 ", + "B_real_finite_words=5263360 B_micro=64 B_incomplete_publish_and_mtp_refused=true ", + "C_rows=256 C_cancel_global_row=193 C_completed_rows=192 ", + "target_draft_checkpoint_unchanged=true whole256_full48_numeric=false ", + "long_context_quality=false speed_claim=false" + ) ); } } @@ -110,6 +126,10 @@ fn tokens(count: usize, vocab: Dimension) -> Result> { Ok(result) } +#[path = "executor_window_numeric/proof.rs"] +mod proof; +use proof::{CancelReport, CandidateReport, Proof}; + fn run<'driver>( binding: &ModelBinding, session: &mut QwenSession<'driver>, @@ -118,225 +138,37 @@ fn run<'driver>( moe: &mut MoeWeightCache<'driver>, dense: &DenseWeightCache<'_>, ) -> Result { - let vocab = Dimension::new(248320)?; - let bounds = crate::qwen_forward::contract::position_dimension(); - let ids = tokens(4, vocab)?; - let next = TokenId::new(0, vocab)?; - let follow = TokenId::new(1, vocab)?; - if !session.test_reset_replay_snapshot(binding)?.has_prefix(0, [-1, -1]) { - return Err(Error::IdentityMismatch); - } - output.test_reset_replay_record(true)?; - + let mut proof = Proof::open(binding, session, output, kar, moe, dense)?; // A: unchanged actual Native4 target/draft path FIRST. The new candidate // changes only the sealed window size; no retrospective baseline policy. - let mut baseline_draft = MtpDecoder::new(binding)?; - let baseline = QwenForward::prefill_chunk_cached_joint( - binding, - &ids, - None, - session, - &mut baseline_draft, - output, - Some(&mut *kar), - moe, - dense, - 512 * 1024 * 1024, - || false, - )? - .ok_or(Error::IdentityMismatch)?; - let baseline_logits = output.test_reset_replay_take_logits()?; - let baseline_greedy = baseline.predicted(session)?; - let baseline_stream = baseline.streams(binding, ids[3], Position::new(3, bounds)?, session)?; - let mut baseline_bits = Vec::new(); - baseline_bits.try_reserve_exact(baseline_stream.len()).map_err(|_| Error::InputAllocation)?; - baseline_bits.extend(baseline_stream.iter().map(|v| v.to_bits())); - let baseline_target = session.test_reset_replay_snapshot(binding)?; - let baseline_mtp = baseline_draft.test_chunk_snapshot(); - baseline_draft.warm_retained(binding, session, &baseline, next, moe)?; - let continuation = QwenForward::prefill_for_mtp_cached( - binding, - next, - Position::new(4, bounds)?, - session, - output, - Some(&mut *kar), - moe, - dense, - )?; - baseline_draft.warm_prefill(binding, session, &continuation, follow, moe)?; - let continued_target = session.test_reset_replay_snapshot(binding)?; - let continued_mtp = baseline_draft.test_chunk_snapshot(); - drop(continuation); + let baseline = proof.baseline()?; + let candidate = proof.candidate(&baseline)?; drop(baseline); - drop(baseline_draft); - QwenForward::reset(session)?; - - let mut candidate_draft = MtpDecoder::new(binding)?; - let (candidate, full_journal) = { - let mut txn = QwenForward::prepare_prefill_chunk_with_policy_windowed( - binding, - &ids, - None, - session, - output, - Some(&mut *kar), - moe, - dense, - 512 * 1024 * 1024, - || false, - ChunkMoePolicy::ExperimentalRetainedGr, - true, - Some(BoundaryPlan::FullMicro2), - )?; - let journal = owned_journal(&txn)?; - let ready = candidate_draft.stage_prefill_chunk(binding, &mut txn, moe, || false)?; - let source = txn.publish_joint(ready)?.into_target_product().ok_or(Error::IdentityMismatch)?; - (source, journal) - }; - let candidate_logits = output.test_reset_replay_take_logits()?; - let logit_words = candidate_logits.len(); - let logit_differences = if baseline_logits.len() == candidate_logits.len() { - baseline_logits.iter().zip(candidate_logits.iter()).filter(|(a, b)| a != b).count() - } else { - usize::MAX - }; - let actual_stream = candidate.streams(binding, ids[3], Position::new(3, bounds)?, session)?; - let residual_words = actual_stream.len(); - let residual_differences = if baseline_bits.len() == actual_stream.len() { - baseline_bits.iter().zip(actual_stream.iter()).filter(|(a, b)| **a != b.to_bits()).count() - } else { - usize::MAX - }; - let greedy_equal = baseline_greedy == candidate.predicted(session)?; - let target_equal = baseline_target.numeric_eq(&session.test_reset_replay_snapshot(binding)?); - let draft_equal = candidate_draft.test_chunk_eq(&baseline_mtp); - candidate_draft.validate_resume(binding, session, &candidate, &[0, 1, 0, 1])?; - candidate_draft.warm_retained(binding, session, &candidate, next, moe)?; - let continuation = QwenForward::prefill_for_mtp_cached( - binding, - next, - Position::new(4, bounds)?, - session, - output, - Some(&mut *kar), - moe, - dense, - )?; - candidate_draft.warm_prefill(binding, session, &continuation, follow, moe)?; - let continued_target_equal = continued_target.numeric_eq(&session.test_reset_replay_snapshot(binding)?); - let continued_draft_equal = candidate_draft.test_chunk_eq(&continued_mtp); - drop(continuation); - drop(candidate); - drop(candidate_draft); - drop(baseline_logits); - drop(candidate_logits); - drop(baseline_target); - drop(baseline_mtp); - drop(continued_target); - drop(continued_mtp); - // B: genuine65/193/256 row inputs and real first-layer outputs, not a // metadata-only fabricated completion. No finish/head/MTP publication. - let mut partials = Vec::new(); - partials.try_reserve_exact(3).map_err(|_| Error::InputAllocation)?; - for count in [65, 193, 256] { - QwenForward::reset(session)?; - let fresh = session.test_reset_replay_snapshot(binding)?; - let actual_ids = tokens(count, vocab)?; - let mut draft = MtpDecoder::new(binding)?; - let original_draft = draft.test_chunk_snapshot(); - let (finite_words, journal, publication_refused, mtp_refused) = { - let mut txn = QwenForward::prepare_prefill_chunk_with_policy_windowed( - binding, - &actual_ids, - Some(next), - session, - output, - Some(&mut *kar), - moe, - dense, - 512 * 1024 * 1024, - || false, - ChunkMoePolicy::ExperimentalRetainedGr, - true, - Some(BoundaryPlan::FirstLayer), - )?; - let finite_words = txn.test_partial_words(); - let journal = owned_journal(&txn)?; - let publication_refused = txn.test_publication_preflight_refused(); - let mtp_refused = - matches!(draft.stage_prefill_chunk(binding, &mut txn, moe, || false), Err(Error::IdentityMismatch)); - (finite_words, journal, publication_refused, mtp_refused) - }; - partials.push(PartialReport { - rows: count, - finite_words, - journal, - publication_refused, - mtp_refused, - target_unchanged: fresh.numeric_eq(&session.test_reset_replay_snapshot(binding)?), - draft_unchanged: draft.test_chunk_eq(&original_draft), - }); - } + let partials = proof.partials()?; + let cancel = proof.cancellation()?; + Ok(window_report(candidate, partials, cancel)) +} - // C: exactly256 real rows, cancellation when the actual fourth-window - // attention producer reaches globalposition193. Original committed state - // and a genuine checkpoint remain untouched; no fake draft branch warm. - QwenForward::reset(session)?; - let checkpoint = session.checkpoint()?; - let fresh = session.test_reset_replay_snapshot(binding)?; - let draft = MtpDecoder::new(binding)?; - let original_draft = draft.test_chunk_snapshot(); - let actual_ids = tokens(256, vocab)?; - let observation = Rc::new(Cell::new(None)); - let cancellation = Rc::new(Cell::new(false)); - let cancelled = matches!( - QwenForward::prepare_prefill_chunk_with_policy_windowed( - binding, - &actual_ids, - Some(next), - session, - output, - Some(&mut *kar), - moe, - dense, - 512 * 1024 * 1024, - || cancellation.get(), - ChunkMoePolicy::ExperimentalRetainedGr, - true, - Some(BoundaryPlan::CancelFirstLayerAt193 { - observed: Rc::clone(&observation), - cancelled: Rc::clone(&cancellation), - }), - ), - Err(Error::Unsupported(Stage::Executor)) - ); - let cancel_target_unchanged = fresh.numeric_eq(&session.test_reset_replay_snapshot(binding)?); - let cancel_draft_unchanged = draft.test_chunk_eq(&original_draft); - session.restore_checkpoint(&checkpoint)?; - let checkpoint_usable = fresh.numeric_eq(&session.test_reset_replay_snapshot(binding)?); - let context = session.mtp_context()?; - moe.validate(binding, context)?; - dense.validate(binding)?; - let healthy = !context.is_poisoned(); - Ok(WindowProofReport { - logit_words, - logit_differences, - residual_words, - residual_differences, - greedy_equal, - target_equal, - draft_equal, - continued_target_equal, - continued_draft_equal, - full_journal, +fn window_report(candidate: CandidateReport, partials: Vec, cancel: CancelReport) -> WindowProofReport { + WindowProofReport { + logit_words: candidate.logit_words, + logit_differences: candidate.logit_differences, + residual_words: candidate.residual_words, + residual_differences: candidate.residual_differences, + greedy_equal: candidate.greedy_equal, + target_equal: candidate.target_equal, + draft_equal: candidate.draft_equal, + continued_target_equal: candidate.continued_target_equal, + continued_draft_equal: candidate.continued_draft_equal, + full_journal: candidate.full_journal, partials, - cancel_observation: observation.get(), - cancelled, - cancel_target_unchanged, - cancel_draft_unchanged, - checkpoint_usable, - healthy, - }) + cancel_observation: cancel.observation, + cancelled: cancel.cancelled, + cancel_target_unchanged: cancel.target_unchanged, + cancel_draft_unchanged: cancel.draft_unchanged, + checkpoint_usable: cancel.checkpoint_usable, + healthy: cancel.healthy, + } } diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/executor_window_numeric/proof.rs b/crates/engine-qwen35-cuda/src/qwen_forward/executor_window_numeric/proof.rs new file mode 100644 index 00000000..408cd2be --- /dev/null +++ b/crates/engine-qwen35-cuda/src/qwen_forward/executor_window_numeric/proof.rs @@ -0,0 +1,332 @@ +//! The proof phases of the window boundary observer (STRA-338 size: extract-method only). + +#[allow(unused_imports)] +use super::*; +use crate::qwen_forward::mtp::MtpChunkNumericSnapshot; +use crate::qwen_forward::session::ResetReplaySnapshot; + +const HOST_BUDGET: usize = 512 * 1024 * 1024; + +type Journal = Vec<(u32, usize, usize)>; + +/// The unchanged Native4 target/draft path, measured first. +pub(super) struct Baseline { + logits: Box<[u32]>, + greedy: TokenId, + bits: Vec, + target: ResetReplaySnapshot, + mtp: MtpChunkNumericSnapshot, + continued_target: ResetReplaySnapshot, + continued_mtp: MtpChunkNumericSnapshot, +} + +/// The windowed candidate compared against the baseline. +pub(super) struct CandidateReport { + pub(super) logit_words: usize, + pub(super) logit_differences: usize, + pub(super) residual_words: usize, + pub(super) residual_differences: usize, + pub(super) greedy_equal: bool, + pub(super) target_equal: bool, + pub(super) draft_equal: bool, + pub(super) continued_target_equal: bool, + pub(super) continued_draft_equal: bool, + pub(super) full_journal: Journal, +} + +/// The cancellation phase of the proof. +pub(super) struct CancelReport { + pub(super) observation: Option<(usize, usize)>, + pub(super) cancelled: bool, + pub(super) target_unchanged: bool, + pub(super) draft_unchanged: bool, + pub(super) checkpoint_usable: bool, + pub(super) healthy: bool, +} + +/// What the first-layer partial window observed before its publication was refused. +struct FirstLayer { + finite_words: usize, + journal: Journal, + publication_refused: bool, + mtp_refused: bool, +} + +fn count_differences(a: &[A], b: &[B], same: impl Fn(&A, &B) -> bool) -> usize { + if a.len() == b.len() { a.iter().zip(b).filter(|(x, y)| !same(x, y)).count() } else { usize::MAX } +} + +/// The windowed prefill transaction under the experimental retained-GR policy. The transaction borrows only the +/// session; every other owner is reborrowed for this call alone. +#[allow(clippy::too_many_arguments)] +fn prepare_txn<'s, 'driver>( + binding: &ModelBinding, + session: &'s mut QwenSession<'driver>, + output: &OutputStage<'_, '_>, + kar: &mut KarPleCache<'_>, + moe: &mut MoeWeightCache<'driver>, + dense: &DenseWeightCache<'_>, + ids: &[TokenId], + known_next: Option, + boundary: BoundaryPlan, + cancelled: impl FnMut() -> bool, +) -> Result> { + let policy = ChunkMoePolicy::ExperimentalRetainedGr; + QwenForward::prepare_prefill_chunk_with_policy_windowed( + binding, + ids, + known_next, + session, + output, + Some(kar), + moe, + dense, + HOST_BUDGET, + cancelled, + policy, + true, + Some(boundary), + ) +} + +/// The loaded owners and fixed tokens every proof phase works on. +pub(super) struct Proof<'a, 'driver, 'k, 'o, 'p, 'd> { + pub(super) binding: &'a ModelBinding, + pub(super) session: &'a mut QwenSession<'driver>, + pub(super) output: &'a OutputStage<'o, 'p>, + pub(super) kar: &'a mut KarPleCache<'k>, + pub(super) moe: &'a mut MoeWeightCache<'driver>, + pub(super) dense: &'a DenseWeightCache<'d>, + pub(super) vocab: Dimension, + pub(super) bounds: Dimension, + pub(super) ids: Vec, + pub(super) next: TokenId, + pub(super) follow: TokenId, +} + +impl<'a, 'driver, 'k, 'o, 'p, 'd> Proof<'a, 'driver, 'k, 'o, 'p, 'd> { + /// The fixed tokens and the fresh-session check, then the output replay recorder on. + pub(super) fn open( + binding: &'a ModelBinding, + session: &'a mut QwenSession<'driver>, + output: &'a OutputStage<'o, 'p>, + kar: &'a mut KarPleCache<'k>, + moe: &'a mut MoeWeightCache<'driver>, + dense: &'a DenseWeightCache<'d>, + ) -> Result { + let vocab = Dimension::new(248320)?; + let bounds = crate::qwen_forward::contract::position_dimension(); + let ids = tokens(4, vocab)?; + let (next, follow) = (TokenId::new(0, vocab)?, TokenId::new(1, vocab)?); + if !session.test_reset_replay_snapshot(binding)?.has_prefix(0, [-1, -1]) { + return Err(Error::IdentityMismatch); + } + output.test_reset_replay_record(true)?; + Ok(Self { binding, session, output, kar, moe, dense, vocab, bounds, ids, next, follow }) + } +} + +impl<'driver> Proof<'_, 'driver, '_, '_, '_, '_> { + fn snapshot(&self) -> Result { + self.session.test_reset_replay_snapshot(self.binding) + } + + fn snapshots(&self, draft: &MtpDecoder) -> Result<(ResetReplaySnapshot, MtpChunkNumericSnapshot)> { + Ok((self.snapshot()?, draft.test_chunk_snapshot())) + } + + /// A: unchanged actual Native4 target/draft path FIRST. The new candidate changes only the sealed window + /// size; no retrospective baseline policy. + pub(super) fn baseline(&mut self) -> Result { + let mut draft = MtpDecoder::new(self.binding)?; + let product = self.baseline_prefill(&mut draft)?; + let (logits, greedy, bits) = self.baseline_outputs(&product)?; + let (target, mtp) = self.snapshots(&draft)?; + draft.warm_retained(self.binding, self.session, &product, self.next, self.moe)?; + self.continuation(&mut draft)?; + let (continued_target, continued_mtp) = self.snapshots(&draft)?; + drop(product); + drop(draft); + QwenForward::reset(self.session)?; + Ok(Baseline { logits, greedy, bits, target, mtp, continued_target, continued_mtp }) + } + + fn baseline_prefill(&mut self, draft: &mut MtpDecoder) -> Result { + QwenForward::prefill_chunk_cached_joint( + self.binding, + &self.ids, + None, + self.session, + draft, + self.output, + Some(&mut *self.kar), + self.moe, + self.dense, + HOST_BUDGET, + || false, + )? + .ok_or(Error::IdentityMismatch) + } + + fn baseline_outputs(&self, product: &TargetStepProduct) -> Result<(Box<[u32]>, TokenId, Vec)> { + let logits = self.output.test_reset_replay_take_logits()?; + let greedy = product.predicted(self.session)?; + let stream = product.streams(self.binding, self.ids[3], Position::new(3, self.bounds)?, self.session)?; + let mut bits = Vec::new(); + bits.try_reserve_exact(stream.len()).map_err(|_| Error::InputAllocation)?; + bits.extend(stream.iter().map(|v| v.to_bits())); + Ok((logits, greedy, bits)) + } + + /// The continuation row after the window: one more actual prefill step, then the draft warm. + fn continuation(&mut self, draft: &mut MtpDecoder) -> Result<()> { + let position = Position::new(4, self.bounds)?; + let step = QwenForward::prefill_for_mtp_cached( + self.binding, + self.next, + position, + self.session, + self.output, + Some(&mut *self.kar), + self.moe, + self.dense, + )?; + draft.warm_prefill(self.binding, self.session, &step, self.follow, self.moe)?; + Ok(()) + } + + pub(super) fn candidate(&mut self, base: &Baseline) -> Result { + let mut draft = MtpDecoder::new(self.binding)?; + let (product, full_journal) = self.stage_full_micro(&mut draft)?; + let logits = self.output.test_reset_replay_take_logits()?; + let position = Position::new(3, self.bounds)?; + let stream = product.streams(self.binding, self.ids[3], position, self.session)?; + let logit_differences = count_differences(&base.logits, &logits, |a, b| a == b); + let residual_differences = count_differences(&base.bits, &stream, |a, b| *a == b.to_bits()); + let greedy_equal = base.greedy == product.predicted(self.session)?; + let target_equal = base.target.numeric_eq(&self.snapshot()?); + let draft_equal = draft.test_chunk_eq(&base.mtp); + let (continued_target_equal, continued_draft_equal) = + self.candidate_continuation(base, &mut draft, &product)?; + Ok(CandidateReport { + logit_words: logits.len(), + logit_differences, + residual_words: stream.len(), + residual_differences, + greedy_equal, + target_equal, + draft_equal, + continued_target_equal, + continued_draft_equal, + full_journal, + }) + } + + fn candidate_continuation( + &mut self, + base: &Baseline, + draft: &mut MtpDecoder, + product: &TargetStepProduct, + ) -> Result<(bool, bool)> { + draft.validate_resume(self.binding, self.session, product, &[0, 1, 0, 1])?; + draft.warm_retained(self.binding, self.session, product, self.next, self.moe)?; + self.continuation(draft)?; + let target_equal = base.continued_target.numeric_eq(&self.snapshot()?); + Ok((target_equal, draft.test_chunk_eq(&base.continued_mtp))) + } + + fn stage_full_micro(&mut self, draft: &mut MtpDecoder) -> Result<(TargetStepProduct, Journal)> { + let (binding, output, dense) = (self.binding, self.output, self.dense); + let plan = BoundaryPlan::FullMicro2; + let mut txn = + prepare_txn(binding, self.session, output, self.kar, self.moe, dense, &self.ids, None, plan, || false)?; + let journal = owned_journal(&txn)?; + let ready = draft.stage_prefill_chunk(binding, &mut txn, self.moe, || false)?; + let source = txn.publish_joint(ready)?.into_target_product().ok_or(Error::IdentityMismatch)?; + Ok((source, journal)) + } + + pub(super) fn partials(&mut self) -> Result> { + let mut partials = Vec::new(); + partials.try_reserve_exact(3).map_err(|_| Error::InputAllocation)?; + for count in [65, 193, 256] { + partials.push(self.partial(count)?); + } + Ok(partials) + } + + fn partial(&mut self, count: usize) -> Result { + QwenForward::reset(self.session)?; + let fresh = self.snapshot()?; + let actual_ids = tokens(count, self.vocab)?; + let mut draft = MtpDecoder::new(self.binding)?; + let original_draft = draft.test_chunk_snapshot(); + let staged = self.stage_first_layer(&actual_ids, &mut draft)?; + let target_unchanged = fresh.numeric_eq(&self.snapshot()?); + let draft_unchanged = draft.test_chunk_eq(&original_draft); + let FirstLayer { finite_words, journal, publication_refused, mtp_refused } = staged; + Ok(PartialReport { + rows: count, + finite_words, + journal, + publication_refused, + mtp_refused, + target_unchanged, + draft_unchanged, + }) + } + + fn stage_first_layer(&mut self, ids: &[TokenId], draft: &mut MtpDecoder) -> Result { + let (binding, output, dense, next) = (self.binding, self.output, self.dense, Some(self.next)); + let plan = BoundaryPlan::FirstLayer; + let mut txn = prepare_txn(binding, self.session, output, self.kar, self.moe, dense, ids, next, plan, || false)?; + let finite_words = txn.test_partial_words(); + let journal = owned_journal(&txn)?; + let publication_refused = txn.test_publication_preflight_refused(); + let staged = draft.stage_prefill_chunk(binding, &mut txn, self.moe, || false); + let mtp_refused = matches!(staged, Err(Error::IdentityMismatch)); + Ok(FirstLayer { finite_words, journal, publication_refused, mtp_refused }) + } + + /// C: exactly256 real rows, cancellation when the actual fourth-window + /// attention producer reaches globalposition193. Original committed state + /// and a genuine checkpoint remain untouched; no fake draft branch warm. + pub(super) fn cancellation(&mut self) -> Result { + QwenForward::reset(self.session)?; + let checkpoint = self.session.checkpoint()?; + let fresh = self.snapshot()?; + let draft = MtpDecoder::new(self.binding)?; + let original_draft = draft.test_chunk_snapshot(); + let observation = Rc::new(Cell::new(None)); + let cancelled = self.cancelled_prepare(&observation)?; + let target_unchanged = fresh.numeric_eq(&self.snapshot()?); + let draft_unchanged = draft.test_chunk_eq(&original_draft); + self.session.restore_checkpoint(&checkpoint)?; + let checkpoint_usable = fresh.numeric_eq(&self.snapshot()?); + let healthy = self.healthy()?; + let observation = observation.get(); + Ok(CancelReport { observation, cancelled, target_unchanged, draft_unchanged, checkpoint_usable, healthy }) + } + + /// The caches still validate against the MTP context, which must not be poisoned. + fn healthy(&mut self) -> Result { + let context = self.session.mtp_context()?; + self.moe.validate(self.binding, context)?; + self.dense.validate(self.binding)?; + Ok(!context.is_poisoned()) + } + + fn cancelled_prepare(&mut self, observation: &Rc>>) -> Result { + let actual_ids = tokens(256, self.vocab)?; + let cancellation = Rc::new(Cell::new(false)); + let plan = BoundaryPlan::CancelFirstLayerAt193 { + observed: Rc::clone(observation), + cancelled: Rc::clone(&cancellation), + }; + let (binding, output, dense, next) = (self.binding, self.output, self.dense, Some(self.next)); + let (session, kar, moe) = (&mut *self.session, &mut *self.kar, &mut *self.moe); + let prepared = + prepare_txn(binding, session, output, kar, moe, dense, &actual_ids, next, plan, || cancellation.get()); + Ok(matches!(prepared, Err(Error::Unsupported(Stage::Executor)))) + } +}