torch-to-onnx
Convert PyTorch models to ONNX using the modern torch.export API and the onnx_ir library.
Features
- ✅ Based on
torch.export(FX graph) for accurate tracing. - ✅ Exports to ONNX with full control over opset and IR versions.
- ✅ Lightweight and extensible – add your own ATen mappings.
- ✅ Produces standard ONNX models that can be loaded by any ONNX runtime.
Installation
pip install torch-to-onnx
If you haven't installed onnx-ir separately, you may need to:
pip install onnx-ir # or follow your own onnx_ir installation
Usage
Basic Example
import torch
import torchvision.models as models
from torch_to_onnx import convert_exported_program_to_onnx
import onnx
# 1. Create a model
model = models.resnet18(pretrained=True).eval()
dummy_input = torch.randn(1, 3, 224, 224)
# 2. Export using torch.export
ep = torch.export.export(model, (dummy_input,), strict=True)
# 3. Convert to ONNX (returns an ir.Model)
ir_model = convert_exported_program_to_onnx(
ep,
opset_version=18,
ir_version=13,
)
# 4. Serialize and save
ir.save(ir_model, "resnet18.onnx")
print("ONNX model saved!")
Customizing the Conversion
You can extend the operator mapping by editing ATEN_TO_ONNX_OP in the source code, or you can fork the repository and modify the converter.
API Reference
convert_exported_program_to_onnx(ep, opset_version=18, ir_version=13, remove_unused=True)
- ep:
torch.export.ExportedProgram– the exported program. - opset_version:
int– ONNX opset version (default 18). - ir_version:
int– ONNX IR version (default 13). - remove_unused:
bool– currently a placeholder, does nothing. - Returns:
onnx_ir.Model– an in‑memory ONNX model representation.
Supported Operators
The converter currently supports common ATen operators such as:
conv2d,batch_norm,relu,max_pool2d,avg_pool2d,add,sub,mul,div,matmul,linear,reshape,transpose,cat,softmax,sigmoid,tanh,dropout,slice,unsqueeze,squeeze,shape, etc.
If you encounter an unsupported operator, the converter will print a warning and skip it. You can easily add new mappings in the ATEN_TO_ONNX_OP dictionary.
Dependencies
- PyTorch >= 1.12.0
- ONNX >= 1.12.0
- onnx-ir (or your own fork of
onnx_ir)
License
MIT
Author
zhengankun (ankun.zheng@qq.com)
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torch_to_onnx-0.0.2.tar.gz.
File metadata
- Download URL: torch_to_onnx-0.0.2.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a208d8445f205d8a19b4ab64abab2b9d5ea73f53926c82a056022d7cd97cb0f8
|
|
| MD5 |
47a2539d32448a6e55920bea5d33be85
|
|
| BLAKE2b-256 |
9ca4970b59af8a71e1a144e381c3291de4bc54453d89887ab035e89dff30fe9b
|
File details
Details for the file torch_to_onnx-0.0.2-py3-none-any.whl.
File metadata
- Download URL: torch_to_onnx-0.0.2-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8df51fc42df47d7f6db88c0965943e30ff003a286843c892f718428ccef94497
|
|
| MD5 |
6716c342b668a369cae329fbf415c132
|
|
| BLAKE2b-256 |
061e7c1586531f7a6f5833bb34cb8e97246323ab98e580cc7032da99ee778538
|