blob: 954c6e9c415890202686e2e1eb488f4768f4ccba [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
Jeremy Johnson65ba8092023-10-09 16:31:13 +01006from pathlib import Path
Eric Kunzee5e26762020-10-13 16:11:07 -07007
Jeremy Johnson65ba8092023-10-09 16:31:13 +01008import conformance.model_files as cmf
Jeremy Johnson2ec34942021-12-14 16:34:05 +00009from generator.tosa_test_gen import TosaTestGen
10from serializer.tosa_serializer import dtype_str_to_val
James Ward24dbc422022-10-19 12:20:31 +010011from serializer.tosa_serializer import DTypeNames
Eric Kunzee5e26762020-10-13 16:11:07 -070012
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +010013OPTION_FP_VALUES_RANGE = "--fp-values-range"
14
Jeremy Johnson5c1364c2022-01-13 15:04:21 +000015
Eric Kunzee5e26762020-10-13 16:11:07 -070016# Used for parsing a comma-separated list of integers in a string
17# to an actual list of integers
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +010018def str_to_list(in_s, is_float=False):
Kevin Cheng550ccc52021-03-03 11:21:43 -080019 """Converts a comma-separated list of string integers to a python list of ints"""
20 lst = in_s.split(",")
Eric Kunzee5e26762020-10-13 16:11:07 -070021 out_list = []
22 for i in lst:
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +010023 val = float(i) if is_float else int(i)
24 out_list.append(val)
Eric Kunzee5e26762020-10-13 16:11:07 -070025 return out_list
26
Kevin Cheng550ccc52021-03-03 11:21:43 -080027
Eric Kunzee5e26762020-10-13 16:11:07 -070028def auto_int(x):
Kevin Cheng550ccc52021-03-03 11:21:43 -080029 """Converts hex/dec argument values to an int"""
Eric Kunzee5e26762020-10-13 16:11:07 -070030 return int(x, 0)
31
Kevin Cheng550ccc52021-03-03 11:21:43 -080032
Jeremy Johnson00423432022-09-12 17:27:37 +010033def parseArgs(argv):
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +010034 """Parse the command line arguments."""
35 if argv is None:
36 argv = sys.argv[1:]
37
38 if OPTION_FP_VALUES_RANGE in argv:
39 # Argparse fix for hyphen (minus values) in argument values
40 # convert "ARG VAL" into "ARG=VAL"
41 # Example --fp-values-range -2.0,2.0 -> --fp-values-range=-2.0,2.0
42 new_argv = []
43 idx = 0
44 while idx < len(argv):
45 arg = argv[idx]
46 if arg == OPTION_FP_VALUES_RANGE and idx + 1 < len(argv):
47 val = argv[idx + 1]
48 if val.startswith("-"):
49 arg = f"{arg}={val}"
50 idx += 1
51 new_argv.append(arg)
52 idx += 1
53 argv = new_argv
Eric Kunzee5e26762020-10-13 16:11:07 -070054
55 parser = argparse.ArgumentParser()
Jeremy Johnson1271c442023-09-05 11:39:26 +010056
57 ops_group = parser.add_argument_group("operator options")
58 tens_group = parser.add_argument_group("tensor options")
59
Kevin Cheng550ccc52021-03-03 11:21:43 -080060 parser.add_argument(
61 "-o", dest="output_dir", type=str, default="vtest", help="Test output directory"
62 )
Eric Kunzee5e26762020-10-13 16:11:07 -070063
Kevin Cheng550ccc52021-03-03 11:21:43 -080064 parser.add_argument(
65 "--seed",
66 dest="random_seed",
67 default=42,
68 type=int,
69 help="Random seed for test generation",
70 )
Eric Kunzee5e26762020-10-13 16:11:07 -070071
Kevin Cheng550ccc52021-03-03 11:21:43 -080072 parser.add_argument(
73 "--filter",
74 dest="filter",
75 default="",
76 type=str,
77 help="Filter operator test names by this expression",
78 )
Eric Kunzee5e26762020-10-13 16:11:07 -070079
Kevin Cheng550ccc52021-03-03 11:21:43 -080080 parser.add_argument(
81 "-v", "--verbose", dest="verbose", action="count", help="Verbose operation"
82 )
Eric Kunzee5e26762020-10-13 16:11:07 -070083
Kevin Cheng550ccc52021-03-03 11:21:43 -080084 parser.add_argument(
Jeremy Johnson1271c442023-09-05 11:39:26 +010085 "--lazy-data-generation",
86 dest="lazy_data_gen",
87 action="store_true",
88 help="Tensor data generation is delayed til test running",
89 )
90
Jeremy Johnson65ba8092023-10-09 16:31:13 +010091 parser.add_argument(
92 "--generate-lib-path",
93 dest="generate_lib_path",
94 type=Path,
95 help="Path to TOSA generate library.",
96 )
97
Jeremy Johnson1271c442023-09-05 11:39:26 +010098 # Constraints on tests
99 tens_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800100 "--tensor-dim-range",
101 dest="tensor_shape_range",
102 default="1,64",
103 type=lambda x: str_to_list(x),
104 help="Min,Max range of tensor shapes",
105 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700106
Jeremy Johnson1271c442023-09-05 11:39:26 +0100107 tens_group.add_argument(
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +0100108 OPTION_FP_VALUES_RANGE,
109 dest="tensor_fp_value_range",
110 default="0.0,1.0",
111 type=lambda x: str_to_list(x, is_float=True),
112 help="Min,Max range of floating point tensor values",
113 )
114
Jeremy Johnson1271c442023-09-05 11:39:26 +0100115 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800116 "--max-batch-size",
117 dest="max_batch_size",
118 default=1,
James Ward30124a82023-02-02 14:56:33 +0000119 type=positive_integer_type,
Kevin Cheng550ccc52021-03-03 11:21:43 -0800120 help="Maximum batch size for NHWC tests",
121 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700122
Jeremy Johnson1271c442023-09-05 11:39:26 +0100123 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800124 "--max-conv-padding",
125 dest="max_conv_padding",
126 default=1,
127 type=int,
128 help="Maximum padding for Conv tests",
129 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700130
Jeremy Johnson1271c442023-09-05 11:39:26 +0100131 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800132 "--max-conv-dilation",
133 dest="max_conv_dilation",
134 default=2,
135 type=int,
136 help="Maximum dilation for Conv tests",
137 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700138
Jeremy Johnson1271c442023-09-05 11:39:26 +0100139 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800140 "--max-conv-stride",
141 dest="max_conv_stride",
142 default=2,
143 type=int,
144 help="Maximum stride for Conv tests",
145 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700146
Jeremy Johnson1271c442023-09-05 11:39:26 +0100147 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800148 "--max-pooling-padding",
149 dest="max_pooling_padding",
150 default=1,
151 type=int,
152 help="Maximum padding for pooling tests",
153 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700154
Jeremy Johnson1271c442023-09-05 11:39:26 +0100155 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800156 "--max-pooling-stride",
157 dest="max_pooling_stride",
158 default=2,
159 type=int,
160 help="Maximum stride for pooling tests",
161 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700162
Jeremy Johnson1271c442023-09-05 11:39:26 +0100163 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800164 "--max-pooling-kernel",
165 dest="max_pooling_kernel",
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000166 default=3,
Kevin Cheng550ccc52021-03-03 11:21:43 -0800167 type=int,
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000168 help="Maximum kernel for pooling tests",
Kevin Cheng550ccc52021-03-03 11:21:43 -0800169 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700170
Jeremy Johnson1271c442023-09-05 11:39:26 +0100171 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800172 "--num-rand-permutations",
173 dest="num_rand_permutations",
174 default=6,
175 type=int,
176 help="Number of random permutations for a given shape/rank for randomly-sampled parameter spaces",
177 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700178
Jeremy Johnson1271c442023-09-05 11:39:26 +0100179 ops_group.add_argument(
Jeremy Johnsona0e03f32022-06-13 17:48:09 +0100180 "--max-resize-output-dim",
181 dest="max_resize_output_dim",
182 default=1000,
183 type=int,
184 help="Upper limit on width and height output dimensions for `resize` op. Default: 1000",
185 )
186
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +0100187 # Targeting a specific shape/rank/dtype
Jeremy Johnson1271c442023-09-05 11:39:26 +0100188 tens_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800189 "--target-shape",
190 dest="target_shapes",
191 action="append",
192 default=[],
193 type=lambda x: str_to_list(x),
194 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)",
195 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700196
Jeremy Johnson1271c442023-09-05 11:39:26 +0100197 tens_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800198 "--target-rank",
199 dest="target_ranks",
200 action="append",
201 default=None,
202 type=lambda x: auto_int(x),
203 help="Create tests with a particular input tensor rank",
204 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700205
James Ward24dbc422022-10-19 12:20:31 +0100206 # Used for parsing a comma-separated list of integers in a string
Jeremy Johnson1271c442023-09-05 11:39:26 +0100207 tens_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800208 "--target-dtype",
209 dest="target_dtypes",
210 action="append",
211 default=None,
212 type=lambda x: dtype_str_to_val(x),
James Ward24dbc422022-10-19 12:20:31 +0100213 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 -0800214 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700215
Jeremy Johnson1271c442023-09-05 11:39:26 +0100216 ops_group.add_argument(
Matthew Haddon818ab902021-07-27 09:12:49 +0100217 "--num-const-inputs-concat",
218 dest="num_const_inputs_concat",
219 default=0,
220 choices=[0, 1, 2, 3],
221 type=int,
222 help="Allow constant input tensors for concat operator",
223 )
224
Matthew Haddon74567092021-07-16 15:38:20 +0100225 parser.add_argument(
226 "--test-type",
227 dest="test_type",
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000228 choices=["positive", "negative", "both"],
Matthew Haddon74567092021-07-16 15:38:20 +0100229 default="positive",
230 type=str,
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000231 help="type of tests produced, positive, negative, or both",
Matthew Haddon74567092021-07-16 15:38:20 +0100232 )
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000233
Jeremy Johnson1271c442023-09-05 11:39:26 +0100234 ops_group.add_argument(
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000235 "--allow-pooling-and-conv-oversizes",
236 dest="oversize",
Jeremy Johnsonae0c1c62022-02-10 17:27:34 +0000237 action="store_true",
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000238 help="allow oversize padding, stride and kernel tests",
239 )
240
Jeremy Johnson1271c442023-09-05 11:39:26 +0100241 ops_group.add_argument(
Jeremy Johnson00423432022-09-12 17:27:37 +0100242 "--zero-point",
243 dest="zeropoint",
244 default=None,
245 type=int,
246 help="set a particular zero point for all valid positive tests",
247 )
248
Jeremy Johnsona0848c62022-09-15 15:01:30 +0100249 parser.add_argument(
250 "--dump-const-tensors",
251 dest="dump_consts",
252 action="store_true",
253 help="output const tensors as numpy files for inspection",
254 )
255
Jeremy Johnson1271c442023-09-05 11:39:26 +0100256 ops_group.add_argument(
Jeremy Johnsonb2099702023-04-12 15:59:01 +0100257 "--level-8k-sizes",
258 dest="level8k",
259 action="store_true",
Jeremy Johnson1271c442023-09-05 11:39:26 +0100260 help="create level 8k size tests",
Jeremy Johnsonb2099702023-04-12 15:59:01 +0100261 )
262
Jeremy Johnson00423432022-09-12 17:27:37 +0100263 args = parser.parse_args(argv)
Eric Kunzee5e26762020-10-13 16:11:07 -0700264
265 return args
266
Eric Kunzee5e26762020-10-13 16:11:07 -0700267
James Ward30124a82023-02-02 14:56:33 +0000268def positive_integer_type(argv_str):
269 value = int(argv_str)
270 if value <= 0:
271 msg = f"{argv_str} is not a valid positive integer"
272 raise argparse.ArgumentTypeError(msg)
273 return value
274
275
Jeremy Johnson00423432022-09-12 17:27:37 +0100276def main(argv=None):
Eric Kunzee5e26762020-10-13 16:11:07 -0700277
Jeremy Johnson00423432022-09-12 17:27:37 +0100278 args = parseArgs(argv)
Eric Kunzee5e26762020-10-13 16:11:07 -0700279
Jeremy Johnson65ba8092023-10-09 16:31:13 +0100280 if not args.lazy_data_gen:
281 if args.generate_lib_path is None:
282 args.generate_lib_path = cmf.find_tosa_file(
283 cmf.TosaFileType.GENERATE_LIBRARY, Path("reference_model"), False
284 )
285 if not args.generate_lib_path.is_file():
286 print(
287 f"Argument error: Generate library (--generate-lib-path) not found - {str(args.generate_lib_path)}"
288 )
289 exit(2)
290
Eric Kunzee5e26762020-10-13 16:11:07 -0700291 ttg = TosaTestGen(args)
292
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000293 if args.test_type == "both":
294 testType = ["positive", "negative"]
Matthew Haddon1c00b712021-10-01 15:51:03 +0100295 else:
296 testType = [args.test_type]
Matthew Haddon74567092021-07-16 15:38:20 +0100297 results = []
Matthew Haddon1c00b712021-10-01 15:51:03 +0100298 for test_type in testType:
299 testList = []
300 for op in ttg.TOSA_OP_LIST:
301 if re.match(args.filter + ".*", op):
302 testList.extend(
303 ttg.genOpTestList(
304 op,
305 shapeFilter=args.target_shapes,
306 rankFilter=args.target_ranks,
307 dtypeFilter=args.target_dtypes,
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000308 testType=test_type,
Matthew Haddon1c00b712021-10-01 15:51:03 +0100309 )
310 )
Matthew Haddon848efb42021-09-09 12:30:53 +0100311
Matthew Haddon1c00b712021-10-01 15:51:03 +0100312 print("{} matching {} tests".format(len(testList), test_type))
313
314 testStrings = []
315 for opName, testStr, dtype, error, shapeList, testArgs in testList:
316 # Check for and skip duplicate tests
317 if testStr in testStrings:
Jeremy Johnsona0e03f32022-06-13 17:48:09 +0100318 print(f"Skipping duplicate test: {testStr}")
Matthew Haddon1c00b712021-10-01 15:51:03 +0100319 continue
320 else:
321 testStrings.append(testStr)
322
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000323 results.append(
324 ttg.serializeTest(opName, testStr, dtype, error, shapeList, testArgs)
325 )
Matthew Haddon74567092021-07-16 15:38:20 +0100326
327 print(f"Done creating {len(results)} tests")
328
Eric Kunzee5e26762020-10-13 16:11:07 -0700329
Kevin Cheng550ccc52021-03-03 11:21:43 -0800330if __name__ == "__main__":
Eric Kunzee5e26762020-10-13 16:11:07 -0700331 exit(main())