Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | #!/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 | |
| 18 | import argparse |
| 19 | import sys |
| 20 | import re |
| 21 | import os |
| 22 | import subprocess |
| 23 | import shlex |
| 24 | import json |
| 25 | import glob |
| 26 | import math |
| 27 | import queue |
| 28 | import threading |
| 29 | import traceback |
| 30 | |
| 31 | |
| 32 | from enum import IntEnum, Enum, unique |
| 33 | from datetime import datetime |
| 34 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 35 | # Include the ../scripts and ../scripts/xunit directory in PYTHONPATH |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 36 | parent_dir = os.path.dirname(os.path.realpath(__file__)) |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 37 | sys.path.append(os.path.join(parent_dir, "..", "scripts")) |
| 38 | sys.path.append(os.path.join(parent_dir, "..", "scripts", "xunit")) |
| 39 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 40 | import xunit |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 41 | |
| 42 | # Include the ../thirdparty/serialization_lib/python directory in PYTHONPATH |
| 43 | parent_dir = os.path.dirname(os.path.realpath(__file__)) |
| 44 | sys.path.append( |
| 45 | os.path.join(parent_dir, "..", "thirdparty", "serialization_lib", "python") |
| 46 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 47 | from tosa_serializer import * |
| 48 | from tosa_test_gen import TosaTestGen |
| 49 | import tosa |
| 50 | |
| 51 | # Used for parsing a comma-separated list of integers in a string |
| 52 | # to an actual list of integers |
| 53 | def str_to_list(in_s): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 54 | """Converts a comma-separated list of string integers to a python list of ints""" |
| 55 | lst = in_s.split(",") |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 56 | out_list = [] |
| 57 | for i in lst: |
| 58 | out_list.append(int(i)) |
| 59 | return out_list |
| 60 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 61 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 62 | def auto_int(x): |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 63 | """Converts hex/dec argument values to an int""" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 64 | return int(x, 0) |
| 65 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 66 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 67 | def parseArgs(): |
| 68 | |
| 69 | parser = argparse.ArgumentParser() |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 70 | parser.add_argument( |
| 71 | "-o", dest="output_dir", type=str, default="vtest", help="Test output directory" |
| 72 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 73 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 74 | parser.add_argument( |
| 75 | "--seed", |
| 76 | dest="random_seed", |
| 77 | default=42, |
| 78 | type=int, |
| 79 | help="Random seed for test generation", |
| 80 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 81 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 82 | parser.add_argument( |
| 83 | "--filter", |
| 84 | dest="filter", |
| 85 | default="", |
| 86 | type=str, |
| 87 | help="Filter operator test names by this expression", |
| 88 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 89 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 90 | parser.add_argument( |
| 91 | "-v", "--verbose", dest="verbose", action="count", help="Verbose operation" |
| 92 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 93 | |
| 94 | # Constraints on tests |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 95 | 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 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-batch-size", |
| 105 | dest="max_batch_size", |
| 106 | default=1, |
| 107 | type=int, |
| 108 | help="Maximum batch size for NHWC 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-conv-padding", |
| 113 | dest="max_conv_padding", |
| 114 | default=1, |
| 115 | type=int, |
| 116 | help="Maximum padding for Conv tests", |
| 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 | "--max-conv-dilation", |
| 121 | dest="max_conv_dilation", |
| 122 | default=2, |
| 123 | type=int, |
| 124 | help="Maximum dilation for Conv tests", |
| 125 | ) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 126 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 127 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 134 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 135 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 142 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 143 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 150 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 151 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 158 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 159 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 166 | |
| 167 | # Targetting a specific shape/rank/dtype |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 168 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 176 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 177 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 185 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 186 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 194 | |
Matthew Haddon | 818ab90 | 2021-07-27 09:12:49 +0100 | [diff] [blame] | 195 | 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 Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 204 | 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 Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 212 | args = parser.parse_args() |
| 213 | |
| 214 | return args |
| 215 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 216 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 217 | def main(): |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 218 | |
| 219 | args = parseArgs() |
| 220 | |
| 221 | ttg = TosaTestGen(args) |
| 222 | |
Matthew Haddon | 1c00b71 | 2021-10-01 15:51:03 +0100 | [diff] [blame] | 223 | if args.test_type == 'both': |
| 224 | testType = ['positive', 'negative'] |
| 225 | else: |
| 226 | testType = [args.test_type] |
Matthew Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 227 | results = [] |
Matthew Haddon | 1c00b71 | 2021-10-01 15:51:03 +0100 | [diff] [blame] | 228 | 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 Haddon | 848efb4 | 2021-09-09 12:30:53 +0100 | [diff] [blame] | 241 | |
Matthew Haddon | 1c00b71 | 2021-10-01 15:51:03 +0100 | [diff] [blame] | 242 | 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 Haddon | 7456709 | 2021-07-16 15:38:20 +0100 | [diff] [blame] | 255 | |
| 256 | print(f"Done creating {len(results)} tests") |
| 257 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 258 | |
| 259 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 260 | if __name__ == "__main__": |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 261 | exit(main()) |