hf2q 0.1.3

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
use super::super::gpu_full_attn::upload_f32;
use super::super::kv_cache::HybridKvCache;
use super::super::mtp::MtpFfnKind;
use super::super::mtp_weights_load::mtp_tensor_names;
use super::super::{default_layer_types, Qwen35Config, Qwen35LayerKind, Qwen35Variant};
use super::load_mtp_weights_if_present;
use mlx_native::gguf::GgufFile;
use mlx_native::{KernelRegistry, MlxDevice};
use std::io::Write;

struct TestTensor {
    name: &'static str,
    dims: Vec<u64>,
    data: Vec<f32>,
}

fn tiny_cfg(mtp_layers: u32) -> Qwen35Config {
    Qwen35Config {
        variant: Qwen35Variant::Dense,
        hidden_size: 32,
        num_hidden_layers: 2,
        num_attention_heads: 1,
        num_key_value_heads: 1,
        head_dim: 32,
        linear_num_key_heads: 1,
        linear_num_value_heads: 1,
        linear_key_head_dim: 32,
        linear_value_head_dim: 32,
        linear_conv_kernel_dim: 4,
        full_attention_interval: 2,
        layer_types: default_layer_types(2, 2),
        partial_rotary_factor: 1.0,
        rope_theta: 1_000_000.0,
        rotary_dim: 32,
        mrope_section: [8, 8, 8, 8],
        mrope_interleaved: true,
        rms_norm_eps: 1e-6,
        max_position_embeddings: 128,
        vocab_size: 64,
        attn_output_gate: true,
        mtp_num_hidden_layers: mtp_layers,
        mtp_use_dedicated_embeddings: true,
        intermediate_size: Some(32),
        moe: None,
    }
}

fn ones(n: usize) -> Vec<f32> {
    vec![1.0; n]
}

fn zeros(n: usize) -> Vec<f32> {
    vec![0.0; n]
}

fn tiny_tensors() -> Vec<TestTensor> {
    let h = 32usize;
    let v = 64usize;
    let m = 32usize;
    vec![
        TestTensor {
            name: "blk.2.nextn.enorm.weight",
            dims: vec![h as u64],
            data: ones(h),
        },
        TestTensor {
            name: "blk.2.nextn.hnorm.weight",
            dims: vec![h as u64],
            data: ones(h),
        },
        TestTensor {
            name: "blk.2.nextn.eh_proj.weight",
            dims: vec![(2 * h) as u64, h as u64],
            data: ones(2 * h * h),
        },
        TestTensor {
            name: "blk.2.nextn.embed_tokens.weight",
            dims: vec![h as u64, v as u64],
            data: zeros(v * h),
        },
        TestTensor {
            name: "blk.2.nextn.shared_head_norm.weight",
            dims: vec![h as u64],
            data: ones(h),
        },
        TestTensor {
            name: "blk.2.nextn.shared_head_head.weight",
            dims: vec![h as u64, v as u64],
            data: zeros(v * h),
        },
        TestTensor {
            name: "blk.2.attn_norm.weight",
            dims: vec![h as u64],
            data: ones(h),
        },
        TestTensor {
            name: "blk.2.post_attention_norm.weight",
            dims: vec![h as u64],
            data: ones(h),
        },
        TestTensor {
            name: "blk.2.attn_q.weight",
            dims: vec![h as u64, h as u64],
            data: zeros(h * h),
        },
        TestTensor {
            name: "blk.2.attn_k.weight",
            dims: vec![h as u64, h as u64],
            data: zeros(h * h),
        },
        TestTensor {
            name: "blk.2.attn_v.weight",
            dims: vec![h as u64, h as u64],
            data: zeros(h * h),
        },
        TestTensor {
            name: "blk.2.attn_output.weight",
            dims: vec![h as u64, h as u64],
            data: zeros(h * h),
        },
        TestTensor {
            name: "blk.2.attn_q_norm.weight",
            dims: vec![h as u64],
            data: ones(h),
        },
        TestTensor {
            name: "blk.2.attn_k_norm.weight",
            dims: vec![h as u64],
            data: ones(h),
        },
        TestTensor {
            name: "blk.2.ffn_gate.weight",
            dims: vec![h as u64, m as u64],
            data: zeros(m * h),
        },
        TestTensor {
            name: "blk.2.ffn_up.weight",
            dims: vec![h as u64, m as u64],
            data: zeros(m * h),
        },
        TestTensor {
            name: "blk.2.ffn_down.weight",
            dims: vec![m as u64, h as u64],
            data: zeros(h * m),
        },
    ]
}

