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

Bring back math_silu_backward which works for all backends. #49439

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions aten/src/ATen/native/Activation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ Tensor silu_backward(
return grad_input;
}

Tensor math_silu_backward(
const Tensor& grad_output,
const Tensor& input) {
auto input_sigmoid = at::sigmoid(input);
return grad_output * (input_sigmoid * (1 + input * (1 - input_sigmoid)));
}

template <typename scalar_t>
inline void _rrelu_with_noise_train(
Tensor& output,
Expand Down
3 changes: 3 additions & 0 deletions aten/src/ATen/native/native_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3654,6 +3654,9 @@
- func: silu_backward(Tensor grad_output, Tensor self) -> Tensor
use_c10_dispatcher: full
python_module: nn
dispatch:
CPU, CUDA: silu_backward
Math: math_silu_backward

- func: sigmoid(Tensor self) -> Tensor
use_c10_dispatcher: full
Expand Down
8 changes: 8 additions & 0 deletions aten/src/ATen/test/math_kernel_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ TEST(MathKernelTest, Addr) {
}
}
}

TEST(MathKernelTest, SiluBackward) {
const auto input = rand({20, 10});
const auto grad_output = rand({20, 10});
auto out = at::native::silu_backward(grad_output, input);
auto math_out = at::native::math_silu_backward(grad_output, input);
ASSERT_ALLCLOSE_TOLERANCES(out, math_out, 1e-4, 1e-6);
}