blob: 50f4033dadd031f179723da35f172c3074d84a4d [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
Eric Kunzee5e26762020-10-13 16:11:07 -07008
Jeremy Johnson5c1364c2022-01-13 15:04:21 +00009
Eric Kunzee5e26762020-10-13 16:11:07 -070010# Used for parsing a comma-separated list of integers in a string
11# to an actual list of integers
12def str_to_list(in_s):
Kevin Cheng550ccc52021-03-03 11:21:43 -080013 """Converts a comma-separated list of string integers to a python list of ints"""
14 lst = in_s.split(",")
Eric Kunzee5e26762020-10-13 16:11:07 -070015 out_list = []
16 for i in lst:
17 out_list.append(int(i))
18 return out_list
19
Kevin Cheng550ccc52021-03-03 11:21:43 -080020
Eric Kunzee5e26762020-10-13 16:11:07 -070021def auto_int(x):
Kevin Cheng550ccc52021-03-03 11:21:43 -080022 """Converts hex/dec argument values to an int"""
Eric Kunzee5e26762020-10-13 16:11:07 -070023 return int(x, 0)
24
Kevin Cheng550ccc52021-03-03 11:21:43 -080025
Eric Kunzee5e26762020-10-13 16:11:07 -070026def parseArgs():
27
28 parser = argparse.ArgumentParser()
Kevin Cheng550ccc52021-03-03 11:21:43 -080029 parser.add_argument(
30 "-o", dest="output_dir", type=str, default="vtest", help="Test output directory"
31 )
Eric Kunzee5e26762020-10-13 16:11:07 -070032
Kevin Cheng550ccc52021-03-03 11:21:43 -080033 parser.add_argument(
34 "--seed",
35 dest="random_seed",
36 default=42,
37 type=int,
38 help="Random seed for test generation",
39 )
Eric Kunzee5e26762020-10-13 16:11:07 -070040
Kevin Cheng550ccc52021-03-03 11:21:43 -080041 parser.add_argument(
42 "--filter",
43 dest="filter",
44 default="",
45 type=str,
46 help="Filter operator test names by this expression",
47 )
Eric Kunzee5e26762020-10-13 16:11:07 -070048
Kevin Cheng550ccc52021-03-03 11:21:43 -080049 parser.add_argument(
50 "-v", "--verbose", dest="verbose", action="count", help="Verbose operation"
51 )
Eric Kunzee5e26762020-10-13 16:11:07 -070052
53 # Constraints on tests
Kevin Cheng550ccc52021-03-03 11:21:43 -080054 parser.add_argument(
55 "--tensor-dim-range",
56 dest="tensor_shape_range",
57 default="1,64",
58 type=lambda x: str_to_list(x),
59 help="Min,Max range of tensor shapes",
60 )
Eric Kunzee5e26762020-10-13 16:11:07 -070061
Kevin Cheng550ccc52021-03-03 11:21:43 -080062 parser.add_argument(
63 "--max-batch-size",
64 dest="max_batch_size",
65 default=1,
66 type=int,
67 help="Maximum batch size for NHWC tests",
68 )
Eric Kunzee5e26762020-10-13 16:11:07 -070069
Kevin Cheng550ccc52021-03-03 11:21:43 -080070 parser.add_argument(
71 "--max-conv-padding",
72 dest="max_conv_padding",
73 default=1,
74 type=int,
75 help="Maximum padding for Conv tests",
76 )
Eric Kunzee5e26762020-10-13 16:11:07 -070077
Kevin Cheng550ccc52021-03-03 11:21:43 -080078 parser.add_argument(
79 "--max-conv-dilation",
80 dest="max_conv_dilation",
81 default=2,
82 type=int,
83 help="Maximum dilation for Conv tests",
84 )
Eric Kunzee5e26762020-10-13 16:11:07 -070085
Kevin Cheng550ccc52021-03-03 11:21:43 -080086 parser.add_argument(
87 "--max-conv-stride",
88 dest="max_conv_stride",
89 default=2,
90 type=int,
91 help="Maximum stride for Conv tests",
92 )
Eric Kunzee5e26762020-10-13 16:11:07 -070093
Kevin Cheng550ccc52021-03-03 11:21:43 -080094 parser.add_argument(
95 "--max-pooling-padding",
96 dest="max_pooling_padding",
97 default=1,
98 type=int,
99 help="Maximum padding for pooling tests",
100 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700101
Kevin Cheng550ccc52021-03-03 11:21:43 -0800102 parser.add_argument(
103 "--max-pooling-stride",
104 dest="max_pooling_stride",
105 default=2,
106 type=int,
107 help="Maximum stride for pooling tests",
108 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700109
Kevin Cheng550ccc52021-03-03 11:21:43 -0800110 parser.add_argument(
111 "--max-pooling-kernel",
112 dest="max_pooling_kernel",
113 default=2,
114 type=int,
115 help="Maximum padding for pooling tests",
116 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700117
Kevin Cheng550ccc52021-03-03 11:21:43 -0800118 parser.add_argument(
119 "--num-rand-permutations",
120 dest="num_rand_permutations",
121 default=6,
122 type=int,
123 help="Number of random permutations for a given shape/rank for randomly-sampled parameter spaces",
124 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700125
126 # Targetting a specific shape/rank/dtype
Kevin Cheng550ccc52021-03-03 11:21:43 -0800127 parser.add_argument(
128 "--target-shape",
129 dest="target_shapes",
130 action="append",
131 default=[],
132 type=lambda x: str_to_list(x),
133 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)",
134 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700135
Kevin Cheng550ccc52021-03-03 11:21:43 -0800136 parser.add_argument(
137 "--target-rank",
138 dest="target_ranks",
139 action="append",
140 default=None,
141 type=lambda x: auto_int(x),
142 help="Create tests with a particular input tensor rank",
143 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700144
Kevin Cheng550ccc52021-03-03 11:21:43 -0800145 parser.add_argument(
146 "--target-dtype",
147 dest="target_dtypes",
148 action="append",
149 default=None,
150 type=lambda x: dtype_str_to_val(x),
151 help="Create test with a particular DType (may be repeated)",
152 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700153
Matthew Haddon818ab902021-07-27 09:12:49 +0100154 parser.add_argument(
155 "--num-const-inputs-concat",
156 dest="num_const_inputs_concat",
157 default=0,
158 choices=[0, 1, 2, 3],
159 type=int,
160 help="Allow constant input tensors for concat operator",
161 )
162
Matthew Haddon74567092021-07-16 15:38:20 +0100163 parser.add_argument(
164 "--test-type",
165 dest="test_type",
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000166 choices=["positive", "negative", "both"],
Matthew Haddon74567092021-07-16 15:38:20 +0100167 default="positive",
168 type=str,
169 help="type of tests produced, postive, negative, or both",
170 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700171 args = parser.parse_args()
172
173 return args
174
Eric Kunzee5e26762020-10-13 16:11:07 -0700175
Kevin Cheng550ccc52021-03-03 11:21:43 -0800176def main():
Eric Kunzee5e26762020-10-13 16:11:07 -0700177
178 args = parseArgs()
179
180 ttg = TosaTestGen(args)
181
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000182 if args.test_type == "both":
183 testType = ["positive", "negative"]
Matthew Haddon1c00b712021-10-01 15:51:03 +0100184 else:
185 testType = [args.test_type]
Matthew Haddon74567092021-07-16 15:38:20 +0100186 results = []
Matthew Haddon1c00b712021-10-01 15:51:03 +0100187 for test_type in testType:
188 testList = []
189 for op in ttg.TOSA_OP_LIST:
190 if re.match(args.filter + ".*", op):
191 testList.extend(
192 ttg.genOpTestList(
193 op,
194 shapeFilter=args.target_shapes,
195 rankFilter=args.target_ranks,
196 dtypeFilter=args.target_dtypes,
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000197 testType=test_type,
Matthew Haddon1c00b712021-10-01 15:51:03 +0100198 )
199 )
Matthew Haddon848efb42021-09-09 12:30:53 +0100200
Matthew Haddon1c00b712021-10-01 15:51:03 +0100201 print("{} matching {} tests".format(len(testList), test_type))
202
203 testStrings = []
204 for opName, testStr, dtype, error, shapeList, testArgs in testList:
205 # Check for and skip duplicate tests
206 if testStr in testStrings:
207 continue
208 else:
209 testStrings.append(testStr)
210
211 if args.verbose:
212 print(testStr)
Jeremy Johnson5c1364c2022-01-13 15:04:21 +0000213 results.append(
214 ttg.serializeTest(opName, testStr, dtype, error, shapeList, testArgs)
215 )
Matthew Haddon74567092021-07-16 15:38:20 +0100216
217 print(f"Done creating {len(results)} tests")
218
Eric Kunzee5e26762020-10-13 16:11:07 -0700219
Kevin Cheng550ccc52021-03-03 11:21:43 -0800220if __name__ == "__main__":
Eric Kunzee5e26762020-10-13 16:11:07 -0700221 exit(main())