Skip to content

Commit

Permalink
[Frontend][TFLite] Add parser support for 'square' operator (apache#4915
Browse files Browse the repository at this point in the history
)

* [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
  • Loading branch information
inadob authored and alexwong committed Feb 29, 2020
1 parent d82a642 commit ada010c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
29 changes: 28 additions & 1 deletion python/tvm/relay/frontend/tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ def __init__(self, model, subgraph, exp_tab):
'SQUARED_DIFFERENCE': self.convert_squared_difference,
'LOGICAL_AND': self.convert_logical_and,
'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):
Expand Down Expand Up @@ -636,6 +637,32 @@ def convert_neg(self, op):
'TFlite quantized NEG operator is not supported yet.')
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):
"""Generic method to Convert TFLite elemwise"""
try:
Expand Down
7 changes: 7 additions & 0 deletions tests/python/frontend/tflite/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,12 @@ def _test_neg(data):
""" One iteration of neg """
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):
# functions that need positive input
Expand All @@ -759,6 +765,7 @@ def test_all_unary_elemwise():
_test_forward_unary_elemwise(_test_sqrt)
_test_forward_unary_elemwise(_test_rsqrt)
_test_forward_unary_elemwise(_test_neg)
_test_forward_unary_elemwise(_test_square)
# ceil and cos come with TFLite 1.14.0.post1 fbs schema
if package_version.parse(tf.VERSION) >= package_version.parse('1.14.0'):
_test_forward_unary_elemwise(_test_ceil)
Expand Down

0 comments on commit ada010c

Please sign in to comment.