|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import argparse |
| 8 | +import logging |
| 9 | +import sys |
| 10 | +from typing import Any, List, OrderedDict, Tuple |
| 11 | + |
| 12 | +from executorch.exir.dialects.edge.op.api import get_callable, to_variant |
| 13 | +from inputgen.argtuple.engine import MetaArgTupleEngine |
| 14 | +from inputgen.argtuple.gen import ArgumentTupleGenerator |
| 15 | +from inputgen.argument.engine import MetaArg |
| 16 | +from inputgen.specs.model import Spec |
| 17 | +from specdb.db import SpecDictDB |
| 18 | +from torch._ops import OpOverload |
| 19 | +from torchgen.model import SchemaKind |
| 20 | + |
| 21 | +logging.basicConfig(stream=sys.stderr, level=logging.WARNING) |
| 22 | + |
| 23 | + |
| 24 | +def smt(meta_tuple): |
| 25 | + return str([str(x) for x in meta_tuple]) |
| 26 | + |
| 27 | + |
| 28 | +class SpecRunner: |
| 29 | + def __init__(self, spec: Spec, *, valid: bool = True, out: bool = False): |
| 30 | + self.spec = spec |
| 31 | + self.generator = ArgumentTupleGenerator(self.spec) |
| 32 | + self.valid = valid |
| 33 | + self.out = out |
| 34 | + self.op_name = spec.op |
| 35 | + self.op = self.get_callable_op() |
| 36 | + |
| 37 | + def get_callable_op(self): |
| 38 | + name = self.spec.op |
| 39 | + op: OpOverload = get_callable(name) |
| 40 | + if self.out: |
| 41 | + # Get the out variant op |
| 42 | + op: OpOverload = to_variant(op, SchemaKind.out) |
| 43 | + return op |
| 44 | + |
| 45 | + def run(self): |
| 46 | + failures = [] |
| 47 | + engine = MetaArgTupleEngine(self.spec, out=self.out) |
| 48 | + for meta_tuple in engine.gen(valid=self.valid): |
| 49 | + success, _, _, _, _ = self.run_meta_tuple(meta_tuple) |
| 50 | + if not success: |
| 51 | + failures.append(meta_tuple) |
| 52 | + if len(failures) > 0: |
| 53 | + print("FAILURES\n") |
| 54 | + for meta_tuple in failures: |
| 55 | + print(f"\t{smt(meta_tuple)}\n") |
| 56 | + else: |
| 57 | + print("SUCCESS\n") |
| 58 | + |
| 59 | + def run_meta_tuple( |
| 60 | + self, meta_tuple: Tuple[MetaArg] |
| 61 | + ) -> Tuple[bool, Any, List[Any], OrderedDict[str, Any], OrderedDict[str, Any]]: |
| 62 | + print(f"Running op: {self.op_name}, meta_tuple: {[str(x) for x in meta_tuple]}") |
| 63 | + posargs, inkwargs, outargs = self.generator.gen_tuple(meta_tuple, out=self.out) |
| 64 | + return self.run_values(meta_tuple, posargs, inkwargs, outargs) |
| 65 | + |
| 66 | + def run_values( |
| 67 | + self, |
| 68 | + meta_tuple: Tuple[MetaArg], |
| 69 | + posargs: List[Any], |
| 70 | + inkwargs: OrderedDict[str, Any], |
| 71 | + outargs: OrderedDict[str, Any], |
| 72 | + ) -> Tuple[bool, Any, List[Any], OrderedDict[str, Any], OrderedDict[str, Any]]: |
| 73 | + try: |
| 74 | + res = self.op(*posargs, **inkwargs, **outargs) |
| 75 | + if not self.valid: |
| 76 | + logging.warning("unexpected success") |
| 77 | + success = self.valid |
| 78 | + return success, res, posargs, inkwargs, outargs |
| 79 | + except AssertionError as e: |
| 80 | + raise RuntimeError( |
| 81 | + f"opname: {self.op_name}, meta_tuple: {meta_tuple}" |
| 82 | + ) from e |
| 83 | + except Exception as e: |
| 84 | + if self.valid: |
| 85 | + logging.warning( |
| 86 | + f"opname: {self.op_name}, meta_tuple: {smt(meta_tuple)}, exception: {e}" |
| 87 | + ) |
| 88 | + success = not self.valid |
| 89 | + return success, None, posargs, inkwargs, outargs |
| 90 | + |
| 91 | + |
| 92 | +def main(): |
| 93 | + parser = argparse.ArgumentParser() |
| 94 | + parser.add_argument("op") |
| 95 | + parser.add_argument( |
| 96 | + "--invalid", action="store_true", help="generate invalid inputs" |
| 97 | + ) |
| 98 | + parser.add_argument("--out", action="store_true", help="run out variants") |
| 99 | + args = parser.parse_args() |
| 100 | + |
| 101 | + if args.op not in SpecDictDB: |
| 102 | + raise RuntimeError(f"Op {args.op} not found in SpecDB") |
| 103 | + |
| 104 | + spec = SpecDictDB[args.op] |
| 105 | + SpecRunner(spec, valid=not args.invalid, out=args.out).run() |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + main() |
0 commit comments