Skip to content

Commit

Permalink
Enabled eager_mode for complex unit tests, except for test_complex_op…
Browse files Browse the repository at this point in the history
….py and test_complex_view_op.py (#40887)
  • Loading branch information
jim19930609 authored Mar 28, 2022
1 parent 8fe8039 commit 56dc8c7
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 14 deletions.
23 changes: 17 additions & 6 deletions python/paddle/fluid/tests/unittests/test_complex_abs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import paddle
import paddle.fluid.dygraph as dg
from op_test import OpTest
from paddle.fluid.framework import _test_eager_guard


class TestComplexAbsOp(OpTest):
def setUp(self):
paddle.enable_static()
self.python_api = paddle.abs
self.op_type = "abs"
self.dtype = np.float64
self.shape = (2, 3, 4, 5)
Expand All @@ -44,20 +46,22 @@ def init_grad_input_output(self):
self.grad_x = self.grad_out * (self.x / np.abs(self.x))

def test_check_output(self):
self.check_output()
self.check_output(check_eager=True)

def test_check_grad(self):
self.check_grad(
['X'],
'Out',
user_defined_grads=[self.grad_x],
user_defined_grad_outputs=[self.grad_out])
user_defined_grad_outputs=[self.grad_out],
check_eager=True)


class TestComplexAbsOpZeroValues(OpTest):
def setUp(self):
paddle.enable_static()
self.op_type = "abs"
self.python_api = paddle.abs
self.dtype = np.float64
self.shape = (2, 3, 4, 5)
self.init_input_output()
Expand All @@ -76,14 +80,15 @@ def init_grad_input_output(self):
self.grad_x = np.zeros(self.shape, self.dtype)

def test_check_output(self):
self.check_output()
self.check_output(check_eager=True)

def test_check_grad(self):
self.check_grad(
['X'],
'Out',
user_defined_grads=[self.grad_x],
user_defined_grad_outputs=[self.grad_out])
user_defined_grad_outputs=[self.grad_out],
check_eager=True)


class TestAbs(unittest.TestCase):
Expand All @@ -101,10 +106,15 @@ def test_all_positive(self):
y = paddle.abs(paddle.to_tensor(x))
self.assertTrue(np.allclose(np.abs(x), y.numpy()))

def test_eager(self):
with _test_eager_guard():
self.test_all_positive()


class TestRealAbsOp(OpTest):
def setUp(self):
paddle.enable_static()
self.python_api = paddle.abs
self.op_type = "abs"
self.dtype = np.float64
self.shape = (2, 3, 4, 5)
Expand All @@ -123,14 +133,15 @@ def init_grad_input_output(self):
self.grad_x = self.grad_out * (self.x / np.abs(self.x))

def test_check_output(self):
self.check_output()
self.check_output(check_eager=True)

def test_check_grad(self):
self.check_grad(
['X'],
'Out',
user_defined_grads=[self.grad_x],
user_defined_grad_outputs=[self.grad_out])
user_defined_grad_outputs=[self.grad_out],
check_eager=True)


if __name__ == '__main__':
Expand Down
7 changes: 7 additions & 0 deletions python/paddle/fluid/tests/unittests/test_complex_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import numpy as np

import paddle
from paddle.fluid.framework import _test_eager_guard


class TestComplexCastOp(unittest.TestCase):
Expand Down Expand Up @@ -68,6 +69,12 @@ def test_complex64_complex128(self):
self.assertTrue(
np.allclose(c_128.cast('complex128').numpy(), c_64.numpy()))

def test_eager(self):
with _test_eager_guard():
self.test_complex64_complex128()
self.test_real_to_complex()
self.test_complex_to_real()


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import paddle
import paddle.fluid as fluid
import paddle.fluid.dygraph as dg
from paddle.fluid.framework import _test_eager_guard