fn write_gguf(path: &std::path::Path, tensors: &[TestTensor]) {
    let mut buf = Vec::new();
    buf.extend_from_slice(b"GGUF");
    buf.extend_from_slice(&3u32.to_le_bytes());
    buf.extend_from_slice(&(tensors.len() as u64).to_le_bytes());
    buf.extend_from_slice(&0u64.to_le_bytes());

    let mut offset = 0u64;
    let mut offsets = Vec::with_capacity(tensors.len());
    for t in tensors {
        while offset % 32 != 0 {
            offset += 1;
        }
        offsets.push(offset);
        offset += (t.data.len() * 4) as u64;
    }

    for (t, off) in tensors.iter().zip(offsets.iter()) {
        buf.extend_from_slice(&(t.name.len() as u64).to_le_bytes());
        buf.extend_from_slice(t.name.as_bytes());
        buf.extend_from_slice(&(t.dims.len() as u32).to_le_bytes());
        for d in &t.dims {
            buf.extend_from_slice(&d.to_le_bytes());
        }
        buf.extend_from_slice(&0u32.to_le_bytes());
        buf.extend_from_slice(&off.to_le_bytes());
    }
    while buf.len() % 32 != 0 {
        buf.push(0);
    }
    let data_start = buf.len();
    for (t, off) in tensors.iter().zip(offsets.iter()) {
        while (buf.len() - data_start) < *off as usize {
            buf.push(0);
        }
        for f in &t.data {
            buf.extend_from_slice(&f.to_le_bytes());
        }
    }

    let mut f = std::fs::File::create(path).expect("create gguf");
    f.write_all(&buf).expect("write gguf");
    f.flush().expect("flush gguf");
}

fn try_device() -> Option<MlxDevice> {
    match MlxDevice::new() {
        Ok(d) => Some(d),
        Err(e) => {
            eprintln!("skipping MTP GPU test: {e}");
            None
        }
    }
}

#[test]
fn mtp_absent_scan_returns_empty() {
    let tmp = std::env::temp_dir().join(format!("mtp_absent_{}.gguf", std::process::id()));
    write_gguf(
        &tmp,
        &[TestTensor {
            name: "blk.0.attn_norm.weight",
            dims: vec![32],
            data: ones(32),
        }],
    );
    let gguf = GgufFile::open(&tmp).expect("open");
    assert!(mtp_tensor_names(&gguf, 2).is_empty());
    std::fs::remove_file(&tmp).ok();
}

#[test]
fn mtp_loads_gpu_weights_from_synthetic_gguf() {
    let Some(device) = try_device() else { return };
    let tmp = std::env::temp_dir().join(format!("mtp_present_{}.gguf", std::process::id()));
    write_gguf(&tmp, &tiny_tensors());
    let gguf = GgufFile::open(&tmp).expect("open");
    let mtp = load_mtp_weights_if_present(&gguf, &tiny_cfg(1), &device)
        .expect("load")
        .expect("some");
    assert_eq!(mtp.layer_index, 2);
    assert_eq!(mtp.hidden_size, 32);
    assert_eq!(mtp.vocab_size, 64);
    assert!(!mtp.is_empty());
    assert!(mtp.has_tensor_suffix("enorm.weight"));
    assert!(mtp.has_tensor_suffix("attn_q.weight"));
    // ADR-013 P14 follow-up: dedicated path populates the buffer.
    assert!(
        mtp.embed_tokens.is_some(),
        "dedicated_embeddings=true must yield Some(embed_tokens) buffer"
    );
    std::fs::remove_file(&tmp).ok();
}

