-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[TFLite] Add option to overwrite OperatorConverter class in relay.frontend.from_tflite #9256
[TFLite] Add option to overwrite OperatorConverter class in relay.frontend.from_tflite #9256
Conversation
@PhilippvK can you retrigger the CI? |
Yeah PyTorch frontend also has something similar tvm/python/tvm/relay/frontend/pytorch.py Lines 3288 to 3289 in 2b57907
|
@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
Thanks @PhilippvK |
…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.
…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.
Motivation
Having an ability to overwrite the mapping from TFLite Operators to TVM Relay Operators via downstream python scripts is desirable to:
Changes
op_converter
totvm.relay.frontend.from_tflite
function, defaulting torelay.frontend.tflite.OperatorConverter
allow_custom_ops
flag toOperatorConverter
, which allows to manually add convert function forCUSTOM
opertator typesTests
I also came up with a test case added to
tvm/python/frontend/tflite/test_forward.py
.