Compare Qwen cache snapshots at matching stages

This commit is contained in:
Georg Bauer committed 2026-10-04 16:55:36 +02:00
1 parent d9026aa7bf
commit 5a8782b9b6
21 files changed
+3397 -149

No files matched your search

@@ -1352,3 +1352,93 @@ struct SessionPrompt {
history_seconds: f64,
restore_seconds: f64,
}
#[test]
#[ignore = "Metal; exact pinned lazy/deep snapshot zero-sign law"]
fn issue114_snapshot_stage_preserves_zero_bits() {
use super::super::{gpu::Context, qwen_mtplx_tests::hex_digest};
use super::array::Dtype;
use super::cache::QsaCache;
super::configure_sources().unwrap();
let _context = Context::open_qwen(0).unwrap();
let streams = super::stream::Streams::new(Some(super::allocator::Allocator::new().unwrap()));
let gpu = streams.default_stream(super::stream::Device::Gpu).unwrap();
let fixture: serde_json::Value = serde_json::from_str(include_str!(
"../../../../tests/fixtures/mtplx-snapshot-zero.json"
))
.unwrap();
let read = |a: &Array| {
ops::evaluate(&streams, std::slice::from_ref(a), gpu, false).unwrap();
let mut bytes = vec![0; a.layout().nbytes()];
a.buffer().read(a.offset(), &mut bytes).unwrap();
bytes
};
for case in fixture["cases"].as_array().unwrap() {
let decode = |key| {
case[key]
.as_str()
.unwrap()
.as_bytes()
.chunks_exact(2)
.map(|pair| u8::from_str_radix(std::str::from_utf8(pair).unwrap(), 16).unwrap())
.collect::<Vec<_>>()
};
let dtype = if case["dtype"].as_str().unwrap().contains("bfloat16") {
Dtype::BF16
} else {
Dtype::F32
};
let raw = decode("raw");
let a = Array::new(&[1, 2, 1, 4], dtype, super::scalar_buffer(&raw).unwrap()).unwrap();
let make = || {
let mut qsa = QsaCache::new(4);
qsa.set_state(&[Some(object(a.clone())), Some(object(a.clone())), None, None])
.unwrap();
vec![Some(Cache {
attention: AttentionCache::Qsa(qsa),
ple: Default::default(),
capture_ple: None,
})]
};
for lazy in [false, true] {
let saved = snapshot(&make(), |_| false, lazy, false, gpu).unwrap();
let Some(State::Qsa(leaves)) = &saved[0] else {
panic!("QSA state missing")
};
let expected = decode(if lazy { "lazy" } else { "deep" });
assert_eq!(read(leaves[0].as_ref().unwrap()), expected);
assert_eq!(
read(&a),
raw,
"taking either snapshot must not mutate its source"
);
for clone in [false, true] {
let mut restored = make();
restore(&mut restored, &saved, clone, gpu).unwrap();
let AttentionCache::Qsa(qsa) = &restored[0].as_ref().unwrap().attention else {
unreachable!()
};
assert_eq!(qsa.kv.offset, 1);
let values = qsa.state(gpu).unwrap();
let got = read(&values[0].as_ref().unwrap().borrow());
assert_eq!(
got,
if clone {
decode("deep")
} else {
expected.clone()
}
);
assert_eq!(
read(leaves[0].as_ref().unwrap()),
expected,
"restore must not mutate banked state"
);
println!(
"{}",
serde_json::json!({"case":"snapshot_zero","dtype":case["dtype"],"lazy":lazy,"clone_on_restore":clone,"sha256":hex_digest(&got),"exact":true})
);
}
}
}
}