Skip to content

Commit 34b6860

Browse files
author
Feiyu Chan
authored
fix fftshift/ifftshift on static mode (#36748)
* fix fftshift/ifftshift on static mode * update roll_op version * add more test cases for fftshift/ifftshift
1 parent 6838a18 commit 34b6860

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

paddle/fluid/operators/roll_op.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,12 @@ REGISTER_OP_VERSION(roll)
183183
"(std::vector<int64_t>) Axis along which to roll. "
184184
"It must have the same size with shifts, or size = 0.",
185185
std::vector<int64_t>())
186-
.DeleteAttr(
187-
"dims",
188-
"(std::vector<int64_t>) Dims along which to roll. "
189-
"It must have the same size with shifts, or size = 0."));
186+
.DeleteAttr("dims",
187+
"(std::vector<int64_t>) Dims along which to roll. "
188+
"It must have the same size with shifts, or size = 0."))
189+
.AddCheckpoint(
190+
R"ROC(Upgrade roll add a dispensable input "ShiftsTensor".)ROC",
191+
paddle::framework::compatible::OpVersionDesc().NewInput(
192+
"ShiftsTensor",
193+
"The number of places by which the elements of"
194+
"the tensor are shifted."));

python/paddle/fft.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,13 +1300,13 @@ def fftshift(x, axes=None, name=None):
13001300
shape = paddle.shape(x)
13011301
if axes is None:
13021302
# shift all axes
1303-
rank = paddle.rank(x).reshape([1])
1304-
axes = axes or paddle.arange(0, rank)
1305-
shifts = [size // 2 for size in shape]
1303+
rank = len(x.shape)
1304+
axes = list(range(0, rank))
1305+
shifts = shape // 2
13061306
elif isinstance(axes, int):
13071307
shifts = shape[axes] // 2
13081308
else:
1309-
shifts = [shape[ax] // 2 for ax in axes]
1309+
shifts = paddle.concat([shape[ax] // 2 for ax in axes])
13101310
return paddle.roll(x, shifts, axes, name=name)
13111311

13121312

@@ -1343,13 +1343,13 @@ def ifftshift(x, axes=None, name=None):
13431343
shape = paddle.shape(x)
13441344
if axes is None:
13451345
# shift all axes
1346-
rank = paddle.rank(x).reshape([1])
1347-
axes = axes or paddle.arange(0, rank)
1348-
shifts = [-size // 2 for size in shape]
1346+
rank = len(x.shape)
1347+
axes = list(range(0, rank))
1348+
shifts = shape // 2
13491349
elif isinstance(axes, int):
13501350
shifts = -shape[axes] // 2
13511351
else:
1352-
shifts = [-shape[ax] // 2 for ax in axes]
1352+
shifts = paddle.concat([-shape[ax] // 2 for ax in axes])
13531353
return paddle.roll(x, shifts, axes, name=name)
13541354

13551355

python/paddle/fluid/tests/unittests/fft/test_fft.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,10 +1009,11 @@ def test_rfftfreq(self):
10091009

10101010

10111011
@place(DEVICES)
1012-
@parameterize((TEST_CASE_NAME, 'x', 'axes', 'dtype'), [
1013-
('test_1d', np.random.randn(10), (0, ), 'float64'),
1014-
('test_2d', np.random.randn(10, 10), (0, 1), 'float64'),
1015-
])
1012+
@parameterize(
1013+
(TEST_CASE_NAME, 'x', 'axes', 'dtype'),
1014+
[('test_1d', np.random.randn(10), (0, ), 'float64'),
1015+
('test_2d', np.random.randn(10, 10), (0, 1), 'float64'),
1016+
('test_2d_with_all_axes', np.random.randn(10, 10), None, 'float64')])
10161017
class TestFftShift(unittest.TestCase):
10171018
def test_fftshift(self):
10181019
"""Test fftshift with norm condition
@@ -1030,6 +1031,7 @@ def test_fftshift(self):
10301031
@parameterize((TEST_CASE_NAME, 'x', 'axes'), [
10311032
('test_1d', np.random.randn(10), (0, ), 'float64'),
10321033
('test_2d', np.random.randn(10, 10), (0, 1), 'float64'),
1034+
('test_2d_with_all_axes', np.random.randn(10, 10), None, 'float64'),
10331035
])
10341036
class TestIfftShift(unittest.TestCase):
10351037
def test_ifftshift(self):

0 commit comments

Comments
 (0)