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

[AOT] Name mangling in AOT #8014

Merged
merged 2 commits into from
Jun 28, 2021
Merged

[AOT] Name mangling in AOT #8014

merged 2 commits into from
Jun 28, 2021

Conversation

giuseros
Copy link
Contributor

@giuseros giuseros commented May 10, 2021

Mini-RFC is here: https://discuss.tvm.apache.org/t/mini-rfc-name-mangling-in-aot

With this change we'll mangle the name of global symbols so that we can bundle
together multiple models in the same application.

The relay.build interface has been left unchanged, which means I am
resuing mod_name as a prefix for all functions.

  • If mod_name is None then tvmgen_ prefix is used
  • If mod_name is specified then tvmgen_mod_name_ prefix is used

I refactored the aot test utils and added some tests for multiple
models.

Change-Id: I310af75c24e422861aeaceb3c3cd4cd602071df5

@giuseros
Copy link
Contributor Author

cc: @manupa-arm @areusch @Mousius

include/tvm/runtime/module.h Outdated Show resolved Hide resolved
include/tvm/tir/builtin.h Outdated Show resolved Hide resolved
python/tvm/micro/model_library_format.py Outdated Show resolved Hide resolved
python/tvm/relay/build_module.py Outdated Show resolved Hide resolved
src/runtime/meta_data.h Outdated Show resolved Hide resolved
tests/python/relay/aot/aot_test_utils.py Outdated Show resolved Hide resolved
tests/python/relay/aot/aot_test_utils.py Show resolved Hide resolved
@giuseros
Copy link
Contributor Author

I marked the naming comments "resolved". The meaning is that we will discuss those on the RFC here: https://discuss.tvm.apache.org/t/mini-rfc-name-mangling-in-aot/

@giuseros
Copy link
Contributor Author

I made the name-mangling logic completely decoupled by the non-name-mangling logic. This means that name mangling is enabled only in AOT and in PartitionGraphWithModName, the rest of TVM won't be using this feature by default.

Copy link
Contributor

@areusch areusch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @giuseros, thanks for adding this! here's an initial round of review comments

python/tvm/micro/model_library_format.py Outdated Show resolved Hide resolved
python/tvm/micro/model_library_format.py Show resolved Hide resolved
python/tvm/relay/build_module.py Show resolved Hide resolved
src/target/source/codegen_c_host.cc Outdated Show resolved Hide resolved
src/target/source/codegen_c_host.cc Outdated Show resolved Hide resolved
tests/python/relay/aot/aot_test_utils.py Show resolved Hide resolved
src/relay/transforms/partition_graph.cc Outdated Show resolved Hide resolved
src/relay/transforms/partition_graph.cc Outdated Show resolved Hide resolved
@giuseros giuseros force-pushed the aot-name-mangling branch 2 times, most recently from cbb0d21 to 18fadc8 Compare May 18, 2021 21:40
Copy link
Contributor

@manupak manupak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @giuseros ,

Sorry been busy a bit :)

I did an initial pass.

I guess my main concern is why are we duplicating lowering functions with and without name mangling support. I would naturally think user would want to name mangle the function names if a mod_name is provided and mangled names are strictly superior to non-mangled polluted names, irrespective of whether they are being used in AoT or not, unless Im missing something.

I'd like to hear @areusch thoughts as well.

include/tvm/runtime/module.h Outdated Show resolved Hide resolved
python/tvm/micro/model_library_format.py Show resolved Hide resolved
python/tvm/relay/build_module.py Outdated Show resolved Hide resolved
python/tvm/relay/build_module.py Outdated Show resolved Hide resolved
python/tvm/relay/transform/transform.py Outdated Show resolved Hide resolved
src/relay/backend/aot_executor_codegen.cc Outdated Show resolved Hide resolved
src/relay/backend/compile_engine.cc Outdated Show resolved Hide resolved
src/relay/backend/compile_engine.cc Outdated Show resolved Hide resolved
src/relay/backend/compile_engine.h Outdated Show resolved Hide resolved
tests/python/relay/aot/test_crt_aot.py Outdated Show resolved Hide resolved
@giuseros
Copy link
Contributor Author

