tyger-coreml / scripts /coreml_package.py
matthiastl's picture
TYGER for iPhone (Core ML, Neural Engine)
d65bf0f
Raw History Blame Contribute Delete
1.39 kB
"""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()