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 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-10-16 17:31:29 +02:00
co-authored by Claude Opus 5.5
parent 4780170fe3
commit 84983abdb2
2 changed files with 48 additions and 5 deletions
+44 -1
View File
@@ -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<OriginalDeviceResidual<'arena, 'loan, 'ctx>> {
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<u8>,
e: Vec<u8>,
) -> Result<OriginalDeviceResidual<'arena, 'loan, 'ctx>> {
let rh = Self::span(&lease, S::R, 40960)?;
let eh = Self::span(&lease, S::PleEmbedding, 10240)?;
let guard = match self.context.enter() {
@@ -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())?;}