Skip to content
class

GeluBackward

GeluBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for the tanh-approximation GeLU y=0.5 x (1+tanh⁡(2/π (x+0.044715 x3)))y = 0.5\,x\,(1 + \tanh(\sqrt{2/\pi}\,(x + 0.044715\,x^3))).

The approximate form (Hendrycks & Gimpel 2016, eq. 3) is used by default when callers do not request the exact erf-based variant; it is the form implemented by F.gelu(x, approximate="tanh") in the Python wrapper. The backward is delegated to the backend because the analytic derivative involves both tanh and sech^2 over a cubic polynomial of xx, and fusing them in a single kernel is materially faster than chaining storage primitives.

Math

y=x2 (1+tanh⁡(2/π (x+0.044715 x3)))y = \tfrac{x}{2}\,\bigl(1 + \tanh\bigl(\sqrt{2/\pi}\,(x + 0.044715\,x^3)\bigr)\bigr)

References

Hendrycks & Gimpel, "Gaussian Error Linear Units (GELUs)", arXiv:1606.08415.

Attributes

schema_v1OpSchema
"gelu", AmpPolicy::ForceFP32 — the cubic + tanh chain is not numerically safe in float16.

Static methods

1
static

dispatch

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

Forward — calls IBackend::gelu to compute the tanh-approximation GeLU y=0.5 x (1+tanh⁡(2/π (x+0.044715 x3)))y = 0.5\,x\,(1 + \tanh(\sqrt{2/\pi}\,(x + 0.044715\,x^3))).

Methods

2
fn

grad_formula

int grad_formula(const int & g)

Eager-mode backward: dispatches to IBackend::gelu_backward with the saved input and the upstream gradient.

fn

grad_formula_impl

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

Graph-mode backward: the analytic derivative of the tanh approximation, composed from ops so it can be differentiated again.