blob: 09ee23847b6b4466a57ed01e2ed5d9f3a9958ce1 [file] [log] [blame]
Jeremy Johnson2ec34942021-12-14 16:34:05 +00001# Copyright (c) 2020-2021, ARM Limited.
Eric Kunzee5e26762020-10-13 16:11:07 -07002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
16import argparse
17import sys
18import re
19import os
20import subprocess
21import shlex
22import json
23import glob
24import math
25import queue
26import threading
27import traceback
28
29
30from enum import IntEnum, Enum, unique
31from datetime import datetime
32
Jeremy Johnson2ec34942021-12-14 16:34:05 +000033from generator.tosa_test_gen import TosaTestGen
34from serializer.tosa_serializer import dtype_str_to_val
Eric Kunzee5e26762020-10-13 16:11:07 -070035
36# Used for parsing a comma-separated list of integers in a string
37# to an actual list of integers
38def str_to_list(in_s):
Kevin Cheng550ccc52021-03-03 11:21:43 -080039 """Converts a comma-separated list of string integers to a python list of ints"""
40 lst = in_s.split(",")
Eric Kunzee5e26762020-10-13 16:11:07 -070041 out_list = []
42 for i in lst:
43 out_list.append(int(i))
44 return out_list
45
Kevin Cheng550ccc52021-03-03 11:21:43 -080046
Eric Kunzee5e26762020-10-13 16:11:07 -070047def auto_int(x):
Kevin Cheng550ccc52021-03-03 11:21:43 -080048 """Converts hex/dec argument values to an int"""
Eric Kunzee5e26762020-10-13 16:11:07 -070049 return int(x, 0)
50
Kevin Cheng550ccc52021-03-03 11:21:43 -080051
Eric Kunzee5e26762020-10-13 16:11:07 -070052def parseArgs():
53
54 parser = argparse.ArgumentParser()
Kevin Cheng550ccc52021-03-03 11:21:43 -080055 parser.add_argument(
56 "-o", dest="output_dir", type=str, default="vtest", help="Test output directory"
57 )
Eric Kunzee5e26762020-10-13 16:11:07 -070058
Kevin Cheng550ccc52021-03-03 11:21:43 -080059 parser.add_argument(
60 "--seed",
61 dest="random_seed",
62 default=42,
63 type=int,
64 help="Random seed for test generation",
65 )
Eric Kunzee5e26762020-10-13 16:11:07 -070066
Kevin Cheng550ccc52021-03-03 11:21:43 -080067 parser.add_argument(
68 "--filter",
69 dest="filter",
70 default="",
71 type=str,
72 help="Filter operator test names by this expression",
73 )
Eric Kunzee5e26762020-10-13 16:11:07 -070074
Kevin Cheng550ccc52021-03-03 11:21:43 -080075 parser.add_argument(
76 "-v", "--verbose", dest="verbose", action="count", help="Verbose operation"
77 )
Eric Kunzee5e26762020-10-13 16:11:07 -070078
79 # Constraints on tests
Kevin Cheng550ccc52021-03-03 11:21:43 -080080 parser.add_argument(
81 "--tensor-dim-range",
82 dest="tensor_shape_range",
83 default="1,64",
84 type=lambda x: str_to_list(x),
85 help="Min,Max range of tensor shapes",
86 )
Eric Kunzee5e26762020-10-13 16:11:07 -070087
Kevin Cheng550ccc52021-03-03 11:21:43 -080088 parser.add_argument(
89 "--max-batch-size",
90 dest="max_batch_size",
91 default=1,
92 type=int,
93 help="Maximum batch size for NHWC tests",
94 )
Eric Kunzee5e26762020-10-13 16:11:07 -070095
Kevin Cheng550ccc52021-03-03 11:21:43 -080096 parser.add_argument(
97 "--max-conv-padding",
98 dest="max_conv_padding",
99 default=1,
100 type=int,
101 help="Maximum padding for Conv tests",
102 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700103
Kevin Cheng550ccc52021-03-03 11:21:43 -0800104 parser.add_argument(
105 "--max-conv-dilation",
106 dest="max_conv_dilation",
107 default=2,
108 type=int,
109 help="Maximum dilation for Conv tests",
110 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700111
Kevin Cheng550ccc52021-03-03 11:21:43 -0800112 parser.add_argument(
113 "--max-conv-stride",
114 dest="max_conv_stride",
115 default=2,
116 type=int,
117 help="Maximum stride for Conv tests",
118 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700119
Kevin Cheng550ccc52021-03-03 11:21:43 -0800120 parser.add_argument(
121 "--max-pooling-padding",
122 dest="max_pooling_padding",
123 default=1,
124 type=int,
125 help="Maximum padding for pooling tests",
126 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700127
Kevin Cheng550ccc52021-03-03 11:21:43 -0800128 parser.add_argument(
129 "--max-pooling-stride",
130 dest="max_pooling_stride",
131 default=2,
132 type=int,
133 help="Maximum stride for pooling tests",
134 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700135
Kevin Cheng550ccc52021-03-03 11:21:43 -0800136 parser.add_argument(
137 "--max-pooling-kernel",
138 dest="max_pooling_kernel",
139 default=2,
140 type=int,
141 help="Maximum padding for pooling tests",
142 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700143
Kevin Cheng550ccc52021-03-03 11:21:43 -0800144 parser.add_argument(
145 "--num-rand-permutations",
146 dest="num_rand_permutations",
147 default=6,
148 type=int,
149 help="Number of random permutations for a given shape/rank for randomly-sampled parameter spaces",
150 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700151
152 # Targetting a specific shape/rank/dtype
Kevin Cheng550ccc52021-03-03 11:21:43 -0800153 parser.add_argument(
154 "--target-shape",
155 dest="target_shapes",
156 action="append",
157 default=[],
158 type=lambda x: str_to_list(x),
159 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)",
160 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700161
Kevin Cheng550ccc52021-03-03 11:21:43 -0800162 parser.add_argument(
163 "--target-rank",
164 dest="target_ranks",
165 action="append",
166 default=None,
167 type=lambda x: auto_int(x),
168 help="Create tests with a particular input tensor rank",
169 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700170
Kevin Cheng550ccc52021-03-03 11:21:43 -0800171 parser.add_argument(
172 "--target-dtype",
173 dest="target_dtypes",
174 action="append",
175 default=None,
176 type=lambda x: dtype_str_to_val(x),
177 help="Create test with a particular DType (may be repeated)",
178 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700179
Matthew Haddon818ab902021-07-27 09:12:49 +0100180 parser.add_argument(
181 "--num-const-inputs-concat",
182 dest="num_const_inputs_concat",
183 default=0,
184 choices=[0, 1, 2, 3],
185 type=int,
186 help="Allow constant input tensors for concat operator",
187 )
188
Matthew Haddon74567092021-07-16 15:38:20 +0100189 parser.add_argument(
190 "--test-type",
191 dest="test_type",
192 choices=['positive', 'negative', 'both'],
193 default="positive",
194 type=str,
195 help="type of tests produced, postive, negative, or both",
196 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700197 args = parser.parse_args()
198
199 return args
200
Eric Kunzee5e26762020-10-13 16:11:07 -0700201
Kevin Cheng550ccc52021-03-03 11:21:43 -0800202def main():
Eric Kunzee5e26762020-10-13 16:11:07 -0700203
204 args = parseArgs()
205
206 ttg = TosaTestGen(args)
207
Matthew Haddon1c00b712021-10-01 15:51:03 +0100208 if args.test_type == 'both':
209 testType = ['positive', 'negative']
210 else:
211 testType = [args.test_type]
Matthew Haddon74567092021-07-16 15:38:20 +0100212 results = []
Matthew Haddon1c00b712021-10-01 15:51:03 +0100213 for test_type in testType:
214 testList = []
215 for op in ttg.TOSA_OP_LIST:
216 if re.match(args.filter + ".*", op):
217 testList.extend(
218 ttg.genOpTestList(
219 op,
220 shapeFilter=args.target_shapes,
221 rankFilter=args.target_ranks,
222 dtypeFilter=args.target_dtypes,
223 testType=test_type
224 )
225 )
Matthew Haddon848efb42021-09-09 12:30:53 +0100226
Matthew Haddon1c00b712021-10-01 15:51:03 +0100227 print("{} matching {} tests".format(len(testList), test_type))
228
229 testStrings = []
230 for opName, testStr, dtype, error, shapeList, testArgs in testList:
231 # Check for and skip duplicate tests
232 if testStr in testStrings:
233 continue
234 else:
235 testStrings.append(testStr)
236
237 if args.verbose:
238 print(testStr)
239 results.append(ttg.serializeTest(opName, testStr, dtype, error, shapeList, testArgs))
Matthew Haddon74567092021-07-16 15:38:20 +0100240
241 print(f"Done creating {len(results)} tests")
242
Eric Kunzee5e26762020-10-13 16:11:07 -0700243
244
Kevin Cheng550ccc52021-03-03 11:21:43 -0800245if __name__ == "__main__":
Eric Kunzee5e26762020-10-13 16:11:07 -0700246 exit(main())