giuseros commented May 24, 2021

Hi @manupa-arm ,
Thanks for your reply. I pushed a new set of changes trying to address your comments:

  • Now I am using name-mangling as default in all TVM, removing duplication for non-mangled behaviour.
  • The MangleGraph pass is now the default in the partitioning pipeline
  • The mangled mod-name is formed in python and passed down in C++ where we only concatenate to the function names

I guess that, in addition to make the tests pass, we need to agree on naming:

  • MangleGraph. Is MangleExternalFunction better? Because I am only mangling the functions that will be offloaded to the external compiler
  • run_func: is it ok for everyone if I use only run? My problem with only run is that it seems a bit too generic. Maybe run_model? What do you think?

@giuseros giuseros force-pushed the aot-name-mangling branch 4 times, most recently from 4ee6f0e to 1da5b29 Compare May 24, 2021 16:16
cmake/modules/contrib/VitisAI.cmake Outdated Show resolved Hide resolved
src/relay/transforms/partition_graph.cc Outdated Show resolved Hide resolved
tests/python/relay/aot/aot_test_utils.py Outdated Show resolved Hide resolved
tests/python/relay/aot/aot_test_utils.py Outdated Show resolved Hide resolved
python/tvm/relay/backend/utils.py Outdated Show resolved Hide resolved
python/tvm/relay/backend/utils.py Outdated Show resolved Hide resolved
python/tvm/relay/backend/utils.py Show resolved Hide resolved
python/tvm/relay/backend/utils.py Show resolved Hide resolved
python/tvm/relay/transform/transform.py Outdated Show resolved Hide resolved
@giuseros
Copy link
Contributor Author

giuseros commented Jun 3, 2021

Hi @manupa-arm , @areusch ,
All the tests have finally passed. Please, let me know if there is anything else I should address.

Copy link
Contributor

@areusch areusch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just one other nit (honestly mostly care about the unit test, but adding a comment since i saw this)

src/relay/transforms/partition_graph.cc Outdated Show resolved Hide resolved
@giuseros giuseros force-pushed the aot-name-mangling branch 3 times, most recently from 15072dc to 469d10d Compare June 7, 2021 17:58
Mousius added a commit to Mousius/tvm that referenced this pull request Jun 18, 2021
This introduces structures generated to provide a documented and stable user
friendly interface to a TVM generated model, as can be seen in the AOT
demo application:
```
struct tvm_default_inputs inputs = {
  .input_1 = input_data,
};
struct tvm_default_outputs outputs = {
  .output = output_data,
};
int ret_val = tvm_default_run(&inputs, &outputs, NULL, NULL);
```

To facilitate this, some other changes are included:
* Removed dependency on `aot_executor.{c,h}` in tests, pending the
discussion in the interface RFC as to whether we keep them.
* Moved creation of test DLTensor's into the AOT test utils, in future this
can be replaced by loading via the Python API or otherwise
* Introduce `parametrize_aot_options` which can be used to test
permutations of AOT which work together - for now this filters C
interface and packed operators
* Updated demo application to generate the header for demonstration
purposes, we should consider porting the demo application to Model
Library Format and using the toolchain in the Zephyr App via CMake
instead?

This patch builds upon the improvements @giuseros made to AOT testing
and will greatly benefit from name mangling from apache#8014
@areusch
Copy link
Contributor

areusch commented Jun 22, 2021

apologies, i think i missed this and it now needs a rebase or merge.

i discussed this with @jroesch a bit as well, who is now working in earnest to merge #7518 in the next two weeks. following that PR, we'll arrive at a state where we can begin to think of the entire compiler as a series of IRModule passes. our discussion was that, at that point, it probably makes sense to move things like name mangling and e.g. the type signature changes proposed by @Mousius to one end of the compiler stack (i.e. at the relay level or at the end of the TIR passes). this way, the middle part of the compiler remains as generic as possible.

we'd like to propose the following:

