Skip to content

Commit 5a1c42c

Browse files
Add hand written gelu derivative (#480)
1 parent b60f051 commit 5a1c42c

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/activations.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,17 @@ end
339339
const gelu_λ = (2 / π)
340340
const gelu_2λ = (8 / π)
341341

342+
function deriv_gelu(x)
343+
α = oftf(x, 0.044715)
344+
α2 = oftf(x, 0.08943)
345+
λλ = oftf(x, gelu_2λ)
346+
x2 = x * x
347+
t = muladd(x2, α, one(x))
348+
Ω = sigmoid_fast(λλ * x * t)
349+
= conj* (1 - Ω))
350+
muladd(dσ * λλ * muladd(x2, α2, t), x, Ω)
351+
end
352+
342353
"""
343354
swish(x) = x * σ(x)
344355
@@ -853,7 +864,7 @@ UNARY_ACTS = [ # f, dfdx
853864
(:relu6, :((Ω>0) &<6))),
854865
# rrelu is random, can't write a rule.
855866
(:elu, :(deriv_elu(Ω))),
856-
# gelu
867+
(:gelu, :(deriv_gelu(x))),
857868
(:swish, :(Ω + sigmoid_fast(x) * (1 - Ω))),
858869
(:hardswish, :(deriv_hardswish(x))),
859870
# lisht

0 commit comments

Comments
 (0)