From d0d6d03533e5b6e97a1bfafb78a7a476ca2cd85f Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Sun, 8 Nov 2026 01:57:23 +0100 Subject: [PATCH] STRA-338 params r03: direct packet batch owns its groups and prepared Gate/Up; project_group_direct is a PacketRun method PacketRun now holds the expert groups and the prepared Gate/Up Q8 rows, so execute (6 -> 4), operate (7 -> 5), project_groups (7 -> 5) and execute_direct_packets (12 -> 4) drop them. project_one (7 -> 4) and project_group_direct (11 -> 4, now PacketRun::project_group_direct) take the DirectRun (actual, packets, cancelled). BoundRows::packet_run builds the PacketRun that execute_direct_packets used to assemble from 12 arguments. --- .../src/qwen_forward/moe_group_chunk.rs | 2 - .../moe_group_chunk/bound_rows_fn.rs | 41 +++++++++--- .../moe_group_chunk/direct_packets_fns.rs | 48 +++++++------- .../ordered_down_refusal_fn.rs | 27 +------- .../project_group_direct_fn.rs | 64 ++++++++++--------- 5 files changed, 94 insertions(+), 88 deletions(-) diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk.rs b/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk.rs index a4d2eaac..9079e2b8 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk.rs @@ -85,8 +85,6 @@ use project_group_fn::project_group; mod project_group_direct_fn; #[path = "moe_group_chunk/project_group_shared_fns.rs"] mod project_group_shared_fns; -#[allow(unused_imports)] -use project_group_direct_fn::project_group_direct; #[path = "moe_group_chunk/project_group_source_fenced_fn.rs"] mod project_group_source_fenced_fn; #[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))] diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/bound_rows_fn.rs b/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/bound_rows_fn.rs index a024a038..6467ba7f 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/bound_rows_fn.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/bound_rows_fn.rs @@ -1,5 +1,6 @@ //! The uploaded rows of one chunk and their ungrouped/grouped execution (STRA-338 size: extract-method only). +use super::direct_packets_fns::PacketRun; #[allow(unused_imports)] use super::*; @@ -91,25 +92,45 @@ impl<'a, 'ctx> BoundRows<'a, 'ctx> { let groups = route_groups(self.rows)?; let origins = down_origins(self.binding, self.rows)?; if use_direct_packets(cache) { - return execute_direct_packets( - self.context, - self.binding, - self.rows, + let run = self.packet_run( &groups, - cache, - self.prepared, - self.activations, - self.scope, &origins, - cancelled, - actual.ok_or(Error::IdentityMismatch)?, #[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))] native, ); + return execute_direct_packets(run, cache, cancelled, actual.ok_or(Error::IdentityMismatch)?); } self.execute_scalar_down(cache, cancelled, actual, &groups, &origins) } + /// The rows of this chunk as one direct-packet batch over their expert `groups` and row `origins`. + fn packet_run<'g, 'l, 'd>( + &self, + groups: &'g [Vec], + origins: &'g [Rc], + #[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))] native: Option< + prompt_finish::NativePromptFinish<'l, 'ctx, 'd>, + >, + ) -> PacketRun<'g, 'ctx, 'l, 'd> + where + 'a: 'g, + { + PacketRun { + context: self.context, + binding: self.binding, + rows: self.rows, + groups, + scope: self.scope, + origins, + activations: self.activations, + prepared: self.prepared, + #[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))] + native, + #[cfg(not(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64")))] + marker: std::marker::PhantomData, + } + } + fn execute_scalar_down( &self, cache: &mut MoeWeightCache<'ctx>, diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/direct_packets_fns.rs b/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/direct_packets_fns.rs index 3441aefb..1c8acdb9 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/direct_packets_fns.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/direct_packets_fns.rs @@ -2,16 +2,20 @@ #[allow(unused_imports)] use super::ordered_down_refusal_fn::close_packets; +use super::project_group_direct_fn::DirectRun; use super::*; -/// The sealed rows, their origins and shared activations of one direct-packet layer batch. +/// The sealed rows, their expert groups, origins and shared activations of one direct-packet layer batch, with the +/// Gate/Up Q8 products prepared for those rows. pub(super) struct PacketRun<'a, 'ctx, 'l, 'd> { pub(super) context: &'ctx Context<'ctx>, pub(super) binding: &'a ModelBinding, pub(super) rows: &'a [ChunkFfnRow], + pub(super) groups: &'a [Vec], pub(super) scope: &'a DownScope<'ctx>, pub(super) origins: &'a [Rc], pub(super) activations: &'a [TransportBuffer<'ctx>], + pub(super) prepared: &'a [RoutedGateUpQ8<'a>], #[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))] pub(super) native: Option>, #[cfg(not(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64")))] @@ -22,16 +26,14 @@ impl<'ctx> PacketRun<'_, 'ctx, '_, '_> { /// Reserve the packets, run the batch and close every packet whatever the outcome. pub(super) fn execute( self, - groups: &[Vec], cache: &mut MoeWeightCache<'ctx>, - prepared: &[RoutedGateUpQ8<'_>], cancelled: &mut impl FnMut() -> bool, actual: &QwenChunkTxn<'_, 'ctx>, ) -> Result> { let mut packets = Vec::new(); packets.try_reserve_exact(self.rows.len()).map_err(|_| Error::InputAllocation)?; let scope = self.scope; - let operation = self.operate(&mut packets, groups, cache, prepared, cancelled, actual); + let operation = self.operate(&mut packets, cache, cancelled, actual); scope.record(&operation); let cleanup = close_packets(packets); operation.and_then(|values| cleanup.map(|()| values)) @@ -41,14 +43,12 @@ impl<'ctx> PacketRun<'_, 'ctx, '_, '_> { fn operate( mut self, packets: &mut Vec>, - groups: &[Vec], cache: &mut MoeWeightCache<'ctx>, - prepared: &[RoutedGateUpQ8<'_>], cancelled: &mut impl FnMut() -> bool, actual: &QwenChunkTxn<'_, 'ctx>, ) -> Result> { self.init_packets(packets)?; - self.project_groups(packets, groups, cache, prepared, cancelled, actual)?; + self.project_groups(packets, cache, cancelled, actual)?; #[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))] if let Some(mut finish) = self.native.take() { return self.finish_native(packets, cache, &mut finish, cancelled, actual); @@ -70,23 +70,20 @@ impl<'ctx> PacketRun<'_, 'ctx, '_, '_> { } /// The adaptive producer (when selected) or every non-empty expert group, one at a time. - #[allow(clippy::too_many_arguments)] pub(super) fn project_groups( &self, packets: &mut [ActualSlotPacket<'ctx>], - groups: &[Vec], cache: &mut MoeWeightCache<'ctx>, - prepared: &[RoutedGateUpQ8<'_>], cancelled: &mut impl FnMut() -> bool, actual: &QwenChunkTxn<'_, 'ctx>, ) -> Result<()> { let adaptive = uses_adaptive(cache); - self.project_adaptive(adaptive, packets, groups, cache, prepared, cancelled, actual)?; - for pairs in groups.iter().filter(|group| !group.is_empty() && !adaptive) { + self.project_adaptive(adaptive, packets, self.groups, cache, self.prepared, cancelled, actual)?; + for pairs in self.groups.iter().filter(|group| !group.is_empty() && !adaptive) { if cancelled() { return Err(Error::Unsupported(Stage::Executor)); } - self.project_one(pairs, packets, cache, prepared, cancelled, actual)?; + self.project_one(pairs, cache, DirectRun { actual, packets, cancelled })?; } Ok(()) } @@ -126,26 +123,33 @@ impl<'ctx> PacketRun<'_, 'ctx, '_, '_> { Ok(()) } - #[allow(clippy::too_many_arguments)] fn project_one( &self, pairs: &[Pair], - packets: &mut [ActualSlotPacket<'ctx>], cache: &mut MoeWeightCache<'ctx>, - prepared: &[RoutedGateUpQ8<'_>], - cancelled: &mut impl FnMut() -> bool, - actual: &QwenChunkTxn<'_, 'ctx>, + run: DirectRun<'_, '_, '_, 'ctx, impl FnMut() -> bool>, ) -> Result<()> { + #[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))] + let DirectRun { actual, packets, cancelled } = run; + #[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))] let (context, binding, rows, scope, origins) = (self.context, self.binding, self.rows, self.scope, self.origins); #[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))] return project_group_source_fenced( - context, binding, rows, pairs, cache, prepared, scope, origins, packets, cancelled, actual, + context, + binding, + rows, + pairs, + cache, + self.prepared, + scope, + origins, + packets, + cancelled, + actual, ); #[cfg(not(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64")))] - return project_group_direct( - context, binding, rows, pairs, cache, prepared, scope, origins, packets, cancelled, actual, - ); + return self.project_group_direct(pairs, cache, run); } /// The native prompt finish consumes the completed packets of every row. diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/ordered_down_refusal_fn.rs b/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/ordered_down_refusal_fn.rs index 9a0dd709..2336e9c3 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/ordered_down_refusal_fn.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/ordered_down_refusal_fn.rs @@ -101,36 +101,13 @@ fn validate_pair( row.validate(&binding.identity(), identity.token(), identity.position(), identity.layer())?; packets[pair.row].validate(&origins[pair.row], binding, identity) } -#[allow(clippy::too_many_arguments)] pub(super) fn execute_direct_packets<'ctx>( - context: &'ctx Context<'ctx>, - binding: &ModelBinding, - rows: &[ChunkFfnRow], - groups: &[Vec], + run: PacketRun<'_, 'ctx, '_, '_>, cache: &mut MoeWeightCache<'ctx>, - prepared: &[RoutedGateUpQ8<'_>], - activations: &[TransportBuffer<'ctx>], - scope: &DownScope<'ctx>, - origins: &[Rc], cancelled: &mut impl FnMut() -> bool, actual: &QwenChunkTxn<'_, 'ctx>, - #[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))] native: Option< - prompt_finish::NativePromptFinish<'_, 'ctx, '_>, - >, ) -> Result> { - let run = PacketRun { - context, - binding, - rows, - scope, - origins, - activations, - #[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))] - native, - #[cfg(not(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64")))] - marker: std::marker::PhantomData, - }; - run.execute(groups, cache, prepared, cancelled, actual) + run.execute(cache, cancelled, actual) } /// Close every packet; the first failure is kept and every packet is closed. diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/project_group_direct_fn.rs b/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/project_group_direct_fn.rs index bac01e2a..5dd3527b 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/project_group_direct_fn.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/moe_group_chunk/project_group_direct_fn.rs @@ -1,34 +1,40 @@ //! Moved out of the parent module by STRA-338: fn project_group_direct. +use super::direct_packets_fns::PacketRun; #[allow(unused_imports)] use super::project_group_shared_fns::*; use super::*; -#[allow(clippy::too_many_arguments)] -pub(super) fn project_group_direct<'ctx>( - context: &'ctx Context<'ctx>, - binding: &ModelBinding, - rows: &[ChunkFfnRow], - pairs: &[Pair], - cache: &mut MoeWeightCache<'_>, - prepared: &[RoutedGateUpQ8<'_>], - scope: &DownScope<'ctx>, - origins: &[Rc], - packets: &mut [ActualSlotPacket<'ctx>], - cancelled: &mut impl FnMut() -> bool, - actual: &QwenChunkTxn<'_, '_>, -) -> Result<()> { - validate_packet_pairs(actual, binding, rows, pairs, origins, packets)?; - let first = pairs[0]; - let weights = &rows[first.row].row().weights; - let selection = MoeExpertSelection::Routed { slot: first.slot }; - let group = DirectGroup { context, binding, rows, pairs, scope, origins, weights, selection }; - group.check_descriptors()?; - let mut buffers = GroupBuffers::default(); - let operation = group.run(cache, prepared, DirectRun { actual, packets, cancelled }, &mut buffers); - scope.record(&operation); - let GroupBuffers { gate, up, hidden } = buffers; - operation.and(close_group_buffers(hidden, up, gate)) +impl<'a, 'ctx> PacketRun<'a, 'ctx, '_, '_> { + /// Project one expert group of the batch through the plain device path (no source fence), filling the slot + /// packets of its rows; the group buffers are closed whatever the outcome. + pub(super) fn project_group_direct( + &self, + pairs: &[Pair], + cache: &mut MoeWeightCache<'_>, + run: DirectRun<'_, '_, '_, 'ctx, impl FnMut() -> bool>, + ) -> Result<()> { + validate_packet_pairs(run.actual, self.binding, self.rows, pairs, self.origins, run.packets)?; + let first = pairs[0]; + let weights = &self.rows[first.row].row().weights; + let selection = MoeExpertSelection::Routed { slot: first.slot }; + let group = DirectGroup { + context: self.context, + binding: self.binding, + rows: self.rows, + pairs, + scope: self.scope, + origins: self.origins, + weights, + selection, + }; + group.check_descriptors()?; + let mut buffers = GroupBuffers::default(); + let operation = group.run(cache, self.prepared, run, &mut buffers); + self.scope.record(&operation); + let GroupBuffers { gate, up, hidden } = buffers; + operation.and(close_group_buffers(hidden, up, gate)) + } } /// The Down matrix of a group: its kind, the routed format it is stored in and the cached matrix itself. @@ -39,10 +45,10 @@ struct DownProjection<'a> { matrix: CachedMoeMatrix<'a, 'a>, } /// The chunk transaction, the slot packets a group fills and the cancellation poll of one direct run. -struct DirectRun<'a, 'session, 'driver, 'ctx, C> { - actual: &'a QwenChunkTxn<'session, 'driver>, - packets: &'a mut [ActualSlotPacket<'ctx>], - cancelled: &'a mut C, +pub(super) struct DirectRun<'a, 'session, 'driver, 'ctx, C> { + pub(super) actual: &'a QwenChunkTxn<'session, 'driver>, + pub(super) packets: &'a mut [ActualSlotPacket<'ctx>], + pub(super) cancelled: &'a mut C, } /// The transport buffers a direct group allocates in turn: Gate, Up and the activated hidden rows. #[derive(Default)]