/// ADR-013 P14 follow-up (2026-04-30): Qwen3.6 27B + 35B-A3B share the main
/// model's `token_embd.weight`; convert correctly skips emitting
/// `blk.{N}.nextn.embed_tokens.weight`. The loader must succeed (not bail) when
/// `mtp_use_dedicated_embeddings=false` AND the dedicated tensor is absent, and
/// must populate `embed_tokens = None` to signal the shared path.
#[test]
fn mtp_loads_with_shared_embeddings_when_flag_false_and_tensor_absent() {
    let Some(device) = try_device() else { return };
    // tiny_tensors() includes the dedicated embed_tokens tensor; strip it for
    // the shared-embeddings scenario.
    let tensors: Vec<TestTensor> = tiny_tensors()
        .into_iter()
        .filter(|t| t.name != "blk.2.nextn.embed_tokens.weight")
        .collect();
    let tmp = std::env::temp_dir().join(format!("mtp_shared_{}.gguf", std::process::id()));
    write_gguf(&tmp, &tensors);
    let gguf = GgufFile::open(&tmp).expect("open");
    let mut cfg = tiny_cfg(1);
    cfg.mtp_use_dedicated_embeddings = false;
    let mtp = load_mtp_weights_if_present(&gguf, &cfg, &device)
        .expect("loader must succeed when flag=false and tensor absent")
        .expect("MtpWeights present (mtp_num_hidden_layers=1)");
    assert_eq!(mtp.layer_index, 2);
    assert_eq!(mtp.hidden_size, 32);
    // Shared-embeddings path: no buffer materialised, main model's token_embd
    // is reused via Qwen35Model::token_embd at draft time.
    assert!(
        mtp.embed_tokens.is_none(),
        "mtp_use_dedicated_embeddings=false must yield None (shared with main)"
    );
    // Other MTP tensors still load correctly.
    assert!(mtp.has_tensor_suffix("enorm.weight"));
    assert!(mtp.has_tensor_suffix("attn_q.weight"));
    // The dedicated-embeddings tensor name is NOT in the discovered names.
    assert!(
        !mtp.loaded_tensor_names
            .iter()
            .any(|n| n == "blk.2.nextn.embed_tokens.weight"),
        "dedicated tensor must not appear in loaded_tensor_names"
    );
    std::fs::remove_file(&tmp).ok();
}

/// ADR-013 P14 follow-up: belt-and-suspenders for convert-side inconsistency.
/// If a GGUF carries both `mtp_use_dedicated_embeddings=False` AND the dedicated
/// tensor is present, refuse rather than silently dropping the tensor.
#[test]
fn mtp_rejects_inconsistent_shared_flag_with_dedicated_tensor_present() {
    let Some(device) = try_device() else { return };
    let tmp = std::env::temp_dir().join(format!("mtp_inconsistent_{}.gguf", std::process::id()));
    write_gguf(&tmp, &tiny_tensors()); // dedicated tensor present
    let gguf = GgufFile::open(&tmp).expect("open");
    let mut cfg = tiny_cfg(1);
    cfg.mtp_use_dedicated_embeddings = false; // inconsistent with tensor presence
    let result = load_mtp_weights_if_present(&gguf, &cfg, &device);
    assert!(
        result.is_err(),
        "loader must refuse when flag=false but dedicated tensor present"
    );
    std::fs::remove_file(&tmp).ok();
}

#[test]
fn mtp_forward_draft_returns_logits() {
    let Some(device) = try_device() else { return };
    let tmp = std::env::temp_dir().join(format!("mtp_forward_{}.gguf", std::process::id()));
    write_gguf(&tmp, &tiny_tensors());
    let gguf = GgufFile::open(&tmp).expect("open");
    let cfg = tiny_cfg(1);
    let mtp = load_mtp_weights_if_present(&gguf, &cfg, &device)
        .expect("load")
        .expect("some");
    let mut registry = KernelRegistry::new();
    let mut kv = HybridKvCache::new(&cfg, &device, 16, 1).expect("cache");
    assert!(kv.mtp_slot.is_some());
    let prev = upload_f32(&vec![0.0; 32], &device).expect("prev");
    let embed = upload_f32(&vec![0.0; 32], &device).expect("embed");
    let logits = mtp
        .forward_draft(
            &prev,
            &embed,
            &mut kv,
            &[0, 0, 0, 0],
            &device,
            &mut registry,
            &cfg,
        )
        .expect("forward");
    assert_eq!(logits.element_count(), 64);
    std::fs::remove_file(&tmp).ok();
}

