File size: 3,123 Bytes
72c7148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""``python -m macjev.export.cli --weights <path> --formats gguf-f16,... --out <dir>`` (CONTRACTS section 8).

Writes ``<out>/text-only`` (HF Qwen3_5ForCausalLM, no vision/MTP), ``<out>/gguf/model-*.gguf``,
``<out>/mlx/{bf16,affine8-g64,affine4-g64}``, ``<out>/export_manifest.json`` and, unless
``--no-parity``, the G5 parity report in ``<out>/parity/report.json``.
"""
from __future__ import annotations

import argparse
import json
import time
from pathlib import Path

from .convert import ALL_FORMATS, GGUF_FORMATS, MLX_FORMATS, llama_cpp_commit, to_gguf, to_mlx
from .text_only import save_text_only


def main(argv=None) -> int:
    ap = argparse.ArgumentParser(description=__doc__)
    ap.add_argument("--weights", required=True, help="HF dir / repo id / weights-*.safetensors / run dir")
    ap.add_argument("--formats", default=",".join(ALL_FORMATS))
    ap.add_argument("--out", required=True)
    ap.add_argument("--base", default="Qwen/Qwen3.5-0.8B-Base")
    ap.add_argument("--no-parity", action="store_true")
    ap.add_argument("--parity-rows", type=int, default=240)
    ap.add_argument("--parity-device", default="mps")
    ap.add_argument("--readouts", default="verdict,letter")
    ap.add_argument("--no-noise-floor", action="store_true")
    ap.add_argument("--parity-long-points", default="16384,25600",
                    help="reported-only long parity points in tokens (comma list, <= 25600; '' = off)")
    a = ap.parse_args(argv)
    formats = [f.strip() for f in a.formats.split(",") if f.strip()]
    bad = [f for f in formats if f not in ALL_FORMATS]
    if bad:
        ap.error(f"unknown formats {bad}; choose from {ALL_FORMATS}")
    out = Path(a.out)
    out.mkdir(parents=True, exist_ok=True)
    t0 = time.time()
    manifest = {"weights": str(a.weights), "base": a.base, "formats": formats, "created_unix": t0}
    manifest["text_only"] = save_text_only(a.weights, out / "text-only", base=a.base)
    g = [f for f in formats if f in GGUF_FORMATS]
    m = [f for f in formats if f in MLX_FORMATS]
    if g:
        manifest["gguf"] = to_gguf(out / "text-only", out / "gguf", g)
    if m:
        manifest["mlx"] = to_mlx(out / "text-only", out / "mlx", m)
    manifest["llama_cpp_commit"] = llama_cpp_commit()
    manifest["export_seconds"] = time.time() - t0
    (out / "export_manifest.json").write_text(json.dumps(manifest, indent=2))
    if not a.no_parity:
        from .parity import RUNTIME_VARIANTS, run_parity
        pformats = formats + [v for v, (b, _) in RUNTIME_VARIANTS.items() if b in formats]
        rep = run_parity(out, a.weights, out / "parity", n=a.parity_rows, formats=pformats,
                         readouts=tuple(a.readouts.split(",")), ref_device=a.parity_device,
                         noise_floor=not a.no_noise_floor, base=a.base,
                         long_points=tuple(int(x) for x in a.parity_long_points.split(",") if x.strip()))
        print(json.dumps({"g5_pass": rep["g5_pass"], "g5_failures": rep["g5_failures"]}, indent=2))
        return 0 if rep["g5_pass"] else 3
    return 0


if __name__ == "__main__":
    raise SystemExit(main())