Skip to content

Commit

Permalink
Add parses support for zeros_like tflite operator (#4042)
Browse files Browse the repository at this point in the history
The tensorflow zeros_like operation provided in array_ops.py produces directly a tensor with zeros
without a graph, using only the shape and type of the input. This imposes the use of gen_array_ops.py
that produces both a tensor and a graph so a comparison between tflite and tvm can be done.
  • Loading branch information
inadob authored and kevinthesun committed Oct 6, 2019
1 parent 3d9b173 commit 260d660
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions python/tvm/relay/frontend/tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def __init__(self, model, subgraph, exp_tab):
'MAXIMUM': self.convert_maximum,
'MINIMUM': self.convert_minimum,
'GREATER': self.convert_greater,
'ZEROS_LIKE': self.convert_zeros_like,
'REDUCE_MIN': self._convert_reduce_min,
'REDUCE_MAX': self._convert_reduce_max,
'MEAN': self._convert_reduce_mean,
Expand Down Expand Up @@ -478,6 +479,23 @@ def convert_minimum(self, op):
def convert_greater(self, op):
return self._convert_elemwise(_op.greater, op)

def convert_zeros_like(self, op):
"""Convert TFLite ZEROS LIKE"""
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)
out = _op.zeros_like(in_expr)

return out

def _convert_reduce(self, relay_op, op):
"""Generic method to Convert TFLite MEAN operators"""
try:
Expand Down
19 changes: 19 additions & 0 deletions tests/python/frontend/tflite/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import nn_ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import gen_array_ops
from tensorflow.python.ops import variables
try:
from tensorflow import lite as interpreter_wrapper
Expand Down Expand Up @@ -632,6 +633,21 @@ def test_all_elemwise():
_test_forward_elemwise(_test_minimum)
_test_forward_elemwise(_test_greater)

#######################################################################
# Zeros like
# --------

def _test_zeros_like(data):
""" One iteration of ZEROS LIKE """
with tf.Graph().as_default():
in_data = array_ops.placeholder(shape=data.shape, dtype=data.dtype)
out = gen_array_ops.zeros_like(in_data)
compare_tflite_with_tvm(data, 'Placeholder:0', [in_data], [out])

def test_forward_zeros_like():
""" ZEROS LIKE """
_test_zeros_like(np.arange(6.0, dtype=np.float32).reshape((1, 6)))

#######################################################################
# Reduce
# ------
Expand Down Expand Up @@ -1020,6 +1036,9 @@ def test_forward_ssd_mobilenet_v1():
# Elemwise
test_all_elemwise()

# Zeros Like
test_forward_zeros_like()

# Reduce
test_all_reduce()

Expand Down

0 comments on commit 260d660

Please sign in to comment.