Skip to content

Commit 1f55e3a

Browse files
authored
fix default stride value (#6248)
* fix default stride value * rename * Remove numpy * fix autotest to default * modify x to input
1 parent 1868c19 commit 1f55e3a

File tree

6 files changed

+158
-29
lines changed

6 files changed

+158
-29
lines changed

oneflow/core/functional/functional_api.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -638,23 +638,23 @@
638638

639639
- name: "max_pool1d"
640640
signature:
641-
'TensorTuple (Tensor x, Int32List[1] kernel_size, Int32List[1] stride=1,
641+
'TensorTuple (Tensor input, Int32List[1] kernel_size, Int32List[1] stride=None,
642642
Int32List[1] padding=0, Int32List[1] dilation=1,
643643
Bool return_indices=True, Bool ceil_mode=False,
644644
String data_format="channels_first") => Maxpool1D'
645645
bind_python: True
646646

647647
- name: "max_pool2d"
648648
signature:
649-
'TensorTuple (Tensor x, Int32List[2] kernel_size, Int32List[2] stride=1,
649+
'TensorTuple (Tensor input, Int32List[2] kernel_size, Int32List[2] stride=None,
650650
Int32List[2] padding=0, Int32List[2] dilation=1,
651651
Bool return_indices=True, Bool ceil_mode=False,
652652
String data_format="channels_first") => Maxpool2D'
653653
bind_python: True
654654

655655
- name: "max_pool3d"
656656
signature:
657-
'TensorTuple (Tensor x, Int32List[3] kernel_size, Int32List[3] stride=1,
657+
'TensorTuple (Tensor input, Int32List[3] kernel_size, Int32List[3] stride=None,
658658
Int32List[3] padding=0, Int32List[3] dilation=1,
659659
Bool return_indices=True, Bool ceil_mode=False,
660660
String data_format="channels_first") => Maxpool3D'
@@ -996,21 +996,21 @@
996996

997997
- name: "avg_pool1d"
998998
signature:
999-
'Tensor (Tensor x, Int32List[1] kernel_size, Int32List[1] stride=1,
999+
'Tensor (Tensor input, Int32List[1] kernel_size, Int32List[1] stride=None,
10001000
Int32List[1] padding=0, Bool ceil_mode=False, Bool count_include_pad=True,
10011001
Int64 divisor_override=0, String data_format="channels_first") => Avgpool1D'
10021002
bind_python: True
10031003

10041004
- name: "avg_pool2d"
10051005
signature:
1006-
'Tensor (Tensor x, Int32List[2] kernel_size, Int32List[2] stride=1,
1006+
'Tensor (Tensor input, Int32List[2] kernel_size, Int32List[2] stride=None,
10071007
Int32List[2] padding=0, Bool ceil_mode=False, Bool count_include_pad=True,
10081008
Int64 divisor_override=0, String data_format="channels_first") => Avgpool2D'
10091009
bind_python: True
10101010

10111011
- name: "avg_pool3d"
10121012
signature:
1013-
'Tensor (Tensor x, Int32List[3] kernel_size, Int32List[3] stride=1,
1013+
'Tensor (Tensor input, Int32List[3] kernel_size, Int32List[3] stride=None,
10141014
Int32List[3] padding=0, Bool ceil_mode=False, Bool count_include_pad=True,
10151015
Int64 divisor_override=0, String data_format="channels_first") => Avgpool3D'
10161016
bind_python: True

