`tools/export_onnx.py` breaks on current PyTorch: `torch.onnx` has no attribute `_export`
tools/export_onnx.py calls the internal/undocumented torch.onnx._export directly, instead of the public torch.onnx.export. That private function no longer exists in current PyTorch, so export fails immediately.
Root cause
torch.onnx._export(
model,
dummy_input,
args.output_name,
input_names=[args.input],
output_names=[args.output],
dynamic_axes={args.input: {0: 'batch'},
args.output: {0: 'batch'}} if args.dynamic else None,
opset_version=args.opset,
)torch.onnx._export was always an internal implementation detail (the public torch.onnx.export used to just call it) and has since been removed.
Environment
- PyTorch: 2.13.0
- YOLOX: fresh clone of
main, installed viapip install -e .
Steps to reproduce
- Run
tools/export_onnx.pyagainst any trained checkpoint. - Traceback:
File ".../tools/export_onnx.py", line 95, in main
torch.onnx._export(
AttributeError: module 'torch.onnx' has no attribute '_export'. Did you mean: 'export'?Suggested fix
Switch to the public torch.onnx.export. Note that current PyTorch's torch.onnx.export now defaults to the newer Dynamo-based exporter (dynamo=True); since YOLOX's custom modules (e.g. Focus, the SiLU replacement) were only ever exercised against the legacy TorchScript-tracing exporter, pass dynamo=False explicitly to keep the original export behavior:
torch.onnx.export(
model,
dummy_input,
args.output_name,
input_names=[args.input],
output_names=[args.output],
dynamic_axes={args.input: {0: 'batch'},
args.output: {0: 'batch'}} if args.dynamic else None,
opset_version=args.opset,
dynamo=False,
)Verified this produces a working ONNX model, including a successful onnxsim simplification pass afterward.
Source: Megvii-BaseDetection/YOLOX