hf2q 0.1.33

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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! The draft prefetch (`HF2Q_DFLASH2_DRAFT_PREFETCH=1`) against the stock
//! order of a step that settles acceptance on the host.
//!
//! Stock: the context commit is committed and waited for, and the next step
//! issues its draft. Prefetch: the context commit is submitted reading its
//! own copy of the step's positions, the next step's draft is committed
//! behind it, a replay stand-in runs beside both, and the next step's draft
//! call adopts the command when its inputs are the prefetched ones. (The
//! scheduler commits its real replay between the two; the stand-in here is
//! committed and waited for behind the draft, which the bits do not see.)
//!
//! These tests drive the runtime calls the scheduler's prefetch branch
//! makes, on a synthetic checkpoint and both draft routes:
//!
//! - adopted prefetches: every draft output and the rings are the stock
//!   order's bits at every step, past a ring wrap, with full, partial and
//!   empty retained counts, through both draft entry points;
//! - a next step with other inputs (a lane left, another anchor, another
//!   row limit, the other entry point) and a ring change between the steps
//!   (a cleared lane, a prefill ingest) drop the prefetched command and
//!   draft again, with the stock bits;
//! - a step that fails after its submissions leaves no prefetched draft,
//!   clears its lanes and leaves the other lanes' bytes;
//! - the draft outputs are unreadable from a prefetch to the next draft.
//!
//! Needs Apple Metal (`hf2q_gpu_test_lock`):
//!
//! ```text
//! cargo test --release --bin hf2q draft_prefetch_tests
//! ```

use mlx_native::{DType, GgmlType, KernelRegistry, MlxDevice};

use super::context_commit_tests::{lane_rings, random, rings, runtime, Replay, LANES};
use super::*;
use crate::inference::spec_decode::dflash::hidden_capture::DFlashCaptureSession;
use crate::inference::spec_decode::dflash2::config::DFlash2TargetShape;
use crate::serve::forward_mlx_shared::MlxQWeight;
use crate::serve::gpu::QuantWeightInfo;

/// A dense F32 target of the synthetic checkpoint's shape.
struct Target {
    embedding: MlxQWeight,
    head: MlxQWeight,
}

impl Target {
    fn new(device: &MlxDevice, config: &DFlash2Config) -> Self {
        let dense = |seed: u64, scale: f32| {
            let (rows, cols) = (config.vocab_size, config.hidden_size);
            let mut buffer = device
                .alloc_buffer(rows * cols * 4, DType::F32, vec![rows, cols])
                .unwrap();
            let mut state = seed;
            for value in buffer.as_mut_slice::<f32>().unwrap() {
                *value = scale * random(&mut state);
            }
            MlxQWeight {
                buffer,
                info: QuantWeightInfo {
                    ggml_dtype: GgmlType::F32,
                    rows,
                    cols,
                },
                affine: None,
                f16_shadow: None,
                decode_record_q6k_m1: std::sync::OnceLock::new(),
            }
        };
        Self {
            embedding: dense(101, 1.0),
            head: dense(103, 0.25),
        }
    }
}

impl DFlash2TargetFamily for Target {
    fn family_name(&self) -> &'static str {
        "synthetic"
    }

    fn target_shape(&self) -> DFlash2TargetShape<'_> {
        DFlash2TargetShape {
            architecture: "synthetic",
            hidden_size: self.embedding.info.cols,
            vocab_size: self.embedding.info.rows,
            num_hidden_layers: 1,
        }
    }

    fn embedding(&self) -> Result<&MlxQWeight> {
        Ok(&self.embedding)
    }

    fn vocabulary_head(&self) -> Result<&MlxQWeight> {
        Ok(&self.head)
    }

    fn embedding_scale(&self) -> f32 {
        1.0
    }
}

/// One step's draft call.
#[derive(Clone, Debug, PartialEq)]
struct Draft {
    anchors: [u32; LANES],
    maximum_rows: [u32; LANES],
    /// `None`: [`DFlash2Runtime::run_draft`]; `Some(order)`:
    /// [`DFlash2Runtime::run_draft_for_gpu_target`].
    gpu_order: Option<Vec<u32>>,
    repetition: Option<DraftRepetitionPrior>,
}

