-
Notifications
You must be signed in to change notification settings - Fork 526
[6058907] Fix ShapeInferenceError in ONNX int8+fp16 quantization of weakly-typed models #1627
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9359aa0
[6058907][ONNX] Fix ShapeInferenceError in int8+fp16 quantization of …
ajrasane 26fe4a6
[6058907][ONNX] Extract clear_types_and_shapes_recursive to autocast …
ajrasane d4d2667
[6058907][ONNX] Simplify clear_types_and_shapes_recursive
ajrasane c3a8dae
[6058907][ONNX] Simplify _reconcile_stale_output_shapes
ajrasane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ajrasane There's similar logic in
PrecisionConverter::_clear_types_and_shapes_recursive. In which we don't check what's stale and what's not, just clear everything since we're going to infer types (and optionally shapes) from the graph.Perhaps we can extract it to utils + add the fallback to standalone type inference on shape inference expcetion?
Model-Optimizer/modelopt/onnx/autocast/precisionconverter.py
Lines 287 to 345 in cf2d65a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @galagam — good call, addressed in 7374672.
1. Extracted the clear logic to utils.
_clear_types_and_shapes_recursiveis now a module-levelclear_types_and_shapes_recursive(graph, clear_shapes=...)inautocast/utils.py, right next towalk_subgraphs_recursive(its only dependency).PrecisionConvertercalls it withclear_shapes=not self.use_standalone_type_inference. I kept it in the autocast utils rather than the generalonnx/utils.pybecause the latter can only reachwalk_subgraphs_recursivethrough the lazy import it already does in_infer_types_only— moving it there would force the same circular-import dance.2. Fallback to standalone type inference on shape-inference exception is centralized in
onnx_utils.infer_types(the try/except in this PR). The AutoCast precision-conversion path already routes through it viainfer_types(..., strict_mode=True), so it gets the fallback for free.One thing I deliberately did not fold into the wholesale clear: the output-shape reconciliation (
_reconcile_stale_output_shapes).clear_types_and_shapes_recursiveonly renames concrete dims tounkand setselem_type=UNDEFINED— it never changes a tensor's rank. The bug here is a stale rank-0 output on a tensor that's really rank-2+, so clear-and-reinfer leaves the rank-0 in place and it re-poisons inference. We also can't justClearField("shape")the output: a graph output with no shape failsonnx.checker, and the type-only fallback won't repopulate it. So reconciliation re-derives a present, correct-rank shape (ORT symbolic inference →infer_shapes) and overwrites only genuinely-stale declarations.