43 lines
1.6 KiB
Python
43 lines
1.6 KiB
Python
"""Official Piper exporter with explicit legacy ONNX export on Torch 2.13.
|
|
|
|
Piper's pinned exporter predates PyTorch's dynamo-default change. Architecture,
|
|
opset and tensor names remain those of the official exporter, unmodified.
|
|
"""
|
|
import argparse
|
|
from functools import partial
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path, PosixPath
|
|
import shutil
|
|
import sys
|
|
import time
|
|
|
|
os.environ['CUDA_VISIBLE_DEVICES']=''
|
|
os.environ['OMP_NUM_THREADS']='2'
|
|
os.environ['MKL_NUM_THREADS']='2'
|
|
import torch
|
|
from piper.train.export_onnx import main as official_export
|
|
|
|
parser=argparse.ArgumentParser()
|
|
parser.add_argument('--checkpoint',required=True)
|
|
parser.add_argument('--output',required=True)
|
|
args=parser.parse_args()
|
|
torch.set_num_threads(2)
|
|
torch.serialization.add_safe_globals([PosixPath,(PosixPath,'pathlib.PosixPath')])
|
|
torch.onnx.export=partial(torch.onnx.export,dynamo=False)
|
|
sys.argv=['piper.train.export_onnx','--checkpoint',args.checkpoint,'--output-file',args.output]
|
|
started=time.monotonic()
|
|
official_export()
|
|
root=Path('/data/athena-piper-test-20261027/anna-pilot')
|
|
shutil.copy2(root/'anna.onnx.json',args.output+'.json')
|
|
import onnx
|
|
onnx.checker.check_model(args.output)
|
|
path=Path(args.output)
|
|
result=dict(checkpoint=Path(args.checkpoint).name,output=path.name,
|
|
bytes=path.stat().st_size,sha256=hashlib.file_digest(path.open('rb'),'sha256').hexdigest(),
|
|
export_seconds=time.monotonic()-started,exporter='Piper 1.8.0, explicit dynamo=False',
|
|
onnx_checked=True)
|
|
Path(args.output+'.export.json').write_text(json.dumps(result,indent=2))
|
|
print(json.dumps(result),flush=True)
|