Skip to content

Commit

Permalink
Add hand written gelu derivative (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
chengchingwen authored Mar 4, 2023
1 parent b60f051 commit 5a1c42c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/activations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,17 @@ end
const gelu_λ = (2 / π)
const gelu_2λ = (8 / π)

function deriv_gelu(x)
α = oftf(x, 0.044715)
α2 = oftf(x, 0.08943)
λλ = oftf(x, gelu_2λ)
x2 = x * x
t = muladd(x2, α, one(x))
Ω = sigmoid_fast(λλ * x * t)
= conj* (1 - Ω))
muladd(dσ * λλ * muladd(x2, α2, t), x, Ω)
end

"""
swish(x) = x * σ(x)
Expand Down Expand Up @@ -853,7 +864,7 @@ UNARY_ACTS = [ # f, dfdx
(:relu6, :((Ω>0) &<6))),
# rrelu is random, can't write a rule.
(:elu, :(deriv_elu(Ω))),
# gelu
(:gelu, :(deriv_gelu(x))),
(:swish, :(Ω + sigmoid_fast(x) * (1 - Ω))),
(:hardswish, :(deriv_hardswish(x))),
# lisht
Expand Down

0 comments on commit 5a1c42c

Please sign in to comment.