Skip to content
class

GeluExactBackward

GeluExactBackward

Autograd node for the exact (Gaussian-CDF) GeLU y=0.5 x (1+erf(x/2))y = 0.5\,x\,(1 + \mathrm{erf}(x/\sqrt{2})).

Routed to by F.gelu(x, approximate="none"). Implementing this as a dedicated op (instead of the prior ten-op Python composition built around _erf_approx) lets the GPU backend dispatch to a single MPSGraph node and keeps the autograd graph compact for memory accounting.

Math

\qquad \frac{\partial y}{\partial x} = \tfrac{1}{2}\,\Bigl(1 + \mathrm{erf}\bigl(\tfrac{x}{\sqrt{2}}\bigr)\Bigr) + \tfrac{x}{\sqrt{2\pi}}\,e^{-x^2/2}$$

Attributes

schema_v1OpSchema
"gelu_exact", AmpPolicy::ForceFP32 — matches the tanh-approx variant for consistent AMP semantics across approximations.

Static methods

1
static

dispatch

int dispatch(backend::IBackend & be, const int & a, const int & s, Dtype dt)

Forward — calls IBackend::gelu_exact to compute the erf-based exact GeLU y=0.5 x (1+erf(x/2))y = 0.5\,x\,(1 + \mathrm{erf}(x/\sqrt{2})).

Methods

2
fn

grad_formula

int grad_formula(const int & g)

Eager-mode backward: dispatches to IBackend::gelu_exact_backward.

fn

grad_formula_impl

int grad_formula_impl(const int & g, const int & x, const int &)

Graph-mode backward: \Phi(x) + x\,\phi(x) built from erf and exp, both of which carry their own graph-mode formulas.