blob: ab78b1aa6912d602fa09b44c52f99af56ee7f312 [file] [log] [blame]
Jeremy Johnson5c1364c2022-01-13 15:04:21 +00001# Copyright (c) 2020-2022, ARM Limited.
2# SPDX-License-Identifier: Apache-2.0
Eric Kunzee5e26762020-10-13 16:11:07 -07003import argparse
Eric Kunzee5e26762020-10-13 16:11:07 -07004import re
Eric Kunzee5e26762020-10-13 16:11:07 -07005
Jeremy Johnson2ec34942021-12-14 16:34:05 +00006from generator.tosa_test_gen import TosaTestGen
7from serializer.tosa_serializer import dtype_str_to_val
James Ward24dbc422022-10-19 12:20:31 +01008from serializer.tosa_serializer import DTypeNames
Eric Kunzee5e26762020-10-13 16:11:07 -07009
Jeremy Johnson5c1364c2022-01-13 15:04:21 +000010
Eric Kunzee5e26762020-10-13 16:11:07 -070011# Used for parsing a comma-separated list of integers in a string
12# to an actual list of integers
13def str_to_list(in_s):
Kevin Cheng550ccc52021-03-03 11:21:43 -080014 """Converts a comma-separated list of string integers to a python list of ints"""
15 lst = in_s.split(",")
Eric Kunzee5e26762020-10-13 16:11:07 -070016 out_list = []
17 for i in lst:
18 out_list.append(int(i))
19 return out_list
20
Kevin Cheng550ccc52021-03-03 11:21:43 -080021
Eric Kunzee5e26762020-10-13 16:11:07 -070022def auto_int(x):
Kevin Cheng550ccc52021-03-03 11:21:43 -080023 """Converts hex/dec argument values to an int"""
Eric Kunzee5e26762020-10-13 16:11:07 -070024 return int(x, 0)
25
Kevin Cheng550ccc52021-03-03 11:21:43 -080026
Jeremy Johnson00423432022-09-12 17:27:37 +010027def parseArgs(argv):
Eric Kunzee5e26762020-10-13 16:11:07 -070028
29 parser = argparse.ArgumentParser()
Kevin Cheng550ccc52021-03-03 11:21:43 -080030 parser.add_argument(
31 "-o", dest="output_dir", type=str, default="vtest", help="Test output directory"
32 )
Eric Kunzee5e26762020-10-13 16:11:07 -070033
Kevin Cheng550ccc52021-03-03 11:21:43 -080034 parser.add_argument(
35 "--seed",
36 dest="random_seed",
37 default=42,
38 type=int,
39 help="Random seed for test generation",
40 )
Eric Kunzee5e26762020-10-13 16:11:07 -070041
Kevin Cheng550ccc52021-03-03 11:21:43 -080042 parser.add_argument(
43 "--filter",
44 dest="filter",
45 default="",
46 type=str,
47 help="Filter operator test names by this expression",
48 )
Eric Kunzee5e26762020-10-13 16:11:07 -070049
Kevin Cheng550ccc52021-03-03 11:21:43 -080050 parser.add_argument(
51 "-v", "--verbose", dest="verbose", action="count", help="Verbose operation"
52 )
Eric Kunzee5e26762020-10-13 16:11:07 -070053
54 # Constraints on tests
Kevin Cheng550ccc52021-03-03 11:21:43 -080055 parser.add_argument(
56 "--tensor-dim-range",
57 dest="tensor_shape_range",
58 default="1,64",
59 type=lambda x: str_to_list(x),
60 help="Min,Max range of tensor shapes",
61 )
Eric Kunzee5e26762020-10-13 16:11:07 -070062
Kevin Cheng550ccc52021-03-03 11:21:43 -080063 parser.add_argument(
64 "--max-batch-size",
65 dest="max_batch_size",
66 default=1,
67 type=int,
68 help="Maximum batch size for NHWC tests",
69 )
Eric Kunzee5e26762020-10-13 16:11:07 -070070
Kevin Cheng550ccc52021-03-03 11:21:43 -080071 parser.add_argument(
72 "--max-conv-padding",
73 dest="max_conv_padding",
74 default=1,
75 type=int,
76 help="Maximum padding for Conv tests",
77 )
Eric Kunzee5e26762020-10-13 16:11:07 -070078
Kevin Cheng550ccc52021-03-03 11:21:43 -080079 parser.add_argument(
80 "--max-conv-dilation",
81 dest="max_conv_dilation",
82 default=2,
83 type=int,
84 help="Maximum dilation for Conv tests",
85 )
Eric Kunzee5e26762020-10-13 16:11:07 -070086
Kevin Cheng550ccc52021-03-03 11:21:43 -080087 parser.add_argument(
88 "--max-conv-stride",
89 dest="max_conv_stride",
90 default=2,
91 type=int,
92 help="Maximum stride for Conv tests",
93 )
Eric Kunzee5e26762020-10-13 16:11:07 -070094
Kevin Cheng550ccc52021-03-03 11:21:43 -080095 parser.add_argument(
96 "--max-pooling-padding",
97 dest="max_pooling_padding",
98 default=1,
99 type=int,
100 help="Maximum padding for pooling tests",
101 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700102
Kevin Cheng550ccc52021-03-03 11:21:43 -0800103 parser.add_argument(
104 "--max-pooling-stride",
105 dest="max_pooling_stride",
106 default=2,
107 type=int,
108 help="Maximum stride for pooling tests",
109 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700110
Kevin Cheng550ccc52021-03-03 11:21:43 -0800111 parser.add_argument(
112 "--max-pooling-kernel",
113 dest="max_pooling_kernel",
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000114 default=3,
Kevin Cheng550ccc52021-03-03 11:21:43 -0800115 type=int,
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000116 help="Maximum kernel for pooling tests",
Kevin Cheng550ccc52021-03-03 11:21:43 -0800117 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700118
Kevin Cheng550ccc52021-03-03 11:21:43 -0800119 parser.add_argument(
120 "--num-rand-permutations",
121 dest="num_rand_permutations",
122 default=6,
123 type=int,
124 help="Number of random permutations for a given shape/rank for randomly-sampled parameter spaces",
125 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700126
Jeremy Johnsona0e03f32022-06-13 17:48:09 +0100127 parser.add_argument(
128 "--max-resize-output-dim",
129 dest="max_resize_output_dim",
130 default=1000,
131 type=int,
132 help="Upper limit on width and height output dimensions for `resize` op. Default: 1000",
133 )
134
Eric Kunzee5e26762020-10-13 16:11:07 -0700135 # Targetting a specific shape/rank/dtype
Kevin Cheng550ccc52021-03-03 11:21:43 -0800136 parser.add_argument(
137 "--target-shape",
138 dest="target_shapes",
139 action="append",
140 default=[],
141 type=lambda x: str_to_list(x),
142 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)",
143 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700144
Kevin Cheng550ccc52021-03-03 11:21:43 -0800145 parser.add_argument(
146 "--target-rank",
147 dest="target_ranks",
148 action="append",
149 default=None,
150 type=lambda x: auto_int(x),
151 help="Create tests with a particular input tensor rank",
152 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700153
James Ward24dbc422022-10-19 12:20:31 +0100154 # Used for parsing a comma-separated list of integers in a string
Kevin Cheng550ccc52021-03-03 11:21:43 -0800155 parser.add_argument(
156 "--target-dtype",
157 dest="target_dtypes",
158 action="append",
159 default=None,
160 type=lambda x: dtype_str_to_val(x),
James Ward24dbc422022-10-19 12:20:31 +0100161 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 -0800162 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700163
Matthew Haddon818ab902021-07-27 09:12:49 +0100164 parser.add_argument(
165 "--num-const-inputs-concat",
166 dest="num_const_inputs_concat",
167 default=0,
168 choices=[0, 1, 2, 3],
169 type=int,
170 help="Allow constant input tensors for concat operator",
171 )
172
Matthew Haddon74567092021-07-16 15:38:20 +0100173 parser.add_argument(
174 "--test-type",
175 dest="test_type",
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000176 choices=["positive", "negative", "both"],
Matthew Haddon74567092021-07-16 15:38:20 +0100177 default="positive",
178 type=str,
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000179 help="type of tests produced, positive, negative, or both",
Matthew Haddon74567092021-07-16 15:38:20 +0100180 )
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000181
182 parser.add_argument(
183 "--allow-pooling-and-conv-oversizes",
184 dest="oversize",
Jeremy Johnsonae0c1c62022-02-10 17:27:34 +0000185 action="store_true",
Jeremy Johnson39f127b2022-01-25 17:51:26 +0000186 help="allow oversize padding, stride and kernel tests",
187 )
188
Jeremy Johnson00423432022-09-12 17:27:37 +0100189 parser.add_argument(
190 "--zero-point",
191 dest="zeropoint",
192 default=None,
193 type=int,
194 help="set a particular zero point for all valid positive tests",
195 )
196
Jeremy Johnsona0848c62022-09-15 15:01:30 +0100197 parser.add_argument(
198 "--dump-const-tensors",
199 dest="dump_consts",
200 action="store_true",
201 help="output const tensors as numpy files for inspection",
202 )
203
Jeremy Johnson00423432022-09-12 17:27:37 +0100204 args = parser.parse_args(argv)
Eric Kunzee5e26762020-10-13 16:11:07 -0700205
206 return args
207
Eric Kunzee5e26762020-10-13 16:11:07 -0700208
Jeremy Johnson00423432022-09-12 17:27:37 +0100209def main(argv=None):
Eric Kunzee5e26762020-10-13 16:11:07 -0700210
Jeremy Johnson00423432022-09-12 17:27:37 +0100211 args = parseArgs(argv)
Eric Kunzee5e26762020-10-13 16:11:07 -0700212
213 ttg = TosaTestGen(args)
214
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000215 if args.test_type == "both":
216 testType = ["positive", "negative"]
Matthew Haddon1c00b712021-10-01 15:51:03 +0100217 else:
218 testType = [args.test_type]
Matthew Haddon74567092021-07-16 15:38:20 +0100219 results = []
Matthew Haddon1c00b712021-10-01 15:51:03 +0100220 for test_type in testType:
221 testList = []
222 for op in ttg.TOSA_OP_LIST:
223 if re.match(args.filter + ".*", op):
224 testList.extend(
225 ttg.genOpTestList(
226 op,
227 shapeFilter=args.target_shapes,
228 rankFilter=args.target_ranks,
229 dtypeFilter=args.target_dtypes,
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000230 testType=test_type,
Matthew Haddon1c00b712021-10-01 15:51:03 +0100231 )
232 )
Matthew Haddon848efb42021-09-09 12:30:53 +0100233
Matthew Haddon1c00b712021-10-01 15:51:03 +0100234 print("{} matching {} tests".format(len(testList), test_type))
235
236 testStrings = []
237 for opName, testStr, dtype, error, shapeList, testArgs in testList:
238 # Check for and skip duplicate tests
239 if testStr in testStrings:
Jeremy Johnsona0e03f32022-06-13 17:48:09 +0100240 print(f"Skipping duplicate test: {testStr}")
Matthew Haddon1c00b712021-10-01 15:51:03 +0100241 continue
242 else:
243 testStrings.append(testStr)
244
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000245 results.append(
246 ttg.serializeTest(opName, testStr, dtype, error, shapeList, testArgs)
247 )
Matthew Haddon74567092021-07-16 15:38:20 +0100248
249 print(f"Done creating {len(results)} tests")
250
Eric Kunzee5e26762020-10-13 16:11:07 -0700251
Kevin Cheng550ccc52021-03-03 11:21:43 -0800252if __name__ == "__main__":
Eric Kunzee5e26762020-10-13 16:11:07 -0700253 exit(main())