diff --git a/crates/leleka-runtime/src/qwen_forward/kar_ple_cache.rs b/crates/leleka-runtime/src/qwen_forward/kar_ple_cache.rs index 5a0e13a6..f15ca5ea 100644 --- a/crates/leleka-runtime/src/qwen_forward/kar_ple_cache.rs +++ b/crates/leleka-runtime/src/qwen_forward/kar_ple_cache.rs @@ -12,6 +12,14 @@ use std::thread::JoinHandle; const CACHE_BYTES: usize = 64 * 1024 * 1024; const SLOTS: u32 = (CACHE_BYTES / 64) as u32; const OUTPUT_BYTES: usize = 16 * 160 * 4; +/// One update record of the scatter: slot (4) + packet payload (50) + partition (2). +const UPDATE_RECORD_BYTES: usize = 56; +/// A token has sixteen rows, so at most sixteen of them miss. +const MAX_MISSES: usize = 16; +/// Bytes of the staging buffers for 1..=16 misses. +fn scratch_bytes() -> usize { + (1..=MAX_MISSES).map(|k| k * UPDATE_RECORD_BYTES).sum() +} #[derive(Clone, Copy)] struct Entry { slot: u32, @@ -351,6 +359,10 @@ pub(crate) struct KarPleCache<'ctx> { books: DeviceBuffer<'ctx>, slots: DeviceBuffer<'ctx>, output: DeviceBuffer<'ctx>, + /// Staging for the scatter's update records, one buffer per miss count (`scratch[k - 1]` holds `k * 56` bytes): the scatter's + /// layout names the exact byte length of its update block, so one buffer of the largest size would be refused, and a buffer + /// allocated and freed on every token costs a `cuMemAlloc` and a `cuMemFree` (which also waits for the whole device). + scratch: Vec>, binding: BindingIdentity, archive: [u8; 32], profile: [u8; 32], @@ -368,7 +380,7 @@ impl<'ctx> KarPleCache<'ctx> { .ok_or(Error::Overflow)?; let needed = CACHE_BYTES .checked_add(book_bytes) - .and_then(|n| n.checked_add(64 + OUTPUT_BYTES + 16 * 56)) + .and_then(|n| n.checked_add(64 + OUTPUT_BYTES + scratch_bytes())) .ok_or(Error::Overflow)?; if context .allocated_bytes() @@ -396,6 +408,17 @@ impl<'ctx> KarPleCache<'ctx> { let output = context .allocate(OUTPUT_BYTES) .map_err(|_| Error::CudaExecution)?; + let mut scratch = Vec::new(); + scratch + .try_reserve_exact(MAX_MISSES) + .map_err(|_| Error::InputAllocation)?; + for misses in 1..=MAX_MISSES { + scratch.push( + context + .allocate(misses * UPDATE_RECORD_BYTES) + .map_err(|_| Error::CudaExecution)?, + ); + } Ok(Self { ahead: Ahead::default(), context, @@ -403,6 +426,7 @@ impl<'ctx> KarPleCache<'ctx> { books, slots, output, + scratch, binding: binding.identity(), archive: *binding.archive_digest(), profile: *binding.profile_digest(), @@ -669,17 +693,17 @@ impl<'ctx> KarPleCache<'ctx> { let _device_span = super::original_serve_profile::span("ple.device"); let completed = (|| { if !updates.is_empty() { - let mut device = self - .context - .allocate(updates.len()) - .map_err(|_| Error::CudaExecution)?; + let device = self + .scratch + .get_mut(misses.len() - 1) + .filter(|b| b.len() == updates.len()) + .ok_or(Error::CudaExecution)?; device .copy_from(&updates) .map_err(|_| Error::CudaExecution)?; self.context - .kar_ple_scatter(&mut self.cache, &device, misses.len() as u32) + .kar_ple_scatter(&mut self.cache, device, misses.len() as u32) .map_err(|_| Error::CudaExecution)?; - device.release().map_err(|_| Error::CudaExecution)?; } self.slots .copy_from(&slot_bytes) @@ -838,6 +862,23 @@ mod slot_tests { entries.insert(*row, miss_slot[i], stamp); } } + /// The staging buffers of the scatter: one per miss count, each exactly the update block the kernel's layout names + /// (`misses * 56` bytes: slot u32, packet payload 50, partition u16), so the persistent buffers are admitted like the former + /// per-call allocation of `updates.len()` bytes. + #[test] + fn the_scatter_staging_buffers_match_the_update_records() { + assert_eq!(UPDATE_RECORD_BYTES, 4 + 50 + 2); + assert_eq!(MAX_MISSES, 16); + assert_eq!(scratch_bytes(), UPDATE_RECORD_BYTES * (1..=16).sum::()); + let sizes: Vec = (1..=MAX_MISSES).map(|k| k * UPDATE_RECORD_BYTES).collect(); + assert_eq!(sizes[0], 56); + assert_eq!(sizes[15], 16 * 56); + // `gather` builds `updates` as 56 bytes per miss, so `scratch[misses - 1]` is the buffer of exactly that length + for misses in 1..=16usize { + assert_eq!(sizes[misses - 1], misses * (4 + 50 + 2)); + } + } + fn chunk(rows: &[u64]) -> Vec<[u64; 16]> { rows.chunks(16) .map(|c| std::array::from_fn(|i| c[i % c.len()]))