From b7e4d974e99f90039b8a4644581a19d5c77ab792 Mon Sep 17 00:00:00 2001 From: GGBond8488 <33050871+GGBond8488@users.noreply.github.com> Date: Thu, 9 Mar 2023 19:55:57 +0800 Subject: [PATCH] add prim erf grad (#50436) * add prim erf grad * add yaml config for prim erf grad * add math.h * add cmath * add math defines * use define math * use define math * define M_2_SQRTPI * M_2_SQRTPI math * try math.h * fix typro * remove pow in erf grad * use new optest * add fp16 fp32 test * remove fp16 test --- .../composite_backward/composite_backward_api.h | 17 +++++++++++++++++ paddle/phi/api/yaml/backward.yaml | 1 + .../paddle/fluid/tests/unittests/test_erf_op.py | 4 +++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/prim/api/composite_backward/composite_backward_api.h b/paddle/fluid/prim/api/composite_backward/composite_backward_api.h index 823a068dcd7..2fe33c92ff8 100644 --- a/paddle/fluid/prim/api/composite_backward/composite_backward_api.h +++ b/paddle/fluid/prim/api/composite_backward/composite_backward_api.h @@ -14,6 +14,12 @@ #pragma once +#ifndef _USE_MATH_DEFINES +#define _USE_MATH_DEFINES +#endif + +#include + #include "paddle/fluid/prim/api/all.h" #include "paddle/fluid/prim/api/generated_prim/prim_generated_api.h" #include "paddle/phi/common/int_array.h" @@ -881,5 +887,16 @@ void gather_nd_grad(const Tensor& x, } } +template +void erf_grad(const Tensor& x, const Tensor& out_grad, Tensor* x_grad) { + if (x_grad) { + auto m_2_sqrt_pi = full(phi::vectorize(x.dims()), M_2_SQRTPI, x.dtype()); + auto neg_one = full(phi::vectorize(x.dims()), -1.0, x.dtype()); + auto neg_tmp = neg_one * x * x; + auto mul_tmp = m_2_sqrt_pi * exp(neg_tmp); + set_output(out_grad * mul_tmp, x_grad); + } +} + } // namespace prim } // namespace paddle diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index 4a42799764f..6108fbb5081 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -423,6 +423,7 @@ kernel : func : erf_grad data_type : out_grad + composite : erf_grad(x, out_grad, x_grad) - backward_op : erfinv_grad forward : erfinv (Tensor x) -> Tensor(out) diff --git a/python/paddle/fluid/tests/unittests/test_erf_op.py b/python/paddle/fluid/tests/unittests/test_erf_op.py index db5c48151c5..0d78457b145 100644 --- a/python/paddle/fluid/tests/unittests/test_erf_op.py +++ b/python/paddle/fluid/tests/unittests/test_erf_op.py @@ -26,6 +26,8 @@ import paddle.fluid.dygraph as dg class TestErfOp(OpTest): def setUp(self): self.op_type = "erf" + self.prim_op_type = "prim" + self.enable_cinn = True self.python_api = paddle.erf self.dtype = self._init_dtype() self.x_shape = [11, 17] @@ -41,7 +43,7 @@ class TestErfOp(OpTest): self.check_output() def test_check_grad(self): - self.check_grad(['X'], 'Out') + self.check_grad(['X'], 'Out', check_prim=True) class TestErfLayer(unittest.TestCase): -- GitLab