paddle_apis = {
"add": paddle.add,
Expand Down Expand Up @@ -98,6 +99,12 @@ def test_real_x_complex_y(self):
self.compare_by_basic_api(x, y)
self.compare_op_by_basic_api(x, y)

def test_eager(self):
with _test_eager_guard():
self.test_real_x_complex_y()
self.test_complex_x_real_y()
self.test_complex_xy()


if __name__ == '__main__':
unittest.main()
10 changes: 10 additions & 0 deletions python/paddle/fluid/tests/unittests/test_complex_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import numpy as np
import paddle.fluid as fluid
import paddle.fluid.dygraph as dg
from paddle.fluid.framework import _test_eager_guard


class TestComplexGetitemLayer(unittest.TestCase):
Expand Down Expand Up @@ -93,6 +94,15 @@ def test_case6(self):

np.testing.assert_allclose(x_var_slice.numpy(), x_np_slice)

def test_eager(self):
with _test_eager_guard():
self.test_case1()
self.test_case2()
self.test_case3()
self.test_case4()
self.test_case5()
self.test_case6()


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import paddle

import paddle.fluid.core as core
from paddle.fluid.framework import _test_eager_guard


class Optimization_ex1(paddle.nn.Layer):
Expand Down Expand Up @@ -119,6 +120,12 @@ def test_case_non_param(self):
self.train(dev, dtype, 3)
self.train_no_clear_grad(dev, dtype, 3)

def test_eager(self):
with _test_eager_guard():
self.test_case_one_step()
self.test_case_two_step()
self.test_case_non_param()


if __name__ == '__main__':
unittest.main()
6 changes: 6 additions & 0 deletions python/paddle/fluid/tests/unittests/test_complex_kron.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import paddle.fluid.dygraph as dg
import numpy as np
import unittest
from paddle.fluid.framework import _test_eager_guard


class ComplexKronTestCase(unittest.TestCase):
Expand All @@ -34,6 +35,7 @@ def setUp(self):
def runTest(self):
for place in self._places:
self.test_kron_api(place)
self.test_eager(place)

def test_kron_api(self, place):
with dg.guard(place):
Expand All @@ -42,6 +44,10 @@ def test_kron_api(self, place):
out_var = paddle.kron(x_var, y_var)
self.assertTrue(np.allclose(out_var.numpy(), self.ref_result))

def test_eager(self, place):
with _test_eager_guard():
self.test_kron_api(place)


def load_tests(loader, standard_tests, pattern):
suite = unittest.TestSuite()
Expand Down
9 changes: 9 additions & 0 deletions python/paddle/fluid/tests/unittests/test_complex_matmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import numpy as np
import paddle.fluid as fluid
import paddle.fluid.dygraph as dg
from paddle.fluid.framework import _test_eager_guard


class TestComplexMatMulLayer(unittest.TestCase):
Expand Down Expand Up @@ -121,6 +122,14 @@ def test_complex_xy_gemm(self):
self.compare_by_basic_api(x, y, np_result)
self.compare_op_by_basic_api(x, y, np_result)

def test_eager(self):
with _test_eager_guard():
self.test_complex_xy_gemm()
self.test_complex_xy_gemv()
self.test_real_x_complex_y()
self.test_complex_x_real_y()
self.test_complex_xy()


if __name__ == '__main__':
unittest.main()
17 changes: 13 additions & 4 deletions python/paddle/fluid/tests/unittests/test_complex_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import paddle
from paddle.fluid import dygraph
from paddle import static
from paddle.fluid.framework import _test_eager_guard

paddle.enable_static()


Expand Down Expand Up @@ -65,7 +67,7 @@ def setUp(self):
self.outputs = {'Out': out_ref}

def test_check_output(self):
self.check_output()
self.check_output(check_eager=True)

def test_check_grad(self):
dout = self.out_grad
Expand All @@ -75,7 +77,8 @@ def test_check_grad(self):
['X', 'Y'],
'Out',
user_defined_grads=[dx, dy],
user_defined_grad_outputs=[dout])
user_defined_grad_outputs=[dout],
check_eager=True)

