From 4b7aeba4bca3da1174e608813de01a5d0ff2ae1b Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Fri, 13 Jan 2023 20:24:09 +0800 Subject: [PATCH] [Phi] heaviside add yaml (#49807) --- paddle/phi/api/yaml/legacy_backward.yaml | 10 ++++++++++ paddle/phi/api/yaml/legacy_ops.yaml | 9 +++++++++ .../tests/unittests/test_elementwise_heaviside_op.py | 9 +++++---- python/paddle/tensor/math.py | 8 ++------ 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index 2050653904..9810242037 100755 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -411,6 +411,16 @@ kernel : func : einsum_grad +- backward_op : elementwise_heaviside_grad + forward : heaviside (Tensor x, Tensor y) -> Tensor(out) + args : (Tensor x, Tensor y, Tensor out_grad, int axis = -1) + output : Tensor(x_grad), Tensor(y_grad) + infer_meta : + func : GeneralBinaryGradInferMeta + param : [x, y] + kernel : + func : elementwise_heaviside_grad + - backward_op : elementwise_pow_grad forward : elementwise_pow(Tensor x, Tensor y) -> Tensor(out) args : (Tensor x, Tensor y, Tensor out_grad, int axis=-1) diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index b448dadbd5..3be9cdf371 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -564,6 +564,15 @@ func : einsum backward : einsum_grad +- op : elementwise_heaviside + args : (Tensor x, Tensor y) + output : Tensor + infer_meta : + func : ElementwiseInferMeta + kernel : + func : elementwise_heaviside + backward : elementwise_heaviside_grad + - op : elementwise_pow args : (Tensor x, Tensor y) output : Tensor(out) diff --git a/python/paddle/fluid/tests/unittests/test_elementwise_heaviside_op.py b/python/paddle/fluid/tests/unittests/test_elementwise_heaviside_op.py index d917a6bc33..4e80ef9187 100644 --- a/python/paddle/fluid/tests/unittests/test_elementwise_heaviside_op.py +++ b/python/paddle/fluid/tests/unittests/test_elementwise_heaviside_op.py @@ -32,20 +32,21 @@ class TestElementwiseOp(OpTest): self.op_type = "elementwise_heaviside" x = np.random.random((13, 17)).astype("float64") y = np.random.random((13, 17)).astype("float64") + self.python_api = paddle.heaviside self.inputs = {'X': x, 'Y': y} self.outputs = {'Out': np.heaviside(self.inputs['X'], self.inputs['Y'])} def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def test_check_grad_normal(self): - self.check_grad(['X', 'Y'], 'Out') + self.check_grad(['X', 'Y'], 'Out', check_eager=True) def test_check_grad_ingore_x(self): - self.check_grad(['Y'], 'Out', no_grad_set=set("X")) + self.check_grad(['Y'], 'Out', no_grad_set=set("X"), check_eager=True) def test_check_grad_ingore_y(self): - self.check_grad(['X'], 'Out', no_grad_set=set('Y')) + self.check_grad(['X'], 'Out', no_grad_set=set('Y'), check_eager=True) class TestHeavisideBroadcast(unittest.TestCase): diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 32978bc30d..81b092f4c3 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -4857,14 +4857,10 @@ def heaviside(x, y, name=None): # [[0. , 0.20000000, 1. ], # [0. , 1. , 0.30000001]] """ - op_type = 'elementwise_heaviside' - axis = -1 - act = None if in_dygraph_mode(): - return _elementwise_op_in_dygraph( - x, y, axis=axis, act=act, op_name=op_type - ) + return _C_ops.elementwise_heaviside(x, y) else: + op_type = 'elementwise_heaviside' return _elementwise_op(LayerHelper(op_type, **locals())) -- GitLab