From c9e6ed1635bc6f4bf0c01ec090eb559a048de4a3 Mon Sep 17 00:00:00 2001 From: Ilya Gozman <92577591+ilyag-grovety@users.noreply.github.com> Date: Fri, 24 Mar 2023 19:04:43 +0400 Subject: [PATCH 1/3] [microNPU] Add support for RESIZE_NEAREST_NEIGHBOR with half_pixel_centers=True --- python/tvm/relay/op/contrib/ethosu.py | 15 ++++++++++++--- tests/python/contrib/test_ethosu/test_codegen.py | 12 +++++++++--- tests/python/contrib/test_ethosu/test_legalize.py | 6 ++++-- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/python/tvm/relay/op/contrib/ethosu.py b/python/tvm/relay/op/contrib/ethosu.py index a6d959c98b01..e8ea0f040a7e 100644 --- a/python/tvm/relay/op/contrib/ethosu.py +++ b/python/tvm/relay/op/contrib/ethosu.py @@ -1671,7 +1671,18 @@ def check_compatible_size(mode, method, upscale_size, ifm_size): return False if self.method not in ("nearest_neighbor", "linear"): return False - if self.coordinate_transformation_mode not in ("asymmetric", "align_corners"): + if self.coordinate_transformation_mode not in ( + "asymmetric", + "align_corners", + "half_pixel", + ): + return False + if ( + self.coordinate_transformation_mode == "half_pixel" + and self.rounding_method != "round_prefer_ceil" + ): + return False + if self.coordinate_transformation_mode != "half_pixel" and self.rounding_method != "": return False if not check_compatible_size( self.coordinate_transformation_mode, @@ -1680,8 +1691,6 @@ def check_compatible_size(mode, method, upscale_size, ifm_size): self.ifm.shape[1:3], ): return False - if self.rounding_method != "": - return False if self.out_dtype and self.out_dtype != "int8": return False return True diff --git a/tests/python/contrib/test_ethosu/test_codegen.py b/tests/python/contrib/test_ethosu/test_codegen.py index 6eb382d8f588..9f3630073c31 100644 --- a/tests/python/contrib/test_ethosu/test_codegen.py +++ b/tests/python/contrib/test_ethosu/test_codegen.py @@ -1084,18 +1084,24 @@ def squeeze_func(x): @pytest.mark.parametrize("accel_type", ACCEL_TYPES) +@pytest.mark.parametrize("half_pixel", [False, True]) @pytest.mark.parametrize( "ifm_shape,size", - [[(1, 2, 2, 1), (4, 4)], [(1, 4, 7, 3), (8, 14)], [(1, 3, 5, 3), (3, 5)]], + [ + [(1, 2, 2, 1), (4, 4)], + [(1, 4, 7, 3), (8, 14)], + [(1, 3, 5, 3), (3, 5)], + [(1, 6, 6, 96), (12, 12)], + ], ) -def test_tflite_resize2d_nearest_neighbor(accel_type, ifm_shape, size): +def test_tflite_resize2d_nearest_neighbor(accel_type, ifm_shape, size, half_pixel): np.random.seed(0) align_corners = False @tf.function def resize_model(x): return tf.compat.v1.image.resize_nearest_neighbor( - x, size, align_corners=align_corners, half_pixel_centers=False + x, size, align_corners=align_corners, half_pixel_centers=half_pixel, ) infra.compare_tvm_with_tflite( diff --git a/tests/python/contrib/test_ethosu/test_legalize.py b/tests/python/contrib/test_ethosu/test_legalize.py index 0bd9c1ac3bf4..5a108d82cde7 100644 --- a/tests/python/contrib/test_ethosu/test_legalize.py +++ b/tests/python/contrib/test_ethosu/test_legalize.py @@ -2341,15 +2341,17 @@ def verify(ext_func): verify(mod["tvmgen_default_ethos_u_main_0"]) +@pytest.mark.parametrize("half_pixel", [False, True]) @pytest.mark.parametrize( "ifm_shape,size", [ [(1, 2, 2, 1), (4, 4)], [(1, 4, 7, 3), (8, 14)], [(1, 3, 5, 3), (3, 5)], + [(1, 6, 6, 96), (12, 12)], ], ) -def test_tflite_resize2d_nearest_neighbor(ifm_shape, size): +def test_tflite_resize2d_nearest_neighbor(ifm_shape, size, half_pixel): align_corners = False dtype = "int8" @@ -2357,7 +2359,7 @@ def create_tflite_graph(): @tf.function def resize_model(x): return tf.compat.v1.image.resize_nearest_neighbor( - x, size, align_corners=align_corners, half_pixel_centers=False + x, size, align_corners=align_corners, half_pixel_centers=half_pixel, ) concrete_func = resize_model.get_concrete_function( From 624b5d236d154ca7744ab0cc971df876c50e1d41 Mon Sep 17 00:00:00 2001 From: Ilya Gozman <92577591+ilyag-grovety@users.noreply.github.com> Date: Mon, 27 Mar 2023 11:36:18 +0400 Subject: [PATCH 2/3] Fix lint --- tests/python/contrib/test_ethosu/test_codegen.py | 5 ++++- tests/python/contrib/test_ethosu/test_legalize.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/python/contrib/test_ethosu/test_codegen.py b/tests/python/contrib/test_ethosu/test_codegen.py index 9f3630073c31..90a1c88934cc 100644 --- a/tests/python/contrib/test_ethosu/test_codegen.py +++ b/tests/python/contrib/test_ethosu/test_codegen.py @@ -1101,7 +1101,10 @@ def test_tflite_resize2d_nearest_neighbor(accel_type, ifm_shape, size, half_pixe @tf.function def resize_model(x): return tf.compat.v1.image.resize_nearest_neighbor( - x, size, align_corners=align_corners, half_pixel_centers=half_pixel, + x, + size, + align_corners=align_corners, + half_pixel_centers=half_pixel, ) infra.compare_tvm_with_tflite( diff --git a/tests/python/contrib/test_ethosu/test_legalize.py b/tests/python/contrib/test_ethosu/test_legalize.py index 5a108d82cde7..cd56a0b41f16 100644 --- a/tests/python/contrib/test_ethosu/test_legalize.py +++ b/tests/python/contrib/test_ethosu/test_legalize.py @@ -2359,7 +2359,10 @@ def create_tflite_graph(): @tf.function def resize_model(x): return tf.compat.v1.image.resize_nearest_neighbor( - x, size, align_corners=align_corners, half_pixel_centers=half_pixel, + x, + size, + align_corners=align_corners, + half_pixel_centers=half_pixel, ) concrete_func = resize_model.get_concrete_function( From 500c63d986911ad20d80abb70e8592341deecdf3 Mon Sep 17 00:00:00 2001 From: Ilya Gozman <92577591+ilyag-grovety@users.noreply.github.com> Date: Tue, 4 Apr 2023 14:30:35 +0400 Subject: [PATCH 3/3] Update tests --- python/tvm/relay/op/contrib/ethosu.py | 4 ++-- tests/python/contrib/test_ethosu/test_codegen.py | 13 +++++++------ tests/python/contrib/test_ethosu/test_legalize.py | 13 +++++++------ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/python/tvm/relay/op/contrib/ethosu.py b/python/tvm/relay/op/contrib/ethosu.py index e8ea0f040a7e..d74140da5db2 100644 --- a/python/tvm/relay/op/contrib/ethosu.py +++ b/python/tvm/relay/op/contrib/ethosu.py @@ -1680,10 +1680,10 @@ def check_compatible_size(mode, method, upscale_size, ifm_size): if ( self.coordinate_transformation_mode == "half_pixel" and self.rounding_method != "round_prefer_ceil" + or self.coordinate_transformation_mode != "half_pixel" + and self.rounding_method != "" ): return False - if self.coordinate_transformation_mode != "half_pixel" and self.rounding_method != "": - return False if not check_compatible_size( self.coordinate_transformation_mode, self.method, diff --git a/tests/python/contrib/test_ethosu/test_codegen.py b/tests/python/contrib/test_ethosu/test_codegen.py index 90a1c88934cc..c68dde1288b8 100644 --- a/tests/python/contrib/test_ethosu/test_codegen.py +++ b/tests/python/contrib/test_ethosu/test_codegen.py @@ -1084,14 +1084,15 @@ def squeeze_func(x): @pytest.mark.parametrize("accel_type", ACCEL_TYPES) -@pytest.mark.parametrize("half_pixel", [False, True]) @pytest.mark.parametrize( - "ifm_shape,size", + "ifm_shape,size,half_pixel", [ - [(1, 2, 2, 1), (4, 4)], - [(1, 4, 7, 3), (8, 14)], - [(1, 3, 5, 3), (3, 5)], - [(1, 6, 6, 96), (12, 12)], + [(1, 2, 2, 1), (4, 4), False], + [(1, 2, 2, 1), (4, 4), True], + [(1, 4, 7, 3), (8, 14), False], + [(1, 3, 5, 3), (3, 5), False], + [(1, 6, 6, 96), (12, 12), False], + [(1, 6, 6, 96), (12, 12), True], ], ) def test_tflite_resize2d_nearest_neighbor(accel_type, ifm_shape, size, half_pixel): diff --git a/tests/python/contrib/test_ethosu/test_legalize.py b/tests/python/contrib/test_ethosu/test_legalize.py index cd56a0b41f16..594f4a0e2aef 100644 --- a/tests/python/contrib/test_ethosu/test_legalize.py +++ b/tests/python/contrib/test_ethosu/test_legalize.py @@ -2341,14 +2341,15 @@ def verify(ext_func): verify(mod["tvmgen_default_ethos_u_main_0"]) -@pytest.mark.parametrize("half_pixel", [False, True]) @pytest.mark.parametrize( - "ifm_shape,size", + "ifm_shape,size,half_pixel", [ - [(1, 2, 2, 1), (4, 4)], - [(1, 4, 7, 3), (8, 14)], - [(1, 3, 5, 3), (3, 5)], - [(1, 6, 6, 96), (12, 12)], + [(1, 2, 2, 1), (4, 4), False], + [(1, 2, 2, 1), (4, 4), True], + [(1, 4, 7, 3), (8, 14), False], + [(1, 3, 5, 3), (3, 5), False], + [(1, 6, 6, 96), (12, 12), False], + [(1, 6, 6, 96), (12, 12), True], ], ) def test_tflite_resize2d_nearest_neighbor(ifm_shape, size, half_pixel):