Skip to content

Commit 574e109

Browse files
authored
[Backend Tester] Add convolution tests (#12847)
Add tests for convolution-family operators.
1 parent da8af21 commit 574e109

File tree

6 files changed

+954
-0
lines changed

6 files changed

+954
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# pyre-unsafe
8+
9+
10+
import torch
11+
from executorch.backends.test.suite.flow import TestFlow
12+
13+
from executorch.backends.test.suite.operators import (
14+
dtype_test,
15+
operator_test,
16+
OperatorTest,
17+
)
18+
19+
20+
class Model(torch.nn.Module):
21+
def __init__(
22+
self,
23+
in_channels=3,
24+
out_channels=6,
25+
kernel_size=3,
26+
stride=1,
27+
padding=0,
28+
dilation=1,
29+
groups=1,
30+
bias=True,
31+
padding_mode="zeros",
32+
):
33+
super().__init__()
34+
self.conv = torch.nn.Conv1d(
35+
in_channels=in_channels,
36+
out_channels=out_channels,
37+
kernel_size=kernel_size,
38+
stride=stride,
39+
padding=padding,
40+
dilation=dilation,
41+
groups=groups,
42+
bias=bias,
43+
padding_mode=padding_mode,
44+
)
45+
46+
def forward(self, x):
47+
return self.conv(x)
48+
49+
50+
@operator_test
51+
class Conv1d(OperatorTest):
52+
@dtype_test
53+
def test_conv1d_dtype(self, flow: TestFlow, dtype) -> None:
54+
self._test_op(
55+
Model().to(dtype),
56+
((torch.rand(4, 3, 50) * 10).to(dtype),),
57+
flow,
58+
)
59+
60+
def test_conv1d_basic(self, flow: TestFlow) -> None:
61+
self._test_op(
62+
Model(),
63+
(torch.randn(4, 3, 50),),
64+
flow,
65+
)
66+
67+
def test_conv1d_kernel_size(self, flow: TestFlow) -> None:
68+
self._test_op(
69+
Model(kernel_size=1),
70+
(torch.randn(4, 3, 50),),
71+
flow,
72+
)
73+
self._test_op(
74+
Model(kernel_size=5),
75+
(torch.randn(4, 3, 50),),
76+
flow,
77+
)
78+
79+
def test_conv1d_stride(self, flow: TestFlow) -> None:
80+
self._test_op(
81+
Model(stride=2),
82+
(torch.randn(4, 3, 50),),
83+
flow,
84+
)
85+
86+
def test_conv1d_padding(self, flow: TestFlow) -> None:
87+
self._test_op(
88+
Model(padding=1),
89+
(torch.randn(4, 3, 50),),
90+
flow,
91+
)
92+
self._test_op(
93+
Model(padding=2),
94+
(torch.randn(4, 3, 50),),
95+
flow,
96+
)
97+
98+
def test_conv1d_dilation(self, flow: TestFlow) -> None:
99+
self._test_op(
100+
Model(dilation=2),
101+
(torch.randn(4, 3, 50),),
102+
flow,
103+
)
104+
105+
def test_conv1d_groups(self, flow: TestFlow) -> None:
106+
self._test_op(
107+
Model(in_channels=6, out_channels=6, groups=3),
108+
(torch.randn(4, 6, 50),),
109+
flow,
110+
)
111+
112+
def test_conv1d_depthwise(self, flow: TestFlow) -> None:
113+
self._test_op(
114+
Model(in_channels=8, out_channels=8, groups=8),
115+
(torch.randn(4, 8, 50),),
116+
flow,
117+
)
118+
119+
def test_conv1d_no_bias(self, flow: TestFlow) -> None:
120+
self._test_op(
121+
Model(bias=False),
122+
(torch.randn(4, 3, 50),),
123+
flow,
124+
)
125+
126+
def test_conv1d_padding_modes(self, flow: TestFlow) -> None:
127+
for mode in ["zeros", "reflect", "replicate", "circular"]:
128+
self._test_op(
129+
Model(padding=1, padding_mode=mode),
130+
(torch.randn(4, 3, 50),),
131+
flow,
132+
)
133+
134+
def test_conv1d_channels(self, flow: TestFlow) -> None:
135+
self._test_op(
136+
Model(in_channels=1, out_channels=1),
137+
(torch.randn(4, 1, 50),),
138+
flow,
139+
)
140+
self._test_op(
141+
Model(in_channels=5, out_channels=10),
142+
(torch.randn(4, 5, 50),),
143+
flow,
144+
)
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# pyre-unsafe
8+
9+
from typing import Tuple, Union
10+
11+
import torch
12+
from executorch.backends.test.suite.flow import TestFlow
13+
14+
from executorch.backends.test.suite.operators import (
15+
dtype_test,
16+
operator_test,
17+
OperatorTest,
18+
)
19+
20+
21+
class Model(torch.nn.Module):
22+
def __init__(
23+
self,
24+
in_channels=3,
25+
out_channels=6,
26+
kernel_size: Union[int, Tuple[int, int]] = 3,
27+
stride: Union[int, Tuple[int, int]] = 1,
28+
padding: Union[int, Tuple[int, int]] = 0,
29+
dilation: Union[int, Tuple[int, int]] = 1,
30+
groups=1,
31+
bias=True,
32+
padding_mode="zeros",
33+
):
34+
super().__init__()
35+
self.conv = torch.nn.Conv2d(
36+
in_channels=in_channels,
37+
out_channels=out_channels,
38+
kernel_size=kernel_size,
39+
stride=stride,
40+
padding=padding,
41+
dilation=dilation,
42+
groups=groups,
43+
bias=bias,
44+
padding_mode=padding_mode,
45+
)
46+
47+
def forward(self, x):
48+
return self.conv(x)
49+
50+
51+
@operator_test
52+
class Conv2d(OperatorTest):
53+
@dtype_test
54+
def test_conv2d_dtype(self, flow: TestFlow, dtype) -> None:
55+
self._test_op(
56+
Model().to(dtype),
57+
((torch.rand(4, 3, 16, 16) * 10).to(dtype),),
58+
flow,
59+
)
60+
61+
def test_conv2d_basic(self, flow: TestFlow) -> None:
62+
self._test_op(
63+
Model(),
64+
(torch.randn(4, 3, 16, 16),),
65+
flow,
66+
)
67+
68+
def test_conv2d_kernel_size(self, flow: TestFlow) -> None:
69+
self._test_op(
70+
Model(kernel_size=1),
71+
(torch.randn(4, 3, 16, 16),),
72+
flow,
73+
)
74+
self._test_op(
75+
Model(kernel_size=5),
76+
(torch.randn(4, 3, 16, 16),),
77+
flow,
78+
)
79+
self._test_op(
80+
Model(kernel_size=(3, 5)),
81+
(torch.randn(4, 3, 16, 16),),
82+
flow,
83+
)
84+
85+
def test_conv2d_stride(self, flow: TestFlow) -> None:
86+
self._test_op(
87+
Model(stride=2),
88+
(torch.randn(4, 3, 16, 16),),
89+
flow,
90+
)
91+
self._test_op(
92+
Model(stride=(2, 1)),
93+
(torch.randn(4, 3, 16, 16),),
94+
flow,
95+
)
96+
97+
def test_conv2d_padding(self, flow: TestFlow) -> None:
98+
self._test_op(
99+
Model(padding=1),
100+
(torch.randn(4, 3, 16, 16),),
101+
flow,
102+
)
103+
self._test_op(
104+
Model(padding=(1, 2)),
105+
(torch.randn(4, 3, 16, 16),),
106+
flow,
107+
)
108+
109+
def test_conv2d_dilation(self, flow: TestFlow) -> None:
110+
self._test_op(
111+
Model(dilation=2),
112+
(torch.randn(4, 3, 16, 16),),
113+
flow,
114+
)
115+
self._test_op(
116+
Model(dilation=(2, 1)),
117+
(torch.randn(4, 3, 16, 16),),
118+
flow,
119+
)
120+
121+
def test_conv2d_groups(self, flow: TestFlow) -> None:
122+
self._test_op(
123+
Model(in_channels=6, out_channels=6, groups=3),
124+
(torch.randn(4, 6, 16, 16),),
125+
flow,
126+
)
127+
128+
def test_conv2d_depthwise(self, flow: TestFlow) -> None:
129+
self._test_op(
130+
Model(in_channels=8, out_channels=8, groups=8),
131+
(torch.randn(4, 8, 16, 16),),
132+
flow,
133+
)
134+
135+
def test_conv2d_no_bias(self, flow: TestFlow) -> None:
136+
self._test_op(
137+
Model(bias=False),
138+
(torch.randn(4, 3, 16, 16),),
139+
flow,
140+
)
141+
142+
def test_conv2d_padding_modes(self, flow: TestFlow) -> None:
143+
for mode in ["zeros", "reflect", "replicate", "circular"]:
144+
self._test_op(
145+
Model(padding=1, padding_mode=mode),
146+
(torch.randn(4, 3, 16, 16),),
147+
flow,
148+
)
149+
150+
def test_conv2d_channels(self, flow: TestFlow) -> None:
151+
self._test_op(
152+
Model(in_channels=1, out_channels=1),
153+
(torch.randn(4, 1, 16, 16),),
154+
flow,
155+
)
156+
self._test_op(
157+
Model(in_channels=5, out_channels=10),
158+
(torch.randn(4, 5, 16, 16),),
159+
flow,
160+
)
161+
162+
def test_conv2d_different_spatial_dims(self, flow: TestFlow) -> None:
163+
self._test_op(
164+
Model(),
165+
(torch.randn(4, 3, 20, 16),),
166+
flow,
167+
)

0 commit comments

Comments
 (0)