blob: 0d532c0dcf752295c0e592308492a8d05a95dafb [file] [log] [blame]
James Ward30124a82023-02-02 14:56:33 +00001# Copyright (c) 2020-2023, ARM Limited.
Jeremy Johnson5c1364c2022-01-13 15:04:21 +00002# SPDX-License-Identifier: Apache-2.0
Eric Kunzee5e26762020-10-13 16:11:07 -07003import argparse
Eric Kunzee5e26762020-10-13 16:11:07 -07004import re
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +01005import sys
Eric Kunzee5e26762020-10-13 16:11:07 -07006
Jeremy Johnson2ec34942021-12-14 16:34:05 +00007from generator.tosa_test_gen import TosaTestGen
8from serializer.tosa_serializer import dtype_str_to_val
James Ward24dbc422022-10-19 12:20:31 +01009from serializer.tosa_serializer import DTypeNames
Eric Kunzee5e26762020-10-13 16:11:07 -070010
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +010011OPTION_FP_VALUES_RANGE = "--fp-values-range"
12
Jeremy Johnson5c1364c2022-01-13 15:04:21 +000013
Eric Kunzee5e26762020-10-13 16:11:07 -070014# Used for parsing a comma-separated list of integers in a string
15# to an actual list of integers
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +010016def str_to_list(in_s, is_float=False):
Kevin Cheng550ccc52021-03-03 11:21:43 -080017 """Converts a comma-separated list of string integers to a python list of ints"""
18 lst = in_s.split(",")
Eric Kunzee5e26762020-10-13 16:11:07 -070019 out_list = []
20 for i in lst:
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +010021 val = float(i) if is_float else int(i)
22 out_list.append(val)
Eric Kunzee5e26762020-10-13 16:11:07 -070023 return out_list
24
Kevin Cheng550ccc52021-03-03 11:21:43 -080025
Eric Kunzee5e26762020-10-13 16:11:07 -070026def auto_int(x):
Kevin Cheng550ccc52021-03-03 11:21:43 -080027 """Converts hex/dec argument values to an int"""
Eric Kunzee5e26762020-10-13 16:11:07 -070028 return int(x, 0)
29
Kevin Cheng550ccc52021-03-03 11:21:43 -080030
Jeremy Johnson00423432022-09-12 17:27:37 +010031def parseArgs(argv):
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +010032 """Parse the command line arguments."""
33 if argv is None:
34 argv = sys.argv[1:]
35
36 if OPTION_FP_VALUES_RANGE in argv:
37 # Argparse fix for hyphen (minus values) in argument values
38 # convert "ARG VAL" into "ARG=VAL"
39 # Example --fp-values-range -2.0,2.0 -> --fp-values-range=-2.0,2.0
40 new_argv = []
41 idx = 0
42 while idx < len(argv):
43 arg = argv[idx]
44 if arg == OPTION_FP_VALUES_RANGE and idx + 1 < len(argv):
45 val = argv[idx + 1]
46 if val.startswith("-"):
47 arg = f"{arg}={val}"
48 idx += 1
49 new_argv.append(arg)
50 idx += 1
51 argv = new_argv
Eric Kunzee5e26762020-10-13 16:11:07 -070052
53 parser = argparse.ArgumentParser()
Jeremy Johnson1271c442023-09-05 11:39:26 +010054
55 ops_group = parser.add_argument_group("operator options")
56 tens_group = parser.add_argument_group("tensor options")
57
Kevin Cheng550ccc52021-03-03 11:21:43 -080058 parser.add_argument(
59 "-o", dest="output_dir", type=str, default="vtest", help="Test output directory"
60 )
Eric Kunzee5e26762020-10-13 16:11:07 -070061
Kevin Cheng550ccc52021-03-03 11:21:43 -080062 parser.add_argument(
63 "--seed",
64 dest="random_seed",
65 default=42,
66 type=int,
67 help="Random seed for test generation",
68 )
Eric Kunzee5e26762020-10-13 16:11:07 -070069
Kevin Cheng550ccc52021-03-03 11:21:43 -080070 parser.add_argument(
71 "--filter",
72 dest="filter",
73 default="",
74 type=str,
75 help="Filter operator test names by this expression",
76 )
Eric Kunzee5e26762020-10-13 16:11:07 -070077
Kevin Cheng550ccc52021-03-03 11:21:43 -080078 parser.add_argument(
79 "-v", "--verbose", dest="verbose", action="count", help="Verbose operation"
80 )
Eric Kunzee5e26762020-10-13 16:11:07 -070081
Kevin Cheng550ccc52021-03-03 11:21:43 -080082 parser.add_argument(
Jeremy Johnson1271c442023-09-05 11:39:26 +010083 "--lazy-data-generation",
84 dest="lazy_data_gen",
85 action="store_true",
86 help="Tensor data generation is delayed til test running",
87 )
88
89 # Constraints on tests
90 tens_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -080091 "--tensor-dim-range",
92 dest="tensor_shape_range",
93 default="1,64",
94 type=lambda x: str_to_list(x),
95 help="Min,Max range of tensor shapes",
96 )
Eric Kunzee5e26762020-10-13 16:11:07 -070097
Jeremy Johnson1271c442023-09-05 11:39:26 +010098 tens_group.add_argument(
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +010099 OPTION_FP_VALUES_RANGE,
100 dest="tensor_fp_value_range",
101 default="0.0,1.0",
102 type=lambda x: str_to_list(x, is_float=True),
103 help="Min,Max range of floating point tensor values",
104 )
105
Jeremy Johnson1271c442023-09-05 11:39:26 +0100106 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800107 "--max-batch-size",
108 dest="max_batch_size",
109 default=1,
James Ward30124a82023-02-02 14:56:33 +0000110 type=positive_integer_type,
Kevin Cheng550ccc52021-03-03 11:21:43 -0800111 help="Maximum batch size for NHWC tests",
112 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700113
Jeremy Johnson1271c442023-09-05 11:39:26 +0100114 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800115 "--max-conv-padding",
116 dest="max_conv_padding",
117 default=1,
118 type=int,
119 help="Maximum padding for Conv tests",
120 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700121
Jeremy Johnson1271c442023-09-05 11:39:26 +0100122 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800123 "--max-conv-dilation",
124 dest="max_conv_dilation",
125 default=2,
126 type=int,
127 help="Maximum dilation for Conv tests",
128 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700129
Jeremy Johnson1271c442023-09-05 11:39:26 +0100130 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800131 "--max-conv-stride",
132 dest="max_conv_stride",
133 default=2,
134 type=int,
135 help="Maximum stride for Conv tests",
136 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700137
Jeremy Johnson1271c442023-09-05 11:39:26 +0100138 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800139 "--max-pooling-padding",
140 dest="max_pooling_padding",
141 default=1,
142 type=int,
143 help="Maximum padding for pooling tests",
144 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700145
Jeremy Johnson1271c442023-09-05 11:39:26 +0100146 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800147 "--max-pooling-stride",
148 dest="max_pooling_stride",
149 default=2,
150 type=int,
151 help="Maximum stride for pooling tests",
152 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700153
Jeremy Johnson1271c442023-09-05 11:39:26 +0100154 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800155 "--max-pooling-kernel",
156 dest="max_pooling_kernel",
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000157 default=3,
Kevin Cheng550ccc52021-03-03 11:21:43 -0800158 type=int,
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000159 help="Maximum kernel for pooling tests",
Kevin Cheng550ccc52021-03-03 11:21:43 -0800160 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700161
Jeremy Johnson1271c442023-09-05 11:39:26 +0100162 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800163 "--num-rand-permutations",
164 dest="num_rand_permutations",
165 default=6,
166 type=int,
167 help="Number of random permutations for a given shape/rank for randomly-sampled parameter spaces",
168 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700169
Jeremy Johnson1271c442023-09-05 11:39:26 +0100170 ops_group.add_argument(
Jeremy Johnsona0e03f32022-06-13 17:48:09 +0100171 "--max-resize-output-dim",
172 dest="max_resize_output_dim",
173 default=1000,
174 type=int,
175 help="Upper limit on width and height output dimensions for `resize` op. Default: 1000",
176 )
177
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +0100178 # Targeting a specific shape/rank/dtype
Jeremy Johnson1271c442023-09-05 11:39:26 +0100179 tens_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800180 "--target-shape",
181 dest="target_shapes",
182 action="append",
183 default=[],
184 type=lambda x: str_to_list(x),
185 help="Create tests with a particular input tensor shape, e.g., 1,4,4,8 (may be repeated for tests that require multiple input shapes)",
186 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700187
Jeremy Johnson1271c442023-09-05 11:39:26 +0100188 tens_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800189 "--target-rank",
190 dest="target_ranks",
191 action="append",
192 default=None,
193 type=lambda x: auto_int(x),
194 help="Create tests with a particular input tensor rank",
195 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700196
James Ward24dbc422022-10-19 12:20:31 +0100197 # Used for parsing a comma-separated list of integers in a string
Jeremy Johnson1271c442023-09-05 11:39:26 +0100198 tens_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800199 "--target-dtype",
200 dest="target_dtypes",
201 action="append",
202 default=None,
203 type=lambda x: dtype_str_to_val(x),
James Ward24dbc422022-10-19 12:20:31 +0100204 help=f"Create test with a particular DType: [{', '.join([d.lower() for d in DTypeNames[1:]])}] (may be repeated)",
Kevin Cheng550ccc52021-03-03 11:21:43 -0800205 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700206
Jeremy Johnson1271c442023-09-05 11:39:26 +0100207 ops_group.add_argument(
Matthew Haddon818ab902021-07-27 09:12:49 +0100208 "--num-const-inputs-concat",
209 dest="num_const_inputs_concat",
210 default=0,
211 choices=[0, 1, 2, 3],
212 type=int,
213 help="Allow constant input tensors for concat operator",
214 )
215
Matthew Haddon74567092021-07-16 15:38:20 +0100216 parser.add_argument(
217 "--test-type",
218 dest="test_type",
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000219 choices=["positive", "negative", "both"],
Matthew Haddon74567092021-07-16 15:38:20 +0100220 default="positive",
221 type=str,
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000222 help="type of tests produced, positive, negative, or both",
Matthew Haddon74567092021-07-16 15:38:20 +0100223 )
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000224
Jeremy Johnson1271c442023-09-05 11:39:26 +0100225 ops_group.add_argument(
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000226 "--allow-pooling-and-conv-oversizes",
227 dest="oversize",
Jeremy Johnsonae0c1c62022-02-10 17:27:34 +0000228 action="store_true",
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000229 help="allow oversize padding, stride and kernel tests",
230 )
231
Jeremy Johnson1271c442023-09-05 11:39:26 +0100232 ops_group.add_argument(
Jeremy Johnson00423432022-09-12 17:27:37 +0100233 "--zero-point",
234 dest="zeropoint",
235 default=None,
236 type=int,
237 help="set a particular zero point for all valid positive tests",
238 )
239
Jeremy Johnsona0848c62022-09-15 15:01:30 +0100240 parser.add_argument(
241 "--dump-const-tensors",
242 dest="dump_consts",
243 action="store_true",
244 help="output const tensors as numpy files for inspection",
245 )
246
Jeremy Johnson1271c442023-09-05 11:39:26 +0100247 ops_group.add_argument(
Jeremy Johnsonb2099702023-04-12 15:59:01 +0100248 "--level-8k-sizes",
249 dest="level8k",
250 action="store_true",
Jeremy Johnson1271c442023-09-05 11:39:26 +0100251 help="create level 8k size tests",
Jeremy Johnsonb2099702023-04-12 15:59:01 +0100252 )
253
Jeremy Johnson00423432022-09-12 17:27:37 +0100254 args = parser.parse_args(argv)
Eric Kunzee5e26762020-10-13 16:11:07 -0700255
256 return args
257
Eric Kunzee5e26762020-10-13 16:11:07 -0700258
James Ward30124a82023-02-02 14:56:33 +0000259def positive_integer_type(argv_str):
260 value = int(argv_str)
261 if value <= 0:
262 msg = f"{argv_str} is not a valid positive integer"
263 raise argparse.ArgumentTypeError(msg)
264 return value
265
266
Jeremy Johnson00423432022-09-12 17:27:37 +0100267def main(argv=None):
Eric Kunzee5e26762020-10-13 16:11:07 -0700268
Jeremy Johnson00423432022-09-12 17:27:37 +0100269 args = parseArgs(argv)
Eric Kunzee5e26762020-10-13 16:11:07 -0700270
271 ttg = TosaTestGen(args)
272
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000273 if args.test_type == "both":
274 testType = ["positive", "negative"]
Matthew Haddon1c00b712021-10-01 15:51:03 +0100275 else:
276 testType = [args.test_type]
Matthew Haddon74567092021-07-16 15:38:20 +0100277 results = []
Matthew Haddon1c00b712021-10-01 15:51:03 +0100278 for test_type in testType:
279 testList = []
280 for op in ttg.TOSA_OP_LIST:
281 if re.match(args.filter + ".*", op):
282 testList.extend(
283 ttg.genOpTestList(
284 op,
285 shapeFilter=args.target_shapes,
286 rankFilter=args.target_ranks,
287 dtypeFilter=args.target_dtypes,
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000288 testType=test_type,
Matthew Haddon1c00b712021-10-01 15:51:03 +0100289 )
290 )
Matthew Haddon848efb42021-09-09 12:30:53 +0100291
Matthew Haddon1c00b712021-10-01 15:51:03 +0100292 print("{} matching {} tests".format(len(testList), test_type))
293
294 testStrings = []
295 for opName, testStr, dtype, error, shapeList, testArgs in testList:
296 # Check for and skip duplicate tests
297 if testStr in testStrings:
Jeremy Johnsona0e03f32022-06-13 17:48:09 +0100298 print(f"Skipping duplicate test: {testStr}")
Matthew Haddon1c00b712021-10-01 15:51:03 +0100299 continue
300 else:
301 testStrings.append(testStr)
302
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000303 results.append(
304 ttg.serializeTest(opName, testStr, dtype, error, shapeList, testArgs)
305 )
Matthew Haddon74567092021-07-16 15:38:20 +0100306
307 print(f"Done creating {len(results)} tests")
308
Eric Kunzee5e26762020-10-13 16:11:07 -0700309
Kevin Cheng550ccc52021-03-03 11:21:43 -0800310if __name__ == "__main__":
Eric Kunzee5e26762020-10-13 16:11:07 -0700311 exit(main())