u99127 commented on a change in pull request #4788: [FRONTEND][TFLITE]Gather,
StridedSlice op support added
URL: https://github.com/apache/incubator-tvm/pull/4788#discussion_r381991135
##########
File path: python/tvm/relay/frontend/tflite.py
##########
@@ -852,6 +855,193 @@ def convert_logical_or(self, op):
"""Convert tflite LOGICAL_OR"""
return self._convert_logical_binary(_op.logical_or, op)
+ def convert_gather(self, op):
+ """Method to Convert TFLite GATHER operator"""
+ try:
+ from tflite.BuiltinOptions import BuiltinOptions
+ from tflite.GatherOptions import GatherOptions
+ from tflite.TensorType import TensorType
+ except ImportError:
+ raise ImportError("The tflite package must be installed")
+
+ input_tensors = self.get_input_tensors(op)
+ assert len(input_tensors) == 2, "input tensors length should be 2"
+
+ data = self.get_expr(input_tensors[0].tensor_idx)
+
+ indices = input_tensors[1]
+ indices_type = indices.tensor.Type()
+ assert indices_type in (TensorType.INT32, TensorType.INT64)
+ indices_type_str = self.get_tensor_type_str(indices_type)
+ indices = self.exp_tab.new_const(self.get_tensor_value(indices),
+ dtype=indices_type_str)
+
+ assert op.BuiltinOptionsType() == BuiltinOptions.GatherOptions
+ op_options = op.BuiltinOptions()
+ gather_options = GatherOptions()
+ gather_options.Init(op_options.Bytes, op_options.Pos)
+ axis = gather_options.Axis()
+
+ # Check the indices are oob, tflite is unpredictable in case of oob.
+ data_shape = list(input_tensors[0].tensor.ShapeAsNumpy())
+ data_dim = len(data_shape)
+
+ axis_n = axis
+ if axis_n < 0:
+ axis_n += axis_n + data_dim
+ assert axis_n >= 0, "Axis out of bounds"
+ assert axis_n < data_dim, "Axis out of bounds"
+
+ indices_val = self.get_tensor_value(input_tensors[1])
+ indices_shape = list(indices_val.shape)
+ indices_len = len(indices_shape)
+
+ out_shape = []
+ for i in range(data_dim):
+ if axis_n == i:
+ for j in range(indices_len):
+ out_shape.append(indices_shape[j])
+ else:
+ out_shape.append(data_shape[i])
+
+ loopover = [range(s) for s in out_shape]
+ for idx in list(itertools.product(*loopover)):
+ indices_position = [idx[j] for j in range(axis_n,
axis_n+indices_len)]
+
+ real_indices = [idx[j] for j in range(axis_n)]
+ real_indices.append(indices_val[tuple(indices_position)])
+ real_indices.extend([idx[j] for j in range(axis_n + indices_len,
len(idx))])
+ for r, d in zip(real_indices, data_shape):
+ if r >= d:
+ raise ValueError("TFLite out of bound indices are not
supported.")
+
+ # Use mode as fast since already checked for oob.
+ out = _op.take(data, indices, axis=axis, mode="fast")
+ return out
+
+ def convert_strided_slice(self, op):
+ """Method to Convert TFLite STRIDED_SLICE operator.
+ Note: Eventhough tf2.0 supports begin_mask, end_mask,
ellipsis_mask, new_axis_mask
+ and shrink_axis_mask, tflite doesn't support these and expect these
values to be zero.
+ But in future, they may open up the mask implementation, so kept
the implementation
+ same as tensorflow.
+ TVM Relay implementation of doesn't support mask, so the mask
values are processed here
Review comment:
relay.op.strided_slice ?
Instead of "similar to tensorflow parsing" a bit more context would be
better here about what is actually being done because it is not clear what
exactly you refer to as there are no comments next to op.strided_slice in
tensorflow.py ;)
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services