diff --git a/crates/experimental-ref/tests/emulated_kernels/kv_stream_and_keyless_routing.rs b/crates/experimental-ref/tests/emulated_kernels/kv_stream_and_keyless_routing.rs index d913bc27..bb542faf 100644 --- a/crates/experimental-ref/tests/emulated_kernels/kv_stream_and_keyless_routing.rs +++ b/crates/experimental-ref/tests/emulated_kernels/kv_stream_and_keyless_routing.rs @@ -3,119 +3,109 @@ #[allow(unused_imports)] use super::*; +const N_BLOCKS: usize = 1600; +const N_SLOTS: usize = 1030; +const CAP: usize = 76; +const N_Q: usize = 3; +const CALLS: usize = 45; + +/// The integers of a fixture array. +fn recorded_i32s(v: &serde_json::Value) -> Vec { + v.as_array().unwrap().iter().map(|x| x.as_i64().unwrap() as i32).collect() +} + +/// The ascending cell ids (and widths) of the queries of call number `call`. +fn call_queries(g: &mut Lcg, call: usize) -> (Vec, Vec) { + let mut ids = vec![0i32; N_Q * CAP]; + let mut steps = vec![0i32; N_Q * 4]; + for q in 0..N_Q { + let mut blocks = BTreeSet::new(); + while blocks.len() < CAP / 4 { + let b = if g.below(2) != 0 { + g.below(N_BLOCKS as u32) + } else { + (call as u32 * 7 + g.below(120)) % N_BLOCKS as u32 + }; + blocks.insert(b); + } + let mut w = 0; + for b in blocks { + for i in 0..4 { + ids[q * CAP + w] = (b * 4 + i) as i32; + w += 1; + } + } + steps[q * 4 + 3] = w as i32; + } + (ids, steps) +} + +/// Replay the recorded calls against the map and the slot pools; true when some call evicted. +fn replay_calls(f: &serde_json::Value, map: &mut StreamMap, (slots, host): (&mut PoolSet, &PoolSet)) -> bool { + let mut g = Lcg::new(51); + let recorded = f["calls"].as_array().unwrap(); + assert_eq!(recorded.len(), CALLS); + let mut seen_evictions = false; + for (call, rec) in recorded.iter().enumerate() { + let (ids, steps) = call_queries(&mut g, call); + let order = recorded_i32s(&rec["order"]); + let report = map.resolve(&ids, &steps, N_Q, CAP, 4, &MissOrder::Observed(order.clone())).unwrap(); + assert_eq!(report.placed, order.len(), "call {call}"); + map.copy_missed(slots, host, &S, StreamFormat::Int8); + assert_eq!(map.ctl.to_vec(), recorded_i32s(&rec["ctl"]), "ctl after call {call}"); + assert_eq!( + state_hash(map, report.placed) as i64, + rec["state_fnv"].as_i64().unwrap(), + "state after call {call}" + ); + assert!(map.is_consistent()); + seen_evictions |= map.resident_blocks() == N_SLOTS; + } + seen_evictions +} + #[test] fn kv_stream_resolver_copy_and_ring_match_the_model() { let fx = fixture("emulated_kernels.json"); let f = &fx["stream"]; - let (n_blocks, n_slots, cap, n_q, calls) = (1600usize, 1030usize, 76usize, 3usize, 45usize); - let mut map = StreamMap::new(n_blocks, n_slots); + let mut map = StreamMap::new(N_BLOCKS, N_SLOTS); assert_eq!(state_hash(&map, 0) as i64, f["reset_state_fnv"].as_i64().unwrap()); let rows = S.rows_per_page(); - let mut host = PoolSet::new(KvFormat::Int8, &S, n_blocks as u64 * rows).unwrap(); - let mut slots = PoolSet::new(KvFormat::Int8, &S, n_slots as u64 * rows).unwrap(); + let mut host = PoolSet::new(KvFormat::Int8, &S, N_BLOCKS as u64 * rows).unwrap(); + let mut slots = PoolSet::new(KvFormat::Int8, &S, N_SLOTS as u64 * rows).unwrap(); for (a, name) in ["k_q", "v_q", "k_scale", "v_scale"].iter().enumerate() { for (i, b) in host.array_mut(name).unwrap().iter_mut().enumerate() { *b = host_byte(a, i); } } - - let mut g = Lcg::new(51); - let recorded = f["calls"].as_array().unwrap(); - assert_eq!(recorded.len(), calls); - let mut seen_evictions = false; - for (call, rec) in recorded.iter().enumerate() { - let mut ids = vec![0i32; n_q * cap]; - let mut steps = vec![0i32; n_q * 4]; - for q in 0..n_q { - let mut blocks = BTreeSet::new(); - while blocks.len() < cap / 4 { - let b = if g.below(2) != 0 { - g.below(n_blocks as u32) - } else { - (call as u32 * 7 + g.below(120)) % n_blocks as u32 - }; - blocks.insert(b); - } - let mut w = 0; - for b in blocks { - for i in 0..4 { - ids[q * cap + w] = (b * 4 + i) as i32; - w += 1; - } - } - steps[q * 4 + 3] = w as i32; - } - let order: Vec = rec["order"] - .as_array() - .unwrap() - .iter() - .map(|x| x.as_i64().unwrap() as i32) - .collect(); - let report = map - .resolve(&ids, &steps, n_q, cap, 4, &MissOrder::Observed(order.clone())) - .unwrap(); - assert_eq!(report.placed, order.len(), "call {call}"); - map.copy_missed(&mut slots, &host, &S, StreamFormat::Int8); - let ctl: Vec = rec["ctl"] - .as_array() - .unwrap() - .iter() - .map(|x| x.as_i64().unwrap() as i32) - .collect(); - assert_eq!(map.ctl.to_vec(), ctl, "ctl after call {call}"); - assert_eq!( - state_hash(&map, report.placed) as i64, - rec["state_fnv"].as_i64().unwrap(), - "state after call {call}" - ); - assert!(map.is_consistent()); - seen_evictions |= map.resident_blocks() == n_slots; - } - assert!( - seen_evictions, - "the scenario must fill every slot so that later calls evict" - ); + let seen_evictions = replay_calls(f, &mut map, (&mut slots, &host)); + assert!(seen_evictions, "the scenario must fill every slot so that later calls evict"); for (name, key) in [ ("k_q", "slots_fnv_k_q"), ("v_q", "slots_fnv_v_q"), ("k_scale", "slots_fnv_k_scale"), ("v_scale", "slots_fnv_v_scale"), ] { - assert_eq!( - fnv1a(slots.array(name).unwrap()) as i64, - f[key].as_i64().unwrap(), - "{name}" - ); + assert_eq!(fnv1a(slots.array(name).unwrap()) as i64, f[key].as_i64().unwrap(), "{name}"); } - - let ring: Vec = f["ring_table"] - .as_array() - .unwrap() - .iter() - .map(|x| x.as_i64().unwrap()) - .collect(); - assert_eq!( - kv_stream::ring_table(37, 8) - .iter() - .map(|x| i64::from(*x)) - .collect::>(), - ring - ); - assert_eq!(f["copy_u4_equal"].as_i64(), Some(1)); - + check_ring(f); map.reset(); assert_eq!(state_hash(&map, 0) as i64, f["final_reset_state_fnv"].as_i64().unwrap()); } +/// The ring table and the equal-copy flag of the fixture. +fn check_ring(f: &serde_json::Value) { + let ring: Vec = f["ring_table"].as_array().unwrap().iter().map(|x| x.as_i64().unwrap()).collect(); + assert_eq!(kv_stream::ring_table(37, 8).iter().map(|x| i64::from(*x)).collect::>(), ring); + assert_eq!(f["copy_u4_equal"].as_i64(), Some(1)); +} + #[test] fn keyless_query_routing_kernel_matches_the_fmaf_reference_bit_for_bit() { let fx = fixture("emulated_kernels.json"); let f = &fx["route"]; - let (tokens, heads) = ( - f["tokens"].as_u64().unwrap() as usize, - f["heads"].as_u64().unwrap() as usize, - ); + let (tokens, heads) = (f["tokens"].as_u64().unwrap() as usize, f["heads"].as_u64().unwrap() as usize); let mut g = Lcg::new(61); let mut q: Vec = (0..tokens * heads * 256).map(|_| g.soft()).collect(); keyless::route_query(&mut q, &routing_matrices(heads), tokens, heads).unwrap();