const UNIFORMS: [f32; LANES * 7] = [0.5; LANES * 7];
const TEMPERATURES: [f32; LANES] = [0.0; LANES];

fn positions(runtime: &DFlash2Runtime) -> Vec<u32> {
    runtime
        .lane_lengths
        .iter()
        .map(|&length| length as u32)
        .collect()
}

/// Issue `draft` as the scheduler does and return every output a step reads
/// (waiting for an unwaited command first, as the target's verify would).
fn draft(
    runtime: &mut DFlash2Runtime,
    device: &MlxDevice,
    registry: &mut KernelRegistry,
    target: &Target,
    draft: &Draft,
) -> Vec<Vec<u32>> {
    let positions = positions(runtime);
    match &draft.gpu_order {
        None => runtime
            .run_draft(
                device,
                registry,
                target,
                &draft.anchors,
                &positions,
                &TEMPERATURES,
                0,
                &draft.maximum_rows,
                &UNIFORMS,
                draft.repetition.as_ref(),
            )
            .unwrap(),
        Some(order) => {
            runtime
                .run_draft_for_gpu_target(
                    device,
                    registry,
                    target,
                    &draft.anchors,
                    &positions,
                    &TEMPERATURES,
                    0,
                    &draft.maximum_rows,
                    order,
                    &UNIFORMS,
                    draft.repetition.as_ref(),
                )
                .unwrap();
        }
    }
    outputs(runtime)
}

fn outputs(runtime: &DFlash2Runtime) -> Vec<Vec<u32>> {
    let bits = |buffer: &mlx_native::MlxBuffer| {
        buffer
            .as_slice::<f32>()
            .unwrap()
            .iter()
            .map(|value| value.to_bits())
            .collect::<Vec<_>>()
    };
    vec![
        runtime.proposals().unwrap().to_vec(),
        runtime.verify_rows().unwrap().to_vec(),
        runtime
            .workspace
            .target_tokens
            .as_slice::<u32>()
            .unwrap()
            .to_vec(),
        bits(&runtime.workspace.confidence),
        bits(&runtime.workspace.proposal_probabilities),
        bits(&runtime.workspace.logits),
    ]
}

/// The rows a target verify would capture for the step.
fn capture(runtime: &mut DFlash2Runtime, state: &mut u64) {
    for value in runtime
        .workspace
        .captured_target_hidden
        .as_mut_slice::<f32>()
        .unwrap()
    {
        *value = 2.0 * random(state);
    }
}

/// The scheduler's prefetch branch after a step's plan: context commit
/// submitted, `next` committed behind it, the replay stand-in beside both,
/// then the waits and the settle.
fn commit_and_prefetch(
    runtime: &mut DFlash2Runtime,
    device: &MlxDevice,
    registry: &mut KernelRegistry,
    target: &Target,
    replay: &Replay,
    retained: &[u32; LANES],
    next: Option<&Draft>,
    step_lanes: &[usize],
    replay_fails: bool,
) -> (bool, Result<()>) {
    let submitted = runtime.submit_target_context_detached(device, registry, retained);
    if let (Ok(_), Some(next)) = (&submitted, next) {
        let positions = positions(runtime);
        runtime
            .prefetch_draft(
                device,
                registry,
                target,
                &next.anchors,
                &positions,
                &TEMPERATURES,
                0,
                &next.maximum_rows,
                next.gpu_order.as_deref().unwrap_or(&[]),
                &UNIFORMS,
                next.gpu_order.is_none(),
                next.repetition.as_ref(),
            )
            .unwrap();
        assert!(runtime.prefetch_outstanding());
    }
    replay.run(device, registry).unwrap();
    let committed = !replay_fails;
    let context =
        submitted.and_then(|submitted| runtime.finish_target_context(submitted, retained, false));
    (
        committed,
        runtime.settle_submitted_step(committed, context, step_lanes),
    )
}

