Jeremy Johnson | 5c1364c | 2022-01-13 15:04:21 +0000 | [diff] [blame] | 1 | # Copyright (c) 2020-2022, ARM Limited. |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3 | import argparse |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 4 | import re |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 5 | |
Jeremy Johnson | 2ec3494 | 2021-12-14 16:34:05 +0000 | [diff] [blame] | 6 | from generator.tosa_test_gen import TosaTestGen |
| 7 | from serializer.tosa_serializer import dtype_str_to_val |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame^] | 8 | from serializer.tosa_serializer import DTypeNames |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 9 | |
Jeremy Johnson | 5c1364c | 2022-01-13 15:04:21 +0000 | [diff] [blame] | 10 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 11 | # Used for parsing a comma-separated list of integers in a string |
| 12 | # to an actual list of integers |
| 13 | def str_to_list(in_s): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 14 | """Converts a comma-separated list of string integers to a python list of ints""" |
| 15 | lst = in_s.split(",") |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 16 | out_list = [] |
| 17 | for i in lst: |
| 18 | out_list.append(int(i)) |
| 19 | return out_list |
| 20 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 21 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 22 | def auto_int(x): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 23 | """Converts hex/dec argument values to an int""" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 24 | return int(x, 0) |
| 25 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 26 | |
Jeremy Johnson | 0042343 | 2022-09-12 17:27:37 +0100 | [diff] [blame] | 27 | def parseArgs(argv): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 28 | |
| 29 | parser = argparse.ArgumentParser() |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 30 | parser.add_argument( |
| 31 | "-o", dest="output_dir", type=str, default="vtest", help="Test output directory" |
| 32 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 33 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 34 | parser.add_argument( |
| 35 | "--seed", |
| 36 | dest="random_seed", |
| 37 | default=42, |
| 38 | type=int, |
| 39 | help="Random seed for test generation", |
| 40 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 41 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 42 | parser.add_argument( |
| 43 | "--filter", |
| 44 | dest="filter", |
| 45 | default="", |
| 46 | type=str, |
| 47 | help="Filter operator test names by this expression", |
| 48 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 49 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 50 | parser.add_argument( |
| 51 | "-v", "--verbose", dest="verbose", action="count", help="Verbose operation" |
| 52 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 53 | |
| 54 | # Constraints on tests |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 55 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 62 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 63 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 70 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 71 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 78 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 79 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 86 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 87 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 94 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 95 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 102 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 103 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 110 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 111 | parser.add_argument( |
| 112 | "--max-pooling-kernel", |
| 113 | dest="max_pooling_kernel", |
Jeremy Johnson | 39f127b | 2022-01-25 17:51:26 +0000 | [diff] [blame] | 114 | default=3, |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 115 | type=int, |
Jeremy Johnson | 39f127b | 2022-01-25 17:51:26 +0000 | [diff] [blame] | 116 | help="Maximum kernel for pooling tests", |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 117 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 118 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 119 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 126 | |
Jeremy Johnson | a0e03f3 | 2022-06-13 17:48:09 +0100 | [diff] [blame] | 127 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 135 | # Targetting a specific shape/rank/dtype |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 136 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 144 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 145 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 153 | |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame^] | 154 | # Used for parsing a comma-separated list of integers in a string |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 155 | 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 Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame^] | 161 | help=f"Create test with a particular DType: [{', '.join([d.lower() for d in DTypeNames[1:]])}] (may be repeated)", |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 162 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 163 | |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 164 | 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 Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 173 | parser.add_argument( |
| 174 | "--test-type", |
| 175 | dest="test_type", |
Jeremy Johnson | 5c1364c | 2022-01-13 15:04:21 +0000 | [diff] [blame] | 176 | choices=["positive", "negative", "both"], |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 177 | default="positive", |
| 178 | type=str, |
Jeremy Johnson | 39f127b | 2022-01-25 17:51:26 +0000 | [diff] [blame] | 179 | help="type of tests produced, positive, negative, or both", |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 180 | ) |
Jeremy Johnson | 39f127b | 2022-01-25 17:51:26 +0000 | [diff] [blame] | 181 | |
| 182 | parser.add_argument( |
| 183 | "--allow-pooling-and-conv-oversizes", |
| 184 | dest="oversize", |
Jeremy Johnson | ae0c1c6 | 2022-02-10 17:27:34 +0000 | [diff] [blame] | 185 | action="store_true", |
Jeremy Johnson | 39f127b | 2022-01-25 17:51:26 +0000 | [diff] [blame] | 186 | help="allow oversize padding, stride and kernel tests", |
| 187 | ) |
| 188 | |
Jeremy Johnson | 0042343 | 2022-09-12 17:27:37 +0100 | [diff] [blame] | 189 | 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 Johnson | a0848c6 | 2022-09-15 15:01:30 +0100 | [diff] [blame] | 197 | 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 Johnson | 0042343 | 2022-09-12 17:27:37 +0100 | [diff] [blame] | 204 | args = parser.parse_args(argv) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 205 | |
| 206 | return args |
| 207 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 208 | |
Jeremy Johnson | 0042343 | 2022-09-12 17:27:37 +0100 | [diff] [blame] | 209 | def main(argv=None): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 210 | |
Jeremy Johnson | 0042343 | 2022-09-12 17:27:37 +0100 | [diff] [blame] | 211 | args = parseArgs(argv) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 212 | |
| 213 | ttg = TosaTestGen(args) |
| 214 | |
Jeremy Johnson | 5c1364c | 2022-01-13 15:04:21 +0000 | [diff] [blame] | 215 | if args.test_type == "both": |
| 216 | testType = ["positive", "negative"] |
Matthew Haddon | 1c00b71 | 2021-10-01 15:51:03 +0100 | [diff] [blame] | 217 | else: |
| 218 | testType = [args.test_type] |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 219 | results = [] |
Matthew Haddon | 1c00b71 | 2021-10-01 15:51:03 +0100 | [diff] [blame] | 220 | 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 Johnson | 5c1364c | 2022-01-13 15:04:21 +0000 | [diff] [blame] | 230 | testType=test_type, |
Matthew Haddon | 1c00b71 | 2021-10-01 15:51:03 +0100 | [diff] [blame] | 231 | ) |
| 232 | ) |
Matthew Haddon | 848efb4 | 2021-09-09 12:30:53 +0100 | [diff] [blame] | 233 | |
Matthew Haddon | 1c00b71 | 2021-10-01 15:51:03 +0100 | [diff] [blame] | 234 | 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 Johnson | a0e03f3 | 2022-06-13 17:48:09 +0100 | [diff] [blame] | 240 | print(f"Skipping duplicate test: {testStr}") |
Matthew Haddon | 1c00b71 | 2021-10-01 15:51:03 +0100 | [diff] [blame] | 241 | continue |
| 242 | else: |
| 243 | testStrings.append(testStr) |
| 244 | |
Jeremy Johnson | 5c1364c | 2022-01-13 15:04:21 +0000 | [diff] [blame] | 245 | results.append( |
| 246 | ttg.serializeTest(opName, testStr, dtype, error, shapeList, testArgs) |
| 247 | ) |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 248 | |
| 249 | print(f"Done creating {len(results)} tests") |
| 250 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 251 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 252 | if __name__ == "__main__": |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 253 | exit(main()) |