hf2q 0.1.1

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
//! `QuantSelector` — unified `--quant <name>` parser for `hf2q convert-v2`.
//!
//! Per ADR-033 Decision §6, `hf2q convert-v2 --quant <name>` accepts three
//! disjoint name-spaces:
//!
//! 1. **Standard llama.cpp ftypes** — `f32`, `f16`, `bf16`, `q4_0`,
//!    `q4_1`, `q5_0`, `q5_1`, `q8_0`, `q2_k`, `q3_k_s/m/l`, `q4_k_s/m`,
//!    `q5_k_s/m`, `q6_k`, `iq4_nl`, etc. — parsed via
//!    [`LlamaFtype::from_name`].
//! 2. **Apex algorithmic tiers** — `apex-quality`, `apex-i-quality`,
//!    `apex-balanced`, `apex-i-balanced`, `apex-compact`, `apex-i-compact`,
//!    `apex-mini`. Resolved to [`ApexTier`]; the driver pairs each tier
//!    with an `ApexPolicy` built from the source model's `n_layers` and
//!    `n_expert`.
//! 3. **Apex custom** — `apex-custom` carries an operator-supplied
//!    `--tensor-type-file <path>` (out of v1 scope for the convert-v2
//!    driver — typed error stub only).
//!
//! Per [[feedback-no-loop-suppression-2026-05-17]] + ADR §"reserved-name
//! typed-error stubs": every unsupported / reserved name surfaces as a
//! typed [`QuantSelectorError`] — never silent fallback. Reserved names
//! (`dwq`, bare `apex`, `tq1_0`, `tq2_0`) are explicitly rejected with
//! diagnostic errors.
//!
//! Per [[feedback-no-backwards-compat-2026-05-18]]: no compat aliases for
//! legacy `--quant` values.

use std::path::PathBuf;

use crate::quantize::ggml_quants::apex::{ApexTier, SUPPORTED_APEX_TIERS};
use crate::quantize::ggml_quants::{LlamaFtype, DEEPSEEK4_AGENTIC_Q2_NAME};

/// One resolved `--quant <name>` selector.
///
/// The CLI layer parses the operator-supplied string into this enum via
/// [`QuantSelector::from_name`]; the convert-v2 driver then branches on
/// the variant to pick the right policy (StandardPolicy vs ApexPolicy
/// vs operator-supplied tensor-type file).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QuantSelector {
    /// Standard llama.cpp file-type. The driver builds the orchestrator
    /// with `StandardPolicy` and emits `general.file_type` = the
    /// underlying [`LlamaFtype`] discriminant.
    Standard(LlamaFtype),
    /// DeepSeek-V4 agent profile: a standard Q2_K expert body with Q3_K
    /// down projections and Q8_0 context-discrimination tensors.
    Deepseek4AgenticQ2,
    /// Apex algorithmic tier. The driver builds an `ApexPolicy { tier,
    /// n_layers, n_expert }` from the source model's `config.json`.
    /// `general.file_type` carries the closest standard LlamaFtype
    /// approximation (see [`approximate_for_apex`]).
    Apex(ApexTier),
    /// Operator-supplied `apex-custom --tensor-type-file <path>`. Out of
    /// v1 scope for the convert-v2 driver; placeholder for the future
    /// per-tensor override path described in ADR §"Per-model APEX config
    /// override".
    ApexCustom(PathBuf),
}

