Skip to content

Commit b8538ac

Browse files
zonglinpengfacebook-github-bot
authored andcommitted
add unit test for op_add
Summary: titled Differential Revision: D66510372
1 parent 30d5a11 commit b8538ac

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
2+
3+
import unittest
4+
from typing import Tuple
5+
6+
from parameterized import parameterized
7+
8+
from executorch.backends.cadence.aot.ops_registrations import * # noqa
9+
10+
import torch
11+
import torch.nn as nn
12+
from executorch.backends.cadence.aot.export_example import export_model
13+
14+
15+
class ATenOpTestCases(unittest.TestCase):
16+
@parameterized.expand(
17+
[
18+
[(7, 5, 6), (7, 5, 6)],
19+
[(7, 5, 6), (1)],
20+
[(1), (7, 5, 6)],
21+
[(1), (7, 5, 6), 2.23],
22+
[(1), (7, 5, 6), -1.0],
23+
[(1), (7, 5, 6), -2.23],
24+
[(7, 5, 6), (7, 5, 6), 1.23],
25+
[(6, 7), (6, 7)],
26+
[(6, 7), (6, 7), 2],
27+
# Broadcast tests (should be optimized on G3)
28+
[(1, 32, 64), (1, 1, 64)],
29+
[(1, 32, 64), (64)],
30+
[(1, 1, 32), (32)],
31+
[(16, 1, 16), (1, 1, 16)],
32+
[(16, 1, 16), (16)],
33+
[(1, 4, 8, 8), (1, 1, 8, 8)],
34+
[(1, 4, 8, 8), (8, 8)],
35+
# Broadcast tests (should go to portable ops)
36+
[(1, 10, 1, 8), (4, 1, 4, 1)],
37+
[(1, 1, 16), (1, 8, 1), 2.5],
38+
# # aten.upsample_nearest2d tests
39+
[(5, 6, 6, 8), (5, 6, 6, 8)],
40+
[(1, 1, 12, 16), (1, 1, 12, 16)],
41+
]
42+
)
43+
def test_aten_add_out(
44+
self, Xshape: Tuple[int], Yshape: Tuple[int], alpha: float = 1
45+
) -> None:
46+
class AddTensor(nn.Module):
47+
def __init__(self, alpha: float):
48+
super().__init__()
49+
self.alpha = alpha
50+
51+
def forward(self, x: torch.Tensor, y: torch.Tensor):
52+
return torch.add(x, y, alpha=self.alpha)
53+
54+
model = AddTensor(alpha)
55+
56+
X = torch.randn(Xshape)
57+
Y = torch.randn(Yshape)
58+
59+
model.eval()
60+
export_model(model, (X, Y))
61+
62+
@parameterized.expand(
63+
[
64+
[(7, 5, 6), (7, 5, 6)],
65+
[(7, 5, 6), (1)],
66+
[(1), (7, 5, 6)],
67+
[(1), (7, 5, 6), 2.23],
68+
[(1), (7, 5, 6), -1.0],
69+
[(1), (7, 5, 6), -2.23],
70+
[(7, 5, 6), (7, 5, 6), 1.23],
71+
[(6, 7), (6, 7)],
72+
[(6, 7), (6, 7), 2],
73+
# Broadcast tests (should be optimized on G3)
74+
[(1, 32, 64), (1, 1, 64)],
75+
[(1, 32, 64), (64)],
76+
[(1, 1, 32), (32)],
77+
[(16, 1, 16), (1, 1, 16)],
78+
[(16, 1, 16), (16)],
79+
[(1, 4, 8, 8), (1, 1, 8, 8)],
80+
[(1, 4, 8, 8), (8, 8)],
81+
# Broadcast tests (should go to portable ops)
82+
[(1, 10, 1, 8), (4, 1, 4, 1)],
83+
[(1, 1, 16), (1, 8, 1), 2.5],
84+
# # aten.upsample_nearest2d tests
85+
[(5, 6, 6, 8), (5, 6, 6, 8)],
86+
[(1, 1, 12, 16), (1, 1, 12, 16)],
87+
]
88+
)
89+
def test_aten_add_scalar_out(
90+
self, Xshape: Tuple[int], Yshape: Tuple[int], alpha: float = 1
91+
) -> None:
92+
# Tensor-Scalar addition
93+
class AddScalar(nn.Module):
94+
def __init__(self, alpha: float):
95+
super().__init__()
96+
self.alpha = alpha
97+
98+
def forward(self, x: torch.Tensor, y: float):
99+
return torch.add(x, y, alpha=self.alpha)
100+
101+
model = AddScalar(alpha)
102+
103+
X = torch.randn(Xshape)
104+
Y = 2.34
105+
106+
model.eval()
107+
export_model(model, (X, Y))

0 commit comments

Comments
 (0)