Skip to content

Commit

Permalink
[microNPU] Add support for ResizeNearestNeighbor with half_pixel_cent…
Browse files Browse the repository at this point in the history
…ers=True (#14401)

This PR involves supporting the legalization case of RESIZE_NEAREST_NEIGHBOR where coordinate transformation mode is set to half_pixel.
  • Loading branch information
ilyag-grovety authored Apr 5, 2023
1 parent 25ec646 commit 9dcd40d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
15 changes: 12 additions & 3 deletions python/tvm/relay/op/contrib/ethosu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
or self.coordinate_transformation_mode != "half_pixel"
and self.rounding_method != ""
):
return False
if not check_compatible_size(
self.coordinate_transformation_mode,
Expand All @@ -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
Expand Down
18 changes: 14 additions & 4 deletions tests/python/contrib/test_ethosu/test_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,17 +1085,27 @@ def squeeze_func(x):

@pytest.mark.parametrize("accel_type", ACCEL_TYPES)
@pytest.mark.parametrize(
"ifm_shape,size",
[[(1, 2, 2, 1), (4, 4)], [(1, 4, 7, 3), (8, 14)], [(1, 3, 5, 3), (3, 5)]],
"ifm_shape,size,half_pixel",
[
[(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):
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(
Expand Down
18 changes: 12 additions & 6 deletions tests/python/contrib/test_ethosu/test_legalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2342,22 +2342,28 @@ def verify(ext_func):


@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, 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):
def test_tflite_resize2d_nearest_neighbor(ifm_shape, size, half_pixel):
align_corners = False
dtype = "int8"

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(
Expand Down

0 comments on commit 9dcd40d

Please sign in to comment.