#1907·YOLOX

`tools/export_onnx.py` breaks on current PyTorch: `torch.onnx` has no attribute `_export`

Author: HikeAndMapCreated Aug 29, 2026Updated Aug 29, 2026

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

python
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 via pip install -e .

Steps to reproduce

  1. Run tools/export_onnx.py against any trained checkpoint.
  2. 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:

python
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