hf2q 0.1.6

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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
use super::*;

#[path = "engine_gemma4_tiny_prefill_test_support.rs"]
mod support;
use support::*;

/// Direct decode -> tiny-cold-prefill kernel discriminator for the
/// intermittent N=8 worker fault. Without constructing scheduler/reply
/// ownership, it mirrors both relevant GPU transaction shapes: eight tiny
/// cold prefills followed by a width-eight batched decode, and staggered
/// decode widths one through eight around later tiny admissions. Every cold
/// prefill and the coalesced width-eight decode must match independent N=1
/// references; staircase decode rows must remain finite before the following
/// cold prefill checks for persistent corruption.
#[test]
fn gemma_n8_decode_then_tiny_cold_prefill_is_repeat_invariant() {
    if skip_unless_gated("gemma_n8_decode_then_tiny_cold_prefill_is_repeat_invariant") {
        return;
    }
    let gguf_path: PathBuf = std::env::var(BYTE_EQUIV_E2E_GGUF_ENV)
        .map(PathBuf::from)
        .expect("HF2Q_BYTE_EQUIV_E2E_GGUF set");
    assert!(gguf_path.exists(), "GGUF missing: {}", gguf_path.display());
    let load_opts = LoadOptions {
        model_path: gguf_path,
        tokenizer_path: None,
        config_path: None,
        dwq_overlay_path: None,
        kv_persist_dir: None,
    };
    let prompts: Vec<Vec<u32>> = vec![
        vec![1, 2, 3],
        vec![4, 5, 6, 7],
        vec![8, 9],
        vec![10, 11, 12, 13, 14],
        vec![15, 16],
        vec![17, 18, 19, 20],
        vec![21, 22, 23],
        vec![24, 25, 26, 27, 28],
    ];
    let max_decode_tokens = 24usize;

    let expected: Vec<(u32, u32)> = prompts
        .iter()
        .map(|prompt| {
            let mut loaded = LoadedModel::load(&load_opts).expect("load N=1 reference");
            let LoadedModel::Gemma(g) = &mut loaded else {
                panic!("expected Gemma GGUF")
            };
            g.provision_multi_seq_kv_for_slot_aware(1)
                .expect("provision N=1 reference KV");
            let mut kv = g.multi_seq_kv.take().expect("N=1 HB scaffold");
            let mut hybrid = g.multi_seq_kv_hybrid.take();
            let mut dense = g.multi_seq_kv_dense.take();
            let mut mlx = g.multi_seq_kv_mlx.take();
            let first = g
                .weights
                .forward_prefill_with_soft_tokens_slot_aware(
                    prompt,
                    &[],
                    max_decode_tokens,
                    &mut g.ctx,
                    SlotId(0),
                    &mut kv,
                    hybrid.as_mut(),
                    dense.as_mut(),
                    mlx.as_mut(),
                )
                .expect("N=1 reference prefill");
            clear_gemma4_self_mounts(g);
            let mut profile = None;
            let second = g
                .weights
                .forward_decode_slot_aware(
                    first,
                    prompt.len(),
                    &mut g.ctx,
                    &mut profile,
                    SlotId(0),
                    &mut kv,
                    hybrid.as_mut(),
                    dense.as_mut(),
                    mlx.as_mut(),
                )
                .expect("N=1 reference decode");
            (first, second)
        })
        .collect();
    assert!(
        expected
            .iter()
            .all(|&(first, second)| first != 0 && second != 0),
        "vacuous: an N=1 reference selected token 0: {expected:?}"
    );
    let resume_base: Vec<u32> = (100u32..140).collect();
    let resume_prompts: Vec<Vec<u32>> = [vec![41u32], vec![42u32, 43], vec![44u32, 45, 46, 47, 48]]
        .into_iter()
        .map(|suffix| {
            let mut prompt = resume_base.clone();
            prompt.extend(suffix);
            prompt
        })
        .collect();
    let expected_resumes: Vec<u32> = resume_prompts
        .iter()
        .map(|prompt| {
            let mut loaded = LoadedModel::load(&load_opts).expect("load resume reference");
            let LoadedModel::Gemma(g) = &mut loaded else {
                panic!("expected Gemma GGUF")
            };
            g.provision_multi_seq_kv_for_slot_aware(1)
                .expect("provision resume reference KV");
            let mut kv = g.multi_seq_kv.take().expect("resume reference HB scaffold");
            let mut hybrid = g.multi_seq_kv_hybrid.take();
            let mut dense = g.multi_seq_kv_dense.take();
            let mut mlx = g.multi_seq_kv_mlx.take();
            g.weights
                .forward_prefill_with_soft_tokens_slot_aware(
                    prompt,
                    &[],
                    max_decode_tokens,
                    &mut g.ctx,
                    SlotId(0),
                    &mut kv,
                    hybrid.as_mut(),
                    dense.as_mut(),
                    mlx.as_mut(),
                )
                .expect("cold full-prompt resume reference")
        })
        .collect();
    assert!(
        expected_resumes.iter().all(|&token| token != 0),
        "vacuous: a cold resume reference selected token 0: {expected_resumes:?}"
    );
    let boundary_prompts: Vec<Vec<u32>> = [31usize, 32usize]
        .into_iter()
        .map(|len| (1..=len as u32).collect())
        .collect();
    let expected_boundaries: Vec<u32> = boundary_prompts
        .iter()
        .map(|prompt| {
            let mut loaded = LoadedModel::load(&load_opts).expect("load boundary reference");
            let LoadedModel::Gemma(g) = &mut loaded else {
                panic!("expected Gemma GGUF")
            };
            g.provision_multi_seq_kv_for_slot_aware(1)
                .expect("provision boundary reference KV");
            let mut kv = g
                .multi_seq_kv
                .take()
                .expect("boundary reference HB scaffold");
            let mut hybrid = g.multi_seq_kv_hybrid.take();
            let mut dense = g.multi_seq_kv_dense.take();
            let mut mlx = g.multi_seq_kv_mlx.take();
            g.weights
                .forward_prefill_with_soft_tokens_slot_aware(
                    prompt,
                    &[],
                    max_decode_tokens,
                    &mut g.ctx,
                    SlotId(0),
                    &mut kv,
                    hybrid.as_mut(),
                    dense.as_mut(),
                    mlx.as_mut(),
                )
                .expect("N=1 boundary reference")
        })
        .collect();

    let mut loaded = LoadedModel::load(&load_opts).expect("load N=8 discriminator");
    let LoadedModel::Gemma(g) = &mut loaded else {
        panic!("expected Gemma GGUF")
    };
    g.provision_multi_seq_kv_for_slot_aware(8)
        .expect("provision N=8 KV");
    let mut kv = g.multi_seq_kv.take().expect("N=8 HB scaffold");
    let mut hybrid = g.multi_seq_kv_hybrid.take();
    let mut dense = g.multi_seq_kv_dense.take();
    let mut mlx = g.multi_seq_kv_mlx.take();
    let repeats = positive_test_env_usize("HF2Q_GEMMA_N8_PREFILL_REPEATS", 256, 4_096);
    assert!(
        dense.is_none(),
        "tiny-prefill discriminator requires HF2Q_USE_DENSE=0"
    );
    assert!(
        mlx.is_none(),
        "tiny-prefill discriminator requires HF2Q_TQ_CODEBOOK_BITS=8"
    );
    let actual_regime = if hybrid.is_some() {
        "hybrid"
    } else {
        "full-tq"
    };
    let expected_regime = match std::env::var("HF2Q_GEMMA_N8_EXPECTED_KV_REGIME").as_deref() {
        Ok("hybrid") => "hybrid",
        Ok("full-tq") => "full-tq",
        Ok(other) => {
            panic!("HF2Q_GEMMA_N8_EXPECTED_KV_REGIME must be hybrid or full-tq, got {other:?}")
        }
        Err(error) => panic!("HF2Q_GEMMA_N8_EXPECTED_KV_REGIME is required: {error}"),
    };
    assert_eq!(
        actual_regime, expected_regime,
        "tiny-prefill discriminator did not provision the requested KV regime"
    );
    eprintln!("[gemma-n8-tiny-prefill] kv_regime={actual_regime} cold_rounds={repeats}");

    for iteration in 0..repeats {
        // Canonical cross-slot admission coalesces the eight cold requests,
        // then the tiny-work containment runs their linear prefills before
        // the scheduler's first width-eight batched decode.
        for slot_idx in 0..8 {
            let slot_id = SlotId(slot_idx as u32);
            for buffer in &mut kv {
                buffer.reset_for_slot(slot_id).expect("reset N=8 HB slot");
            }
            if let Some(hybrid) = hybrid.as_mut() {
                for buffer in hybrid {
                    buffer
                        .reset_for_slot(slot_id)
                        .expect("reset N=8 hybrid slot");
                }
            }
        }

        let mut feed_tokens = Vec::with_capacity(8);
        let mut positions = Vec::with_capacity(8);
        for (slot_idx, prompt) in prompts.iter().enumerate() {
            let slot_id = SlotId(slot_idx as u32);
            clear_gemma4_self_mounts(g);
            let actual = g
                .weights
                .forward_prefill_with_soft_tokens_slot_aware(
                    prompt,
                    &[],
                    max_decode_tokens,
                    &mut g.ctx,
                    slot_id,
                    &mut kv,
                    hybrid.as_mut(),
                    dense.as_mut(),
                    mlx.as_mut(),
                )
                .unwrap_or_else(|error| {
                    panic!(
                        "coalesced prefill failed at iteration {iteration}, slot {slot_idx}: {error:#}"
                    )
                });
            clear_gemma4_self_mounts(g);
            assert_eq!(
                actual, expected[slot_idx].0,
                "coalesced prefill diverged at iteration {iteration}, slot {slot_idx}"
            );
            feed_tokens.push(actual);
            positions.push(prompt.len());
        }
        gemma4_test_decode_rows(
            g,
            kv.as_mut_slice(),
            hybrid.as_mut().map(Vec::as_mut_slice),
            feed_tokens.as_mut_slice(),
            positions.as_mut_slice(),
            8,
            &format!("coalesced iteration {iteration}, width 8"),
        );
        assert_eq!(
            feed_tokens,
            expected
                .iter()
                .map(|&(_, second)| second)
                .collect::<Vec<_>>(),
            "coalesced width-eight decode diverged at iteration {iteration}"
        );

        // Non-coalesced admission alternates one new cold lane with the
        // scheduler's current active decode width. Cover every width 1..=8;
        // a following prefill catches persistent corruption from the prior
        // transaction even when its logits remained finite.
        for slot_idx in 0..8 {
            let slot_id = SlotId(slot_idx as u32);
            for buffer in &mut kv {
                buffer.reset_for_slot(slot_id).expect("reset N=8 HB slot");
            }
            if let Some(hybrid) = hybrid.as_mut() {
                for buffer in hybrid {
                    buffer
                        .reset_for_slot(slot_id)
                        .expect("reset N=8 hybrid slot");
                }
            }
        }
        feed_tokens.clear();
        positions.clear();
        for (slot_idx, prompt) in prompts.iter().enumerate() {
            if slot_idx >= 1 {
                gemma4_test_decode_rows(
                    g,
                    kv.as_mut_slice(),
                    hybrid.as_mut().map(Vec::as_mut_slice),
                    feed_tokens.as_mut_slice(),
                    positions.as_mut_slice(),
                    slot_idx,
                    &format!("staircase iteration {iteration}, width {slot_idx}"),
                );
            }
            let slot_id = SlotId(slot_idx as u32);
            clear_gemma4_self_mounts(g);
            let actual = g
                .weights
                .forward_prefill_with_soft_tokens_slot_aware(
                    prompt,
                    &[],
                    max_decode_tokens,
                    &mut g.ctx,
                    slot_id,
                    &mut kv,
                    hybrid.as_mut(),
                    dense.as_mut(),
                    mlx.as_mut(),
                )
                .unwrap_or_else(|error| {
                    panic!(
                        "staircase prefill failed at iteration {iteration}, slot {slot_idx}: {error:#}"
                    )
                });
            clear_gemma4_self_mounts(g);
            assert_eq!(
                actual, expected[slot_idx].0,
                "staircase prefill diverged at iteration {iteration}, slot {slot_idx}"
            );
            feed_tokens.push(actual);
            positions.push(prompt.len());
        }
        gemma4_test_decode_rows(
            g,
            kv.as_mut_slice(),
            hybrid.as_mut().map(Vec::as_mut_slice),
            feed_tokens.as_mut_slice(),
            positions.as_mut_slice(),
            8,
            &format!("staircase iteration {iteration}, width 8"),
        );
    }

    let resume_repeats = positive_test_env_usize("HF2Q_GEMMA_N8_RESUME_REPEATS", 64, 1_024);
    eprintln!("[gemma-n8-tiny-prefill] retained_suffix_rounds={resume_repeats}");
    for iteration in 0..resume_repeats {
        let mut feed_tokens = Vec::with_capacity(2);
        let mut positions = Vec::with_capacity(2);
        for (slot_idx, prompt) in prompts[..2].iter().enumerate() {
            let slot_id = SlotId(slot_idx as u32);
            for buffer in &mut kv {
                buffer
                    .reset_for_slot(slot_id)
                    .expect("reset active HB slot before resume discriminator");
            }
            if let Some(hybrid) = hybrid.as_mut() {
                for buffer in hybrid {
                    buffer
                        .reset_for_slot(slot_id)
                        .expect("reset active hybrid slot before resume discriminator");
                }
            }
            clear_gemma4_self_mounts(g);
            let first = g
                .weights
                .forward_prefill_with_soft_tokens_slot_aware(
                    prompt,
                    &[],
                    max_decode_tokens,
                    &mut g.ctx,
                    slot_id,
                    &mut kv,
                    hybrid.as_mut(),
                    dense.as_mut(),
                    mlx.as_mut(),
                )
                .expect("prefill active row before resume discriminator");
            clear_gemma4_self_mounts(g);
            feed_tokens.push(first);
            positions.push(prompt.len());
        }

        for (case, (prompt, &expected)) in resume_prompts
            .iter()
            .zip(expected_resumes.iter())
            .enumerate()
        {
            let resume_slot = SlotId(2);
            for buffer in &mut kv {
                buffer
                    .reset_for_slot(resume_slot)
                    .expect("reset HB resume slot");
            }
            if let Some(hybrid) = hybrid.as_mut() {
                for buffer in hybrid {
                    buffer
                        .reset_for_slot(resume_slot)
                        .expect("reset hybrid resume slot");
                }
            }
            clear_gemma4_self_mounts(g);
            g.weights
                .forward_prefill_with_soft_tokens_slot_aware(
                    &resume_base,
                    &[],
                    max_decode_tokens,
                    &mut g.ctx,
                    resume_slot,
                    &mut kv,
                    hybrid.as_mut(),
                    dense.as_mut(),
                    mlx.as_mut(),
                )
                .expect("prefill exact live prefix");
            clear_gemma4_self_mounts(g);

            gemma4_test_decode_rows(
                g,
                kv.as_mut_slice(),
                hybrid.as_mut().map(Vec::as_mut_slice),
                feed_tokens.as_mut_slice(),
                positions.as_mut_slice(),
                2,
                &format!("resume iteration {iteration}, suffix case {case}"),
            );
            clear_gemma4_self_mounts(g);
            let actual = g
                .weights
                .forward_prefill_with_soft_tokens_slot_aware_resume(
                    prompt,
                    &[],
                    max_decode_tokens,
                    &mut g.ctx,
                    resume_slot,
                    &mut kv,
                    hybrid.as_mut(),
                    dense.as_mut(),
                    mlx.as_mut(),
                    resume_base.len(),
                )
                .unwrap_or_else(|error| {
                    panic!(
                        "tiny live resume failed at iteration {iteration}, suffix case {case}: {error:#}"
                    )
                });
            clear_gemma4_self_mounts(g);
            assert_eq!(
                actual, expected,
                "tiny live resume diverged at iteration {iteration}, suffix case {case}"
            );
        }

        for (case, (prompt, &expected)) in boundary_prompts
            .iter()
            .zip(expected_boundaries.iter())
            .enumerate()
        {
            let boundary_slot = SlotId(2);
            for buffer in &mut kv {
                buffer
                    .reset_for_slot(boundary_slot)
                    .expect("reset HB boundary slot");
            }
            if let Some(hybrid) = hybrid.as_mut() {
                for buffer in hybrid {
                    buffer
                        .reset_for_slot(boundary_slot)
                        .expect("reset hybrid boundary slot");
                }
            }
            gemma4_test_decode_rows(
                g,
                kv.as_mut_slice(),
                hybrid.as_mut().map(Vec::as_mut_slice),
                feed_tokens.as_mut_slice(),
                positions.as_mut_slice(),
                2,
                &format!("boundary iteration {iteration}, case {case}"),
            );
            clear_gemma4_self_mounts(g);
            let actual = g
                .weights
                .forward_prefill_with_soft_tokens_slot_aware(
                    prompt,
                    &[],
                    max_decode_tokens,
                    &mut g.ctx,
                    boundary_slot,
                    &mut kv,
                    hybrid.as_mut(),
                    dense.as_mut(),
                    mlx.as_mut(),
                )
                .unwrap_or_else(|error| {
                    panic!(
                        "boundary prefill failed at iteration {iteration}, case {case}: {error:#}"
                    )
                });
            clear_gemma4_self_mounts(g);
            assert_eq!(
                actual, expected,
                "boundary prefill diverged at iteration {iteration}, case {case}"
            );
        }
    }
}