fn step_draft(step: u32, gpu: bool) -> Draft {
    Draft {
        anchors: [1 + step % 60, 2 + (step * 7) % 61, 3 + (step * 11) % 59, 5],
        maximum_rows: [8, 8, 8, 8],
        gpu_order: gpu.then(|| vec![0, 1, 2, 3]),
        repetition: None,
    }
}

#[test]
fn an_adopted_prefetch_is_the_stock_draft_bitwise() {
    let _gpu = crate::inference::hf2q_gpu_test_lock();
    let Ok(device) = MlxDevice::new() else {
        return;
    };
    for staged in [false, true] {
        for gpu in [false, true] {
            let mut registry = KernelRegistry::new();
            let mut stock = runtime(&device, staged);
            let mut ahead = runtime(&device, staged);
            let target = Target::new(&device, &stock.config);
            let replay = Replay::new(&device);
            let (mut stock_state, mut ahead_state) = (41_u64, 41_u64);
            let mut varied = std::collections::HashSet::new();
            for step in 0..40_u32 {
                let this = step_draft(step, gpu);
                let expected = draft(&mut stock, &device, &mut registry, &target, &this);
                let adopted_before = ahead.prefetch_counts().adopted;
                let actual = draft(&mut ahead, &device, &mut registry, &target, &this);
                assert!(
                    actual == expected,
                    "staged={staged} gpu={gpu} step {step}: draft outputs differ"
                );
                assert_eq!(
                    ahead.prefetch_counts().adopted - adopted_before,
                    u64::from(step > 0),
                    "staged={staged} gpu={gpu} step {step}: the prefetched draft was not adopted"
                );
                varied.insert(expected[0].clone());

                let retained = [8, step % 9, 3, 0];
                capture(&mut stock, &mut stock_state);
                stock
                    .commit_target_context(&device, &mut registry, &retained)
                    .unwrap();

                capture(&mut ahead, &mut ahead_state);
                let next = step_draft(step + 1, gpu);
                let (committed, context) = commit_and_prefetch(
                    &mut ahead,
                    &device,
                    &mut registry,
                    &target,
                    &replay,
                    &retained,
                    Some(&next),
                    &[0, 1, 2, 3],
                    false,
                );
                assert!(committed);
                context.unwrap();
                assert_eq!(ahead.lane_lengths, stock.lane_lengths, "step {step}");
            }
            // Everything after the last prefetch: the rings need the queue idle.
            ahead.settle_prefetch().unwrap();
            assert!(
                rings(&ahead) == rings(&stock),
                "staged={staged} gpu={gpu}: the prefetch order left other ring bytes"
            );
            assert!(stock.lane_lengths[0] > stock.config.sliding_window);
            assert!(varied.len() > 8, "the draft outputs never changed");
            let counts = ahead.prefetch_counts();
            assert_eq!(
                (counts.prefetched, counts.adopted, counts.discarded),
                (40, 39, 1)
            );
        }
    }
}

