cuda-driver: chunked persistent page list for MTP ring snapshots (the per-round snapshot copied every page Rc)
finish_original_graph_mutation built the next snapshot's page vector by cloning one Rc per page below the first changed one: 65,536 clones at 256k per round, and the same number of decrements when the previous snapshot was dropped. Pages are now held in chunks of 256 behind Rc; a new snapshot shares every whole chunk and copies at most one partial chunk plus the new pages, so the per-round cost no longer grows with the context. Contents and lineage identical (unit tests: get/iter equal the vector, prefix pointer-identical, whole chunks shared). Not yet measured on the 5090. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
c84fcdcdd5
commit
ee4f261775
@@ -89,9 +89,61 @@ fn snapshot_memo() -> bool {
|
||||
struct Page {
|
||||
runs: [Box<[u8]>; 4],
|
||||
}
|
||||
/// The immutable page list of a snapshot, in chunks of `PAGES_PER_CHUNK` so that the next snapshot of a speculation round shares every
|
||||
/// chunk the round did not touch: the page list is O(context / 4) long (65,536 pages at 256k) and the round changes its last few entries only.
|
||||
#[derive(Clone)]
|
||||
struct Pages {
|
||||
chunks: Rc<Vec<Rc<Vec<Rc<Page>>>>>,
|
||||
len: usize,
|
||||
}
|
||||
const PAGES_PER_CHUNK: usize = 256;
|
||||
impl Pages {
|
||||
fn empty() -> Self {
|
||||
Self { chunks: Rc::new(Vec::new()), len: 0 }
|
||||
}
|
||||
fn from_vec(pages: Vec<Rc<Page>>) -> Self {
|
||||
let len = pages.len();
|
||||
let chunks = pages.chunks(PAGES_PER_CHUNK).map(|c| Rc::new(c.to_vec())).collect();
|
||||
Self { chunks: Rc::new(chunks), len }
|
||||
}
|
||||
/// The first `keep` pages of `self` (`keep <= len`), then `tail`. Whole chunks inside `keep` are shared, not copied.
|
||||
fn with_tail(&self, keep: usize, tail: Vec<Rc<Page>>) -> Self {
|
||||
let keep = keep.min(self.len);
|
||||
let whole = keep / PAGES_PER_CHUNK;
|
||||
let mut chunks: Vec<Rc<Vec<Rc<Page>>>> = Vec::with_capacity(whole + 1 + tail.len() / PAGES_PER_CHUNK);
|
||||
chunks.extend(self.chunks[..whole].iter().cloned());
|
||||
let mut open: Vec<Rc<Page>> = Vec::with_capacity(PAGES_PER_CHUNK);
|
||||
if keep % PAGES_PER_CHUNK != 0 {
|
||||
open.extend(self.chunks[whole][..keep % PAGES_PER_CHUNK].iter().cloned());
|
||||
}
|
||||
let len = keep + tail.len();
|
||||
for page in tail {
|
||||
open.push(page);
|
||||
if open.len() == PAGES_PER_CHUNK {
|
||||
chunks.push(Rc::new(std::mem::replace(&mut open, Vec::with_capacity(PAGES_PER_CHUNK))));
|
||||
}
|
||||
}
|
||||
if !open.is_empty() {
|
||||
chunks.push(Rc::new(open));
|
||||
}
|
||||
Self { chunks: Rc::new(chunks), len }
|
||||
}
|
||||
fn len(&self) -> usize {
|
||||
self.len
|
||||
}
|
||||
fn get(&self, p: usize) -> Option<&Rc<Page>> {
|
||||
if p >= self.len {
|
||||
return None;
|
||||
}
|
||||
self.chunks.get(p / PAGES_PER_CHUNK)?.get(p % PAGES_PER_CHUNK)
|
||||
}
|
||||
fn iter(&self) -> impl Iterator<Item = &Rc<Page>> {
|
||||
self.chunks.iter().flat_map(|c| c.iter())
|
||||
}
|
||||
}
|
||||
struct Snapshot {
|
||||
origin: Rc<Origin>,
|
||||
pages: Rc<Box<[Rc<Page>]>>,
|
||||
pages: Pages,
|
||||
end: usize,
|
||||
/// The pages up to `end` were checked to have the exact run sizes (`validate_snapshot`). The snapshot is immutable, so the
|
||||
/// answer cannot change; the scan is O(context / 4) and ran several times per speculation round.
|
||||
@@ -766,7 +818,7 @@ impl<'ctx> MtpOriginalInt8<'ctx> {
|
||||
self.validate()?;
|
||||
Ok(MtpInt8Snapshot(Rc::new(Snapshot {
|
||||
origin: self.origin.clone(),
|
||||
pages: Rc::new(Vec::new().into_boxed_slice()),
|
||||
pages: Pages::empty(),
|
||||
end: 0,
|
||||
checked: std::cell::Cell::new(true),
|
||||
})))
|
||||
@@ -1241,7 +1293,7 @@ impl<'ctx> MtpOriginalInt8<'ctx> {
|
||||
}
|
||||
let snapshot = MtpInt8Snapshot(Rc::new(Snapshot {
|
||||
origin: self.origin.clone(),
|
||||
pages: Rc::new(pages.into_boxed_slice()),
|
||||
pages: Pages::from_vec(pages),
|
||||
end,
|
||||
checked: std::cell::Cell::new(false),
|
||||
}));
|
||||
@@ -1582,3 +1634,53 @@ impl<'ctx> MtpOriginalInt8<'ctx> {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod pages_tests {
|
||||
use super::*;
|
||||
|
||||
fn page(tag: u8) -> Rc<Page> {
|
||||
Rc::new(Page { runs: std::array::from_fn(|_| vec![tag].into_boxed_slice()) })
|
||||
}
|
||||
fn tags(pages: &Pages) -> Vec<u8> {
|
||||
pages.iter().map(|p| p.runs[0][0]).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_page_list_reads_like_the_vector_it_was_built_from() {
|
||||
for n in [0usize, 1, 255, 256, 257, 511, 512, 513, 1000] {
|
||||
let v: Vec<Rc<Page>> = (0..n).map(|i| page(i as u8)).collect();
|
||||
let pages = Pages::from_vec(v.clone());
|
||||
assert_eq!(pages.len(), n);
|
||||
assert_eq!(tags(&pages), (0..n).map(|i| i as u8).collect::<Vec<_>>());
|
||||
for i in 0..n {
|
||||
assert!(Rc::ptr_eq(pages.get(i).unwrap(), &v[i]));
|
||||
}
|
||||
assert!(pages.get(n).is_none());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn with_tail_keeps_the_prefix_replaces_the_rest_and_shares_whole_chunks() {
|
||||
let v: Vec<Rc<Page>> = (0..1000).map(|i| page(i as u8)).collect();
|
||||
let base = Pages::from_vec(v.clone());
|
||||
for keep in [0usize, 1, 255, 256, 257, 512, 700, 999, 1000] {
|
||||
let tail: Vec<Rc<Page>> = (0..5).map(|i| page(200 + i)).collect();
|
||||
let next = base.with_tail(keep, tail.clone());
|
||||
assert_eq!(next.len(), keep + 5);
|
||||
for i in 0..keep {
|
||||
assert!(Rc::ptr_eq(next.get(i).unwrap(), &v[i]), "keep {keep} page {i}");
|
||||
}
|
||||
for (i, t) in tail.iter().enumerate() {
|
||||
assert!(Rc::ptr_eq(next.get(keep + i).unwrap(), t));
|
||||
}
|
||||
assert!(next.get(keep + 5).is_none());
|
||||
for c in 0..keep / PAGES_PER_CHUNK {
|
||||
assert!(Rc::ptr_eq(&next.chunks[c], &base.chunks[c]), "chunk {c} shared at keep {keep}");
|
||||
}
|
||||
}
|
||||
// a base is unchanged by the snapshots made from it
|
||||
assert_eq!(base.len(), 1000);
|
||||
assert_eq!(tags(&base), (0..1000).map(|i| i as u8).collect::<Vec<_>>());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,27 +172,26 @@ impl MtpOriginalInt8<'_> {
|
||||
}
|
||||
let page0 = first / 4;
|
||||
let page1 = end.div_ceil(4);
|
||||
let mut pages = Vec::new();
|
||||
pages
|
||||
.try_reserve_exact(page1)
|
||||
if page0 > prior.0.pages.len() {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
// The leading pages are shared with the prior (whole chunks, no per-page work); only the pages from `page0` on are read back.
|
||||
let mut tail = Vec::new();
|
||||
tail.try_reserve_exact(page1.saturating_sub(page0))
|
||||
.map_err(|_| Error::HostAllocation)?;
|
||||
for p in 0..page1 {
|
||||
if p < page0 {
|
||||
pages.push(prior.0.pages.get(p).ok_or(Error::InvalidQwenShape)?.clone());
|
||||
continue;
|
||||
}
|
||||
for p in page0..page1 {
|
||||
let mut runs: [Vec<u8>; 4] = std::array::from_fn(|_| Vec::new());
|
||||
for (i, n) in PAGE_BYTES.into_iter().enumerate() {
|
||||
runs[i] = zero_bytes(n)?;
|
||||
}
|
||||
self.read_encoded_page(p, &mut runs)?;
|
||||
pages.push(Rc::new(Page {
|
||||
tail.push(Rc::new(Page {
|
||||
runs: runs.map(Vec::into_boxed_slice),
|
||||
}));
|
||||
}
|
||||
let snapshot = MtpInt8Snapshot(Rc::new(Snapshot {
|
||||
origin: self.origin.clone(),
|
||||
pages: Rc::new(pages.into_boxed_slice()),
|
||||
pages: prior.0.pages.with_tail(page0, tail),
|
||||
end,
|
||||
// the leading pages are the checked prior's (`validate_snapshot` above), the others were just sized by `zero_bytes`
|
||||
checked: std::cell::Cell::new(true),
|
||||
|
||||
@@ -1527,7 +1527,7 @@ impl<'ctx> OriginalMtpPrefill<'ctx> {
|
||||
}
|
||||
let snapshot = MtpInt8Snapshot(Rc::new(Snapshot {
|
||||
origin: ring.origin.clone(),
|
||||
pages: Rc::new(pages.into_boxed_slice()),
|
||||
pages: Pages::from_vec(pages),
|
||||
end,
|
||||
checked: std::cell::Cell::new(false),
|
||||
}));
|
||||
|
||||
@@ -1355,7 +1355,7 @@ impl<'ctx> OriginalMtpScalarPrefill<'ctx> {
|
||||
}
|
||||
let snapshot = MtpInt8Snapshot(Rc::new(Snapshot {
|
||||
origin: ring.origin.clone(),
|
||||
pages: Rc::new(pages.into_boxed_slice()),
|
||||
pages: Pages::from_vec(pages),
|
||||
end,
|
||||
checked: std::cell::Cell::new(false),
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user