blob: 02d1934393b7f1d66ac4b2bdab7c3193440385c6 [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
223 testList = []
224 for op in ttg.TOSA_OP_LIST:
Kevin Cheng550ccc52021-03-03 11:21:43 -0800225 if re.match(args.filter + ".*", op):
226 testList.extend(
227 ttg.genOpTestList(
228 op,
229 shapeFilter=args.target_shapes,
230 rankFilter=args.target_ranks,
231 dtypeFilter=args.target_dtypes,
Matthew Haddon74567092021-07-16 15:38:20 +0100232 testType=args.test_type
Kevin Cheng550ccc52021-03-03 11:21:43 -0800233 )
234 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700235
Kevin Cheng550ccc52021-03-03 11:21:43 -0800236 print("{} matching tests".format(len(testList)))
Matthew Haddon74567092021-07-16 15:38:20 +0100237 results = []
Eric Kunzee5e26762020-10-13 16:11:07 -0700238 for opName, testStr, dtype, shapeList, testArgs in testList:
Jared Smolense2e13cd2021-01-27 08:33:20 -0800239 if args.verbose:
240 print(testStr)
Matthew Haddon74567092021-07-16 15:38:20 +0100241 results.append(ttg.serializeTest(opName, testStr, dtype, shapeList, testArgs))
242
243 print(f"Done creating {len(results)} tests")
244
Eric Kunzee5e26762020-10-13 16:11:07 -0700245
246
Kevin Cheng550ccc52021-03-03 11:21:43 -0800247if __name__ == "__main__":
Eric Kunzee5e26762020-10-13 16:11:07 -0700248 exit(main())