#[test]
fn a_prefetch_that_does_not_match_the_next_step_is_dropped() {
    let _gpu = crate::inference::hf2q_gpu_test_lock();
    let Ok(device) = MlxDevice::new() else {
        return;
    };
    for staged in [false, true] {
        let mut registry = KernelRegistry::new();
        let mut stock = runtime(&device, staged);
        let mut ahead = runtime(&device, staged);
        let target = Target::new(&device, &stock.config);
        let replay = Replay::new(&device);
        let (mut stock_state, mut ahead_state) = (43_u64, 43_u64);
        let guessed = step_draft(3, true);
        // What the next step really asks for, each differing in one input.
        let actual_next = [
            Draft {
                anchors: [9, 9, 9, 9],
                ..guessed.clone()
            },
            Draft {
                maximum_rows: [8, 3, 8, 8],
                ..guessed.clone()
            },
            Draft {
                gpu_order: Some(vec![0, 2, 3]),
                ..guessed.clone()
            },
            Draft {
                gpu_order: Some(vec![3, 2, 1, 0]),
                ..guessed.clone()
            },
            Draft {
                gpu_order: None,
                ..guessed.clone()
            },
        ];
        for (case, real) in actual_next.iter().enumerate() {
            let retained = [8, 5, 0, 2];
            capture(&mut stock, &mut stock_state);
            stock
                .commit_target_context(&device, &mut registry, &retained)
                .unwrap();
            capture(&mut ahead, &mut ahead_state);
            let (_, context) = commit_and_prefetch(
                &mut ahead,
                &device,
                &mut registry,
                &target,
                &replay,
                &retained,
                Some(&guessed),
                &[0, 1, 2, 3],
                false,
            );
            context.unwrap();
            // The step's outputs are the next draft's now.
            assert!(ahead.proposals().is_err() && ahead.verify_rows().is_err());
            assert!(ahead.select_verify_rows(&[0], &[8, 8, 8, 8]).is_err());

            let counts = ahead.prefetch_counts();
            let expected = draft(&mut stock, &device, &mut registry, &target, real);
            let outputs = draft(&mut ahead, &device, &mut registry, &target, real);
            assert!(
                outputs == expected,
                "staged={staged} case {case}: a dropped prefetch changed the draft"
            );
            let after = ahead.prefetch_counts();
            assert_eq!(
                (after.adopted, after.discarded),
                (counts.adopted, counts.discarded + 1),
                "staged={staged} case {case}"
            );
            assert!(
                rings(&ahead) == rings(&stock),
                "staged={staged} case {case}"
            );
        }

        // A ring change between the steps: a cleared lane, a prefill ingest.
        let width = stock.config.fc_input_dim();
        let mut ingest = DFlashCaptureSession::new_shared(
            &device, stock.config.target_layer_ids.clone(), 11,
            width / stock.config.target_layer_ids.len(), false,
        ).unwrap();
        let mut state = 47_u64;
        for value in ingest.hidden_output.iter_mut() {
            *value = random(&mut state);
        }
        for change in 0..2 {
            let retained = [3, 8, 8, 1];
            let next = step_draft(20 + change, true);
            capture(&mut stock, &mut stock_state);
            stock
                .commit_target_context(&device, &mut registry, &retained)
                .unwrap();
            capture(&mut ahead, &mut ahead_state);
            commit_and_prefetch(
                &mut ahead,
                &device,
                &mut registry,
                &target,
                &replay,
                &retained,
                Some(&next),
                &[0, 1, 2, 3],
                false,
            )
            .1
            .unwrap();
            let discarded = ahead.prefetch_counts().discarded;
            for runtime in [&mut stock, &mut ahead] {
                if change == 0 {
                    runtime.clear_lane(2).unwrap();
                } else {
                    let start = runtime.lane_lengths[1];
                    runtime
                        .ingest_target_capture(&device, &mut registry, &ingest, 1, start)
                        .unwrap();
                }
            }
            assert_eq!(ahead.prefetch_counts().discarded, discarded + 1);
            assert!(!ahead.prefetch_outstanding());
            let expected = draft(&mut stock, &device, &mut registry, &target, &next);
            let outputs = draft(&mut ahead, &device, &mut registry, &target, &next);
            assert!(
                outputs == expected,
                "staged={staged} change {change}: a draft over changed rings was adopted"
            );
            assert!(rings(&ahead) == rings(&stock));
            assert_eq!(ahead.lane_lengths, stock.lane_lengths);
        }
    }
}

