From 1e3d2e4a6c6e1ec6d704d2b6f3ee34f92075723a Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Sat, 30 Apr 2022 10:12:35 +0800 Subject: [PATCH] [Eager] Support test_diff_op switch to eager mode (#42360) (#42392) --- paddle/fluid/eager/backward.cc | 8 +++++++- .../fluid/tests/unittests/test_diff_op.py | 19 +++++++++++++++++-- python/paddle/tensor/math.py | 18 ++++++++++++------ 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/paddle/fluid/eager/backward.cc b/paddle/fluid/eager/backward.cc index 7ca1b49bcbc..b1f31e20be4 100644 --- a/paddle/fluid/eager/backward.cc +++ b/paddle/fluid/eager/backward.cc @@ -546,7 +546,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 4a96827bd7c..b4359754520 100644 --- a/python/paddle/fluid/tests/unittests/test_diff_op.py +++ b/python/paddle/fluid/tests/unittests/test_diff_op.py @@ -19,6 +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 _test_eager_guard class TestDiffOp(unittest.TestCase): @@ -53,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) @@ -69,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()] @@ -108,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: @@ -127,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 ede3bcad2f3..efd1746f91b 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -3999,15 +3999,21 @@ def diff(x, n=1, axis=-1, prepend=None, append=None, name=None): attrs_2 += ('starts', starts_2) ends_2 = [dim_len] attrs_2 += ('ends', ends_2) - input_back = _C_ops.slice(new_input, None, None, None, None, 'axes', axes, \ - 'infer_flags', infer_flags, *attrs_2) + + if in_dygraph_mode(): + 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