Files
leleka/crates/experimental-ref/tests/stream_plan.rs
T
Pavlo RatushnyiandClaude Sonnet 5.5 1d1f5bfc14 feat(experimental-ref): KV streaming planner and CLOCK resolver oracle with golden and emulated-kernel checks
kv_plan/qsa_state_bytes/host copy sizing equal layer.cpp's own functions (extracted verbatim)
over 480 format x context x ring x --kv-resident cases. StreamMap::resolve models the
kernel's three phases sequentially; the miss-list order is the only device nondeterminism
and is supplied as input. The literal kv_stream.cu text, run with 1024 emulated threads,
reproduces the model state after each of 45 calls with constant eviction.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 15:24:37 +02:00

51 lines
2.2 KiB
Rust

//! The KV streaming planner against `layer.cpp`'s own functions (`oracle/gen_stream_golden.sh` extracts them
//! verbatim and drives them over a grid of formats, contexts, rings and `--kv-resident` values).
mod common;
use common::fixture;
use leleka_experimental_ref::{
kv_stream::{self, PlanSwitches, StreamMode},
layout::{KvFormat, QsaShapes},
};
#[test]
fn planner_and_state_bytes_match_layer_cpp_over_the_whole_grid() {
let g = fixture("stream_plan_golden.json");
assert_eq!(g["resident_min"].as_i64(), Some(kv_stream::KV_RESIDENT_MIN));
assert_eq!(g["map_bytes_5120"].as_u64(), Some(kv_stream::map_bytes(5120)));
let s = QsaShapes::real();
let mut n = 0;
let mut modes = [0usize; 3];
for c in g["cases"].as_array().unwrap() {
let format = match c["format"].as_str().unwrap() {
"fp16" => KvFormat::Fp16,
"int8" => KvFormat::Int8,
"q4_0" => KvFormat::Q4_0,
"k8v4" => KvFormat::K8V4,
"int8-keyless" => KvFormat::Int8Keyless,
other => panic!("{other}"),
};
let (max_cells, ring, resident) = (
c["max_cells"].as_i64().unwrap(),
c["ring"].as_i64().unwrap(),
c["resident"].as_i64().unwrap(),
);
let sw = PlanSwitches::default();
let plan = kv_stream::kv_plan(&s, max_cells, ring, resident, sw);
let ctx = format!("{format:?} {max_cells} ring {ring} resident {resident}");
assert_eq!(plan.mode as i64, c["mode"].as_i64().unwrap(), "{ctx}");
assert_eq!(plan.pages, c["pages"].as_i64().unwrap(), "{ctx}");
assert_eq!(plan.slots, c["slots"].as_i64().unwrap(), "{ctx}");
assert_eq!(plan.pooled_rows, c["pooled_rows"].as_i64().unwrap(), "{ctx}");
let bytes = kv_stream::qsa_state_bytes(&s, format, max_cells, c["rope"].as_i64() == Some(1), ring, resident, sw);
assert_eq!(bytes, c["state_bytes"].as_u64().unwrap(), "{ctx}");
modes[plan.mode as usize] += 1;
n += 1;
}
assert_eq!(n, 5 * 4 * 3 * 4 * 2);
// the grid exercises all three modes
assert!(modes.iter().all(|m| *m > 0), "{modes:?}");
assert_eq!(StreamMode::Streamed as i32, 1);
}