|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | from __future__ import print_function |
16 | | - |
17 | | -import sys.path |
18 | 16 | import unittest |
19 | 17 |
|
20 | 18 | import numpy as np |
21 | 19 | import paddle |
| 20 | + |
| 21 | +import re |
| 22 | +import sys |
| 23 | +from spectral_op_np import fft_c2c, fft_r2c, fft_c2r |
22 | 24 | import paddle.fluid.core as core |
23 | 25 | import paddle.fluid.dygraph as dg |
24 | 26 | import paddle.static as static |
25 | 27 | from numpy.random import random as rand |
26 | 28 | from paddle.fluid import Program, program_guard |
27 | | - |
28 | | -from ..op_test import OpTest |
| 29 | +sys.path.append("../") |
| 30 | +from op_test import OpTest |
29 | 31 |
|
30 | 32 | paddle.enable_static() |
31 | 33 |
|
| 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 | + |
32 | 109 |
|
| 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)]) |
33 | 124 | class TestFFTC2ROp(OpTest): |
| 125 | + # Because framwork not support complex numerial gradient, we skip gradient check. |
| 126 | + no_need_check_grad = True |
| 127 | + |
34 | 128 | def setUp(self): |
35 | 129 | self.op_type = "fft_c2r" |
36 | | - self.init_dtype_type() |
37 | | - self.init_input_output() |
38 | | - self.init_grad_input_output() |
39 | 130 |
|
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) |
42 | 133 |
|
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 | + } |
49 | 141 | self.outputs = {'Out': out} |
50 | 142 |
|
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 | | - |
56 | 143 | def test_check_output(self): |
57 | 144 | self.check_output() |
58 | 145 |
|
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