to that end, we'd like to briefly prioritize #7518 next week over AOT changes to reduce the churn and let it land, since it's a fairly invasive PR.

if that's ok with you guys, i'll hand this off to @jroesch to merge while i'm ooo next few days.

Mini-RFC is here: https://discuss.tvm.apache.org/t/mini-rfc-name-mangling-in-aot

With this change we'll mangle the name of global symbols so that we can bundle
together multiple models in the same application.

The relay.build interface has been left unchanged, which means I am
resuing mod_name as a prefix for all functions. If mod_name is None then
a "_tvm" prefix is used.

I had to add two different compilation functions:
- _CompileEngineLowerWithModuleName to mangle all the operators with the mod_name
- PartitionGraphWithModName to mangle all the operators produced by BYOC

I could have changed signature of both, but that would have meant a very
invasive refactoring.

I refactored the aot test utils and added some tests for multiple
models.

Change-Id: I30e93fa075f660054577ea36cf9268ec0c6eebcb
@giuseros
Copy link
Contributor Author

Hi @areusch , @jroesch ,
I would say, let's get this merged and once #7518 lands, we can sync-up if we want to have the mangling as a pass or as a feature into the compiler. I wouldn't have any strong opinions (and honestly I like the idea of having it as a pass) but maybe @manupa-arm or @Mousius might.

Thanks,
Giuseppe

@giuseros
Copy link
Contributor Author

Hi @jroesch , @areusch ,
After further thinking we were wondering if we could merge both this PR and #8096 before merging #7518 . This is because #8096 would solve quite a few issues that we have with AoT at the moment.

What do you think?

Change-Id: I4f11da7fce1327ad89bb25f25209b57077b2c6a3
@giuseros
Copy link
Contributor Author

A friendly ping @manupa-arm , @jroesch , @areusch !

@areusch
Copy link
Contributor

areusch commented Jun 25, 2021

@jroesch can you comment on @giuseros question above?

Copy link
Contributor

@areusch areusch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @giuseros , i think we can merge this now. i'll review #8096 separately, but i think we are okay merging those before #7518 . depending on the complexity of the refactor, we might ask for your help migrating the logic to live in the post-TEcompiler architecture.

@areusch
Copy link
Contributor

areusch commented Jun 25, 2021

@manupa-arm @Mousius please take a look and explicitly approve if you're ok w/ this change

Copy link
Contributor

@manupak manupak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Member

@Mousius Mousius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And me 😸

@areusch areusch merged commit 36fc525 into apache:main Jun 28, 2021
Mousius added a commit to Mousius/tvm that referenced this pull request Jul 1, 2021
This introduces structures generated to provide a documented and stable user
friendly interface to a TVM generated model, as can be seen in the AOT
demo application:
```
struct tvmgen_default_inputs inputs = {
  .input_1 = input_data,
};
struct tvmgen_default_outputs outputs = {
  .output = output_data,
};
int ret_val = tvmgen_default_run(&inputs, &outputs, NULL, NULL);
```

To facilitate this, some other changes are included:
* Removed dependency on `aot_executor.{c,h}` in tests, pending the
discussion in the interface RFC as to whether we keep them.
* Moved creation of test DLTensor's into the AOT test utils, in future this
can be replaced by loading via the Python API or otherwise
* Introduce `parametrize_aot_options` which can be used to test
permutations of AOT which work together - for now this filters C
interface and packed operators
* Updated demo application to generate the header for demonstration
purposes, we should consider porting the demo application to Model
Library Format and using the toolchain in the Zephyr App via CMake
instead?

This patch builds upon the improvements @giuseros made to AOT testing
and name mangling from apache#8014
Mousius added a commit to Mousius/tvm that referenced this pull request Jul 2, 2021
This introduces structures generated to provide a documented and stable user
friendly interface to a TVM generated model, as can be seen in the AOT
demo application:
```
struct tvmgen_default_inputs inputs = {
  .input_1 = input_data,
};
struct tvmgen_default_outputs outputs = {
  .output = output_data,
};
int ret_val = tvmgen_default_run(&inputs, &outputs, NULL, NULL);
```

