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
//! Ignored synthetic postprojection control, not warmup/model policy proof.
use crate::inference::models::deepseek4 as model;
use anyhow::{ensure, Result};
use mlx_native::ops::kv_cache_copy::dispatch_kv_cache_copy;
use mlx_native::{GraphExecutor, KernelRegistry, MlxBuffer, MlxDevice};
use model::cache::{allocate_layer_from_plan, initialize_layer_state};
use model::cache::{Deepseek4CachePlan, LayerCache, LayerCacheSpan};
use model::cache_window_write::{encode_window_write, set_omit_copy};
use model::compressor_tail::{encode_indexer_tail, encode_main_tail};
#[path = "encoder_budget.rs"]
mod budget;
#[path = "encoder_packet.rs"]
mod packet;
use packet::{bf16_words, fill_bf16, window_expected, Packet, CACHE_BYTES, TOTAL_BYTES};

fn roots(layer: &LayerCache) -> impl Iterator<Item = &MlxBuffer> {
    std::iter::once(&layer.attention_kv)
        .chain(layer.indexer_kv.iter())
        .chain(layer.main_kv_state.iter())
        .chain(layer.main_score_state.iter())
        .chain(layer.indexer_kv_state.iter())
        .chain(layer.indexer_score_state.iter())
}
fn initialize(layer: &mut LayerCache, index: usize, canary: u16) -> Result<()> {
    fill_bf16(&mut layer.attention_kv, canary)?;
    if let Some(buffer) = &mut layer.indexer_kv {
        fill_bf16(buffer, canary)?;
    }
    initialize_layer_state(layer, index)?;
    Ok(())
}
fn known_window(layer: &LayerCache) -> Result<()> {
    let words = bf16_words(&layer.window_kv)?;
    for row in 0..64 {
        for col in 0..512 {
            ensure!(
                words[row * 512 + col] == window_expected(row, col),
                "independent window oracle row{row} col{col}"
            );
        }
    }
    Ok(())
}
fn copied_rows(
    destination: &MlxBuffer,
    source: &MlxBuffer,
    width: usize,
    canary: u16,
) -> Result<()> {
    let dst = bf16_words(destination)?;
    let src = bf16_words(source)?;
    ensure!(
        dst[..16 * width] == src[..16 * width],
        "compressed source/destination copy mismatch"
    );
    for row in 0..16 {
        ensure!(
            src[row * width..(row + 1) * width]
                .iter()
                .any(|word| *word != canary),
            "unchanged compressor row{row}"
        );
    }
    Ok(())
}
fn boundaries(layer: &LayerCache, ratio: u32, canary: u16, packet: &Packet) -> Result<()> {
    known_window(layer)?;
    ensure!(
        bf16_words(&layer.window_kv)?[64 * 512..]
            .iter()
            .all(|v| *v == canary),
        "window tail modified"
    );
    if let Some(cache) = &layer.compressed_kv {
        let valid = if ratio == 4 { 16 } else { 0 };
        if valid > 0 {
            copied_rows(cache, &packet.roots[3], 512, canary)?;
        }
        ensure!(
            bf16_words(cache)?[valid * 512..]
                .iter()
                .all(|v| *v == canary),
            "compressed tail modified"
        );
    }
    if let Some(cache) = &layer.indexer_kv {
        copied_rows(cache, &packet.roots[9], 128, canary)?;
        ensure!(
            bf16_words(cache)?[16 * 128..].iter().all(|v| *v == canary),
            "indexer tail modified"
        );
    }
    Ok(())
}
fn semantic<'a>(layer: &'a LayerCache, ratio: u32) -> Result<Vec<&'a [u8]>> {
    let mut result = Vec::with_capacity(7);
    result.push(&layer.window_kv.as_slice::<u8>()?[..64 * 512 * 2]);
    if ratio == 4 {
        result.push(&layer.compressed_kv.as_ref().unwrap().as_slice::<u8>()?[..16 * 512 * 2]);
        result.push(&layer.indexer_kv.as_ref().unwrap().as_slice::<u8>()?[..16 * 128 * 2]);
    }
    for buffer in layer
        .main_kv_state
        .iter()
        .chain(layer.main_score_state.iter())
        .chain(layer.indexer_kv_state.iter())
        .chain(layer.indexer_score_state.iter())
    {
        result.push(buffer.as_slice::<u8>()?);
    }
    Ok(result)
}
fn state_reset(layer: &mut LayerCache, index: usize) -> Result<()> {
    initialize_layer_state(layer, index)?;
    for state in layer
        .main_kv_state
        .iter()
        .chain(layer.indexer_kv_state.iter())
    {
        ensure!(state.as_slice::<f32>()?.iter().all(|v| v.to_bits() == 0));
    }
    for state in layer
        .main_score_state
        .iter()
        .chain(layer.indexer_score_state.iter())
    {
        ensure!(state
            .as_slice::<f32>()?
            .iter()
            .all(|v| v.to_bits() == f32::NEG_INFINITY.to_bits()));
    }
    Ok(())
}
#[allow(clippy::too_many_arguments)]
fn encode<'a>(
    exec: &'a GraphExecutor,
    registry: &mut KernelRegistry,
    device: &MlxDevice,
    cfg: &model::Deepseek4Config,
    layer: &LayerCache,
    span: &LayerCacheSpan,
    ratio: u32,
    packet: &Packet,
    window_only: bool,
) -> Result<mlx_native::graph::GraphSession<'a>> {
    let mut session = exec.begin()?;
    if ratio == 0 {
        // Ratio0 is allocation/layout + copy primitive ONLY, not dense encoder.
        dispatch_kv_cache_copy(
            session.encoder_mut(),
            registry,
            device.metal_device(),
            &packet.roots[12],
            &layer.window_kv,
            0,
            512,
            64,
            128,
            true,
        )?;
    } else {
        encode_window_write(
            &mut session,
            registry,
            device,
            layer,
            &packet.roots[12],
            &[&packet.roots[12]],
            Some(span),
            None,
            64,
            128,
            512,
        )?;
        if !window_only {
            let main = packet.main(ratio)?;
            let main = main.borrowed();
            let positions = packet.positions(if ratio == 4 { 16 } else { 0 })?;
            let cache = layer.compressed_kv.as_ref().unwrap();
            encode_main_tail(
                &mut session,
                registry,
                device,
                cfg,
                ratio,
                64,
                0,
                0,
                if ratio == 4 { 16 } else { 0 },
                cache,
                cache.shape()[0],
                layer.main_kv_state.as_ref().unwrap(),
                layer.main_score_state.as_ref().unwrap(),
                &main,
                &positions,
                &packet.roots[14],
            )?;
            if ratio == 4 {
                let indexer = packet.indexer()?;
                let indexer = indexer.borrowed();
                let cache = layer.indexer_kv.as_ref().unwrap();
                encode_indexer_tail(
                    &mut session,
                    registry,
                    device,
                    cfg,
                    64,
                    0,
                    0,
                    16,
                    cache,
                    cache.shape()[0],
                    layer.indexer_kv_state.as_ref().unwrap(),
                    layer.indexer_score_state.as_ref().unwrap(),
                    &indexer,
                    &positions,
                    &packet.roots[14],
                )?;
            }
        }
    }
    Ok(session)
}
fn drain_pair(
    first: mlx_native::graph::GraphSession<'_>,
    second: mlx_native::graph::GraphSession<'_>,
    ratio: u32,
    negative: bool,
) -> Result<()> {
    // Both fallible encodes finished before either commit. All packet/cache
    // roots and registry outlive this pair. No CPU access between submissions.
    let first = first.commit();
    let second = second.commit();
    let b = second.wait_until_completed();
    let a = first.wait_until_completed();
    eprintln!(
        "Q5_CACHE_COMMANDS ratio={ratio} negative={negative} small_ok={} large_ok={}",
        a.is_ok(),
        b.is_ok()
    );
    // Collect BOTH results before any assertion/error return/readback.
    a?;
    b?;
    Ok(())
}
struct OmissionGuard;
impl Drop for OmissionGuard {
    fn drop(&mut self) {
        set_omit_copy(false);
    }
}