/// Typed errors raised by [`QuantSelector::from_name`].
///
/// Per [[feedback-no-loop-suppression-2026-05-17]]: every unsupported
/// `--quant` value is rejected with a structured error; the driver never
/// falls back to a default.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum QuantSelectorError {
    /// The name didn't match any standard `LlamaFtype` or apex prefix.
    #[error("unknown --quant value `{name}` (no LlamaFtype / Apex tier mapping)")]
    UnknownQuant { name: String },

    /// `--quant apex-<X>` where `<X>` isn't in the v1 supported tier
    /// set. v1 tiers per ADR §6:
    /// `quality / i-quality / balanced / i-balanced / compact / i-compact / mini`.
    #[error("unknown Apex tier `apex-{tier}`; v1 supports {supported:?}")]
    UnknownApexTier {
        tier: String,
        supported: &'static [&'static str],
    },

    /// Mudler's experimental tiers (`nano / i-nano / micro / i-micro`)
    /// were dropped from v1's surface (per ADR §"Decision §6"). They are
    /// reachable only via `--quant apex-custom --tensor-type-file <file>`
    /// with the vendored per-model config.
    #[error(
        "Apex tier `apex-{tier}` is out of v1 scope (mudler's experimental tiers); \
         use `--quant apex-custom --tensor-type-file <vendored-config>` instead"
    )]
    ApexTierOutOfScope { tier: String },

    /// `apex-custom` requires `--tensor-type-file <path>`. The selector
    /// parses the `--quant` flag in isolation; the tensor-type-file
    /// resolution happens at a higher CLI layer when it's wired.
    #[error("--quant apex-custom requires --tensor-type-file <path>")]
    ApexCustomRequiresTensorTypeFile,

    /// `--quant dwq` is reserved for a future training-loop entry point
    /// (ADR-020 follow-up). Surface a typed error rather than silently
    /// treating it as an unknown quant.
    #[error(
        "--quant dwq is reserved for the future DWQ-train pipeline; \
         not implemented in convert-v2"
    )]
    DwqReserved,

    /// Bare `apex` without a tier suffix. Per ADR §6 the operator MUST
    /// pick a tier explicitly — there is no implicit Apex default.
    #[error("--quant apex is unqualified; use one of {supported:?} or `apex-custom`")]
    ApexUnqualified { supported: &'static [&'static str] },

    /// TQ1_0 / TQ2_0 are valid `LlamaFtype` variants but out of v1
    /// convert-v2 scope (no quantizer implementation — see
    /// `quantizer.rs`). We surface a more diagnostic error than the
    /// generic UnknownQuant.
    #[error(
        "--quant {name} is a recognized ftype but out of v1 convert-v2 scope \
         (no Quantizer impl)"
    )]
    TqOutOfV1Scope { name: String },
}

impl QuantSelector {
    /// Canonical spelling persisted in conversion receipts.
    pub fn receipt_name(&self) -> String {
        match self {
            QuantSelector::Standard(ftype) => ftype.name().to_string(),
            QuantSelector::Deepseek4AgenticQ2 => DEEPSEEK4_AGENTIC_Q2_NAME.to_string(),
            QuantSelector::Apex(tier) => format!("apex-{}", tier.cli_name()),
            QuantSelector::ApexCustom(path) => format!("apex-custom:{}", path.display()),
        }
    }

    /// Parse a `--quant <name>` string into the matching selector.
    ///
    /// Resolution order:
    /// 1. Try [`LlamaFtype::from_name`] first — the most common case.
    /// 2. Try the `apex-<tier>` prefix.
    /// 3. Match reserved-name typed errors (`dwq`, bare `apex`,
    ///    `tq1_0`, `tq2_0`).
    /// 4. Anything else: [`QuantSelectorError::UnknownQuant`].
    ///
    /// Per [[feedback-no-backwards-compat-2026-05-18]]: no compat aliases
    /// for legacy `--quant` values.
    pub fn from_name(s: &str) -> Result<Self, QuantSelectorError> {
        if s == DEEPSEEK4_AGENTIC_Q2_NAME {
            return Ok(QuantSelector::Deepseek4AgenticQ2);
        }

        // 1. Standard llama.cpp ftypes. TQ1_0 / TQ2_0 are members of
        //    LlamaFtype but the convert-v2 pipeline has no Quantizer impl
        //    for them today — surface a more diagnostic error rather than
        //    letting them slip through and panic at quantize time.
        match s {
            "tq1_0" | "tq2_0" => {
                return Err(QuantSelectorError::TqOutOfV1Scope {
                    name: s.to_string(),
                });
            }
            _ => {}
        }
        if let Some(ftype) = LlamaFtype::from_name(s) {
            return Ok(QuantSelector::Standard(ftype));
        }

        // 2. Apex tier names.
        if let Some(rest) = s.strip_prefix("apex-") {
            let tier = match rest {
                "quality" => ApexTier::Quality,
                "i-quality" => ApexTier::IQuality,
                "balanced" => ApexTier::Balanced,
                "i-balanced" => ApexTier::IBalanced,
                "compact" => ApexTier::Compact,
                "i-compact" => ApexTier::ICompact,
                "mini" => ApexTier::Mini,
                "custom" => {
                    return Err(QuantSelectorError::ApexCustomRequiresTensorTypeFile);
                }
                // Mudler experimental tiers dropped from v1 surface per
                // ADR §6 — reachable only via apex-custom.
                "nano" | "i-nano" | "micro" | "i-micro" => {
                    return Err(QuantSelectorError::ApexTierOutOfScope {
                        tier: rest.to_string(),
                    });
                }
                _ => {
                    return Err(QuantSelectorError::UnknownApexTier {
                        tier: rest.to_string(),
                        supported: SUPPORTED_APEX_TIERS,
                    });
                }
            };
            return Ok(QuantSelector::Apex(tier));
        }

        // 3. Reserved-name typed errors.
        match s {
            "dwq" => Err(QuantSelectorError::DwqReserved),
            "apex" => Err(QuantSelectorError::ApexUnqualified {
                supported: SUPPORTED_APEX_TIERS,
            }),
            _ => Err(QuantSelectorError::UnknownQuant {
                name: s.to_string(),
            }),
        }
    }
}

