blob: d01e8a76ad937ce7166fae32b0e985cde58a30a6 [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
Jeremy Johnsona4d907e2023-10-26 13:53:14 +010016# Used for parsing a comma-separated list of integers/floats in a string
17# to an actual list of integers/floats with special case max
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +010018def str_to_list(in_s, is_float=False):
Jeremy Johnsona4d907e2023-10-26 13:53:14 +010019 """Converts a comma-separated list string to a python list of numbers."""
Kevin Cheng550ccc52021-03-03 11:21:43 -080020 lst = in_s.split(",")
Eric Kunzee5e26762020-10-13 16:11:07 -070021 out_list = []
22 for i in lst:
Jeremy Johnsona4d907e2023-10-26 13:53:14 +010023 # Special case for allowing maximum FP numbers
24 if is_float and i in ("-max", "max"):
25 val = i
26 else:
27 val = float(i) if is_float else int(i)
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +010028 out_list.append(val)
Eric Kunzee5e26762020-10-13 16:11:07 -070029 return out_list
30
Kevin Cheng550ccc52021-03-03 11:21:43 -080031
Eric Kunzee5e26762020-10-13 16:11:07 -070032def auto_int(x):
Kevin Cheng550ccc52021-03-03 11:21:43 -080033 """Converts hex/dec argument values to an int"""
Eric Kunzee5e26762020-10-13 16:11:07 -070034 return int(x, 0)
35
Kevin Cheng550ccc52021-03-03 11:21:43 -080036
Jeremy Johnson00423432022-09-12 17:27:37 +010037def parseArgs(argv):
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +010038 """Parse the command line arguments."""
39 if argv is None:
40 argv = sys.argv[1:]
41
42 if OPTION_FP_VALUES_RANGE in argv:
43 # Argparse fix for hyphen (minus values) in argument values
44 # convert "ARG VAL" into "ARG=VAL"
45 # Example --fp-values-range -2.0,2.0 -> --fp-values-range=-2.0,2.0
46 new_argv = []
47 idx = 0
48 while idx < len(argv):
49 arg = argv[idx]
50 if arg == OPTION_FP_VALUES_RANGE and idx + 1 < len(argv):
51 val = argv[idx + 1]
52 if val.startswith("-"):
53 arg = f"{arg}={val}"
54 idx += 1
55 new_argv.append(arg)
56 idx += 1
57 argv = new_argv
Eric Kunzee5e26762020-10-13 16:11:07 -070058
59 parser = argparse.ArgumentParser()
Jeremy Johnson1271c442023-09-05 11:39:26 +010060
61 ops_group = parser.add_argument_group("operator options")
62 tens_group = parser.add_argument_group("tensor options")
63
Kevin Cheng550ccc52021-03-03 11:21:43 -080064 parser.add_argument(
65 "-o", dest="output_dir", type=str, default="vtest", help="Test output directory"
66 )
Eric Kunzee5e26762020-10-13 16:11:07 -070067
Kevin Cheng550ccc52021-03-03 11:21:43 -080068 parser.add_argument(
69 "--seed",
70 dest="random_seed",
71 default=42,
72 type=int,
73 help="Random seed for test generation",
74 )
Eric Kunzee5e26762020-10-13 16:11:07 -070075
Kevin Cheng550ccc52021-03-03 11:21:43 -080076 parser.add_argument(
77 "--filter",
78 dest="filter",
79 default="",
80 type=str,
81 help="Filter operator test names by this expression",
82 )
Eric Kunzee5e26762020-10-13 16:11:07 -070083
Kevin Cheng550ccc52021-03-03 11:21:43 -080084 parser.add_argument(
85 "-v", "--verbose", dest="verbose", action="count", help="Verbose operation"
86 )
Eric Kunzee5e26762020-10-13 16:11:07 -070087
Kevin Cheng550ccc52021-03-03 11:21:43 -080088 parser.add_argument(
Jeremy Johnson1271c442023-09-05 11:39:26 +010089 "--lazy-data-generation",
90 dest="lazy_data_gen",
91 action="store_true",
92 help="Tensor data generation is delayed til test running",
93 )
94
Jeremy Johnson65ba8092023-10-09 16:31:13 +010095 parser.add_argument(
96 "--generate-lib-path",
97 dest="generate_lib_path",
98 type=Path,
99 help="Path to TOSA generate library.",
100 )
101
Jeremy Johnson1271c442023-09-05 11:39:26 +0100102 # Constraints on tests
103 tens_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800104 "--tensor-dim-range",
105 dest="tensor_shape_range",
106 default="1,64",
107 type=lambda x: str_to_list(x),
108 help="Min,Max range of tensor shapes",
109 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700110
Jeremy Johnson1271c442023-09-05 11:39:26 +0100111 tens_group.add_argument(
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +0100112 OPTION_FP_VALUES_RANGE,
113 dest="tensor_fp_value_range",
114 default="0.0,1.0",
115 type=lambda x: str_to_list(x, is_float=True),
116 help="Min,Max range of floating point tensor values",
117 )
118
Jeremy Johnson1271c442023-09-05 11:39:26 +0100119 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800120 "--max-batch-size",
121 dest="max_batch_size",
122 default=1,
James Ward30124a82023-02-02 14:56:33 +0000123 type=positive_integer_type,
Kevin Cheng550ccc52021-03-03 11:21:43 -0800124 help="Maximum batch size for NHWC tests",
125 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700126
Jeremy Johnson1271c442023-09-05 11:39:26 +0100127 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800128 "--max-conv-padding",
129 dest="max_conv_padding",
130 default=1,
131 type=int,
132 help="Maximum padding for Conv tests",
133 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700134
Jeremy Johnson1271c442023-09-05 11:39:26 +0100135 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800136 "--max-conv-dilation",
137 dest="max_conv_dilation",
138 default=2,
139 type=int,
140 help="Maximum dilation for Conv tests",
141 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700142
Jeremy Johnson1271c442023-09-05 11:39:26 +0100143 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800144 "--max-conv-stride",
145 dest="max_conv_stride",
146 default=2,
147 type=int,
148 help="Maximum stride for Conv tests",
149 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700150
Jeremy Johnson1271c442023-09-05 11:39:26 +0100151 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800152 "--max-pooling-padding",
153 dest="max_pooling_padding",
154 default=1,
155 type=int,
156 help="Maximum padding for pooling tests",
157 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700158
Jeremy Johnson1271c442023-09-05 11:39:26 +0100159 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800160 "--max-pooling-stride",
161 dest="max_pooling_stride",
162 default=2,
163 type=int,
164 help="Maximum stride for pooling tests",
165 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700166
Jeremy Johnson1271c442023-09-05 11:39:26 +0100167 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800168 "--max-pooling-kernel",
169 dest="max_pooling_kernel",
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000170 default=3,
Kevin Cheng550ccc52021-03-03 11:21:43 -0800171 type=int,
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000172 help="Maximum kernel for pooling tests",
Kevin Cheng550ccc52021-03-03 11:21:43 -0800173 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700174
Jeremy Johnson1271c442023-09-05 11:39:26 +0100175 ops_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800176 "--num-rand-permutations",
177 dest="num_rand_permutations",
178 default=6,
179 type=int,
180 help="Number of random permutations for a given shape/rank for randomly-sampled parameter spaces",
181 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700182
Jeremy Johnson1271c442023-09-05 11:39:26 +0100183 ops_group.add_argument(
Jeremy Johnsona0e03f32022-06-13 17:48:09 +0100184 "--max-resize-output-dim",
185 dest="max_resize_output_dim",
186 default=1000,
187 type=int,
188 help="Upper limit on width and height output dimensions for `resize` op. Default: 1000",
189 )
190
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +0100191 # Targeting a specific shape/rank/dtype
Jeremy Johnson1271c442023-09-05 11:39:26 +0100192 tens_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800193 "--target-shape",
194 dest="target_shapes",
195 action="append",
196 default=[],
197 type=lambda x: str_to_list(x),
198 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)",
199 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700200
Jeremy Johnson1271c442023-09-05 11:39:26 +0100201 tens_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800202 "--target-rank",
203 dest="target_ranks",
204 action="append",
205 default=None,
206 type=lambda x: auto_int(x),
207 help="Create tests with a particular input tensor rank",
208 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700209
James Ward24dbc422022-10-19 12:20:31 +0100210 # Used for parsing a comma-separated list of integers in a string
Jeremy Johnson1271c442023-09-05 11:39:26 +0100211 tens_group.add_argument(
Kevin Cheng550ccc52021-03-03 11:21:43 -0800212 "--target-dtype",
213 dest="target_dtypes",
214 action="append",
215 default=None,
216 type=lambda x: dtype_str_to_val(x),
James Ward24dbc422022-10-19 12:20:31 +0100217 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 -0800218 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700219
Jeremy Johnson1271c442023-09-05 11:39:26 +0100220 ops_group.add_argument(
Matthew Haddon818ab902021-07-27 09:12:49 +0100221 "--num-const-inputs-concat",
222 dest="num_const_inputs_concat",
223 default=0,
224 choices=[0, 1, 2, 3],
225 type=int,
226 help="Allow constant input tensors for concat operator",
227 )
228
Matthew Haddon74567092021-07-16 15:38:20 +0100229 parser.add_argument(
230 "--test-type",
231 dest="test_type",
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000232 choices=["positive", "negative", "both"],
Matthew Haddon74567092021-07-16 15:38:20 +0100233 default="positive",
234 type=str,
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000235 help="type of tests produced, positive, negative, or both",
Matthew Haddon74567092021-07-16 15:38:20 +0100236 )
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000237
Jeremy Johnson1271c442023-09-05 11:39:26 +0100238 ops_group.add_argument(
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000239 "--allow-pooling-and-conv-oversizes",
240 dest="oversize",
Jeremy Johnsonae0c1c62022-02-10 17:27:34 +0000241 action="store_true",
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000242 help="allow oversize padding, stride and kernel tests",
243 )
244
Jeremy Johnson1271c442023-09-05 11:39:26 +0100245 ops_group.add_argument(
Jeremy Johnson00423432022-09-12 17:27:37 +0100246 "--zero-point",
247 dest="zeropoint",
248 default=None,
249 type=int,
250 help="set a particular zero point for all valid positive tests",
251 )
252
Jeremy Johnsona0848c62022-09-15 15:01:30 +0100253 parser.add_argument(
254 "--dump-const-tensors",
255 dest="dump_consts",
256 action="store_true",
257 help="output const tensors as numpy files for inspection",
258 )
259
Jeremy Johnson1271c442023-09-05 11:39:26 +0100260 ops_group.add_argument(
Jeremy Johnsonb2099702023-04-12 15:59:01 +0100261 "--level-8k-sizes",
262 dest="level8k",
263 action="store_true",
Jeremy Johnson1271c442023-09-05 11:39:26 +0100264 help="create level 8k size tests",
Jeremy Johnsonb2099702023-04-12 15:59:01 +0100265 )
266
Jeremy Johnson00423432022-09-12 17:27:37 +0100267 args = parser.parse_args(argv)
Eric Kunzee5e26762020-10-13 16:11:07 -0700268
269 return args
270
Eric Kunzee5e26762020-10-13 16:11:07 -0700271
James Ward30124a82023-02-02 14:56:33 +0000272def positive_integer_type(argv_str):
273 value = int(argv_str)
274 if value <= 0:
275 msg = f"{argv_str} is not a valid positive integer"
276 raise argparse.ArgumentTypeError(msg)
277 return value
278
279
Jeremy Johnson00423432022-09-12 17:27:37 +0100280def main(argv=None):
Eric Kunzee5e26762020-10-13 16:11:07 -0700281
Jeremy Johnson00423432022-09-12 17:27:37 +0100282 args = parseArgs(argv)
Eric Kunzee5e26762020-10-13 16:11:07 -0700283
Jeremy Johnson65ba8092023-10-09 16:31:13 +0100284 if not args.lazy_data_gen:
285 if args.generate_lib_path is None:
286 args.generate_lib_path = cmf.find_tosa_file(
287 cmf.TosaFileType.GENERATE_LIBRARY, Path("reference_model"), False
288 )
289 if not args.generate_lib_path.is_file():
290 print(
291 f"Argument error: Generate library (--generate-lib-path) not found - {str(args.generate_lib_path)}"
292 )
293 exit(2)
294
Eric Kunzee5e26762020-10-13 16:11:07 -0700295 ttg = TosaTestGen(args)
296
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000297 if args.test_type == "both":
298 testType = ["positive", "negative"]
Matthew Haddon1c00b712021-10-01 15:51:03 +0100299 else:
300 testType = [args.test_type]
Matthew Haddon74567092021-07-16 15:38:20 +0100301 results = []
Matthew Haddon1c00b712021-10-01 15:51:03 +0100302 for test_type in testType:
303 testList = []
Jeremy Johnson7bf0cb92023-10-31 14:37:54 +0000304 try:
305 for opName in ttg.TOSA_OP_LIST:
306 if re.match(args.filter + ".*", opName):
307 testList.extend(
308 ttg.genOpTestList(
309 opName,
310 shapeFilter=args.target_shapes,
311 rankFilter=args.target_ranks,
312 dtypeFilter=args.target_dtypes,
313 testType=test_type,
314 )
Matthew Haddon1c00b712021-10-01 15:51:03 +0100315 )
Jeremy Johnson7bf0cb92023-10-31 14:37:54 +0000316 except Exception as e:
317 print(f"INTERNAL ERROR: Failure generating test lists for {opName}")
318 raise e
Matthew Haddon848efb42021-09-09 12:30:53 +0100319
Matthew Haddon1c00b712021-10-01 15:51:03 +0100320 print("{} matching {} tests".format(len(testList), test_type))
321
322 testStrings = []
Jeremy Johnson7bf0cb92023-10-31 14:37:54 +0000323 try:
324 for opName, testStr, dtype, error, shapeList, testArgs in testList:
325 # Check for and skip duplicate tests
326 if testStr in testStrings:
327 print(f"Skipping duplicate test: {testStr}")
328 continue
329 else:
330 testStrings.append(testStr)
Matthew Haddon1c00b712021-10-01 15:51:03 +0100331
Jeremy Johnson7bf0cb92023-10-31 14:37:54 +0000332 results.append(
333 ttg.serializeTest(
334 opName, testStr, dtype, error, shapeList, testArgs
335 )
336 )
337 except Exception as e:
338 print(f"INTERNAL ERROR: Failure creating test output for {opName}")
339 raise e
Matthew Haddon74567092021-07-16 15:38:20 +0100340
341 print(f"Done creating {len(results)} tests")
342
Eric Kunzee5e26762020-10-13 16:11:07 -0700343
Kevin Cheng550ccc52021-03-03 11:21:43 -0800344if __name__ == "__main__":
Eric Kunzee5e26762020-10-13 16:11:07 -0700345 exit(main())