#[test]
fn a_step_that_fails_after_its_submissions_leaves_no_prefetched_draft() {
    let _gpu = crate::inference::hf2q_gpu_test_lock();
    let Ok(device) = MlxDevice::new() else {
        return;
    };
    for staged in [false, true] {
        let mut registry = KernelRegistry::new();
        let mut runtime = runtime(&device, staged);
        let target = Target::new(&device, &runtime.config);
        let replay = Replay::new(&device);
        let mut state = 53_u64;
        let mut step = 0;
        // History in every lane, past a wrap in lanes 0 and 2.
        for _ in 0..36 {
            draft(
                &mut runtime,
                &device,
                &mut registry,
                &target,
                &step_draft(step, true),
            );
            capture(&mut runtime, &mut state);
            runtime
                .commit_target_context(&device, &mut registry, &[8, 5, 8, 2])
                .unwrap();
            step += 1;
        }
        let lengths = runtime.lane_lengths.clone();
        let untouched = [lane_rings(&runtime, 2), lane_rings(&runtime, 3)];

        // The failing step holds lanes 0 and 1.
        draft(
            &mut runtime,
            &device,
            &mut registry,
            &target,
            &step_draft(step, true),
        );
        capture(&mut runtime, &mut state);
        let next = Draft {
            gpu_order: Some(vec![0, 1]),
            ..step_draft(step + 1, true)
        };
        let (committed, context) = commit_and_prefetch(
            &mut runtime,
            &device,
            &mut registry,
            &target,
            &replay,
            &[8, 3, 0, 0],
            Some(&next),
            &[0, 1],
            true,
        );
        assert!(!committed);
        context.expect("both commands were waited for and the lanes cleared");
        assert!(!runtime.prefetch_outstanding());
        assert_eq!(runtime.lane_lengths, [0, 0, lengths[2], lengths[3]]);
        for lane in [0, 1] {
            assert!(
                lane_rings(&runtime, lane)
                    .iter()
                    .flatten()
                    .all(|&bits| bits == 0),
                "staged={staged}: lane {lane} of the failed step was not cleared"
            );
        }
        assert!(
            [lane_rings(&runtime, 2), lane_rings(&runtime, 3)] == untouched,
            "staged={staged}: a lane outside the failed step changed"
        );
        // The cleared lanes draft and commit like fresh ones.
        let adopted = runtime.prefetch_counts().adopted;
        draft(
            &mut runtime,
            &device,
            &mut registry,
            &target,
            &Draft {
                gpu_order: Some(vec![0, 1]),
                ..step_draft(step + 1, true)
            },
        );
        assert_eq!(runtime.prefetch_counts().adopted, adopted);
        capture(&mut runtime, &mut state);
        runtime
            .commit_target_context(&device, &mut registry, &[8, 8, 0, 0])
            .unwrap();
        assert_eq!(runtime.lane_lengths, [8, 8, lengths[2], lengths[3]]);
    }
}

#[test]
fn changed_repetition_history_cannot_reuse_prefetched_logits() {
    let _gpu = crate::inference::hf2q_gpu_test_lock();
    let device = MlxDevice::new().unwrap();
    for staged in [false, true] {
        for gpu in [false, true] {
            let mut registry = KernelRegistry::new();
            let mut stock = runtime(&device, staged);
            let mut ahead = runtime(&device, staged);
            stock.proposal_repetition_enabled = true;
            ahead.proposal_repetition_enabled = true;
            let target = Target::new(&device, &stock.config);
            let replay = Replay::new(&device);
            let mut guessed = step_draft(3, gpu);
            let mut prior = DraftRepetitionPrior::default();
            prior.set_lane(0, 1.5, [1_u32].into_iter()).unwrap();
            guessed.repetition = Some(prior);
            let mut real = guessed.clone();
            prior.set_lane(0, 1.5, [2_u32].into_iter()).unwrap();
            real.repetition = Some(prior);

            let retained = [8, 5, 0, 2];
            capture(&mut stock, &mut 43);
            stock
                .commit_target_context(&device, &mut registry, &retained)
                .unwrap();
            capture(&mut ahead, &mut 43);
            let (_, context) = commit_and_prefetch(
                &mut ahead,
                &device,
                &mut registry,
                &target,
                &replay,
                &retained,
                Some(&guessed),
                &[0, 1, 2, 3],
                false,
            );
            context.unwrap();
            // Replay's explicit wait is behind the draft, so this internal
            // buffer read is safe despite the public prefetch readback guard.
            let stale_logits = ahead.workspace.logits.as_slice::<u32>().unwrap().to_vec();
            let expected = draft(&mut stock, &device, &mut registry, &target, &real);
            let expected_logits = stock.workspace.logits.as_slice::<u32>().unwrap().to_vec();
            assert_ne!(
                stale_logits, expected_logits,
                "fixture must expose stale penalty output"
            );
            let actual = draft(&mut ahead, &device, &mut registry, &target, &real);
            assert_eq!(actual, expected);
            assert_eq!(
                ahead.workspace.logits.as_slice::<u32>().unwrap(),
                expected_logits
            );
        }
    }
}