Skip to content

Commit

Permalink
optimize gradient for silu a bit (#2393)
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkFather authored Aug 4, 2024
1 parent aa7ac18 commit c0a559d
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions candle-core/src/backprop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,9 @@ impl Tensor {
}
Op::Unary(arg, UnaryOp::Silu) => {
let sum_grad = grads.or_insert(arg)?;
// d/dx silu = sigmoid(x) * (1 + x * (1 - sigmoid(x)))
// d/dx silu = sigmoid(x) * (1 + x * (1 - sigmoid(x))) = sigmoid(x) * (1 - node) + node
let sigmoid_arg = (arg.neg()?.exp()? + 1.)?.recip()?;
let silu_grad = (&sigmoid_arg * (1. + (arg * (1. - &sigmoid_arg)?)?)?)?;
let silu_grad = &sigmoid_arg * (1. - *node) + *node;
*sum_grad = sum_grad.add(&(&grad * silu_grad)?)?
}
Op::Elu(arg, alpha) => {
Expand Down

0 comments on commit c0a559d

Please sign in to comment.