blob: 66b2e565b71a208df64e5dfc96b016dd138d0105 [file] [log] [blame]
Jeremy Johnson35396f22023-01-04 17:05:25 +00001# Copyright (c) 2021-2023, ARM Limited.
Jeremy Johnson0ecfa372022-06-30 14:27:56 +01002# SPDX-License-Identifier: Apache-2.0
3"""Select generated tests."""
4import argparse
5import itertools
6import json
7import logging
James Ward736fd1a2023-01-23 17:13:37 +00008import re
Jeremy Johnson0ecfa372022-06-30 14:27:56 +01009from pathlib import Path
10from typing import Any
11from typing import Dict
12from typing import List
13
14logging.basicConfig()
15logger = logging.getLogger("test_select")
16
17
18def expand_params(permutes: Dict[str, List[Any]], others: Dict[str, List[Any]]):
19 """Generate permuted combinations of a dictionary of values and combine with others.
20
21 permutes: a dictionary with sequences of values to be fully permuted
22 others: a dictionary with sequences of values not fully permuted, but all used
23
24 This yields dictionaries with one value from each of the items in permutes,
25 combined with one value from each of the items in others.
26
27 Example 1:
28
29 permutes = {"a": [1, 2], "b": [3, 4]}
30 others = {"c": [5, 6, 7], "d" [True, False]}
31
32 generates:
33
34 [
35 {"a": 1, "b": 3, "c": 5, "d": True},
36 {"a": 1, "b": 4, "c": 6, "d": False`},
37 {"a": 2, "b": 3, "c": 7, "d": True},
38 {"a": 2, "b": 4, "c": 5, "d": False`},
39 ]
40
41 Example 2:
42
43 permutes = {"a": [1, 2], "b": [3, 4]}
44 others = {"c": [5, 6, 7, 8, 9], "d" [True, False]}
45
46 generates:
47
48 [
49 {"a": 1, "b": 3, "c": 5, "d": True},
50 {"a": 1, "b": 4, "c": 6, "d": False},
51 {"a": 2, "b": 3, "c": 7, "d": True},
52 {"a": 2, "b": 4, "c": 8, "d": False},
53 {"a": 1, "b": 3, "c": 9, "d": True},
54 ]
55
56 Raises:
57 ValueError if any item is in both permutes and others
58 """
59 for k in permutes:
60 if k in others:
61 raise ValueError(f"item conflict: {k}")
62
63 p_keys = []
64 p_vals = []
65 # if permutes is empty, p_permute_len should be 0, but we leave it as 1
66 # so we return a single, empty dictionary, if others is also empty
67 p_product_len = 1
68 # extract the keys and values from the permutes dictionary
69 # and calulate the product of the number of values in each item as we do so
70 for k, v in permutes.items():
71 p_keys.append(k)
72 p_vals.append(v)
73 p_product_len *= len(v)
74 # create a cyclic generator for the product of all the permuted values
75 p_product = itertools.product(*p_vals)
76 p_generator = itertools.cycle(p_product)
77
78 o_keys = []
79 o_vals = []
80 o_generators = []
81 # extract the keys and values from the others dictionary
82 # and create a cyclic generator for each list of values
83 for k, v in others.items():
84 o_keys.append(k)
85 o_vals.append(v)
86 o_generators.append(itertools.cycle(v))
87
88 # The number of params dictionaries generated will be the maximumum size
89 # of the permuted values and the non-permuted values from others
90 max_items = max([p_product_len] + [len(x) for x in o_vals])
91
92 # create a dictionary with a single value for each of the permutes and others keys
93 for _ in range(max_items):
94 params = {}
95 # add the values for the permutes parameters
96 # the permuted values generator returns a value for each of the permuted keys
97 # in the same order as they were originally given
98 p_vals = next(p_generator)
99 for i in range(len(p_keys)):
100 params[p_keys[i]] = p_vals[i]
101 # add the values for the others parameters
102 # there is a separate generator for each of the others values
103 for i in range(len(o_keys)):
104 params[o_keys[i]] = next(o_generators[i])
105 yield params
106
107
108class Operator:
109 """Base class for operator specific selection properties."""
110
111 # A registry of all Operator subclasses, indexed by the operator name
112 registry = {}
113
114 def __init_subclass__(cls, **kwargs):
115 """Subclass initialiser to register all Operator classes."""
116 super().__init_subclass__(**kwargs)
117 cls.registry[cls.name] = cls
118
119 # Derived classes must override the operator name
120 name = None
121 # Operators with additional parameters must override the param_names
122 # NB: the order must match the order the values appear in the test names
123 param_names = ["shape", "type"]
124
125 # Working set of param_names - updated for negative tests
126 wks_param_names = None
127
128 def __init__(
129 self,
130 test_dir: Path,
131 config: Dict[str, Dict[str, List[Any]]],
132 negative=False,
Jeremy Johnsonfd05bb32023-02-07 16:39:24 +0000133 ignore_missing=False,
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100134 ):
135 """Initialise the selection parameters for an operator.
136
James Ward736fd1a2023-01-23 17:13:37 +0000137 test_dir: the directory where the tests for all operators can
138 be found
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100139 config: a dictionary with:
James Ward736fd1a2023-01-23 17:13:37 +0000140 "params" - a dictionary with mappings of parameter
141 names to the values to select (a sub-set of
142 expected values for instance)
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100143 "permutes" - a list of parameter names to be permuted
James Ward736fd1a2023-01-23 17:13:37 +0000144 "preselected" - a list of dictionaries containing
145 parameter names and pre-chosen values
146 "sparsity" - a dictionary of parameter names with a
147 sparsity value
Jeremy Johnsonfd05bb32023-02-07 16:39:24 +0000148 "full_sparsity" - "true"/"false" to use the sparsity
149 value on permutes/params/preselected
James Ward736fd1a2023-01-23 17:13:37 +0000150 "exclude_patterns" - a list of regex's whereby each
151 match will not be considered for selection.
152 Exclusion happens BEFORE test selection (i.e.
153 before permutes are applied).
154 "errorifs" - list of ERRORIF case names to be selected
Jeremy Johnsondd3e9aa2023-02-06 16:58:04 +0000155 after exclusion (negative tests)
James Ward736fd1a2023-01-23 17:13:37 +0000156 negative: bool indicating if negative testing is being selected
Jeremy Johnsondd3e9aa2023-02-06 16:58:04 +0000157 which filters for ERRORIF in the test name and only selects
158 the first test found (ERRORIF tests)
Jeremy Johnsonfd05bb32023-02-07 16:39:24 +0000159 ignore_missing: bool indicating if missing tests should be ignored
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +0100160
Jeremy Johnsondd3e9aa2023-02-06 16:58:04 +0000161 EXAMPLE CONFIG (with non-json comments):
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +0100162 "params": {
163 "output_type": [
164 "outi8",
165 "outb"
166 ]
167 },
168 "permutes": [
169 "shape",
170 "type"
171 ],
172 "sparsity": {
173 "pad": 15
174 },
175 "preselected": [
176 {
177 "shape": "6",
178 "type": "i8",
179 "pad": "pad00"
180 }
181 ],
James Ward736fd1a2023-01-23 17:13:37 +0000182 "exclude_patterns": [
Jeremy Johnsondd3e9aa2023-02-06 16:58:04 +0000183 # Exclude positive (not ERRORIF) integer tests
184 "^((?!ERRORIF).)*_(i8|i16|i32|b)_out(i8|i16|i32|b)",
185 # Exclude negative (ERRORIF) i8 test
186 ".*_ERRORIF_.*_i8_outi8"
James Ward736fd1a2023-01-23 17:13:37 +0000187 ],
Jeremy Johnsone4b08ff2022-09-15 10:38:17 +0100188 "errorifs": [
189 "InputZeroPointNotZero"
190 ]
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100191 """
192 assert isinstance(
193 self.name, str
194 ), f"{self.__class__.__name__}: {self.name} is not a valid operator name"
195
196 self.negative = negative
Jeremy Johnsonfd05bb32023-02-07 16:39:24 +0000197 self.ignore_missing = ignore_missing
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100198 self.wks_param_names = self.param_names.copy()
199 if self.negative:
200 # need to override positive set up - use "errorifs" config if set
201 # add in errorif case before shape to support all ops, including
202 # different ops like COND_IF and CONVnD etc
203 index = self.wks_param_names.index("shape")
204 self.wks_param_names[index:index] = ["ERRORIF", "case"]
205 config["params"] = {x: [] for x in self.wks_param_names}
206 config["params"]["case"] = (
207 config["errorifs"] if "errorifs" in config else []
208 )
209 config["permutes"] = []
210 config["preselected"] = {}
211
212 self.params = config["params"] if "params" in config else {}
213 self.permutes = config["permutes"] if "permutes" in config else []
214 self.sparsity = config["sparsity"] if "sparsity" in config else {}
Jeremy Johnsonfd05bb32023-02-07 16:39:24 +0000215 self.full_sparsity = (
216 (config["full_sparsity"] == "true") if "full_sparsity" in config else False
217 )
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100218 self.preselected = config["preselected"] if "preselected" in config else {}
James Ward736fd1a2023-01-23 17:13:37 +0000219 self.exclude_patterns = (
220 config["exclude_patterns"] if "exclude_patterns" in config else []
221 )
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100222 self.non_permutes = [x for x in self.wks_param_names if x not in self.permutes]
223 logger.info(f"{self.name}: permutes={self.permutes}")
224 logger.info(f"{self.name}: non_permutes={self.non_permutes}")
James Ward736fd1a2023-01-23 17:13:37 +0000225 logger.info(f"{self.name}: exclude_patterns={self.exclude_patterns}")
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100226
James Ward736fd1a2023-01-23 17:13:37 +0000227 self.test_paths = []
228 excluded_paths = []
229 for path in self.get_test_paths(test_dir, self.negative):
230 pattern_match = False
231 for pattern in self.exclude_patterns:
232 if re.fullmatch(pattern, path.name):
233 excluded_paths.append(path)
234 pattern_match = True
235 break
236 if not pattern_match:
237 self.test_paths.append(path)
238
239 logger.debug(f"{self.name}: regex excluded paths={excluded_paths}")
240
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100241 if not self.test_paths:
242 logger.error(f"no tests found for {self.name} in {test_dir}")
243 logger.debug(f"{self.name}: paths={self.test_paths}")
244
245 # get default parameter values for any not given in the config
246 default_params = self.get_default_params()
247 for param in default_params:
248 if param not in self.params or not self.params[param]:
249 self.params[param] = default_params[param]
250 for param in self.wks_param_names:
251 logger.info(f"{self.name}: params[{param}]={self.params[param]}")
252
253 @staticmethod
254 def _get_test_paths(test_dir: Path, base_dir_glob, path_glob, negative):
255 """Generate test paths for operators using operator specifics."""
256 for base_dir in sorted(test_dir.glob(base_dir_glob)):
257 for path in sorted(base_dir.glob(path_glob)):
258 if (not negative and "ERRORIF" not in str(path)) or (
259 negative and "ERRORIF" in str(path)
260 ):
261 yield path
262
263 @classmethod
264 def get_test_paths(cls, test_dir: Path, negative):
265 """Generate test paths for this operator."""
266 yield from Operator._get_test_paths(test_dir, f"{cls.name}*", "*", negative)
267
268 def path_params(self, path):
269 """Return a dictionary of params from the test path."""
270 params = {}
271 op_name_parts = self.name.split("_")
272 values = path.name.split("_")[len(op_name_parts) :]
273 assert len(values) == len(
274 self.wks_param_names
275 ), f"len({values}) == len({self.wks_param_names})"
276 for i, param in enumerate(self.wks_param_names):
277 params[param] = values[i]
278 return params
279
280 def get_default_params(self):
281 """Get the default parameter values from the test names."""
282 params = {param: set() for param in self.wks_param_names}
283 for path in self.test_paths:
284 path_params = self.path_params(path)
285 for k in params:
286 params[k].add(path_params[k])
287 for param in params:
288 params[param] = sorted(list(params[param]))
289 return params
290
291 def select_tests(self): # noqa: C901 (function too complex)
292 """Generate the paths to the selected tests for this operator."""
293 if not self.test_paths:
294 # Exit early when nothing to select from
295 return
296
297 # the test paths that have not been selected yet
298 unused_paths = set(self.test_paths)
299
300 # a list of dictionaries of unused preselected parameter combinations
301 unused_preselected = [x for x in self.preselected]
302 logger.debug(f"preselected: {unused_preselected}")
303
304 # a list of dictionaries of unused permuted parameter combinations
305 permutes = {k: self.params[k] for k in self.permutes}
306 others = {k: self.params[k] for k in self.non_permutes}
307 unused_permuted = [x for x in expand_params(permutes, others)]
308 logger.debug(f"permuted: {unused_permuted}")
309
310 # a dictionary of sets of unused parameter values
311 if self.negative:
312 # We only care about selecting a test for each errorif case
313 unused_values = {k: set() for k in self.params}
314 unused_values["case"] = set(self.params["case"])
315 else:
316 unused_values = {k: set(v) for k, v in self.params.items()}
317
318 # select tests matching permuted, or preselected, parameter combinations
Jeremy Johnsonfd05bb32023-02-07 16:39:24 +0000319 for n, path in enumerate(self.test_paths):
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100320 path_params = self.path_params(path)
321 if path_params in unused_permuted or path_params in unused_preselected:
322 unused_paths.remove(path)
323 if path_params in unused_preselected:
324 unused_preselected.remove(path_params)
325 if path_params in unused_permuted:
326 unused_permuted.remove(path_params)
327 if self.negative:
328 # remove any other errorif cases, so we only match one
329 for p in list(unused_permuted):
330 if p["case"] == path_params["case"]:
331 unused_permuted.remove(p)
Jeremy Johnsonfd05bb32023-02-07 16:39:24 +0000332 if self.full_sparsity:
333 # Test for sparsity
334 skip = False
335 for k in path_params:
336 if k in self.sparsity and n % self.sparsity[k] != 0:
337 logger.debug(f"Skipping due to {k} sparsity - {path.name}")
338 skip = True
339 break
340 if skip:
341 continue
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100342 # remove the param values used by this path
343 for k in path_params:
344 unused_values[k].discard(path_params[k])
Jeremy Johnsonfd05bb32023-02-07 16:39:24 +0000345 logger.debug(f"FOUND wanted: {path.name}")
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100346 yield path
347
348 # search for tests that match any unused parameter values
349 for n, path in enumerate(sorted(list(unused_paths))):
350 path_params = self.path_params(path)
351 # select paths with unused param values
352 # skipping some, if sparsity is set for the param
353 for k in path_params:
354 if path_params[k] in unused_values[k] and (
355 k not in self.sparsity or n % self.sparsity[k] == 0
356 ):
357 # remove the param values used by this path
358 for p in path_params:
359 unused_values[p].discard(path_params[p])
Jeremy Johnsonfd05bb32023-02-07 16:39:24 +0000360 sparsity = self.sparsity[k] if k in self.sparsity else 0
361 logger.debug(f"FOUND unused [{k}/{n}/{sparsity}]: {path.name}")
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100362 yield path
363 break
364
Jeremy Johnsonfd05bb32023-02-07 16:39:24 +0000365 if not self.ignore_missing:
366 # report any preselected combinations that were not found
367 for params in unused_preselected:
368 logger.warning(f"MISSING preselected: {params}")
369 # report any permuted combinations that were not found
370 for params in unused_permuted:
371 logger.debug(f"MISSING permutation: {params}")
372 # report any param values that were not found
373 for k, values in unused_values.items():
374 if values:
375 if k not in self.sparsity:
376 logger.warning(
377 f"MISSING {len(values)} values for {k}: {values}"
378 )
379 else:
380 logger.info(
381 f"Skipped {len(values)} values for {k} due to sparsity setting"
382 )
383 logger.debug(f"Values skipped: {values}")
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100384
385
386class AbsOperator(Operator):
387 """Test selector for the ABS operator."""
388
389 name = "abs"
390
391
392class ArithmeticRightShiftOperator(Operator):
393 """Test selector for the Arithmetic Right Shift operator."""
394
395 name = "arithmetic_right_shift"
396 param_names = ["shape", "type", "rounding"]
397
398
399class AddOperator(Operator):
400 """Test selector for the ADD operator."""
401
402 name = "add"
403
404
405class ArgmaxOperator(Operator):
406 """Test selector for the ARGMAX operator."""
407
408 name = "argmax"
409 param_names = ["shape", "type", "axis"]
410
411
412class AvgPool2dOperator(Operator):
413 """Test selector for the AVG_POOL2D operator."""
414
415 name = "avg_pool2d"
Jeremy Johnson93d43902022-09-27 12:26:14 +0100416 param_names = ["shape", "type", "accum_type", "stride", "kernel", "pad"]
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100417
418
419class BitwiseAndOperator(Operator):
420 """Test selector for the BITWISE_AND operator."""
421
422 name = "bitwise_and"
423
424
425class BitwiseNotOperator(Operator):
426 """Test selector for the BITWISE_NOT operator."""
427
428 name = "bitwise_not"
429
430
431class BitwiseOrOperator(Operator):
432 """Test selector for the BITWISE_OR operator."""
433
434 name = "bitwise_or"
435
436
437class BitwiseXorOperator(Operator):
438 """Test selector for the BITWISE_XOR operator."""
439
440 name = "bitwise_xor"
441
442
443class CastOperator(Operator):
444 """Test selector for the CAST operator."""
445
446 name = "cast"
447 param_names = ["shape", "type", "output_type"]
448
449
James Ward71616fe2022-11-23 11:00:47 +0000450class CeilOperator(Operator):
451 """Test selector for the CEIL operator."""
452
453 name = "ceil"
454
455
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100456class ClampOperator(Operator):
457 """Test selector for the CLAMP operator."""
458
459 name = "clamp"
460
461
462class CLZOperator(Operator):
463 """Test selector for the CLZ operator."""
464
465 name = "clz"
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100466
467
468class ConcatOperator(Operator):
469 """Test selector for the CONCAT operator."""
470
471 name = "concat"
472 param_names = ["shape", "type", "axis"]
473
474
475class CondIfOperator(Operator):
476 """Test selector for the COND_IF operator."""
477
478 name = "cond_if"
479 param_names = ["variant", "shape", "type", "cond"]
480
481
482class ConstOperator(Operator):
483 """Test selector for the CONST operator."""
484
485 name = "const"
486
487
488class Conv2dOperator(Operator):
489 """Test selector for the CONV2D operator."""
490
491 name = "conv2d"
Jeremy Johnson93d43902022-09-27 12:26:14 +0100492 param_names = ["kernel", "shape", "type", "accum_type", "stride", "pad", "dilation"]
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100493
494
495class Conv3dOperator(Operator):
496 """Test selector for the CONV3D operator."""
497
498 name = "conv3d"
Jeremy Johnson93d43902022-09-27 12:26:14 +0100499 param_names = ["kernel", "shape", "type", "accum_type", "stride", "pad", "dilation"]
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100500
501
502class DepthwiseConv2dOperator(Operator):
503 """Test selector for the DEPTHWISE_CONV2D operator."""
504
505 name = "depthwise_conv2d"
Jeremy Johnson93d43902022-09-27 12:26:14 +0100506 param_names = ["kernel", "shape", "type", "accum_type", "stride", "pad", "dilation"]
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100507
508
509class EqualOperator(Operator):
510 """Test selector for the EQUAL operator."""
511
512 name = "equal"
513
514
Jeremy Johnson35396f22023-01-04 17:05:25 +0000515class ExpOperator(Operator):
516 """Test selector for the EXP operator."""
517
518 name = "exp"
519
520
Jeremy Johnsonc5d75932023-02-14 11:47:46 +0000521class FFT2DOperator(Operator):
522 """Test selector for the FFT2D operator."""
523
524 name = "fft2d"
525 param_names = ["shape", "type", "inverse"]
526
527
James Ward71616fe2022-11-23 11:00:47 +0000528class FloorOperator(Operator):
529 """Test selector for the FLOOR operator."""
530
531 name = "floor"
532
533
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100534class FullyConnectedOperator(Operator):
535 """Test selector for the FULLY_CONNECTED operator."""
536
537 name = "fully_connected"
Jeremy Johnson93d43902022-09-27 12:26:14 +0100538 param_names = ["shape", "type", "accum_type"]
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100539
540
541class GatherOperator(Operator):
542 """Test selector for the GATHER operator."""
543
544 name = "gather"
545
546
547class GreaterOperator(Operator):
548 """Test selector for the GREATER operator."""
549
550 name = "greater"
551
552 @classmethod
553 def get_test_paths(cls, test_dir: Path, negative):
554 """Generate test paths for this operator."""
555 yield from Operator._get_test_paths(test_dir, f"{cls.name}", "*", negative)
556
557
558class GreaterEqualOperator(Operator):
559 """Test selector for the GREATER_EQUAL operator."""
560
561 name = "greater_equal"
562
563
564class IdentityOperator(Operator):
565 """Test selector for the IDENTITY operator."""
566
567 name = "identity"
568
569
570class IntDivOperator(Operator):
Jeremy Johnson35396f22023-01-04 17:05:25 +0000571 """Test selector for the INTDIV operator."""
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100572
573 name = "intdiv"
574
575
Jeremy Johnson35396f22023-01-04 17:05:25 +0000576class LogOperator(Operator):
577 """Test selector for the LOG operator."""
578
579 name = "log"
580
581
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100582class LogicalAndOperator(Operator):
583 """Test selector for the LOGICAL_AND operator."""
584
585 name = "logical_and"
586
587
588class LogicalLeftShiftOperator(Operator):
589 """Test selector for the LOGICAL_LEFT_SHIFT operator."""
590
591 name = "logical_left_shift"
592
593
594class LogicalNotOperator(Operator):
595 """Test selector for the LOGICAL_NOT operator."""
596
597 name = "logical_not"
598
599
600class LogicalOrOperator(Operator):
601 """Test selector for the LOGICAL_OR operator."""
602
603 name = "logical_or"
604
605
606class LogicalRightShiftOperator(Operator):
607 """Test selector for the LOGICAL_RIGHT_SHIFT operator."""
608
609 name = "logical_right_shift"
610
611
612class LogicalXorOperator(Operator):
613 """Test selector for the LOGICAL_XOR operator."""
614
615 name = "logical_xor"
616
617
618class MatmulOperator(Operator):
619 """Test selector for the MATMUL operator."""
620
621 name = "matmul"
Jeremy Johnson93d43902022-09-27 12:26:14 +0100622 param_names = ["shape", "type", "accum_type"]
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100623
624
625class MaximumOperator(Operator):
626 """Test selector for the Maximum operator."""
627
628 name = "maximum"
629
630
631class MaxPool2dOperator(Operator):
632 """Test selector for the MAX_POOL2D operator."""
633
634 name = "max_pool2d"
635 param_names = ["shape", "type", "stride", "kernel", "pad"]
636
637
638class MinimumOperator(Operator):
639 """Test selector for the Minimum operator."""
640
641 name = "minimum"
642
643
644class MulOperator(Operator):
645 """Test selector for the MUL operator."""
646
647 name = "mul"
648 param_names = ["shape", "type", "perm", "shift"]
649
650
651class NegateOperator(Operator):
652 """Test selector for the Negate operator."""
653
654 name = "negate"
655
656
657class PadOperator(Operator):
658 """Test selector for the PAD operator."""
659
660 name = "pad"
661 param_names = ["shape", "type", "pad"]
662
663
Jeremy Johnson6ffb7c82022-12-05 16:59:28 +0000664class PowOperator(Operator):
665 """Test selector for the POW operator."""
666
667 name = "pow"
668
669
Jeremy Johnson35396f22023-01-04 17:05:25 +0000670class ReciprocalOperator(Operator):
671 """Test selector for the RECIPROCAL operator."""
672
673 name = "reciprocal"
674
675
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100676class ReduceAllOperator(Operator):
677 """Test selector for the REDUCE_ALL operator."""
678
679 name = "reduce_all"
680 param_names = ["shape", "type", "axis"]
681
682
683class ReduceAnyOperator(Operator):
684 """Test selector for the REDUCE_ANY operator."""
685
686 name = "reduce_any"
687 param_names = ["shape", "type", "axis"]
688
689
690class ReduceMaxOperator(Operator):
691 """Test selector for the REDUCE_MAX operator."""
692
693 name = "reduce_max"
694 param_names = ["shape", "type", "axis"]
695
696
697class ReduceMinOperator(Operator):
698 """Test selector for the REDUCE_MIN operator."""
699
700 name = "reduce_min"
701 param_names = ["shape", "type", "axis"]
702
703
James Ward512c1ca2023-01-27 18:46:44 +0000704class ReduceProductOperator(Operator):
705 """Test selector for the REDUCE_PRODUCT operator."""
706
707 name = "reduce_product"
708 param_names = ["shape", "type", "axis"]
709
710
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100711class ReduceSumOperator(Operator):
712 """Test selector for the REDUCE_SUM operator."""
713
714 name = "reduce_sum"
715 param_names = ["shape", "type", "axis"]
716
717
718class RescaleOperator(Operator):
719 """Test selector for the RESCALE operator."""
720
721 name = "rescale"
722 param_names = [
723 "shape",
724 "type",
725 "output_type",
726 "scale",
727 "double_round",
728 "per_channel",
729 ]
730
731
732class ReshapeOperator(Operator):
733 """Test selector for the RESHAPE operator."""
734
735 name = "reshape"
736 param_names = ["shape", "type", "perm", "rank"]
737
738
739class ResizeOperator(Operator):
740 """Test selector for the RESIZE operator."""
741
742 name = "resize"
743 param_names = [
744 "shape",
745 "type",
746 "mode",
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100747 "output_type",
Jeremy Johnsona0e03f32022-06-13 17:48:09 +0100748 "scale",
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100749 "offset",
Jeremy Johnsona0e03f32022-06-13 17:48:09 +0100750 "border",
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100751 ]
752
753
754class ReverseOperator(Operator):
755 """Test selector for the REVERSE operator."""
756
757 name = "reverse"
758 param_names = ["shape", "type", "axis"]
759
760
Jeremy Johnsonc5d75932023-02-14 11:47:46 +0000761class RFFT2DOperator(Operator):
762 """Test selector for the RFFT2D operator."""
763
764 name = "rfft2d"
765
766
Jeremy Johnson35396f22023-01-04 17:05:25 +0000767class RsqrtOperator(Operator):
768 """Test selector for the RSQRT operator."""
769
770 name = "rsqrt"
771
772
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100773class ScatterOperator(Operator):
774 """Test selector for the SCATTER operator."""
775
776 name = "scatter"
777
778
779class SelectOperator(Operator):
780 """Test selector for the SELECT operator."""
781
782 name = "select"
783
784
James Wardb45db9a2022-12-12 13:02:44 +0000785class SigmoidOperator(Operator):
786 """Test selector for the SIGMOID operator."""
787
788 name = "sigmoid"
789
790
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100791class SliceOperator(Operator):
792 """Test selector for the SLICE operator."""
793
794 name = "slice"
795 param_names = ["shape", "type", "perm"]
796
797
798class SubOperator(Operator):
799 """Test selector for the SUB operator."""
800
801 name = "sub"
802
803
804class TableOperator(Operator):
805 """Test selector for the TABLE operator."""
806
807 name = "table"
808
809
James Wardb45db9a2022-12-12 13:02:44 +0000810class TanhOperator(Operator):
811 """Test selector for the TANH operator."""
812
813 name = "tanh"
814
815
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100816class TileOperator(Operator):
817 """Test selector for the TILE operator."""
818
819 name = "tile"
820 param_names = ["shape", "type", "perm"]
821
822
823class TransposeOperator(Operator):
824 """Test selector for the TRANSPOSE operator."""
825
826 name = "transpose"
827 param_names = ["shape", "type", "perm"]
828
829 @classmethod
830 def get_test_paths(cls, test_dir: Path, negative):
831 """Generate test paths for this operator."""
832 yield from Operator._get_test_paths(test_dir, f"{cls.name}", "*", negative)
833
834
835class TransposeConv2dOperator(Operator):
836 """Test selector for the TRANSPOSE_CONV2D operator."""
837
838 name = "transpose_conv2d"
Jeremy Johnson93d43902022-09-27 12:26:14 +0100839 param_names = [
840 "kernel",
841 "shape",
842 "type",
843 "accum_type",
844 "stride",
845 "pad",
846 "out_shape",
847 ]
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100848
849 def path_params(self, path):
850 """Return a dictionary of params from the test path."""
851 params = super().path_params(path)
852 # out_shape is different for every test case, so ignore it for selection
853 params["out_shape"] = ""
854 return params
855
856
857class WhileLoopOperator(Operator):
858 """Test selector for the WHILE_LOOP operator."""
859
860 name = "while_loop"
861 param_names = ["shape", "type", "cond"]
862
863
864def parse_args():
865 """Parse the arguments."""
866 parser = argparse.ArgumentParser()
867 parser.add_argument(
868 "--test-dir",
869 default=Path.cwd(),
870 type=Path,
871 help=(
872 "The directory where test subdirectories for all operators can be found"
873 " (default: current working directory)"
874 ),
875 )
876 parser.add_argument(
877 "--config",
878 default=Path(__file__).with_suffix(".json"),
879 type=Path,
880 help="A JSON file defining the parameters to use for each operator",
881 )
882 parser.add_argument(
Jeremy Johnsonfd05bb32023-02-07 16:39:24 +0000883 "--selector",
884 default="default",
885 type=str,
886 help="The selector in the selection dictionary to use for each operator",
887 )
888 parser.add_argument(
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100889 "--full-path", action="store_true", help="output the full path for each test"
890 )
891 parser.add_argument(
892 "-v",
893 dest="verbosity",
894 action="count",
895 default=0,
896 help="Verbosity (can be used multiple times for more details)",
897 )
898 parser.add_argument(
899 "operators",
900 type=str,
901 nargs="*",
902 help=(
903 f"Select tests for the specified operator(s)"
904 f" - all operators are assumed if none are specified)"
905 f" - choose from: {[n for n in Operator.registry]}"
906 ),
907 )
908 parser.add_argument(
909 "--test-type",
910 dest="test_type",
911 choices=["positive", "negative"],
912 default="positive",
913 type=str,
914 help="type of tests selected, positive or negative",
915 )
916 return parser.parse_args()
917
918
919def main():
920 """Example test selection."""
921 args = parse_args()
922
923 loglevels = (logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG)
James Ward635bc992022-11-23 11:55:32 +0000924 logger.setLevel(loglevels[min(args.verbosity, len(loglevels) - 1)])
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100925 logger.info(f"{__file__}: args: {args}")
926
927 try:
928 with open(args.config, "r") as fd:
929 config = json.load(fd)
930 except Exception as e:
931 logger.error(f"Config file error: {e}")
932 return 2
933
934 negative = args.test_type == "negative"
935 for op_name in Operator.registry:
936 if not args.operators or op_name in args.operators:
937 op_params = config[op_name] if op_name in config else {}
Jeremy Johnsonfd05bb32023-02-07 16:39:24 +0000938 if "selection" in op_params and args.selector in op_params["selection"]:
939 selection_config = op_params["selection"][args.selector]
940 else:
941 logger.warning(
942 f"Could not find selection config {args.selector} for {op_name}"
943 )
944 selection_config = {}
945 op = Operator.registry[op_name](args.test_dir, selection_config, negative)
Jeremy Johnson0ecfa372022-06-30 14:27:56 +0100946 for test_path in op.select_tests():
947 print(test_path.resolve() if args.full_path else test_path.name)
948
949 return 0
950
951
952if __name__ == "__main__":
953 exit(main())