Skip to content
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

Merged
merged 9 commits into from
Nov 14, 2024

Conversation

vinayakdsci
Copy link
Contributor

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.

@vinayakdsci vinayakdsci marked this pull request as draft October 24, 2024 14:15
@vinayakdsci vinayakdsci marked this pull request as ready for review October 24, 2024 14:16
@vinayakdsci
Copy link
Contributor Author

cc @kumardeepakamd.

@vinayakdsci
Copy link
Contributor Author

vinayakdsci commented Oct 24, 2024

There is a failure for models with dynamic dims when the parameters are externalized: #18881.

However, it appears to be unrelated to this patch.

Copy link
Collaborator

@stellaraccident stellaraccident left a 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.

@vinayakdsci
Copy link
Contributor Author

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 ResNet152_vaiq_int8 https://gist.github.com/vinayakdsci/1e75b651fb66be238f4f6bd9d5e50502

@ScottTodd
Copy link
Member

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)

@vinayakdsci
Copy link
Contributor Author

Also, have you read through the discussions on #18365?

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).

@vinayakdsci vinayakdsci force-pushed the importer-external-weight-support branch from 5eb7b87 to b6ecf5a Compare October 25, 2024 17:56
@vinayakdsci vinayakdsci requested a review from zjgarvey October 25, 2024 17:59
@vinayakdsci vinayakdsci force-pushed the importer-external-weight-support branch from b6ecf5a to 4f52c82 Compare October 25, 2024 18:10
@ScottTodd
Copy link
Member

Also, have you read through the discussions on #18365?

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).

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 iree-import-onnx with a new API, while this PR extends the existing tool. There are architectural discussions to be had there on the issue. This component needs an owner that can manage those discussions and coordinate work between contributors/maintainers.

@vinayakdsci
Copy link
Contributor Author

@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.

@vinayakdsci vinayakdsci requested a review from ScottTodd November 4, 2024 13:09
Copy link
Contributor

@zjgarvey zjgarvey left a 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?

Copy link
Member

@ScottTodd ScottTodd left a 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!

@vinayakdsci vinayakdsci force-pushed the importer-external-weight-support branch from b5db48b to 13b5638 Compare November 11, 2024 06:01
@zjgarvey
Copy link
Contributor

How big is the model.onnx file? I'd be fine with a test using small weights just to make sure the api works.

@vinayakdsci
Copy link
Contributor Author

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?

@zjgarvey
Copy link
Contributor

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.

@vinayakdsci vinayakdsci force-pushed the importer-external-weight-support branch from 1da3d7a to 9a5f443 Compare November 12, 2024 20:37
@vinayakdsci
Copy link
Contributor Author

@zjgarvey reduced the model file size to 8.0K.

@zjgarvey
Copy link
Contributor

Okay, resolve outdated conversations with @ScottTodd and we will wait for his approval.

@benvanik
Copy link
Collaborator

8KB is fine - thanks for making it small - git never forgets and every KB added is future pain.

@ScottTodd ScottTodd added the integrations/onnx ONNX integration work label Nov 13, 2024
Copy link
Member

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

Copy link
Member

@ScottTodd ScottTodd left a 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):
Copy link
Member

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.

Comment on lines +31 to +65
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")
Copy link
Member

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.

@vinayakdsci vinayakdsci force-pushed the importer-external-weight-support branch from 9a5f443 to c586034 Compare November 14, 2024 19:24
@vinayakdsci
Copy link
Contributor Author

@ScottTodd thanks for the approval! I have addressed the remaining comments and rebased the PR.

  • Will you follow up with documentation, or should we file an issue?

I should be able to open a PR to follow up with the documentation too.

@zjgarvey
Copy link
Contributor

@vinayakdsci would you like me to merge this now?

@zjgarvey zjgarvey merged commit d497571 into iree-org:main Nov 14, 2024
36 checks passed
ScottTodd added a commit that referenced this pull request Nov 19, 2024
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
Copy link
Member

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

Copy link
Contributor

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.

Copy link
Contributor Author

@vinayakdsci vinayakdsci Nov 20, 2024

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.

Copy link
Contributor

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.

Groverkss pushed a commit to Groverkss/iree that referenced this pull request Dec 1, 2024
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.
Groverkss pushed a commit to Groverkss/iree that referenced this pull request Dec 1, 2024
…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.
giacs-epic pushed a commit to giacs-epic/iree that referenced this pull request Dec 4, 2024
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>
giacs-epic pushed a commit to giacs-epic/iree that referenced this pull request Dec 4, 2024
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrations/onnx ONNX integration work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants