From 21d94dd3a08f4e5700c7b98e2f66a8697cfea7a0 Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Fri, 29 Apr 2022 11:35:28 +0800 Subject: [PATCH] [Eager] Support test_diff_op switch to eager mode (#42360) --- paddle/fluid/eager/backward.cc | 8 ++++++- .../fluid/tests/unittests/test_diff_op.py | 21 +++++++++++++++---- python/paddle/tensor/math.py | 11 +++++----- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/paddle/fluid/eager/backward.cc b/paddle/fluid/eager/backward.cc index 3f56c2d01c7..7a4e7f81611 100644 --- a/paddle/fluid/eager/backward.cc +++ b/paddle/fluid/eager/backward.cc @@ -553,7 +553,13 @@ std::vector RunBackward( for (size_t i = 0; i < tensors.size(); i++) { const paddle::experimental::Tensor& tensor = tensors[i]; - AutogradMeta* auto_grad_meta = EagerUtils::unsafe_autograd_meta(tensor); + AutogradMeta* auto_grad_meta = EagerUtils::nullable_autograd_meta(tensor); + if (auto_grad_meta == nullptr) { + VLOG(3) << "Skip auto grad since there is no grad op for var or loss is " + "stop_gradient=True: " + << tensor.name(); + continue; + } // Get grad input info from target tensors auto input_info = auto_grad_meta->OutRankInfo(); diff --git a/python/paddle/fluid/tests/unittests/test_diff_op.py b/python/paddle/fluid/tests/unittests/test_diff_op.py index 99a46bfd958..b4359754520 100644 --- a/python/paddle/fluid/tests/unittests/test_diff_op.py +++ b/python/paddle/fluid/tests/unittests/test_diff_op.py @@ -19,8 +19,7 @@ import paddle import paddle.fluid as fluid import paddle.fluid.layers as layers import paddle.fluid.core as core -from paddle.fluid.framework import _enable_legacy_dygraph -_enable_legacy_dygraph() +from paddle.fluid.framework import _test_eager_guard class TestDiffOp(unittest.TestCase): @@ -55,7 +54,7 @@ class TestDiffOp(unittest.TestCase): if core.is_compiled_with_cuda(): self.places.append(paddle.CUDAPlace(0)) - def test_dygraph(self): + def func_dygraph(self): for place in self.places: paddle.disable_static() x = paddle.to_tensor(self.input, place=place) @@ -71,6 +70,13 @@ class TestDiffOp(unittest.TestCase): append=self.append) self.assertTrue((out.numpy() == self.output).all(), True) + def test_dygraph(self): + with _test_eager_guard(): + self.setUp() + self.func_dygraph() + self.setUp() + self.func_dygraph() + def test_static(self): paddle.enable_static() places = [fluid.CPUPlace()] @@ -110,7 +116,7 @@ class TestDiffOp(unittest.TestCase): fetch_list=[out]) self.assertTrue((fetches[0] == self.output).all(), True) - def test_grad(self): + def func_grad(self): for place in self.places: x = paddle.to_tensor(self.input, place=place, stop_gradient=False) if self.prepend is not None: @@ -129,6 +135,13 @@ class TestDiffOp(unittest.TestCase): except: raise RuntimeError("Check Diff Gradient Failed") + def test_grad(self): + with _test_eager_guard(): + self.setUp() + self.func_grad() + self.setUp() + self.func_grad() + class TestDiffOpAxis(TestDiffOp): def set_args(self): diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index b7b08af9e60..83501b03994 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -4260,18 +4260,19 @@ def diff(x, n=1, axis=-1, prepend=None, append=None, name=None): ends_2 = [dim_len] attrs_2 += ('ends', ends_2) if in_dygraph_mode(): - input_back = input_front = _C_ops.final_state_slice(new_input, axes, starts_2, ends_2, infer_flags, + input_back = _C_ops.final_state_slice(new_input, axes, starts_2, ends_2, infer_flags, []) else: input_back = _C_ops.slice(new_input, None, None, None, None, 'axes', axes, \ 'infer_flags', infer_flags, *attrs_2) if x.dtype == paddle.bool: - op = getattr(_C_ops, "logical_xor") - out = op(input_back, input_front) + if in_dygraph_mode(): + return _C_ops.final_state_logical_xor(input_back, input_front) + else: + return _C_ops.logical_xor(input_back, input_front) else: - out = elementwise_sub(input_back, input_front, axis=axis) - return out + return elementwise_sub(input_back, input_front, axis=axis) else: check_variable_and_dtype(x, 'x', ['float32', 'float64', 'bool', 'int32', 'int64'], 'diff') -- GitLab