/// Closest standard [`LlamaFtype`] for an [`ApexTier`].
///
/// Apex tiers are mixed-precision recipes — no single LlamaFtype is a
/// faithful encoding. We pick the closest "headline" ftype so the GGUF
/// `general.file_type` byte at least clues operators / inspectors into
/// the tier's bit-budget class:
///
/// | Apex tier              | Approximate LlamaFtype |
/// |------------------------|------------------------|
/// | Quality / IQuality     | MostlyQ6_K (18)        |
/// | Balanced / IBalanced   | MostlyQ5_K_M (17)      |
/// | Compact / ICompact     | MostlyQ4_K_M (15)      |
/// | Mini                   | MostlyQ3_K_S (11)      |
///
/// The rationale is the dominant mid-band expert quant for each tier:
/// quality's `mid_exp=IQ4_XS` floats up to `mid_attn=Q6_K`; balanced is
/// `mid_exp=Q5_K`; compact is `mid_exp=Q3_K + mid_attn=Q4_K`; mini is
/// `mid_exp=IQ2_S + mid_attn=Q3_K`. **This is purely a
/// header-metadata pick** — actual per-tensor types are decided by
/// `ApexPolicy::target_for` and recorded on each tensor's own
/// `ggml_type` field, not via the file-type byte.
pub const fn approximate_for_apex(tier: ApexTier) -> LlamaFtype {
    match tier {
        ApexTier::Quality | ApexTier::IQuality => LlamaFtype::MostlyQ6_K,
        ApexTier::Balanced | ApexTier::IBalanced => LlamaFtype::MostlyQ5_K_M,
        ApexTier::Compact | ApexTier::ICompact => LlamaFtype::MostlyQ4_K_M,
        ApexTier::Mini => LlamaFtype::MostlyQ3_K_S,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_quant_selector_standard_round_trip() {
        // Spot-check a handful of standard ftype names — full coverage
        // lives in `LlamaFtype::name_round_trip` already.
        assert_eq!(
            QuantSelector::from_name("q5_k_m").unwrap(),
            QuantSelector::Standard(LlamaFtype::MostlyQ5_K_M)
        );
        assert_eq!(
            QuantSelector::from_name("q8_0").unwrap(),
            QuantSelector::Standard(LlamaFtype::MostlyQ8_0)
        );
        assert_eq!(
            QuantSelector::from_name("f16").unwrap(),
            QuantSelector::Standard(LlamaFtype::MostlyF16)
        );
        assert_eq!(
            QuantSelector::from_name("iq4_nl").unwrap(),
            QuantSelector::Standard(LlamaFtype::MostlyIQ4_NL)
        );
    }

    #[test]
    fn parse_deepseek4_agentic_q2_round_trip() {
        let selector = QuantSelector::from_name(DEEPSEEK4_AGENTIC_Q2_NAME).unwrap();
        assert_eq!(selector, QuantSelector::Deepseek4AgenticQ2);
        assert_eq!(selector.receipt_name(), DEEPSEEK4_AGENTIC_Q2_NAME);
    }

    #[test]
    fn parse_quant_selector_apex_round_trip() {
        assert_eq!(
            QuantSelector::from_name("apex-balanced").unwrap(),
            QuantSelector::Apex(ApexTier::Balanced)
        );
        assert_eq!(
            QuantSelector::from_name("apex-quality").unwrap(),
            QuantSelector::Apex(ApexTier::Quality)
        );
        assert_eq!(
            QuantSelector::from_name("apex-compact").unwrap(),
            QuantSelector::Apex(ApexTier::Compact)
        );
        assert_eq!(
            QuantSelector::from_name("apex-mini").unwrap(),
            QuantSelector::Apex(ApexTier::Mini)
        );
    }

    #[test]
    fn parse_quant_selector_apex_i_variant() {
        assert_eq!(
            QuantSelector::from_name("apex-i-quality").unwrap(),
            QuantSelector::Apex(ApexTier::IQuality)
        );
        assert_eq!(
            QuantSelector::from_name("apex-i-balanced").unwrap(),
            QuantSelector::Apex(ApexTier::IBalanced)
        );
        assert_eq!(
            QuantSelector::from_name("apex-i-compact").unwrap(),
            QuantSelector::Apex(ApexTier::ICompact)
        );
    }

    #[test]
    fn parse_quant_selector_apex_custom_errors() {
        let err = QuantSelector::from_name("apex-custom").unwrap_err();
        assert!(matches!(
            err,
            QuantSelectorError::ApexCustomRequiresTensorTypeFile
        ));
    }

    #[test]
    fn parse_quant_selector_dwq_reserved() {
        let err = QuantSelector::from_name("dwq").unwrap_err();
        assert!(matches!(err, QuantSelectorError::DwqReserved));
    }

    #[test]
    fn parse_quant_selector_apex_nano_out_of_scope() {
        let err = QuantSelector::from_name("apex-nano").unwrap_err();
        match err {
            QuantSelectorError::ApexTierOutOfScope { tier } => {
                assert_eq!(tier, "nano");
            }
            other => panic!("expected ApexTierOutOfScope, got {other:?}"),
        }

        // Same for i-nano / micro / i-micro.
        for v in ["apex-i-nano", "apex-micro", "apex-i-micro"] {
            let err = QuantSelector::from_name(v).unwrap_err();
            assert!(
                matches!(err, QuantSelectorError::ApexTierOutOfScope { .. }),
                "{v} should be ApexTierOutOfScope, got {err:?}"
            );
        }
    }

    #[test]
    fn parse_quant_selector_bare_apex_unqualified() {
        let err = QuantSelector::from_name("apex").unwrap_err();
        match err {
            QuantSelectorError::ApexUnqualified { supported } => {
                assert!(supported.contains(&"balanced"));
                assert!(supported.contains(&"quality"));
                assert!(supported.contains(&"mini"));
            }
            other => panic!("expected ApexUnqualified, got {other:?}"),
        }
    }

    #[test]
    fn parse_quant_selector_unknown_apex_tier_errors() {
        let err = QuantSelector::from_name("apex-bogus").unwrap_err();
        match err {
            QuantSelectorError::UnknownApexTier { tier, supported } => {
                assert_eq!(tier, "bogus");
                assert!(supported.contains(&"balanced"));
            }
            other => panic!("expected UnknownApexTier, got {other:?}"),
        }
    }

    #[test]
    fn parse_quant_selector_tq_out_of_v1_scope() {
        let err = QuantSelector::from_name("tq1_0").unwrap_err();
        assert!(matches!(err, QuantSelectorError::TqOutOfV1Scope { .. }));
        let err = QuantSelector::from_name("tq2_0").unwrap_err();
        assert!(matches!(err, QuantSelectorError::TqOutOfV1Scope { .. }));
    }

    #[test]
    fn parse_quant_selector_unknown_quant_errors() {
        let err = QuantSelector::from_name("garbage").unwrap_err();
        match err {
            QuantSelectorError::UnknownQuant { name } => assert_eq!(name, "garbage"),
            other => panic!("expected UnknownQuant, got {other:?}"),
        }
    }

    // ============================================================================
    // ADR-033 §P7 AC#3 — every typed-error variant's MESSAGE contains
    // the actionable hint the operator needs to recover (the supported
    // alternative / the tracking issue / the future-ADR reference).
    //
    // Variant-only matching is not enough: a future refactor that
    // removes the hint from the `#[error(...)]` template would not be
    // caught by `matches!(err, Variant)`. These tests `.to_string()`
    // the error and assert substring presence, so the user-facing
    // diagnostic stays informative.
    // ============================================================================

    #[test]
    fn p7_ac3_hint_dwq_reserved() {
        let msg = QuantSelectorError::DwqReserved.to_string();
        assert!(
            msg.contains("dwq"),
            "msg should name the rejected flag: {msg}"
        );
        assert!(
            msg.contains("reserved") || msg.contains("future"),
            "msg should hint at the reserved/future-pipeline status: {msg}"
        );
    }

    #[test]
    fn p7_ac3_hint_apex_unqualified() {
        let err = QuantSelectorError::ApexUnqualified {
            supported: SUPPORTED_APEX_TIERS,
        };
        let msg = err.to_string();
        assert!(msg.contains("apex"), "msg should name the flag: {msg}");
        // Must enumerate the supported tier names so the operator
        // can immediately pick one.
        assert!(
            msg.contains("balanced"),
            "msg should list `balanced`: {msg}"
        );
        assert!(msg.contains("mini"), "msg should list `mini`: {msg}");
        assert!(
            msg.contains("apex-custom"),
            "msg should mention the apex-custom escape hatch: {msg}"
        );
    }

    #[test]
    fn p7_ac3_hint_tq_out_of_v1_scope() {
        let msg = QuantSelectorError::TqOutOfV1Scope {
            name: "tq1_0".to_string(),
        }
        .to_string();
        assert!(
            msg.contains("tq1_0"),
            "msg should echo the rejected name: {msg}"
        );
        assert!(
            msg.contains("out of v1") || msg.contains("scope"),
            "msg should hint at the scope reason: {msg}"
        );
        assert!(
            msg.contains("Quantizer"),
            "msg should reference the missing Quantizer impl: {msg}"
        );
    }

    #[test]
    fn p7_ac3_hint_unknown_apex_tier_lists_supported() {
        let msg = QuantSelectorError::UnknownApexTier {
            tier: "bogus".to_string(),
            supported: SUPPORTED_APEX_TIERS,
        }
        .to_string();
        assert!(msg.contains("bogus"), "msg should echo the bad tier: {msg}");
        assert!(
            msg.contains("balanced"),
            "msg should list the supported tiers (e.g. `balanced`): {msg}"
        );
    }

    #[test]
    fn p7_ac3_hint_apex_custom_requires_tensor_type_file() {
        let msg = QuantSelectorError::ApexCustomRequiresTensorTypeFile.to_string();
        assert!(
            msg.contains("apex-custom") || msg.contains("--tensor-type-file"),
            "msg should name the missing flag the operator must supply: {msg}"
        );
    }

    #[test]
    fn p7_ac3_hint_apex_tier_out_of_scope() {
        let msg = QuantSelectorError::ApexTierOutOfScope {
            tier: "nano".to_string(),
        }
        .to_string();
        assert!(
            msg.contains("nano"),
            "msg should echo the rejected tier: {msg}"
        );
        assert!(
            msg.contains("apex-custom") || msg.contains("scope"),
            "msg should hint at the escape hatch or scope reason: {msg}"
        );
    }

    #[test]
    fn p7_ac3_hint_unknown_quant() {
        let msg = QuantSelectorError::UnknownQuant {
            name: "garbage".to_string(),
        }
        .to_string();
        assert!(
            msg.contains("garbage"),
            "msg should echo the bad name: {msg}"
        );
    }

    #[test]
    fn approximate_for_apex_table() {
        assert_eq!(
            approximate_for_apex(ApexTier::Quality),
            LlamaFtype::MostlyQ6_K
        );
        assert_eq!(
            approximate_for_apex(ApexTier::IQuality),
            LlamaFtype::MostlyQ6_K
        );
        assert_eq!(
            approximate_for_apex(ApexTier::Balanced),
            LlamaFtype::MostlyQ5_K_M
        );
        assert_eq!(
            approximate_for_apex(ApexTier::IBalanced),
            LlamaFtype::MostlyQ5_K_M
        );
        assert_eq!(
            approximate_for_apex(ApexTier::Compact),
            LlamaFtype::MostlyQ4_K_M
        );
        assert_eq!(
            approximate_for_apex(ApexTier::ICompact),
            LlamaFtype::MostlyQ4_K_M
        );
        assert_eq!(
            approximate_for_apex(ApexTier::Mini),
            LlamaFtype::MostlyQ3_K_S
        );
    }
}