Unverified Commit 8e7e7792 by Ina Dobreva Committed by GitHub

[Frontend][TFLite] Add parser support for 'square' operator (#4915)

* [Frontend][TFLite] Add parser support for square operator

* Add parser implementation
* Add relevant tests
* Note: 'square' is an unary elemwise operator but it's added separately
  in the parser since there is no Relay 'square' op
  and instead we have to use 'multiply'

* Change relay operation from 'multiply' to 'power'

* Remove a redundant line as requested
parent 502ab605
...@@ -121,7 +121,8 @@ class OperatorConverter(object): ...@@ -121,7 +121,8 @@ class OperatorConverter(object):
'SQUARED_DIFFERENCE': self.convert_squared_difference, 'SQUARED_DIFFERENCE': self.convert_squared_difference,
'LOGICAL_AND': self.convert_logical_and, 'LOGICAL_AND': self.convert_logical_and,
'LOGICAL_OR': self.convert_logical_or, 'LOGICAL_OR': self.convert_logical_or,
'DETECTION_POSTPROCESS': self.convert_detection_postprocess 'DETECTION_POSTPROCESS': self.convert_detection_postprocess,
'SQUARE': self.convert_square,
} }
def check_unsupported_ops(self): def check_unsupported_ops(self):
...@@ -636,6 +637,32 @@ class OperatorConverter(object): ...@@ -636,6 +637,32 @@ class OperatorConverter(object):
'TFlite quantized NEG operator is not supported yet.') 'TFlite quantized NEG operator is not supported yet.')
return self._convert_unary_elemwise(_op.negative, op) return self._convert_unary_elemwise(_op.negative, op)
def convert_square(self, op):
"""Convert TFLite SQUARE"""
try:
from tflite.Operator import Operator
except ImportError:
raise ImportError("The tflite package must be installed")
assert isinstance(op, Operator)
input_tensors = self.get_input_tensors(op)
assert len(input_tensors) == 1, "input tensors length should be 1"
input_tensor = input_tensors[0]
in_expr = self.get_expr(input_tensor.tensor_idx)
output_tensors = self.get_output_tensors(op)
assert len(output_tensors) == 1, "output tensors length should be 1"
output_tensor = output_tensors[0]
if self.is_quantized(op):
raise tvm.error.OpNotImplemented(
'TFlite quantized SQUARE operator is not supported yet.')
exp_type = self.get_tensor_type_str(output_tensor.tensor.Type())
out = _op.power(in_expr, relay.const(2, exp_type))
return out
def _convert_elemwise(self, relay_op, op): def _convert_elemwise(self, relay_op, op):
"""Generic method to Convert TFLite elemwise""" """Generic method to Convert TFLite elemwise"""
try: try:
......
...@@ -742,6 +742,12 @@ def _test_neg(data): ...@@ -742,6 +742,12 @@ def _test_neg(data):
""" One iteration of neg """ """ One iteration of neg """
return _test_unary_elemwise(math_ops.neg, data) return _test_unary_elemwise(math_ops.neg, data)
####################################################################### #######################################################################
# Square
# ------
def _test_square(data):
""" One iteration of square """
return _test_unary_elemwise(math_ops.square, data)
def _test_forward_unary_elemwise(test_op): def _test_forward_unary_elemwise(test_op):
# functions that need positive input # functions that need positive input
...@@ -759,6 +765,7 @@ def test_all_unary_elemwise(): ...@@ -759,6 +765,7 @@ def test_all_unary_elemwise():
_test_forward_unary_elemwise(_test_sqrt) _test_forward_unary_elemwise(_test_sqrt)
_test_forward_unary_elemwise(_test_rsqrt) _test_forward_unary_elemwise(_test_rsqrt)
_test_forward_unary_elemwise(_test_neg) _test_forward_unary_elemwise(_test_neg)
_test_forward_unary_elemwise(_test_square)
# ceil and cos come with TFLite 1.14.0.post1 fbs schema # ceil and cos come with TFLite 1.14.0.post1 fbs schema
if package_version.parse(tf.VERSION) >= package_version.parse('1.14.0'): if package_version.parse(tf.VERSION) >= package_version.parse('1.14.0'):
_test_forward_unary_elemwise(_test_ceil) _test_forward_unary_elemwise(_test_ceil)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment