Skip to content

Commit e8d6913

Browse files
Feiyu Chanlijiaqi0612
andauthored
Unittest of op with FFT C2C, C2R and r2c added (#38)
* documents and single case * test c2r case * New C2R Python layer normal and exception use cases * Documentation of the common interfaces of c2r and c2c * Unittest of op with FFT C2C, C2R and r2c added Co-authored-by: lijiaqi <[email protected]>
1 parent f002f3a commit e8d6913

File tree

3 files changed

+141
-31
lines changed

3 files changed

+141
-31
lines changed

python/paddle/fluid/tests/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ endif()
665665
add_subdirectory(sequence)
666666
add_subdirectory(dygraph_to_static)
667667
add_subdirectory(rnn)
668+
add_subdirectory(fft)
668669

669670
if (WITH_XPU)
670671
add_subdirectory(xpu)

python/paddle/fluid/tests/unittests/fft/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,3 @@ string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}")
44
foreach(TEST_OP ${TEST_OPS})
55
py_test_modules(${TEST_OP} MODULES ${TEST_OP})
66
endforeach(TEST_OP)
7-
if(NOT WIN32)
8-
set_tests_properties(test_rnn_nets_static PROPERTIES TIMEOUT 120)
9-
set_tests_properties(test_rnn_nets PROPERTIES TIMEOUT 120)
10-
endif()

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

Lines changed: 140 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,166 @@
1313
# limitations under the License.
1414

1515
from __future__ import print_function
16-
17-
import sys.path
1816
import unittest
1917

2018
import numpy as np
2119
import paddle
20+
21+
import re
22+
import sys
23+
from spectral_op_np import fft_c2c, fft_r2c, fft_c2r
2224
import paddle.fluid.core as core
2325
import paddle.fluid.dygraph as dg
2426
import paddle.static as static
2527
from numpy.random import random as rand
2628
from paddle.fluid import Program, program_guard
27-
28-
from ..op_test import OpTest
29+
sys.path.append("../")
30+
from op_test import OpTest
2931

3032
paddle.enable_static()
3133

34+
TEST_CASE_NAME = 'test_case'
35+
36+
37+
def parameterize(attrs, input_values=None):
38+
39+
if isinstance(attrs, str):
40+
attrs = [attrs]
41+
input_dicts = (attrs if input_values is None else
42+
[dict(zip(attrs, vals)) for vals in input_values])
43+
44+
def decorator(base_class):
45+
test_class_module = sys.modules[base_class.__module__].__dict__
46+
for idx, input_dict in enumerate(input_dicts):
47+
test_class_dict = dict(base_class.__dict__)
48+
test_class_dict.update(input_dict)
49+
50+
name = class_name(base_class, idx, input_dict)
51+
52+
test_class_module[name] = type(name, (base_class, ),
53+
test_class_dict)
54+
55+
for method_name in list(base_class.__dict__):
56+
if method_name.startswith("test"):
57+
delattr(base_class, method_name)
58+
return base_class
59+
60+
return decorator
61+
62+
63+
def to_safe_name(s):
64+
return str(re.sub("[^a-zA-Z0-9_]+", "_", s))
65+
66+
67+
def class_name(cls, num, params_dict):
68+
suffix = to_safe_name(
69+
next((v for v in params_dict.values() if isinstance(v, str)), ""))
70+
if TEST_CASE_NAME in params_dict:
71+
suffix = to_safe_name(params_dict["test_case"])
72+
return "{}_{}{}".format(cls.__name__, num, suffix and "_" + suffix)
73+
74+
75+
@parameterize((TEST_CASE_NAME, 'x', 'axes', 'norm', 'forward'), [
76+
('test_axes_is_sqe_type', (np.random.random(
77+
(12, 14)) + 1j * np.random.random((12, 14))).astype(np.complex128),
78+
[0, 1], 'forward', True), ('test_axis_not_last', (np.random.random(
79+
(4, 4, 4)) + 1j * np.random.random((4, 4, 4))).astype(np.complex128),
80+
(0, 1), "backward", False),
81+
('test_norm_forward', (np.random.random((12, 14)) + 1j * np.random.random(
82+
(12, 14))).astype(np.complex128), (0, ), "forward",
83+
False), ('test_norm_backward', (np.random.random(
84+
(12, 14)) + 1j * np.random.random((12, 14))).astype(np.complex128),
85+
(0, ), "backward", True), ('test_norm_ortho', (np.random.random(
86+
(12, 14)) + 1j * np.random.random(
87+
(12, 14))).astype(np.complex128), (1, ), "ortho", True)
88+
])
89+
class TestFFTC2COp(OpTest):
90+
# Because framwork not support complex numerial gradient, we skip gradient check.
91+
no_need_check_grad = True
92+
93+
def setUp(self):
94+
self.op_type = "fft_c2c"
95+
96+
out = fft_c2c(self.x, self.axes, self.norm, self.forward)
97+
98+
self.inputs = {'X': self.x}
99+
self.attrs = {
100+
'axes': self.axes,
101+
'normalization': self.norm,
102+
"forward": self.forward
103+
}
104+
self.outputs = {'Out': out}
105+
106+
def test_check_output(self):
107+
self.check_output()
108+
32109

