Megvii-BaseDetection / Megvii-BaseDetection/YOLOX

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

Open Beginner friendly
#1,907 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
10.6k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

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 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:

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open tools/export_onnx.py and inspect main around the torch.onnx._export call at line 95. Replace the removed private exporter with the public API while preserving the legacy exporter setting described in the issue. Run the export against a trained checkpoint and verify that an ONNX model is produced and the subsequent onnxsim simplification succeeds.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
84/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.