-
Notifications
You must be signed in to change notification settings - Fork 66
Update opset imports in version_converter #2295
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
from __future__ import annotations | ||
|
||
import copy | ||
import dataclasses | ||
import functools | ||
import logging | ||
|
@@ -276,39 +277,45 @@ def visit_node( | |
self.replace_node(node, replacement, root) | ||
return None | ||
|
||
def visit_graph(self, graph: ir.Graph) -> None: | ||
def visit_graph(self, graph: ir.Graph) -> ir.Graph | None: | ||
if self.target_version > SUPPORTED_MAX_ONNX_OPSET: | ||
logger.warning( | ||
"Conversion to target opset: %s not currently supported.", | ||
self.target_version, | ||
) | ||
return None | ||
for node in graph: | ||
up_conversion = True | ||
if node.version is None: | ||
node.version = self.model_version | ||
# Iterate each node from current node version -> target version | ||
# and updating node based on the correct adapter | ||
# Up-conversion [ver->ver+1] or down-conversion [ver->ver-1] | ||
# TODO(shubhambhokare1): Remove once down-conversion adapters are supoorted | ||
if self.target_version < node.version: | ||
up_conversion = False | ||
logger.warning( | ||
"Target opset: %s less than %s, downstream version conversion not currently handled.", | ||
self.target_version, | ||
self.model_version, | ||
) | ||
return None | ||
for opset_version in range(node.version, self.target_version): | ||
|
||
# TODO(shubhambhokare1): Support down-conversion | ||
while self.model_version < self.target_version: | ||
pre_conversion_graph = copy.copy(graph) | ||
# Up-convert each node in the graph from opset_version -> opset_version + 1 | ||
# or down-convert from opset_version -> opset_version - 1 | ||
# Return non-converted graph if any node fails to convert. | ||
for node in graph: | ||
up_conversion = True | ||
if node.version is None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should check if it is an onnx domain op first: else skip, and don't do anything (even updating node.version). |
||
node.version = self.model_version | ||
if self.target_version < node.version: | ||
# up_conversion = False | ||
# TODO(shubhambhokare1): Remove once down-conversion adapters are supoorted | ||
logger.warning( | ||
"Target opset: %s less than %s, downstream version conversion not currently handled.", | ||
self.target_version, | ||
node.version, | ||
) | ||
return pre_conversion_graph | ||
try: | ||
self.visit_node(node, graph, opset_version, up_conversion) | ||
self._upgrade_version(node, opset_version, up_conversion) | ||
self.visit_node(node, graph, self.model_version, up_conversion) | ||
self._upgrade_version(node, self.model_version, up_conversion) | ||
except VersionConverterError as e: | ||
logger.warning( | ||
"Skipping version conversion for node %s due to exception: %s", | ||
node.op_type, | ||
e, | ||
) | ||
return pre_conversion_graph | ||
self.model_version += 1 | ||
del pre_conversion_graph | ||
return None | ||
|
||
def visit_model(self, model: ir.Model) -> None: | ||
|
@@ -319,7 +326,18 @@ def visit_model(self, model: ir.Model) -> None: | |
if model_version is None: | ||
return None | ||
self.model_version = model_version | ||
self.visit_graph(model.graph) | ||
graph = self.visit_graph(model.graph) | ||
if graph is not None: | ||
model.graph = graph | ||
|
||
# Finally, update the opset imports for the model | ||
if self.model_version != self.target_version: | ||
logger.warning( | ||
"Converting to opset %s failed. Model was converted to opset version %s.", | ||
self.target_version, | ||
self.model_version, | ||
) | ||
model.opset_imports[""] = self.model_version | ||
return None | ||
|
||
|
||
|
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.
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.
@justinchuby : how expensive is copy? Specifically, what does it do for large tensors?
Uh oh!
There was an error while loading. Please reload this page.
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.
We can't copy graphs yet. This won't work (sorry)
I intend to implement
graph.duplicate()
. But I have not done it. cc @titaiwangms who has a similar need.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.
@justinchuby What would be the correct way to store pre-conversion state graphs?
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.
Maybe implement a proper graph.duplicate()? Before that maybe aborting on failures is acceptable?
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.
#2298