oneflow/core/functional/impl/nn_functor.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,20 @@ class PoolingNDFunctor {
266266
virtual ~PoolingNDFunctor() = default;
267267
Maybe<TensorTuple> operator()(const std::shared_ptr<one::Tensor>& x,
268268
const std::vector<int32_t>& kernel_size,
269-
const std::vector<int32_t>& stride,
269+
const Optional<std::vector<int32_t>>& stride,
270270
const std::vector<int32_t>& padding,
271271
const std::vector<int32_t>& dilation, const bool& return_indices,
272272
const bool& ceil_mode, const std::string& data_format) const {
273273
MutableAttrMap attrs;
274274
JUST(attrs.SetAttr<std::string>("data_format", data_format));
275275
JUST(attrs.SetAttr<std::vector<int32_t>>("padding", padding));
276276
JUST(attrs.SetAttr<std::vector<int32_t>>("kernel_size", kernel_size));
277-
JUST(attrs.SetAttr<std::vector<int32_t>>("stride", stride));
277+
if (stride.has_value()) {
278+
JUST(attrs.SetAttr<std::vector<int32_t>>("stride", *JUST(stride.value())));
279+
} else {
280+
JUST(attrs.SetAttr<std::vector<int32_t>>(
281+
"stride", kernel_size)); // If stride is None, we set it as kernel_size to align Pytorch.
282+
}
278283
JUST(attrs.SetAttr<std::vector<int32_t>>("dilation", dilation));
279284
JUST(attrs.SetAttr<bool>("return_indices", return_indices));
280285
JUST(attrs.SetAttr<bool>("ceil_mode", ceil_mode));
@@ -820,14 +825,20 @@ class AvgPoolingNDFunctor {
820825
virtual ~AvgPoolingNDFunctor() = default;
821826
Maybe<Tensor> operator()(const std::shared_ptr<one::Tensor>& x,
822827
const std::vector<int32_t>& kernel_size,
823-
const std::vector<int32_t>& stride, const std::vector<int32_t>& padding,
824-
const bool& ceil_mode, const bool& count_include_pad,
825-
const int64_t& divisor_override, const std::string& data_format) const {
828+
const Optional<std::vector<int32_t>>& stride,
829+
const std::vector<int32_t>& padding, const bool& ceil_mode,
830+
const bool& count_include_pad, const int64_t& divisor_override,
831+
const std::string& data_format) const {
826832
MutableAttrMap attrs;
827833
JUST(attrs.SetAttr<std::string>("data_format", data_format));
828834
JUST(attrs.SetAttr<std::vector<int32_t>>("padding", padding));
829835
JUST(attrs.SetAttr<std::vector<int32_t>>("kernel_size", kernel_size));
830-
JUST(attrs.SetAttr<std::vector<int32_t>>("stride", stride));
836+
if (stride.has_value()) {
837+
JUST(attrs.SetAttr<std::vector<int32_t>>("stride", *JUST(stride.value())));
838+
} else {
839+
JUST(attrs.SetAttr<std::vector<int32_t>>(
840+
"stride", kernel_size)); // If stride is None, we set it as kernel_size to align Pytorch.
841+
}
831842
JUST(attrs.SetAttr<bool>("ceil_mode", ceil_mode));
832843
JUST(attrs.SetAttr<bool>("count_include_pad", count_include_pad));
833844
JUST(attrs.SetAttr<int64_t>("divisor_override", divisor_override));

python/oneflow/nn/functional/functional_maxpool.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
def max_pool1d(
2121
x,
2222
kernel_size,
23-
stride=1,
23+
stride=None,
2424
padding=0,
2525
dilation=1,
2626
return_indices=False,
@@ -46,7 +46,7 @@ def max_pool1d(
4646
def max_pool2d(
4747
x,
4848
kernel_size,
49-
stride=1,
49+
stride=None,
5050
padding=0,
5151
dilation=1,
5252
return_indices=False,
@@ -72,7 +72,7 @@ def max_pool2d(
7272
def max_pool3d(
7373
x,
7474
kernel_size,
75-
stride=1,
75+
stride=None,
7676
padding=0,
7777
dilation=1,
7878
return_indices=False,

python/oneflow/test/modules/test_avgpool.py

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
import unittest
1717

1818
import oneflow as flow
19+
from oneflow.test_utils.automated_test_util.generators import constant, random_bool
1920
import oneflow.unittest
2021
from automated_test_util import *
2122

2223

2324
@flow.unittest.skip_unless_1n1d()
2425
class TestAvgPoolingModule(flow.unittest.TestCase):
25-
@autotest(n=100)
26+
@autotest()
2627
def test_avgpool1d_with_random_data(test_case):
2728
m = torch.nn.AvgPool1d(
2829
kernel_size=random(4, 6),
@@ -38,7 +39,7 @@ def test_avgpool1d_with_random_data(test_case):
3839
y = m(x)
3940
return y
4041

41-
@autotest(n=100)
42+
@autotest()
4243
def test_avgpool2d_with_random_data(test_case):
4344
m = torch.nn.AvgPool2d(
4445
kernel_size=random(4, 6),
@@ -57,7 +58,7 @@ def test_avgpool2d_with_random_data(test_case):
5758
y = m(x)
5859
return y
5960

60-
@autotest(n=100)
61+
@autotest()
6162
def test_avgpool3d_with_random_data(test_case):
6263
m = torch.nn.AvgPool3d(
6364
kernel_size=random(4, 6),
@@ -77,5 +78,54 @@ def test_avgpool3d_with_random_data(test_case):
7778
return y
7879

7980

81+
@flow.unittest.skip_unless_1n1d()
82+
class TestAvgPoolingFunctional(flow.unittest.TestCase):
83+
@autotest()
84+
def test_avgpool1d_functional(test_case):
85+
device = random_device()
86+
x = random_pytorch_tensor(ndim=3, dim2=random(20, 22)).to(device)
87+
y = torch.nn.functional.avg_pool1d(
88+
x,
89+
kernel_size=random(1, 6).to(int),
90+
stride=random(1, 3).to(int) | nothing(),
91+
padding=random(1, 3).to(int),
92+
ceil_mode=random_bool(),
93+
count_include_pad=random_bool(),
94+
)
95+
return y
96+
97+
@autotest()
98+
def test_avgpool2d_functional(test_case):
99+
device = random_device()
100+
x = random_pytorch_tensor(ndim=4, dim2=random(20, 22), dim3=random(20, 22)).to(
101+
device
102+
)
103+
y = torch.nn.functional.avg_pool2d(
104+
x,
105+
kernel_size=random(1, 6).to(int),
106+
stride=random(1, 3).to(int) | nothing(),
107+
padding=random(1, 3).to(int),
108+
ceil_mode=random_bool(),
109+
count_include_pad=random_bool(),
110+
)
111+
return y
112+
113+
@autotest()
114+
def test_avgpool3d_functional(test_case):
115+
device = random_device()
116+
x = random_pytorch_tensor(
117+
ndim=5, dim2=random(20, 22), dim3=random(20, 22), dim4=random(20, 22)
118+
).to(device)
119+
y = torch.nn.functional.avg_pool3d(
120+
x,
121+
kernel_size=random(1, 6).to(int),
122+
stride=random(1, 3).to(int) | nothing(),
123+
padding=random(1, 3).to(int),
124+
ceil_mode=random_bool(),
125+
count_include_pad=random_bool(),
126+
)
127+
return y
128+
129+
80130
if __name__ == "__main__":
81131
unittest.main()

python/oneflow/test/modules/test_pooling.py renamed to python/oneflow/test/modules/test_maxpool.py

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def unpack_indices(dual_object):
2828

2929
@flow.unittest.skip_unless_1n1d()
3030
class TestMaxPooling(flow.unittest.TestCase):
31-
@autotest(n=100, auto_backward=False)
31+
@autotest(auto_backward=False)
3232
def test_maxpool1d_with_random_data(test_case):
3333
return_indices = random().to(bool).value()
3434
m = torch.nn.MaxPool1d(
@@ -50,7 +50,7 @@ def test_maxpool1d_with_random_data(test_case):
5050
else:
5151
return y, y.sum().backward()
5252

53-
@autotest(n=100, auto_backward=False)
53+
@autotest(auto_backward=False)
5454
def test_maxpool2d_with_random_data(test_case):
5555
return_indices = random().to(bool).value()
5656
m = torch.nn.MaxPool2d(
@@ -74,7 +74,7 @@ def test_maxpool2d_with_random_data(test_case):
7474
else:
7575
return y, y.sum().backward()
7676

77-
@autotest(n=100, auto_backward=False)
77+
@autotest(auto_backward=False)
7878
def test_maxpool3d_with_random_data(test_case):
7979
return_indices = random().to(bool).value()
8080
m = torch.nn.MaxPool3d(
@@ -99,5 +99,72 @@ def test_maxpool3d_with_random_data(test_case):
9999
return y, y.sum().backward()
100100

101101

102+
@flow.unittest.skip_unless_1n1d()
103+
class TestMaxPoolingFunctional(flow.unittest.TestCase):
104+
@autotest(auto_backward=False)
105+
def test_maxpool1d_with_random_data(test_case):
106+
return_indices = random().to(bool).value()
107+
device = random_device()
108+
x = random_pytorch_tensor(ndim=3, dim2=random(20, 22)).to(device)
109+
y = torch.nn.functional.max_pool1d(
110+
x,
111+
kernel_size=random(4, 6).to(int),
112+
stride=random(1, 3).to(int) | nothing(),
113+
padding=random(1, 3).to(int) | nothing(),
114+
dilation=random(2, 4).to(int) | nothing(),
115+
ceil_mode=random().to(bool),
116+
return_indices=return_indices,
117+
)
118+
119+
if return_indices:
120+
return unpack_indices(y)
121+
else:
122+
return y, y.sum().backward()
123+
124+
@autotest(auto_backward=False)
125+
def test_maxpool2d_with_random_data(test_case):
126+
return_indices = random().to(bool).value()
127+
device = random_device()
128+
x = random_pytorch_tensor(ndim=4, dim2=random(20, 22), dim3=random(20, 22)).to(
129+
device
130+
)
131+
y = torch.nn.functional.max_pool2d(
132+
x,
133+
kernel_size=random(4, 6).to(int),
134+
stride=random(1, 3).to(int) | nothing(),
135+
padding=random(1, 3).to(int) | nothing(),
136+
dilation=random(2, 4).to(int) | nothing(),
137+
ceil_mode=random().to(bool),
138+
return_indices=return_indices,
139+
)
140+
141+
if return_indices:
142+
return unpack_indices(y)
143+
else:
144+
return y, y.sum().backward()
145+
146+
@autotest(auto_backward=False)
147+
def test_maxpool3d_with_random_data(test_case):
148+
return_indices = random().to(bool).value()
149+
device = random_device()
150+
x = random_pytorch_tensor(
151+
ndim=5, dim2=random(20, 22), dim3=random(20, 22), dim4=random(20, 22)
152+
).to(device)
153+
y = torch.nn.functional.max_pool3d(
154+
x,
155+
kernel_size=random(4, 6).to(int),
156+
stride=random(1, 3).to(int) | nothing(),
157+
padding=random(1, 3).to(int) | nothing(),
158+
dilation=random(2, 4).to(int) | nothing(),
159+
ceil_mode=random().to(bool),
160+
return_indices=return_indices,
161+
)
162+
163+
if return_indices:
164+
return unpack_indices(y)
165+
else:
166+
return y, y.sum().backward()
167+
168+
102169
if __name__ == "__main__":
103170
unittest.main()

tools/functional/generator.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,17 @@ def __init__(self, fmt, keyword_only=False):
222222
sp = self._name.find("=")
223223
if sp != -1:
224224
self._default_value = _normalize(self._name[sp + 1 :])
225-
if self._type.endswith("List"):
226-
_value_list = [
227-
self._default_value for i in range(self._size)
228-
] # For int32List[2] = 1, _value_list will be [1, 1]
229-
self._default_cpp_value = (
230-
"{" + ", ".join(_value_list) + "}"
231-
) # [1, 1] -> "{1, 1}"
232-
elif self._default_value == "None":
225+
if self._default_value == "None":
233226
self._optional = True
234227
self._default_cpp_value = ""
228+
elif self._type.endswith("List"):
229+
if self._default_value != "None":
230+
_value_list = [
231+
self._default_value for i in range(self._size)
232+
] # For int32List[2] = 2, _value_list will be ["2", "2"]
233+
self._default_cpp_value = (
234+
"{" + ", ".join(_value_list) + "}"
235+
) # ["2", "2"] -> "{2, 2}"
235236
elif self._default_value in value_aliases:
236237
self._default_cpp_value = value_aliases[self._default_value]
237238
else:

0 commit comments

Comments
 (0)