// ADR-017 iter-3.6 follow-up (2026-05-05): #[ignore] removed; path
// fixed to actual fixture name (APEX-Q5_K_M.gguf). Test runtime-skips
// when the artefact is absent.
#[test]
fn mtp_on_real_apex_returns_none() {
    let Some(device) = try_device() else { return };
    let path = std::path::PathBuf::from(
        "/opt/hf2q/models/qwen3.6-35b-a3b-abliterix-ega-abliterated-apex/\
         APEX-Q5_K_M.gguf",
    );
    if !path.exists() {
        eprintln!("skipping: apex GGUF not at expected path");
        return;
    }
    let gguf = match GgufFile::open(&path) {
        Ok(g) => g,
        Err(e) => {
            eprintln!("skipping: {e}");
            return;
        }
    };
    let cfg = Qwen35Config::from_gguf(&gguf).expect("cfg");
    let result = load_mtp_weights_if_present(&gguf, &cfg, &device).expect("load_mtp");
    assert!(result.is_none(), "apex GGUF should have MTP stripped");
}

#[test]
fn test_cfg_layer_types_not_all_full() {
    let cfg = tiny_cfg(1);
    assert_eq!(cfg.layer_types[0], Qwen35LayerKind::LinearAttention);
    assert_eq!(cfg.layer_types[1], Qwen35LayerKind::FullAttention);
}

/// ADR-034 P3.1 regression gate (2026-05-21 at HEAD `1cfefea0`):
///
/// Canonical Qwen 3.5 35B-A3B MoE-MTP GGUFs emit 8 MoE-style tensors at the
/// MTP block (`blk.40.ffn_gate_inp/ffn_{gate,up,down}_exps/ffn_{gate,up,down,gate_inp}_shexp`).
/// Before HEAD `afbf5684` the MTP loader hardcoded the dense FFN tensor
/// names and crashed at load time. This test asserts the structural
/// outcome of the fix:
///
///   1. `load_mtp_weights_if_present` succeeds against a canonical MoE-MTP GGUF
///   2. The resulting `MtpWeights::ffn_kind()` returns `MtpFfnKind::Moe`
///   3. `loaded_tensor_names` contains MoE expert tensor suffixes (so
///      `has_tensor_suffix("ffn_gate_exps.weight")` reports them)
///   4. `layer_index` matches the model's MTP block index (40 for 35B-A3B)
///
/// Skips when the canonical fixture isn't present locally (CI / contributor
/// environments without the 20 GB GGUF on disk). The empirical 8/8 quant
/// sweep in the cover commits validates the runtime path; this test locks
/// in the LOAD-side structural contract as a CI-side gate.
#[test]
fn mtp_loads_canonical_moe_mtp_q4_k_m_with_moe_variant_2026_05_21() {
    let Some(device) = try_device() else { return };
    let path = std::path::PathBuf::from(
        "/opt/hf2q/cache/byte_cmp/Qwen-Qwen3.5-35B-A3B_canonical_q4_k_m.gguf",
    );
    if !path.exists() {
        eprintln!(
            "skipping: canonical MoE-MTP fixture not at {}; this test \
             requires the 20 GB Qwen 3.5 35B-A3B Q4_K_M GGUF to exercise \
             the MoE inner-FFN load path",
            path.display()
        );
        return;
    }
    let gguf = match GgufFile::open(&path) {
        Ok(g) => g,
        Err(e) => {
            eprintln!("skipping: {e}");
            return;
        }
    };
    let cfg = Qwen35Config::from_gguf(&gguf).expect("cfg");
    let result = load_mtp_weights_if_present(&gguf, &cfg, &device)
        .expect("MoE-MTP loader must succeed on canonical Q4_K_M GGUF");
    let mtp = result.expect("canonical Q4_K_M ships MTP weights");

    // Structural assertions on the MoE-MTP branch.
    assert_eq!(
        mtp.ffn_kind(),
        MtpFfnKind::Moe,
        "canonical Qwen 3.5 35B-A3B MoE-MTP must load via MtpFfnWeightsGpu::Moe branch"
    );
    assert_eq!(
        mtp.layer_index, cfg.num_hidden_layers,
        "MTP block sits at blk.{{num_hidden_layers}}"
    );

    // MoE-specific tensor names must be reflected in loaded_tensor_names
    // (the mtp_tensor_names membership set was extended in the cover fix).
    assert!(
        mtp.has_tensor_suffix("ffn_gate_exps.weight"),
        "MoE expert gate tensor must be tracked"
    );
    assert!(
        mtp.has_tensor_suffix("ffn_up_exps.weight"),
        "MoE expert up tensor must be tracked"
    );
    assert!(
        mtp.has_tensor_suffix("ffn_down_exps.weight"),
        "MoE expert down tensor must be tracked"
    );
    assert!(
        mtp.has_tensor_suffix("ffn_gate_inp.weight"),
        "MoE router must be tracked"
    );
    assert!(
        mtp.has_tensor_suffix("ffn_gate_inp_shexp.weight"),
        "MoE shared-expert sigmoid gate must be tracked"
    );

    // The dense suffix MUST NOT appear in a MoE-MTP GGUF — guard against
    // silent fall-through if a future loader change accidentally pulls
    // dense names back in.
    assert!(
        !mtp.has_tensor_suffix("ffn_gate.weight"),
        "MoE-MTP GGUF must not advertise dense ffn_gate.weight at the MTP block"
    );
}

