Skip to content

Commit e2cc507

Browse files
committed
Update
[ghstack-poisoned]
1 parent 5f672f2 commit e2cc507

File tree

3 files changed

+470
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)