未验证 提交 675b3486 编写于 作者: 姜永久 提交者: GitHub

rm unittests eager guard tests part22 tril2warpctc (#48893)

上级 fb8f2de6
...@@ -19,7 +19,6 @@ from op_test import OpTest ...@@ -19,7 +19,6 @@ from op_test import OpTest
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid.framework import _test_eager_guard
class TestTrilIndicesOp(OpTest): class TestTrilIndicesOp(OpTest):
...@@ -89,10 +88,6 @@ class TestTrilIndicesAPICaseDygraph(unittest.TestCase): ...@@ -89,10 +88,6 @@ class TestTrilIndicesAPICaseDygraph(unittest.TestCase):
expected_result1 = np.tril_indices(4, 2, 4) expected_result1 = np.tril_indices(4, 2, 4)
self.assertEqual((out1.numpy() == expected_result1).all(), True) self.assertEqual((out1.numpy() == expected_result1).all(), True)
def test_dygraph_eager(self):
with _test_eager_guard():
self.test_dygraph()
class TestTrilIndicesAPICaseError(unittest.TestCase): class TestTrilIndicesAPICaseError(unittest.TestCase):
def test_case_error(self): def test_case_error(self):
......
...@@ -19,7 +19,6 @@ from op_test import OpTest ...@@ -19,7 +19,6 @@ from op_test import OpTest
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid.framework import _test_eager_guard
class TestTriuIndicesOp(OpTest): class TestTriuIndicesOp(OpTest):
...@@ -84,10 +83,6 @@ class TestTriuIndicesAPICaseDygraph(unittest.TestCase): ...@@ -84,10 +83,6 @@ class TestTriuIndicesAPICaseDygraph(unittest.TestCase):
expected_result = np.triu_indices(4, 2, 4) expected_result = np.triu_indices(4, 2, 4)
np.testing.assert_array_equal(out, expected_result) np.testing.assert_array_equal(out, expected_result)
def test_dygraph_eager(self):
with _test_eager_guard():
self.test_dygraph()
class TestTriuIndicesAPICaseError(unittest.TestCase): class TestTriuIndicesAPICaseError(unittest.TestCase):
def test_case_error(self): def test_case_error(self):
......
...@@ -18,7 +18,6 @@ import numpy as np ...@@ -18,7 +18,6 @@ import numpy as np
from op_test import OpTest from op_test import OpTest
import paddle import paddle
from paddle.fluid.framework import _test_eager_guard
paddle.enable_static() paddle.enable_static()
...@@ -85,20 +84,6 @@ class TestTruncAPI(unittest.TestCase): ...@@ -85,20 +84,6 @@ class TestTruncAPI(unittest.TestCase):
np.testing.assert_allclose(out.numpy(), out_ref, rtol=1e-08) np.testing.assert_allclose(out.numpy(), out_ref, rtol=1e-08)
paddle.enable_static() paddle.enable_static()
def test_api_eager(self):
paddle.disable_static(self.place)
with _test_eager_guard():
x_tensor = paddle.to_tensor(self.x)
out = paddle.trunc(x_tensor)
out_ref = np.trunc(self.x)
np.testing.assert_allclose(out.numpy(), out_ref, rtol=1e-08)
paddle.enable_static()
def test_api_eager_dygraph(self):
with _test_eager_guard():
self.test_api_dygraph()
def test_errors(self): def test_errors(self):
with paddle.static.program_guard(paddle.static.Program()): with paddle.static.program_guard(paddle.static.Program()):
x = paddle.fluid.data('X', [20, 20], 'bool') x = paddle.fluid.data('X', [20, 20], 'bool')
......
...@@ -20,7 +20,6 @@ import paddle ...@@ -20,7 +20,6 @@ import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.core as core import paddle.fluid.core as core
from paddle.fluid.executor import Executor from paddle.fluid.executor import Executor
from paddle.fluid.framework import _test_eager_guard
class TestTrunctedGaussianRandomOp(unittest.TestCase): class TestTrunctedGaussianRandomOp(unittest.TestCase):
...@@ -70,17 +69,16 @@ class TestTrunctedGaussianRandomOp(unittest.TestCase): ...@@ -70,17 +69,16 @@ class TestTrunctedGaussianRandomOp(unittest.TestCase):
# directly # directly
def gaussian_random_test_eager(self, place): def gaussian_random_test_eager(self, place):
with fluid.dygraph.guard(place): with fluid.dygraph.guard(place):
with _test_eager_guard(): out = paddle._C_ops.truncated_gaussian_random(
out = paddle._C_ops.truncated_gaussian_random( self.attrs["shape"],
self.attrs["shape"], self.attrs["mean"],
self.attrs["mean"], self.attrs["std"],
self.attrs["std"], self.attrs["seed"],
self.attrs["seed"], core.VarDesc.VarType.FP32,
core.VarDesc.VarType.FP32, place,
place, )
) self.assertAlmostEqual(numpy.mean(out.numpy()), 0.0, delta=0.1)
self.assertAlmostEqual(numpy.mean(out.numpy()), 0.0, delta=0.1) self.assertAlmostEqual(numpy.var(out.numpy()), 0.773, delta=0.1)
self.assertAlmostEqual(numpy.var(out.numpy()), 0.773, delta=0.1)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -21,7 +21,6 @@ import paddle ...@@ -21,7 +21,6 @@ import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.tensor as tensor import paddle.tensor as tensor
from paddle.fluid import Program, program_guard from paddle.fluid import Program, program_guard
from paddle.fluid.framework import _test_eager_guard
class TestUnbind(unittest.TestCase): class TestUnbind(unittest.TestCase):
...@@ -57,10 +56,6 @@ class TestUnbind(unittest.TestCase): ...@@ -57,10 +56,6 @@ class TestUnbind(unittest.TestCase):
out.backward() out.backward()
np.testing.assert_array_equal(x.grad.numpy(), np_grad) np.testing.assert_array_equal(x.grad.numpy(), np_grad)
def test_unbind_dygraph_final_state(self):
with _test_eager_guard():
self.test_unbind_dygraph()
class TestLayersUnbind(unittest.TestCase): class TestLayersUnbind(unittest.TestCase):
def test_layers_unbind(self): def test_layers_unbind(self):
......
...@@ -23,7 +23,6 @@ import paddle ...@@ -23,7 +23,6 @@ import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.core as core import paddle.fluid.core as core
from paddle.fluid import Program, program_guard from paddle.fluid import Program, program_guard
from paddle.fluid.framework import _test_eager_guard
from paddle.fluid.op import Operator from paddle.fluid.op import Operator
from paddle.tensor import random from paddle.tensor import random
...@@ -171,7 +170,7 @@ class TestUniformRandomOp(OpTest): ...@@ -171,7 +170,7 @@ class TestUniformRandomOp(OpTest):
hist, prob = self.output_hist(np.array(outs[0])) hist, prob = self.output_hist(np.array(outs[0]))
np.testing.assert_allclose(hist, prob, rtol=0, atol=0.01) np.testing.assert_allclose(hist, prob, rtol=0, atol=0.01)
def func_test_check_api(self): def test_check_api(self):
places = self._get_places() places = self._get_places()
for place in places: for place in places:
with fluid.dygraph.base.guard(place=place): with fluid.dygraph.base.guard(place=place):
...@@ -183,11 +182,6 @@ class TestUniformRandomOp(OpTest): ...@@ -183,11 +182,6 @@ class TestUniformRandomOp(OpTest):
self.attrs['seed'], self.attrs['seed'],
) )
def test_check_api_eager(self):
with _test_eager_guard():
self.func_test_check_api()
self.func_test_check_api()
class TestUniformRandomOpError(unittest.TestCase): class TestUniformRandomOpError(unittest.TestCase):
def test_errors(self): def test_errors(self):
......
...@@ -20,7 +20,6 @@ from op_test import OpTest ...@@ -20,7 +20,6 @@ from op_test import OpTest
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.core as core import paddle.fluid.core as core
from paddle.fluid.framework import _test_eager_guard
class TestUniqueOp(OpTest): class TestUniqueOp(OpTest):
...@@ -262,12 +261,6 @@ class TestUniqueAPI(unittest.TestCase): ...@@ -262,12 +261,6 @@ class TestUniqueAPI(unittest.TestCase):
self.assertTrue((counts.numpy() == np_counts).all(), True) self.assertTrue((counts.numpy() == np_counts).all(), True)
paddle.enable_static() paddle.enable_static()
def test_dygraph_api(self):
with _test_eager_guard():
self.test_dygraph_api_out()
self.test_dygraph_api_attr()
self.test_dygraph_attr_dtype()
def test_static_graph(self): def test_static_graph(self):
with paddle.static.program_guard( with paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program() paddle.static.Program(), paddle.static.Program()
......
...@@ -21,7 +21,7 @@ import paddle ...@@ -21,7 +21,7 @@ import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.core as core import paddle.fluid.core as core
import paddle.nn.functional as F import paddle.nn.functional as F
from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard from paddle.fluid.framework import _in_legacy_dygraph
class TestVarBase(unittest.TestCase): class TestVarBase(unittest.TestCase):
...@@ -30,7 +30,7 @@ class TestVarBase(unittest.TestCase): ...@@ -30,7 +30,7 @@ class TestVarBase(unittest.TestCase):
self.dtype = np.float32 self.dtype = np.float32
self.array = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) self.array = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
def func_test_to_tensor(self): def test_to_tensor(self):
def check_with_place(place): def check_with_place(place):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
paddle.set_default_dtype('float32') paddle.set_default_dtype('float32')
...@@ -277,12 +277,7 @@ class TestVarBase(unittest.TestCase): ...@@ -277,12 +277,7 @@ class TestVarBase(unittest.TestCase):
check_with_place(core.NPUPlace(0)) check_with_place(core.NPUPlace(0))
check_with_place("npu:0") check_with_place("npu:0")
def test_to_tensor(self): def test_to_tensor_not_change_input_stop_gradient(self):
with _test_eager_guard():
self.func_test_to_tensor()
self.func_test_to_tensor()
def func_test_to_tensor_not_change_input_stop_gradient(self):
with paddle.fluid.dygraph.guard(core.CPUPlace()): with paddle.fluid.dygraph.guard(core.CPUPlace()):
a = paddle.zeros([1024]) a = paddle.zeros([1024])
a.stop_gradient = False a.stop_gradient = False
...@@ -290,12 +285,7 @@ class TestVarBase(unittest.TestCase): ...@@ -290,12 +285,7 @@ class TestVarBase(unittest.TestCase):
self.assertEqual(a.stop_gradient, False) self.assertEqual(a.stop_gradient, False)
self.assertEqual(b.stop_gradient, True) self.assertEqual(b.stop_gradient, True)
def test_to_tensor_not_change_input_stop_gradient(self): def test_to_tensor_change_place(self):
with _test_eager_guard():
self.func_test_to_tensor_not_change_input_stop_gradient()
self.func_test_to_tensor_not_change_input_stop_gradient()
def func_test_to_tensor_change_place(self):
if core.is_compiled_with_cuda(): if core.is_compiled_with_cuda():
a_np = np.random.rand(1024, 1024) a_np = np.random.rand(1024, 1024)
with paddle.fluid.dygraph.guard(core.CPUPlace()): with paddle.fluid.dygraph.guard(core.CPUPlace()):
...@@ -313,12 +303,7 @@ class TestVarBase(unittest.TestCase): ...@@ -313,12 +303,7 @@ class TestVarBase(unittest.TestCase):
a = paddle.to_tensor(a, place=paddle.CUDAPinnedPlace()) a = paddle.to_tensor(a, place=paddle.CUDAPinnedPlace())
self.assertEqual(a.place.__repr__(), "Place(gpu_pinned)") self.assertEqual(a.place.__repr__(), "Place(gpu_pinned)")
def test_to_tensor_change_place(self): def test_to_tensor_with_lodtensor(self):
with _test_eager_guard():
self.func_test_to_tensor_change_place()
self.func_test_to_tensor_change_place()
def func_test_to_tensor_with_lodtensor(self):
if core.is_compiled_with_cuda(): if core.is_compiled_with_cuda():
a_np = np.random.rand(1024, 1024) a_np = np.random.rand(1024, 1024)
with paddle.fluid.dygraph.guard(core.CPUPlace()): with paddle.fluid.dygraph.guard(core.CPUPlace()):
...@@ -334,12 +319,7 @@ class TestVarBase(unittest.TestCase): ...@@ -334,12 +319,7 @@ class TestVarBase(unittest.TestCase):
np.testing.assert_array_equal(a_np, a.numpy()) np.testing.assert_array_equal(a_np, a.numpy())
self.assertTrue(a.place.__repr__(), "Place(cpu)") self.assertTrue(a.place.__repr__(), "Place(cpu)")
def test_to_tensor_with_lodtensor(self): def test_to_variable(self):
with _test_eager_guard():
self.func_test_to_tensor_with_lodtensor()
self.func_test_to_tensor_with_lodtensor()
def func_test_to_variable(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
var = fluid.dygraph.to_variable(self.array, name="abc") var = fluid.dygraph.to_variable(self.array, name="abc")
np.testing.assert_array_equal(var.numpy(), self.array) np.testing.assert_array_equal(var.numpy(), self.array)
...@@ -358,12 +338,7 @@ class TestVarBase(unittest.TestCase): ...@@ -358,12 +338,7 @@ class TestVarBase(unittest.TestCase):
linear = paddle.nn.Linear(32, 64) linear = paddle.nn.Linear(32, 64)
var = linear._helper.to_variable("test", name="abc") var = linear._helper.to_variable("test", name="abc")
def test_to_variable(self): def test_list_to_variable(self):
with _test_eager_guard():
self.func_test_to_variable()
self.func_test_to_variable()
def func_test_list_to_variable(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
array = [[[1, 2], [1, 2], [1.0, 2]], [[1, 2], [1, 2], [1, 2]]] array = [[[1, 2], [1, 2], [1.0, 2]], [[1, 2], [1, 2], [1, 2]]]
var = fluid.dygraph.to_variable(array, dtype='int32') var = fluid.dygraph.to_variable(array, dtype='int32')
...@@ -372,12 +347,7 @@ class TestVarBase(unittest.TestCase): ...@@ -372,12 +347,7 @@ class TestVarBase(unittest.TestCase):
self.assertEqual(var.dtype, core.VarDesc.VarType.INT32) self.assertEqual(var.dtype, core.VarDesc.VarType.INT32)
self.assertEqual(var.type, core.VarDesc.VarType.LOD_TENSOR) self.assertEqual(var.type, core.VarDesc.VarType.LOD_TENSOR)
def test_list_to_variable(self): def test_tuple_to_variable(self):
with _test_eager_guard():
self.func_test_list_to_variable()
self.func_test_list_to_variable()
def func_test_tuple_to_variable(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
array = (((1, 2), (1, 2), (1, 2)), ((1, 2), (1, 2), (1, 2))) array = (((1, 2), (1, 2), (1, 2)), ((1, 2), (1, 2), (1, 2)))
var = fluid.dygraph.to_variable(array, dtype='float32') var = fluid.dygraph.to_variable(array, dtype='float32')
...@@ -386,24 +356,14 @@ class TestVarBase(unittest.TestCase): ...@@ -386,24 +356,14 @@ class TestVarBase(unittest.TestCase):
self.assertEqual(var.dtype, core.VarDesc.VarType.FP32) self.assertEqual(var.dtype, core.VarDesc.VarType.FP32)
self.assertEqual(var.type, core.VarDesc.VarType.LOD_TENSOR) self.assertEqual(var.type, core.VarDesc.VarType.LOD_TENSOR)
def test_tuple_to_variable(self): def test_tensor_to_variable(self):
with _test_eager_guard():
self.func_test_tuple_to_variable()
self.func_test_tuple_to_variable()
def func_test_tensor_to_variable(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
t = fluid.Tensor() t = fluid.Tensor()
t.set(np.random.random((1024, 1024)), fluid.CPUPlace()) t.set(np.random.random((1024, 1024)), fluid.CPUPlace())
var = fluid.dygraph.to_variable(t) var = fluid.dygraph.to_variable(t)
np.testing.assert_array_equal(t, var.numpy()) np.testing.assert_array_equal(t, var.numpy())
def test_tensor_to_variable(self): def test_leaf_tensor(self):
with _test_eager_guard():
self.func_test_tensor_to_variable()
self.func_test_tensor_to_variable()
def func_test_leaf_tensor(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
x = paddle.to_tensor(np.random.uniform(-1, 1, size=[10, 10])) x = paddle.to_tensor(np.random.uniform(-1, 1, size=[10, 10]))
self.assertTrue(x.is_leaf) self.assertTrue(x.is_leaf)
...@@ -429,12 +389,7 @@ class TestVarBase(unittest.TestCase): ...@@ -429,12 +389,7 @@ class TestVarBase(unittest.TestCase):
self.assertTrue(linear.bias.is_leaf) self.assertTrue(linear.bias.is_leaf)
self.assertFalse(out.is_leaf) self.assertFalse(out.is_leaf)
def test_leaf_tensor(self): def test_detach(self):
with _test_eager_guard():
self.func_test_leaf_tensor()
self.func_test_leaf_tensor()
def func_test_detach(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
x = paddle.to_tensor(1.0, dtype="float64", stop_gradient=False) x = paddle.to_tensor(1.0, dtype="float64", stop_gradient=False)
detach_x = x.detach() detach_x = x.detach()
...@@ -470,12 +425,7 @@ class TestVarBase(unittest.TestCase): ...@@ -470,12 +425,7 @@ class TestVarBase(unittest.TestCase):
detach_x[:] = 5.0 detach_x[:] = 5.0
y.backward() y.backward()
def test_detach(self): def test_write_property(self):
with _test_eager_guard():
self.func_test_detach()
self.func_test_detach()
def func_test_write_property(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
var = fluid.dygraph.to_variable(self.array) var = fluid.dygraph.to_variable(self.array)
...@@ -491,12 +441,7 @@ class TestVarBase(unittest.TestCase): ...@@ -491,12 +441,7 @@ class TestVarBase(unittest.TestCase):
var.stop_gradient = False var.stop_gradient = False
self.assertEqual(var.stop_gradient, False) self.assertEqual(var.stop_gradient, False)
def test_write_property(self): def test_deep_copy(self):
with _test_eager_guard():
self.func_test_write_property()
self.func_test_write_property()
def func_test_deep_copy(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
if _in_legacy_dygraph(): if _in_legacy_dygraph():
empty_var = core.VarBase() empty_var = core.VarBase()
...@@ -579,13 +524,8 @@ class TestVarBase(unittest.TestCase): ...@@ -579,13 +524,8 @@ class TestVarBase(unittest.TestCase):
np.array(selected_rows.get_tensor()), np.array(selected_rows.get_tensor()),
) )
def test_deep_copy(self):
with _test_eager_guard():
self.func_test_deep_copy()
self.func_test_deep_copy()
# test some patched methods # test some patched methods
def func_test_set_value(self): def test_set_value(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
var = fluid.dygraph.to_variable(self.array) var = fluid.dygraph.to_variable(self.array)
tmp1 = np.random.uniform(0.1, 1, [2, 2, 3]).astype(self.dtype) tmp1 = np.random.uniform(0.1, 1, [2, 2, 3]).astype(self.dtype)
...@@ -595,22 +535,12 @@ class TestVarBase(unittest.TestCase): ...@@ -595,22 +535,12 @@ class TestVarBase(unittest.TestCase):
var.set_value(tmp2) var.set_value(tmp2)
np.testing.assert_array_equal(var.numpy(), tmp2) np.testing.assert_array_equal(var.numpy(), tmp2)
def test_set_value(self): def test_to_string(self):
with _test_eager_guard():
self.func_test_set_value()
self.func_test_set_value()
def func_test_to_string(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
var = fluid.dygraph.to_variable(self.array) var = fluid.dygraph.to_variable(self.array)
self.assertTrue(isinstance(str(var), str)) self.assertTrue(isinstance(str(var), str))
def test_to_string(self): def test_element_size(self):
with _test_eager_guard():
self.func_test_to_string()
self.func_test_to_string()
def func_test_element_size(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
x = paddle.to_tensor(1, dtype='bool') x = paddle.to_tensor(1, dtype='bool')
self.assertEqual(x.element_size(), 1) self.assertEqual(x.element_size(), 1)
...@@ -645,12 +575,7 @@ class TestVarBase(unittest.TestCase): ...@@ -645,12 +575,7 @@ class TestVarBase(unittest.TestCase):
x = paddle.to_tensor(1, dtype='complex128') x = paddle.to_tensor(1, dtype='complex128')
self.assertEqual(x.element_size(), 16) self.assertEqual(x.element_size(), 16)
def test_element_size(self): def test_backward(self):
with _test_eager_guard():
self.func_test_element_size()
self.func_test_element_size()
def func_test_backward(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
var = fluid.dygraph.to_variable(self.array) var = fluid.dygraph.to_variable(self.array)
var.stop_gradient = False var.stop_gradient = False
...@@ -659,12 +584,7 @@ class TestVarBase(unittest.TestCase): ...@@ -659,12 +584,7 @@ class TestVarBase(unittest.TestCase):
grad_var = var._grad_ivar() grad_var = var._grad_ivar()
self.assertEqual(grad_var.shape, self.shape) self.assertEqual(grad_var.shape, self.shape)
def test_backward(self): def test_gradient(self):
with _test_eager_guard():
self.func_test_backward()
self.func_test_backward()
def func_test_gradient(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
var = fluid.dygraph.to_variable(self.array) var = fluid.dygraph.to_variable(self.array)
var.stop_gradient = False var.stop_gradient = False
...@@ -673,23 +593,13 @@ class TestVarBase(unittest.TestCase): ...@@ -673,23 +593,13 @@ class TestVarBase(unittest.TestCase):
grad_var = var.gradient() grad_var = var.gradient()
self.assertEqual(grad_var.shape, self.array.shape) self.assertEqual(grad_var.shape, self.array.shape)
def test_gradient(self): def test_block(self):
with _test_eager_guard():
self.func_test_gradient()
self.func_test_gradient()
def func_test_block(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
var = fluid.dygraph.to_variable(self.array) var = fluid.dygraph.to_variable(self.array)
self.assertEqual( self.assertEqual(
var.block, fluid.default_main_program().global_block() var.block, fluid.default_main_program().global_block()
) )
def test_block(self):
with _test_eager_guard():
self.func_test_block()
self.func_test_block()
def _test_slice(self): def _test_slice(self):
w = fluid.dygraph.to_variable( w = fluid.dygraph.to_variable(
np.random.random((784, 100, 100)).astype('float64') np.random.random((784, 100, 100)).astype('float64')
...@@ -1079,7 +989,7 @@ class TestVarBase(unittest.TestCase): ...@@ -1079,7 +989,7 @@ class TestVarBase(unittest.TestCase):
col = np.array([2, 1, 3]) col = np.array([2, 1, 3])
np.testing.assert_array_equal(array[row, col], x[row, col].numpy()) np.testing.assert_array_equal(array[row, col], x[row, col].numpy())
def func_test_slice(self): def test_slice(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
self._test_slice() self._test_slice()
self._test_slice_for_tensor_attr() self._test_slice_for_tensor_attr()
...@@ -1105,24 +1015,14 @@ class TestVarBase(unittest.TestCase): ...@@ -1105,24 +1015,14 @@ class TestVarBase(unittest.TestCase):
mask = np.array([1, 0, 1, 0], dtype=bool) mask = np.array([1, 0, 1, 0], dtype=bool)
var[paddle.to_tensor([0, 1]), mask] var[paddle.to_tensor([0, 1]), mask]
def test_slice(self): def test_var_base_to_np(self):
with _test_eager_guard():
self.func_test_slice()
self.func_test_slice()
def func_test_var_base_to_np(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
var = fluid.dygraph.to_variable(self.array) var = fluid.dygraph.to_variable(self.array)
np.testing.assert_array_equal( np.testing.assert_array_equal(
var.numpy(), fluid.framework._var_base_to_np(var) var.numpy(), fluid.framework._var_base_to_np(var)
) )
def test_var_base_to_np(self): def test_var_base_as_np(self):
with _test_eager_guard():
self.func_test_var_base_to_np()
self.func_test_var_base_to_np()
def func_test_var_base_as_np(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
var = fluid.dygraph.to_variable(self.array) var = fluid.dygraph.to_variable(self.array)
np.testing.assert_array_equal(var.numpy(), np.array(var)) np.testing.assert_array_equal(var.numpy(), np.array(var))
...@@ -1130,12 +1030,7 @@ class TestVarBase(unittest.TestCase): ...@@ -1130,12 +1030,7 @@ class TestVarBase(unittest.TestCase):
var.numpy(), np.array(var, dtype=np.float32) var.numpy(), np.array(var, dtype=np.float32)
) )
def test_var_base_as_np(self): def test_if(self):
with _test_eager_guard():
self.func_test_var_base_as_np()
self.func_test_var_base_as_np()
def func_test_if(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
var1 = fluid.dygraph.to_variable(np.array([[[0]]])) var1 = fluid.dygraph.to_variable(np.array([[[0]]]))
var2 = fluid.dygraph.to_variable(np.array([[[1]]])) var2 = fluid.dygraph.to_variable(np.array([[[1]]]))
...@@ -1154,12 +1049,7 @@ class TestVarBase(unittest.TestCase): ...@@ -1154,12 +1049,7 @@ class TestVarBase(unittest.TestCase):
assert not bool(var1), "bool(var1) is False" assert not bool(var1), "bool(var1) is False"
assert bool(var2), "bool(var2) is True" assert bool(var2), "bool(var2) is True"
def test_if(self): def test_to_static_var(self):
with _test_eager_guard():
self.func_test_if()
self.func_test_if()
def func_test_to_static_var(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
# Convert VarBase into Variable or Parameter # Convert VarBase into Variable or Parameter
var_base = fluid.dygraph.to_variable(self.array, name="var_base_1") var_base = fluid.dygraph.to_variable(self.array, name="var_base_1")
...@@ -1184,11 +1074,6 @@ class TestVarBase(unittest.TestCase): ...@@ -1184,11 +1074,6 @@ class TestVarBase(unittest.TestCase):
static_param = weight._to_static_var() static_param = weight._to_static_var()
self._assert_to_static(weight, static_param, True) self._assert_to_static(weight, static_param, True)
def test_to_static_var(self):
with _test_eager_guard():
self.func_test_to_static_var()
self.func_test_to_static_var()
def _assert_to_static(self, var_base, static_var, is_param=False): def _assert_to_static(self, var_base, static_var, is_param=False):
if is_param: if is_param:
self.assertTrue(isinstance(static_var, fluid.framework.Parameter)) self.assertTrue(isinstance(static_var, fluid.framework.Parameter))
...@@ -1216,7 +1101,7 @@ class TestVarBase(unittest.TestCase): ...@@ -1216,7 +1101,7 @@ class TestVarBase(unittest.TestCase):
self.assertListEqual(list(var_base.shape), list(static_var.shape)) self.assertListEqual(list(var_base.shape), list(static_var.shape))
def func_test_tensor_str(self): def test_tensor_str(self):
paddle.enable_static() paddle.enable_static()
paddle.disable_static(paddle.CPUPlace()) paddle.disable_static(paddle.CPUPlace())
paddle.seed(10) paddle.seed(10)
...@@ -1235,12 +1120,7 @@ class TestVarBase(unittest.TestCase): ...@@ -1235,12 +1120,7 @@ class TestVarBase(unittest.TestCase):
self.assertEqual(a_str, expected) self.assertEqual(a_str, expected)
def test_tensor_str(self): def test_tensor_str2(self):
with _test_eager_guard():
self.func_test_tensor_str()
self.func_test_tensor_str()
def func_test_tensor_str2(self):
paddle.disable_static(paddle.CPUPlace()) paddle.disable_static(paddle.CPUPlace())
a = paddle.to_tensor([[1.5111111, 1.0], [0, 0]]) a = paddle.to_tensor([[1.5111111, 1.0], [0, 0]])
a_str = str(a) a_str = str(a)
...@@ -1251,12 +1131,7 @@ class TestVarBase(unittest.TestCase): ...@@ -1251,12 +1131,7 @@ class TestVarBase(unittest.TestCase):
self.assertEqual(a_str, expected) self.assertEqual(a_str, expected)
def test_tensor_str2(self): def test_tensor_str3(self):
with _test_eager_guard():
self.func_test_tensor_str2()
self.func_test_tensor_str2()
def func_test_tensor_str3(self):
paddle.disable_static(paddle.CPUPlace()) paddle.disable_static(paddle.CPUPlace())
a = paddle.to_tensor([[-1.5111111, 1.0], [0, -0.5]]) a = paddle.to_tensor([[-1.5111111, 1.0], [0, -0.5]])
a_str = str(a) a_str = str(a)
...@@ -1267,12 +1142,7 @@ class TestVarBase(unittest.TestCase): ...@@ -1267,12 +1142,7 @@ class TestVarBase(unittest.TestCase):
self.assertEqual(a_str, expected) self.assertEqual(a_str, expected)
def test_tensor_str3(self): def test_tensor_str_scaler(self):
with _test_eager_guard():
self.func_test_tensor_str3()
self.func_test_tensor_str3()
def func_test_tensor_str_scaler(self):
paddle.disable_static(paddle.CPUPlace()) paddle.disable_static(paddle.CPUPlace())
a = paddle.to_tensor(np.array(False)) a = paddle.to_tensor(np.array(False))
a_str = str(a) a_str = str(a)
...@@ -1282,12 +1152,7 @@ class TestVarBase(unittest.TestCase): ...@@ -1282,12 +1152,7 @@ class TestVarBase(unittest.TestCase):
self.assertEqual(a_str, expected) self.assertEqual(a_str, expected)
def test_tensor_str_scaler(self): def test_tensor_str_shape_with_zero(self):
with _test_eager_guard():
self.func_test_tensor_str_scaler()
self.func_test_tensor_str_scaler()
def func_test_tensor_str_shape_with_zero(self):
paddle.disable_static(paddle.CPUPlace()) paddle.disable_static(paddle.CPUPlace())
x = paddle.ones((10, 10)) x = paddle.ones((10, 10))
y = paddle.nonzero(x == 0) y = paddle.nonzero(x == 0)
...@@ -1298,12 +1163,7 @@ class TestVarBase(unittest.TestCase): ...@@ -1298,12 +1163,7 @@ class TestVarBase(unittest.TestCase):
self.assertEqual(a_str, expected) self.assertEqual(a_str, expected)
def test_tensor_str_shape_with_zero(self): def test_tensor_str_linewidth(self):
with _test_eager_guard():
self.func_test_tensor_str_shape_with_zero()
self.func_test_tensor_str_shape_with_zero()
def func_test_tensor_str_linewidth(self):
paddle.disable_static(paddle.CPUPlace()) paddle.disable_static(paddle.CPUPlace())
paddle.seed(2021) paddle.seed(2021)
x = paddle.rand([128]) x = paddle.rand([128])
...@@ -1331,12 +1191,7 @@ class TestVarBase(unittest.TestCase): ...@@ -1331,12 +1191,7 @@ class TestVarBase(unittest.TestCase):
self.assertEqual(a_str, expected) self.assertEqual(a_str, expected)
def test_tensor_str_linewidth(self): def test_tensor_str_linewidth2(self):
with _test_eager_guard():
self.func_test_tensor_str_linewidth()
self.func_test_tensor_str_linewidth()
def func_test_tensor_str_linewidth2(self):
paddle.disable_static(paddle.CPUPlace()) paddle.disable_static(paddle.CPUPlace())
paddle.seed(2021) paddle.seed(2021)
x = paddle.rand([128]) x = paddle.rand([128])
...@@ -1358,12 +1213,7 @@ class TestVarBase(unittest.TestCase): ...@@ -1358,12 +1213,7 @@ class TestVarBase(unittest.TestCase):
self.assertEqual(a_str, expected) self.assertEqual(a_str, expected)
def test_tensor_str_linewidth2(self): def test_tensor_str_bf16(self):
with _test_eager_guard():
self.func_test_tensor_str_linewidth2()
self.func_test_tensor_str_linewidth2()
def func_tensor_str_bf16(self):
paddle.disable_static(paddle.CPUPlace()) paddle.disable_static(paddle.CPUPlace())
a = paddle.to_tensor([[1.5, 1.0], [0, 0]]) a = paddle.to_tensor([[1.5, 1.0], [0, 0]])
a = paddle.cast(a, dtype=core.VarDesc.VarType.BF16) a = paddle.cast(a, dtype=core.VarDesc.VarType.BF16)
...@@ -1376,12 +1226,7 @@ class TestVarBase(unittest.TestCase): ...@@ -1376,12 +1226,7 @@ class TestVarBase(unittest.TestCase):
self.assertEqual(a_str, expected) self.assertEqual(a_str, expected)
def test_tensor_str_bf16(self): def test_print_tensor_dtype(self):
with _test_eager_guard():
self.func_tensor_str_bf16()
self.func_tensor_str_bf16()
def func_test_print_tensor_dtype(self):
paddle.disable_static(paddle.CPUPlace()) paddle.disable_static(paddle.CPUPlace())
a = paddle.rand([1]) a = paddle.rand([1])
a_str = str(a.dtype) a_str = str(a.dtype)
...@@ -1390,11 +1235,6 @@ class TestVarBase(unittest.TestCase): ...@@ -1390,11 +1235,6 @@ class TestVarBase(unittest.TestCase):
self.assertEqual(a_str, expected) self.assertEqual(a_str, expected)
def test_print_tensor_dtype(self):
with _test_eager_guard():
self.func_test_print_tensor_dtype()
self.func_test_print_tensor_dtype()
class TestVarBaseSetitem(unittest.TestCase): class TestVarBaseSetitem(unittest.TestCase):
def func_setUp(self): def func_setUp(self):
...@@ -1440,9 +1280,6 @@ class TestVarBaseSetitem(unittest.TestCase): ...@@ -1440,9 +1280,6 @@ class TestVarBaseSetitem(unittest.TestCase):
self._test(self.tensor_value) self._test(self.tensor_value)
def test_value_tensor(self): def test_value_tensor(self):
with _test_eager_guard():
self.func_setUp()
self.func_test_value_tensor()
self.func_setUp() self.func_setUp()
self.func_test_value_tensor() self.func_test_value_tensor()
...@@ -1450,9 +1287,6 @@ class TestVarBaseSetitem(unittest.TestCase): ...@@ -1450,9 +1287,6 @@ class TestVarBaseSetitem(unittest.TestCase):
self._test(self.np_value) self._test(self.np_value)
def test_value_numpy(self): def test_value_numpy(self):
with _test_eager_guard():
self.func_setUp()
self.func_test_value_numpy()
self.func_setUp() self.func_setUp()
self.func_test_value_numpy() self.func_test_value_numpy()
...@@ -1460,9 +1294,6 @@ class TestVarBaseSetitem(unittest.TestCase): ...@@ -1460,9 +1294,6 @@ class TestVarBaseSetitem(unittest.TestCase):
self._test(10) self._test(10)
def test_value_int(self): def test_value_int(self):
with _test_eager_guard():
self.func_setUp()
self.func_test_value_int()
self.func_setUp() self.func_setUp()
self.func_test_value_int() self.func_test_value_int()
...@@ -1481,9 +1312,6 @@ class TestVarBaseSetitemFp32(TestVarBaseSetitem): ...@@ -1481,9 +1312,6 @@ class TestVarBaseSetitemFp32(TestVarBaseSetitem):
self._test(3.3) self._test(3.3)
def test_value_float(self): def test_value_float(self):
with _test_eager_guard():
self.func_setUp()
self.func_test_value_float()
self.func_setUp() self.func_setUp()
self.func_test_value_float() self.func_test_value_float()
...@@ -1542,9 +1370,6 @@ class TestVarBaseSetitemBoolIndex(unittest.TestCase): ...@@ -1542,9 +1370,6 @@ class TestVarBaseSetitemBoolIndex(unittest.TestCase):
self._test(self.tensor_value) self._test(self.tensor_value)
def test_value_tensor(self): def test_value_tensor(self):
with _test_eager_guard():
self.func_setUp()
self.func_test_value_tensor()
self.func_setUp() self.func_setUp()
self.func_test_value_tensor() self.func_test_value_tensor()
...@@ -1553,9 +1378,6 @@ class TestVarBaseSetitemBoolIndex(unittest.TestCase): ...@@ -1553,9 +1378,6 @@ class TestVarBaseSetitemBoolIndex(unittest.TestCase):
self._test(self.np_value) self._test(self.np_value)
def test_value_numpy(self): def test_value_numpy(self):
with _test_eager_guard():
self.func_setUp()
self.func_test_value_numpy()
self.func_setUp() self.func_setUp()
self.func_test_value_numpy() self.func_test_value_numpy()
...@@ -1564,9 +1386,6 @@ class TestVarBaseSetitemBoolIndex(unittest.TestCase): ...@@ -1564,9 +1386,6 @@ class TestVarBaseSetitemBoolIndex(unittest.TestCase):
self._test(10) self._test(10)
def test_value_int(self): def test_value_int(self):
with _test_eager_guard():
self.func_setUp()
self.func_test_value_int()
self.func_setUp() self.func_setUp()
self.func_test_value_int() self.func_test_value_int()
...@@ -1597,7 +1416,7 @@ class TestVarBaseSetitemBoolScalarIndex(unittest.TestCase): ...@@ -1597,7 +1416,7 @@ class TestVarBaseSetitemBoolScalarIndex(unittest.TestCase):
class TestVarBaseInplaceVersion(unittest.TestCase): class TestVarBaseInplaceVersion(unittest.TestCase):
def func_test_setitem(self): def test_setitem(self):
paddle.disable_static() paddle.disable_static()
var = paddle.ones(shape=[4, 2, 3], dtype="float32") var = paddle.ones(shape=[4, 2, 3], dtype="float32")
...@@ -1609,12 +1428,7 @@ class TestVarBaseInplaceVersion(unittest.TestCase): ...@@ -1609,12 +1428,7 @@ class TestVarBaseInplaceVersion(unittest.TestCase):
var[1:2] = 1 var[1:2] = 1
self.assertEqual(var.inplace_version, 2) self.assertEqual(var.inplace_version, 2)
def test_setitem(self): def test_bump_inplace_version(self):
with _test_eager_guard():
self.func_test_setitem()
self.func_test_setitem()
def func_test_bump_inplace_version(self):
paddle.disable_static() paddle.disable_static()
var = paddle.ones(shape=[4, 2, 3], dtype="float32") var = paddle.ones(shape=[4, 2, 3], dtype="float32")
self.assertEqual(var.inplace_version, 0) self.assertEqual(var.inplace_version, 0)
...@@ -1625,14 +1439,9 @@ class TestVarBaseInplaceVersion(unittest.TestCase): ...@@ -1625,14 +1439,9 @@ class TestVarBaseInplaceVersion(unittest.TestCase):
var._bump_inplace_version() var._bump_inplace_version()
self.assertEqual(var.inplace_version, 2) self.assertEqual(var.inplace_version, 2)
def test_bump_inplace_version(self):
with _test_eager_guard():
self.func_test_bump_inplace_version()
self.func_test_bump_inplace_version()
class TestVarBaseSlice(unittest.TestCase): class TestVarBaseSlice(unittest.TestCase):
def func_test_slice(self): def test_slice(self):
paddle.disable_static() paddle.disable_static()
np_x = np.random.random((3, 8, 8)) np_x = np.random.random((3, 8, 8))
x = paddle.to_tensor(np_x, dtype="float64") x = paddle.to_tensor(np_x, dtype="float64")
...@@ -1640,28 +1449,18 @@ class TestVarBaseSlice(unittest.TestCase): ...@@ -1640,28 +1449,18 @@ class TestVarBaseSlice(unittest.TestCase):
actual_x = paddle.to_tensor(actual_x) actual_x = paddle.to_tensor(actual_x)
self.assertEqual(actual_x.numpy().all(), np_x[0:1].all()) self.assertEqual(actual_x.numpy().all(), np_x[0:1].all())
def test_slice(self):
with _test_eager_guard():
self.func_test_slice()
self.func_test_slice()
class TestVarBaseClear(unittest.TestCase): class TestVarBaseClear(unittest.TestCase):
def func_test_clear(self): def test_clear(self):
paddle.disable_static() paddle.disable_static()
np_x = np.random.random((3, 8, 8)) np_x = np.random.random((3, 8, 8))
x = paddle.to_tensor(np_x, dtype="float64") x = paddle.to_tensor(np_x, dtype="float64")
x._clear() x._clear()
self.assertEqual(str(x), "Tensor(Not initialized)") self.assertEqual(str(x), "Tensor(Not initialized)")
def test_clear(self):
with _test_eager_guard():
self.func_test_clear()
self.func_test_clear()
class TestVarBaseOffset(unittest.TestCase): class TestVarBaseOffset(unittest.TestCase):
def func_offset(self): def test_offset(self):
paddle.disable_static() paddle.disable_static()
np_x = np.random.random((3, 8, 8)) np_x = np.random.random((3, 8, 8))
x = paddle.to_tensor(np_x, dtype="float64") x = paddle.to_tensor(np_x, dtype="float64")
...@@ -1670,14 +1469,9 @@ class TestVarBaseOffset(unittest.TestCase): ...@@ -1670,14 +1469,9 @@ class TestVarBaseOffset(unittest.TestCase):
actual_x = paddle.to_tensor(actual_x) actual_x = paddle.to_tensor(actual_x)
self.assertEqual(actual_x._offset(), expected_offset) self.assertEqual(actual_x._offset(), expected_offset)
def test_offset(self):
with _test_eager_guard():
self.func_offset()
self.func_offset()
class TestVarBaseShareBufferTo(unittest.TestCase): class TestVarBaseShareBufferTo(unittest.TestCase):
def func_test_share_buffer_To(self): def test_share_buffer_To(self):
paddle.disable_static() paddle.disable_static()
np_src = np.random.random((3, 8, 8)) np_src = np.random.random((3, 8, 8))
src = paddle.to_tensor(np_src, dtype="float64") src = paddle.to_tensor(np_src, dtype="float64")
...@@ -1689,11 +1483,6 @@ class TestVarBaseShareBufferTo(unittest.TestCase): ...@@ -1689,11 +1483,6 @@ class TestVarBaseShareBufferTo(unittest.TestCase):
src._share_buffer_to(dst) src._share_buffer_to(dst)
self.assertEqual(src._is_shared_buffer_with(dst), True) self.assertEqual(src._is_shared_buffer_with(dst), True)
def test_share_buffer_To(self):
with _test_eager_guard():
self.func_test_share_buffer_To()
self.func_test_share_buffer_To()
class TestVarBaseTo(unittest.TestCase): class TestVarBaseTo(unittest.TestCase):
def func_setUp(self): def func_setUp(self):
...@@ -1751,15 +1540,12 @@ class TestVarBaseTo(unittest.TestCase): ...@@ -1751,15 +1540,12 @@ class TestVarBaseTo(unittest.TestCase):
self.assertRaises(AssertionError, self.x._to, blocking=1) self.assertRaises(AssertionError, self.x._to, blocking=1)
def test_to_api(self): def test_to_api(self):
with _test_eager_guard():
self.func_setUp()
self.func_test_to_api()
self.func_setUp() self.func_setUp()
self.func_test_to_api() self.func_test_to_api()
class TestVarBaseInitVarBaseFromTensorWithDevice(unittest.TestCase): class TestVarBaseInitVarBaseFromTensorWithDevice(unittest.TestCase):
def func_test_varbase_init(self): def test_varbase_init(self):
paddle.disable_static() paddle.disable_static()
t = fluid.Tensor() t = fluid.Tensor()
np_x = np.random.random((3, 8, 8)) np_x = np.random.random((3, 8, 8))
...@@ -1781,14 +1567,9 @@ class TestVarBaseInitVarBaseFromTensorWithDevice(unittest.TestCase): ...@@ -1781,14 +1567,9 @@ class TestVarBaseInitVarBaseFromTensorWithDevice(unittest.TestCase):
tmp = fluid.core.eager.Tensor(t, device) tmp = fluid.core.eager.Tensor(t, device)
self.assertEqual(tmp.numpy().all(), np_x.all()) self.assertEqual(tmp.numpy().all(), np_x.all())
def test_varbase_init(self):
with _test_eager_guard():
self.func_test_varbase_init()
self.func_test_varbase_init()
class TestVarBaseNumel(unittest.TestCase): class TestVarBaseNumel(unittest.TestCase):
def func_test_numel_normal(self): def test_numel_normal(self):
paddle.disable_static() paddle.disable_static()
np_x = np.random.random((3, 8, 8)) np_x = np.random.random((3, 8, 8))
x = paddle.to_tensor(np_x, dtype="float64") x = paddle.to_tensor(np_x, dtype="float64")
...@@ -1796,12 +1577,7 @@ class TestVarBaseNumel(unittest.TestCase): ...@@ -1796,12 +1577,7 @@ class TestVarBaseNumel(unittest.TestCase):
x_expected_numel = np.product((3, 8, 8)) x_expected_numel = np.product((3, 8, 8))
self.assertEqual(x_actual_numel, x_expected_numel) self.assertEqual(x_actual_numel, x_expected_numel)
def test_numel_normal(self): def test_numel_without_holder(self):
with _test_eager_guard():
self.func_test_numel_normal()
self.func_test_numel_normal()
def func_test_numel_without_holder(self):
paddle.disable_static() paddle.disable_static()
if _in_legacy_dygraph(): if _in_legacy_dygraph():
x_without_holder = core.VarBase() x_without_holder = core.VarBase()
...@@ -1810,14 +1586,9 @@ class TestVarBaseNumel(unittest.TestCase): ...@@ -1810,14 +1586,9 @@ class TestVarBaseNumel(unittest.TestCase):
x_actual_numel = x_without_holder._numel() x_actual_numel = x_without_holder._numel()
self.assertEqual(x_actual_numel, 0) self.assertEqual(x_actual_numel, 0)
def ttest_numel_without_holder(self):
with _test_eager_guard():
self.func_test_numel_without_holder()
self.func_test_numel_without_holder()
class TestVarBaseCopyGradientFrom(unittest.TestCase): class TestVarBaseCopyGradientFrom(unittest.TestCase):
def func_test_copy_gradient_from(self): def test_copy_gradient_from(self):
paddle.disable_static() paddle.disable_static()
np_x = np.random.random((2, 2)) np_x = np.random.random((2, 2))
np_y = np.random.random((2, 2)) np_y = np.random.random((2, 2))
...@@ -1828,23 +1599,17 @@ class TestVarBaseCopyGradientFrom(unittest.TestCase): ...@@ -1828,23 +1599,17 @@ class TestVarBaseCopyGradientFrom(unittest.TestCase):
x._copy_gradient_from(y) x._copy_gradient_from(y)
self.assertEqual(x.grad.numpy().all(), np_y.all()) self.assertEqual(x.grad.numpy().all(), np_y.all())
def test_copy_gradient_from(self):
with _test_eager_guard():
self.func_test_copy_gradient_from()
self.func_test_copy_gradient_from()
class TestEagerTensorGradNameValue(unittest.TestCase): class TestEagerTensorGradNameValue(unittest.TestCase):
def test_eager_tensor_grad_name_value(self): def test_eager_tensor_grad_name_value(self):
with _test_eager_guard(): a_np = np.array([2, 3]).astype('float32')
a_np = np.array([2, 3]).astype('float32') a = paddle.to_tensor(a_np)
a = paddle.to_tensor(a_np) a.stop_gradient = False
a.stop_gradient = False b = a**2
b = a**2 self.assertIsNone(a._grad_value())
self.assertIsNone(a._grad_value()) b.backward()
b.backward() # Note, for new dygraph, there are no generated grad name, so we skip the name check.
# Note, for new dygraph, there are no generated grad name, so we skip the name check. self.assertIsNotNone(a._grad_value())
self.assertIsNotNone(a._grad_value())
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -17,7 +17,6 @@ import unittest ...@@ -17,7 +17,6 @@ import unittest
import numpy as np import numpy as np
import paddle import paddle
from paddle.fluid.framework import _test_eager_guard
# NOTE(pangyoki): Tensor View Strategy. # NOTE(pangyoki): Tensor View Strategy.
...@@ -36,7 +35,7 @@ class TestDygraphViewReuseAllocation(unittest.TestCase): ...@@ -36,7 +35,7 @@ class TestDygraphViewReuseAllocation(unittest.TestCase):
def view_api_processing(self, var): def view_api_processing(self, var):
return paddle.squeeze(var) return paddle.squeeze(var)
def func_test_view_api(self): def test_view_api(self):
var = paddle.rand(self.input_shape) var = paddle.rand(self.input_shape)
view_var = self.view_api_processing(var) view_var = self.view_api_processing(var)
view_var[0] = 2.0 view_var[0] = 2.0
...@@ -47,12 +46,7 @@ class TestDygraphViewReuseAllocation(unittest.TestCase): ...@@ -47,12 +46,7 @@ class TestDygraphViewReuseAllocation(unittest.TestCase):
view_var_numpy = view_var.numpy() view_var_numpy = view_var.numpy()
np.testing.assert_array_equal(var_numpy, view_var_numpy) np.testing.assert_array_equal(var_numpy, view_var_numpy)
def test_view_api(self): def test_forward_version(self):
with _test_eager_guard():
self.func_test_view_api()
self.func_test_view_api()
def func_test_forward_version(self):
var = paddle.rand(self.input_shape) var = paddle.rand(self.input_shape)
self.assertEqual(var.inplace_version, 0) self.assertEqual(var.inplace_version, 0)
view_var = self.view_api_processing(var) view_var = self.view_api_processing(var)
...@@ -69,12 +63,7 @@ class TestDygraphViewReuseAllocation(unittest.TestCase): ...@@ -69,12 +63,7 @@ class TestDygraphViewReuseAllocation(unittest.TestCase):
self.assertEqual(view_var.inplace_version, 2) self.assertEqual(view_var.inplace_version, 2)
self.assertEqual(view_var_2.inplace_version, 2) self.assertEqual(view_var_2.inplace_version, 2)
def test_forward_version(self): def test_backward_error(self):
with _test_eager_guard():
self.func_test_forward_version()
self.func_test_forward_version()
def func_test_backward_error(self):
# It raises an error because the inplace operator will result # It raises an error because the inplace operator will result
# in incorrect gradient computation. # in incorrect gradient computation.
with paddle.fluid.dygraph.guard(): with paddle.fluid.dygraph.guard():
...@@ -97,11 +86,6 @@ class TestDygraphViewReuseAllocation(unittest.TestCase): ...@@ -97,11 +86,6 @@ class TestDygraphViewReuseAllocation(unittest.TestCase):
): ):
loss.backward() loss.backward()
def test_backward_error(self):
with _test_eager_guard():
self.func_test_backward_error()
self.func_test_backward_error()
class TestUnsqueezeDygraphViewReuseAllocation(TestDygraphViewReuseAllocation): class TestUnsqueezeDygraphViewReuseAllocation(TestDygraphViewReuseAllocation):
def init_shape(self): def init_shape(self):
......
...@@ -23,7 +23,6 @@ import paddle ...@@ -23,7 +23,6 @@ import paddle
import paddle.fluid.core as core import paddle.fluid.core as core
import paddle.nn.functional as F import paddle.nn.functional as F
from paddle.fluid import Program, program_guard from paddle.fluid import Program, program_guard
from paddle.fluid.framework import _test_eager_guard
paddle.enable_static() paddle.enable_static()
...@@ -718,8 +717,6 @@ class TestCTCLossAPICase(unittest.TestCase): ...@@ -718,8 +717,6 @@ class TestCTCLossAPICase(unittest.TestCase):
loss_pd_sum, loss_np_sum, rtol=1e-05, atol=1 loss_pd_sum, loss_np_sum, rtol=1e-05, atol=1
) )
with _test_eager_guard():
test_functinal_api()
test_functinal_api() test_functinal_api()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册