From 9e53bbda411af988a2a56566f94a6d7f81d1d5ce Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 20 Jul 2022 07:54:33 +0000 Subject: [PATCH 1/4] Update test for tflite2_quantized_depthwise_convolution --- tests/python/frontend/tflite/test_forward.py | 43 ++++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 6acc8554b4dd..5322ba7fa7cb 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -959,6 +959,39 @@ def test_forward_quantized_convolution(): int_quant_dtype=int_quant_dtype, ) +def test_forward_quantized_depthwise_convolution(): + for int_quant_dtype in [tf.int8, tf.int16]: + _test_tflite2_quantized_depthwise_convolution( + [1, 8, 8, 128], + [1, 1, 128, 1], + [1, 1], + [1, 1], + 'SAME', + 'NHWC', + 1, + int_quant_dtype + ) + _test_tflite2_quantized_depthwise_convolution( + [1, 17, 17, 12], + [3, 3, 12, 1], + [1, 1], + [2, 2], + 'VALID', + 'NHWC', + 1, + int_quant_dtype + ) + _test_tflite2_quantized_depthwise_convolution( + [1, 24, 24, 3], + [7, 7, 3, 8], + [1, 1], + [2, 2], + 'SAME', + 'NHWC', + 8, + int_quant_dtype + ) + def _test_tflite2_quantized_depthwise_convolution( input_shape, kernel_shape, dilations, strides, padding, data_format, depth_multiplier @@ -1231,15 +1264,6 @@ def test_forward_convolution(): [1, 17, 17, 124], [1, 1, 124, 19], [1, 1], [1, 1], "SAME", "NHWC", quantized=True ) - # Disable as tests are flaky - https://github.com/apache/tvm/issues/6064 - # depthwise convolution - # _test_tflite2_quantized_depthwise_convolution([1, 8, 8, 128], [1, 1, 128, 1], [1, 1], [1, 1], - # 'SAME', 'NHWC', 1) - # _test_tflite2_quantized_depthwise_convolution([1, 17, 17, 12], [3, 3, 12, 1], [1, 1], [2, 2], - # 'VALID', 'NHWC', 1) - # _test_tflite2_quantized_depthwise_convolution([1, 24, 24, 3], [7, 7, 3, 8], [1, 1], [2, 2], - # 'SAME', 'NHWC', 8) - ####################################################################### # Transpose Convolution @@ -5106,6 +5130,7 @@ def test_forward_nms_v5(): test_forward_qnn_coco_ssd_mobilenet_v1() # TFLite 2.1.0 quantized tests + test_forward_quantized_depthwise_convolution() test_forward_tflite2_qnn_resnet50() test_forward_tflite2_qnn_inception_v1() test_forward_tflite2_qnn_mobilenet_v2() From 6bbc7163e208c0b0241dbb27b7194395885f38fc Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 20 Jul 2022 07:55:14 +0000 Subject: [PATCH 2/4] fix input_node name error --- tests/python/frontend/tflite/test_forward.py | 46 ++++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 5322ba7fa7cb..6fe647eec794 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -933,9 +933,24 @@ def representative_data_gen(): is_float_output=True, int_quant_dtype=int_quant_dtype, ) + # TFLite.Model.Model has changed to TFLite.Model from 1.14 to 2.1 + try: + import tflite.Model + + tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_quant, 0) + except AttributeError: + import tflite + + tflite_model = tflite.Model.GetRootAsModel(tflite_model_quant, 0) + except ImportError: + raise ImportError("The tflite package must be installed") + + subgraph = tflite_model.Subgraphs(0) + model_input = subgraph.InputsAsNumpy() + input_node = subgraph.Tensors(model_input).Name().decode("utf-8") tflite_output = run_tflite_graph(tflite_model_quant, data) - tvm_output = run_tvm_graph(tflite_model_quant, data, data_in.name.replace(":0", "")) + tvm_output = run_tvm_graph(tflite_model_quant, data, input_node) tvm.testing.assert_allclose( np.squeeze(tvm_output[0]), np.squeeze(tflite_output[0]), rtol=1e-2, atol=1e-2 ) @@ -994,7 +1009,8 @@ def test_forward_quantized_depthwise_convolution(): def _test_tflite2_quantized_depthwise_convolution( - input_shape, kernel_shape, dilations, strides, padding, data_format, depth_multiplier + input_shape, kernel_shape, dilations, strides, padding, data_format, depth_multiplier, + int_quant_dtype=tf.int8 ): """One iteration of TFLite2 quantized depthwise convolution with given shapes and attributes""" @@ -1020,10 +1036,32 @@ def representative_data_gen(): for i in range(1): yield [data] - tflite_model_quant = _quantize_keras_model(keras_model, representative_data_gen) + tflite_model_quant = _quantize_keras_model( + keras_model, + representative_data_gen, + is_float_input=True, + is_float_output=True, + int_quant_dtype=int_quant_dtype, + ) + + # TFLite.Model.Model has changed to TFLite.Model from 1.14 to 2.1 + try: + import tflite.Model + + tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_quant, 0) + except AttributeError: + import tflite + + tflite_model = tflite.Model.GetRootAsModel(tflite_model_quant, 0) + except ImportError: + raise ImportError("The tflite package must be installed") + + subgraph = tflite_model.Subgraphs(0) + model_input = subgraph.InputsAsNumpy() + input_node = subgraph.Tensors(model_input).Name().decode("utf-8") tflite_output = run_tflite_graph(tflite_model_quant, data) - tvm_output = run_tvm_graph(tflite_model_quant, data, data_in.name.replace(":0", "")) + tvm_output = run_tvm_graph(tflite_model_quant, data, input_node) tvm.testing.assert_allclose( np.squeeze(tvm_output[0]), np.squeeze(tflite_output[0]), rtol=1e-2, atol=1e-2 ) From e96d0d453e5f19412644af3f49d9de27c8fb8392 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 20 Jul 2022 07:55:50 +0000 Subject: [PATCH 3/4] Add entry for test_quantized_convolution --- 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 6fe647eec794..ed9640467528 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -5168,6 +5168,7 @@ def test_forward_nms_v5(): test_forward_qnn_coco_ssd_mobilenet_v1() # TFLite 2.1.0 quantized tests + test_forward_quantized_convolution() test_forward_quantized_depthwise_convolution() test_forward_tflite2_qnn_resnet50() test_forward_tflite2_qnn_inception_v1() From 7603113e9a335e65e3c0be87610357e07b14b679 Mon Sep 17 00:00:00 2001 From: blackkker <823036806@qq.com> Date: Wed, 20 Jul 2022 08:18:07 +0000 Subject: [PATCH 4/4] reformatted by black --- tests/python/frontend/tflite/test_forward.py | 38 +++++++------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index ed9640467528..42848783967c 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -974,43 +974,29 @@ def test_forward_quantized_convolution(): int_quant_dtype=int_quant_dtype, ) + def test_forward_quantized_depthwise_convolution(): for int_quant_dtype in [tf.int8, tf.int16]: _test_tflite2_quantized_depthwise_convolution( - [1, 8, 8, 128], - [1, 1, 128, 1], - [1, 1], - [1, 1], - 'SAME', - 'NHWC', - 1, - int_quant_dtype + [1, 8, 8, 128], [1, 1, 128, 1], [1, 1], [1, 1], "SAME", "NHWC", 1, int_quant_dtype ) _test_tflite2_quantized_depthwise_convolution( - [1, 17, 17, 12], - [3, 3, 12, 1], - [1, 1], - [2, 2], - 'VALID', - 'NHWC', - 1, - int_quant_dtype + [1, 17, 17, 12], [3, 3, 12, 1], [1, 1], [2, 2], "VALID", "NHWC", 1, int_quant_dtype ) _test_tflite2_quantized_depthwise_convolution( - [1, 24, 24, 3], - [7, 7, 3, 8], - [1, 1], - [2, 2], - 'SAME', - 'NHWC', - 8, - int_quant_dtype + [1, 24, 24, 3], [7, 7, 3, 8], [1, 1], [2, 2], "SAME", "NHWC", 8, int_quant_dtype ) def _test_tflite2_quantized_depthwise_convolution( - input_shape, kernel_shape, dilations, strides, padding, data_format, depth_multiplier, - int_quant_dtype=tf.int8 + input_shape, + kernel_shape, + dilations, + strides, + padding, + data_format, + depth_multiplier, + int_quant_dtype=tf.int8, ): """One iteration of TFLite2 quantized depthwise convolution with given shapes and attributes"""