/// Companion to the MoE-MTP gate: locks in the dense-MTP path against a
/// regression. Qwen 3.6 27B emits a dense SwiGLU FFN at the MTP block
/// (`blk.64.ffn_{gate,up,down}.weight`). The loader must take the Dense
/// branch and `ffn_kind()` must report Dense.
///
/// Skips when the 27 GB Qwen 3.6 27B MTP GGUF isn't present locally.
#[test]
fn mtp_loads_canonical_dense_mtp_q8_0_with_dense_variant_2026_05_21() {
    let Some(device) = try_device() else { return };
    let path =
        std::path::PathBuf::from("/opt/hf2q/models/Qwen3.6-27B-MTP-GGUF/Qwen3.6-27B-Q8_0-mtp.gguf");
    if !path.exists() {
        eprintln!(
            "skipping: canonical dense MTP fixture not at {}",
            path.display()
        );
        return;
    }
    let gguf = match GgufFile::open(&path) {
        Ok(g) => g,
        Err(e) => {
            eprintln!("skipping: {e}");
            return;
        }
    };
    let cfg = Qwen35Config::from_gguf(&gguf).expect("cfg");
    let result = load_mtp_weights_if_present(&gguf, &cfg, &device)
        .expect("dense MTP loader must succeed on canonical Qwen 3.6 27B GGUF");
    let mtp = result.expect("canonical Qwen 3.6 27B Q8_0 ships MTP weights");

    assert_eq!(
        mtp.ffn_kind(),
        MtpFfnKind::Dense,
        "canonical Qwen 3.6 27B dense-MTP must load via MtpFfnWeightsGpu::Dense branch"
    );
    assert_eq!(mtp.layer_index, cfg.num_hidden_layers);
    assert!(mtp.has_tensor_suffix("ffn_gate.weight"));
    assert!(mtp.has_tensor_suffix("ffn_up.weight"));
    assert!(mtp.has_tensor_suffix("ffn_down.weight"));
    // MoE suffixes MUST NOT appear in a dense MTP GGUF.
    assert!(!mtp.has_tensor_suffix("ffn_gate_exps.weight"));
    assert!(!mtp.has_tensor_suffix("ffn_gate_inp.weight"));
}