Jeremy Johnson | 2ec3494 | 2021-12-14 16:34:05 +0000 | [diff] [blame^] | 1 | # Copyright (c) 2020-2021, ARM Limited. |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 2 | # |
| 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 | |
| 16 | import argparse |
| 17 | import sys |
| 18 | import re |
| 19 | import os |
| 20 | import subprocess |
| 21 | import shlex |
| 22 | import json |
| 23 | import glob |
| 24 | import math |
| 25 | import queue |
| 26 | import threading |
| 27 | import traceback |
| 28 | |
| 29 | |
| 30 | from enum import IntEnum, Enum, unique |
| 31 | from datetime import datetime |
| 32 | |
Jeremy Johnson | 2ec3494 | 2021-12-14 16:34:05 +0000 | [diff] [blame^] | 33 | from generator.tosa_test_gen import TosaTestGen |
| 34 | from serializer.tosa_serializer import dtype_str_to_val |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 35 | |
| 36 | # Used for parsing a comma-separated list of integers in a string |
| 37 | # to an actual list of integers |
| 38 | def str_to_list(in_s): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 39 | """Converts a comma-separated list of string integers to a python list of ints""" |
| 40 | lst = in_s.split(",") |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 41 | out_list = [] |
| 42 | for i in lst: |
| 43 | out_list.append(int(i)) |
| 44 | return out_list |
| 45 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 46 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 47 | def auto_int(x): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 48 | """Converts hex/dec argument values to an int""" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 49 | return int(x, 0) |
| 50 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 51 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 52 | def parseArgs(): |
| 53 | |
| 54 | parser = argparse.ArgumentParser() |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 55 | parser.add_argument( |
| 56 | "-o", dest="output_dir", type=str, default="vtest", help="Test output directory" |
| 57 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 58 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 59 | parser.add_argument( |
| 60 | "--seed", |
| 61 | dest="random_seed", |
| 62 | default=42, |
| 63 | type=int, |
| 64 | help="Random seed for test generation", |
| 65 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 66 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 67 | parser.add_argument( |
| 68 | "--filter", |
| 69 | dest="filter", |
| 70 | default="", |
| 71 | type=str, |
| 72 | help="Filter operator test names by this expression", |
| 73 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 74 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 75 | parser.add_argument( |
| 76 | "-v", "--verbose", dest="verbose", action="count", help="Verbose operation" |
| 77 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 78 | |
| 79 | # Constraints on tests |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 80 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 87 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 88 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 95 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 96 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 103 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 104 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 111 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 112 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 119 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 120 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 127 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 128 | 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 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 | "--max-pooling-kernel", |
| 138 | dest="max_pooling_kernel", |
| 139 | default=2, |
| 140 | type=int, |
| 141 | help="Maximum padding for pooling tests", |
| 142 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 143 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 144 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 151 | |
| 152 | # Targetting a specific shape/rank/dtype |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 153 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 161 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 162 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 170 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 171 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 179 | |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 180 | 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 Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 189 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 197 | args = parser.parse_args() |
| 198 | |
| 199 | return args |
| 200 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 201 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 202 | def main(): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 203 | |
| 204 | args = parseArgs() |
| 205 | |
| 206 | ttg = TosaTestGen(args) |
| 207 | |
Matthew Haddon | 1c00b71 | 2021-10-01 15:51:03 +0100 | [diff] [blame] | 208 | if args.test_type == 'both': |
| 209 | testType = ['positive', 'negative'] |
| 210 | else: |
| 211 | testType = [args.test_type] |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 212 | results = [] |
Matthew Haddon | 1c00b71 | 2021-10-01 15:51:03 +0100 | [diff] [blame] | 213 | 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 Haddon | 848efb4 | 2021-09-09 12:30:53 +0100 | [diff] [blame] | 226 | |
Matthew Haddon | 1c00b71 | 2021-10-01 15:51:03 +0100 | [diff] [blame] | 227 | 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 Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 240 | |
| 241 | print(f"Done creating {len(results)} tests") |
| 242 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 243 | |
| 244 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 245 | if __name__ == "__main__": |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 246 | exit(main()) |