prefix cache: the decode-end checkpoint leaves its request's prompt-end checkpoint standing
A client that resends the history without the reply's reasoning continues from the prompt end; superseding it by the decode-end tail made every such turn resume from the last grid point and re-read up to a block of tokens (4.8k-5.6k per turn in the 256k session). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
cb32ebdd01
commit
2cb26cc333
@@ -566,19 +566,26 @@ impl<N: ByteSize, B: ByteSize> PrefixStore<N, B> {
|
||||
if candidate.tail_len != 0 {
|
||||
return Err(Reject::Mismatch);
|
||||
}
|
||||
self.insert(candidate, tokens, state, blocks, None)
|
||||
self.insert(candidate, tokens, state, blocks, None, None)
|
||||
}
|
||||
|
||||
/// `commit` of a tail node: `tail` holds the cells of the tokens after the last full block. Tail nodes of the same history that
|
||||
/// end earlier (a token-wise prefix of this one) are superseded: the next request of the session starts from this one.
|
||||
pub fn commit_tail(&mut self, candidate: Candidate, tokens: &[i32], state: N, blocks: Vec<(usize, B)>, tail: B) -> Result<(), Reject> {
|
||||
self.commit_tail_keeping(candidate, tokens, state, blocks, tail, None)
|
||||
}
|
||||
|
||||
/// `commit_tail` that leaves the tail node ending at `keep_end` tokens standing. The decode-end checkpoint of a request extends
|
||||
/// the same request's prompt-end checkpoint; when the client resends the history without the reply's reasoning, the next prompt
|
||||
/// shares only the prompt part, so the prompt-end node must survive (the next request's own tails supersede it as usual).
|
||||
pub fn commit_tail_keeping(&mut self, candidate: Candidate, tokens: &[i32], state: N, blocks: Vec<(usize, B)>, tail: B, keep_end: Option<usize>) -> Result<(), Reject> {
|
||||
if candidate.tail_len == 0 {
|
||||
return Err(Reject::Mismatch);
|
||||
}
|
||||
self.insert(candidate, tokens, state, blocks, Some(tail))
|
||||
self.insert(candidate, tokens, state, blocks, Some(tail), keep_end)
|
||||
}
|
||||
|
||||
fn insert(&mut self, candidate: Candidate, tokens: &[i32], state: N, blocks: Vec<(usize, B)>, tail: Option<B>) -> Result<(), Reject> {
|
||||
fn insert(&mut self, candidate: Candidate, tokens: &[i32], state: N, blocks: Vec<(usize, B)>, tail: Option<B>, keep_end: Option<usize>) -> Result<(), Reject> {
|
||||
let g = self.cfg.block_tokens;
|
||||
if tokens.len() != candidate.path.len() * g + candidate.tail_len
|
||||
|| blocks.len() != candidate.missing.len()
|
||||
@@ -601,7 +608,7 @@ impl<N: ByteSize, B: ByteSize> PrefixStore<N, B> {
|
||||
None => return Err(Reject::Mismatch),
|
||||
}
|
||||
}
|
||||
let superseded = if candidate.tail_len > 0 { self.superseded_tails(&candidate.path, tokens) } else { Vec::new() };
|
||||
let superseded = if candidate.tail_len > 0 { self.superseded_tails(&candidate.path, tokens, keep_end) } else { Vec::new() };
|
||||
let tail = tail.map(|payload| Tail { tokens: tokens[tokens.len() - candidate.tail_len..].into(), payload });
|
||||
let bytes = state.byte_size() + tail.as_ref().map_or(0, |t| t.payload.byte_size());
|
||||
self.node_bytes += bytes;
|
||||
@@ -633,7 +640,7 @@ impl<N: ByteSize, B: ByteSize> PrefixStore<N, B> {
|
||||
}
|
||||
|
||||
/// Tail nodes whose tokens are a strict prefix of `tokens` (the history this tail node continues).
|
||||
fn superseded_tails(&self, path: &[u64], tokens: &[i32]) -> Vec<u64> {
|
||||
fn superseded_tails(&self, path: &[u64], tokens: &[i32], keep_end: Option<usize>) -> Vec<u64> {
|
||||
let g = self.cfg.block_tokens;
|
||||
let mut out = Vec::new();
|
||||
for (j, parent) in path.iter().enumerate() {
|
||||
@@ -641,7 +648,7 @@ impl<N: ByteSize, B: ByteSize> PrefixStore<N, B> {
|
||||
for key in self.tails.get(parent).into_iter().flatten() {
|
||||
let Some(tail) = self.nodes.get(key).and_then(|n| n.tail.as_ref()) else { continue };
|
||||
let end = start + tail.tokens.len();
|
||||
if end < tokens.len() && tokens[start..end] == *tail.tokens {
|
||||
if end < tokens.len() && Some(end) != keep_end && tokens[start..end] == *tail.tokens {
|
||||
out.push(*key);
|
||||
}
|
||||
}
|
||||
@@ -882,6 +889,40 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Insert a tail node that leaves the tail ending at `keep` standing (the decode-end checkpoint of a request).
|
||||
fn put_tail_keeping(s: &mut PrefixStore<Payload, Payload>, t: &[i32], len: usize, keep: usize) {
|
||||
let prefix = &t[..len];
|
||||
match s.consider_tail(prefix, true, STATE, BLOCK) {
|
||||
Consider::Admit(c) => {
|
||||
let blocks_in: Vec<(usize, Payload)> = c.missing.iter().map(|j| (*j, Payload(BLOCK))).collect();
|
||||
s.commit_tail_keeping(c, prefix, Payload(STATE), blocks_in, Payload(BLOCK / 2), Some(keep)).unwrap();
|
||||
s.check_invariants();
|
||||
}
|
||||
other => panic!("not admitted: {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_prompt_end_checkpoint_survives_the_decode_end_checkpoint_of_its_request() {
|
||||
let mut s = store(100_000);
|
||||
let a = tokens(1, 2, 8);
|
||||
put(&mut s, &a, 3, true);
|
||||
// the prompt ended 5 tokens after the third grid point, the decode added 4 more
|
||||
assert_eq!(put_tail(&mut s, &a, 3 * G + 5), Consider::Exists);
|
||||
let before = s.node_count();
|
||||
put_tail_keeping(&mut s, &a, 3 * G + 9, 3 * G + 5);
|
||||
assert_eq!(s.node_count(), before + 1, "the decode end is added and the prompt end stays");
|
||||
// the next prompt resends the prompt but not the reply: it continues from the prompt end
|
||||
let mut next = a[..3 * G + 5].to_vec();
|
||||
next.extend_from_slice(&[900, 901, 902]);
|
||||
let m = s.longest_match(&next, next.len()).unwrap();
|
||||
assert_eq!(m.tokens, 3 * G + 5);
|
||||
// the next request's own prompt-end checkpoint supersedes the earlier prompt end as before
|
||||
assert_eq!(put_tail(&mut s, &next, 3 * G + 7), Consider::Exists);
|
||||
let m = s.longest_match(&next, next.len()).unwrap();
|
||||
assert_eq!(m.tokens, 3 * G + 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_checkpoint_between_two_grid_points_is_matched_exactly_and_supersedes_the_one_before_it() {
|
||||
let mut s = store(100_000);
|
||||
|
||||
@@ -464,7 +464,7 @@ impl<S: ByteSize, B: ByteSize> PrefixServe<S, B> {
|
||||
return Ok(ChunkOutcome::Nothing);
|
||||
}
|
||||
let end = prompt.len().saturating_sub(1);
|
||||
self.admit_tail(device, &prompt[..end])
|
||||
self.admit_tail(device, &prompt[..end], None)
|
||||
}
|
||||
|
||||
/// The end of the decode: the session stands at `consumed.len()` (the prompt and the committed reply), settled, with the draft
|
||||
@@ -473,7 +473,10 @@ impl<S: ByteSize, B: ByteSize> PrefixServe<S, B> {
|
||||
if self.cfg.tail != TailMode::Reply || self.bypass || self.req.is_none() {
|
||||
return Ok(ChunkOutcome::Nothing);
|
||||
}
|
||||
let outcome = self.admit_tail(device, consumed)?;
|
||||
// the prompt-end checkpoint of this request stays: a client that resends the history without the reasoning of the reply
|
||||
// continues from the prompt, not from the decode end
|
||||
let keep = self.req.as_ref().map(|r| r.limit);
|
||||
let outcome = self.admit_tail(device, consumed, keep)?;
|
||||
if matches!(outcome, ChunkOutcome::Admitted { .. } | ChunkOutcome::Exists) {
|
||||
// the buffer holds exactly these cells, and the node has a copy of them
|
||||
self.live.clear();
|
||||
@@ -482,7 +485,7 @@ impl<S: ByteSize, B: ByteSize> PrefixServe<S, B> {
|
||||
Ok(outcome)
|
||||
}
|
||||
|
||||
fn admit_tail<D: PrefixDevice<State = S, Block = B>>(&mut self, device: &mut D, tokens: &[i32]) -> Result<ChunkOutcome, Error> {
|
||||
fn admit_tail<D: PrefixDevice<State = S, Block = B>>(&mut self, device: &mut D, tokens: &[i32], keep_end: Option<usize>) -> Result<ChunkOutcome, Error> {
|
||||
let g = self.block_tokens;
|
||||
// a grid point is a pure checkpoint of its own (and a node below one block has no blocks to hang on)
|
||||
if tokens.len() <= g || tokens.len() % g == 0 || device.position()? != tokens.len() as u64 {
|
||||
@@ -519,7 +522,7 @@ impl<S: ByteSize, B: ByteSize> PrefixServe<S, B> {
|
||||
let new_blocks = blocks.len();
|
||||
let bytes = state.byte_size() + tail.byte_size() + blocks.iter().map(|(_, b)| b.byte_size()).sum::<usize>();
|
||||
let park_ms = started.elapsed().as_secs_f64() * 1e3;
|
||||
self.store.commit_tail(candidate, tokens, state, blocks, tail).map_err(|_| Error::IdentityMismatch)?;
|
||||
self.store.commit_tail_keeping(candidate, tokens, state, blocks, tail, keep_end).map_err(|_| Error::IdentityMismatch)?;
|
||||
self.park_ms_request += park_ms;
|
||||
self.admitted_request += 1;
|
||||
Ok(ChunkOutcome::Admitted { tokens: tokens.len(), new_blocks, bytes, park_ms })
|
||||
|
||||
Reference in New Issue
Block a user