[GitHub] [incubator-tvm] wyc-ruiker commented on a change in pull request #4788: [FRONTEND][TFLITE]Gather, StridedSlice op support added

2020-02-04 Thread GitBox
wyc-ruiker 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_r374744847
 
 

 ##
 File path: tests/python/frontend/tflite/test_forward.py
 ##
 @@ -244,6 +244,74 @@ def test_forward_slice():
 _test_slice(np.arange(8, dtype=np.int32).reshape((2, 4)), begin=[0, 
1], size=[-1, -1])
 _test_slice(np.arange(5, dtype=np.int32).reshape((5, )), begin=[4], 
size=[-1])
 
+###
+# Gather
+# --
+
+def _test_gather(dshape, indices, axis, dtype):
+""" One iteration of Gather """
+data = np.random.uniform(1, 10, size=dshape).astype(dtype)
+indices = np.asarray(indices).astype('int32')
+
+with tf.Graph().as_default():
+in_data = array_ops.placeholder(shape=data.shape, dtype=data.dtype)
+out = array_ops.gather(in_data, indices, axis=axis)
+compare_tflite_with_tvm(data, 'Placeholder:0', [in_data], [out])
+
+#Test quantized input
+data = np.random.uniform(1, 10, size=dshape).astype(np.uint8)
+with tf.Graph().as_default():
+in_data = array_ops.placeholder(shape=data.shape, dtype=data.dtype, 
name="in_data")
+out = array_ops.gather(in_data, indices, axis=axis)
+compare_tflite_with_tvm([data], ['in_data:0'], [in_data], [out], 
quantized=True)
+
+def test_forward_gather():
+""" GATHER """
+_test_gather((4,), [1], 0, 'float32')
+_test_gather((1, 4), [0], 0, 'int32')
+_test_gather((4,), [[[1, 0], [0, 1]]], 0, 'float32')
+_test_gather((2, 2), [[[1, 0], [0, 1]]], 0, 'int32')
+_test_gather((2, 2), [[[1, 0], [0, 1]]], 1, 'int32')
+_test_gather((2, 2), [[[1, 0], [0, 1]]], 0, 'float32')
+_test_gather((3, 3, 3),  [[[1, 0]]], 0, 'int32')
+_test_gather((3, 3, 3), [[[1, 0]]], 2, 'int32')
+_test_gather((4, 3, 5, 6),  [[2, 1, 0, 0]], 0, 'float32')
+
+###
+# StridedSlice
+# 
+
+def _test_stridedslice(ip_shape, begin, end, stride, dtype,
+   begin_mask=0, end_mask=0, new_axis_mask=0,
+   shrink_axis_mask=0, ellipsis_mask=0):
+""" One iteration of a Stridedslice """
+data = np.random.uniform(size=ip_shape).astype(dtype)
+with tf.Graph().as_default():
+in_data = tf.placeholder(dtype, ip_shape, name="in_data")
+out = array_ops.strided_slice(in_data, begin, end, stride,
+  begin_mask=begin_mask,
+  end_mask=end_mask, 
new_axis_mask=new_axis_mask,
+  shrink_axis_mask=shrink_axis_mask,
+  ellipsis_mask=ellipsis_mask)
+compare_tflite_with_tvm(data, 'in_data:0', [in_data], [out])
+
+#Test with quantized inputs
+data = np.random.uniform(size=ip_shape).astype(np.uint8)
+with tf.Graph().as_default():
+in_data = tf.placeholder(dtype, ip_shape, name="in_data")
+out = array_ops.strided_slice(in_data, begin, end, stride,
+  begin_mask=begin_mask,
+  end_mask=end_mask, 
new_axis_mask=new_axis_mask,
+  shrink_axis_mask=shrink_axis_mask,
+  ellipsis_mask=ellipsis_mask)
+compare_tflite_with_tvm([data], ['in_data:0'], [in_data], [out], 
quantized=True)
+
+def test_forward_stridedslice():
+'''test StridedSlice'''
 
 Review comment:
   Is it necessary to move [all these 
tests](https://github.com/apache/incubator-tvm/blob/master/tests/python/frontend/tensorflow/test_forward.py#L1146)
  here? These unit tests don't cover all situations.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-tvm] wyc-ruiker commented on a change in pull request #4788: [FRONTEND][TFLITE]Gather, StridedSlice op support added

2020-02-04 Thread GitBox
wyc-ruiker 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_r374734446
 
 

 ##
 File path: python/tvm/relay/frontend/tflite.py
 ##
 @@ -792,6 +794,147 @@ def convert_not_equal(self, op):
 'TFlite quantized NOT_EQUAL operator is not supported yet.')
 return self._convert_elemwise(_op.not_equal, 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)
+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()
+
+out = _op.take(data, indices, axis=axis)
+return out
+
+def convert_strided_slice(self, op):
+"""Method to Convert TFLite STRIDED_SLICE operator"""
+try:
+from tflite.BuiltinOptions import BuiltinOptions
+from tflite.StridedSliceOptions import StridedSliceOptions
+except ImportError:
+raise ImportError("The tflite package must be installed")
+
+input_tensors = self.get_input_tensors(op)
 
 Review comment:
   We also need an assert here.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-tvm] wyc-ruiker commented on a change in pull request #4788: [FRONTEND][TFLITE]Gather, StridedSlice op support added

2020-02-04 Thread GitBox
wyc-ruiker 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_r374733608
 
 

 ##
 File path: python/tvm/relay/frontend/tflite.py
 ##
 @@ -792,6 +794,147 @@ def convert_not_equal(self, op):
 'TFlite quantized NOT_EQUAL operator is not supported yet.')
 return self._convert_elemwise(_op.not_equal, 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)
 
 Review comment:
   We need an assert here like `assert len(input_tensors) == 2, "input tensors 
length should be 2"`


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:
us...@infra.apache.org


With regards,
Apache Git Services