hf2q 0.1.32

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Issue #727: admission decides spill/restore by longest prefix, and a
//! reclaimed conversation is restored bitwise into whatever slot it is
//! granted on its return. The shared store's own tests are in
//! `slot_spill_tests.rs`.

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,
    }
}

/// The shared reclaim plan at Qwen's types.
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)));
    // The occupant: stable anchor 0..12, then the assistant header 12..14,
    // reasoning and answer up to 30.
    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);

    // Cold admission into an occupied slot: move its ledger into the store.
    let plan = qwen_plan(None, None, &other, &occupant, true);
    assert_eq!((plan.restore, plan.victim), (None, VictimSpill::Move));
    // Cold admission into an empty slot spills nothing.
    let plan = qwen_plan(None, None, &other, &[], false);
    assert_eq!(plan.victim, VictimSpill::Keep);
    // A live-tail resume keeps the slot's conversation.
    let plan = qwen_plan(Some(live), None, &extended, &occupant, true);
    assert_eq!((plan.restore, plan.victim), (None, VictimSpill::Keep));
    // The occupant's own next turn or retry rewinds to its stable anchor but
    // shares the assistant header beyond it: no copy.
    let plan = qwen_plan(Some(anchor), None, &continuation, &occupant, true);
    assert_eq!(plan.victim, VictimSpill::Keep);
    // A request that starts new content exactly at the anchor (a fork, or
    // another conversation sharing the anchor) would discard the occupant's
    // tail: copy the ledger, the anchor stays in place for this request.
    let plan = qwen_plan(Some(anchor), None, &fork, &occupant, true);
    assert_eq!(plan.victim, VictimSpill::Copy);
    // No tail beyond the anchor: nothing to lose.
    let plan = qwen_plan(Some(anchor), None, &fork, &occupant[..12], true);
    assert_eq!(plan.victim, VictimSpill::Keep);

    // A spill restores only when it resumes strictly more tokens.
    let plan = qwen_plan(Some(anchor), spill(16), &continuation, &occupant, true);
    assert_eq!(plan.restore, spill(16));
    assert_eq!(plan.victim, VictimSpill::Move);
    // A spill matching no more than the live tail (30 tokens) stays stored.
    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();
    // Newest first: index 2 is newest.
    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);

    // A full-prompt anchor beats a shorter live tail and reuses its logits.
    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);

    // A live tail must be a strict prefix; unrelated ledgers never match.
    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());
}

/// Two full-attention layers, two DeltaNet layers, no MTP.
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;
    }
}

/// Fill `slot`'s region of every buffer with a seed-distinct pattern.
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;
    }
}

/// Conversation A in `slot`: an anchor at `anchor_len` tokens, then a live
/// tail up to `tokens.len()`.
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, false).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;
    // Different slots/windows of one padded joint parent, not logical rows.
    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");

    // A cold request C is granted A's idle slot: A moves into the store.
    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());

    // C runs in slot 0 and overwrites it.
    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];

    // A returns with one more turn and is granted the empty slot 2.
    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");

    // D shares A's anchor and rewinds slot 2 to it: A's tail is copied out
    // while the anchor stays in place for D.
    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());

    // A's own next turn also rewinds slot 2 to the anchor, but it shares the
    // tokens after it (the assistant header): no copy is taken.
    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 returns again, granted slot 1: its copied tail is restored.
    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);

    // An embedding request takes slot 1 over: A is spilled again.
    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");
    // The ledger stays for admission's ordinary cold clear.
    assert_eq!(retained.tokens, a);
}