File size: 1,151 Bytes
7a102c1 7011ac6 7a102c1 8a7b200 7a102c1 7011ac6 7a102c1 7011ac6 7a102c1 8a7b200 7a102c1 7011ac6 7a102c1 7011ac6 7a102c1 | 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 | """Stage 2: export the BC1S (Neural Engine layout) d1 function to .aimodel, fp16.
.venv/bin/python export_ane.py [out/d1_ane_prompt_fp16.aimodel]
"""
import os, shutil, sys, time
from pathlib import Path
import torch
from coreai_models.export.macos import export_to_coreai
from d1_common import embeddings, load_meta, prompt_inputs
from d1_torch import D1Fixed
from d1_ane import D1ANE
out = Path(sys.argv[1] if len(sys.argv) > 1 else "out/d1_ane_prompt_fp16.aimodel")
meta = load_meta()
D1ANE.MLP_SPLIT = int(os.environ.get("MLP_SPLIT", D1ANE.MLP_SPLIT))
model = D1ANE(D1Fixed(meta, torch.float16)).eval()
_, gh, gw = meta["grid_thw"]
ref = {"image": torch.zeros(1, 3, gh * 16, gw * 16, dtype=torch.float32),
**{k: torch.from_numpy(v) for k, v in prompt_inputs(meta, embeddings()).items()}} # the questions are inputs
t = time.time()
program = export_to_coreai(model, ref, input_names=("image", "text", "head", "attend", "slots"), output_names=("logits",))
print("export_to_coreai %.1fs" % (time.time() - t), flush=True)
program.optimize()
if out.exists():
shutil.rmtree(out)
program.save_asset(out)
print("saved", out, flush=True)
|