Skip to content

Commit 4e8c0d1

Browse files
authored
[Backend Tester] Add avgpool tests (#13239)
Add tests for avgpooling operators. This is done in the context of #12898.
1 parent f556b96 commit 4e8c0d1

File tree

3 files changed

+486
-0
lines changed

3 files changed

+486
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
kernel_size=3,
24+
stride=None,
25+
padding=0,
26+
ceil_mode=False,
27+
count_include_pad=True,
28+
):
29+
super().__init__()
30+
self.avgpool = torch.nn.AvgPool1d(
31+
kernel_size=kernel_size,
32+
stride=stride,
33+
padding=padding,
34+
ceil_mode=ceil_mode,
35+
count_include_pad=count_include_pad,
36+
)
37+
38+
def forward(self, x):
39+
return self.avgpool(x)
40+
41+
42+
@operator_test
43+
class AvgPool1d(OperatorTest):
44+
@dtype_test
45+
def test_avgpool1d_dtype(self, flow: TestFlow, dtype) -> None:
46+
# Input shape: (batch_size, channels, length)
47+
self._test_op(
48+
Model().to(dtype),
49+
((torch.rand(1, 8, 100) * 10).to(dtype),),
50+
flow,
51+
)
52+
53+
def test_avgpool1d_kernel_size(self, flow: TestFlow) -> None:
54+
# Test with different kernel sizes
55+
self._test_op(
56+
Model(kernel_size=1),
57+
(torch.randn(1, 8, 100),),
58+
flow,
59+
)
60+
self._test_op(
61+
Model(kernel_size=5),
62+
(torch.randn(1, 8, 100),),
63+
flow,
64+
)
65+
66+
def test_avgpool1d_stride(self, flow: TestFlow) -> None:
67+
# Test with different stride values
68+
self._test_op(
69+
Model(stride=2),
70+
(torch.randn(1, 8, 100),),
71+
flow,
72+
)
73+
self._test_op(
74+
Model(stride=3),
75+
(torch.randn(1, 8, 100),),
76+
flow,
77+
)
78+
79+
def test_avgpool1d_padding(self, flow: TestFlow) -> None:
80+
# Test with different padding values
81+
self._test_op(
82+
Model(padding=1),
83+
(torch.randn(1, 8, 100),),
84+
flow,
85+
)
86+
self._test_op(
87+
Model(padding=2),
88+
(torch.randn(1, 8, 100),),
89+
flow,
90+
)
91+
92+
def test_avgpool1d_ceil_mode(self, flow: TestFlow) -> None:
93+
# Test with ceil_mode=True
94+
self._test_op(
95+
Model(ceil_mode=True),
96+
(torch.randn(1, 8, 100),),
97+
flow,
98+
)
99+
100+
def test_avgpool1d_count_include_pad(self, flow: TestFlow) -> None:
101+
# Test with count_include_pad=False
102+
self._test_op(
103+
Model(padding=1, count_include_pad=False),
104+
(torch.randn(1, 8, 100),),
105+
flow,
106+
)
107+
108+
def test_avgpool1d_batch_sizes(self, flow: TestFlow) -> None:
109+
# Test with batch inputs
110+
self._test_op(
111+
Model(),
112+
(torch.randn(2, 8, 100),),
113+
flow,
114+
)
115+
self._test_op(
116+
Model(),
117+
(torch.randn(8, 8, 100),),
118+
flow,
119+
)
120+
self._test_op(
121+
Model(),
122+
(torch.randn(16, 8, 100),),
123+
flow,
124+
)
125+
126+
def test_avgpool1d_input_sizes(self, flow: TestFlow) -> None:
127+
# Test with different input sizes
128+
self._test_op(
129+
Model(),
130+
(torch.randn(1, 4, 100),),
131+
flow,
132+
)
133+
self._test_op(
134+
Model(),
135+
(torch.randn(1, 16, 100),),
136+
flow,
137+
)
138+
139+
def test_avgpool1d_combinations(self, flow: TestFlow) -> None:
140+
# Test with combinations of parameters
141+
self._test_op(
142+
Model(kernel_size=2, stride=2, padding=1),
143+
(torch.randn(1, 8, 100),),
144+
flow,
145+
)
146+
self._test_op(
147+
Model(kernel_size=3, stride=2, padding=1, ceil_mode=True),
148+
(torch.randn(1, 8, 100),),
149+
flow,
150+
)
151+
self._test_op(
152+
Model(kernel_size=2, stride=2, padding=1, count_include_pad=False),
153+
(torch.randn(1, 8, 100),),
154+
flow,
155+
)
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
kernel_size=3,
24+
stride=None,
25+
padding=0,
26+
ceil_mode=False,
27+
count_include_pad=True,
28+
):
29+
super().__init__()
30+
31+
# Create the avgpool layer with the given parameters
32+
# torch.nn.AvgPool2d accepts both int and tuple types for kernel_size, stride, and padding
33+
self.avgpool = torch.nn.AvgPool2d(
34+
kernel_size=kernel_size,
35+
stride=stride,
36+
padding=padding,
37+
ceil_mode=ceil_mode,
38+
count_include_pad=count_include_pad,
39+
)
40+
41+
def forward(self, x):
42+
return self.avgpool(x)
43+
44+
45+
@operator_test
46+
class AvgPool2d(OperatorTest):
47+
@dtype_test
48+
def test_avgpool2d_dtype(self, flow: TestFlow, dtype) -> None:
49+
# Input shape: (batch_size, channels, height, width)
50+
self._test_op(
51+
Model().to(dtype),
52+
((torch.rand(1, 8, 20, 20) * 10).to(dtype),),
53+
flow,
54+
)
55+
56+
def test_avgpool2d_kernel_size(self, flow: TestFlow) -> None:
57+
# Test with different kernel sizes
58+
self._test_op(
59+
Model(kernel_size=1),
60+
(torch.randn(1, 8, 20, 20),),
61+
flow,
62+
)
63+
self._test_op(
64+
Model(kernel_size=5),
65+
(torch.randn(1, 8, 20, 20),),
66+
flow,
67+
)
68+
self._test_op(
69+
Model(kernel_size=(3, 2)),
70+
(torch.randn(1, 8, 20, 20),),
71+
flow,
72+
)
73+
74+
def test_avgpool2d_stride(self, flow: TestFlow) -> None:
75+
# Test with different stride values
76+
self._test_op(
77+
Model(stride=2),
78+
(torch.randn(1, 8, 20, 20),),
79+
flow,
80+
)
81+
self._test_op(
82+
Model(stride=(2, 1)),
83+
(torch.randn(1, 8, 20, 20),),
84+
flow,
85+
)
86+
87+
def test_avgpool2d_padding(self, flow: TestFlow) -> None:
88+
# Test with different padding values
89+
self._test_op(
90+
Model(padding=1),
91+
(torch.randn(1, 8, 20, 20),),
92+
flow,
93+
)
94+
self._test_op(
95+
Model(padding=(1, 2)),
96+
(torch.randn(1, 8, 20, 20),),
97+
flow,
98+
)
99+
100+
def test_avgpool2d_ceil_mode(self, flow: TestFlow) -> None:
101+
# Test with ceil_mode=True
102+
self._test_op(
103+
Model(ceil_mode=True),
104+
(torch.randn(1, 8, 20, 20),),
105+
flow,
106+
)
107+
108+
def test_avgpool2d_count_include_pad(self, flow: TestFlow) -> None:
109+
# Test with count_include_pad=False
110+
self._test_op(
111+
Model(padding=1, count_include_pad=False),
112+
(torch.randn(1, 8, 20, 20),),
113+
flow,
114+
)
115+
116+
def test_avgpool2d_batch_sizes(self, flow: TestFlow) -> None:
117+
# Test with batch inputs
118+
self._test_op(
119+
Model(),
120+
(torch.randn(2, 8, 20, 20),),
121+
flow,
122+
)
123+
self._test_op(
124+
Model(),
125+
(torch.randn(8, 8, 20, 20),),
126+
flow,
127+
)
128+
self._test_op(
129+
Model(),
130+
(torch.randn(16, 8, 20, 20),),
131+
flow,
132+
)
133+
134+
def test_avgpool2d_input_sizes(self, flow: TestFlow) -> None:
135+
# Test with different input sizes
136+
self._test_op(
137+
Model(),
138+
(torch.randn(1, 4, 20, 20),),
139+
flow,
140+
)
141+
self._test_op(
142+
Model(),
143+
(torch.randn(1, 16, 20, 20),),
144+
flow,
145+
)
146+
147+
def test_avgpool2d_combinations(self, flow: TestFlow) -> None:
148+
# Test with combinations of parameters
149+
self._test_op(
150+
Model(kernel_size=2, stride=2, padding=1),
151+
(torch.randn(1, 8, 20, 20),),
152+
flow,
153+
)
154+
self._test_op(
155+
Model(kernel_size=3, stride=2, padding=1, ceil_mode=True),
156+
(torch.randn(1, 8, 21, 21),),
157+
flow,
158+
)
159+
self._test_op(
160+
Model(
161+
kernel_size=(2, 3),
162+
stride=(2, 1),
163+
padding=(1, 0),
164+
count_include_pad=False,
165+
),
166+
(torch.randn(1, 8, 20, 20),),
167+
flow,
168+
)

0 commit comments

Comments
 (0)