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

Add hand written gelu derivative #480

Merged
merged 6 commits into from
Mar 4, 2023
Merged
Changes from 5 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
11 changes: 10 additions & 1 deletion src/activations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,15 @@ end
const gelu_λ = √(2 / π)
const gelu_2λ = √(8 / π)

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

"""
swish(x) = x * σ(x)

Expand Down Expand Up @@ -853,7 +862,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