From 41617ac7c3bde9ddd661615260a7dd29ce14b1ef Mon Sep 17 00:00:00 2001 From: onkar-sima-ai Date: Mon, 8 Nov 2021 09:58:12 +0000 Subject: [PATCH 1/3] added tests for quantized tflite sin operator --- python/tvm/relay/frontend/tflite.py | 2 - tests/python/frontend/tflite/test_forward.py | 44 ++++++++++++++++++-- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/python/tvm/relay/frontend/tflite.py b/python/tvm/relay/frontend/tflite.py index 05b3041320c9..12beca5e898a 100644 --- a/python/tvm/relay/frontend/tflite.py +++ b/python/tvm/relay/frontend/tflite.py @@ -1174,8 +1174,6 @@ def convert_log(self, op): def convert_sin(self, op): """Convert TFLite SIN""" - if self.is_quantized(op): - raise tvm.error.OpNotImplemented("TFlite quantized SIN operator is not supported yet.") return self._convert_unary_elemwise(_op.sin, op) def convert_tan(self, op): diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index cb9122bfdc83..15704ebeb0a0 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -1825,10 +1825,38 @@ def _test_log(data): # --- -def _test_sin(data): +def _test_sin(data, quantized=False): """One iteration of sin""" - return _test_unary_elemwise(math_ops.sin, data) + with tf.Graph().as_default(): + in_data = array_ops.placeholder(shape=data.shape, dtype="float32", name="in_0") + if quantized: + inq_data = tf.quantization.fake_quant_with_min_max_args( + in_data, min=1, max=6, name="inq_0" + ) + input_range = {"inq_0": (1, 6)} + out = math_ops.sin(inq_data) + out = tf.quantization.fake_quant_with_min_max_args(out, min=1, max=6, name="out") + compare_tflite_with_tvm( + data, + "inq_0:0", + [inq_data], + [out], + quantized=True, + input_range=input_range, + experimental_new_converter=True, + ) + else: + out = math_ops.sin(in_data) + compare_tflite_with_tvm(data, "in_0:0", [in_data], [out]) + + +def test_forward_sin(): + """SIN""" + _test_sin(np.arange(-2.0, 4.0, dtype=np.float32), quantized=False) + _test_sin(np.arange(-2.0, 4.0, dtype=np.float32).reshape((2, 1, 3)), quantized=False) + _test_sin(np.arange(1, 240, 40, dtype=np.uint8), quantized=True) + _test_sin(np.arange(1, 240, 40, dtype=np.uint8).reshape((2, 1, 3)), quantized=True) ####################################################################### # Cos @@ -1850,6 +1878,16 @@ def _test_tan(data): return _test_unary_elemwise(math_ops.tan, data) +####################################################################### +# Sqrt +# ---- + + +def _test_sqrt(data): + """One iteration of sqrt""" + return _test_unary_elemwise(math_ops.sqrt, data) + + ####################################################################### # Square # ------ @@ -1882,7 +1920,6 @@ def test_all_unary_elemwise(): _test_forward_unary_elemwise(_test_floor) _test_forward_unary_elemwise(_test_exp) _test_forward_unary_elemwise(_test_log) - _test_forward_unary_elemwise(_test_sin) _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"): @@ -4783,6 +4820,7 @@ def test_prevent_tensorflow_dynamic_range(): test_forward_tanh() test_forward_rsqrt() test_forward_neg() + test_forward_sin() test_forward_abs() test_forward_sqrt() test_forward_relu() From 4ae9db4030f6d558bc664e06e43c130ab964ca8d Mon Sep 17 00:00:00 2001 From: Onkar Chougule Date: Tue, 9 Nov 2021 22:42:57 +0530 Subject: [PATCH 2/3] removing unnecessary rsqrt code_block --- tests/python/frontend/tflite/test_forward.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 15704ebeb0a0..34c112624015 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -1878,16 +1878,6 @@ def _test_tan(data): return _test_unary_elemwise(math_ops.tan, data) -####################################################################### -# Sqrt -# ---- - - -def _test_sqrt(data): - """One iteration of sqrt""" - return _test_unary_elemwise(math_ops.sqrt, data) - - ####################################################################### # Square # ------ From 15ac6e519099deea8d3c8fd346285a483799e581 Mon Sep 17 00:00:00 2001 From: Onkar Chougule Date: Tue, 9 Nov 2021 23:15:02 +0530 Subject: [PATCH 3/3] resolving linting error --- tests/python/frontend/tflite/test_forward.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 34c112624015..956c71a7511c 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -1858,6 +1858,7 @@ def test_forward_sin(): _test_sin(np.arange(1, 240, 40, dtype=np.uint8), quantized=True) _test_sin(np.arange(1, 240, 40, dtype=np.uint8).reshape((2, 1, 3)), quantized=True) + ####################################################################### # Cos # ---