This is an automated email from the ASF dual-hosted git repository.

kparzysz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new 9863cf0d5f [hexagon][testing] Better pytest ID strings (#12154)
9863cf0d5f is described below

commit 9863cf0d5f49e54c21d1a1243d87fd01f7277911
Author: Christian Convey <ccon...@octoml.ai>
AuthorDate: Fri Jul 22 15:52:56 2022 -0400

    [hexagon][testing] Better pytest ID strings (#12154)
    
    - Add utility functions to allow more human-readable pytest test IDs.
      Helpful when ID strings become too large for humans to easily read.
    
    - Update the `test_avg_pool2d_slice.py` unit test to use this mechanism.
---
 tests/python/contrib/test_hexagon/pytest_util.py   | 93 ++++++++++++++++++++++
 .../test_hexagon/topi/test_avg_pool2d_slice.py     | 45 ++++++++---
 2 files changed, 125 insertions(+), 13 deletions(-)

diff --git a/tests/python/contrib/test_hexagon/pytest_util.py 
b/tests/python/contrib/test_hexagon/pytest_util.py
new file mode 100644
index 0000000000..fb28ebeb68
--- /dev/null
+++ b/tests/python/contrib/test_hexagon/pytest_util.py
@@ -0,0 +1,93 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import pytest
+import numpy as np
+from typing import *
+import collections
+import tvm.testing
+
+
+def get_test_id(*test_params, test_param_descs: List[Optional[str]] = None) -> 
str:
+    """
+    An opinionated alternative to pytest's default algorithm for generating a
+    test's ID string.  Intended to make it easier for human readers to
+    interpret the test IDs.
+
+    'test_params': The sequence of pytest parameter values supplied to some 
unit
+       test.
+
+    'test_param_descs': An (optional) means to provide additional text for 
some/all of the
+       paramuments in 'test_params'.
+
+       If provided, then len(test_params) must equal len(test_param_descs).
+       Each element test_param_descs that is a non-empty string will be used
+       in some sensible way in this function's returned string.
+    """
+
+    assert len(test_params) > 0
+
+    if test_param_descs is None:
+        test_param_descs = [None] * len(test_params)
+    else:
+        assert len(test_param_descs) == len(test_params)
+
+    def get_single_param_chunk(param_val, param_desc: Optional[str]):
+        if type(param_val) == list:
+            # Like str(list), but avoid the whitespace padding.
+            val_str = "[" + ",".join(str(x) for x in param_val) + "]"
+            need_prefix_separator = False
+
+        elif type(param_val) == bool:
+            if param_val:
+                val_str = "T"
+            else:
+                val_str = "F"
+            need_prefix_separator = True
+
+        else:
+            val_str = str(param_val)
+            need_prefix_separator = True
+
+        if param_desc and need_prefix_separator:
+            return f"{param_desc}:{val_str}"
+        elif param_desc and not need_prefix_separator:
+            return f"{param_desc}{val_str}"
+        else:
+            return val_str
+
+    chunks = [
+        get_single_param_chunk(param_val, param_desc)
+        for param_val, param_desc in zip(test_params, test_param_descs)
+    ]
+    return "-".join(chunks)
+
+
+def get_multitest_ids(
+    multitest_params_list: List[List], param_descs: 
Optional[List[Optional[str]]]
+) -> List[str]:
+    """
+    A convenience function for classes that use both 'tvm.testing.parameters' 
and 'get_test_id'.
+
+    This function provides a workaround for a specific quirk in Python, where 
list-comprehension
+    can't necessarily access the value of another class-variable, discused 
here:
+    https://stackoverflow.com/q/13905741
+    """
+    return [
+        get_test_id(*single_test_param_list, test_param_descs=param_descs)
+        for single_test_param_list in multitest_params_list
+    ]
diff --git a/tests/python/contrib/test_hexagon/topi/test_avg_pool2d_slice.py 
b/tests/python/contrib/test_hexagon/topi/test_avg_pool2d_slice.py
index 5b1f59c897..34e9b751b9 100644
--- a/tests/python/contrib/test_hexagon/topi/test_avg_pool2d_slice.py
+++ b/tests/python/contrib/test_hexagon/topi/test_avg_pool2d_slice.py
@@ -17,6 +17,8 @@
 
 import pytest
 import numpy as np
+from typing import *
+import collections
 
 from tvm import te
 import tvm.testing
@@ -25,6 +27,7 @@ from tvm.contrib.hexagon.build import HexagonLauncher
 from tvm.contrib.hexagon.session import Session
 import tvm.topi.hexagon.slice_ops as sl
 from ..infrastructure import allocate_hexagon_array, transform_numpy
+from ..pytest_util import get_multitest_ids
 
 
 input_layout = tvm.testing.parameter(
@@ -48,18 +51,19 @@ def transformed_input_np_padded(input_np_padded, 
input_layout):
 
 
 class TestAvgPool2dSlice:
-    # NOTE: input_layout is always assumed to be "nhwc-8h2w32c2w-2d"
-    (
-        output_shape,
-        kernel,
-        stride,
-        dilation,
-        padding,
-        ceil_mode,
-        count_include_pad,
-        output_layout,
-        dtype,
-    ) = tvm.testing.parameters(
+    _param_descs = [
+        "out_shape",  # output_shape
+        "kernel",  # kernel
+        "stride",  # stride
+        "dil",  # dilation
+        "pad",  # padding
+        "ceil",  # ceil_mode
+        "cnt_padded",  # count_include_pad
+        "out_layout",  # output_layout
+        None,  # dtype
+    ]
+
+    _multitest_params = [
         (
             [1, 8, 8, 32],
             [3, 3],
@@ -217,7 +221,22 @@ class TestAvgPool2dSlice:
             "n11c-1024c-2d",
             "float16",
         ),
-    )
+    ]
+
+    _param_ids = get_multitest_ids(_multitest_params, _param_descs)
+
+    # NOTE: input_layout is always assumed to be "nhwc-8h2w32c2w-2d"
+    (
+        output_shape,
+        kernel,
+        stride,
+        dilation,
+        padding,
+        ceil_mode,
+        count_include_pad,
+        output_layout,
+        dtype,
+    ) = tvm.testing.parameters(*_multitest_params, ids=_param_ids)
 
     @tvm.testing.fixture
     def expected_output_np(

Reply via email to