Tests: PLE rows are matched by row, never by position (plan 0.4; upstream 36381540)
Upstream's gather_batch compared its prefetch list with the batch by position and re-read reordered or duplicated rows. Our reader has no such matching (tickets own their output slots, the row cache and the KAR GPU cache are keyed by row id); the tests pin it with the upstream cases and with chunks that are permuted, shifted, repeated or partly new. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
6be35e9752
commit
c159a7fdee
@@ -679,6 +679,51 @@ mod slot_tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Upstream 41cf05a0 `36381540` (`gather_batch` matched the prefetched rows by position): here rows are looked up by row
|
||||
/// id, so a reordered, shifted, repeated or partly new chunk finds each row in the slot it was given, and a hit never moves.
|
||||
#[test]
|
||||
fn rows_are_matched_by_row_id_never_by_position_in_the_chunk() {
|
||||
let mut entries = RowMap::<Entry>::default();
|
||||
let first: Vec<u64> = (1..=32).map(|r| r * 1000).collect();
|
||||
let lookup = lookup_rows(&mut entries, &chunk(&first), 1).unwrap();
|
||||
let (slot, victims, _) = assign_slots(&entries, 0, 64, 1, lookup.miss_rows.len()).unwrap();
|
||||
apply(&mut entries, &lookup, &slot, &victims, 1);
|
||||
let given: std::collections::HashMap<u64, u32> = entries.iter().map(|(r, e)| (*r, e.slot)).collect();
|
||||
let slot_of = |row: u64| given[&row];
|
||||
let expect = |rows: &[u64]| -> Vec<u32> { rows.iter().map(|&r| slot_of(r)).collect() };
|
||||
let flat = |lookup: &Lookup| lookup.slots.clone();
|
||||
// a permutation (halves swapped), a reversal, a shift by one position, a repeat of one row and a subset
|
||||
let mut swapped = first.clone();
|
||||
swapped.rotate_left(16);
|
||||
let mut reversed = first.clone();
|
||||
reversed.reverse();
|
||||
let mut shifted = first.clone();
|
||||
shifted.rotate_left(1);
|
||||
for (name, rows) in [
|
||||
("swapped", swapped),
|
||||
("reversed", reversed),
|
||||
("shifted", shifted),
|
||||
("one row repeated", vec![first[5]; 16]),
|
||||
("subset", (0..16).map(|i| first[(i * 7) % 32]).collect()),
|
||||
] {
|
||||
let lookup = lookup_rows(&mut entries, &chunk(&rows), 2).unwrap();
|
||||
assert!(lookup.miss_rows.is_empty() && lookup.pending.is_empty(), "{name}: a cached row is a miss");
|
||||
assert_eq!(flat(&lookup), expect(&rows), "{name}: a position got another row's slot");
|
||||
}
|
||||
// a chunk of known and new rows: the known keep their slots wherever they stand, each new row is one miss however often it repeats
|
||||
let mixed: Vec<u64> = vec![first[3], 77_001, first[0], 77_001, 77_002, first[3], first[31], 77_002];
|
||||
let lookup = lookup_rows(&mut entries, &chunk(&mixed), 3).unwrap();
|
||||
assert_eq!(lookup.miss_rows, vec![77_001, 77_002]);
|
||||
for (at, &row) in mixed.iter().enumerate() {
|
||||
let want = if row >= 77_000 { u32::MAX } else { slot_of(row) };
|
||||
// `chunk` pads a short chunk by repeating its rows: the first `mixed.len()` positions are the rows themselves
|
||||
assert_eq!(lookup.slots[at], want, "mixed position {at}");
|
||||
}
|
||||
// 77_001 stands at 2 of the 8 rows and 77_002 at 2: with the padding of the chunk, 4 positions name each
|
||||
let named = |m: u32| lookup.pending.iter().filter(|(_, miss)| *miss == m).count();
|
||||
assert_eq!((named(0), named(1)), (4, 4));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_chunk_that_needs_more_slots_than_the_untouched_rows_hold_is_refused() {
|
||||
let mut entries = RowMap::<Entry>::default();
|
||||
|
||||
@@ -557,3 +557,90 @@ fn probe_reads_an_aligned_page_and_reports_failures() {
|
||||
assert!(!probe_aligned_read(src.as_ref(), 0));
|
||||
assert!(!probe_aligned_read(Mem::new(vec![]).as_ref(), 0));
|
||||
}
|
||||
|
||||
/// Upstream 41cf05a0 `36381540` (`ngram/ple_reader_test.cpp` `prefetched_keys_are_reused`, issue #2622): `gather_batch` matched
|
||||
/// the prefetched tokens by position, so a prefetch of A..F gathered as D,E,F,A,B,C (or A gathered as A,A,A) read rows that were
|
||||
/// already in flight a second time. Here a ticket owns its rows and the row cache is keyed by row, so there is no position to
|
||||
/// match; this pins that: every output slot holds the bytes of ITS row whatever the order, duplication or overlap of the tickets
|
||||
/// in flight, and a row already fetched is not read again.
|
||||
#[test]
|
||||
fn prefetched_keys_are_matched_by_row_never_by_position() {
|
||||
struct Case {
|
||||
name: &'static str,
|
||||
pre: Vec<u32>,
|
||||
want: Vec<u32>,
|
||||
/// every wanted row is among the prefetched ones: with the cache on, it needs no read of its own
|
||||
covered: bool,
|
||||
}
|
||||
// rows 2 pages apart (90-byte rows: 45 to a page) so that no two rows share a page and reads count rows
|
||||
let row = |i: u32| i * 90;
|
||||
let seventeen: Vec<u32> = (0..17).map(row).collect();
|
||||
let cases = vec![
|
||||
Case { name: "ordered", pre: vec![row(0), row(1)], want: vec![row(0), row(1)], covered: true },
|
||||
Case {
|
||||
name: "permutation",
|
||||
pre: (0..6).map(row).collect(),
|
||||
want: [3, 4, 5, 0, 1, 2].map(row).to_vec(),
|
||||
covered: true,
|
||||
},
|
||||
Case { name: "subset", pre: (0..6).map(row).collect(), want: vec![row(4), row(1)], covered: true },
|
||||
Case { name: "duplicates", pre: vec![row(0)], want: vec![row(0), row(0), row(0)], covered: true },
|
||||
Case { name: "duplicate prefetch", pre: vec![row(0), row(0), row(1)], want: vec![row(0), row(1)], covered: true },
|
||||
Case { name: "missing key", pre: vec![row(0), row(1)], want: vec![row(0), row(2)], covered: false },
|
||||
Case { name: "extra prefetch", pre: (0..3).map(row).collect(), want: vec![row(1)], covered: true },
|
||||
Case { name: "none", pre: vec![row(0)], want: vec![], covered: true },
|
||||
Case { name: "over the window", pre: vec![row(0), row(1)], want: seventeen.clone(), covered: false },
|
||||
Case { name: "window overflow", pre: seventeen, want: vec![row(16)], covered: true },
|
||||
Case { name: "out of range among them", pre: vec![row(1), 9_999_999], want: vec![9_999_999, row(1)], covered: false },
|
||||
];
|
||||
for cache_rows in [0u64, 8192] {
|
||||
for io_threads in [Some(0usize), None, Some(3)] {
|
||||
for case in &cases {
|
||||
let c = {
|
||||
let mut c = cfg(100, 2000 * 90);
|
||||
c.cache_rows = cache_rows;
|
||||
c.io_threads = io_threads;
|
||||
c
|
||||
};
|
||||
let tag = format!("{} (cache {cache_rows}, io {io_threads:?})", case.name);
|
||||
let src = Mem::new(table(100, 2000 * 90, 90));
|
||||
let reader = PleReader::open(src.clone(), c.clone()).unwrap();
|
||||
// everything below is in flight at once; the wanted ticket is issued last, collected first, the others in
|
||||
// reverse order of issue
|
||||
let tickets: Vec<_> = case.pre.iter().map(|&r| reader.issue(&[r])).collect();
|
||||
let wanted = reader.issue(&case.want);
|
||||
assert_eq!(reader.collect(wanted).unwrap(), oracle(&src.bytes, &c, &case.want), "{tag}: wanted");
|
||||
for (ticket, &r) in tickets.into_iter().zip(&case.pre).rev() {
|
||||
assert_eq!(reader.collect(ticket).unwrap(), oracle(&src.bytes, &c, &[r]), "{tag}: prefetched {r}");
|
||||
}
|
||||
// the same keys again, one batch in the reverse order: slot i is row want[i]
|
||||
let mut rev = case.want.clone();
|
||||
rev.reverse();
|
||||
let before = reader.snapshot();
|
||||
let again = reader.read_rows(&rev).unwrap();
|
||||
assert_eq!(again, oracle(&src.bytes, &c, &rev), "{tag}: reversed");
|
||||
if cache_rows > 0 && !case.want.is_empty() {
|
||||
// every in-range row was fetched above, so none is read again
|
||||
let after = reader.snapshot();
|
||||
let in_range = rev.iter().filter(|&&r| u64::from(r) < c.n_rows).count() as u64;
|
||||
assert_eq!(after.reads, before.reads, "{tag}: a row already fetched is read again");
|
||||
assert_eq!(after.cache_hits - before.cache_hits, in_range, "{tag}: cache hits");
|
||||
}
|
||||
// prefetch collected first, then the wanted rows: a covered batch costs no read when the cache is on
|
||||
let reader = PleReader::open(src.clone(), c.clone()).unwrap();
|
||||
for &r in &case.pre {
|
||||
reader.read_rows(&[r]).unwrap();
|
||||
}
|
||||
let before = reader.snapshot();
|
||||
assert_eq!(reader.read_rows(&case.want).unwrap(), oracle(&src.bytes, &c, &case.want), "{tag}: after prefetch");
|
||||
let after = reader.snapshot();
|
||||
if cache_rows > 0 && case.covered {
|
||||
assert_eq!(after.reads, before.reads, "{tag}: a prefetched row is read again");
|
||||
}
|
||||
// a batch never reads more pages than it has distinct rows
|
||||
let distinct: std::collections::HashSet<_> = case.want.iter().collect();
|
||||
assert!(after.reads - before.reads <= distinct.len() as u64, "{tag}: reads {}", after.reads - before.reads);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ the run-to-run control); **L** live Codex session.
|
||||
| 0.1 | Control-token text in messages becomes real special tokens (`c16f31f8`, `98b7ea94`, `0d613031`) | swap `<think>`, `</think>` and the other control literals in message text (user, tool, system, assistant history, tool-call arguments, tool descriptions) for private-use marks before the template renders; after it encode those spans as plain text (`Tokenizer::encode` plain spans); the template's own markers stay special; same path for token counting | U (injection fixtures), L | **U done** (2026-10-25, native Windows `cargo test`: leleka-tokenizer lib 11 + marked 7 + fast_path 7 + oracle 3, strata-control 259 lib + 33 backend_worker_http, leleka-runtime lib 504, all green; the injection fixtures are in `crates/tokenizer/tests/marked.rs`, `responses.rs` tests and `backend_worker_http.rs`). `leleka_tokenizer::LiteralMarks` maps every type 3/4 literal to a plane-15 private-use mark (a forged mark is escaped); `render_request` marks instructions, message text, reasoning, call arguments, tool outputs, tool descriptions/schemas; the gateway's own `<tool_call>`/`<tool_response>` markers stay literal; `Tokenizer::encode_marked` is the one path for generation and `/input_tokens`; the text contract carries the marked string and `leleka-runtime` `prepare_prompt` reads it with `encode_marked`. Not done: `strata-desktop-core` (`runtime_gateway`, `runtime_tools`) still renders unmarked text; replayed assistant text keeps its marks too (a model-written `</think>` in history is replayed as text); an engine older than this change reads the marks as private-use characters. **L todo** (live Codex session) |
|
||||
| 0.2 | `--dump-logits` header lies on native packs (`504579da`) | check every parity harness that compared against it | U | todo |
|
||||
| 0.3 | Spec window / MTP drafts near the context end run past `max_cells` (`e85567b6`) | test the controller at `max_context - k` | U | **U done** (2026-10-26, server `rnr5090`, `cargo test --release`: `leleka-runtime` lib `original_spec_controller` + `original_serve_engine` 60 passed, `leleka-cuda-driver --features cuda-driver` lib 382 passed). Ours was not the upstream crash (the serve loop ended as length instead of "ran out of context") but had the same hole in two places: (a) `verify_commit` ended the request when `p + t > max_context` instead of taking the rows that fit (upstream generate.cpp `room`), losing up to 7 tokens at the end of a context, and the MTP chain drafted `mtp_t - 1` cells whatever the context had left (upstream `mtp.cpp`: `max_steps = min(.., max_cells - (p + a))`); (b) the driver refused a draft launch when `first + a + max_t - 1 > 262144` (`mtp_original_full_drafter.rs`, `mtp_original_full_round.rs`) although only draft 0 was needed: a request admitted at the edge (`n + max_new + 8 = 262144`, a window of up to 8 rows plus accept 7) died with `InvalidQwenShape`. Now: the window shrinks to `max_context - p` (no room = finished as context; a shrunk window is not observed by the adaptive policy), the chain is clamped to `max_context - (p + a)` through `set_draft_plan` and restored on the next round, `chain_end_within_cells` bounds the driver's chain loop, the staging checks bound what is staged (`first + max(rows, a + 1)`). Tests: `spec_windows_and_drafts_never_pass_max_context_at_any_distance_from_the_end` (max_t 2/4/6/8, k = 1..max_t+3 from the end, fixed / suffix / min_p / adaptive: no row or draft cell >= max_context, text = greedy of every row that fits), `a_window_near_the_context_end_takes_only_the_rows_that_fit`, `the_chain_is_shortened_near_the_end_and_restored_after_it`, `a_chain_clamped_by_one_request_is_restored_for_the_next`, `serve_request_to_the_context_end_stays_inside_the_cells_and_ends_as_context` (real engine over the fake port), `the_chain_stops_at_the_last_cell_that_exists` (driver). Mutation check: with the window shrink and the chain clamp removed, 5 of these fail. Not verified on a model (no GPU run: the driver check relaxation is exercised only by the pure helper test). Open: the MTP ring is fixed at `MTP_ORIGINAL_MAX_CELLS` = 262144 cells while `leleka_context::geometry::MAX_CELLS` is 524288: a 512K context cannot draft past cell 262143 (not changed here) |
|
||||
| 0.4 | PLE `gather_batch` matches prefetched rows by position (`36381540`) | test reordered and duplicated keys on our async PLE reader | U | todo |
|
||||
| 0.4 | PLE `gather_batch` matches prefetched rows by position (`36381540`) | test reordered and duplicated keys on our async PLE reader | U | **U done, ours is not affected** (2026-10-26, server, `cargo test --release`: `leleka-model-format` lib `ple_reader` 27 passed, `leleka-runtime` lib `kar_ple_cache` 5 passed, 2 ignored need the real archive). Upstream's bug is in its `PleTable::gather_batch` fast path, which compared the prefetch slot list with the requested token list position by position and re-read the rows of a reordered or duplicated batch (a second ticket; the bytes were right, the read was wasted). Ours has no such matching: `PleReader::issue` returns a `Ticket` that owns its output buffer (slot `i` = request row `i`; `plan_reads` records one `Use` per request slot, duplicates included) and the row cache is keyed by row id; the per-token path (`ple_issue`/`ple_collect`, `original_window_ple`) pairs a token with its own ticket, and the KAR PLE GPU cache (`kar_ple_cache.rs`, `lookup_rows`) is a `RowMap` keyed by row id. Pinned by `ple_reader::tests::prefetched_keys_are_matched_by_row_never_by_position` (the upstream cases: ordered, permutation, subset, duplicates, duplicate prefetch, missing key, extra prefetch, none, over the window, window overflow, plus out-of-range rows; cache 0 / 8192 rows, inline / threaded; every slot equals the byte oracle, tickets collected out of order, a row already fetched is not read again, a batch never reads more than its distinct rows) and `kar_ple_cache::slot_tests::rows_are_matched_by_row_id_never_by_position_in_the_chunk` (swapped, reversed, shifted, repeated, subset and mixed known/new chunks keep each row in its slot). No code change |
|
||||
| 0.5 | Conversation/prefix cache edge cases (`8824e217` cancelled between windows loses the restored conversation; `940011da` full cache drops instead of evicting; `aece46db` 1-token prompt with spec) | tests on the prefix store and serve loop | U | todo |
|
||||
| 0.6 | Callback-thread affinity (`0e20d9ae`, `c08fd902`): one no-op host callback before the host thread pins itself; claimed 142.8 -> 207.8 tok/s on a 5090 + 9950X | check our host functions (`compute_drain`, DMA staging); microbench the callback latency on the server | R | todo |
|
||||
|
||||
|
||||
Reference in New Issue
Block a user