kar ple cache: persistent staging buffers for the scatter's update records instead of a cuMemAlloc and cuMemFree per token
`KarPleCache::gather` (one decode token: 16 rows) allocated a device buffer for the update records of its misses, copied into it, ran the scatter and freed the buffer on every call. cuMemFree also waits for the whole device. With ple.device at 0.046 ms per call and 2.5 calls per round (speedtrain-ab spans) the allocation pair is a measurable part of a 0.116 ms per round span that runs before every window launch. The cache now owns sixteen buffers, `scratch[k - 1]` of `k * 56` bytes, because the scatter's layout names the exact byte length of its update block (a single 16-record buffer would be refused for fewer misses). Output identity argument: the same bytes are copied into a device buffer of the same length before the same kernel call with the same count; the kernel and the decode that follows are unchanged, and every `kar_ple_*` launch is followed by a context synchronize (`execute`), so a reused buffer is never overwritten while the scatter reads it. The decoded rows depend only on the packets. Test: the staging sizes are the record sizes the layout and `gather` use (CPU). Not run on a GPU: covered by the replay identity job. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
8735bbc5d5
commit
c2198a7b9c
@@ -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<DeviceBuffer<'ctx>>,
|
||||
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::<usize>());
|
||||
let sizes: Vec<usize> = (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()]))
|
||||
|
||||
Reference in New Issue
Block a user