Skip to content

Commit a189889

Browse files
author
Christian Convey
committed
[hexagon][testing] nonrandom tests
Add support for populating unit-test input tensors with values other than random.
1 parent 885fead commit a189889

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

tests/python/contrib/test_hexagon/pytest_util.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ def get_single_param_chunk(param_val, param_desc: Optional[str]):
5959
val_str = "F"
6060
need_prefix_separator = True
6161

62+
elif type(param_val) == TensorContentConstant:
63+
val_str = f"const({param_val.elem_value})"
64+
need_prefix_separator = True
65+
66+
elif type(param_val) == TensorContentDtypeMin:
67+
val_str = "min"
68+
need_prefix_separator = True
69+
70+
elif type(param_val) == TensorContentDtypeMax:
71+
val_str = "min"
72+
need_prefix_separator = True
73+
74+
elif type(param_val) == TensorContentRandom:
75+
val_str = "random"
76+
need_prefix_separator = True
77+
6278
else:
6379
val_str = str(param_val)
6480
need_prefix_separator = True
@@ -91,3 +107,40 @@ def get_multitest_ids(
91107
get_test_id(*single_test_param_list, test_param_descs=param_descs)
92108
for single_test_param_list in multitest_params_list
93109
]
110+
111+
112+
# Note that the *actual* tensor dtype is given as a parameter to the
113+
# 'create_populated_ndarray' function.
114+
# 'elem_value' must be something that 'input_np' knows how to convert
115+
# to the desired tensor dtype.
116+
TensorContentConstant = collections.namedtuple("TensorContentConstant", ["elem_value"])
117+
118+
TensorContentRandom = collections.namedtuple("TensorContentRandom", [])
119+
TensorContentDtypeMin = collections.namedtuple("TensorContentDtypeMin", [])
120+
TensorContentDtypeMax = collections.namedtuple("TensorContentDtypeMax", [])
121+
122+
123+
def create_populated_numpy_ndarray(
124+
input_shape: Union[list, tuple], dtype: str, input_tensor_populator
125+
) -> np.ndarray:
126+
"""
127+
Create a numpy tensor with the specified shape, dtype, and content.
128+
"""
129+
itp = input_tensor_populator # just for brevity
130+
131+
if type(itp) == TensorContentConstant:
132+
return np.full(tuple(input_shape), itp.elem_value, dtype=dtype)
133+
134+
elif type(itp) == TensorContentDtypeMin:
135+
info = _get_np_dtype_info(dtype)
136+
return np.full(tuple(input_shape), info.min, dtype=dtype)
137+
138+
elif type(itp) == TensorContentDtypeMax:
139+
info = _get_np_dtype_info(dtype)
140+
return np.full(tuple(input_shape), info.max, dtype=dtype)
141+
142+
elif type(itp) == TensorContentRandom:
143+
return np.random.random(input_shape).astype(dtype)
144+
145+
else:
146+
raise ValueError(f"Unexpected input_tensor_populator type: {type(itp)}")

tests/python/contrib/test_hexagon/topi/test_avg_pool2d_slice.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import pytest
1919
import numpy as np
2020
from typing import *
21-
import collections
2221

2322
from tvm import te
2423
import tvm.testing
@@ -27,7 +26,14 @@
2726
from tvm.contrib.hexagon.session import Session
2827
import tvm.topi.hexagon.slice_ops as sl
2928
from ..infrastructure import allocate_hexagon_array, transform_numpy
30-
from ..pytest_util import get_multitest_ids
29+
from ..pytest_util import (
30+
get_multitest_ids,
31+
create_populated_numpy_ndarray,
32+
TensorContentConstant,
33+
TensorContentRandom,
34+
TensorContentDtypeMin,
35+
TensorContentDtypeMax,
36+
)
3137

3238

3339
input_layout = tvm.testing.parameter(
@@ -36,8 +42,8 @@
3642

3743

3844
@tvm.testing.fixture
39-
def input_np(input_shape, dtype):
40-
return np.random.random(input_shape).astype(dtype)
45+
def input_np(input_shape, dtype: str, input_tensor_populator):
46+
return create_populated_numpy_ndarray(input_shape, dtype, input_tensor_populator)
4147

4248

4349
@tvm.testing.fixture
@@ -61,6 +67,7 @@ class TestAvgPool2dSlice:
6167
"cnt_padded", # count_include_pad
6268
"out_layout", # output_layout
6369
None, # dtype
70+
None, # input_tensor_populator
6471
]
6572

