blob: c667e79a968480815f7fab4de228e739ef98c6b8 [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001#!/usr/bin/env python3
2
3# Copyright (c) 2020, ARM Limited.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
18import argparse
19import sys
20import re
21import os
22import subprocess
23import shlex
24import json
25import glob
26import math
27import queue
28import threading
29import traceback
30
31
32from enum import IntEnum, Enum, unique
33from datetime import datetime
34
Kevin Cheng550ccc52021-03-03 11:21:43 -080035# Include the ../scripts and ../scripts/xunit directory in PYTHONPATH
Eric Kunzee5e26762020-10-13 16:11:07 -070036parent_dir = os.path.dirname(os.path.realpath(__file__))
Kevin Cheng550ccc52021-03-03 11:21:43 -080037sys.path.append(os.path.join(parent_dir, "..", "scripts"))
38sys.path.append(os.path.join(parent_dir, "..", "scripts", "xunit"))
39
Eric Kunzee5e26762020-10-13 16:11:07 -070040import xunit
Kevin Cheng550ccc52021-03-03 11:21:43 -080041
42# Include the ../thirdparty/serialization_lib/python directory in PYTHONPATH
43parent_dir = os.path.dirname(os.path.realpath(__file__))
44sys.path.append(
45 os.path.join(parent_dir, "..", "thirdparty", "serialization_lib", "python")
46)
Eric Kunzee5e26762020-10-13 16:11:07 -070047from tosa_serializer import *
48from tosa_test_gen import TosaTestGen
49import tosa
50
51# Used for parsing a comma-separated list of integers in a string
52# to an actual list of integers
53def str_to_list(in_s):
Kevin Cheng550ccc52021-03-03 11:21:43 -080054 """Converts a comma-separated list of string integers to a python list of ints"""
55 lst = in_s.split(",")
Eric Kunzee5e26762020-10-13 16:11:07 -070056 out_list = []
57 for i in lst:
58 out_list.append(int(i))
59 return out_list
60
Kevin Cheng550ccc52021-03-03 11:21:43 -080061
Eric Kunzee5e26762020-10-13 16:11:07 -070062def auto_int(x):
Kevin Cheng550ccc52021-03-03 11:21:43 -080063 """Converts hex/dec argument values to an int"""
Eric Kunzee5e26762020-10-13 16:11:07 -070064 return int(x, 0)
65
Kevin Cheng550ccc52021-03-03 11:21:43 -080066
Eric Kunzee5e26762020-10-13 16:11:07 -070067def parseArgs():
68
69 parser = argparse.ArgumentParser()
Kevin Cheng550ccc52021-03-03 11:21:43 -080070 parser.add_argument(
71 "-o", dest="output_dir", type=str, default="vtest", help="Test output directory"
72 )
Eric Kunzee5e26762020-10-13 16:11:07 -070073
Kevin Cheng550ccc52021-03-03 11:21:43 -080074 parser.add_argument(
75 "--seed",
76 dest="random_seed",
77 default=42,
78 type=int,
79 help="Random seed for test generation",
80 )
Eric Kunzee5e26762020-10-13 16:11:07 -070081
Kevin Cheng550ccc52021-03-03 11:21:43 -080082 parser.add_argument(
83 "--filter",
84 dest="filter",
85 default="",
86 type=str,
87 help="Filter operator test names by this expression",
88 )
Eric Kunzee5e26762020-10-13 16:11:07 -070089
Kevin Cheng550ccc52021-03-03 11:21:43 -080090 parser.add_argument(
91 "-v", "--verbose", dest="verbose", action="count", help="Verbose operation"
92 )
Eric Kunzee5e26762020-10-13 16:11:07 -070093
94 # Constraints on tests
Kevin Cheng550ccc52021-03-03 11:21:43 -080095 parser.add_argument(
96 "--tensor-dim-range",
97 dest="tensor_shape_range",
98 default="1,64",
99 type=lambda x: str_to_list(x),
100 help="Min,Max range of tensor shapes",
101 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700102
Kevin Cheng550ccc52021-03-03 11:21:43 -0800103 parser.add_argument(
104 "--max-batch-size",
105 dest="max_batch_size",
106 default=1,
107 type=int,
108 help="Maximum batch size for NHWC tests",
109 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700110
Kevin Cheng550ccc52021-03-03 11:21:43 -0800111 parser.add_argument(
112 "--max-conv-padding",
113 dest="max_conv_padding",
114 default=1,
115 type=int,
116 help="Maximum padding for Conv tests",
117 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700118
Kevin Cheng550ccc52021-03-03 11:21:43 -0800119 parser.add_argument(
120 "--max-conv-dilation",
121 dest="max_conv_dilation",
122 default=2,
123 type=int,
124 help="Maximum dilation for Conv tests",
125 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700126
Kevin Cheng550ccc52021-03-03 11:21:43 -0800127 parser.add_argument(
128 "--max-conv-stride",
129 dest="max_conv_stride",
130 default=2,
131 type=int,
132 help="Maximum stride for Conv tests",
133 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700134
Kevin Cheng550ccc52021-03-03 11:21:43 -0800135 parser.add_argument(
136 "--max-pooling-padding",
137 dest="max_pooling_padding",
138 default=1,
139 type=int,
140 help="Maximum padding for pooling tests",
141 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700142
Kevin Cheng550ccc52021-03-03 11:21:43 -0800143 parser.add_argument(
144 "--max-pooling-stride",
145 dest="max_pooling_stride",
146 default=2,
147 type=int,
148 help="Maximum stride for pooling tests",
149 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700150
Kevin Cheng550ccc52021-03-03 11:21:43 -0800151 parser.add_argument(
152 "--max-pooling-kernel",
153 dest="max_pooling_kernel",
154 default=2,
155 type=int,
156 help="Maximum padding for pooling tests",
157 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700158
Kevin Cheng550ccc52021-03-03 11:21:43 -0800159 parser.add_argument(
160 "--num-rand-permutations",
161 dest="num_rand_permutations",
162 default=6,
163 type=int,
164 help="Number of random permutations for a given shape/rank for randomly-sampled parameter spaces",
165 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700166
167 # Targetting a specific shape/rank/dtype
Kevin Cheng550ccc52021-03-03 11:21:43 -0800168 parser.add_argument(
169 "--target-shape",
170 dest="target_shapes",
171 action="append",
172 default=[],
173 type=lambda x: str_to_list(x),
174 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)",
175 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700176
Kevin Cheng550ccc52021-03-03 11:21:43 -0800177 parser.add_argument(
178 "--target-rank",
179 dest="target_ranks",
180 action="append",
181 default=None,
182 type=lambda x: auto_int(x),
183 help="Create tests with a particular input tensor rank",
184 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700185
Kevin Cheng550ccc52021-03-03 11:21:43 -0800186 parser.add_argument(
187 "--target-dtype",
188 dest="target_dtypes",
189 action="append",
190 default=None,
191 type=lambda x: dtype_str_to_val(x),
192 help="Create test with a particular DType (may be repeated)",
193 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700194
Matthew Haddon818ab902021-07-27 09:12:49 +0100195 parser.add_argument(
196 "--num-const-inputs-concat",
197 dest="num_const_inputs_concat",
198 default=0,
199 choices=[0, 1, 2, 3],
200 type=int,
201 help="Allow constant input tensors for concat operator",
202 )
203
Matthew Haddon74567092021-07-16 15:38:20 +0100204 parser.add_argument(
205 "--test-type",
206 dest="test_type",
207 choices=['positive', 'negative', 'both'],
208 default="positive",
209 type=str,
210 help="type of tests produced, postive, negative, or both",
211 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700212 args = parser.parse_args()
213
214 return args
215
Eric Kunzee5e26762020-10-13 16:11:07 -0700216
Kevin Cheng550ccc52021-03-03 11:21:43 -0800217def main():
Eric Kunzee5e26762020-10-13 16:11:07 -0700218
219 args = parseArgs()
220
221 ttg = TosaTestGen(args)
222
Matthew Haddon1c00b712021-10-01 15:51:03 +0100223 if args.test_type == 'both':
224 testType = ['positive', 'negative']
225 else:
226 testType = [args.test_type]
Matthew Haddon74567092021-07-16 15:38:20 +0100227 results = []
Matthew Haddon1c00b712021-10-01 15:51:03 +0100228 for test_type in testType:
229 testList = []
230 for op in ttg.TOSA_OP_LIST:
231 if re.match(args.filter + ".*", op):
232 testList.extend(
233 ttg.genOpTestList(
234 op,
235 shapeFilter=args.target_shapes,
236 rankFilter=args.target_ranks,
237 dtypeFilter=args.target_dtypes,
238 testType=test_type
239 )
240 )
Matthew Haddon848efb42021-09-09 12:30:53 +0100241
Matthew Haddon1c00b712021-10-01 15:51:03 +0100242 print("{} matching {} tests".format(len(testList), test_type))
243
244 testStrings = []
245 for opName, testStr, dtype, error, shapeList, testArgs in testList:
246 # Check for and skip duplicate tests
247 if testStr in testStrings:
248 continue
249 else:
250 testStrings.append(testStr)
251
252 if args.verbose:
253 print(testStr)
254 results.append(ttg.serializeTest(opName, testStr, dtype, error, shapeList, testArgs))
Matthew Haddon74567092021-07-16 15:38:20 +0100255
256 print(f"Done creating {len(results)} tests")
257
Eric Kunzee5e26762020-10-13 16:11:07 -0700258
259
Kevin Cheng550ccc52021-03-03 11:21:43 -0800260if __name__ == "__main__":
Eric Kunzee5e26762020-10-13 16:11:07 -0700261 exit(main())