To facilitate this, some other changes are included:
* Removed dependency on `aot_executor.{c,h}` in tests, pending the
discussion in the interface RFC as to whether we keep them.
* Moved creation of test DLTensor's into the AOT test utils, in future this
can be replaced by loading via the Python API or otherwise
* Introduce `parametrize_aot_options` which can be used to test
permutations of AOT which work together - for now this filters C
interface and packed operators
* Updated demo application to generate the header for demonstration
purposes, we should consider porting the demo application to Model
Library Format and using the toolchain in the Zephyr App via CMake
instead?

This patch builds upon the improvements @giuseros made to AOT testing
and name mangling from apache#8014
Mousius added a commit to Mousius/tvm that referenced this pull request Jul 22, 2021
This introduces structures generated to provide a documented and stable user
friendly interface to a TVM generated model, as can be seen in the AOT
demo application:
```
struct tvmgen_default_inputs inputs = {
  .input_1 = input_data,
};
struct tvmgen_default_outputs outputs = {
  .output = output_data,
};
int ret_val = tvmgen_default_run(&inputs, &outputs, NULL, NULL);
```

To facilitate this, some other changes are included:
* Removed dependency on `aot_executor.{c,h}` in tests, pending the
discussion in the interface RFC as to whether we keep them.
* Moved creation of test DLTensor's into the AOT test utils, in future this
can be replaced by loading via the Python API or otherwise
* Introduce `parametrize_aot_options` which can be used to test
permutations of AOT which work together - for now this filters C
interface and packed operators
* Updated demo application to generate the header for demonstration
purposes, we should consider porting the demo application to Model
Library Format and using the toolchain in the Zephyr App via CMake
instead?

This patch builds upon the improvements @giuseros made to AOT testing
and name mangling from apache#8014
Mousius added a commit to Mousius/tvm that referenced this pull request Jul 26, 2021
This introduces structures generated to provide a documented and stable user
friendly interface to a TVM generated model, as can be seen in the AOT
demo application:
```
struct tvmgen_default_inputs inputs = {
  .input_1 = input_data,
};
struct tvmgen_default_outputs outputs = {
  .output = output_data,
};
int ret_val = tvmgen_default_run(&inputs, &outputs, NULL, NULL);
```

To facilitate this, some other changes are included:
* Removed dependency on `aot_executor.{c,h}` in tests, pending the
discussion in the interface RFC as to whether we keep them.
* Moved creation of test DLTensor's into the AOT test utils, in future this
can be replaced by loading via the Python API or otherwise
* Introduce `parametrize_aot_options` which can be used to test
permutations of AOT which work together - for now this filters C
interface and packed operators
* Updated demo application to generate the header for demonstration
purposes, we should consider porting the demo application to Model
Library Format and using the toolchain in the Zephyr App via CMake
instead?

This patch builds upon the improvements @giuseros made to AOT testing
and name mangling from apache#8014
Mousius added a commit to Mousius/tvm that referenced this pull request Jul 30, 2021
This introduces structures generated to provide a documented and stable user
friendly interface to a TVM generated model, as can be seen in the AOT
demo application:
```
struct tvmgen_default_inputs inputs = {
  .input_1 = input_data,
};
struct tvmgen_default_outputs outputs = {
  .output = output_data,
};
int ret_val = tvmgen_default_run(&inputs, &outputs, NULL, NULL);
```

To facilitate this, some other changes are included:
* Removed dependency on `aot_executor.{c,h}` in tests, pending the
discussion in the interface RFC as to whether we keep them.
* Moved creation of test DLTensor's into the AOT test utils, in future this
can be replaced by loading via the Python API or otherwise
* Introduce `parametrize_aot_options` which can be used to test
permutations of AOT which work together - for now this filters C
interface and packed operators
* Updated demo application to generate the header for demonstration
purposes, we should consider porting the demo application to Model
Library Format and using the toolchain in the Zephyr App via CMake
instead?

