diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/executor_original_prompt/impl_drop_for_originalpromptexecutor.rs b/crates/engine-qwen35-cuda/src/qwen_forward/executor_original_prompt/impl_drop_for_originalpromptexecutor.rs index e85d1238..cfe738c0 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/executor_original_prompt/impl_drop_for_originalpromptexecutor.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/executor_original_prompt/impl_drop_for_originalpromptexecutor.rs @@ -4,6 +4,7 @@ #[allow(unused_imports)] use super::*; +use crate::qwen_forward::session::original_prompt::{DraftRollback, PromptPublication, PromptWeights}; impl Drop for OriginalPromptExecutor<'_> { fn drop(&mut self) { @@ -14,15 +15,11 @@ impl OriginalPromptPending<'_, '_, '_> { /// Any fallible publication leaves the owning transaction present so Drop /// restores both real physical accepted prefixes. Success consumes it. pub(in crate::qwen_forward) fn publish(mut self) -> Result<(TargetStepOrigin, Option)> { - let result = self.txn.as_mut().ok_or(Error::IdentityMismatch)?.publish( - self.binding, - self.dense, - self.layers, - &self.completed, - self.draft, - &self.draft_kv, - &self.sample, - ); + let weights = PromptWeights { binding: self.binding, dense: self.dense }; + let publication = + PromptPublication { completed: &self.completed, draft_kv: &self.draft_kv, sample: &self.sample }; + let txn = self.txn.as_mut().ok_or(Error::IdentityMismatch)?; + let result = txn.publish(weights, self.layers, publication, self.draft); if result.is_ok() { self.txn.take(); } @@ -32,17 +29,9 @@ impl OriginalPromptPending<'_, '_, '_> { impl Drop for OriginalPromptPending<'_, '_, '_> { fn drop(&mut self) { if let Some(txn) = self.txn.take() { - if txn - .abort_with_draft( - self.binding, - self.dense, - self.layers, - &self.checkpoint, - self.draft, - &self.draft_checkpoint, - ) - .is_err() - { + let weights = PromptWeights { binding: self.binding, dense: self.dense }; + let rollback = DraftRollback { draft: self.draft, prior: &self.draft_checkpoint }; + if txn.abort_with_draft(weights, self.layers, &self.checkpoint, rollback).is_err() { *self.failed = true; } } diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/executor_original_prompt/impl_originalpromptexecutor/impl_originalpromptexecutor_2/impl_originalpromptexecutor_2_2.rs b/crates/engine-qwen35-cuda/src/qwen_forward/executor_original_prompt/impl_originalpromptexecutor/impl_originalpromptexecutor_2/impl_originalpromptexecutor_2_2.rs index 47500c64..ecb9b6ad 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/executor_original_prompt/impl_originalpromptexecutor/impl_originalpromptexecutor_2/impl_originalpromptexecutor_2_2.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/executor_original_prompt/impl_originalpromptexecutor/impl_originalpromptexecutor_2/impl_originalpromptexecutor_2_2.rs @@ -2,6 +2,7 @@ #[allow(unused_imports)] use super::*; +use crate::qwen_forward::session::original_prompt::{ParkedPoint, PromptWeights}; /// The index image and mapped K/V journal of each of the twelve resident QSA layers, by layer slot. type QsaOwners<'ctx> = @@ -153,7 +154,8 @@ impl<'ctx> OriginalPromptExecutor<'ctx> { if self.failed || self.context.is_poisoned() { return Err(Error::CudaExecution); } - let imported = session.import_parked(binding, dense, &mut self.layers, parked, blocks); + let weights = PromptWeights { binding, dense }; + let imported = session.import_parked(weights, &mut self.layers, ParkedPoint { session: parked, blocks }); if imported.is_err() { self.failed = true; } diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/executor_original_prompt/impl_originalpromptexecutor/impl_originalpromptexecutor_2_2/impl_originalpromptexecutor.rs b/crates/engine-qwen35-cuda/src/qwen_forward/executor_original_prompt/impl_originalpromptexecutor/impl_originalpromptexecutor_2_2/impl_originalpromptexecutor.rs index f87ade97..ac448979 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/executor_original_prompt/impl_originalpromptexecutor/impl_originalpromptexecutor_2_2/impl_originalpromptexecutor.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/executor_original_prompt/impl_originalpromptexecutor/impl_originalpromptexecutor_2_2/impl_originalpromptexecutor.rs @@ -2,6 +2,7 @@ #[allow(unused_imports)] use super::*; +use crate::qwen_forward::session::original_prompt::{DraftRollback, PromptWeights}; #[path = "impl_originalpromptexecutor/forward_draft.rs"] mod forward_draft; @@ -204,7 +205,9 @@ impl<'ctx> OriginalPromptExecutor<'ctx> { if moe.end_prompt_stream().is_err() { self.failed = true; } - let cleanup = txn.abort_with_draft(binding, dense, &mut self.layers, checkpoint, draft, draft_checkpoint); + let weights = PromptWeights { binding, dense }; + let rollback = DraftRollback { draft, prior: draft_checkpoint }; + let cleanup = txn.abort_with_draft(weights, &mut self.layers, checkpoint, rollback); if cleanup.is_err() || self.context.is_poisoned() { self.failed = true; } diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/prompt_device_rows/impl_promptdeviceread/produce_original_gdn_fn.rs b/crates/engine-qwen35-cuda/src/qwen_forward/prompt_device_rows/impl_promptdeviceread/produce_original_gdn_fn.rs index ab934c04..c4d99217 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/prompt_device_rows/impl_promptdeviceread/produce_original_gdn_fn.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/prompt_device_rows/impl_promptdeviceread/produce_original_gdn_fn.rs @@ -7,7 +7,7 @@ use super::*; use super::super::super::gdn::chunk_device::{ChunkEdge, GdnChunkDevice}; use super::super::super::gdn::{GdnPrior, PromptLayer, PromptProgram, PromptSource}; use super::super::super::qsa_index::prompt_device::resident_device::{ - ResidentQsaCompleted, ResidentQsaLayer, ResidentQsaPrior, ResidentQsaSelection, + ResidentQsaCompleted, ResidentQsaLayer, ResidentQsaPrior, ResidentQsaSelection, ResidentSealOwners, }; use super::super::super::session::chunk::{GdnChunkRow, GdnChunkSeal, GdnChunkStart}; use std::cell::RefMut; @@ -206,8 +206,9 @@ impl<'a, 's, 'ctx> QsaLayerRun<'a, 's, 'ctx> { completed.validate_rows(self.binding, self.dense, session, &self.inputs)?; self.lap.at(4, "validate_rows"); let accepted = self.txn.len(); - let ws = &mut self.workspace; - let seal = completed.seal_prefix(self.binding, self.dense, session, &self.inputs, qsa, ws, accepted)?; + let (binding, cache, workspace) = (self.binding, self.dense, &mut self.workspace); + let owners = ResidentSealOwners { binding, cache, session, producer: qsa, workspace }; + let seal = completed.seal_prefix(owners, &self.inputs, accepted)?; self.lap.at(5, "seal_prefix"); drop(self.workspace); self.txn.stage_qsa(self.binding, self.dense, seal)?; diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/qsa_attention/impl_qsaready.rs b/crates/engine-qwen35-cuda/src/qwen_forward/qsa_attention/impl_qsaready.rs index aa86874f..fe1bdf93 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/qsa_attention/impl_qsaready.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/qsa_attention/impl_qsaready.rs @@ -88,7 +88,7 @@ impl QsaAttentionStage { let context = txn.cuda_context()?; admit_budget(context)?; if let Some(cache) = cache { - return decode_cached(binding, &prepared, context, cache, &k, &v, width); + return decode_cached(binding, &prepared, context, cache, SelectedKv { keys: &k, values: &v, width }); } Self::decode_host(binding, &prepared, context, (&k, &v), width) } @@ -192,15 +192,22 @@ impl<'ctx> QsaGated<'ctx> { primary.map_or(cleanup, Err) } } +/// The canonical halfword pages of the selected keys and values and the width of the selection they were copied for. +#[derive(Clone, Copy)] +pub(super) struct SelectedKv<'a> { + pub(super) keys: &'a [u8], + pub(super) values: &'a [u8], + pub(super) width: u32, +} + pub(super) fn decode_cached( binding: &ModelBinding, prepared: &QsaPrepared, context: &Context<'_>, cache: &DenseWeightCache<'_>, - keys: &[u8], - values: &[u8], - width: u32, + selected: SelectedKv<'_>, ) -> Result { + let SelectedKv { keys, values, width } = selected; let context = cache.retained_context(binding, context)?; let failed = cache.transport_failure_flag(binding, context)?; let attended = attend_retained(context, prepared.q(), keys, values, width, failed)?; diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/qsa_resident_device.rs b/crates/engine-qwen35-cuda/src/qwen_forward/qsa_resident_device.rs index 9adcc71b..beebc703 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/qsa_resident_device.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/qsa_resident_device.rs @@ -87,6 +87,7 @@ pub(in crate::qwen_forward) use scalar::{ResidentQsaScalar, ResidentQsaScalarRea #[path = "qsa_resident_device/impl_residentqsacompleted.rs"] mod impl_residentqsacompleted; +pub(in crate::qwen_forward) use impl_residentqsacompleted::ResidentSealOwners; #[path = "qsa_resident_device/impl_residentqsalayer.rs"] mod impl_residentqsalayer; #[path = "qsa_resident_device/impl_residentqsalayer_2.rs"] diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/qsa_resident_device/impl_residentqsacompleted.rs b/crates/engine-qwen35-cuda/src/qwen_forward/qsa_resident_device/impl_residentqsacompleted.rs index 106d3be7..5e581408 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/qsa_resident_device/impl_residentqsacompleted.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/qsa_resident_device/impl_residentqsacompleted.rs @@ -4,6 +4,16 @@ use super::impl_residentqsalayer::mint; use super::*; +/// The owners a resident QSA prefix seal validates against and commits through: model binding, dense weight cache, +/// original session, prompt producer and native batch workspace. +pub(in crate::qwen_forward) struct ResidentSealOwners<'a, 'ctx> { + pub(in crate::qwen_forward) binding: &'a ModelBinding, + pub(in crate::qwen_forward) cache: &'a DenseWeightCache<'ctx>, + pub(in crate::qwen_forward) session: &'a QwenSession<'ctx>, + pub(in crate::qwen_forward) producer: &'a mut OriginalQsaResidentPromptDevice<'ctx>, + pub(in crate::qwen_forward) workspace: &'a mut NativeBatchWorkspace<'ctx>, +} + impl<'index, 'weights, 'ctx> ResidentQsaCompleted<'index, 'weights, 'ctx> { pub(in crate::qwen_forward) fn first(&self) -> u64 { self.prior.next_position() @@ -95,17 +105,13 @@ impl<'index, 'weights, 'ctx> ResidentQsaCompleted<'index, 'weights, 'ctx> { txn.validate_input(id.token(), id.position())?; Ok((local, id)) } - #[allow(clippy::too_many_arguments)] pub(in crate::qwen_forward) fn seal_prefix( mut self, - binding: &ModelBinding, - cache: &DenseWeightCache<'ctx>, - session: &QwenSession<'ctx>, + owners: ResidentSealOwners<'_, 'ctx>, inputs: &[&InputReady], - producer: &mut OriginalQsaResidentPromptDevice<'ctx>, - ws: &mut NativeBatchWorkspace<'ctx>, accepted: usize, ) -> Result> { + let ResidentSealOwners { binding, cache, session, producer, workspace: ws } = owners; self.validate_rows(binding, cache, session, inputs)?; if accepted > self.rows() { return Err(Error::InvalidPosition); diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt.rs b/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt.rs index 84a62ed8..df6fabdc 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt.rs @@ -9,6 +9,9 @@ use super::*; use std::cell::{Cell, RefCell}; #[path = "session_original_draft.rs"] pub(in crate::qwen_forward) mod draft; +#[path = "session_original_prompt/prompt_txn_owners.rs"] +mod prompt_txn_owners; +pub(in crate::qwen_forward) use prompt_txn_owners::{DraftRollback, PromptPublication, PromptWeights}; /// Actual tokenized request owner. Known-next conditioning IDs are read from /// this same immutable input, never from a sampled or caller-labelled token. @@ -39,6 +42,12 @@ pub(in crate::qwen_forward) struct ParkedSession { gdn: [Option; STATE_CAPACITY], tails: [Option>; STATE_CAPACITY], } +/// A parked session point and the blocks that hold the cells below its position. +#[derive(Clone, Copy)] +pub(in crate::qwen_forward) struct ParkedPoint<'a> { + pub(in crate::qwen_forward) session: &'a ParkedSession, + pub(in crate::qwen_forward) blocks: &'a [&'a ParkedBlock], +} /// The cells `first_token .. first_token + tokens` of every resident QSA layer, as plain bytes. pub(in crate::qwen_forward) struct ParkedBlock { first_token: usize, diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt/impl_originalpromptinput/impl_qwensession.rs b/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt/impl_originalpromptinput/impl_qwensession.rs index 08173b5b..8d06668c 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt/impl_originalpromptinput/impl_qwensession.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt/impl_originalpromptinput/impl_qwensession.rs @@ -259,8 +259,9 @@ impl<'driver> QwenSession<'driver> { &self, binding: &ModelBinding, layers: &[Option>; STATE_CAPACITY], - (parked, blocks): (&ParkedSession, &[&ParkedBlock]), + point: ParkedPoint<'_>, ) -> Result<()> { + let ParkedPoint { session: parked, blocks } = point; self.target_origin()?; if !self.binding.matches(&binding.identity()) || self.layer_count != 48 @@ -282,12 +283,13 @@ impl<'driver> QwenSession<'driver> { /// Write every layer's parked cells and re-mint its witness, attempting all of them; the first failure is returned. fn import_layers( &self, - binding: &ModelBinding, - dense: &DenseWeightCache<'driver>, + weights: PromptWeights<'_, 'driver>, layers: &mut [Option>; STATE_CAPACITY], - (parked, blocks): (&ParkedSession, &[&ParkedBlock]), + point: ParkedPoint<'_>, workspace: &mut leleka_cuda_driver::NativeBatchWorkspace<'driver>, ) -> Result<(RestoredLayers<'driver>, Option)> { + let PromptWeights { binding, dense } = weights; + let ParkedPoint { session: parked, blocks } = point; let mut restored = std::array::from_fn(|_| None); let mut first_error = None; let end = usize::try_from(parked.position).map_err(|_| Error::Overflow)?; @@ -332,16 +334,16 @@ impl<'driver> QwenSession<'driver> { /// is bumped first, fencing every older checkpoint. pub(in crate::qwen_forward) fn import_parked( &mut self, - binding: &ModelBinding, - dense: &DenseWeightCache<'driver>, + weights: PromptWeights<'_, 'driver>, layers: &mut [Option>; STATE_CAPACITY], - parked: &ParkedSession, - blocks: &[&ParkedBlock], + point: ParkedPoint<'_>, ) -> Result<()> { - self.check_import(binding, layers, (parked, blocks))?; + let PromptWeights { binding, dense } = weights; + let parked = point.session; + self.check_import(binding, layers, point)?; self.fence_import_branch()?; let mut workspace = self.import_workspace(binding, dense)?; - let (restored, first_error) = self.import_layers(binding, dense, layers, (parked, blocks), &mut workspace)?; + let (restored, first_error) = self.import_layers(weights, layers, point, &mut workspace)?; drop(workspace); self.fail_first(first_error)?; self.complete_cuda()?; diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt/impl_originalprompttxn/impl_originalprompttxn_2_2.rs b/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt/impl_originalprompttxn/impl_originalprompttxn_2_2.rs index 37282b8c..7ad3aee8 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt/impl_originalprompttxn/impl_originalprompttxn_2_2.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt/impl_originalprompttxn/impl_originalprompttxn_2_2.rs @@ -146,13 +146,13 @@ impl<'session, 'driver> OriginalPromptTxn<'session, 'driver> { /// inferred from a requested position or the draft's end metadata. pub(in crate::qwen_forward) fn abort_with_draft( self, - binding: &ModelBinding, - dense: &DenseWeightCache<'driver>, + weights: PromptWeights<'_, 'driver>, layers: &mut [Option>; STATE_CAPACITY], checkpoint: &OriginalSessionCheckpoint<'driver>, - draft: &mut draft::OriginalDraftSession<'driver>, - prior: &draft::OriginalDraftCheckpoint, + rollback: DraftRollback<'_, 'driver>, ) -> Result<()> { + let PromptWeights { binding, dense } = weights; + let DraftRollback { draft, prior } = rollback; self.phase.failed.set(true); let mut first = draft.restore_physical(binding, dense, prior).err(); if let Err(error) = self.session.restore_original_checkpoint(binding, dense, layers, checkpoint) { diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt/impl_originalprompttxn/impl_originalprompttxn_3.rs b/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt/impl_originalprompttxn/impl_originalprompttxn_3.rs index 4e9a4bbf..646134b4 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt/impl_originalprompttxn/impl_originalprompttxn_3.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt/impl_originalprompttxn/impl_originalprompttxn_3.rs @@ -142,14 +142,13 @@ impl<'session, 'driver> OriginalPromptTxn<'session, 'driver> { /// No host QsaIndexPrior is fabricated from an end-only resident checkpoint. pub(in crate::qwen_forward) fn publish( &mut self, - binding: &ModelBinding, - dense: &DenseWeightCache<'driver>, + weights: PromptWeights<'_, 'driver>, layers: &mut [Option>; STATE_CAPACITY], - completed: &super::super::super::super::prompt_device_rows::PromptDeviceFinal, + publication: PromptPublication<'_>, draft: &mut draft::OriginalDraftSession<'driver>, - draft_kv: &super::super::super::super::prompt_device_rows::OriginalDraftKvPending, - sample: &super::super::super::super::output::OriginalPromptSample, ) -> Result<(TargetStepOrigin, Option)> { + let PromptWeights { binding, dense } = weights; + let PromptPublication { completed, draft_kv, sample } = publication; self.validate_publish_inputs(binding, completed, draft_kv, sample)?; let draft_permit = draft.prepare_publication(binding, self, draft_kv.completed())?; let end = Position::new(self.phase.end, crate::qwen_forward::contract::position_dimension())?; diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt/prompt_txn_owners.rs b/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt/prompt_txn_owners.rs new file mode 100644 index 00000000..012fc09a --- /dev/null +++ b/crates/engine-qwen35-cuda/src/qwen_forward/session_original_prompt/prompt_txn_owners.rs @@ -0,0 +1,25 @@ +//! The owners and inputs a prompt transaction is published, aborted or imported with, grouped by meaning. + +use super::draft::{OriginalDraftCheckpoint, OriginalDraftSession}; +use super::*; + +/// The model binding and the dense weight cache a prompt transaction validates and restores against. +#[derive(Clone, Copy)] +pub(in crate::qwen_forward) struct PromptWeights<'a, 'driver> { + pub(in crate::qwen_forward) binding: &'a ModelBinding, + pub(in crate::qwen_forward) dense: &'a DenseWeightCache<'driver>, +} + +/// The draft session of a prompt transaction and the checkpoint a failed prompt restores it to. +pub(in crate::qwen_forward) struct DraftRollback<'a, 'driver> { + pub(in crate::qwen_forward) draft: &'a mut OriginalDraftSession<'driver>, + pub(in crate::qwen_forward) prior: &'a OriginalDraftCheckpoint, +} + +/// The final device rows, the pending draft K/V and the sampled token a prompt publication commits together. +#[derive(Clone, Copy)] +pub(in crate::qwen_forward) struct PromptPublication<'a> { + pub(in crate::qwen_forward) completed: &'a crate::qwen_forward::prompt_device_rows::PromptDeviceFinal, + pub(in crate::qwen_forward) draft_kv: &'a crate::qwen_forward::prompt_device_rows::OriginalDraftKvPending, + pub(in crate::qwen_forward) sample: &'a crate::qwen_forward::output::OriginalPromptSample, +}