Skip to content

Commit 1e36504

Browse files
manuelcandalesfacebook-github-bot
authored andcommitted
add example
Summary: Adds an example of how to use FACTO to test add.Tensor op Reviewed By: zonglinpengmeta Differential Revision: D59071076 fbshipit-source-id: f1e485fde418da1ad42fb13a4b575be5b3b57024
1 parent 2eb3834 commit 1e36504

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

examples/example.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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()

examples/minimal_example.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 main():
13+
# minimal example to test add.Tensor using FACTO
14+
spec = SpecDictDB["add.Tensor"]
15+
op = torch.ops.aten.add.Tensor
16+
for posargs, inkwargs, outargs in ArgumentTupleGenerator(spec).gen():
17+
op(*posargs, **inkwargs, **outargs)
18+
19+
20+
if __name__ == "__main__":
21+
main()

0 commit comments

Comments
 (0)