From a5d8d00ed691ce6f58571abdb78a8c6afdeef7ff Mon Sep 17 00:00:00 2001 From: Tlopex <68688494+tlopex@users.noreply.github.com> Date: Thu, 12 Oct 2023 01:16:09 +0800 Subject: [PATCH 1/9] add quantized reverse_sequence --- python/tvm/relay/frontend/tflite.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/python/tvm/relay/frontend/tflite.py b/python/tvm/relay/frontend/tflite.py index 532318b804da..043da8704221 100644 --- a/python/tvm/relay/frontend/tflite.py +++ b/python/tvm/relay/frontend/tflite.py @@ -2432,11 +2432,6 @@ def convert_reverse_sequence(self, op): except ImportError: raise ImportError("The tflite package must be installed") - if self.is_quantized(op): - raise tvm.error.OpNotImplemented( - "TFLite does not support quantized REVERSE_SEQUENCE operator yet." - ) - input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" From 19b9d136ee1fde26db1bd4b2ce8d4e28f54563d5 Mon Sep 17 00:00:00 2001 From: Tlopex <68688494+tlopex@users.noreply.github.com> Date: Thu, 12 Oct 2023 01:29:43 +0800 Subject: [PATCH 2/9] add tests for quantized reverse_sequence --- tests/python/frontend/tflite/test_forward.py | 28 ++++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index f60166702454..8ac913796c4d 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -4280,11 +4280,24 @@ def _test_reverse_sequence(shape, dtype, seq_lengths, batch_axis, seq_axis): data = np.random.uniform(0, 100, size=shape).astype(dtype) with tf.Graph().as_default(): in_data = array_ops.placeholder(dtype=dtype, name="input", shape=shape) - out = tf.reverse_sequence( - in_data, seq_lengths=seq_lengths, batch_axis=batch_axis, seq_axis=seq_axis - ) - - compare_tflite_with_tvm(data, "input", [in_data], [out]) + + if quantized: + inq_data = tf.quantization.fake_quant_with_min_max_args( + in_data, min=0, max=10, name="inq_0" + ) + input_range = {"inq_0": (0, 10)} + out = tf.reverse_sequence( + inq_data, seq_lengths=seq_lengths, batch_axis=batch_axis, seq_axis=seq_axis + ) + out = tf.quantization.fake_quant_with_min_max_args(out, min=0, max=6, name="out") + compare_tflite_with_tvm( + data, "inq_0:0", [inq_data], [out], quantized=True, input_range=input_range + ) + else: + out = tf.reverse_sequence( + in_data, seq_lengths=seq_lengths, batch_axis=batch_axis, seq_axis=seq_axis + ) + compare_tflite_with_tvm(data, "input", [in_data], [out]) def test_forward_reverse_sequence(): @@ -4294,6 +4307,11 @@ def test_forward_reverse_sequence(): _test_reverse_sequence([2, 3, 3, 3], "float32", [2, 3, 2], 2, 1) _test_reverse_sequence([2, 4, 6, 4, 5], "float32", [5, 3], 0, 2) _test_reverse_sequence([2, 4, 6, 4, 5], "float32", [5, 3, 1, 4], 3, 2) + _test_reverse_sequence([4, 3], "uint8", [3, 2, 1], 1, 0, quantized=True) + _test_reverse_sequence([4, 3], "uint8", [3, 2, 1, 3], 0, 1, quantized=True) + _test_reverse_sequence([2, 3, 3, 3], "uint8", [2, 3, 2], 2, 1, quantized=True) + _test_reverse_sequence([2, 4, 6, 4, 5], "uint8", [5, 3], 0, 2, quantized=True) + _test_reverse_sequence([2, 4, 6, 4, 5], "uint8", [5, 3, 1, 4], 3, 2, quantized=True) ####################################################################### From 11c6a2a27900bd193f33c2bd2c197e3302b5308f Mon Sep 17 00:00:00 2001 From: Tlopex <68688494+tlopex@users.noreply.github.com> Date: Thu, 12 Oct 2023 11:29:08 +0800 Subject: [PATCH 3/9] Update test_forward.py --- tests/python/frontend/tflite/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 8ac913796c4d..a90e4249afe0 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -4274,7 +4274,7 @@ def test_forward_spacetodepth(): # --------------- -def _test_reverse_sequence(shape, dtype, seq_lengths, batch_axis, seq_axis): +def _test_reverse_sequence(shape, dtype, seq_lengths, batch_axis, seq_axis, quantized=False): """One iteration of reverse_sequence operation with given data and attributes""" data = np.random.uniform(0, 100, size=shape).astype(dtype) From 5a1f20392a4fc931352685135b7b56eecb215d14 Mon Sep 17 00:00:00 2001 From: Tlopex <68688494+tlopex@users.noreply.github.com> Date: Thu, 12 Oct 2023 18:05:39 +0800 Subject: [PATCH 4/9] Update test_forward.py --- tests/python/frontend/tflite/test_forward.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index a90e4249afe0..6ab17844a83d 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -4280,7 +4280,6 @@ def _test_reverse_sequence(shape, dtype, seq_lengths, batch_axis, seq_axis, quan data = np.random.uniform(0, 100, size=shape).astype(dtype) with tf.Graph().as_default(): in_data = array_ops.placeholder(dtype=dtype, name="input", shape=shape) - if quantized: inq_data = tf.quantization.fake_quant_with_min_max_args( in_data, min=0, max=10, name="inq_0" @@ -4307,11 +4306,11 @@ def test_forward_reverse_sequence(): _test_reverse_sequence([2, 3, 3, 3], "float32", [2, 3, 2], 2, 1) _test_reverse_sequence([2, 4, 6, 4, 5], "float32", [5, 3], 0, 2) _test_reverse_sequence([2, 4, 6, 4, 5], "float32", [5, 3, 1, 4], 3, 2) - _test_reverse_sequence([4, 3], "uint8", [3, 2, 1], 1, 0, quantized=True) - _test_reverse_sequence([4, 3], "uint8", [3, 2, 1, 3], 0, 1, quantized=True) - _test_reverse_sequence([2, 3, 3, 3], "uint8", [2, 3, 2], 2, 1, quantized=True) - _test_reverse_sequence([2, 4, 6, 4, 5], "uint8", [5, 3], 0, 2, quantized=True) - _test_reverse_sequence([2, 4, 6, 4, 5], "uint8", [5, 3, 1, 4], 3, 2, quantized=True) + _test_reverse_sequence([4, 3], "np.uint8", [3, 2, 1], 1, 0, quantized=True) + _test_reverse_sequence([4, 3], "np.uint8", [3, 2, 1, 3], 0, 1, quantized=True) + _test_reverse_sequence([2, 3, 3, 3], "np.uint8", [2, 3, 2], 2, 1, quantized=True) + _test_reverse_sequence([2, 4, 6, 4, 5], "np.uint8", [5, 3], 0, 2, quantized=True) + _test_reverse_sequence([2, 4, 6, 4, 5], "np.uint8", [5, 3, 1, 4], 3, 2, quantized=True) ####################################################################### From 20fe481bd781d67e8fb590d33e856d05181c13af Mon Sep 17 00:00:00 2001 From: Tlopex <68688494+tlopex@users.noreply.github.com> Date: Thu, 12 Oct 2023 18:42:47 +0800 Subject: [PATCH 5/9] Update test_forward.py --- 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 6ab17844a83d..34723db457dd 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -4280,6 +4280,7 @@ def _test_reverse_sequence(shape, dtype, seq_lengths, batch_axis, seq_axis, quan data = np.random.uniform(0, 100, size=shape).astype(dtype) with tf.Graph().as_default(): in_data = array_ops.placeholder(dtype=dtype, name="input", shape=shape) + if quantized: inq_data = tf.quantization.fake_quant_with_min_max_args( in_data, min=0, max=10, name="inq_0" From ce4bc3c93ae35e4d8bb1eade13e14d29e78f0e00 Mon Sep 17 00:00:00 2001 From: Tlopex <68688494+tlopex@users.noreply.github.com> Date: Fri, 13 Oct 2023 12:28:15 +0800 Subject: [PATCH 6/9] Update test_forward.py --- tests/python/frontend/tflite/test_forward.py | 21 +++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 34723db457dd..1248eb4e0bc5 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -4272,20 +4272,17 @@ def test_forward_spacetodepth(): ####################################################################### # ReverseSequence # --------------- - - def _test_reverse_sequence(shape, dtype, seq_lengths, batch_axis, seq_axis, quantized=False): """One iteration of reverse_sequence operation with given data and attributes""" - + data = np.random.uniform(0, 100, size=shape).astype(dtype) with tf.Graph().as_default(): - in_data = array_ops.placeholder(dtype=dtype, name="input", shape=shape) - + in_data = array_ops.placeholder(dtype="float32", name="in_0", shape=shape) if quantized: inq_data = tf.quantization.fake_quant_with_min_max_args( in_data, min=0, max=10, name="inq_0" ) - input_range = {"inq_0": (0, 10)} + input_range = {"inq_0": (-10, 10)} out = tf.reverse_sequence( inq_data, seq_lengths=seq_lengths, batch_axis=batch_axis, seq_axis=seq_axis ) @@ -4297,7 +4294,7 @@ def _test_reverse_sequence(shape, dtype, seq_lengths, batch_axis, seq_axis, quan out = tf.reverse_sequence( in_data, seq_lengths=seq_lengths, batch_axis=batch_axis, seq_axis=seq_axis ) - compare_tflite_with_tvm(data, "input", [in_data], [out]) + compare_tflite_with_tvm(data, "in_0:0", [in_data], [out]) def test_forward_reverse_sequence(): @@ -4307,11 +4304,11 @@ def test_forward_reverse_sequence(): _test_reverse_sequence([2, 3, 3, 3], "float32", [2, 3, 2], 2, 1) _test_reverse_sequence([2, 4, 6, 4, 5], "float32", [5, 3], 0, 2) _test_reverse_sequence([2, 4, 6, 4, 5], "float32", [5, 3, 1, 4], 3, 2) - _test_reverse_sequence([4, 3], "np.uint8", [3, 2, 1], 1, 0, quantized=True) - _test_reverse_sequence([4, 3], "np.uint8", [3, 2, 1, 3], 0, 1, quantized=True) - _test_reverse_sequence([2, 3, 3, 3], "np.uint8", [2, 3, 2], 2, 1, quantized=True) - _test_reverse_sequence([2, 4, 6, 4, 5], "np.uint8", [5, 3], 0, 2, quantized=True) - _test_reverse_sequence([2, 4, 6, 4, 5], "np.uint8", [5, 3, 1, 4], 3, 2, quantized=True) + _test_reverse_sequence([4, 3], "uint8", [3, 2, 1], 1, 0, quantized=True) + _test_reverse_sequence([4, 3], "uint8", [3, 2, 1, 3], 0, 1, quantized=True) + _test_reverse_sequence([2, 3, 3, 3], "uint8", [2, 3, 2], 2, 1, quantized=True) + _test_reverse_sequence([2, 4, 6, 4, 5], "uint8", [5, 3], 0, 2, quantized=True) + _test_reverse_sequence([2, 4, 6, 4, 5], "uint8", [5, 3, 1, 4], 3, 2, quantized=True) ####################################################################### From d4a3febc20c087a2ebd6b2ab37d18378a3215c6f Mon Sep 17 00:00:00 2001 From: Tlopex <68688494+tlopex@users.noreply.github.com> Date: Fri, 13 Oct 2023 13:29:16 +0800 Subject: [PATCH 7/9] Update test_forward.py --- tests/python/frontend/tflite/test_forward.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 1248eb4e0bc5..b9a0fa92d8b5 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -4274,7 +4274,6 @@ def test_forward_spacetodepth(): # --------------- def _test_reverse_sequence(shape, dtype, seq_lengths, batch_axis, seq_axis, quantized=False): """One iteration of reverse_sequence operation with given data and attributes""" - data = np.random.uniform(0, 100, size=shape).astype(dtype) with tf.Graph().as_default(): in_data = array_ops.placeholder(dtype="float32", name="in_0", shape=shape) From 166bc29eeab988d390ebfcd77fe138f153852e2a Mon Sep 17 00:00:00 2001 From: Shushi Hong <820958424@qq.com> Date: Tue, 21 May 2024 22:45:48 +0800 Subject: [PATCH 8/9] Update test_forward.py --- 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 b9a0fa92d8b5..072ccfb1cdd8 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -4297,6 +4297,7 @@ def _test_reverse_sequence(shape, dtype, seq_lengths, batch_axis, seq_axis, quan def test_forward_reverse_sequence(): + """Tests the reverse_sequence function with different input shapes, data types, and sequence lengths.""" if package_version.parse(tf.VERSION) >= package_version.parse("1.14.0"): _test_reverse_sequence([4, 3], "float32", [3, 2, 1], 1, 0) _test_reverse_sequence([4, 3], "float32", [3, 2, 1, 3], 0, 1) From 3faba03841080d6aa07936005157524294c916f4 Mon Sep 17 00:00:00 2001 From: Shushi Hong <820958424@qq.com> Date: Tue, 21 May 2024 23:02:40 +0800 Subject: [PATCH 9/9] Update test_forward.py --- tests/python/frontend/tflite/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 072ccfb1cdd8..8780ced60efa 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -4297,7 +4297,7 @@ def _test_reverse_sequence(shape, dtype, seq_lengths, batch_axis, seq_axis, quan def test_forward_reverse_sequence(): - """Tests the reverse_sequence function with different input shapes, data types, and sequence lengths.""" + """Tests the reverse_sequence function with different input shapes, data types.""" if package_version.parse(tf.VERSION) >= package_version.parse("1.14.0"): _test_reverse_sequence([4, 3], "float32", [3, 2, 1], 1, 0) _test_reverse_sequence([4, 3], "float32", [3, 2, 1, 3], 0, 1)