From 5b3f91df0cdef5fe358f3decc2fdfb8187a1cd7a Mon Sep 17 00:00:00 2001 From: Li Min <11663212+limin2021@users.noreply.github.com> Date: Thu, 21 Jul 2022 16:29:44 +0800 Subject: [PATCH] Replace with dygraph op calling method. (#44331) * Replace with dygraph op calling method. --- python/paddle/fluid/dygraph/math_op_patch.py | 7 +++++-- python/paddle/fluid/tests/unittests/test_var_base.py | 2 +- python/paddle/tensor/creation.py | 8 ++++++-- python/paddle/tensor/manipulation.py | 5 ++--- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/python/paddle/fluid/dygraph/math_op_patch.py b/python/paddle/fluid/dygraph/math_op_patch.py index 13f11ea161..a7b399307e 100644 --- a/python/paddle/fluid/dygraph/math_op_patch.py +++ b/python/paddle/fluid/dygraph/math_op_patch.py @@ -376,8 +376,11 @@ def monkey_patch_math_varbase(): if framework._in_eager_mode_ else ('__rtruediv__', _binary_creator_('rtruediv__', 'elementwise_div', True, None)), - ('__pow__', _binary_creator_('__pow__', 'elementwise_pow', False, - None)), + ('__pow__', + _binary_creator_('__pow__', 'final_state_elementwise_pow', False, None, + True)) if framework._in_eager_mode_ else + ('__pow__', + _binary_creator_('__pow__', 'elementwise_pow', False, None)), ('__rpow__', _binary_creator_('__rpow__', 'elementwise_pow', True, None)), ('__floordiv__', diff --git a/python/paddle/fluid/tests/unittests/test_var_base.py b/python/paddle/fluid/tests/unittests/test_var_base.py index e66f310eb9..fd5f20a37d 100644 --- a/python/paddle/fluid/tests/unittests/test_var_base.py +++ b/python/paddle/fluid/tests/unittests/test_var_base.py @@ -1776,7 +1776,7 @@ class TestEagerTensorGradNameValue(unittest.TestCase): b = a**2 self.assertEqual(a._grad_value(), None) b.backward() - self.assertEqual('eager_in_tmp' in a._grad_name(), True) + # Note, for new dygraph, there are no generated grad name, so we skip the name check. self.assertNotEqual(a._grad_value(), None) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 85f8ba4aa4..d89918ad8b 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -663,8 +663,12 @@ def eye(num_rows, num_columns=None, dtype=None, name=None): num_columns = num_rows if _non_static_mode(): - out = _C_ops.eye('dtype', dtype, 'num_rows', num_rows, 'num_columns', - num_columns) + if in_dygraph_mode(): + out = _C_ops.final_state_eye(num_rows, num_columns, dtype, + _current_expected_place()) + elif _in_legacy_dygraph(): + out = _C_ops.eye('dtype', dtype, 'num_rows', num_rows, + 'num_columns', num_columns) else: helper = LayerHelper("eye", **locals()) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 06a2cfebf8..05823efc3f 100755 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -2705,8 +2705,7 @@ def scatter_nd_add(x, index, updates, name=None): # [3, 5, 9, 10] """ if in_dygraph_mode(): - op = getattr(_C_ops, 'scatter_nd_add') - return op(x, index, updates) + return _C_ops.final_state_scatter_nd_add(x, index, updates) else: if _in_legacy_dygraph(): op = getattr(_C_ops, 'scatter_nd_add') @@ -3002,7 +3001,7 @@ def broadcast_to(x, shape, name=None): # [[1, 2, 3], [1, 2, 3]] """ if paddle.in_dynamic_mode(): - return _C_ops.expand_v2(x, 'shape', shape) + return _C_ops.final_state_expand(x, shape) if isinstance(shape, Variable): assert len(shape.shape) == 1, ('shape must be an 1-D Tensor.') -- GitLab