From 84983abdb22dfb7eb68d9ed0fb6c6b6ff47ceff1 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Fri, 16 Oct 2026 17:31:29 +0200 Subject: [PATCH] perf(prefill): stage the initial rows in one pass per buffer PromptDeviceRows::initialize concatenated every row's 10240 stream values and 2560 PLE values into two host vectors, then the driver scanned them for non-finite values and converted them to bytes: three passes over ~420 MB at an 8192-token chunk. The driver's initialize_rows takes the row slices and builds the two byte buffers in one pass each, with the same finite refusal and the same device bytes. Co-Authored-By: Claude Opus 5.5 --- .../cuda-driver/src/native_prompt_device.rs | 45 ++++++++++++++++++- .../src/qwen_forward/prompt_device_rows.rs | 8 ++-- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/crates/cuda-driver/src/native_prompt_device.rs b/crates/cuda-driver/src/native_prompt_device.rs index 3bde02fa..fe22131f 100644 --- a/crates/cuda-driver/src/native_prompt_device.rs +++ b/crates/cuda-driver/src/native_prompt_device.rs @@ -658,7 +658,7 @@ impl<'ctx> OriginalPromptDevice<'ctx> { /// Initial authentic prompt staging only. Later phases never download R. pub fn initialize<'arena, 'loan>( &mut self, - mut lease: OriginalPromptRows<'arena, 'loan, 'ctx>, + lease: OriginalPromptRows<'arena, 'loan, 'ctx>, workspace: &mut NativeBatchWorkspace<'ctx>, streams: &[f32], embedding: &[f32], @@ -673,6 +673,49 @@ impl<'ctx> OriginalPromptDevice<'ctx> { } let s = encode(streams)?; let e = encode(embedding)?; + self.upload_initial(lease, workspace, s, e) + } + /// `initialize` over the rows in place: each row's 10240 residual-stream values and 2560 PLE embedding + /// values, in row order. The same finite refusal and the same device bytes, staged in one pass per buffer + /// instead of a caller concatenation, a finite scan and a byte conversion. + pub fn initialize_rows<'arena, 'loan>( + &mut self, + lease: OriginalPromptRows<'arena, 'loan, 'ctx>, + workspace: &mut NativeBatchWorkspace<'ctx>, + rows: &[(&[f32], &[f32])], + ) -> Result> { + self.validate(&lease, workspace)?; + let t = lease.rows(); + if rows.len() != t || rows.iter().any(|(s, e)| s.len() != 10240 || e.len() != 2560) { + return Err(Error::InvalidQwenShape); + } + let mut s = Vec::new(); + s.try_reserve_exact(t * 10240 * 4).map_err(|_| Error::HostAllocation)?; + let mut e = Vec::new(); + e.try_reserve_exact(t * 2560 * 4).map_err(|_| Error::HostAllocation)?; + let mut finite = true; + for (streams, embedding) in rows { + for x in streams.iter() { + finite &= x.is_finite(); + s.extend_from_slice(&x.to_le_bytes()); + } + for x in embedding.iter() { + finite &= x.is_finite(); + e.extend_from_slice(&x.to_le_bytes()); + } + } + if !finite { + return Err(Error::NonFiniteMmqOutput("Original prompt input")); + } + self.upload_initial(lease, workspace, s, e) + } + fn upload_initial<'arena, 'loan>( + &mut self, + mut lease: OriginalPromptRows<'arena, 'loan, 'ctx>, + workspace: &mut NativeBatchWorkspace<'ctx>, + s: Vec, + e: Vec, + ) -> Result> { let rh = Self::span(&lease, S::R, 40960)?; let eh = Self::span(&lease, S::PleEmbedding, 10240)?; let guard = match self.context.enter() { diff --git a/crates/leleka-runtime/src/qwen_forward/prompt_device_rows.rs b/crates/leleka-runtime/src/qwen_forward/prompt_device_rows.rs index b0751bfc..9119e4b2 100644 --- a/crates/leleka-runtime/src/qwen_forward/prompt_device_rows.rs +++ b/crates/leleka-runtime/src/qwen_forward/prompt_device_rows.rs @@ -193,11 +193,11 @@ impl<'source,'arena,'loan,'ctx> PromptDeviceRows<'source,'arena,'loan,'ctx>{ let sources=Sources{binding:binding.identity(),context,rows,role:GrRole::Attention(0),validated:0};sources.validate(binding,cache)?; // Only initial real source staging: Original also uploads initial R // and PLE embeddings. Later layer phases retain this same device R. - let mut streams=Vec::new();streams.try_reserve_exact(sources.rows.len()*10240).map_err(|_|Error::InputAllocation)?; - let mut embedding=Vec::new();embedding.try_reserve_exact(sources.rows.len()*2560).map_err(|_|Error::InputAllocation)?; - for s in &sources.rows{streams.extend_from_slice(s.input.streams().values());embedding.extend_from_slice(s.input.ple_embedding());} + // The driver stages every row's streams and PLE embedding in place (one pass, same bytes and refusals). + let mut slices=Vec::new();slices.try_reserve_exact(sources.rows.len()).map_err(|_|Error::InputAllocation)?; + for s in &sources.rows{slices.push((s.input.streams().values(),s.input.ple_embedding()));} let result=(||{let mut workspace=cache.native_batch_workspace(binding,context)?.ok_or(Error::IdentityMismatch)?; - producer.initialize(lease,&mut workspace,&streams,&embedding).map_err(micro_driver_error)})(); + producer.initialize_rows(lease,&mut workspace,&slices).map_err(micro_driver_error)})(); if producer.is_failed(){cache.transport_failure_flag(binding,context)?.set(true)} let device=result?;sources.validate(binding,cache)?; if let Some(phase)=sources.original_phase()?{phase.initialize(sources.rows.len())?;}