-
Notifications
You must be signed in to change notification settings - Fork 645
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
[onnx][importer] Add support for externalized params #18880
[onnx][importer] Add support for externalized params #18880
Conversation
cc @kumardeepakamd. |
There is a failure for models with dynamic dims when the parameters are externalized: #18881. However, it appears to be unrelated to this patch. |
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. I think you have the basic idea of what to do here, but I need to take a closer look: I think there are ways to do this with less code duplication.
Yeah, I am sure about that too. But I had some issues with calls to super(), so I had to duplicate some code to get it working. P.S: In case it helps, here is the IR generated by the importer for |
Please tag related issues on PRs (#18289 here). Also, have you read through the discussions on #18365? (the top of https://iree.dev/developers/general/contributing/ has a note about communicating so work isn't duplicated) |
Thanks, read it now. (The changes there, though similar, seem to me to be unrelated to this PR, and it is mentioned there that this should have had been implemented separately). |
compiler/bindings/python/iree/compiler/tools/import_onnx/__main__.py
Outdated
Show resolved
Hide resolved
compiler/bindings/python/iree/compiler/tools/import_onnx/__main__.py
Outdated
Show resolved
Hide resolved
compiler/bindings/python/iree/compiler/tools/import_onnx/__main__.py
Outdated
Show resolved
Hide resolved
compiler/bindings/python/iree/compiler/tools/import_onnx/__main__.py
Outdated
Show resolved
Hide resolved
compiler/bindings/python/iree/compiler/tools/import_onnx/__main__.py
Outdated
Show resolved
Hide resolved
5eb7b87
to
b6ecf5a
Compare
b6ecf5a
to
4f52c82
Compare
Yep, I just want us to be coordinated via public GitHub issues discussions. The issue and PR I linked were tossing around the idea of replacing |
compiler/bindings/python/iree/compiler/tools/import_onnx/__main__.py
Outdated
Show resolved
Hide resolved
compiler/bindings/python/iree/compiler/tools/import_onnx/__main__.py
Outdated
Show resolved
Hide resolved
compiler/bindings/python/iree/compiler/tools/import_onnx/__main__.py
Outdated
Show resolved
Hide resolved
@ScottTodd my apologies for the delay, I have addressed all your comments in the latest commit. Also added a new flag that allows configuring the parameter scope. |
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.
I think this is in a good spot to merge for now. @ScottTodd do you have any lingering comments?
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.
A few style comments and a request for test coverage. This is pretty close otherwise. Thanks!
compiler/bindings/python/iree/compiler/tools/import_onnx/importer_externalization_overrides.py
Show resolved
Hide resolved
compiler/bindings/python/iree/compiler/tools/import_onnx/importer_externalization_overrides.py
Outdated
Show resolved
Hide resolved
compiler/bindings/python/iree/compiler/tools/import_onnx/importer_externalization_overrides.py
Show resolved
Hide resolved
compiler/bindings/python/iree/compiler/tools/import_onnx/importer_externalization_overrides.py
Outdated
Show resolved
Hide resolved
b5db48b
to
13b5638
Compare
How big is the model.onnx file? I'd be fine with a test using small weights just to make sure the api works. |
The model file is 9.8M in size. Would you like me to make it smaller? |
Yeah, we want a functionality test, so the file size isn't necessary. Just big enough to work for the default size threshold would be ideal. |
1da3d7a
to
9a5f443
Compare
@zjgarvey reduced the model file size to 8.0K. |
Okay, resolve outdated conversations with @ScottTodd and we will wait for his approval. |
8KB is fine - thanks for making it small - git never forgets and every KB added is future pain. |
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.
I won't have time to review this again for a few days. One thing that could be worked on in the meantime is documentation at https://iree.dev/guides/ml-frameworks/onnx/ for parameter usage, similar to https://iree.dev/guides/ml-frameworks/pytorch/#using-external-parameters. The source of that page is here: https://github.com/iree-org/iree/blob/main/docs/website/docs/guides/ml-frameworks/onnx.md
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.
Found a bit of time to review. LGTM!
- Please move the test function down in the file
- Will you follow up with documentation, or should we file an issue?
) | ||
|
||
|
||
class ImportOnnxwithExternalizationTest(unittest.TestCase): |
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.
Please move this test case under ImportOnnxTest
in the file (just before if __name__ == "__main__":
), so the test file starts simple and then has more complex test cases.
def setUp(self): | ||
with tempfile.NamedTemporaryFile(delete=False) as f: | ||
self.outputPath = f.name | ||
|
||
def tearDown(self) -> None: | ||
if os.path.exists(self.outputPath): | ||
os.unlink(self.outputPath) | ||
if os.path.exists("custom_params_file.irpa"): | ||
os.unlink("custom_params_file.irpa") | ||
if os.path.exists(str(self.outputPath) + "_params.irpa"): | ||
os.unlink(str(self.outputPath) + "_params.irpa") |
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.
This code pattern for opening a temporary file, storing the name of that file, and then later deleting it may not work on all platforms.
edit: I ran this on Windows and it worked. Phew.
9a5f443
to
c586034
Compare
@ScottTodd thanks for the approval! I have addressed the remaining comments and rebased the PR.
I should be able to open a PR to follow up with the documentation too. |
@vinayakdsci would you like me to merge this now? |
As discussed on #18630 (comment), now that `iree-import-onnx` supports upgrading the model version itself (added in #18880), we don't need to also maintain this logic in the `iree.build` package.
import copy | ||
import random | ||
import string | ||
import iree.runtime as rt |
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.
I missed this during review, but this is the only place in the compiler Python bindings that depends on the runtime Python bindings. It is used for rt.ParameterIndex
and create_archive_file
. That makes
pip install iree-base-compiler[onnx]
insufficient for running iree-import-onnx
now, as iree-base-runtime
must also be installed
We could
- have this check the import and fail gracefully
- add a compiler API for creating parameter archives so the runtime API is not needed
- have the compiler Python package depend on the runtime Python package
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.
I'm working on a few improvements in #19217. Will make a conditional import for this in that patch.
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.
@ScottTodd I think it would be best to fail gracefully in case the import fails, at least for now. I am not very sure about how having the compiler python package depend on the runtime package should be implemented (maybe you could look at that if you find the time?), and adding a compiler API for creating parameter archives could possibly lead to code duplication IMO.
I can create a PR for the first option right away.
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.
I think the first option, importing conditionally on whether "--externalize-parameters" is passed, with a message saying something like "externalizing parameters requires runtime api" would be the most efficient route.
This patch adds support to externalize params, and store them to the given path as an IRPA file. The IR imported with externalization should now prevent possible OOM errors happening due to large inlined parameters.
…rg#19210) As discussed on iree-org#18630 (comment), now that `iree-import-onnx` supports upgrading the model version itself (added in iree-org#18880), we don't need to also maintain this logic in the `iree.build` package.
This patch adds support to externalize params, and store them to the given path as an IRPA file. The IR imported with externalization should now prevent possible OOM errors happening due to large inlined parameters. Signed-off-by: Giacomo Serafini <179146510+giacs-epic@users.noreply.github.com>
…rg#19210) As discussed on iree-org#18630 (comment), now that `iree-import-onnx` supports upgrading the model version itself (added in iree-org#18880), we don't need to also maintain this logic in the `iree.build` package. Signed-off-by: Giacomo Serafini <179146510+giacs-epic@users.noreply.github.com>
This patch adds support to externalize params, and store them to the given path as an IRPA file.
The IR imported with externalization should now prevent possible OOM errors happening due to large inlined parameters.