use mlx_native::MlxDevice;
use super::super::slot_spill::{plan_reclaim, ReclaimPlan, SlotSpillStore, VictimSpill};
use super::super::{Qwen35PromptAnchor, Qwen35RetainedPrefix, Qwen35SlotPreference, SlotId};
use super::{
qwen35_best_ledger_match, reclaim_qwen35_slot, spill_reclaimed_qwen35_slot,
Qwen35SlotSpillStore,
};
use crate::inference::models::qwen35::kv_cache::HybridKvCache;
use crate::inference::models::qwen35::{default_layer_types, Qwen35Config, Qwen35Variant};
use crate::serve::api::engine_qwen35::anchor_store::Qwen35AnchorStore;
fn preference(cached_tokens: usize, anchor: bool) -> Qwen35SlotPreference {
Qwen35SlotPreference {
slot: SlotId(0),
cached_tokens,
restore_prompt_anchor: anchor,
use_prefill_logits: false,
}
}
fn qwen_plan(
in_place: Option<Qwen35SlotPreference>,
spill_match: Option<(usize, Qwen35SlotPreference)>,
prompt_tokens: &[u32],
victim_retained: &[u32],
victim_has_ledger: bool,
) -> ReclaimPlan<usize, Qwen35SlotPreference> {
plan_reclaim(
in_place,
spill_match,
prompt_tokens,
victim_retained,
victim_has_ledger,
)
}
#[test]
fn reclaim_plan_spills_only_discarded_state_and_restores_only_longer_matches() {
let live = preference(30, false);
let anchor = preference(12, true);
let spill = |tokens| Some((3, preference(tokens, false)));
let occupant: Vec<u32> = (0..30).collect();
let other: Vec<u32> = (500..520).collect();
let mut continuation: Vec<u32> = occupant[..14].to_vec();
continuation.extend([900, 901, 902]);
let mut fork: Vec<u32> = occupant[..12].to_vec();
fork.extend([700, 701]);
let mut extended = occupant.clone();
extended.push(31);
let plan = qwen_plan(None, None, &other, &occupant, true);
assert_eq!((plan.restore, plan.victim), (None, VictimSpill::Move));
let plan = qwen_plan(None, None, &other, &[], false);
assert_eq!(plan.victim, VictimSpill::Keep);
let plan = qwen_plan(Some(live), None, &extended, &occupant, true);
assert_eq!((plan.restore, plan.victim), (None, VictimSpill::Keep));
let plan = qwen_plan(Some(anchor), None, &continuation, &occupant, true);
assert_eq!(plan.victim, VictimSpill::Keep);
let plan = qwen_plan(Some(anchor), None, &fork, &occupant, true);
assert_eq!(plan.victim, VictimSpill::Copy);
let plan = qwen_plan(Some(anchor), None, &fork, &occupant[..12], true);
assert_eq!(plan.victim, VictimSpill::Keep);
let plan = qwen_plan(Some(anchor), spill(16), &continuation, &occupant, true);
assert_eq!(plan.restore, spill(16));
assert_eq!(plan.victim, VictimSpill::Move);
let plan = qwen_plan(Some(live), spill(30), &extended, &occupant, true);
assert_eq!((plan.restore, plan.victim), (None, VictimSpill::Keep));
let plan = qwen_plan(None, spill(8), &other, &[], false);
assert_eq!((plan.restore, plan.victim), (spill(8), VictimSpill::Keep));
}
#[test]
fn ledger_match_is_longest_prefix_then_newest() {
let prompt: Vec<u32> = (1..=20).collect();
let other: Vec<u32> = (100..120).collect();
let candidates = [
(2, &prompt[..8], Some(&prompt[..4])),
(1, &prompt[..12], Some(&prompt[..4])),
(0, &prompt[..12], Some(&prompt[..4])),
];
let (index, best) = qwen35_best_ledger_match(candidates, &prompt).expect("match");
assert_eq!(
(index, best.cached_tokens),
(1, 12),
"longest, newest on ties"
);
assert!(!best.restore_prompt_anchor);
let candidates = [(0, &prompt[..15], None), (1, &other[..], Some(&prompt[..]))];
let (index, best) = qwen35_best_ledger_match(candidates, &prompt).expect("match");
assert_eq!((index, best.cached_tokens), (1, 20));
assert!(best.restore_prompt_anchor && best.use_prefill_logits);
assert!(qwen35_best_ledger_match([(0, &prompt[..], None)], &prompt).is_none());
assert!(qwen35_best_ledger_match([(0, &other[..5], Some(&other[..3]))], &prompt).is_none());
}
fn tiny_cfg() -> Qwen35Config {
Qwen35Config {
variant: Qwen35Variant::Dense,
hidden_size: 64,
num_hidden_layers: 4,
num_attention_heads: 2,
num_key_value_heads: 2,
head_dim: 32,
linear_num_key_heads: 2,
linear_num_value_heads: 4,
linear_key_head_dim: 8,
linear_value_head_dim: 8,
linear_conv_kernel_dim: 4,
full_attention_interval: 2,
layer_types: default_layer_types(4, 2),
partial_rotary_factor: 0.25,
rope_theta: 1e7,
rotary_dim: 8,
mrope_section: [2, 2, 2, 2],
mrope_interleaved: true,
rms_norm_eps: 1e-6,
max_position_embeddings: 4096,
vocab_size: 256,
attn_output_gate: true,
mtp_num_hidden_layers: 0,
mtp_use_dedicated_embeddings: true,
intermediate_size: Some(128),
moe: None,
}
}
fn set_cursor(cache: &mut HybridKvCache, slot: SlotId, cursor: u32) {
for block in &mut cache.full_attn {
block.current_len[slot.0 as usize] = cursor;
}
}
fn plant(cache: &mut HybridKvCache, slot: SlotId, seed: usize) {
let n_seqs = cache.n_seqs as usize;
let fill = |buffer: &mut mlx_native::MlxBuffer, index: usize| {
let region = buffer.byte_len() / n_seqs;
let start = slot.0 as usize * region;
let bytes = buffer.as_mut_slice::<u8>().expect("cache bytes");
for (offset, byte) in bytes[start..start + region].iter_mut().enumerate() {
*byte = ((seed * 29 + index * 13 + offset) % 251) as u8;
}
};
let mut index = 0;
for block in &mut cache.full_attn {
let tq = block.tq.as_mut().expect("TQ cache");
for buffer in [
&mut tq.k_packed,
&mut tq.k_norms,
&mut tq.v_packed,
&mut tq.v_norms,
] {
fill(buffer, index);
index += 1;
}
}
for linear in &mut cache.linear_attn {
for buffer in [
&mut linear.conv_state,
&mut linear.conv_state_scratch,
&mut linear.recurrent,
&mut linear.recurrent_scratch,
] {
fill(buffer, index);
index += 1;
}
linear.pp_flipped[slot.0 as usize] = seed % 2 == 1;
}
}
fn seat_conversation(
cache: &mut HybridKvCache,
slot: SlotId,
tokens: &[u32],
anchor_len: usize,
seed: usize,
) -> (Qwen35RetainedPrefix, Qwen35AnchorStore) {
plant(cache, slot, seed);
set_cursor(cache, slot, anchor_len as u32);
let kv = cache
.snapshot_slot_anchor(slot, anchor_len)
.expect("anchor checkpoint");
set_cursor(cache, slot, tokens.len() as u32);
let retained = Qwen35RetainedPrefix {
tokens: tokens.to_vec(),
spec: None,
};
let mut anchors = Qwen35AnchorStore::new(4).expect("test anchor store");
anchors
.stage(
seed as u64,
Qwen35PromptAnchor {
id: 0,
request: 0,
prompt_tokens: tokens[..anchor_len].to_vec(),
kv,
prefill_logits: vec![0.25; 8],
vision_fingerprint: None,
spec: None,
},
)
.expect("stage anchor");
anchors.commit(seed as u64).expect("commit anchor");
(retained, anchors)
}
#[test]
fn copied_spill_and_live_prefix_share_full_parent_until_the_final_owner_releases() {
use crate::inference::models::qwen35::carry_memory::CarryOwners;
use crate::serve::api::engine_qwen35::Qwen35SpecPrefixBoundary;
use mlx_native::DType;
let _gpu = crate::inference::hf2q_gpu_test_lock();
let Ok(device) = MlxDevice::new() else { return };
let mut cache = HybridKvCache::new_with_options(&tiny_cfg(), &device, 32, 2, true).unwrap();
let (mut retained, mut anchors) = seat_conversation(
&mut cache, SlotId(0), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 6, 1,
);
let parent = device.alloc_buffer(8 * 64 * 4, DType::F32, vec![8, 64]).unwrap();
let backing_bytes = parent.byte_len() as u64;
let active = parent.slice_view(7 * 64 * 4, 64);
retained.spec = Some(Qwen35SpecPrefixBoundary {
token_count: 10, pending_target_hidden: parent.slice_view(64 * 4, 64),
});
drop(parent);
let mut store = Qwen35SlotSpillStore::new(1 << 30, 8);
super::spill_qwen35_victim(
&cache, &mut store, SlotId(0), &mut retained, &mut anchors, VictimSpill::Copy,
);
assert_eq!(store.len(), 1);
assert!(store.resident_bytes() > backing_bytes, "full host KV/ledger payload stays charged");
let mut joined = CarryOwners::default();
joined.include(&active);
joined.include(&retained.spec.as_ref().unwrap().pending_target_hidden);
super::include_carry_owners(&store, &mut joined);
assert_eq!(joined.bytes(), backing_bytes);
let replacement = device.alloc_buffer(3 * 64 * 4, DType::F32, vec![3, 64]).unwrap();
retained.spec = Some(Qwen35SpecPrefixBoundary {
token_count: 10, pending_target_hidden: replacement.slice_view(2 * 64 * 4, 64),
});
drop(active);
drop(joined);
let mut replaced = CarryOwners::default();
replaced.include(&retained.spec.as_ref().unwrap().pending_target_hidden);
super::include_carry_owners(&store, &mut replaced);
assert_eq!(replaced.bytes(), backing_bytes + replacement.byte_len() as u64);
drop(replaced);
drop(store);
let mut remaining = CarryOwners::default();
remaining.include(&retained.spec.as_ref().unwrap().pending_target_hidden);
assert_eq!(remaining.bytes(), replacement.byte_len() as u64);
}
#[test]
fn reclaimed_conversation_is_spilled_then_restored_bitwise_into_its_granted_slot() {
let _gpu = crate::inference::hf2q_gpu_test_lock();
let device = MlxDevice::new().expect("Metal device");
let mut cache = HybridKvCache::new_with_options(&tiny_cfg(), &device, 32, 3, true)
.expect("three-slot cache");
let mut retained: Vec<Qwen35RetainedPrefix> =
(0..3).map(|_| Qwen35RetainedPrefix::default()).collect();
let mut anchors: Vec<Qwen35AnchorStore> = (0..3)
.map(|_| Qwen35AnchorStore::new(4).expect("test anchor store"))
.collect();
let mut store: Qwen35SlotSpillStore = SlotSpillStore::new(1 << 30, 8);
let a: Vec<u32> = (1..=10).collect();
(retained[0], anchors[0]) = seat_conversation(&mut cache, SlotId(0), &a, 6, 1);
let a_state = cache.spill_slot(SlotId(0)).expect("reference copy of A");
let c = [50u32, 51, 52];
let granted = reclaim_qwen35_slot(
&mut cache,
&mut store,
SlotId(0),
&mut retained[0],
&mut anchors[0],
None,
&c,
None,
None,
)
.expect("cold reclaim");
assert_eq!(granted, None, "C stays cold");
assert_eq!(store.len(), 1);
assert!(retained[0].is_empty() && anchors[0].is_empty());
cache.reset_for_slot(SlotId(0)).expect("cold reset");
plant(&mut cache, SlotId(0), 2);
set_cursor(&mut cache, SlotId(0), 4);
retained[0].tokens = vec![50, 51, 52, 53];
let mut a_turn: Vec<u32> = a.clone();
a_turn.extend([11, 12]);
let granted = reclaim_qwen35_slot(
&mut cache,
&mut store,
SlotId(2),
&mut retained[2],
&mut anchors[2],
None,
&a_turn,
None,
None,
)
.expect("restore A");
assert_eq!(
granted,
Some(Qwen35SlotPreference {
slot: SlotId(2),
cached_tokens: 10,
restore_prompt_anchor: false,
use_prefill_logits: false,
}),
"A resumes its live tail in slot 2"
);
assert!(store.is_empty(), "a restored spill leaves the store");
assert_eq!(retained[2].tokens, a);
assert_eq!(
anchors[2]
.deepest()
.map(|anchor| anchor.prompt_tokens.as_slice()),
Some(&a[..6])
);
assert!(
cache.spill_slot(SlotId(2)).expect("restored A") == a_state,
"slot 2 holds A's state bitwise"
);
assert_eq!(retained[0].tokens, vec![50, 51, 52, 53], "C is untouched");
let mut d = a[..6].to_vec();
d.push(99);
let in_place = Some(Qwen35SlotPreference {
slot: SlotId(2),
cached_tokens: 6,
restore_prompt_anchor: true,
use_prefill_logits: false,
});
let granted = reclaim_qwen35_slot(
&mut cache,
&mut store,
SlotId(2),
&mut retained[2],
&mut anchors[2],
in_place,
&d,
None,
None,
)
.expect("anchor reclaim");
assert_eq!(granted, in_place);
assert_eq!(store.len(), 1);
assert_eq!(retained[2].tokens, a, "the slot ledger is unchanged");
assert!(!anchors[2].is_empty());
let mut continuation = a[..8].to_vec();
continuation.push(77);
let granted = reclaim_qwen35_slot(
&mut cache,
&mut store,
SlotId(2),
&mut retained[2],
&mut anchors[2],
in_place,
&continuation,
None,
None,
)
.expect("continuation keeps its slot");
assert_eq!(granted, in_place);
assert_eq!(store.len(), 1, "a continuation copies nothing");
a_turn.push(13);
let granted = reclaim_qwen35_slot(
&mut cache,
&mut store,
SlotId(1),
&mut retained[1],
&mut anchors[1],
None,
&a_turn,
None,
None,
)
.expect("restore A again");
assert_eq!(granted.map(|preference| preference.cached_tokens), Some(10));
assert!(store.is_empty());
assert!(cache.spill_slot(SlotId(1)).expect("restored A") == a_state);
spill_reclaimed_qwen35_slot(
&cache,
&mut store,
SlotId(1),
&mut retained[1],
&mut anchors[1],
);
assert_eq!(store.len(), 1);
assert!(retained[1].is_empty() && anchors[1].is_empty());
}
#[test]
fn a_spill_above_the_budget_is_not_stored_and_the_victim_goes_cold() {
let _gpu = crate::inference::hf2q_gpu_test_lock();
let device = MlxDevice::new().expect("Metal device");
let mut cache =
HybridKvCache::new_with_options(&tiny_cfg(), &device, 32, 2, true).expect("two-slot cache");
let a: Vec<u32> = (1..=10).collect();
let (mut retained, mut anchor) = seat_conversation(&mut cache, SlotId(0), &a, 6, 1);
let mut store: Qwen35SlotSpillStore = SlotSpillStore::new(1024, 8);
let granted = reclaim_qwen35_slot(
&mut cache,
&mut store,
SlotId(0),
&mut retained,
&mut anchor,
None,
&[7, 7, 7],
None,
None,
)
.expect("cold reclaim");
assert_eq!(granted, None);
assert!(store.is_empty(), "a 1 KiB budget cannot hold the slot");
assert_eq!(retained.tokens, a);
}