|
| 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 torch |
| 8 | +from inputgen.argtuple.gen import ArgumentTupleGenerator |
| 9 | +from specdb.db import SpecDictDB |
| 10 | + |
| 11 | + |
| 12 | +def pretty_print_add_args(posargs, inkwargs, outargs): |
| 13 | + return "".join( |
| 14 | + [ |
| 15 | + "tensor(shape=", |
| 16 | + str(list(posargs[0].shape)), |
| 17 | + ", dtype=", |
| 18 | + str(posargs[0].dtype), |
| 19 | + ") + tensor(shape=", |
| 20 | + str(list(posargs[1].shape)), |
| 21 | + ", dtype=", |
| 22 | + str(posargs[0].dtype), |
| 23 | + ") alpha = ", |
| 24 | + str(inkwargs["alpha"]), |
| 25 | + ] |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +def generate_inputs(): |
| 30 | + spec = SpecDictDB["add.Tensor"] |
| 31 | + generator = ArgumentTupleGenerator(spec) |
| 32 | + for ix, tup in enumerate(generator.gen()): |
| 33 | + posargs, inkwargs, outargs = tup |
| 34 | + # Pretty printing the inputs and outputs |
| 35 | + print(f"Tuple #{ix}: {pretty_print_add_args(posargs, inkwargs, outargs)}") |
| 36 | + yield posargs, inkwargs, outargs |
| 37 | + |
| 38 | + |
| 39 | +def test_add_op(): |
| 40 | + op = torch.ops.aten.add.Tensor |
| 41 | + for posargs, inkwargs, outargs in generate_inputs(): |
| 42 | + try: |
| 43 | + op(*posargs, **inkwargs, **outargs) |
| 44 | + except Exception: |
| 45 | + return False |
| 46 | + return True |
| 47 | + |
| 48 | + |
| 49 | +def main(): |
| 50 | + print("Testing add.Tensor with the following input tuples:") |
| 51 | + success = test_add_op() |
| 52 | + if success: |
| 53 | + print("Success!") |
| 54 | + else: |
| 55 | + print("Failure!") |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + main() |
0 commit comments