merge: claude/long-context-fix (draft-ring publication across prefill chunks; 40000-token prompt passes, streamed == resident)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-10-16 12:41:45 +02:00
co-authored by Claude Opus 5.5
2 changed files with 67 additions and 3 deletions
@@ -155,14 +155,20 @@ impl<'ctx> OriginalDraftSession<'ctx>{
let ring=self.ring.as_ref().ok_or(Error::IdentityMismatch)?;
completed.validate_current(ring).map_err(draft_driver_error)?;
let end=usize::try_from(txn.end_position()).map_err(|_|Error::Overflow)?;
let start=usize::try_from(txn.start_position()).map_err(|_|Error::Overflow)?;
let first_materialized=published_first_materialized(self.first_materialized,self.snapshot.next_position(),
start,completed.first_materialized(),end);
if completed.next_position()!=end||end==0
||completed.first_materialized()>end.saturating_sub(leleka_cuda_driver::MTP_ORIGINAL_WINDOW)
||first_materialized>end.saturating_sub(leleka_cuda_driver::MTP_ORIGINAL_WINDOW)
||!completed.owner_id().same_owner(&self.snapshot.owner_id()){
crate::bringup_trace!("DBG draft.prepare_publication refused: start={start} end={end} completed_next={} completed_first={} prior_first={} prior_next={} lower={first_materialized} same_owner={}",
completed.next_position(),completed.first_materialized(),self.first_materialized,self.snapshot.next_position(),
completed.owner_id().same_owner(&self.snapshot.owner_id()));
return Err(Error::IdentityMismatch);
}
let snapshot=completed.snapshot_after(end-1).map_err(draft_driver_error)?;
ring.validate_snapshot(&snapshot).map_err(draft_driver_error)?;
Ok(OriginalDraftPublication{owner:self,snapshot,first_materialized:completed.first_materialized()})
Ok(OriginalDraftPublication{owner:self,snapshot,first_materialized})
}
/// All physical restores are attempted by the coupled target owner. An
/// uncertain ring restore poisons this owner even when target rollback works.
@@ -248,6 +254,52 @@ impl OriginalDraftPublication<'_, '_>{
}
}
impl Drop for OriginalDraftSession<'_>{fn drop(&mut self){let _=self.close_inner();}}
/// The ring's lower materialized bound after a prompt chunk `[start, end)` whose recording materialized
/// `[chunk_first, end)`. The driver reports the chunk's own bound (`cell0 + r0`); when the chunk starts where
/// the ring's materialized range already reaches (`prior_next >= start`) and recorded from its first row, the
/// range simply continues and keeps the earlier bound. A chunk that skipped its leading rows (the Original's
/// `first_needed` cut) starts a new range at its own bound. Taking the chunk's bound alone made every chunk
/// after the first look as if `[0, start)` had never been recorded, so the second chunk of every multi-chunk
/// prompt was refused (`first_materialized > end - MTP_ORIGINAL_WINDOW`).
fn published_first_materialized(prior_first:usize,prior_next:usize,start:usize,chunk_first:usize,end:usize)->usize{
if chunk_first==start&&prior_next>=start&&prior_first<=start&&start<end {prior_first} else {chunk_first}
}
#[cfg(test)]
mod first_materialized_tests{
use super::published_first_materialized as lower;
use leleka_cuda_driver::MTP_ORIGINAL_WINDOW as W;
/// Walk a whole prompt in chunks the way `prefill_actions` does and check every publication's bound.
fn walk(prompt:usize,chunk:usize)->Vec<(usize,usize)>{
let (mut first,mut next)=(0usize,0usize);let mut out=Vec::new();
let mut start=0;
while start<prompt-1 {let end=(start+chunk).min(prompt-1);
// with_pending_ring: first_needed = end - window; the driver records from max(first_needed, start)
let chunk_first=end.saturating_sub(W).max(start);
first=lower(first,next,start,chunk_first,end);next=end;out.push((first,end));start=end;}
out
}
#[test]
fn contiguous_chunks_keep_the_rings_lower_bound(){
// the 8000-token prompt of job 311: four 2048-row chunks
for (first,end) in walk(8000,2048) {assert_eq!(first,0);assert!(first<=end.saturating_sub(W));}
// the 40000-token prompt of job 310: twenty chunks, past the 32768-cell window
for (first,end) in walk(40000,2048) {assert!(first<=end.saturating_sub(W),"{first} {end}");}
assert_eq!(lower(0,2048,2048,2048,4096),0);
}
#[test]
fn a_chunk_that_skipped_rows_or_left_a_gap_starts_a_new_range(){
assert_eq!(lower(0,2048,2048,2100,4096),2100); // leading rows skipped
assert_eq!(lower(0,1024,2048,2048,4096),2048); // the ring did not reach the chunk's start
assert_eq!(lower(0,0,0,0,2048),0); // first chunk
}
#[test]
fn the_old_rule_refused_the_second_chunk(){
// what publication checked before: the chunk's own bound against the window
let (start,end)=(2048usize,4096usize);
assert!(start>end.saturating_sub(W));
assert!(lower(0,start,start,start,end)<=end.saturating_sub(W));
}
}
fn draft_driver_error(error:leleka_cuda_driver::Error)->Error{match error{
leleka_cuda_driver::Error::ContextMismatch=>Error::IdentityMismatch,
leleka_cuda_driver::Error::InvalidQwenShape|leleka_cuda_driver::Error::InvalidSize=>Error::InvalidDimension,
+13 -1
View File
@@ -65,9 +65,21 @@ pages and `page_table[block]` is the slot or -1.
`LELEKA_ORIGINAL_SERVE_MAX_CONTEXT=65536`, plus the usual model inputs. Compare the `long-smoke output ids` line
of a 20480 run with a 65536 run (no eviction): they must be identical.
* Real-model result 2026-10-16 (job 312, RTX 5090, branch `claude/long-context-fix` 791a4fa, budget 24 GiB,
capacity 2048, 40000-token prompt, greedy, 32 new tokens): both windows pass and print identical output ids
(20480 cells: 5120 pages/layer, 0.246 GiB VRAM; 65536 cells: no eviction, 0.780 GiB VRAM); 12 rounds,
~133 s per request after load. `needle_found=false` only because 32 tokens end inside the model's `<think>`
preamble (the text is already quoting the prompt's opening line); this is a correctness smoke, not a quality
or speed result.
* Jobs 310/311 had failed before that fix (`Failed(IdentityMismatch)`) at the publication of the SECOND
batched prefill chunk of any prompt longer than one chunk: `OriginalDraftSession::prepare_publication`
(`session_original_draft.rs`) checked the driver's per-chunk `first_materialized` (the chunk's start) against
`end - MTP_ORIGINAL_WINDOW`. Unrelated to KV streaming; fixed in 791a4fa with unit tests.
## Open
* The real-model long-context run and its A/B against the non-evicting window.
* A longer generation (or a no-think prompt) to read the passcode answer; parity against the Original on the
same ids.
* The freed VRAM is not yet handed to GPU expert slots (expert residency is ported separately); the prefill
chunk does not follow the Original's slot-derived ladder.
* Counters are not printed at the end of a request (the Original's `kv_stream` summary line).