STRA-338 size2 en4: moe_layer0_pair, moe_mmq_slotpack and moe_prompt_finish functions split into phases
This commit is contained in:
@@ -32,7 +32,47 @@ fn validate_sources(
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn check_products(binding: &ModelBinding, rows: &[ChunkFfnRow], products: &[ExpertReady]) -> Result<()> {
|
||||
if products.len() != rows.len() {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
for (index, (product, row)) in products.iter().zip(rows).enumerate() {
|
||||
product.validate_origin(&binding.identity())?;
|
||||
if product.identity() != row.row().route.identity() {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
observation::final_values(index, product.output())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn witness_delta(before: (u64, u64, u64), after: (u64, u64, u64)) -> Result<[u64; 3]> {
|
||||
Ok([
|
||||
after.0.checked_sub(before.0).ok_or(Error::Overflow)?,
|
||||
after.1.checked_sub(before.1).ok_or(Error::Overflow)?,
|
||||
after.2.checked_sub(before.2).ok_or(Error::Overflow)?,
|
||||
])
|
||||
}
|
||||
impl MoeExpertStage {
|
||||
fn test_layer0_arm<'ctx>(
|
||||
binding: &ModelBinding,
|
||||
layer: LayerId,
|
||||
rows: &[ChunkFfnRow],
|
||||
chunk: &mut QwenChunkTxn<'_, 'ctx>,
|
||||
cache: &mut MoeWeightCache<'ctx>,
|
||||
arm: usize,
|
||||
) -> Result<()> {
|
||||
let _arm = observation::arm(arm)?;
|
||||
let products = if arm == 0 {
|
||||
Self::execute_chunk(binding, layer, rows, chunk, cache, &mut || false)?
|
||||
} else {
|
||||
Self::execute_chunk_mmq(binding, layer, rows, chunk, cache, &mut || false)?
|
||||
};
|
||||
check_products(binding, rows, &products)?;
|
||||
validate_sources(binding, layer, rows, chunk, false)?;
|
||||
observation::cache(arm + 1, counters(cache))?;
|
||||
drop(products);
|
||||
Ok(())
|
||||
}
|
||||
pub(in crate::qwen_forward) fn test_layer0_same_rows<'ctx>(
|
||||
binding: &ModelBinding,
|
||||
layer: LayerId,
|
||||
@@ -44,32 +84,8 @@ impl MoeExpertStage {
|
||||
observation::cache(0, counters(cache))?;
|
||||
let before = cache.test_mmq_dispatch_witness();
|
||||
for arm in 0..2 {
|
||||
let _arm = observation::arm(arm)?;
|
||||
let products = if arm == 0 {
|
||||
Self::execute_chunk(binding, layer, rows, chunk, cache, &mut || false)?
|
||||
} else {
|
||||
Self::execute_chunk_mmq(binding, layer, rows, chunk, cache, &mut || false)?
|
||||
};
|
||||
if products.len() != rows.len() {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
for (index, (product, row)) in products.iter().zip(rows).enumerate() {
|
||||
product.validate_origin(&binding.identity())?;
|
||||
if product.identity() != row.row().route.identity() {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
observation::final_values(index, product.output())?;
|
||||
}
|
||||
validate_sources(binding, layer, rows, chunk, false)?;
|
||||
observation::cache(arm + 1, counters(cache))?;
|
||||
drop(products);
|
||||
Self::test_layer0_arm(binding, layer, rows, chunk, cache, arm)?;
|
||||
}
|
||||
let after = cache.test_mmq_dispatch_witness();
|
||||
let delta = [
|
||||
after.0.checked_sub(before.0).ok_or(Error::Overflow)?,
|
||||
after.1.checked_sub(before.1).ok_or(Error::Overflow)?,
|
||||
after.2.checked_sub(before.2).ok_or(Error::Overflow)?,
|
||||
];
|
||||
observation::complete(delta)
|
||||
observation::complete(witness_delta(before, cache.test_mmq_dispatch_witness())?)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,47 @@ fn validate_row(
|
||||
origin.identity.validate(token, position, layer)?;
|
||||
row.row().validate(&binding.identity(), token, position, layer)
|
||||
}
|
||||
/// The sealed rows a group copy is validated against, borrowed apart from the mutable packet storage.
|
||||
struct RowsView<'a, 'session, 'ctx> {
|
||||
txn: &'a QwenChunkTxn<'session, 'ctx>,
|
||||
binding: &'a ModelBinding,
|
||||
layer: LayerId,
|
||||
rows: &'a [ChunkFfnRow],
|
||||
origins: &'a [Rc<DownOrigin>],
|
||||
}
|
||||
impl RowsView<'_, '_, '_> {
|
||||
fn validate_group_columns(&self, group: &GroupOrigin) -> Result<()> {
|
||||
for (column, pair) in group.pairs.iter().enumerate() {
|
||||
let row = self.rows.get(pair.row).ok_or(Error::IdentityMismatch)?;
|
||||
let origin = self.origins.get(pair.row).ok_or(Error::IdentityMismatch)?;
|
||||
if !Rc::ptr_eq(origin, &group.origins[column]) || row.row().route.experts()[pair.slot].get() != group.expert
|
||||
{
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
validate_row(self.txn, self.binding, self.layer, pair.row, row, origin)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
/// The packets of the group rows, in ascending row order, each with the slot its column lands in.
|
||||
fn select_packets<'p, 'ctx>(
|
||||
packets: &'p mut [ResidentPackedDown<'ctx>],
|
||||
pairs: &[Pair],
|
||||
) -> Result<Vec<(&'p mut ResidentPackedDown<'ctx>, usize)>> {
|
||||
let mut selected = Vec::new();
|
||||
selected.try_reserve_exact(pairs.len()).map_err(|_| Error::InputAllocation)?;
|
||||
let mut pairs_left = pairs.iter().peekable();
|
||||
for (row, packet) in packets.iter_mut().enumerate() {
|
||||
if pairs_left.peek().is_some_and(|pair| pair.row == row) {
|
||||
let pair = pairs_left.next().ok_or(Error::IdentityMismatch)?;
|
||||
selected.push((packet, pair.slot));
|
||||
}
|
||||
}
|
||||
if pairs_left.next().is_some() || selected.len() != pairs.len() {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
Ok(selected)
|
||||
}
|
||||
impl<'source, 'session, 'ctx> SlotPacks<'source, 'session, 'ctx> {
|
||||
pub(super) fn new(
|
||||
txn: &'source QwenChunkTxn<'session, 'ctx>,
|
||||
@@ -128,35 +169,17 @@ impl<'source, 'session, 'ctx> SlotPacks<'source, 'session, 'ctx> {
|
||||
// Iterating unique storage once supplies disjoint exclusive packets.
|
||||
// Genuine group row order is strictly ascending, so producer column
|
||||
// index remains its real inverse route mapping without raw buffer views.
|
||||
let mut selected = Vec::new();
|
||||
selected.try_reserve_exact(group.pairs.len()).map_err(|_| Error::InputAllocation)?;
|
||||
let mut pairs = group.pairs.iter().peekable();
|
||||
for (row, packet) in self.packets.iter_mut().enumerate() {
|
||||
if pairs.peek().is_some_and(|pair| pair.row == row) {
|
||||
let pair = pairs.next().ok_or(Error::IdentityMismatch)?;
|
||||
selected.push((packet, pair.slot));
|
||||
}
|
||||
}
|
||||
if pairs.next().is_some() || selected.len() != group.pairs.len() {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
let txn = self.txn;
|
||||
let binding = self.binding;
|
||||
let layer = self.layer;
|
||||
let rows = self.rows;
|
||||
let origins = &self.origins;
|
||||
let mut selected = select_packets(&mut self.packets, &group.pairs)?;
|
||||
let view = RowsView {
|
||||
txn: self.txn,
|
||||
binding: self.binding,
|
||||
layer: self.layer,
|
||||
rows: self.rows,
|
||||
origins: &self.origins,
|
||||
};
|
||||
ResidentPackedDown::copy_group_with(self.scope, &mut selected, |destinations| {
|
||||
copy(destinations)?;
|
||||
for (column, pair) in group.pairs.iter().enumerate() {
|
||||
let row = rows.get(pair.row).ok_or(Error::IdentityMismatch)?;
|
||||
let origin = origins.get(pair.row).ok_or(Error::IdentityMismatch)?;
|
||||
if !Rc::ptr_eq(origin, &group.origins[column])
|
||||
|| row.row().route.experts()[pair.slot].get() != group.expert
|
||||
{
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
validate_row(txn, binding, layer, pair.row, row, origin)?;
|
||||
}
|
||||
view.validate_group_columns(group)?;
|
||||
if cancelled() {
|
||||
return Err(Error::Unsupported(Stage::Executor));
|
||||
}
|
||||
@@ -190,6 +213,40 @@ impl<'source, 'session, 'ctx> SlotPacks<'source, 'session, 'ctx> {
|
||||
cancelled,
|
||||
)
|
||||
}
|
||||
fn finish_values(
|
||||
&self,
|
||||
row: &ChunkFfnRow,
|
||||
origin: &Rc<DownOrigin>,
|
||||
packed: &ResidentPackedDown<'ctx>,
|
||||
activation: &DeviceBuffer<'_>,
|
||||
cache: &mut MoeWeightCache<'_>,
|
||||
) -> Result<Box<[f32]>> {
|
||||
if use_shared_tail(cache) {
|
||||
packed.with_completed(self.scope, |packet| {
|
||||
finish_shared_queued(
|
||||
self.context,
|
||||
self.binding,
|
||||
row.row(),
|
||||
activation,
|
||||
cache,
|
||||
self.scope,
|
||||
origin,
|
||||
packet,
|
||||
)
|
||||
})
|
||||
} else {
|
||||
finish_shared_with(
|
||||
self.context,
|
||||
self.binding,
|
||||
row.row(),
|
||||
activation,
|
||||
cache,
|
||||
self.scope,
|
||||
origin,
|
||||
|weights, scaled| packed.combine(self.scope, weights, scaled),
|
||||
)
|
||||
}
|
||||
}
|
||||
pub(super) fn finish_row(
|
||||
&self,
|
||||
index: usize,
|
||||
@@ -205,31 +262,7 @@ impl<'source, 'session, 'ctx> SlotPacks<'source, 'session, 'ctx> {
|
||||
}
|
||||
let packed = self.packets.get(index).ok_or(Error::IdentityMismatch)?;
|
||||
packed.complete(self.scope)?;
|
||||
let values = if use_shared_tail(cache) {
|
||||
packed.with_completed(self.scope, |packet| {
|
||||
finish_shared_queued(
|
||||
self.context,
|
||||
self.binding,
|
||||
row.row(),
|
||||
activation,
|
||||
cache,
|
||||
self.scope,
|
||||
origin,
|
||||
packet,
|
||||
)
|
||||
})?
|
||||
} else {
|
||||
finish_shared_with(
|
||||
self.context,
|
||||
self.binding,
|
||||
row.row(),
|
||||
activation,
|
||||
cache,
|
||||
self.scope,
|
||||
origin,
|
||||
|weights, scaled| packed.combine(self.scope, weights, scaled),
|
||||
)?
|
||||
};
|
||||
let values = self.finish_values(row, origin, packed, activation, cache)?;
|
||||
validate_row(self.txn, self.binding, self.layer, index, row, origin)?;
|
||||
if cancelled() {
|
||||
return Err(Error::Unsupported(Stage::Executor));
|
||||
|
||||
@@ -9,6 +9,191 @@ pub(super) struct NativePromptFinish<'loan, 'ctx, 'dense> {
|
||||
pub(super) dense: &'loan DenseWeightCache<'dense>,
|
||||
}
|
||||
|
||||
/// The sealed window rows one native prompt finish is checked against.
|
||||
struct PromptRows<'a, 'session, 'ctx> {
|
||||
context: &'ctx Context<'ctx>,
|
||||
binding: &'a ModelBinding,
|
||||
layer: LayerId,
|
||||
rows: &'a [ChunkFfnRow],
|
||||
txn: &'a QwenChunkTxn<'session, 'ctx>,
|
||||
scope: &'a DownScope<'ctx>,
|
||||
}
|
||||
impl<'ctx> PromptRows<'_, '_, 'ctx> {
|
||||
fn validate_shape(&self, packets: &[&ResidentPackedDown<'ctx>]) -> Result<()> {
|
||||
if self.rows.len() != self.txn.micro_len()?
|
||||
|| self.rows.len() != packets.len()
|
||||
|| !(1..=64).contains(&self.rows.len())
|
||||
|| !std::ptr::eq(self.context, self.txn.context()?)
|
||||
{
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn expected(&self, packets: &[&ResidentPackedDown<'ctx>]) -> Result<Vec<TokenLayer>> {
|
||||
let mut expected = Vec::new();
|
||||
expected.try_reserve_exact(self.rows.len()).map_err(|_| Error::InputAllocation)?;
|
||||
for (index, (row, packet)) in self.rows.iter().zip(packets).enumerate() {
|
||||
self.txn.validate_ffn_row(self.txn.micro_row_index(index)?, self.layer, row)?;
|
||||
let identity = row.row().route.identity();
|
||||
row.row().validate(&self.binding.identity(), identity.token(), identity.position(), self.layer)?;
|
||||
packet.complete(self.scope)?;
|
||||
expected.push(identity);
|
||||
}
|
||||
Ok(expected)
|
||||
}
|
||||
/// The route that drove each expert slot is the producer's same real route,
|
||||
/// including bit-exact weights. A same-sized packet bank is insufficient.
|
||||
fn check_routes(&self, products: &MoeMicroProducts<'ctx>) -> Result<()> {
|
||||
for (row, route) in self.rows.iter().zip(products.rows()) {
|
||||
for slot in 0..K {
|
||||
if route.ids[slot] != row.row().route.experts()[slot].get() as i32
|
||||
|| route.weights[slot].to_bits() != row.row().route.weights()[slot].get().to_bits()
|
||||
{
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Both actual packet storage and the produced shared/route owners already
|
||||
/// exist. Reserve the simultaneous gathered Dm and whole Bo before allocation.
|
||||
fn reserve_dm(&self, cache: &mut MoeWeightCache<'ctx>) -> Result<usize> {
|
||||
let (context, rows) = (self.context, self.rows);
|
||||
let dm_bytes = rows.len().checked_mul(K * N * 4).ok_or(Error::Overflow)?;
|
||||
let extra = dm_bytes.checked_add(rows.len() * N * 4).ok_or(Error::Overflow)?;
|
||||
cache.reserve_mmq_transient(self.binding, context, extra)?;
|
||||
let needed =
|
||||
extra.checked_add(super::super::super::moe_weight_cache::WORKING_RESERVE).ok_or(Error::Overflow)?;
|
||||
if context.allocated_bytes().checked_add(needed).is_none_or(|n| n > context.budget_bytes())
|
||||
|| context.memory_info().map_err(driver_error)?.0 < needed
|
||||
{
|
||||
return Err(Error::InputAllocation);
|
||||
}
|
||||
Ok(dm_bytes)
|
||||
}
|
||||
fn gather_sources<'p>(
|
||||
&self,
|
||||
packets: &[&'p ResidentPackedDown<'ctx>],
|
||||
cancelled: &mut impl FnMut() -> bool,
|
||||
) -> Result<Vec<&'p DeviceBuffer<'ctx>>> {
|
||||
let mut sources = Vec::new();
|
||||
sources.try_reserve_exact(packets.len()).map_err(|_| Error::InputAllocation)?;
|
||||
for (index, packet) in packets.iter().enumerate() {
|
||||
if cancelled() {
|
||||
return Err(Error::Unsupported(Stage::Executor));
|
||||
}
|
||||
self.txn.validate_ffn_row(self.txn.micro_row_index(index)?, self.layer, &self.rows[index])?;
|
||||
sources.push(packet.completed_buffer(self.scope)?);
|
||||
}
|
||||
Ok(sources)
|
||||
}
|
||||
fn copy_packets_into(
|
||||
&self,
|
||||
packets: &[&ResidentPackedDown<'ctx>],
|
||||
dm: &mut TransportBuffer<'ctx>,
|
||||
cancelled: &mut impl FnMut() -> bool,
|
||||
) -> Result<()> {
|
||||
let sources = self.gather_sources(packets, cancelled)?;
|
||||
self.context.copy_moe_prompt_packets(&sources, dm.buffer_mut()?).map_err(driver_error)?;
|
||||
for (index, packet) in packets.iter().enumerate() {
|
||||
packet.complete(self.scope)?;
|
||||
self.txn.validate_ffn_row(self.txn.micro_row_index(index)?, self.layer, &self.rows[index])?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn slot_indices(&self) -> Result<Vec<i32>> {
|
||||
let mut slots = Vec::new();
|
||||
slots.try_reserve_exact(self.rows.len() * K).map_err(|_| Error::InputAllocation)?;
|
||||
for slot in 0..self.rows.len() * K {
|
||||
slots.push(i32::try_from(slot).map_err(|_| Error::Overflow)?);
|
||||
}
|
||||
Ok(slots)
|
||||
}
|
||||
fn combine(
|
||||
&self,
|
||||
cache: &mut MoeWeightCache<'ctx>,
|
||||
finish: &mut NativePromptFinish<'_, 'ctx, '_>,
|
||||
expected: &[TokenLayer],
|
||||
(dm, slots): (&TransportBuffer<'ctx>, &[i32]),
|
||||
) -> Result<()> {
|
||||
let (binding, context) = (self.binding, self.context);
|
||||
let mut resources = cache.native_prefill_resources(binding, context)?;
|
||||
let mut workspace = finish.dense.native_batch_workspace(binding, context)?.ok_or(Error::IdentityMismatch)?;
|
||||
let gemm = workspace.gdn_gemm().map_err(driver_error)?;
|
||||
MoePrefillStage::combine_micro(
|
||||
binding,
|
||||
self.layer,
|
||||
expected,
|
||||
context,
|
||||
finish.products,
|
||||
CombineInput { dm: dm.buffer()?, slot: slots },
|
||||
&mut resources,
|
||||
&gemm,
|
||||
)
|
||||
}
|
||||
fn collect_outputs(
|
||||
&self,
|
||||
products: &MoeMicroProducts<'ctx>,
|
||||
cancelled: &mut impl FnMut() -> bool,
|
||||
) -> Result<Vec<ExpertReady>> {
|
||||
let values = download_f32(products.combined().ok_or(Error::IdentityMismatch)?, self.rows.len() * N)?;
|
||||
let mut outputs = Vec::new();
|
||||
outputs.try_reserve_exact(self.rows.len()).map_err(|_| Error::InputAllocation)?;
|
||||
for (index, row) in self.rows.iter().enumerate() {
|
||||
if cancelled() {
|
||||
return Err(Error::Unsupported(Stage::Executor));
|
||||
}
|
||||
self.txn.validate_ffn_row(self.txn.micro_row_index(index)?, self.layer, row)?;
|
||||
let start = index * N;
|
||||
let mut actual = Vec::new();
|
||||
actual.try_reserve_exact(N).map_err(|_| Error::InputAllocation)?;
|
||||
actual.extend_from_slice(&values[start..start + N]);
|
||||
let identity = row.row().route.identity();
|
||||
outputs.push(ExpertReady::from_completed(self.binding.identity(), identity, actual.into_boxed_slice())?);
|
||||
}
|
||||
Ok(outputs)
|
||||
}
|
||||
fn run(
|
||||
&self,
|
||||
packets: &[&ResidentPackedDown<'ctx>],
|
||||
cache: &mut MoeWeightCache<'ctx>,
|
||||
finish: &mut NativePromptFinish<'_, 'ctx, '_>,
|
||||
(expected, dm): (&[TokenLayer], &mut TransportBuffer<'ctx>),
|
||||
cancelled: &mut impl FnMut() -> bool,
|
||||
) -> Result<Vec<ExpertReady>> {
|
||||
self.copy_packets_into(packets, dm, cancelled)?;
|
||||
if cancelled() {
|
||||
return Err(Error::Unsupported(Stage::Executor));
|
||||
}
|
||||
let slots = self.slot_indices()?;
|
||||
self.combine(cache, finish, expected, (dm, &slots))?;
|
||||
self.collect_outputs(finish.products, cancelled)
|
||||
}
|
||||
fn finish(
|
||||
&self,
|
||||
packets: &[&ResidentPackedDown<'ctx>],
|
||||
cache: &mut MoeWeightCache<'ctx>,
|
||||
finish: &mut NativePromptFinish<'_, 'ctx, '_>,
|
||||
cancelled: &mut impl FnMut() -> bool,
|
||||
) -> Result<Vec<ExpertReady>> {
|
||||
self.validate_shape(packets)?;
|
||||
cache.validate(self.binding, self.context)?;
|
||||
finish.dense.retained_context(self.binding, self.context)?;
|
||||
let expected = self.expected(packets)?;
|
||||
finish.products.validate_window(&self.binding.identity(), self.layer, &expected)?;
|
||||
self.check_routes(finish.products)?;
|
||||
if cancelled() {
|
||||
return Err(Error::Unsupported(Stage::Executor));
|
||||
}
|
||||
let dm_bytes = self.reserve_dm(cache)?;
|
||||
let mut dm = self.scope.allocate(dm_bytes)?;
|
||||
let operation = self.run(packets, cache, finish, (&expected, &mut dm), cancelled);
|
||||
self.scope.record(&operation);
|
||||
let cleanup = dm.close();
|
||||
operation.and_then(|values| cleanup.map(|()| values))
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(super) fn finish<'ctx>(
|
||||
context: &'ctx Context<'ctx>,
|
||||
@@ -22,108 +207,5 @@ pub(super) fn finish<'ctx>(
|
||||
finish: &mut NativePromptFinish<'_, 'ctx, '_>,
|
||||
cancelled: &mut impl FnMut() -> bool,
|
||||
) -> Result<Vec<ExpertReady>> {
|
||||
if rows.len() != txn.micro_len()?
|
||||
|| rows.len() != packets.len()
|
||||
|| !(1..=64).contains(&rows.len())
|
||||
|| !std::ptr::eq(context, txn.context()?)
|
||||
{
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
cache.validate(binding, context)?;
|
||||
finish.dense.retained_context(binding, context)?;
|
||||
let mut expected = Vec::new();
|
||||
expected.try_reserve_exact(rows.len()).map_err(|_| Error::InputAllocation)?;
|
||||
for (index, (row, packet)) in rows.iter().zip(packets).enumerate() {
|
||||
txn.validate_ffn_row(txn.micro_row_index(index)?, layer, row)?;
|
||||
let identity = row.row().route.identity();
|
||||
row.row().validate(&binding.identity(), identity.token(), identity.position(), layer)?;
|
||||
packet.complete(scope)?;
|
||||
expected.push(identity);
|
||||
}
|
||||
finish.products.validate_window(&binding.identity(), layer, &expected)?;
|
||||
// The route that drove each expert slot is the producer's same real route,
|
||||
// including bit-exact weights. A same-sized packet bank is insufficient.
|
||||
for (row, route) in rows.iter().zip(finish.products.rows()) {
|
||||
for slot in 0..K {
|
||||
if route.ids[slot] != row.row().route.experts()[slot].get() as i32
|
||||
|| route.weights[slot].to_bits() != row.row().route.weights()[slot].get().to_bits()
|
||||
{
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
}
|
||||
}
|
||||
if cancelled() {
|
||||
return Err(Error::Unsupported(Stage::Executor));
|
||||
}
|
||||
// Both actual packet storage and the produced shared/route owners already
|
||||
// exist. Reserve the simultaneous gathered Dm and whole Bo before allocation.
|
||||
let dm_bytes = rows.len().checked_mul(K * N * 4).ok_or(Error::Overflow)?;
|
||||
let extra = dm_bytes.checked_add(rows.len() * N * 4).ok_or(Error::Overflow)?;
|
||||
cache.reserve_mmq_transient(binding, context, extra)?;
|
||||
let needed = extra.checked_add(super::super::super::moe_weight_cache::WORKING_RESERVE).ok_or(Error::Overflow)?;
|
||||
if context.allocated_bytes().checked_add(needed).is_none_or(|n| n > context.budget_bytes())
|
||||
|| context.memory_info().map_err(driver_error)?.0 < needed
|
||||
{
|
||||
return Err(Error::InputAllocation);
|
||||
}
|
||||
let mut dm = scope.allocate(dm_bytes)?;
|
||||
let operation = (|| {
|
||||
let mut sources = Vec::new();
|
||||
sources.try_reserve_exact(packets.len()).map_err(|_| Error::InputAllocation)?;
|
||||
for (index, packet) in packets.iter().enumerate() {
|
||||
if cancelled() {
|
||||
return Err(Error::Unsupported(Stage::Executor));
|
||||
}
|
||||
txn.validate_ffn_row(txn.micro_row_index(index)?, layer, &rows[index])?;
|
||||
sources.push(packet.completed_buffer(scope)?);
|
||||
}
|
||||
context.copy_moe_prompt_packets(&sources, dm.buffer_mut()?).map_err(driver_error)?;
|
||||
for (index, packet) in packets.iter().enumerate() {
|
||||
packet.complete(scope)?;
|
||||
txn.validate_ffn_row(txn.micro_row_index(index)?, layer, &rows[index])?;
|
||||
}
|
||||
if cancelled() {
|
||||
return Err(Error::Unsupported(Stage::Executor));
|
||||
}
|
||||
let mut slots = Vec::new();
|
||||
slots.try_reserve_exact(rows.len() * K).map_err(|_| Error::InputAllocation)?;
|
||||
for slot in 0..rows.len() * K {
|
||||
slots.push(i32::try_from(slot).map_err(|_| Error::Overflow)?);
|
||||
}
|
||||
let mut resources = cache.native_prefill_resources(binding, context)?;
|
||||
let mut workspace = finish.dense.native_batch_workspace(binding, context)?.ok_or(Error::IdentityMismatch)?;
|
||||
let gemm = workspace.gdn_gemm().map_err(driver_error)?;
|
||||
MoePrefillStage::combine_micro(
|
||||
binding,
|
||||
layer,
|
||||
&expected,
|
||||
context,
|
||||
finish.products,
|
||||
CombineInput { dm: dm.buffer()?, slot: &slots },
|
||||
&mut resources,
|
||||
&gemm,
|
||||
)?;
|
||||
let values = download_f32(finish.products.combined().ok_or(Error::IdentityMismatch)?, rows.len() * N)?;
|
||||
let mut outputs = Vec::new();
|
||||
outputs.try_reserve_exact(rows.len()).map_err(|_| Error::InputAllocation)?;
|
||||
for (index, row) in rows.iter().enumerate() {
|
||||
if cancelled() {
|
||||
return Err(Error::Unsupported(Stage::Executor));
|
||||
}
|
||||
txn.validate_ffn_row(txn.micro_row_index(index)?, layer, row)?;
|
||||
let start = index * N;
|
||||
let mut actual = Vec::new();
|
||||
actual.try_reserve_exact(N).map_err(|_| Error::InputAllocation)?;
|
||||
actual.extend_from_slice(&values[start..start + N]);
|
||||
outputs.push(ExpertReady::from_completed(
|
||||
binding.identity(),
|
||||
row.row().route.identity(),
|
||||
actual.into_boxed_slice(),
|
||||
)?);
|
||||
}
|
||||
Ok(outputs)
|
||||
})();
|
||||
scope.record(&operation);
|
||||
let cleanup = dm.close();
|
||||
operation.and_then(|values| cleanup.map(|()| values))
|
||||
PromptRows { context, binding, layer, rows, txn, scope }.finish(packets, cache, finish, cancelled)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user