File size: 1,389 Bytes
d65bf0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The Core ML package from the exported encoder: ONNX Runtime's Core ML provider writes it to its cache when it loads
the model; this copies it out as TygerEncoder.mlpackage.

    python -I -B coreml_package.py --model export/l15.static.b2.fp16.onnx --out ../TygerEncoder.mlpackage
"""
from __future__ import annotations

import argparse
import shutil
import sys
import tempfile
from pathlib import Path

HERE = Path(__file__).parent
sys.path[:0] = [str(HERE), str(HERE.parent)]
import coreml_try as ct  # noqa: E402


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
    parser.add_argument("--model", type=Path, required=True)
    parser.add_argument("--out", type=Path, required=True)
    args = parser.parse_args()
    with tempfile.TemporaryDirectory() as cache:
        ct.session(args.model, "CPUAndNeuralEngine", cache=Path(cache))
        packages = [p for p in Path(cache).rglob("model") if (p / "Manifest.json").exists()]
        assert len(packages) == 1, packages                     # the whole encoder is one Core ML model
        args.out.mkdir(parents=True)
        shutil.copy(packages[0] / "Manifest.json", args.out)
        shutil.copytree(packages[0] / "Data", args.out / "Data")
    print(args.out, sorted(str(p.relative_to(args.out)) for p in args.out.rglob("*") if p.is_file()))


if __name__ == "__main__":
    main()