def test_check_grad_ignore_x(self):
dout = self.out_grad
Expand All @@ -88,7 +91,8 @@ def test_check_grad_ignore_x(self):
'Out',
no_grad_set=set('X'),
user_defined_grads=[dy],
user_defined_grad_outputs=[dout])
user_defined_grad_outputs=[dout],
check_eager=True)

def test_check_grad_ignore_y(self):
dout = self.out_grad
Expand All @@ -99,7 +103,8 @@ def test_check_grad_ignore_y(self):
'Out',
no_grad_set=set('Y'),
user_defined_grads=[dx],
user_defined_grad_outputs=[dout])
user_defined_grad_outputs=[dout],
check_eager=True)


class TestComplexOpBroadcast1(TestComplexOp):
Expand Down Expand Up @@ -151,6 +156,10 @@ def test_static(self):
fetch_list=[out])
self.assertTrue(np.allclose(self.out, out_np))

def test_eager(self):
with _test_eager_guard():
self.test_dygraph()


if __name__ == "__main__":
unittest.main()
6 changes: 6 additions & 0 deletions python/paddle/fluid/tests/unittests/test_complex_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import paddle.fluid.dygraph as dg
import numpy as np
import unittest
from paddle.fluid.framework import _test_eager_guard


class TestComplexReshape(unittest.TestCase):
Expand Down Expand Up @@ -53,6 +54,11 @@ def test_shape_omit_dims(self):
y_np = y_var.numpy()
self.assertTrue(np.allclose(np.reshape(x_np, shape_), y_np))

def test_eager(self):
with _test_eager_guard():
self.test_shape_norm_dims()
self.test_shape_omit_dims()


if __name__ == "__main__":
unittest.main()
5 changes: 5 additions & 0 deletions python/paddle/fluid/tests/unittests/test_complex_simplenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import paddle

import paddle.fluid.core as core
from paddle.fluid.framework import _test_eager_guard


class Optimization_ex1(paddle.nn.Layer):
Expand Down Expand Up @@ -67,6 +68,10 @@ def test_train_success(self):
for dev in self.devices:
self.train(dev)

def test_eager(self):
with _test_eager_guard():
self.test_train_success()


if __name__ == '__main__':
unittest.main()
5 changes: 5 additions & 0 deletions python/paddle/fluid/tests/unittests/test_complex_sum_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from paddle import tensor
import paddle.fluid as fluid
import paddle.fluid.dygraph as dg
from paddle.fluid.framework import _test_eager_guard


class TestComplexSumLayer(unittest.TestCase):
Expand All @@ -39,6 +40,10 @@ def test_complex_basic_api(self):
target = np.sum(input, axis=(1, 2))
self.assertTrue(np.allclose(result, target))

def test_eager(self):
with _test_eager_guard():
self.test_complex_basic_api()


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from paddle import tensor
import paddle.fluid as fluid
import paddle.fluid.dygraph as dg
from paddle.fluid.framework import _test_eager_guard


class TestComplexTraceLayer(unittest.TestCase):
Expand All @@ -40,6 +41,10 @@ def test_basic_api(self):
target = np.trace(input, offset=1, axis1=0, axis2=2)
self.assertTrue(np.allclose(result, target))

def test_eager(self):
with _test_eager_guard():
self.test_basic_api()


if __name__ == '__main__':
unittest.main()
5 changes: 5 additions & 0 deletions python/paddle/fluid/tests/unittests/test_complex_transpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import numpy as np
import paddle.fluid as fluid
import paddle.fluid.dygraph as dg
from paddle.fluid.framework import _test_eager_guard


class TestComplexTransposeLayer(unittest.TestCase):
Expand All @@ -39,6 +40,10 @@ def test_transpose_by_complex_api(self):
trans = paddle.transpose(var, perm=perm)
self.assertTrue(np.allclose(trans.numpy(), np_trans))

def test_eager(self):
with _test_eager_guard():
self.test_transpose_by_complex_api()


if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit 56dc8c7

Please sign in to comment.