Skip to content

Commit

Permalink
fix fftshift/ifftshift on static mode (#36748)
Browse files Browse the repository at this point in the history
* fix fftshift/ifftshift on static mode
* update roll_op version
* add more test cases for fftshift/ifftshift
  • Loading branch information
Feiyu Chan authored Oct 27, 2021
1 parent 6838a18 commit 34b6860
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
13 changes: 9 additions & 4 deletions paddle/fluid/operators/roll_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,12 @@ REGISTER_OP_VERSION(roll)
"(std::vector<int64_t>) Axis along which to roll. "
"It must have the same size with shifts, or size = 0.",
std::vector<int64_t>())
.DeleteAttr(
"dims",
"(std::vector<int64_t>) Dims along which to roll. "
"It must have the same size with shifts, or size = 0."));
.DeleteAttr("dims",
"(std::vector<int64_t>) Dims along which to roll. "
"It must have the same size with shifts, or size = 0."))
.AddCheckpoint(
R"ROC(Upgrade roll add a dispensable input "ShiftsTensor".)ROC",
paddle::framework::compatible::OpVersionDesc().NewInput(
"ShiftsTensor",
"The number of places by which the elements of"
"the tensor are shifted."));
16 changes: 8 additions & 8 deletions python/paddle/fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,13 +1300,13 @@ def fftshift(x, axes=None, name=None):
shape = paddle.shape(x)
if axes is None:
# shift all axes
rank = paddle.rank(x).reshape([1])
axes = axes or paddle.arange(0, rank)
shifts = [size // 2 for size in shape]
rank = len(x.shape)
axes = list(range(0, rank))
shifts = shape // 2
elif isinstance(axes, int):
shifts = shape[axes] // 2
else:
shifts = [shape[ax] // 2 for ax in axes]
shifts = paddle.concat([shape[ax] // 2 for ax in axes])
return paddle.roll(x, shifts, axes, name=name)


Expand Down Expand Up @@ -1343,13 +1343,13 @@ def ifftshift(x, axes=None, name=None):
shape = paddle.shape(x)
if axes is None:
# shift all axes
rank = paddle.rank(x).reshape([1])
axes = axes or paddle.arange(0, rank)
shifts = [-size // 2 for size in shape]
rank = len(x.shape)
axes = list(range(0, rank))
shifts = shape // 2
elif isinstance(axes, int):
shifts = -shape[axes] // 2
else:
shifts = [-shape[ax] // 2 for ax in axes]
shifts = paddle.concat([-shape[ax] // 2 for ax in axes])
return paddle.roll(x, shifts, axes, name=name)


Expand Down
10 changes: 6 additions & 4 deletions python/paddle/fluid/tests/unittests/fft/test_fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,10 +1009,11 @@ def test_rfftfreq(self):


@place(DEVICES)
@parameterize((TEST_CASE_NAME, 'x', 'axes', 'dtype'), [
('test_1d', np.random.randn(10), (0, ), 'float64'),
('test_2d', np.random.randn(10, 10), (0, 1), 'float64'),
])
@parameterize(
(TEST_CASE_NAME, 'x', 'axes', 'dtype'),
[('test_1d', np.random.randn(10), (0, ), 'float64'),
('test_2d', np.random.randn(10, 10), (0, 1), 'float64'),
('test_2d_with_all_axes', np.random.randn(10, 10), None, 'float64')])
class TestFftShift(unittest.TestCase):
def test_fftshift(self):
"""Test fftshift with norm condition
Expand All @@ -1030,6 +1031,7 @@ def test_fftshift(self):
@parameterize((TEST_CASE_NAME, 'x', 'axes'), [
('test_1d', np.random.randn(10), (0, ), 'float64'),
('test_2d', np.random.randn(10, 10), (0, 1), 'float64'),
('test_2d_with_all_axes', np.random.randn(10, 10), None, 'float64'),
])
class TestIfftShift(unittest.TestCase):
def test_ifftshift(self):
Expand Down

0 comments on commit 34b6860

Please sign in to comment.