Skip to content

Utility to inline model-local functions #5105

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 21 commits into from
May 4, 2023
Merged

Conversation

gramalingam
Copy link
Contributor

@gramalingam gramalingam commented Apr 9, 2023

Description

An initial version of utility to inline all calls to model-local functions.

Extensions to be handled in future:

  • Ideally, we should allow users to specify filters to indicate which functions to inline and/or which not to inline.
  • Doesn't yet handle opschema-defined functions.

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
@gramalingam gramalingam requested a review from a team as a code owner April 9, 2023 04:27
@jbachurski
Copy link
Member

jbachurski commented Apr 9, 2023

I would propose to implement a utility to inline a given list of FunctionProtos, which is more general and already indirectly implemented here. It'll have more uses and finding all model local functions is trivial.

@gramalingam
Copy link
Contributor Author

I would propose to implement a utility to inline a given list of FunctionProtos, which is more general and already indirectly implemented here. It'll have more uses and finding all model local functions is trivial.

I think that is equivalent? To be more specific, the implementation provides a core (namely inline_functions) that is generic and intended to enable other scenarios as well, and one specific scenario (namely inline_local_functions). But only the latter is currently exposed as an external API in the include file. The reason is that I think the core API can be made even more general, and so it may be good to iterate through other scenarios as well before finalizing the signature of this core API.

For example, I think it would be useful to allow the callers to specify which functions to inline or NOT inline (via the names of functions).

Anyway: I agree that other use-cases will arise and we should support them. This is just the first step.

@jbachurski
Copy link
Member

Thanks for responding! I think we are on the same page then. I noticed it ends up implemented in C++ but I had only looked for a Python test/bindings.
In any case, it would be nice to have an ergonomic API for this graph transformation.

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
@gramalingam gramalingam changed the title Utility to inline model-local functions (Draft) Utility to inline model-local functions Apr 13, 2023
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
@gramalingam gramalingam enabled auto-merge (squash) April 28, 2023 18:09
@justinchuby
Copy link
Member

justinchuby commented May 4, 2023

I tested the inliner with this model, but it seemed to have returned the same model:

gpt2-dataprop.zip

@gramalingam
Copy link
Contributor Author

I tested the inliner with this model, but it seemed to have returned the same model:

gpt2-dataprop.zip

I tried it. Looks like the model uses onnx opset 17, while the functions use opset 18. Currently, inliner does not inline functions when there is an opset mismatch. Changed model opset to 18, and I think it did something: looks like it inlined 24 of 26 functions, the number of nodes in main graph went up from 383 to 595. Didn't check anything more.

@gramalingam
Copy link
Contributor Author

I tested the inliner with this model, but it seemed to have returned the same model:
gpt2-dataprop.zip

I tried it. Looks like the model uses onnx opset 17, while the functions use opset 18. Currently, inliner does not inline functions when there is an opset mismatch. Changed model opset to 18, and I think it did something: looks like it inlined 24 of 26 functions, the number of nodes in main graph went up from 383 to 595. Didn't check anything more.

Integrating the version-converter to automatically convert functions to model's opset is work in progress. (Even that may not always succeed for for version down-conversion; version up-conversion should be more complete.)

@justinchuby
Copy link
Member

I tried it. Looks like the model uses onnx opset 17

Ah that's right. Didn't realize that. Thanks!

@gramalingam gramalingam merged commit 65e8f81 into onnx:main May 4, 2023
adityagoel4512 pushed a commit to adityagoel4512/onnx that referenced this pull request May 9, 2023
* Add function inliner part 1

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Inliner for model-local functions

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Minor fixes

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Export inliner to python

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Fix emptied files

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* remove constexpr

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Replace move by copy

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Run formatter

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Format file

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Check names are unique

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* naming conventions

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Test for opset mistmatch

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Remove move

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Address PR feedback

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Add testcase with two calls to same function

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Comment debugging code

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Add comments about renaming

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Run formatter

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

---------

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Aditya Goel <agoel4512@gmail.com>
adityagoel4512 pushed a commit to adityagoel4512/onnx that referenced this pull request Jul 28, 2023
* Add function inliner part 1

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Inliner for model-local functions

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Minor fixes

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Export inliner to python

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Fix emptied files

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* remove constexpr

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Replace move by copy

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Run formatter

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Format file

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Check names are unique

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* naming conventions

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Test for opset mistmatch

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Remove move

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Address PR feedback

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Add testcase with two calls to same function

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Comment debugging code

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Add comments about renaming

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

* Run formatter

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>

---------

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Aditya Goel <agoel4512@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: utility Helper modules
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants