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 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 8 | |
Jeremy Johnson | 5c1364c | 2022-01-13 15:04:21 +0000 | [diff] [blame] | 9 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 10 | # Used for parsing a comma-separated list of integers in a string |
| 11 | # to an actual list of integers |
| 12 | def str_to_list(in_s): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 13 | """Converts a comma-separated list of string integers to a python list of ints""" |
| 14 | lst = in_s.split(",") |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 15 | out_list = [] |
| 16 | for i in lst: |
| 17 | out_list.append(int(i)) |
| 18 | return out_list |
| 19 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 20 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 21 | def auto_int(x): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 22 | """Converts hex/dec argument values to an int""" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 23 | return int(x, 0) |
| 24 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 25 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 26 | def parseArgs(): |
| 27 | |
| 28 | parser = argparse.ArgumentParser() |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 29 | parser.add_argument( |
| 30 | "-o", dest="output_dir", type=str, default="vtest", help="Test output directory" |
| 31 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 32 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 33 | parser.add_argument( |
| 34 | "--seed", |
| 35 | dest="random_seed", |
| 36 | default=42, |
| 37 | type=int, |
| 38 | help="Random seed for test generation", |
| 39 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 40 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 41 | parser.add_argument( |
| 42 | "--filter", |
| 43 | dest="filter", |
| 44 | default="", |
| 45 | type=str, |
| 46 | help="Filter operator test names by this expression", |
| 47 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 48 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 49 | parser.add_argument( |
| 50 | "-v", "--verbose", dest="verbose", action="count", help="Verbose operation" |
| 51 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 52 | |
| 53 | # Constraints on tests |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 54 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 61 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 62 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 69 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 70 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 77 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 78 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 85 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 86 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 93 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 94 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 101 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 102 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 109 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 110 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 117 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 118 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 125 | |
| 126 | # Targetting a specific shape/rank/dtype |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 127 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 135 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 136 | 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 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-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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 153 | |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 154 | 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 Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 163 | parser.add_argument( |
| 164 | "--test-type", |
| 165 | dest="test_type", |
Jeremy Johnson | 5c1364c | 2022-01-13 15:04:21 +0000 | [diff] [blame] | 166 | choices=["positive", "negative", "both"], |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 167 | default="positive", |
| 168 | type=str, |
| 169 | help="type of tests produced, postive, negative, or both", |
| 170 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 171 | args = parser.parse_args() |
| 172 | |
| 173 | return args |
| 174 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 175 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 176 | def main(): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 177 | |
| 178 | args = parseArgs() |
| 179 | |
| 180 | ttg = TosaTestGen(args) |
| 181 | |
Jeremy Johnson | 5c1364c | 2022-01-13 15:04:21 +0000 | [diff] [blame] | 182 | if args.test_type == "both": |
| 183 | testType = ["positive", "negative"] |
Matthew Haddon | 1c00b71 | 2021-10-01 15:51:03 +0100 | [diff] [blame] | 184 | else: |
| 185 | testType = [args.test_type] |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 186 | results = [] |
Matthew Haddon | 1c00b71 | 2021-10-01 15:51:03 +0100 | [diff] [blame] | 187 | 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 Johnson | 5c1364c | 2022-01-13 15:04:21 +0000 | [diff] [blame] | 197 | testType=test_type, |
Matthew Haddon | 1c00b71 | 2021-10-01 15:51:03 +0100 | [diff] [blame] | 198 | ) |
| 199 | ) |
Matthew Haddon | 848efb4 | 2021-09-09 12:30:53 +0100 | [diff] [blame] | 200 | |
Matthew Haddon | 1c00b71 | 2021-10-01 15:51:03 +0100 | [diff] [blame] | 201 | 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 Johnson | 5c1364c | 2022-01-13 15:04:21 +0000 | [diff] [blame] | 213 | results.append( |
| 214 | ttg.serializeTest(opName, testStr, dtype, error, shapeList, testArgs) |
| 215 | ) |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 216 | |
| 217 | print(f"Done creating {len(results)} tests") |
| 218 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 219 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 220 | if __name__ == "__main__": |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 221 | exit(main()) |