Skip to content

Restrict ONNX opset to 16 and up #3051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 34 commits into
base: main
Choose a base branch
from

Conversation

antimora
Copy link
Collaborator

@antimora antimora commented Apr 20, 2025

Related Issue:

Fixes #3046

Changes:

  • Updated ONNX model import documentation for clarity and added a new "ONNX Compatibility" section.
  • Required ONNX models to use opset version 16 or higher, deprecating older versions.
  • Simplified documentation text and added a Python script for upgrading ONNX models to opset 16.
  • Disabled outdated ONNX test cases (e.g., clip_opset7, dropout_opset7, reduce_sum_opset11) to align with opset 16.
  • Removed unnecessary imports in test code for a cleaner codebase.

Testing:

  • Verified exception handling for opset 7 ONNX models.
  • Confirmed all existing tests pass.

Checklist:

  • Ran run-checks all script.
  • Updated the book with PR changes.

antimora added 10 commits March 4, 2025 15:39
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
- Update struct TensorData field access patterns
- Add support for non-optional data fields
- Fix issues with tensor data handling
The PR resolves several compilation errors caused by changes to the TensorType structure:

1. Modified code to adapt to the removal of the `shape` field from TensorType
2. Fixed pattern matching issues in rank_inference.rs to properly match against TensorData.data
3. Updated from_onnx.rs's remap_unsqueeze_to_reshape function to work with the new API
4. Fixed unused imports across multiple files
5. Fixed function calls that were using Option.len() incorrectly
Enhance tensor type system to support both static shapes and dynamic ranks across multiple ONNX operations including Expand, RandomNormal, Constant, and related nodes. Ensure proper shape validation and improve type safety throughout the conversion process.
@antimora antimora requested review from Copilot and laggui April 20, 2025 22:23
Copilot

This comment was marked as outdated.

@antimora antimora changed the title Restrict opset 16 Restrict ONNX opset to 16 and up Apr 20, 2025
Copy link

codecov bot commented Apr 20, 2025

Codecov Report

Attention: Patch coverage is 86.56716% with 9 lines in your changes missing coverage. Please review.

Project coverage is 81.10%. Comparing base (d6533da) to head (e15a2b3).

Files with missing lines Patch % Lines
crates/onnx-ir/src/from_onnx.rs 61.11% 7 Missing ⚠️
crates/onnx-ir/src/util.rs 87.50% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3051      +/-   ##
==========================================
- Coverage   81.12%   81.10%   -0.03%     
==========================================
  Files         816      816              
  Lines      117358   117312      -46     
==========================================
- Hits        95210    95142      -68     
- Misses      22148    22170      +22     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@laggui laggui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to restrict to more recent opsets. The ONNX spec is already large enough, we should try to limit the scope 😅

In this case, I would simply delete the tests and ONNX files associated with older opsets.

@antimora antimora force-pushed the restrict-opset-16 branch from 1a8e016 to db436b9 Compare April 24, 2025 15:56
@antimora antimora requested a review from laggui April 24, 2025 23:56
@antimora antimora force-pushed the restrict-opset-16 branch from 0c4de8d to a4eab9b Compare April 25, 2025 16:58
Copy link
Member

@laggui laggui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall! Mostly minor comments.

Will wait for the linked PR to be merged before this review is finalized.

Option 1: Use the provided utility script:

```
uv run --script https://raw.githubusercontent.com/tracel-ai/burn/refs/heads/main/crates/burn-import/onnx-opset-upgrade.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably provide default command using python (regardless of package manager used)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True but I am seeing more people shifting towards uv. We are using it consistently with Burn too.

There are a few convenient aspect of this example:

  1. uv downloads the script directly and executes.
  2. creates necessary virtual environment
  3. installs the dependency packages specified in the script itself.

Copy link
Member

@laggui laggui Apr 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I am not advocating to remove the uv command entirely, just that we might want to provide the basic steps to convert the model with python using the provided script 🙂

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can download the script and run in python like python3 onnx_opset_upgrade.py too or they the can follow the python script steps.

Another alternative is to call uv as a library. We can put it behind a feature flag. This pattern can also work with dataset crate. See astral-sh/uv#4642

@antimora antimora force-pushed the restrict-opset-16 branch from a4eab9b to f607b89 Compare April 25, 2025 18:27
@antimora antimora requested a review from laggui April 25, 2025 19:19
Comment on lines 67 to 79
```python
import onnx
from onnx import version_converter

# Load the ONNX model
model = onnx.load('path/to/your/model.onnx')

# Upgrade to opset version 16
converted_model = version_converter.convert_version(model, 16)

# Save the upgraded model
onnx.save(converted_model, 'upgraded_model.onnx')
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, why are there so many more steps in the onnx_opset_upgrade.py script? Actually, most of the additional code is for debug info or user input, but what about the additional shape inference?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the document to include that step. I added it as a safe measure in case the older opset files didn't infer shapes already. But also as a precursor to use inferred node output shapes. For now, I think we can figure out Input/Output ranks from the static shapes.

@antimora antimora requested a review from laggui April 26, 2025 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ONNX import: require opset version to be 16 at least
2 participants