[TFLite] Add option to overwrite OperatorConverter class in relay.frontend.from_tflite#9256
Merged
masahi merged 2 commits intoapache:mainfrom Oct 14, 2021
Merged
Conversation
Contributor
|
@PhilippvK can you retrigger the CI? |
Member
|
Yeah PyTorch frontend also has something similar tvm/python/tvm/relay/frontend/pytorch.py Lines 3288 to 3289 in 2b57907 |
Contributor
Author
|
@masahi I first tried to follow the same approach as in the PyTorch frontend, just passing a dict to overwrite the convert map. However in the TFLite frontend, the |
This allows to overwrite the mapping from TFLite Operators to TVM Relay Operators from external python scripts. This has the following advantages:
- Adding support for unsupported builtin or even custom operators by adding a hand-written convert function
- Enables overwriting of existing convert functions for supported operators by alternative implementations (useful for currently unsupported edge cases)
Example Usage:
```
class CustomOperatorConverter(relay.frontend.tflite.OperatorConverter):
def __init__(self, model, subgraph, exp_tab):
super(CustomOperatorConverter, self).__init__(model, subgraph, exp_tab)
convert_map_overwrite = {"SUB": self.convert_sub_custom}
self.convert_map.update(convert_map_overwrite)
def convert_sub_custom(self, op):
...
...
relay_mod = relay.frontend.from_tflite(
tflite_model, shape_dict=shape_dict, dtype_dict=dtype_dict, op_converter=CustomOperatorConverter
)
```
[TFLite] Make sure that even DETECTION_POSTPROCESS op can be overwritten
This is desirable, because the current implementation of this CUSTOM op is incompatible with MicroTVM targets
…rontend Kept the test as simple as possible by only comparing 2 different implementations of a SUB TFLite operator: 1. Original: c = a - b 2. Dummy: c = a + (-b) Comparison with TFLite reference output is not necessary because tis is already covered by other test cases. Instead comparisons of the two TVM models are used.
10f51a6 to
b354b58
Compare
masahi
approved these changes
Oct 14, 2021
Member
|
Thanks @PhilippvK |
ylc
pushed a commit
to ylc/tvm
that referenced
this pull request
Jan 7, 2022
…ntend.from_tflite (apache#9256) * [TFLite] Relay Frontend: Add option to overwrite OperatorConverter class This allows to overwrite the mapping from TFLite Operators to TVM Relay Operators from external python scripts. This has the following advantages: - Adding support for unsupported builtin or even custom operators by adding a hand-written convert function - Enables overwriting of existing convert functions for supported operators by alternative implementations (useful for currently unsupported edge cases) Example Usage: ``` class CustomOperatorConverter(relay.frontend.tflite.OperatorConverter): def __init__(self, model, subgraph, exp_tab): super(CustomOperatorConverter, self).__init__(model, subgraph, exp_tab) convert_map_overwrite = {"SUB": self.convert_sub_custom} self.convert_map.update(convert_map_overwrite) def convert_sub_custom(self, op): ... ... relay_mod = relay.frontend.from_tflite( tflite_model, shape_dict=shape_dict, dtype_dict=dtype_dict, op_converter=CustomOperatorConverter ) ``` [TFLite] Make sure that even DETECTION_POSTPROCESS op can be overwritten This is desirable, because the current implementation of this CUSTOM op is incompatible with MicroTVM targets * Tests: added test case for overwriting op_converter in TFLite relay frontend Kept the test as simple as possible by only comparing 2 different implementations of a SUB TFLite operator: 1. Original: c = a - b 2. Dummy: c = a + (-b) Comparison with TFLite reference output is not necessary because tis is already covered by other test cases. Instead comparisons of the two TVM models are used.
ylc
pushed a commit
to ylc/tvm
that referenced
this pull request
Jan 13, 2022
…ntend.from_tflite (apache#9256) * [TFLite] Relay Frontend: Add option to overwrite OperatorConverter class This allows to overwrite the mapping from TFLite Operators to TVM Relay Operators from external python scripts. This has the following advantages: - Adding support for unsupported builtin or even custom operators by adding a hand-written convert function - Enables overwriting of existing convert functions for supported operators by alternative implementations (useful for currently unsupported edge cases) Example Usage: ``` class CustomOperatorConverter(relay.frontend.tflite.OperatorConverter): def __init__(self, model, subgraph, exp_tab): super(CustomOperatorConverter, self).__init__(model, subgraph, exp_tab) convert_map_overwrite = {"SUB": self.convert_sub_custom} self.convert_map.update(convert_map_overwrite) def convert_sub_custom(self, op): ... ... relay_mod = relay.frontend.from_tflite( tflite_model, shape_dict=shape_dict, dtype_dict=dtype_dict, op_converter=CustomOperatorConverter ) ``` [TFLite] Make sure that even DETECTION_POSTPROCESS op can be overwritten This is desirable, because the current implementation of this CUSTOM op is incompatible with MicroTVM targets * Tests: added test case for overwriting op_converter in TFLite relay frontend Kept the test as simple as possible by only comparing 2 different implementations of a SUB TFLite operator: 1. Original: c = a - b 2. Dummy: c = a + (-b) Comparison with TFLite reference output is not necessary because tis is already covered by other test cases. Instead comparisons of the two TVM models are used.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
Having an ability to overwrite the mapping from TFLite Operators to TVM Relay Operators via downstream python scripts is desirable to:
Changes
op_convertertotvm.relay.frontend.from_tflitefunction, defaulting torelay.frontend.tflite.OperatorConverterallow_custom_opsflag toOperatorConverter, which allows to manually add convert function forCUSTOMopertator typesTests
I also came up with a test case added to
tvm/python/frontend/tflite/test_forward.py.