This patch builds upon the improvements @giuseros made to AOT testing
and name mangling from apache#8014
Mousius added a commit to Mousius/tvm that referenced this pull request Aug 1, 2021
This introduces structures generated to provide a documented and stable user
friendly interface to a TVM generated model, as can be seen in the AOT
demo application:
```
struct tvmgen_default_inputs inputs = {
  .input_1 = input_data,
};
struct tvmgen_default_outputs outputs = {
  .output = output_data,
};
int ret_val = tvmgen_default_run(&inputs, &outputs, NULL, NULL);
```

To facilitate this, some other changes are included:
* Removed dependency on `aot_executor.{c,h}` in tests, pending the
discussion in the interface RFC as to whether we keep them.
* Moved creation of test DLTensor's into the AOT test utils, in future this
can be replaced by loading via the Python API or otherwise
* Introduce `parametrize_aot_options` which can be used to test
permutations of AOT which work together - for now this filters C
interface and packed operators
* Updated demo application to generate the header for demonstration
purposes, we should consider porting the demo application to Model
Library Format and using the toolchain in the Zephyr App via CMake
instead?

This patch builds upon the improvements @giuseros made to AOT testing
and name mangling from apache#8014
areusch pushed a commit that referenced this pull request Aug 2, 2021
* Introduce --interface-api={c,packed} parameter

This introduces structures generated to provide a documented and stable user
friendly interface to a TVM generated model, as can be seen in the AOT
demo application:
```
struct tvmgen_default_inputs inputs = {
  .input_1 = input_data,
};
struct tvmgen_default_outputs outputs = {
  .output = output_data,
};
int ret_val = tvmgen_default_run(&inputs, &outputs, NULL, NULL);
```

To facilitate this, some other changes are included:
* Removed dependency on `aot_executor.{c,h}` in tests, pending the
discussion in the interface RFC as to whether we keep them.
* Moved creation of test DLTensor's into the AOT test utils, in future this
can be replaced by loading via the Python API or otherwise
* Introduce `parametrize_aot_options` which can be used to test
permutations of AOT which work together - for now this filters C
interface and packed operators
* Updated demo application to generate the header for demonstration
purposes, we should consider porting the demo application to Model
Library Format and using the toolchain in the Zephyr App via CMake
instead?

This patch builds upon the improvements @giuseros made to AOT testing
and name mangling from #8014

* Tweak metadata variable description and MLF target loop

* Remove direct usage of `relay::Var` in meta_data.h

This looks like the only place that could be causing the Windows CI failures, so trying removing the additional header in meta_data.h

* Linting fix

* Post-rebase files fixing

These tests were somehow transmuted in transit, I've updated them to the
most recent variant of the test helpers.

* Strip back interface API to just inputs and outputs

This removes any speculative structures from the generated code and cleans up some of the documentation.

* Add header guards and tweak documentation
ylc pushed a commit to ylc/tvm that referenced this pull request Sep 29, 2021
* [AOT] Name mangling in AOT

Mini-RFC is here: https://discuss.tvm.apache.org/t/mini-rfc-name-mangling-in-aot

With this change we'll mangle the name of global symbols so that we can bundle
together multiple models in the same application.

The relay.build interface has been left unchanged, which means I am
resuing mod_name as a prefix for all functions. If mod_name is None then
a "_tvm" prefix is used.

I had to add two different compilation functions:
- _CompileEngineLowerWithModuleName to mangle all the operators with the mod_name
- PartitionGraphWithModName to mangle all the operators produced by BYOC

I could have changed signature of both, but that would have meant a very
invasive refactoring.

I refactored the aot test utils and added some tests for multiple
models.

Change-Id: I30e93fa075f660054577ea36cf9268ec0c6eebcb

* retrigger CI

Change-Id: I4f11da7fce1327ad89bb25f25209b57077b2c6a3
ylc pushed a commit to ylc/tvm that referenced this pull request Sep 29, 2021
* Introduce --interface-api={c,packed} parameter

