Skip to content

Commit

Permalink
Add parser support for CAST tflite operator
Browse files Browse the repository at this point in the history
This implementation provides cast to limited number of dtypes that tflite
currently supports for placeholder op. The encoding values for
placeholder dtypes are taken from the tflite flatbuff schema.
  • Loading branch information
inadob committed Oct 10, 2019
1 parent 85a1d3f commit 122a1bc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
33 changes: 33 additions & 0 deletions python/tvm/relay/frontend/tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def __init__(self, model, subgraph, exp_tab):
'RELU':self.convert_relu,
'SPLIT': self.convert_split,
'TRANSPOSE': self.convert_transpose,
'CAST': self.convert_cast,
'TILE': self.convert_tile,
'BATCH_TO_SPACE_ND': self.convert_batch_to_space_nd,
'SPACE_TO_BATCH_ND': self.convert_space_to_batch_nd
Expand Down Expand Up @@ -820,6 +821,38 @@ def convert_transpose(self, op):

return out

def convert_cast(self, op):
"""Convert TFLite CAST"""
try:
from tflite.Operator import Operator
from tflite.BuiltinOptions import BuiltinOptions
from tflite.CastOptions import CastOptions
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)

assert op.BuiltinOptionsType() == BuiltinOptions.CastOptions
op_options = op.BuiltinOptions()
cast_options = CastOptions()
cast_options.Init(op_options.Bytes, op_options.Pos)
cast_dtype = cast_options.OutDataType()

# a dict to decode the enumeriton of tensor dtypes as in the tflite fb schema
type_decoder = {
0: 'float32',
2: 'int32',
3: 'uint8',
4: 'int64'
}
out = _op.cast(in_expr, type_decoder[cast_dtype])

return out

def convert_tile(self, op):
"""tile implementation."""
try:
Expand Down
21 changes: 21 additions & 0 deletions tests/python/frontend/tflite/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,24 @@ def test_forward_transpose():
_test_forward_transpose((2, 3, 4, 5), (3, 0, 1, 2))
_test_forward_transpose((2, 3, 4, 5), ())

#######################################################################
# Cast
# --------

def _test_cast(data, cast_dtype):
""" One iteration of CAST """
with tf.Graph().as_default():
in_data = array_ops.placeholder(shape=data.shape, dtype=data.dtype)
out = math_ops.cast(in_data, cast_dtype)
compare_tflite_with_tvm(data, 'Placeholder:0', [in_data], [out])


def test_forward_cast():
""" CAST """
_test_cast(np.arange(6.0, dtype=np.float32).reshape((1, 6)), cast_dtype=tf.int32)
_test_cast(np.arange(6.0, dtype=np.float32).reshape((1, 6)), cast_dtype=tf.uint8)
_test_cast(np.arange(6.0, dtype=np.int64).reshape((1, 6)), cast_dtype=tf.int32)

#######################################################################
# tile
# ---------
Expand Down Expand Up @@ -997,6 +1015,9 @@ def test_forward_ssd_mobilenet_v1():
# Transpose
test_forward_transpose()

# Cast
test_forward_cast()

# Tile
test_forward_tile()

Expand Down

0 comments on commit 122a1bc

Please sign in to comment.