fix(export): onnx export issue#3554
fix(export): onnx export issue#3554ashwinvaidya17 wants to merge 2 commits intoopen-edge-platform:mainfrom
Conversation
Signed-off-by: Ashwin Vaidya <ashwinnitinvaidya@gmail.com>
Signed-off-by: Ashwin Vaidya <ashwinnitinvaidya@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR updates anomalib’s ONNX/OpenVINO export path to better handle PyTorch 2.9+ behavior (notably the torch.onnx.export dynamo exporter), adds targeted regression tests, and relaxes PyTorch version constraints to unblock newer Python/PyTorch combinations.
Changes:
- Add export utility helpers to resolve
dynamodefaults, translatedynamic_axes→dynamic_shapes, and raise an actionable error whenonnxscriptis missing. - Update
ExportMixin.to_onnxto support dynamo export while keeping the legacy exporter as default (with deprecation warning). - Relax PyTorch upper bounds in optional dependency groups and add unit tests covering the new export behavior.
Reviewed changes
Copilot reviewed 6 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/anomalib/models/components/base/export_mixin/mixin.py |
Switch ONNX export logic to explicitly control dynamo, handle dynamic_shapes, and centralize helpers. |
src/anomalib/models/components/base/export_mixin/utils.py |
New helper functions for export flag resolution, validation, dynamic-shape translation, and export-root creation. |
src/anomalib/models/components/base/export_mixin/__init__.py |
Adds package export for ExportMixin. |
tests/unit/deploy/test_export_mixin.py |
New unit tests asserting default legacy behavior, dynamo behavior, translation logic, and actionable onnxscript errors. |
pyproject.toml |
Remove torch<=2.8.0 caps for several extras to allow newer torch versions. |
CHANGELOG.md |
Document legacy exporter deprecation and dynamo-related export fixes under Unreleased. |
Comments suppressed due to low confidence (2)
src/anomalib/models/components/base/export_mixin/mixin.py:1
- The copyright header was changed from a prior year to a single "2026" year. For modified files, the repo commonly uses a year range (e.g.,
src/anomalib/engine/engine.py:1uses2024-2026). Please update this header to reflect the original + current year range (e.g.,2024-2026) instead of only2026.
src/anomalib/models/components/base/export_mixin/mixin.py:131 **kwargs: dict[str, Any]is not the correct typing for**kwargs. This annotation means each individual kwarg value must be adict[str, Any], which is not whattorch.onnx.exportaccepts and will confuse type-checkers. Please change this to**kwargs: Any(or a more specific set of explicit keyword parameters if you want stronger typing).
| def validate_input_names(input_names: object) -> list[str]: | ||
| """Validate ONNX input names. | ||
|
|
||
| Args: | ||
| input_names (object): Candidate input names value. | ||
|
|
||
| Returns: | ||
| list[str]: Validated input names. | ||
|
|
||
| Raises: | ||
| TypeError: If input names are not a list of strings. | ||
| """ | ||
| if isinstance(input_names, list) and all(isinstance(name, str) for name in input_names): | ||
| return input_names | ||
| msg = "input_names must be a list of strings" | ||
| raise TypeError(msg) |
There was a problem hiding this comment.
validate_input_names only accepts list[str], but torch.onnx.export accepts any Sequence[str] for input_names. This is a breaking behavior change for callers that previously passed a tuple (or other sequence). Consider accepting Sequence[str] (and converting to list[str] internally) to keep the public API flexible while still validating element types.
📝 Description
torch.onnx.exportfrom 2.9 isdynamo=True. So even when dynamo=None is passed, it defaults to True. One option is to pin to False (keep legacy) but imo we should update to the new default and slowly increase compatibility (https://docs.pytorch.org/tutorials/intermediate/torch_export_tutorial.html). They also renamed dynamic_axes to shapesdynamo=Truetill we don't switch to functions liketorch.cond✨ Changes
Select what type of change your PR is:
✅ Checklist
Before you submit your pull request, please make sure you have completed the following steps:
For more information about code review checklists, see the Code Review Checklist.