110+
@parameterize(
111+
(TEST_CASE_NAME, 'x', 'axes', 'norm', 'forward', 'last_dim_size'),
112+
[('test_axes_is_sqe_type', (np.random.random(
113+
(12, 14)) + 1j * np.random.random((12, 14))).astype(np.complex128),
114+
[0, 1], 'forward', True, 26), ('test_axis_not_last', (np.random.random(
115+
(4, 4, 4)) + 1j * np.random.random((4, 4, 4))).astype(np.complex128),
116+
(0, 1), "backward", False, None),
117+
('test_norm_forward', (np.random.random((12, 14)) + 1j * np.random.random(
118+
(12, 14))).astype(np.complex128), (0, ), "forward", False, 22),
119+
('test_norm_backward', (np.random.random((12, 14)) + 1j * np.random.random(
120+
(12, 14))).astype(np.complex128), (0, ), "backward", True,
121+
22), ('test_norm_ortho', (np.random.random(
122+
(12, 14)) + 1j * np.random.random((12, 14))).astype(np.complex128),
123+
(1, ), "ortho", True, 26)])
33124
class TestFFTC2ROp(OpTest):
125+
# Because framwork not support complex numerial gradient, we skip gradient check.
126+
no_need_check_grad = True
127+
34128
def setUp(self):
35129
self.op_type = "fft_c2r"
36-
self.init_dtype_type()
37-
self.init_input_output()
38-
self.init_grad_input_output()
39130

40-
def init_dtype_type(self):
41-
self.dtype = np.complex64
131+
out = fft_c2r(self.x, self.axes, self.norm, self.forward,
132+
self.last_dim_size)
42133

43-
def init_input_output(self):
44-
x = (np.random.random((12, 14)) + 1j * np.random.random(
45-
(12, 14))).astype(self.dtype)
46-
out = np.conj(x)
47-
48-
self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
134+
self.inputs = {'X': self.x}
135+
self.attrs = {
136+
"axes": self.axes,
137+
"normalization": self.norm,
138+
"forward": self.forward,
139+
"last_dim_size": self.last_dim_size
140+
}
49141
self.outputs = {'Out': out}
50142

51-
def init_grad_input_output(self):
52-
self.grad_out = (np.ones((12, 14)) + 1j * np.ones(
53-
(12, 14))).astype(self.dtype)
54-
self.grad_in = np.conj(self.grad_out)
55-
56143
def test_check_output(self):
57144
self.check_output()
58145

59-
def test_check_grad_normal(self):
60-
self.check_grad(
61-
['X'],
62-
'Out',
63-
user_defined_grads=[self.grad_in],
64-
user_defined_grad_outputs=[self.grad_out])
65-
from ..op_test import OpTest
146+
147+
@parameterize(
148+
(TEST_CASE_NAME, 'x', 'axes', 'norm', 'forward', 'onesided'),
149+
[('test_axes_is_sqe_type', np.random.randn(12, 14).astype(np.float64),
150+
(0, 1), 'forward', True,
151+
True), ('test_axis_not_last', np.random.randn(4, 4, 4).astype(np.float64),
152+
(0, 1), "backward", False, True),
153+
('test_norm_forward', np.random.randn(12, 14).astype(np.float64), (0, 1),
154+
"forward", False, False),
155+
('test_norm_backward', np.random.randn(12, 14).astype(np.float64), (0, ),
156+
"backward", True, False), ('test_norm_ortho',
157+
np.random.randn(12, 14).astype(np.float64),
158+
(1, ), "ortho", True, False)])
159+
class TestFFTR2COp(OpTest):
160+
# Because framwork not support complex numerial gradient, we skip gradient check.
161+
no_need_check_grad = True
162+
163+
def setUp(self):
164+
self.op_type = "fft_r2c"
165+
166+
out = fft_r2c(self.x, self.axes, self.norm, self.forward, self.onesided)
167+
168+
self.inputs = {'X': self.x}
169+
self.attrs = {
170+
'axes': self.axes,
171+
'normalization': self.norm,
172+
"forward": self.forward,
173+
'onesided': self.onesided
174+
}
175+
self.outputs = {'Out': out}
176+
177+
def test_check_output(self):
178+
self.check_output()

0 commit comments

Comments
 (0)