#[test]
#[ignore = "synthetic 46,063,488B requested Metal payload; external process-group watchdog required"]
fn actual_cache_write_capacity_parity_fresh64() -> Result<()> {
    for name in [
        "MLX_UNRETAINED_REFS",
        "MLX_PROFILE_DISPATCH",
        "MLX_PROFILE_CB",
        "MLX_ENCODE_TRACE",
        "MLX_PROFILE_BARRIERS",
        "HF2Q_AUTO_BARRIER",
        "HF2Q_FORCE_SERIAL_DISPATCH",
        "HF2Q_PIPELINE_TG_MULT_HINT",
        "MLX_METAL_CAPTURE",
        "METAL_CAPTURE_ENABLED",
    ] {
        ensure!(std::env::var_os(name).is_none(), "requires {name} absent");
    }
    let cfg = super::official_config();
    let plans = [
        Deepseek4CachePlan::for_context(&cfg, 128)?,
        Deepseek4CachePlan::for_context(&cfg, 131072)?,
    ];
    let spans = [
        plans[0].plan_prefill_at(0, 64, 0)?,
        plans[1].plan_prefill_at(0, 64, 0)?,
    ];
    ensure!(spans[0] == spans[1]);
    let device = match MlxDevice::new() {
        Ok(device) => device,
        Err(error) => {
            eprintln!("Q5_CACHE_CAPABILITY unavailable_or_unknown device={error}");
            return Ok(());
        }
    };
    let exec = GraphExecutor::new(device.clone());
    let mut registry = KernelRegistry::new();
    let mut packet = Packet::allocate(&device, &cfg)?;
    let mut reference = vec![0u8; 1_048_576].into_boxed_slice();
    budget::check(&cfg, &plans, &spans, &packet)?;
    let mut cache_bytes = 0u64;
    let mut cache_roots = 0usize;
    for (index, ratio) in [(0, 0u32), (2, 4), (3, 128)] {
        let mut layers = [
            allocate_layer_from_plan(&device, &plans[0].layers[index], false)?,
            allocate_layer_from_plan(&device, &plans[1].layers[index], false)?,
        ];
        for layer in &layers {
            for root in roots(layer) {
                cache_bytes += root.byte_len() as u64;
                cache_roots += 1;
                ensure!(cache_bytes <= CACHE_BYTES && cache_roots <= 20);
            }
        }
        for (arm, layer) in layers.iter_mut().enumerate() {
            initialize(layer, index, if arm == 0 { 0x4480 } else { 0xc480 })?;
        }
        packet.prepare()?;
        let first = encode(
            &exec,
            &mut registry,
            &device,
            &cfg,
            &layers[0],
            &spans[0].layers[index],
            ratio,
            &packet,
            false,
        )?;
        let second = encode(
            &exec,
            &mut registry,
            &device,
            &cfg,
            &layers[1],
            &spans[1].layers[index],
            ratio,
            &packet,
            false,
        )?;
        drain_pair(first, second, ratio, false)?;
        for (arm, layer) in layers.iter().enumerate() {
            boundaries(
                layer,
                ratio,
                if arm == 0 { 0x4480 } else { 0xc480 },
                &packet,
            )?;
            let parts = semantic(layer, ratio)?;
            let mut offset = 0;
            for part in parts {
                let end = offset + part.len();
                ensure!(end <= reference.len());
                if arm == 0 {
                    reference[offset..end].copy_from_slice(part);
                } else {
                    ensure!(
                        &reference[offset..end] == part,
                        "ratio{ratio} semantic bit mismatch"
                    );
                }
                offset = end;
            }
            eprintln!(
                "Q5_CACHE_ARM completed ratio={ratio} capacity={} semantic_bytes={offset} scope={}",
                plans[arm].context_length,
                if ratio == 0 {
                    "allocation_layout_copy_primitive"
                } else {
                    "production_postprojection_tails"
                }
            );
        }
        if ratio == 4 {
            // Actual native parameter rejection; this session is NEVER submitted.
            let main = packet.main(4)?;
            let main = main.borrowed();
            let mut rejected_session = exec.begin()?;
            let invalid = mlx_native::ops::deepseek_compressor::dispatch_deepseek_compressor(
                rejected_session.encoder_mut(),
                &mut registry,
                &device,
                main.kv,
                main.score,
                main.ape,
                main.norm,
                layers[0].main_kv_state.as_ref().unwrap(),
                layers[0].main_score_state.as_ref().unwrap(),
                main.output,
                main.output,
                &mlx_native::ops::deepseek_compressor::DeepSeekCompressorParams {
                    batch: 1,
                    seq_len: 64,
                    start_pos: 0,
                    ratio: 4,
                    head_dim: 512,
                    cache_len: 15,
                    epsilon: cfg.rms_norm_eps,
                    write_cache: 0,
                },
            );
            drop(rejected_session);
            ensure!(invalid.is_err(), "native accepted cache_len15 for16 groups");
            eprintln!("Q5_CACHE_NEGATIVE short_cache_rejected=true");
            // Same canary makes equally broken windows agree; independent known
            // integer-BF16 expected writes must STILL reject both broken arms.
            for layer in &mut layers {
                fill_bf16(&mut layer.attention_kv, 0x4480)?;
            }
            set_omit_copy(true);
            let guard = OmissionGuard;
            let a = encode(
                &exec,
                &mut registry,
                &device,
                &cfg,
                &layers[0],
                &spans[0].layers[index],
                ratio,
                &packet,
                true,
            )?;
            let b = encode(
                &exec,
                &mut registry,
                &device,
                &cfg,
                &layers[1],
                &spans[1].layers[index],
                ratio,
                &packet,
                true,
            )?;
            drop(guard);
            drain_pair(a, b, ratio, true)?;
            ensure!(bf16_words(&layers[0].window_kv)? == bf16_words(&layers[1].window_kv)?);
            ensure!(
                known_window(&layers[0]).is_err() && known_window(&layers[1]).is_err(),
                "common omission escaped independent oracle"
            );
            eprintln!("Q5_CACHE_NEGATIVE common_omission_rejected=true equal_broken_windows=true");
        }
        for layer in &mut layers {
            state_reset(layer, index)?;
        }
    }
    ensure!(cache_bytes == CACHE_BYTES && cache_roots == 20);
    let bookkeeping = budget::check(&cfg, &plans, &spans, &packet)?;
    ensure!(reference.len() + 65536 + bookkeeping + 256 + 1024 <= 2_163_968);
    eprintln!("Q5_CACHE_HOST scoped_bookkeeping_bytes={bookkeeping} scoped_reference_bytes={} opaque_metadata_known=false",reference.len());
    eprintln!("Q5_CACHE_CAPABILITY completed temp_roots=15 cache_roots={cache_roots} requested_payload_bytes={TOTAL_BYTES} model_loaded=false runtime_admission=false opaque_metadata_known=false");
    Ok(())
}