This introduces structures generated to provide a documented and stable user
friendly interface to a TVM generated model, as can be seen in the AOT
demo application:
```
struct tvmgen_default_inputs inputs = {
  .input_1 = input_data,
};
struct tvmgen_default_outputs outputs = {
  .output = output_data,
};
int ret_val = tvmgen_default_run(&inputs, &outputs, NULL, NULL);
```

To facilitate this, some other changes are included:
* Removed dependency on `aot_executor.{c,h}` in tests, pending the
discussion in the interface RFC as to whether we keep them.
* Moved creation of test DLTensor's into the AOT test utils, in future this
can be replaced by loading via the Python API or otherwise
* Introduce `parametrize_aot_options` which can be used to test
permutations of AOT which work together - for now this filters C
interface and packed operators
* Updated demo application to generate the header for demonstration
purposes, we should consider porting the demo application to Model
Library Format and using the toolchain in the Zephyr App via CMake
instead?

This patch builds upon the improvements @giuseros made to AOT testing
and name mangling from apache#8014

* Tweak metadata variable description and MLF target loop

* Remove direct usage of `relay::Var` in meta_data.h

This looks like the only place that could be causing the Windows CI failures, so trying removing the additional header in meta_data.h

* Linting fix

* Post-rebase files fixing

These tests were somehow transmuted in transit, I've updated them to the
most recent variant of the test helpers.

* Strip back interface API to just inputs and outputs

This removes any speculative structures from the generated code and cleans up some of the documentation.

* Add header guards and tweak documentation
ylc pushed a commit to ylc/tvm that referenced this pull request Jan 13, 2022
* Introduce --interface-api={c,packed} parameter

This introduces structures generated to provide a documented and stable user
friendly interface to a TVM generated model, as can be seen in the AOT
demo application:
```
struct tvmgen_default_inputs inputs = {
  .input_1 = input_data,
};
struct tvmgen_default_outputs outputs = {
  .output = output_data,
};
int ret_val = tvmgen_default_run(&inputs, &outputs, NULL, NULL);
```

To facilitate this, some other changes are included:
* Removed dependency on `aot_executor.{c,h}` in tests, pending the
discussion in the interface RFC as to whether we keep them.
* Moved creation of test DLTensor's into the AOT test utils, in future this
can be replaced by loading via the Python API or otherwise
* Introduce `parametrize_aot_options` which can be used to test
permutations of AOT which work together - for now this filters C
interface and packed operators
* Updated demo application to generate the header for demonstration
purposes, we should consider porting the demo application to Model
Library Format and using the toolchain in the Zephyr App via CMake
instead?

This patch builds upon the improvements @giuseros made to AOT testing
and name mangling from apache#8014

* Tweak metadata variable description and MLF target loop

* Remove direct usage of `relay::Var` in meta_data.h

This looks like the only place that could be causing the Windows CI failures, so trying removing the additional header in meta_data.h

* Linting fix

* Post-rebase files fixing

These tests were somehow transmuted in transit, I've updated them to the
most recent variant of the test helpers.

* Strip back interface API to just inputs and outputs

This removes any speculative structures from the generated code and cleans up some of the documentation.

* Add header guards and tweak documentation
zxy844288792 pushed a commit to zxy844288792/tvm that referenced this pull request Mar 4, 2022
* [AOT] Name mangling in AOT

Mini-RFC is here: https://discuss.tvm.apache.org/t/mini-rfc-name-mangling-in-aot

With this change we'll mangle the name of global symbols so that we can bundle
together multiple models in the same application.

The relay.build interface has been left unchanged, which means I am
resuing mod_name as a prefix for all functions. If mod_name is None then
a "_tvm" prefix is used.

I had to add two different compilation functions:
- _CompileEngineLowerWithModuleName to mangle all the operators with the mod_name
- PartitionGraphWithModName to mangle all the operators produced by BYOC

I could have changed signature of both, but that would have meant a very
invasive refactoring.

I refactored the aot test utils and added some tests for multiple
models.

Change-Id: I30e93fa075f660054577ea36cf9268ec0c6eebcb

* retrigger CI

Change-Id: I4f11da7fce1327ad89bb25f25209b57077b2c6a3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants