From ff93858ca5ce5482585119de10fa744872746409 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Sat, 17 Oct 2026 15:06:00 +0200 Subject: [PATCH] perf(prefill): upload the routed slot table before the copy ring, not after it After the ring the chunk's expert stream keeps the host-to-device copy engine busy and the 327 KB upload waited milliseconds behind it. Same bytes, same fence. Plus a model-free H2D contention probe. Co-Authored-By: Claude Sonnet 5.5 --- .../src/original_moe_prompt_device.rs | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/crates/cuda-driver/src/original_moe_prompt_device.rs b/crates/cuda-driver/src/original_moe_prompt_device.rs index b99076e0..51ab567a 100644 --- a/crates/cuda-driver/src/original_moe_prompt_device.rs +++ b/crates/cuda-driver/src/original_moe_prompt_device.rs @@ -50,6 +50,7 @@ pub struct OriginalMoePromptLayer<'program, 'arena, 'loan, 'ctx> { counts: Vec, source_rows: Vec, slot_map: Vec, + slot_uploaded: bool, completed_experts: usize, } enum Command { @@ -571,6 +572,7 @@ impl<'ctx> OriginalMoePromptDevice<'ctx> { counts: actual_counts, source_rows, slot_map, + slot_uploaded: false, completed_experts: 0, }) } @@ -664,6 +666,7 @@ impl<'program, 'arena, 'loan, 'ctx> OriginalMoePromptLayer<'program, 'arena, 'lo return Err(Error::InvalidQwenShape); } } + self.upload_slot_table()?; let mut program = workspace.prepare_original_expert_layer_device( &self.read, &self.counts, @@ -720,12 +723,12 @@ impl<'program, 'arena, 'loan, 'ctx> OriginalMoePromptLayer<'program, 'arena, 'lo self.completed_experts = self.order.len(); Ok(()) } - pub fn finish( - self, - workspace: &mut NativeBatchWorkspace<'ctx>, - ) -> Result> { - if self.completed_experts != self.order.len() || self.order.is_empty() { - return Err(Error::InvalidQwenShape); + /// The routed slot table's upload (read only by the combine kernel). The copy ring does it BEFORE its expert + /// copies, because after the ring the chunk's expert stream has the host-to-device copy engine busy and a + /// 327 KB upload then waits for several milliseconds behind it. Same bytes, same completion fence. + fn upload_slot_table(&mut self) -> Result<()> { + if self.slot_uploaded { + return Ok(()); } let rows = self.read.lease(); let t = rows.rows(); @@ -746,6 +749,21 @@ impl<'program, 'arena, 'loan, 'ctx> OriginalMoePromptLayer<'program, 'arena, 'lo return combine(combine(copy, wait), pop).map(|()| unreachable!()); } combine(combine(copy, wait), pop)?; + self.slot_uploaded = true; + Ok(()) + } + pub fn finish( + mut self, + workspace: &mut NativeBatchWorkspace<'ctx>, + ) -> Result> { + if self.completed_experts != self.order.len() || self.order.is_empty() { + return Err(Error::InvalidQwenShape); + } + self.upload_slot_table()?; + let rows = self.read.lease(); + let t = rows.rows(); + let (slot, extent) = rows.span(PrefillPhase::Moe, "slot_dev")?; + let slot = slot.checked_subspan(extent, 0, t * 10 * 4)?; let bo = common(rows, S::Block, 2560 * 4)?; let mut commands = Vec::new(); kernel(