From 2e10b59346d27ad3f505fbe7624c281af082f134 Mon Sep 17 00:00:00 2001 From: Ashutosh Parkhi Date: Tue, 18 Jan 2022 17:50:38 +0000 Subject: [PATCH 1/2] [CMSIS-NN] Separated symmetric and asymmetric padding tests for Conv2D Change-Id: Ia73223291680005ac8f9313d7f16a6afcbfb0b18 --- .../contrib/test_cmsisnn/test_conv2d.py | 110 +++++++++++++++++- 1 file changed, 108 insertions(+), 2 deletions(-) diff --git a/tests/python/contrib/test_cmsisnn/test_conv2d.py b/tests/python/contrib/test_cmsisnn/test_conv2d.py index d8c559cec6e0..b8b32e6f6db4 100644 --- a/tests/python/contrib/test_cmsisnn/test_conv2d.py +++ b/tests/python/contrib/test_cmsisnn/test_conv2d.py @@ -124,7 +124,113 @@ def make_model( @tvm.testing.requires_cmsisnn -@pytest.mark.parametrize("ifm_shape", [(1, 25, 25, 12), (1, 64, 100, 4)]) +@pytest.mark.parametrize("ifm_shape", [(1, 64, 100, 4)]) +@pytest.mark.parametrize("kernel_size", [(3, 3)]) +@pytest.mark.parametrize("padding", ["SAME", "VALID"]) +@pytest.mark.parametrize("strides, dilation", [((1, 1), (1, 1))]) +@pytest.mark.parametrize("relu_type", ["RELU"]) +@pytest.mark.parametrize("enable_bias", [True, False]) +@pytest.mark.parametrize( + "input_zero_point, input_scale, kernel_scale, out_channels", + [(10, 0.0128, [0.11, 0.22], 2), (-64, 1, [1, 0.0256, 1.37], 3)], +) +def test_conv2d_symmetric_padding_int8( + ifm_shape, + kernel_size, + padding, + strides, + dilation, + enable_bias, + relu_type, + input_zero_point, + input_scale, + kernel_scale, + out_channels, +): + interface_api = "c" + use_unpacked_api = True + test_runner = AOT_CORSTONE300_RUNNER + + dtype = "int8" + groups = 1 + weight_format = "HWIO" + kernel_h = kernel_size[0] + kernel_w = kernel_size[1] + kernel_shape = (kernel_h, kernel_w, ifm_shape[3] // groups, out_channels) + kernel_zero_point = 0 + in_min, in_max = get_range_for_dtype_str(dtype) + + output_scale, output_zero_point = get_conv2d_qnn_params( + kernel_shape, + input_scale, + input_zero_point, + kernel_scale, + kernel_zero_point, + dtype, + dtype, + dtype, + ) + + model, params = make_model( + ifm_shape, + kernel_shape, + input_zero_point, + input_scale, + kernel_zero_point, + kernel_scale, + output_zero_point, + output_scale, + padding, + strides, + dilation, + groups, + dtype, + dtype, + out_channels, + weight_format, + enable_bias, + relu_type, + ) + orig_mod = make_module(model) + cmsisnn_mod = cmsisnn.partition_for_cmsisnn(orig_mod, params) + + # validate pattern matching + attrs = [ + cmsisnn_mod[var.name_hint].attrs + for var in cmsisnn_mod.get_global_vars() + if cmsisnn_mod[var.name_hint].attrs + ] + assert any(attrs), "At least one function with external attributes was expected." + + compilers = [ + key == "Compiler" and value == "cmsis-nn" for attr in attrs for key, value in attr.items() + ] + assert any(compilers), "Module does not contain function for cmsis-nn target." + + assert count_num_calls(orig_mod) == count_num_calls( + cmsisnn_mod + ), "Number of calls changed during partitioning" + + # validate the output + rng = np.random.default_rng(12345) + inputs = {"input": rng.integers(in_min, high=in_max, size=ifm_shape, dtype=dtype)} + output_list = generate_ref_data(orig_mod["main"], inputs, params) + compile_and_run( + AOTTestModel( + module=cmsisnn_mod, + inputs=inputs, + outputs=output_list, + params=params, + output_tolerance=1, + ), + test_runner, + interface_api, + use_unpacked_api, + ) + + +@tvm.testing.requires_cmsisnn +@pytest.mark.parametrize("ifm_shape", [(1, 25, 25, 12)]) @pytest.mark.parametrize("kernel_size", [(5, 5)]) @pytest.mark.parametrize("padding", ["SAME", "VALID"]) @pytest.mark.parametrize("strides, dilation", [((2, 2), (1, 1))]) @@ -134,7 +240,7 @@ def make_model( "input_zero_point, input_scale, kernel_scale, out_channels", [(10, 0.0128, [0.11, 0.22], 2), (-64, 1, [1, 0.0256, 1.37], 3)], ) -def test_conv2d_int8( +def test_conv2d_asymmetric_padding_int8( ifm_shape, kernel_size, padding, From b90a17991c91fc0dbbeb20144d8a7e3a12248d4d Mon Sep 17 00:00:00 2001 From: Ashutosh Parkhi Date: Wed, 19 Jan 2022 10:23:11 +0000 Subject: [PATCH 2/2] Hard coded Conv2D parameters in the test to disallow changes to padding values Change-Id: I4ec6dd74a4785d16408da04538cb58a07781d788 --- .../contrib/test_cmsisnn/test_conv2d.py | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/tests/python/contrib/test_cmsisnn/test_conv2d.py b/tests/python/contrib/test_cmsisnn/test_conv2d.py index b8b32e6f6db4..e9eb6fb3a145 100644 --- a/tests/python/contrib/test_cmsisnn/test_conv2d.py +++ b/tests/python/contrib/test_cmsisnn/test_conv2d.py @@ -124,10 +124,7 @@ def make_model( @tvm.testing.requires_cmsisnn -@pytest.mark.parametrize("ifm_shape", [(1, 64, 100, 4)]) -@pytest.mark.parametrize("kernel_size", [(3, 3)]) @pytest.mark.parametrize("padding", ["SAME", "VALID"]) -@pytest.mark.parametrize("strides, dilation", [((1, 1), (1, 1))]) @pytest.mark.parametrize("relu_type", ["RELU"]) @pytest.mark.parametrize("enable_bias", [True, False]) @pytest.mark.parametrize( @@ -135,11 +132,7 @@ def make_model( [(10, 0.0128, [0.11, 0.22], 2), (-64, 1, [1, 0.0256, 1.37], 3)], ) def test_conv2d_symmetric_padding_int8( - ifm_shape, - kernel_size, padding, - strides, - dilation, enable_bias, relu_type, input_zero_point, @@ -151,6 +144,10 @@ def test_conv2d_symmetric_padding_int8( use_unpacked_api = True test_runner = AOT_CORSTONE300_RUNNER + ifm_shape = (1, 64, 100, 4) + kernel_size = (3, 3) + strides = (1, 1) + dilation = (1, 1) dtype = "int8" groups = 1 weight_format = "HWIO" @@ -230,22 +227,15 @@ def test_conv2d_symmetric_padding_int8( @tvm.testing.requires_cmsisnn -@pytest.mark.parametrize("ifm_shape", [(1, 25, 25, 12)]) -@pytest.mark.parametrize("kernel_size", [(5, 5)]) @pytest.mark.parametrize("padding", ["SAME", "VALID"]) -@pytest.mark.parametrize("strides, dilation", [((2, 2), (1, 1))]) -@pytest.mark.parametrize("relu_type", ["RELU"]) +@pytest.mark.parametrize("relu_type", ["RELU", "NONE"]) @pytest.mark.parametrize("enable_bias", [True, False]) @pytest.mark.parametrize( "input_zero_point, input_scale, kernel_scale, out_channels", [(10, 0.0128, [0.11, 0.22], 2), (-64, 1, [1, 0.0256, 1.37], 3)], ) def test_conv2d_asymmetric_padding_int8( - ifm_shape, - kernel_size, padding, - strides, - dilation, enable_bias, relu_type, input_zero_point, @@ -257,6 +247,10 @@ def test_conv2d_asymmetric_padding_int8( use_unpacked_api = True test_runner = AOT_CORSTONE300_RUNNER + ifm_shape = (1, 25, 25, 12) + kernel_size = (5, 5) + strides = (2, 2) + dilation = (1, 1) dtype = "int8" groups = 1 weight_format = "HWIO"