| 26 | |
| 27 | |
| 28 | def onnx_export( |
| 29 | model, |
| 30 | model_args: tuple, |
| 31 | output_path: Path, |
| 32 | ordered_input_names, |
| 33 | output_names, |
| 34 | dynamic_axes, |
| 35 | opset, |
| 36 | use_external_data_format=False, |
| 37 | ): |
| 38 | output_path.parent.mkdir(parents=True, exist_ok=True) |
| 39 | # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11, |
| 40 | # so we check the torch version for backwards compatibility |
| 41 | if is_torch_less_than_1_11: |
| 42 | export( |
| 43 | model, |
| 44 | model_args, |
| 45 | f=output_path.as_posix(), |
| 46 | input_names=ordered_input_names, |
| 47 | output_names=output_names, |
| 48 | dynamic_axes=dynamic_axes, |
| 49 | do_constant_folding=True, |
| 50 | use_external_data_format=use_external_data_format, |
| 51 | enable_onnx_checker=True, |
| 52 | opset_version=opset, |
| 53 | ) |
| 54 | else: |
| 55 | export( |
| 56 | model, |
| 57 | model_args, |
| 58 | f=output_path.as_posix(), |
| 59 | input_names=ordered_input_names, |
| 60 | output_names=output_names, |
| 61 | dynamic_axes=dynamic_axes, |
| 62 | do_constant_folding=True, |
| 63 | opset_version=opset, |
| 64 | ) |
| 65 | |
| 66 | |
| 67 | @torch.no_grad() |