()
| 354 | |
| 355 | |
| 356 | def main(): |
| 357 | parser = argparse.ArgumentParser( |
| 358 | description="Export streaming Sortformer diarizer to ExecuTorch" |
| 359 | ) |
| 360 | parser.add_argument("--output-dir", default="./sortformer_exports") |
| 361 | parser.add_argument( |
| 362 | "--nemo-path", |
| 363 | type=str, |
| 364 | help="Path to .nemo model file", |
| 365 | ) |
| 366 | parser.add_argument( |
| 367 | "--hf-model", |
| 368 | type=str, |
| 369 | help="HuggingFace model ID (default: nvidia/diar_streaming_sortformer_4spk-v2)", |
| 370 | ) |
| 371 | parser.add_argument( |
| 372 | "--backend", |
| 373 | type=str, |
| 374 | default="xnnpack", |
| 375 | choices=["portable", "xnnpack", "cuda", "cuda-windows"], |
| 376 | help="Backend for acceleration (default: xnnpack)", |
| 377 | ) |
| 378 | |
| 379 | args = parser.parse_args() |
| 380 | os.makedirs(args.output_dir, exist_ok=True) |
| 381 | |
| 382 | print("Loading model...") |
| 383 | model = load_model(nemo_path=args.nemo_path, hf_model=args.hf_model) |
| 384 | |
| 385 | print("\nExporting components...") |
| 386 | programs, metadata = export_all(model, backend=args.backend) |
| 387 | |
| 388 | et = lower_to_executorch(programs, metadata=metadata, backend=args.backend) |
| 389 | |
| 390 | pte_path = os.path.join(args.output_dir, "sortformer.pte") |
| 391 | print(f"\nSaving ExecuTorch program to: {pte_path}") |
| 392 | with open(pte_path, "wb") as f: |
| 393 | et.write_to_file(f) |
| 394 | |
| 395 | if args.backend in ("cuda", "cuda-windows"): |
| 396 | cuda_blob_path = os.path.join(args.output_dir, "aoti_cuda_blob.ptd") |
| 397 | print(f"Writing CUDA named-data blob to: {cuda_blob_path}") |
| 398 | et.write_tensor_data_to_file(args.output_dir) |
| 399 | if not os.path.isfile(cuda_blob_path): |
| 400 | raise FileNotFoundError( |
| 401 | f"Expected CUDA named-data blob at {cuda_blob_path}, but it was not created." |
| 402 | ) |
| 403 | print(f"Saved {os.path.getsize(pte_path) / (1024 * 1024):.1f} MB") |
| 404 | |
| 405 | print("\nDone!") |
| 406 | |
| 407 | |
| 408 | if __name__ == "__main__": |
no test coverage detected