From e354237e6be4fa0144de4cef5f79f0e855efa95f Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Mon, 3 Jul 2023 03:09:33 +0000 Subject: [PATCH 01/22] batch add inpalce api --- paddle/phi/api/yaml/ops.yaml | 29 ++++++++++++++++++++--------- test/legacy_test/test_pow.py | 4 ++-- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 2c5d7bc256537..1621ee56c672f 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -13,6 +13,7 @@ kernel : func : abs data_type : x + inplace: (x -> out) backward : abs_grad - op : accuracy @@ -26,20 +27,22 @@ - op : acos args : (Tensor x) - output : Tensor + output : Tensor(out) infer_meta : func : UnchangedInferMeta kernel : func : acos + inplace: (x -> out) backward : acos_grad - op : acosh args : (Tensor x) - output : Tensor + output : Tensor(out) infer_meta : func : UnchangedInferMeta kernel : func : acosh + inplace: (x -> out) backward : acosh_grad - op : adagrad_ @@ -96,6 +99,7 @@ kernel : func : addmm data_type : x + inplace: (x -> out) backward : addmm_grad - op : affine_grid @@ -176,47 +180,52 @@ - op : asin args : (Tensor x) - output : Tensor + output : Tensor(out) infer_meta : func : UnchangedInferMeta kernel : func : asin + inplace: (x -> out) backward : asin_grad - op : asinh args : (Tensor x) - output : Tensor + output : Tensor(out) infer_meta : func : UnchangedInferMeta kernel : func : asinh + inplace: (x -> out) backward : asinh_grad - op : atan args : (Tensor x) - output : Tensor + output : Tensor(out) infer_meta : func : UnchangedInferMeta kernel : func : atan + inplace: (x -> out) backward : atan_grad - op : atan2 args : (Tensor x, Tensor y) - output : Tensor + output : Tensor(out) infer_meta : func : Atan2InferMeta kernel : func : atan2 + inplace: (x -> out) backward : atan2_grad - op : atanh args : (Tensor x) - output : Tensor + output : Tensor(out) infer_meta : func : UnchangedInferMeta kernel : func : atanh + inplace: (x -> out) backward : atanh_grad - op : auc @@ -513,20 +522,22 @@ - op : cos args : (Tensor x) - output : Tensor + output : Tensor(out) infer_meta : func : UnchangedInferMeta kernel : func : cos + inplace: (x -> out) backward : cos_grad - op : cosh args : (Tensor x) - output : Tensor + output : Tensor(out) infer_meta : func : UnchangedInferMeta kernel : func : cosh + inplace: (x -> out) backward : cosh_grad - op : crop diff --git a/test/legacy_test/test_pow.py b/test/legacy_test/test_pow.py index e829230492eee..86f4e67e92458 100755 --- a/test/legacy_test/test_pow.py +++ b/test/legacy_test/test_pow.py @@ -214,7 +214,7 @@ def test_errors(self): self.assertRaises(TypeError, paddle.pow, x, str(y)) -class TestInplacePowerScalar(TestDygraphInplace): +class TestDygraphInplacePowerScalar(TestDygraphInplace): def set_np_compare_func(self): self.np_compare = np.allclose @@ -225,7 +225,7 @@ def non_inplace_api_processing(self, var): return paddle.pow(var, 2) -class TestInplacePowerTensor(TestDygraphInplace): +class TestDygraphInplacePowerTensor(TestDygraphInplace): def init_data(self): self.input_var_numpy = np.random.uniform(-5, 5, [10, 20, 1]) self.dtype = "float32" From bf41254bf4cbe68050e2aa259b93c94224b87549 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Mon, 3 Jul 2023 06:58:01 +0000 Subject: [PATCH 02/22] fix inplace fn generate --- paddle/phi/api/yaml/ops.yaml | 9 ++-- .../paddle/tensor/layer_function_generator.py | 13 ++--- python/paddle/tensor/ops.py | 13 +++++ test/legacy_test/test_inplace.py | 52 +++++++++++++++++++ 4 files changed, 74 insertions(+), 13 deletions(-) diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 1621ee56c672f..e24b44e728aa6 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -2221,20 +2221,22 @@ - op : sin args : (Tensor x) - output : Tensor + output : Tensor(out) infer_meta : func : UnchangedInferMeta kernel : func : sin + inplace: (x -> out) backward : sin_grad - op : sinh args : (Tensor x) - output : Tensor + output : Tensor(out) infer_meta : func : UnchangedInferMeta kernel : func : sinh + inplace: (x -> out) backward : sinh_grad - op : slogdet @@ -2380,11 +2382,12 @@ - op : tan args : (Tensor x) - output : Tensor + output : Tensor(out) infer_meta : func : UnchangedInferMeta kernel : func : tan + inplace: (x -> out) backward : tan_grad - op : tanh diff --git a/python/paddle/tensor/layer_function_generator.py b/python/paddle/tensor/layer_function_generator.py index bdf8ac6e30d87..05e69e11a7b7b 100644 --- a/python/paddle/tensor/layer_function_generator.py +++ b/python/paddle/tensor/layer_function_generator.py @@ -14,10 +14,10 @@ import re import string -import warnings from io import StringIO from paddle import _C_ops, _legacy_C_ops +from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only from ..common_ops_import import Variable from ..fluid.data_feeder import check_variable_and_dtype @@ -352,22 +352,15 @@ def func(x, name=None): else: op = getattr(_legacy_C_ops, inplace_op_type) return op(x) - else: - warnings.warn( - "In static graph mode, {}() is the same as {}() and does not perform inplace operation.".format( - inplace_op_type, origin_op_type - ) - ) - return generate_activation_fn(origin_op_type)(x, name) func.__name__ = inplace_op_type func.__doc__ = """ Inplace version of ``{}`` API, the output Tensor will be inplaced with input ``x``. -Please refer to :ref:`api_fluid_layers_{}`. +Please refer to :ref:`api_paddle_{}`. """.format( origin_op_type, origin_op_type ) - + func = inplace_apis_in_dygraph_only(func) return func diff --git a/python/paddle/tensor/ops.py b/python/paddle/tensor/ops.py index c3f6917c6ab4b..bf718dbab85f6 100644 --- a/python/paddle/tensor/ops.py +++ b/python/paddle/tensor/ops.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from .. import _C_ops from ..fluid.data_feeder import check_variable_and_dtype from ..framework import LayerHelper, in_dynamic_mode @@ -47,6 +48,18 @@ 'round_', 'reciprocal_', 'sigmoid_', + 'abs_' + 'sin_' + 'sinh_' + 'asin_' + 'asinh_' + 'cos_' + 'cosh_' + 'acos_' + 'acosh_' + 'tan_' + 'atan_' + 'atanh_', ] __all__ = [] diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index 91a569d34c62b..e4f93e9f32357 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -122,6 +122,12 @@ def test_inplace_api(self): inplace_var[0] = 2.0 np.testing.assert_array_equal(var.numpy(), inplace_var.numpy()) + var = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) + inplace_var = self.inplace_api_processing(var) + self.assertTrue(id(var) == id(inplace_var)) + + inplace_var[0] = 2.0 + np.testing.assert_array_equal(var.numpy(), inplace_var.numpy()) def test_forward_version(self): with paddle.fluid.dygraph.guard(): @@ -241,6 +247,42 @@ def test_backward_success_2(self): np.testing.assert_array_equal(grad_var_a_inplace, grad_var_a) +class TestDygraphInplaceWithContinuous(unittest.TestCase): + def non_inplace_api_processing(self, var): + return paddle.sin(var) + + def inplace_api_processing(self, var): + return paddle.sin_(var) + + def test_continuous_inplace_backward(self): + # The api that only relies on input to calculate the gradient will copy input before + # the inpalce calculation, so here supports continuous inpalce backward calculation. + grad_var_a, grad_var_a_inplace = 0, 1 + with paddle.fluid.dygraph.guard(): + var_a = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) + var_a.stop_gradient = False + + var_b = var_a**2 + var_c = self.inplace_api_processing(var_b) + var_d = self.inplace_api_processing(var_c) + loss = var_d.sum() + loss.backward() + grad_var_a_inplace = var_a.grad.numpy() + + with paddle.fluid.dygraph.guard(): + var_a = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) + var_a.stop_gradient = False + + var_b = var_a**2 + var_c = self.non_inplace_api_processing(var_b) + var_d = self.non_inplace_api_processing(var_c) + loss = var_d.sum() + loss.backward() + grad_var_a = var_a.grad.numpy() + + self.assertTrue(self.np_compare(grad_var_a_inplace, grad_var_a)) + + class TestDygraphInplaceUnsqueeze(TestDygraphInplace): def non_inplace_api_processing(self, var): return paddle.unsqueeze(var, -1) @@ -506,5 +548,15 @@ def test_getitem_before_inplace(self): loss.backward() +class TestDygraphInplaceSin(TestDygraphInplace): + def non_inplace_api_processing(self, var): + input_var_2 = paddle.to_tensor(self.input_var_numpy_2) + return var.remainder(input_var_2) + + def inplace_api_processing(self, var): + input_var_2 = paddle.to_tensor(self.input_var_numpy_2) + return var.remainder_(input_var_2) + + if __name__ == '__main__': unittest.main() From e1a69e94c671126ed03160c13b42573d8f1e0925 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Mon, 3 Jul 2023 09:04:47 +0000 Subject: [PATCH 03/22] add test for new inpalce api --- python/paddle/__init__.py | 23 ++++++++ python/paddle/tensor/__init__.py | 14 +++++ python/paddle/tensor/math.py | 82 +++++++++++++++++++++++++++ test/legacy_test/test_addmm_op.py | 9 +++ test/legacy_test/test_atan2_op.py | 14 +++++ test/legacy_test/test_inplace.py | 94 +++++++++++++++++++++++++++---- test/legacy_test/test_pow.py | 6 +- 7 files changed, 228 insertions(+), 14 deletions(-) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 4963ad8b51160..8c0c6c8375364 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -203,14 +203,22 @@ from .tensor.manipulation import index_put_ # noqa: F401 from .tensor.manipulation import unflatten # noqa: F401 from .tensor.math import abs # noqa: F401 +from .tensor.math import abs_ # noqa: F401 from .tensor.math import acos # noqa: F401 +from .tensor.math import acos_ # noqa: F401 from .tensor.math import asin # noqa: F401 +from .tensor.math import asin_ # noqa: F401 from .tensor.math import atan # noqa: F401 +from .tensor.math import atan_ # noqa: F401 from .tensor.math import atan2 # noqa: F401 +from .tensor.math import atan2_ # noqa: F401 from .tensor.math import ceil # noqa: F401 from .tensor.math import cos # noqa: F401 +from .tensor.math import cos_ # noqa: F401 from .tensor.math import tan # noqa: F401 +from .tensor.math import tan_ # noqa: F401 from .tensor.math import cosh # noqa: F401 +from .tensor.math import cosh_ # noqa: F401 from .tensor.math import cumsum # noqa: F401 from .tensor.math import cummax # noqa: F401 from .tensor.math import cummin # noqa: F401 @@ -235,7 +243,9 @@ from .tensor.math import scale # noqa: F401 from .tensor.math import sign # noqa: F401 from .tensor.math import sin # noqa: F401 +from .tensor.math import sin_ # noqa: F401 from .tensor.math import sinh # noqa: F401 +from .tensor.math import sinh_ # noqa: F401 from .tensor.math import sqrt # noqa: F401 from .tensor.math import square # noqa: F401 from .tensor.math import stanh # noqa: F401 @@ -270,6 +280,7 @@ from .tensor.math import log1p # noqa: F401 from .tensor.math import erf # noqa: F401 from .tensor.math import addmm # noqa: F401 +from .tensor.math import addmm_ # noqa: F401 from .tensor.math import clip # noqa: F401 from .tensor.math import trace # noqa: F401 from .tensor.math import diagonal # noqa: F401 @@ -285,8 +296,11 @@ from .tensor.math import neg # noqa: F401 from .tensor.math import lgamma # noqa: F401 from .tensor.math import acosh # noqa: F401 +from .tensor.math import acosh_ # noqa: F401 from .tensor.math import asinh # noqa: F401 +from .tensor.math import asinh_ # noqa: F401 from .tensor.math import atanh # noqa: F401 +from .tensor.math import atanh_ # noqa: F401 from .tensor.math import lerp # noqa: F401 from .tensor.math import erfinv # noqa: F401 from .tensor.math import rad2deg # noqa: F401 @@ -431,6 +445,7 @@ 'complex64', 'complex128', 'addmm', + 'addmm_', 'allclose', 'isclose', 't', @@ -468,7 +483,9 @@ 'where', 'log1p', 'cos', + 'cos_', 'tan', + 'tan_', 'mean', 'mode', 'mv', @@ -543,6 +560,7 @@ 'less_equal', 'triu', 'sin', + 'sin_', 'dist', 'cdist', 'unbind', @@ -560,6 +578,7 @@ 'is_grad_enabled', 'mod', 'abs', + 'abs_', 'tril', 'pow', 'pow_', @@ -571,12 +590,14 @@ 'matmul', 'seed', 'acos', + 'acos_', 'logical_xor', 'exp', 'expm1', 'bernoulli', 'poisson', 'sinh', + 'sinh_', 'round', 'DataParallel', 'argmin', @@ -593,7 +614,9 @@ 'divide', 'ceil', 'atan', + 'atan_', 'atan2', + 'atan2_', 'rad2deg', 'deg2rad', 'gcd', diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 95623f145b63d..2b70beca4ddf4 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -141,14 +141,21 @@ from .manipulation import index_put_ # noqa: F401 from .manipulation import unflatten # noqa: F401 from .math import abs # noqa: F401 +from .math import abs_ # noqa: F401 from .math import acos # noqa: F401 +from .math import acos_ # noqa: F401 from .math import asin # noqa: F401 +from .math import asin_ # noqa: F401 from .math import atan # noqa: F401 +from .math import atan_ # noqa: F401 from .math import ceil # noqa: F401 from .math import ceil_ # noqa: F401 from .math import cos # noqa: F401 +from .math import cos_ # noqa: F401 from .math import tan # noqa: F401 +from .math import tan_ # noqa: F401 from .math import cosh # noqa: F401 +from .math import cosh_ # noqa: F401 from .math import cumsum # noqa: F401 from .math import cummax # noqa: F401 from .math import cummin # noqa: F401 @@ -175,7 +182,9 @@ from .math import scale_ # noqa: F401 from .math import sign # noqa: F401 from .math import sin # noqa: F401 +from .math import sin_ # noqa: F401 from .math import sinh # noqa: F401 +from .math import sinh_ # noqa: F401 from .math import sqrt # noqa: F401 from .math import sqrt_ # noqa: F401 from .math import square # noqa: F401 @@ -208,6 +217,7 @@ from .math import subtract # noqa: F401 from .math import subtract_ # noqa: F401 from .math import atan2 # noqa: F401 +from .math import atan2_ # noqa: F401 from .math import logsumexp # noqa: F401 from .math import logaddexp # noqa: F401 from .math import inverse # noqa: F401 @@ -216,6 +226,7 @@ from .math import log1p # noqa: F401 from .math import erf # noqa: F401 from .math import addmm # noqa: F401 +from .math import addmm_ # noqa: F401 from .math import clip # noqa: F401 from .math import clip_ # noqa: F401 from .math import trace # noqa: F401 @@ -234,8 +245,11 @@ from .math import lgamma # noqa: F401 from .math import diagonal # noqa: F401 from .math import acosh # noqa: F401 +from .math import acosh_ # noqa: F401 from .math import asinh # noqa: F401 +from .math import asinh_ # noqa: F401 from .math import atanh # noqa: F401 +from .math import atanh_ # noqa: F401 from .math import lerp # noqa: F401 from .math import lerp_ # noqa: F401 from .math import erfinv # noqa: F401 diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 9998ec79ef725..0573dccb9b027 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -43,16 +43,25 @@ from .layer_function_generator import generate_layer_fn, templatedoc from .manipulation import cast from .ops import abs # noqa: F401 +from .ops import abs_ # noqa: F401 from .ops import acos # noqa: F401 +from .ops import acos_ # noqa: F401 from .ops import acosh # noqa: F401 +from .ops import acosh_ # noqa: F401 from .ops import asin # noqa: F401 +from .ops import asin_ # noqa: F401 from .ops import asinh # noqa: F401 +from .ops import asinh_ # noqa: F401 from .ops import atan # noqa: F401 +from .ops import atan_ # noqa: F401 from .ops import atanh # noqa: F401 +from .ops import atanh_ # noqa: F401 from .ops import ceil # noqa: F401 from .ops import ceil_ # noqa: F401 from .ops import cos # noqa: F401 +from .ops import cos_ # noqa: F401 from .ops import cosh # noqa: F401 +from .ops import cosh_ # noqa: F401 from .ops import erf # noqa: F401 from .ops import exp # noqa: F401 from .ops import exp_ # noqa: F401 @@ -68,11 +77,14 @@ from .ops import sigmoid # noqa: F401 from .ops import sigmoid_ # noqa: F401 from .ops import sin # noqa: F401 +from .ops import sin_ # noqa: F401 from .ops import sinh # noqa: F401 +from .ops import sinh_ # noqa: F401 from .ops import sqrt # noqa: F401 from .ops import sqrt_ # noqa: F401 from .ops import square # noqa: F401 from .ops import tan # noqa: F401 +from .ops import tan_ # noqa: F401 __all__ = [] @@ -2043,6 +2055,66 @@ def addmm(input, x, y, beta=1.0, alpha=1.0, name=None): return out +@inplace_apis_in_dygraph_only +def addmm_(input, x, y, beta=1.0, alpha=1.0, name=None): + """ + Inplace version of ``scale`` API, the output Tensor will be inplaced with input ``x``. + Please refer to :ref:`api_label_addmm`. + """ + input_shape = input.shape + x_shape = x.shape + y_shape = y.shape + if not len(x_shape) == len(y_shape) == 2: + raise ValueError( + "The dimention of x, y should be 2 but receive x's shape: {}, y's shape: {}".format( + x_shape, y_shape + ) + ) + if x_shape[1] != y_shape[0]: + raise ValueError( + "The input Variable x's width must be equal with Variable y' height. But received x's shape = {}, y's shape = {}.".format( + x_shape, y_shape + ) + ) + if len(input_shape) == 2: + if input_shape[0] != x_shape[0]: + if input_shape[0] != 1: + raise ValueError( + "When x's dimension[0] is not equal with input's dimension[0], input's dimension[0] must be 1 but got {}".format( + input_shape[0] + ) + ) + if input_shape[1] != y_shape[1] and input_shape[1] != 1: + raise ValueError( + "When y's dimension[1] is not equal with input's dimension[1], input's dimension[1] must be 1 but got {}".format( + input_shape[1] + ) + ) + if input_shape[1] != y_shape[1]: + if input_shape[1] != 1: + raise ValueError( + "When y's dimension[1] is not equal with input's dimension[1], input's dimension[1] must be 1 but got {}".format( + input_shape[1] + ) + ) + elif len(input_shape) == 1: + if input_shape[0] not in (y_shape[1], 1): + raise ValueError( + "The input's shape: {} is not broadcastable with [x.shape[0], y.shape[1]]: [{},{}]".format( + input_shape, x_shape[0], y_shape[1] + ) + ) + else: + raise ValueError( + "The dimention of input should be 2 or 1 but receive input's shape: {}".format( + input_shape + ) + ) + + if in_dynamic_mode(): + return _C_ops.addmm(input, x, y, beta, alpha) + + def renorm(x, p, axis, max_norm): """ **renorm** @@ -4443,6 +4515,16 @@ def atan2(x, y, name=None): return out +@inplace_apis_in_dygraph_only +def atan2_(x, y, name=None): + r""" + Inplace version of ``lerp`` API, the output Tensor will be inplaced with input ``x``. + Please refer to :ref:`api_paddle_atan2`. + """ + if in_dynamic_mode(): + return _C_ops.atan2(x, y) + + def logit(x, eps=None, name=None): r""" This function generates a new tensor with the logit of the elements of input x. x is clamped to [eps, 1-eps] when eps is not zero. When eps is zero and x < 0 or x > 1, the function will yields NaN. diff --git a/test/legacy_test/test_addmm_op.py b/test/legacy_test/test_addmm_op.py index 66a86961e885d..90cb77af803e5 100644 --- a/test/legacy_test/test_addmm_op.py +++ b/test/legacy_test/test_addmm_op.py @@ -16,6 +16,7 @@ import numpy as np from eager_op_test import OpTest, convert_float_to_uint16 +from test_inplace import TestDygraphInplaceWithContinuous import paddle from paddle import fluid @@ -461,6 +462,14 @@ def test_api_normal_3(self): paddle.enable_static() +class TestDygraphInplaceAddMM(TestDygraphInplaceWithContinuous): + def non_inplace_api_processing(self, var): + return paddle.addmm(var) + + def inplace_api_processing(self, var): + return paddle.addmm_(var) + + if __name__ == "__main__": paddle.enable_static() unittest.main() diff --git a/test/legacy_test/test_atan2_op.py b/test/legacy_test/test_atan2_op.py index ddcaa40dee980..ec536726bbc42 100644 --- a/test/legacy_test/test_atan2_op.py +++ b/test/legacy_test/test_atan2_op.py @@ -16,6 +16,7 @@ import numpy as np from eager_op_test import OpTest, convert_float_to_uint16 +from test_inplace import TestDygraphInplaceWithContinuous import paddle from paddle.fluid import core @@ -131,6 +132,19 @@ def run(place): run(place) +class TestDygraphInplaceAtan2(TestDygraphInplaceWithContinuous): + def init_data(self): + self.input_var_numpy = np.random.uniform(-5, 5, [10, 20, 1]) + self.dtype = "float32" + self.y = paddle.randn([10, 20, 1]) + + def non_inplace_api_processing(self, var): + return paddle.attan2(var) + + def inplace_api_processing(self, var): + return super().inplace_api_processing(var) + + @unittest.skipIf( not core.is_compiled_with_cuda() or not core.is_bfloat16_supported(core.CUDAPlace(0)), diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index e4f93e9f32357..7c77e325862db 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -122,12 +122,6 @@ def test_inplace_api(self): inplace_var[0] = 2.0 np.testing.assert_array_equal(var.numpy(), inplace_var.numpy()) - var = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) - inplace_var = self.inplace_api_processing(var) - self.assertTrue(id(var) == id(inplace_var)) - - inplace_var[0] = 2.0 - np.testing.assert_array_equal(var.numpy(), inplace_var.numpy()) def test_forward_version(self): with paddle.fluid.dygraph.guard(): @@ -548,14 +542,92 @@ def test_getitem_before_inplace(self): loss.backward() -class TestDygraphInplaceSin(TestDygraphInplace): +class TestDygraphInplaceAsin(TestDygraphInplaceWithContinuous): def non_inplace_api_processing(self, var): - input_var_2 = paddle.to_tensor(self.input_var_numpy_2) - return var.remainder(input_var_2) + return paddle.asin(var) def inplace_api_processing(self, var): - input_var_2 = paddle.to_tensor(self.input_var_numpy_2) - return var.remainder_(input_var_2) + return paddle.asin_(var) + + +class TestDygraphInplaceSinh(TestDygraphInplaceWithContinuous): + def non_inplace_api_processing(self, var): + return paddle.sinh(var) + + def inplace_api_processing(self, var): + return paddle.sinh_(var) + + +class TestDygraphInplaceAsinh(TestDygraphInplaceWithContinuous): + def non_inplace_api_processing(self, var): + return paddle.asinh(var) + + def inplace_api_processing(self, var): + return paddle.asinh_(var) + + +class TestDygraphInplaceAbs(TestDygraphInplaceWithContinuous): + def non_inplace_api_processing(self, var): + return paddle.abs(var) + + def inplace_api_processing(self, var): + return paddle.abs_(var) + + +class TestDygraphInplaceCos(TestDygraphInplaceWithContinuous): + def non_inplace_api_processing(self, var): + return paddle.cos(var) + + def inplace_api_processing(self, var): + return paddle.cos_(var) + + +class TestDygraphInplaceCosh(TestDygraphInplaceWithContinuous): + def non_inplace_api_processing(self, var): + return paddle.cosh(var) + + def inplace_api_processing(self, var): + return paddle.cosh_(var) + + +class TestDygraphInplaceAcos(TestDygraphInplaceWithContinuous): + def non_inplace_api_processing(self, var): + return paddle.acos(var) + + def inplace_api_processing(self, var): + return paddle.acos_(var) + + +class TestDygraphInplaceAcosh(TestDygraphInplaceWithContinuous): + def non_inplace_api_processing(self, var): + return paddle.acosh(var) + + def inplace_api_processing(self, var): + return paddle.acosh_(var) + + +class TestDygraphInplaceTan(TestDygraphInplaceWithContinuous): + def non_inplace_api_processing(self, var): + return paddle.tan(var) + + def inplace_api_processing(self, var): + return paddle.tan_(var) + + +class TestDygraphInplaceATan(TestDygraphInplaceWithContinuous): + def non_inplace_api_processing(self, var): + return paddle.atan(var) + + def inplace_api_processing(self, var): + return paddle.atan_(var) + + +class TestDygraphInplaceATanh(TestDygraphInplaceWithContinuous): + def non_inplace_api_processing(self, var): + return paddle.atanh(var) + + def inplace_api_processing(self, var): + return paddle.atanh_(var) if __name__ == '__main__': diff --git a/test/legacy_test/test_pow.py b/test/legacy_test/test_pow.py index 86f4e67e92458..5fd81d757e7d3 100755 --- a/test/legacy_test/test_pow.py +++ b/test/legacy_test/test_pow.py @@ -15,7 +15,7 @@ import unittest import numpy as np -from test_inplace import TestDygraphInplace +from test_inplace import TestDygraphInplaceWithContinuous import paddle from paddle.fluid import core @@ -214,7 +214,7 @@ def test_errors(self): self.assertRaises(TypeError, paddle.pow, x, str(y)) -class TestDygraphInplacePowerScalar(TestDygraphInplace): +class TestDygraphInplacePowerScalar(TestDygraphInplaceWithContinuous): def set_np_compare_func(self): self.np_compare = np.allclose @@ -225,7 +225,7 @@ def non_inplace_api_processing(self, var): return paddle.pow(var, 2) -class TestDygraphInplacePowerTensor(TestDygraphInplace): +class TestDygraphInplacePowerTensor(TestDygraphInplaceWithContinuous): def init_data(self): self.input_var_numpy = np.random.uniform(-5, 5, [10, 20, 1]) self.dtype = "float32" From 95de00826ef598748a6da7cdbc3b06f4f38cadef Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Mon, 3 Jul 2023 09:43:09 +0000 Subject: [PATCH 04/22] fix typro --- test/legacy_test/test_atan2_op.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/legacy_test/test_atan2_op.py b/test/legacy_test/test_atan2_op.py index ec536726bbc42..47f2a931510f9 100644 --- a/test/legacy_test/test_atan2_op.py +++ b/test/legacy_test/test_atan2_op.py @@ -139,10 +139,10 @@ def init_data(self): self.y = paddle.randn([10, 20, 1]) def non_inplace_api_processing(self, var): - return paddle.attan2(var) + return paddle.attan2(var, self.y) def inplace_api_processing(self, var): - return super().inplace_api_processing(var) + return paddle.attan2_(var, self.y) @unittest.skipIf( From f90749b2d68a71a04ecf6f6c8ef872af07eeaccd Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Tue, 4 Jul 2023 07:31:12 +0000 Subject: [PATCH 05/22] fix typro --- python/paddle/tensor/ops.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/python/paddle/tensor/ops.py b/python/paddle/tensor/ops.py index bf718dbab85f6..3bcb8bdf01ae9 100644 --- a/python/paddle/tensor/ops.py +++ b/python/paddle/tensor/ops.py @@ -48,17 +48,17 @@ 'round_', 'reciprocal_', 'sigmoid_', - 'abs_' - 'sin_' - 'sinh_' - 'asin_' - 'asinh_' - 'cos_' - 'cosh_' - 'acos_' - 'acosh_' - 'tan_' - 'atan_' + 'abs_', + 'sin_', + 'sinh_', + 'asin_', + 'asinh_', + 'cos_', + 'cosh_', + 'acos_', + 'acosh_', + 'tan_', + 'atan_', 'atanh_', ] From 6291428dbd578ef3521156c976c5d27facd265e9 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Tue, 4 Jul 2023 07:36:30 +0000 Subject: [PATCH 06/22] fix typro --- test/legacy_test/test_inplace.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index 7c77e325862db..50e3e219bc1c0 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -241,7 +241,7 @@ def test_backward_success_2(self): np.testing.assert_array_equal(grad_var_a_inplace, grad_var_a) -class TestDygraphInplaceWithContinuous(unittest.TestCase): +class TestDygraphInplaceWithContinuous(TestDygraphInplace): def non_inplace_api_processing(self, var): return paddle.sin(var) From d86de214206408eaba86b323bc08fb5f14d80441 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Tue, 4 Jul 2023 09:46:32 +0000 Subject: [PATCH 07/22] fix test error --- paddle/phi/api/yaml/ops.yaml | 4 +-- python/paddle/tensor/__init__.py | 1 + python/paddle/tensor/math.py | 2 +- test/legacy_test/test_addmm_op.py | 9 ----- test/legacy_test/test_inplace.py | 57 +++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 12 deletions(-) diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index e24b44e728aa6..10fe1ca5a5aee 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -93,13 +93,13 @@ - op : addmm args : (Tensor input, Tensor x, Tensor y, float beta=1.0, float alpha=1.0) - output : Tensor + output : Tensor(out) infer_meta : func : AddmmInferMeta kernel : func : addmm data_type : x - inplace: (x -> out) + inplace: (input -> out) backward : addmm_grad - op : affine_grid diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 2b70beca4ddf4..8ef0e95aa1b47 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -435,6 +435,7 @@ 'log1p', 'erf', 'addmm', + 'addmm_', 'clip', 'clip_', 'trace', diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 0573dccb9b027..c8b4b9d5ef8d3 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -2112,7 +2112,7 @@ def addmm_(input, x, y, beta=1.0, alpha=1.0, name=None): ) if in_dynamic_mode(): - return _C_ops.addmm(input, x, y, beta, alpha) + return _C_ops.addmm_(input, x, y, beta, alpha) def renorm(x, p, axis, max_norm): diff --git a/test/legacy_test/test_addmm_op.py b/test/legacy_test/test_addmm_op.py index 90cb77af803e5..66a86961e885d 100644 --- a/test/legacy_test/test_addmm_op.py +++ b/test/legacy_test/test_addmm_op.py @@ -16,7 +16,6 @@ import numpy as np from eager_op_test import OpTest, convert_float_to_uint16 -from test_inplace import TestDygraphInplaceWithContinuous import paddle from paddle import fluid @@ -462,14 +461,6 @@ def test_api_normal_3(self): paddle.enable_static() -class TestDygraphInplaceAddMM(TestDygraphInplaceWithContinuous): - def non_inplace_api_processing(self, var): - return paddle.addmm(var) - - def inplace_api_processing(self, var): - return paddle.addmm_(var) - - if __name__ == "__main__": paddle.enable_static() unittest.main() diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index 50e3e219bc1c0..4a7bc5002a3e7 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import functools import unittest import numpy as np @@ -543,6 +544,13 @@ def test_getitem_before_inplace(self): class TestDygraphInplaceAsin(TestDygraphInplaceWithContinuous): + # asin calculation will appear some nan + def set_np_compare_func(self): + np_array_equal_with_nan = functools.partial( + np.array_equal, equal_nan=True + ) + self.np_compare = np_array_equal_with_nan + def non_inplace_api_processing(self, var): return paddle.asin(var) @@ -559,6 +567,13 @@ def inplace_api_processing(self, var): class TestDygraphInplaceAsinh(TestDygraphInplaceWithContinuous): + # asinh calculation will appear some nan + def set_np_compare_func(self): + np_array_equal_with_nan = functools.partial( + np.array_equal, equal_nan=True + ) + self.np_compare = np_array_equal_with_nan + def non_inplace_api_processing(self, var): return paddle.asinh(var) @@ -591,6 +606,13 @@ def inplace_api_processing(self, var): class TestDygraphInplaceAcos(TestDygraphInplaceWithContinuous): + # acos calculation will appear some nan + def set_np_compare_func(self): + np_array_equal_with_nan = functools.partial( + np.array_equal, equal_nan=True + ) + self.np_compare = np_array_equal_with_nan + def non_inplace_api_processing(self, var): return paddle.acos(var) @@ -599,6 +621,13 @@ def inplace_api_processing(self, var): class TestDygraphInplaceAcosh(TestDygraphInplaceWithContinuous): + # acosh calculation will appear some nan + def set_np_compare_func(self): + np_array_equal_with_nan = functools.partial( + np.array_equal, equal_nan=True + ) + self.np_compare = np_array_equal_with_nan + def non_inplace_api_processing(self, var): return paddle.acosh(var) @@ -615,6 +644,13 @@ def inplace_api_processing(self, var): class TestDygraphInplaceATan(TestDygraphInplaceWithContinuous): + # atan calculation will appear some nan + def set_np_compare_func(self): + np_array_equal_with_nan = functools.partial( + np.array_equal, equal_nan=True + ) + self.np_compare = np_array_equal_with_nan + def non_inplace_api_processing(self, var): return paddle.atan(var) @@ -623,6 +659,13 @@ def inplace_api_processing(self, var): class TestDygraphInplaceATanh(TestDygraphInplaceWithContinuous): + # atanh calculation will appear some nan + def set_np_compare_func(self): + np_array_equal_with_nan = functools.partial( + np.array_equal, equal_nan=True + ) + self.np_compare = np_array_equal_with_nan + def non_inplace_api_processing(self, var): return paddle.atanh(var) @@ -630,5 +673,19 @@ def inplace_api_processing(self, var): return paddle.atanh_(var) +class TestDygraphInplaceAddMM(TestDygraphInplaceWithContinuous): + def init_data(self): + self.input_var_numpy = np.random.uniform(-5, 5, [10, 10]) + self.dtype = "float32" + self.x = paddle.randn([10, 10]) + self.y = paddle.randn([10, 10]) + + def non_inplace_api_processing(self, var): + return paddle.addmm(var, x=self.x, y=self.y) + + def inplace_api_processing(self, var): + return paddle.addmm_(var, x=self.x, y=self.y) + + if __name__ == '__main__': unittest.main() From 27f41c506773c22b037b22372eaf22387300d7e9 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Wed, 5 Jul 2023 06:32:08 +0000 Subject: [PATCH 08/22] fix atan2 --- python/paddle/tensor/math.py | 2 +- test/legacy_test/test_atan2_op.py | 14 -------------- test/legacy_test/test_inplace.py | 13 +++++++++++++ 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index c8b4b9d5ef8d3..69e82b7e6e89e 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -4522,7 +4522,7 @@ def atan2_(x, y, name=None): Please refer to :ref:`api_paddle_atan2`. """ if in_dynamic_mode(): - return _C_ops.atan2(x, y) + return _C_ops.atan2_(x, y) def logit(x, eps=None, name=None): diff --git a/test/legacy_test/test_atan2_op.py b/test/legacy_test/test_atan2_op.py index 47f2a931510f9..ddcaa40dee980 100644 --- a/test/legacy_test/test_atan2_op.py +++ b/test/legacy_test/test_atan2_op.py @@ -16,7 +16,6 @@ import numpy as np from eager_op_test import OpTest, convert_float_to_uint16 -from test_inplace import TestDygraphInplaceWithContinuous import paddle from paddle.fluid import core @@ -132,19 +131,6 @@ def run(place): run(place) -class TestDygraphInplaceAtan2(TestDygraphInplaceWithContinuous): - def init_data(self): - self.input_var_numpy = np.random.uniform(-5, 5, [10, 20, 1]) - self.dtype = "float32" - self.y = paddle.randn([10, 20, 1]) - - def non_inplace_api_processing(self, var): - return paddle.attan2(var, self.y) - - def inplace_api_processing(self, var): - return paddle.attan2_(var, self.y) - - @unittest.skipIf( not core.is_compiled_with_cuda() or not core.is_bfloat16_supported(core.CUDAPlace(0)), diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index 4a7bc5002a3e7..123846f54a1ce 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -687,5 +687,18 @@ def inplace_api_processing(self, var): return paddle.addmm_(var, x=self.x, y=self.y) +class TestDygraphInplaceAtan2(TestDygraphInplaceWithContinuous): + def init_data(self): + self.input_var_numpy = np.random.uniform(-5, 5, [10, 20, 1]) + self.dtype = "float32" + self.y = paddle.randn([10, 20, 1]) + + def non_inplace_api_processing(self, var): + return paddle.atan2(var, self.y) + + def inplace_api_processing(self, var): + return paddle.atan2_(var, self.y) + + if __name__ == '__main__': unittest.main() From bfcdf7741b4022f24896870d7c7fda96e082eeb8 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Wed, 5 Jul 2023 07:15:38 +0000 Subject: [PATCH 09/22] remove atan2 --- paddle/phi/api/yaml/ops.yaml | 1 - python/paddle/__init__.py | 2 -- python/paddle/tensor/__init__.py | 1 - python/paddle/tensor/math.py | 10 ---------- test/legacy_test/test_inplace.py | 13 ------------- 5 files changed, 27 deletions(-) diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 10fe1ca5a5aee..3929004bb6b6c 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -215,7 +215,6 @@ func : Atan2InferMeta kernel : func : atan2 - inplace: (x -> out) backward : atan2_grad - op : atanh diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 8c0c6c8375364..332f288d49809 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -211,7 +211,6 @@ from .tensor.math import atan # noqa: F401 from .tensor.math import atan_ # noqa: F401 from .tensor.math import atan2 # noqa: F401 -from .tensor.math import atan2_ # noqa: F401 from .tensor.math import ceil # noqa: F401 from .tensor.math import cos # noqa: F401 from .tensor.math import cos_ # noqa: F401 @@ -616,7 +615,6 @@ 'atan', 'atan_', 'atan2', - 'atan2_', 'rad2deg', 'deg2rad', 'gcd', diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 8ef0e95aa1b47..ccd61d7bb2114 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -217,7 +217,6 @@ from .math import subtract # noqa: F401 from .math import subtract_ # noqa: F401 from .math import atan2 # noqa: F401 -from .math import atan2_ # noqa: F401 from .math import logsumexp # noqa: F401 from .math import logaddexp # noqa: F401 from .math import inverse # noqa: F401 diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 69e82b7e6e89e..6d19f335602f3 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -4515,16 +4515,6 @@ def atan2(x, y, name=None): return out -@inplace_apis_in_dygraph_only -def atan2_(x, y, name=None): - r""" - Inplace version of ``lerp`` API, the output Tensor will be inplaced with input ``x``. - Please refer to :ref:`api_paddle_atan2`. - """ - if in_dynamic_mode(): - return _C_ops.atan2_(x, y) - - def logit(x, eps=None, name=None): r""" This function generates a new tensor with the logit of the elements of input x. x is clamped to [eps, 1-eps] when eps is not zero. When eps is zero and x < 0 or x > 1, the function will yields NaN. diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index 123846f54a1ce..4a7bc5002a3e7 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -687,18 +687,5 @@ def inplace_api_processing(self, var): return paddle.addmm_(var, x=self.x, y=self.y) -class TestDygraphInplaceAtan2(TestDygraphInplaceWithContinuous): - def init_data(self): - self.input_var_numpy = np.random.uniform(-5, 5, [10, 20, 1]) - self.dtype = "float32" - self.y = paddle.randn([10, 20, 1]) - - def non_inplace_api_processing(self, var): - return paddle.atan2(var, self.y) - - def inplace_api_processing(self, var): - return paddle.atan2_(var, self.y) - - if __name__ == '__main__': unittest.main() From d724c6f9780d93eb46304e06ae91e629f216c5c9 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Thu, 6 Jul 2023 02:48:16 +0000 Subject: [PATCH 10/22] auto genereate inpalce api --- python/paddle/tensor/layer_function_generator.py | 2 -- python/paddle/tensor/ops.py | 5 ++++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/python/paddle/tensor/layer_function_generator.py b/python/paddle/tensor/layer_function_generator.py index 05e69e11a7b7b..955e2b13ec548 100644 --- a/python/paddle/tensor/layer_function_generator.py +++ b/python/paddle/tensor/layer_function_generator.py @@ -17,7 +17,6 @@ from io import StringIO from paddle import _C_ops, _legacy_C_ops -from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only from ..common_ops_import import Variable from ..fluid.data_feeder import check_variable_and_dtype @@ -360,7 +359,6 @@ def func(x, name=None): """.format( origin_op_type, origin_op_type ) - func = inplace_apis_in_dygraph_only(func) return func diff --git a/python/paddle/tensor/ops.py b/python/paddle/tensor/ops.py index 3bcb8bdf01ae9..b707a8840283d 100644 --- a/python/paddle/tensor/ops.py +++ b/python/paddle/tensor/ops.py @@ -13,6 +13,8 @@ # limitations under the License. +from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only + from .. import _C_ops from ..fluid.data_feeder import check_variable_and_dtype from ..framework import LayerHelper, in_dynamic_mode @@ -89,7 +91,8 @@ _new_OP = _OP if _OP in __deprecated_func_name__: _new_OP = __deprecated_func_name__[_OP] - _func = generate_inplace_fn(_OP) + func = generate_inplace_fn(_OP) + _func = inplace_apis_in_dygraph_only(func) globals()[_OP] = _func add_sample_code( From 6f330595a42b32137a1c78bf8545296817ec728e Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Thu, 6 Jul 2023 09:39:11 +0000 Subject: [PATCH 11/22] fix inplace generate fn error --- paddle/phi/api/yaml/ops.yaml | 6 ++++-- python/paddle/__init__.py | 6 ++++++ python/paddle/tensor/math.py | 3 +++ python/paddle/tensor/ops.py | 4 ++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 3929004bb6b6c..15fdbce318a98 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -755,11 +755,12 @@ - op : erf args : (Tensor x) - output : Tensor + output : Tensor(out) infer_meta : func : UnchangedInferMeta kernel : func : erf + inplace : (x -> out) backward : erf_grad - op : erfinv @@ -805,12 +806,13 @@ - op : expm1 args : (Tensor x) - output : Tensor + output : Tensor(out) infer_meta : func : UnchangedInferMeta param : [x] kernel : func : expm1 + inplace: (x -> out) backward : expm1_grad - op : fft_c2c diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 332f288d49809..99c1aa35fd671 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -226,6 +226,7 @@ from .tensor.math import logit # noqa: F401 from .tensor.math import exp # noqa: F401 from .tensor.math import expm1 # noqa: F401 +from .tensor.math import expm1_ # noqa: F401 from .tensor.math import floor # noqa: F401 from .tensor.math import increment # noqa: F401 from .tensor.math import log # noqa: F401 @@ -247,6 +248,7 @@ from .tensor.math import sinh_ # noqa: F401 from .tensor.math import sqrt # noqa: F401 from .tensor.math import square # noqa: F401 +from .tensor.math import square_ # noqa: F401 from .tensor.math import stanh # noqa: F401 from .tensor.math import sum # noqa: F401 from .tensor.math import nan_to_num # noqa: F401 @@ -278,6 +280,7 @@ from .tensor.math import inverse # noqa: F401 from .tensor.math import log1p # noqa: F401 from .tensor.math import erf # noqa: F401 +from .tensor.math import erf_ # noqa: F401 from .tensor.math import addmm # noqa: F401 from .tensor.math import addmm_ # noqa: F401 from .tensor.math import clip # noqa: F401 @@ -593,6 +596,7 @@ 'logical_xor', 'exp', 'expm1', + 'expm1_', 'bernoulli', 'poisson', 'sinh', @@ -610,6 +614,7 @@ 'inner', 'outer', 'square', + 'square_', 'divide', 'ceil', 'atan', @@ -639,6 +644,7 @@ 'dot', 'increment', 'erf', + 'erf_', 'bmm', 'chunk', 'tolist', diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 6d19f335602f3..ca570367d545a 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -63,9 +63,11 @@ from .ops import cosh # noqa: F401 from .ops import cosh_ # noqa: F401 from .ops import erf # noqa: F401 +from .ops import erf_ # noqa: F401 from .ops import exp # noqa: F401 from .ops import exp_ # noqa: F401 from .ops import expm1 # noqa: F401 +from .ops import expm1_ # noqa: F401 from .ops import floor # noqa: F401 from .ops import floor_ # noqa: F401 from .ops import reciprocal # noqa: F401 @@ -83,6 +85,7 @@ from .ops import sqrt # noqa: F401 from .ops import sqrt_ # noqa: F401 from .ops import square # noqa: F401 +from .ops import square_ # noqa: F401 from .ops import tan # noqa: F401 from .ops import tan_ # noqa: F401 diff --git a/python/paddle/tensor/ops.py b/python/paddle/tensor/ops.py index b707a8840283d..67045a5c2e787 100644 --- a/python/paddle/tensor/ops.py +++ b/python/paddle/tensor/ops.py @@ -62,6 +62,9 @@ 'tan_', 'atan_', 'atanh_', + 'expm1_', + 'erf_', + 'square_', ] __all__ = [] @@ -92,6 +95,7 @@ if _OP in __deprecated_func_name__: _new_OP = __deprecated_func_name__[_OP] func = generate_inplace_fn(_OP) + func.__module__ = __name__ _func = inplace_apis_in_dygraph_only(func) globals()[_OP] = _func From 508f84cc7600ad5d3b291f387f02fc5e0c378563 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Fri, 7 Jul 2023 07:23:12 +0000 Subject: [PATCH 12/22] fix windows error --- test/legacy_test/test_inplace.py | 26 +++++++++++++++++++++++++- test/legacy_test/test_pow.py | 6 ------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index 4a7bc5002a3e7..9e534aea3d8a0 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -243,6 +243,12 @@ def test_backward_success_2(self): class TestDygraphInplaceWithContinuous(TestDygraphInplace): + def set_np_compare_func(self): + np_array_equal_with_nan = functools.partial( + np.allclose, atol=1e-6, rtol=1e-6, equal_nan=True + ) + self.np_compare = np_array_equal_with_nan + def non_inplace_api_processing(self, var): return paddle.sin(var) @@ -647,7 +653,7 @@ class TestDygraphInplaceATan(TestDygraphInplaceWithContinuous): # atan calculation will appear some nan def set_np_compare_func(self): np_array_equal_with_nan = functools.partial( - np.array_equal, equal_nan=True + np.allclose, atol=1e-6, rtol=1e-6, equal_nan=True ) self.np_compare = np_array_equal_with_nan @@ -686,6 +692,24 @@ def non_inplace_api_processing(self, var): def inplace_api_processing(self, var): return paddle.addmm_(var, x=self.x, y=self.y) + def test_errors(self): + var = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) + x1 = paddle.randn([10]) + self.assertRaises(ValueError, paddle.addmm_, var, x1, self.y) + + y1 = paddle.randn([12, 10]) + self.assertRaises(ValueError, paddle.addmm_, var, self.x, y1) + x2 = paddle.randn([12, 10]) + self.assertRaises(ValueError, paddle.addmm_, var, x2, self.y) + var1 = paddle.randn([1, 5]) + self.assertRaises(ValueError, paddle.addmm_, var1, x2, self.y) + y2 = paddle.randn([10, 12]) + self.assertRaises(ValueError, paddle.addmm_, var, self.x, y2) + var2 = paddle.randn([6]) + self.assertRaises(ValueError, paddle.addmm_, var2, self.x, self.y) + var3 = paddle.randn([2, 3, 4]) + self.assertRaises(ValueError, paddle.addmm_, var3, self.x, self.y) + if __name__ == '__main__': unittest.main() diff --git a/test/legacy_test/test_pow.py b/test/legacy_test/test_pow.py index 5fd81d757e7d3..179193e2514a5 100755 --- a/test/legacy_test/test_pow.py +++ b/test/legacy_test/test_pow.py @@ -215,9 +215,6 @@ def test_errors(self): class TestDygraphInplacePowerScalar(TestDygraphInplaceWithContinuous): - def set_np_compare_func(self): - self.np_compare = np.allclose - def inplace_api_processing(self, var): return paddle.pow_(var, 2) @@ -231,9 +228,6 @@ def init_data(self): self.dtype = "float32" self.y = paddle.ones([10, 20, 1], dtype="float32") * 2 - def set_np_compare_func(self): - self.np_compare = np.allclose - def inplace_api_processing(self, var): return paddle.pow_(var, self.y) From eda4c47e8bd9917e03e1c9d7d3ce89a19181a84f Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Fri, 7 Jul 2023 08:47:33 +0000 Subject: [PATCH 13/22] fix test error --- test/legacy_test/test_inplace.py | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index 9e534aea3d8a0..cabeb1ca962ac 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -612,13 +612,6 @@ def inplace_api_processing(self, var): class TestDygraphInplaceAcos(TestDygraphInplaceWithContinuous): - # acos calculation will appear some nan - def set_np_compare_func(self): - np_array_equal_with_nan = functools.partial( - np.array_equal, equal_nan=True - ) - self.np_compare = np_array_equal_with_nan - def non_inplace_api_processing(self, var): return paddle.acos(var) @@ -627,13 +620,6 @@ def inplace_api_processing(self, var): class TestDygraphInplaceAcosh(TestDygraphInplaceWithContinuous): - # acosh calculation will appear some nan - def set_np_compare_func(self): - np_array_equal_with_nan = functools.partial( - np.array_equal, equal_nan=True - ) - self.np_compare = np_array_equal_with_nan - def non_inplace_api_processing(self, var): return paddle.acosh(var) @@ -650,13 +636,6 @@ def inplace_api_processing(self, var): class TestDygraphInplaceATan(TestDygraphInplaceWithContinuous): - # atan calculation will appear some nan - def set_np_compare_func(self): - np_array_equal_with_nan = functools.partial( - np.allclose, atol=1e-6, rtol=1e-6, equal_nan=True - ) - self.np_compare = np_array_equal_with_nan - def non_inplace_api_processing(self, var): return paddle.atan(var) @@ -665,13 +644,6 @@ def inplace_api_processing(self, var): class TestDygraphInplaceATanh(TestDygraphInplaceWithContinuous): - # atanh calculation will appear some nan - def set_np_compare_func(self): - np_array_equal_with_nan = functools.partial( - np.array_equal, equal_nan=True - ) - self.np_compare = np_array_equal_with_nan - def non_inplace_api_processing(self, var): return paddle.atanh(var) From a8e6545f1d2597fea89c53f3a0f77c18fd4a20f1 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Mon, 10 Jul 2023 02:34:57 +0000 Subject: [PATCH 14/22] fix test error --- test/legacy_test/test_inplace.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index cabeb1ca962ac..ee495a8b86980 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -550,13 +550,6 @@ def test_getitem_before_inplace(self): class TestDygraphInplaceAsin(TestDygraphInplaceWithContinuous): - # asin calculation will appear some nan - def set_np_compare_func(self): - np_array_equal_with_nan = functools.partial( - np.array_equal, equal_nan=True - ) - self.np_compare = np_array_equal_with_nan - def non_inplace_api_processing(self, var): return paddle.asin(var) @@ -573,13 +566,6 @@ def inplace_api_processing(self, var): class TestDygraphInplaceAsinh(TestDygraphInplaceWithContinuous): - # asinh calculation will appear some nan - def set_np_compare_func(self): - np_array_equal_with_nan = functools.partial( - np.array_equal, equal_nan=True - ) - self.np_compare = np_array_equal_with_nan - def non_inplace_api_processing(self, var): return paddle.asinh(var) From fd744864c44dc45ac31d8c0f7b514b6f9160a2d5 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Mon, 10 Jul 2023 06:07:16 +0000 Subject: [PATCH 15/22] fix windows ci error --- test/legacy_test/test_inplace.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index ee495a8b86980..14ca19886c484 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -558,6 +558,12 @@ def inplace_api_processing(self, var): class TestDygraphInplaceSinh(TestDygraphInplaceWithContinuous): + def set_np_compare_func(self): + np_array_equal_with_nan = functools.partial( + np.allclose, atol=1e-5, rtol=1e-5, equal_nan=True + ) + self.np_compare = np_array_equal_with_nan + def non_inplace_api_processing(self, var): return paddle.sinh(var) @@ -590,6 +596,12 @@ def inplace_api_processing(self, var): class TestDygraphInplaceCosh(TestDygraphInplaceWithContinuous): + def set_np_compare_func(self): + np_array_equal_with_nan = functools.partial( + np.allclose, atol=1e-5, rtol=1e-5, equal_nan=True + ) + self.np_compare = np_array_equal_with_nan + def non_inplace_api_processing(self, var): return paddle.cosh(var) @@ -614,6 +626,12 @@ def inplace_api_processing(self, var): class TestDygraphInplaceTan(TestDygraphInplaceWithContinuous): + def set_np_compare_func(self): + np_array_equal_with_nan = functools.partial( + np.allclose, atol=1e-5, rtol=1e-5, equal_nan=True + ) + self.np_compare = np_array_equal_with_nan + def non_inplace_api_processing(self, var): return paddle.tan(var) From 2c17d2dd6c2d0ef9eb5a49324801cd3bf20b4606 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Mon, 10 Jul 2023 07:00:23 +0000 Subject: [PATCH 16/22] fix test error --- test/legacy_test/test_inplace.py | 30 ++++++++---------------------- test/legacy_test/test_pow.py | 4 ++-- 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index 14ca19886c484..4d75d0db29532 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -243,9 +243,13 @@ def test_backward_success_2(self): class TestDygraphInplaceWithContinuous(TestDygraphInplace): + def init_data(self): + self.input_var_numpy = np.random.uniform(-5, 5, [10, 20, 1]) + self.dtype = "float64" + def set_np_compare_func(self): np_array_equal_with_nan = functools.partial( - np.allclose, atol=1e-6, rtol=1e-6, equal_nan=True + np.array_equal, equal_nan=True ) self.np_compare = np_array_equal_with_nan @@ -558,12 +562,6 @@ def inplace_api_processing(self, var): class TestDygraphInplaceSinh(TestDygraphInplaceWithContinuous): - def set_np_compare_func(self): - np_array_equal_with_nan = functools.partial( - np.allclose, atol=1e-5, rtol=1e-5, equal_nan=True - ) - self.np_compare = np_array_equal_with_nan - def non_inplace_api_processing(self, var): return paddle.sinh(var) @@ -596,12 +594,6 @@ def inplace_api_processing(self, var): class TestDygraphInplaceCosh(TestDygraphInplaceWithContinuous): - def set_np_compare_func(self): - np_array_equal_with_nan = functools.partial( - np.allclose, atol=1e-5, rtol=1e-5, equal_nan=True - ) - self.np_compare = np_array_equal_with_nan - def non_inplace_api_processing(self, var): return paddle.cosh(var) @@ -626,12 +618,6 @@ def inplace_api_processing(self, var): class TestDygraphInplaceTan(TestDygraphInplaceWithContinuous): - def set_np_compare_func(self): - np_array_equal_with_nan = functools.partial( - np.allclose, atol=1e-5, rtol=1e-5, equal_nan=True - ) - self.np_compare = np_array_equal_with_nan - def non_inplace_api_processing(self, var): return paddle.tan(var) @@ -658,9 +644,9 @@ def inplace_api_processing(self, var): class TestDygraphInplaceAddMM(TestDygraphInplaceWithContinuous): def init_data(self): self.input_var_numpy = np.random.uniform(-5, 5, [10, 10]) - self.dtype = "float32" - self.x = paddle.randn([10, 10]) - self.y = paddle.randn([10, 10]) + self.dtype = "float64" + self.x = paddle.randn([10, 10], dtype="float64") + self.y = paddle.randn([10, 10], dtype="float64") def non_inplace_api_processing(self, var): return paddle.addmm(var, x=self.x, y=self.y) diff --git a/test/legacy_test/test_pow.py b/test/legacy_test/test_pow.py index 179193e2514a5..66acbe07000f6 100755 --- a/test/legacy_test/test_pow.py +++ b/test/legacy_test/test_pow.py @@ -225,8 +225,8 @@ def non_inplace_api_processing(self, var): class TestDygraphInplacePowerTensor(TestDygraphInplaceWithContinuous): def init_data(self): self.input_var_numpy = np.random.uniform(-5, 5, [10, 20, 1]) - self.dtype = "float32" - self.y = paddle.ones([10, 20, 1], dtype="float32") * 2 + self.dtype = "float64" + self.y = paddle.ones([10, 20, 1], dtype="float64") * 2 def inplace_api_processing(self, var): return paddle.pow_(var, self.y) From 3f900e4c9353b48008452096f28a3a6770ace02c Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Mon, 10 Jul 2023 09:12:49 +0000 Subject: [PATCH 17/22] fix test_error --- test/legacy_test/test_inplace.py | 31 ++++++++++++++++++++++++++++++- test/legacy_test/test_pow.py | 30 ------------------------------ 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index 4d75d0db29532..e42f0c695a363 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -249,7 +249,7 @@ def init_data(self): def set_np_compare_func(self): np_array_equal_with_nan = functools.partial( - np.array_equal, equal_nan=True + np.allclose, atol=1e-15, rtol=1e-15, equal_nan=True ) self.np_compare = np_array_equal_with_nan @@ -673,5 +673,34 @@ def test_errors(self): self.assertRaises(ValueError, paddle.addmm_, var3, self.x, self.y) +class TestDygraphInplacePowerScalar(TestDygraphInplaceWithContinuous): + def inplace_api_processing(self, var): + return paddle.pow_(var, 2) + + def non_inplace_api_processing(self, var): + return paddle.pow(var, 2) + + +class TestDygraphInplacePowerTensor(TestDygraphInplaceWithContinuous): + def init_data(self): + self.input_var_numpy = np.random.uniform(-5, 5, [10, 20, 1]) + self.dtype = "float64" + self.y = paddle.ones([10, 20, 1], dtype="float64") * 2 + + def inplace_api_processing(self, var): + return paddle.pow_(var, self.y) + + def non_inplace_api_processing(self, var): + return paddle.pow(var, self.y) + + def test_type_error(self): + var = paddle.to_tensor(self.input_var_numpy, dtype=self.dtype) + with self.assertRaisesRegex( + TypeError, + 'y must be scalar or tensor type, but received: %s ' % (type([2])), + ): + paddle.pow_(var, [2]) + + if __name__ == '__main__': unittest.main() diff --git a/test/legacy_test/test_pow.py b/test/legacy_test/test_pow.py index 66acbe07000f6..011593b3e874e 100755 --- a/test/legacy_test/test_pow.py +++ b/test/legacy_test/test_pow.py @@ -15,7 +15,6 @@ import unittest import numpy as np -from test_inplace import TestDygraphInplaceWithContinuous import paddle from paddle.fluid import core @@ -214,34 +213,5 @@ def test_errors(self): self.assertRaises(TypeError, paddle.pow, x, str(y)) -class TestDygraphInplacePowerScalar(TestDygraphInplaceWithContinuous): - def inplace_api_processing(self, var): - return paddle.pow_(var, 2) - - def non_inplace_api_processing(self, var): - return paddle.pow(var, 2) - - -class TestDygraphInplacePowerTensor(TestDygraphInplaceWithContinuous): - def init_data(self): - self.input_var_numpy = np.random.uniform(-5, 5, [10, 20, 1]) - self.dtype = "float64" - self.y = paddle.ones([10, 20, 1], dtype="float64") * 2 - - def inplace_api_processing(self, var): - return paddle.pow_(var, self.y) - - def non_inplace_api_processing(self, var): - return paddle.pow(var, self.y) - - def test_type_error(self): - var = paddle.to_tensor(self.input_var_numpy, dtype=self.dtype) - with self.assertRaisesRegex( - TypeError, - 'y must be scalar or tensor type, but received: %s ' % (type([2])), - ): - paddle.pow_(var, [2]) - - if __name__ == '__main__': unittest.main() From a7dd6b26b8d58d5f4959fb362d505215ea40e1c2 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Tue, 11 Jul 2023 09:31:30 +0000 Subject: [PATCH 18/22] fix test error --- test/legacy_test/test_inplace.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index e42f0c695a363..519cf299c9388 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -245,11 +245,11 @@ def test_backward_success_2(self): class TestDygraphInplaceWithContinuous(TestDygraphInplace): def init_data(self): self.input_var_numpy = np.random.uniform(-5, 5, [10, 20, 1]) - self.dtype = "float64" + self.dtype = "float32" def set_np_compare_func(self): np_array_equal_with_nan = functools.partial( - np.allclose, atol=1e-15, rtol=1e-15, equal_nan=True + np.allclose, atol=1e-5, rtol=1e-5, equal_nan=True ) self.np_compare = np_array_equal_with_nan @@ -644,9 +644,9 @@ def inplace_api_processing(self, var): class TestDygraphInplaceAddMM(TestDygraphInplaceWithContinuous): def init_data(self): self.input_var_numpy = np.random.uniform(-5, 5, [10, 10]) - self.dtype = "float64" - self.x = paddle.randn([10, 10], dtype="float64") - self.y = paddle.randn([10, 10], dtype="float64") + self.dtype = "float32" + self.x = paddle.randn([10, 10], dtype="float32") + self.y = paddle.randn([10, 10], dtype="float32") def non_inplace_api_processing(self, var): return paddle.addmm(var, x=self.x, y=self.y) @@ -684,8 +684,8 @@ def non_inplace_api_processing(self, var): class TestDygraphInplacePowerTensor(TestDygraphInplaceWithContinuous): def init_data(self): self.input_var_numpy = np.random.uniform(-5, 5, [10, 20, 1]) - self.dtype = "float64" - self.y = paddle.ones([10, 20, 1], dtype="float64") * 2 + self.dtype = "float32" + self.y = paddle.ones([10, 20, 1], dtype="float32") * 2 def inplace_api_processing(self, var): return paddle.pow_(var, self.y) From 859fcb7fd1b4802274879b9141ccd83e4901c1a3 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Thu, 13 Jul 2023 07:38:31 +0000 Subject: [PATCH 19/22] fix eigen aliasing error in inplace --- paddle/phi/kernels/funcs/activation_functor.h | 28 +++++++++++-------- test/legacy_test/test_inplace.py | 10 ++++++- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/paddle/phi/kernels/funcs/activation_functor.h b/paddle/phi/kernels/funcs/activation_functor.h index 203f6837d4611..ed0bdb2af0010 100644 --- a/paddle/phi/kernels/funcs/activation_functor.h +++ b/paddle/phi/kernels/funcs/activation_functor.h @@ -116,7 +116,10 @@ template struct SinFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = x.unaryExpr(Sine()); + // Note(GGBond8488): Since Eigen3.3, Behavior like {A = (B * A).cwiseAbs()} + // will give wrong result, details see + // http://eigen.tuxfamily.org/dox/group__TopicAliasing.html + out.device(d) = x.unaryExpr(Sine()).eval(); } }; @@ -448,7 +451,7 @@ template struct CosFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = x.unaryExpr(Cosine()); + out.device(d) = x.unaryExpr(Cosine()).eval(); } }; @@ -762,7 +765,10 @@ template struct TanFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = x.unaryExpr(Tangent()); + // Note(GGBond8488): Since Eigen3.3, Behavior like {A = (B * A).cwiseAbs()} + // will give wrong result, details see + // http://eigen.tuxfamily.org/dox/group__TopicAliasing.html + out.device(d) = x.unaryExpr(Tangent()).eval(); } }; @@ -795,7 +801,7 @@ template struct SinhFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = x.unaryExpr(Sinh()); + out.device(d) = x.unaryExpr(Sinh()).eval(); } }; @@ -804,7 +810,7 @@ template struct CoshFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = x.unaryExpr(Cosh()); + out.device(d) = x.unaryExpr(Cosh()).eval(); } }; @@ -855,7 +861,7 @@ template struct AcosFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = x.unaryExpr(Acos()); + out.device(d) = x.unaryExpr(Acos()).eval(); } }; @@ -892,7 +898,7 @@ template struct AsinFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = x.unaryExpr(Asin()); + out.device(d) = x.unaryExpr(Asin()).eval(); } }; @@ -929,7 +935,7 @@ template struct AtanFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = x.unaryExpr(Atan()); + out.device(d) = x.unaryExpr(Atan()).eval(); } }; @@ -977,7 +983,7 @@ template struct AcoshFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = x.unaryExpr(Acosh()); + out.device(d) = x.unaryExpr(Acosh()).eval(); } }; @@ -1014,7 +1020,7 @@ template struct AsinhFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = x.unaryExpr(Asinh()); + out.device(d) = x.unaryExpr(Asinh()).eval(); } }; @@ -1051,7 +1057,7 @@ template struct AtanhFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = x.unaryExpr(Atanh()); + out.device(d) = x.unaryExpr(Atanh()).eval(); } }; diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index 519cf299c9388..a9061fa68b796 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -124,6 +124,14 @@ def test_inplace_api(self): inplace_var[0] = 2.0 np.testing.assert_array_equal(var.numpy(), inplace_var.numpy()) + def test_forward_result(self): + var = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) + no_inplace_var = self.non_inplace_api_processing(var) + inplace_var = self.inplace_api_processing(var) + np.testing.assert_array_equal( + no_inplace_var.numpy(), inplace_var.numpy() + ) + def test_forward_version(self): with paddle.fluid.dygraph.guard(): var = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) @@ -249,7 +257,7 @@ def init_data(self): def set_np_compare_func(self): np_array_equal_with_nan = functools.partial( - np.allclose, atol=1e-5, rtol=1e-5, equal_nan=True + np.array_equal, equal_nan=True ) self.np_compare = np_array_equal_with_nan From 84f48b48a28255670c5ad36aba1a4fdbf63ad7d7 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Thu, 13 Jul 2023 08:52:42 +0000 Subject: [PATCH 20/22] remove elementwise_pow inplace --- test/legacy_test/test_inplace.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index a9061fa68b796..becd6d102b9a8 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -688,19 +688,6 @@ def inplace_api_processing(self, var): def non_inplace_api_processing(self, var): return paddle.pow(var, 2) - -class TestDygraphInplacePowerTensor(TestDygraphInplaceWithContinuous): - def init_data(self): - self.input_var_numpy = np.random.uniform(-5, 5, [10, 20, 1]) - self.dtype = "float32" - self.y = paddle.ones([10, 20, 1], dtype="float32") * 2 - - def inplace_api_processing(self, var): - return paddle.pow_(var, self.y) - - def non_inplace_api_processing(self, var): - return paddle.pow(var, self.y) - def test_type_error(self): var = paddle.to_tensor(self.input_var_numpy, dtype=self.dtype) with self.assertRaisesRegex( From c8c6d8abe973f367525e277336d6787795bb519d Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Mon, 17 Jul 2023 14:47:06 +0000 Subject: [PATCH 21/22] fix doc error --- paddle/phi/api/yaml/legacy_ops.yaml | 1 - python/paddle/tensor/math.py | 8 ++------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index 7d51086456a32..fa30059ba8005 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -247,7 +247,6 @@ func : ElementwiseInferMeta kernel : func : elementwise_pow - inplace: (x -> out) backward : elementwise_pow_grad - op : embedding diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index ca570367d545a..c1d634b11f69b 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -497,12 +497,8 @@ def pow_(x, y, name=None): """ if isinstance(y, (int, float)): return _C_ops.pow_(x, y) - elif isinstance(y, (paddle.Tensor, Variable)): - return _C_ops.elementwise_pow_(x, y) else: - raise TypeError( - 'y must be scalar or tensor type, but received: %s ' % (type(y)) - ) + raise TypeError('y must be scalar type, but received: %s ' % (type(y))) OP_NAMEMAPPING = { @@ -2061,7 +2057,7 @@ def addmm(input, x, y, beta=1.0, alpha=1.0, name=None): @inplace_apis_in_dygraph_only def addmm_(input, x, y, beta=1.0, alpha=1.0, name=None): """ - Inplace version of ``scale`` API, the output Tensor will be inplaced with input ``x``. + Inplace version of ``addmm`` API, the output Tensor will be inplaced with input ``x``. Please refer to :ref:`api_label_addmm`. """ input_shape = input.shape From dc3b35a4c89325313285e966f0b2f64cf7fee629 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Mon, 17 Jul 2023 17:56:39 +0000 Subject: [PATCH 22/22] fix test error --- test/legacy_test/test_inplace.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index becd6d102b9a8..d1dbc00dd55a4 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -692,7 +692,7 @@ def test_type_error(self): var = paddle.to_tensor(self.input_var_numpy, dtype=self.dtype) with self.assertRaisesRegex( TypeError, - 'y must be scalar or tensor type, but received: %s ' % (type([2])), + 'y must be scalar type, but received: %s ' % (type([2])), ): paddle.pow_(var, [2])