6673
_multitest_params = [
@@ -74,6 +81,7 @@ class TestAvgPool2dSlice:
7481
True,
7582
"nhwc-8h2w32c2w-2d",
7683
"float16",
84+
TensorContentRandom(),
7785
),
7886
(
7987
[1, 16, 16, 32],
@@ -85,6 +93,7 @@ class TestAvgPool2dSlice:
8593
True,
8694
"nhwc-8h2w32c2w-2d",
8795
"float16",
96+
TensorContentRandom(),
8897
),
8998
(
9099
[1, 8, 8, 32],
@@ -96,6 +105,7 @@ class TestAvgPool2dSlice:
96105
True,
97106
"nhwc-8h2w32c2w-2d",
98107
"float16",
108+
TensorContentRandom(),
99109
),
100110
# Test non-one stride and dilation
101111
(
@@ -108,6 +118,7 @@ class TestAvgPool2dSlice:
108118
True,
109119
"nhwc-8h2w32c2w-2d",
110120
"float16",
121+
TensorContentRandom(),
111122
),
112123
(
113124
[1, 8, 8, 32],
@@ -119,6 +130,7 @@ class TestAvgPool2dSlice:
119130
True,
120131
"nhwc-8h2w32c2w-2d",
121132
"float16",
133+
TensorContentRandom(),
122134
),
123135
(
124136
[1, 8, 8, 32],
@@ -130,6 +142,7 @@ class TestAvgPool2dSlice:
130142
True,
131143
"nhwc-8h2w32c2w-2d",
132144
"float16",
145+
TensorContentRandom(),
133146
),
134147
# Test non-zero padding
135148
(
@@ -142,6 +155,7 @@ class TestAvgPool2dSlice:
142155
True,
143156
"nhwc-8h2w32c2w-2d",
144157
"float16",
158+
TensorContentRandom(),
145159
),
146160
(
147161
[1, 8, 8, 32],
@@ -153,6 +167,7 @@ class TestAvgPool2dSlice:
153167
True,
154168
"nhwc-8h2w32c2w-2d",
155169
"float16",
170+
TensorContentRandom(),
156171
),
157172
(
158173
[1, 8, 8, 32],
@@ -164,6 +179,7 @@ class TestAvgPool2dSlice:
164179
True,
165180
"nhwc-8h2w32c2w-2d",
166181
"float16",
182+
TensorContentRandom(),
167183
),
168184
(
169185
[1, 8, 8, 32],
@@ -175,6 +191,7 @@ class TestAvgPool2dSlice:
175191
True,
176192
"nhwc-8h2w32c2w-2d",
177193
"float16",
194+
TensorContentRandom(),
178195
),
179196
# Test n11c-1024c-2d layout which will require input and output to have different layout
180197
(
@@ -187,6 +204,7 @@ class TestAvgPool2dSlice:
187204
True,
188205
"n11c-1024c-2d",
189206
"float16",
207+
TensorContentRandom(),
190208
),
191209
(
192210
[1, 1, 1, 2048],
@@ -198,6 +216,7 @@ class TestAvgPool2dSlice:
198216
True,
199217
"n11c-1024c-2d",
200218
"float16",
219+
TensorContentRandom(),
201220
),
202221
(
203222
[1, 1, 1, 2048],
@@ -209,6 +228,7 @@ class TestAvgPool2dSlice:
209228
True,
210229
"n11c-1024c-2d",
211230
"float16",
231+
TensorContentRandom(),
212232
),
213233
(
214234
[1, 1, 1, 2048],
@@ -220,6 +240,7 @@ class TestAvgPool2dSlice:
220240
True,
221241
"n11c-1024c-2d",
222242
"float16",
243+
TensorContentRandom(),
223244
),
224245
]
225246

@@ -236,6 +257,7 @@ class TestAvgPool2dSlice:
236257
count_include_pad,
237258
output_layout,
238259
dtype,
260+
input_tensor_populator,
239261
) = tvm.testing.parameters(*_multitest_params, ids=_param_ids)
240262

241263
@tvm.testing.fixture

0 commit comments

Comments
 (0)