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.
This commit is contained in:
Pavlo Ratushnyi
2026-11-08 01:57:23 +01:00
parent f02db063c6
commit d0d6d03533
5 changed files with 94 additions and 88 deletions
@@ -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"))]
@@ -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<Pair>],
origins: &'g [Rc<DownOrigin>],
#[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>,
@@ -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<Pair>],
pub(super) scope: &'a DownScope<'ctx>,
pub(super) origins: &'a [Rc<DownOrigin>],
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<prompt_finish::NativePromptFinish<'l, 'ctx, 'd>>,
#[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<Pair>],
cache: &mut MoeWeightCache<'ctx>,
prepared: &[RoutedGateUpQ8<'_>],
cancelled: &mut impl FnMut() -> bool,
actual: &QwenChunkTxn<'_, 'ctx>,
) -> Result<Vec<ExpertReady>> {
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<ActualSlotPacket<'ctx>>,
groups: &[Vec<Pair>],
cache: &mut MoeWeightCache<'ctx>,
prepared: &[RoutedGateUpQ8<'_>],
cancelled: &mut impl FnMut() -> bool,
actual: &QwenChunkTxn<'_, 'ctx>,
) -> Result<Vec<ExpertReady>> {
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<Pair>],
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.
@@ -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<Pair>],
run: PacketRun<'_, 'ctx, '_, '_>,
cache: &mut MoeWeightCache<'ctx>,
prepared: &[RoutedGateUpQ8<'_>],
activations: &[TransportBuffer<'ctx>],
scope: &DownScope<'ctx>,
origins: &[Rc<DownOrigin>],
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<Vec<ExpertReady>> {
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.
@@ -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<DownOrigin>],
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)]