Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement log_grad by primitive logic #51296

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,14 @@ void expand_grad(const Tensor& x,
}
}

template <typename T>
void log_grad(const Tensor& x, const Tensor& out_grad, Tensor* x_grad) {
if (x_grad) {
// dx = dout / x
set_output<T>(out_grad / x, x_grad);
}
}

template <typename T>
void exp_grad(const Tensor& out, const Tensor& out_grad, Tensor* x_grad) {
if (x_grad) {
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/api/yaml/backward.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@
kernel :
func : log_grad
backward : log_double_grad
composite : log_grad(x, out_grad, x_grad)
inplace : (out_grad -> x_grad)

- backward_op : log_loss_grad
Expand Down
9 changes: 7 additions & 2 deletions python/paddle/fluid/tests/unittests/test_activation_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -2611,10 +2611,15 @@ class TestLog(TestActivation):
def setUp(self):
self.op_type = "log"
self.check_eager = True
self.prim_op_type = "prim"
self.python_api = paddle.log
self.init_dtype()
self.init_shape()

if len(self.shape) == 0:
# for 0-D tensor, skip cinn testing
self.enable_cinn = False

np.random.seed(1024)
x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
out = np.log(x)
Expand All @@ -2625,7 +2630,7 @@ def setUp(self):
def test_check_grad(self):
if self.dtype == np.float16:
return
self.check_grad(['X'], 'Out', check_eager=True)
self.check_grad(['X'], 'Out', check_eager=True, check_prim=True)

def test_error(self):
in1 = paddle.static.data(name="in1", shape=[11, 17], dtype="int32")
Expand Down Expand Up @@ -3834,7 +3839,7 @@ def test_check_grad(self):
create_test_act_fp16_class(TestELU)
create_test_act_fp16_class(TestCELU)
create_test_act_fp16_class(TestReciprocal)
create_test_act_fp16_class(TestLog)
create_test_act_fp16_class(TestLog, check_prim=True)
if core.is_compiled_with_rocm():
create_test_act_fp16_class(TestLog2, atol=5e-2, grad_atol=0.85)
else:
Expand Down