The exact GELU: x · Φ(x) #
GELU as Hendrycks and Gimpel define it, gelu(x) = x · Φ(x) with Φ the standard normal CDF.
This is the function PyTorch's nn.GELU and jax.nn.gelu(approximate=False) compute;
Proofs.gelu in Activations is its tanh approximation.
gaussPdf,gaussPhi: the standard normal densityφand CDFΦ(x) = ½ + ∫₀ˣ φ, withhasDerivAt_gaussPhi : Φ' = φby the fundamental theorem of calculus.geluErf,geluErfScalarDeriv_eq,geluErfHasVJP: the activation, its derivativeΦ(x) + x · φ(x), and its VJP.erf,erfc,gaussPhi_eq_erf,gaussPhi_eq_erfc: the error function and the two spellings ofΦthrough it,½ (1 + erf(x/√2))and½ erfc(−x·√½).geluErfScalar_eq_erfc,geluErfScalarDeriv_eq_erfc: the forward and the derivative in theerfcspelling, the form in whichjax.nn.gelu(approximate=False)and itsjax.vjpcompute them. Theerfcspelling keeps the negative tail that1 + erfcancels away in floats.
Mathlib has no error function, so erf is defined here as (2/√π) ∫₀ᶻ exp(−t²) dt. The density
and the CDF are closed forms over the interval integral; GeluErfGaussian proves them equal to
Mathlib's gaussianPDFReal 0 1 and to the cdf of gaussianReal 0 1.
References #
- Hendrycks & Gimpel 2016, Gaussian Error Linear Units (GELUs). https://arxiv.org/abs/1606.08415
The standard normal CDF Φ(x) = ½ + ∫₀ˣ φ(t) dt.
The density is even and integrates to one, so the mass below zero is ½
(integral_Iic_zero_gaussPdf); the interval integral carries the rest, and makes Φ' = φ
the fundamental theorem of calculus (hasDerivAt_gaussPhi).
Equations
- Proofs.gaussPhi x = 1 / 2 + ∫ (t : ℝ) in 0..x, Proofs.gaussPdf t
Instances For
Φ' = φ — the fundamental theorem of calculus at the continuous density.
gaussPhi is differentiable. Tagged for fun_prop so smoothness goals over the exact GELU
dispatch.
Exact GELU forward — gelu(x) = x · Φ(x).
Equations
Instances For
The elementwise exact GELU, applied componentwise to a vector.
Equations
- Proofs.geluErf n x i = Proofs.geluErfScalar (x i)
Instances For
Scalar derivative of geluErfScalar — defined as Mathlib's deriv, as
geluScalarDeriv is; its closed form is geluErfScalarDeriv_eq.
Equations
Instances For
The product rule on x · Φ(x).
Closed form of geluErfScalarDeriv — gelu'(x) = Φ(x) + x · φ(x).
Differentiability of geluErfScalar as a scalar function.
Differentiability of geluErf D as a function on Vec D.
Partial derivative of the exact GELU — diagonal, pdiv_elementwise at
geluErfScalar.
Exact GELU VJP: elementwise multiply by the scalar derivative,
back(x, dy)_i = dy_i * geluErfScalarDeriv(x_i).
Equations
- Proofs.geluErfHasVJP n = { backward := fun (x dy : Proofs.Vec n) (i : Fin n) => dy i * Proofs.geluErfScalarDeriv (x i), correct := ⋯ }
Instances For
Public correctness theorem for geluErfHasVJP: the exact GELU backward (diagonal
scaling by geluErfScalarDeriv) equals the pdiv-contracted Jacobian.
The complementary error function erfc(z) = 1 − erf(z).
Equations
- Proofs.erfc z = 1 - Proofs.erf z
Instances For
The exact GELU derivative as computed — with z = −x · √½,
gelu'(x) = 0.5 · erfc(z) + (2/√π) · (0.5 · x) · exp(−z²) · √½,
the two terms jax.vjp of jax.nn.gelu(approximate=False) forms: Φ(x) through erfc, and
x · φ(x) with the density written as the derivative of erfc at z.