Skip to content
class

TanhBackward

TanhBackward

Python wrappers

Public Python APIs implemented by this engine symbols.

Autograd node for element-wise hyperbolic tangent y=tanh⁡(x)y = \tanh(x).

Saves the output yy rather than the input because the backward formula 1−y21 - y^2 is cheaper than re-evaluating tanh⁡(x)\tanh(x) and avoids a second vvtanhf pass. kSavesInput = false opts out of the default input-save behaviour and kSavesOutput = true registers the output instead. Range is (−1,1)(-1, 1) on all of R\mathbb{R}.

Math

\frac{\partial y}{\partial x} = 1 - \tanh^2(x) = 1 - y^2, \qquad \frac{\partial \mathcal{L}}{\partial x} = (1 - y^2)\,\frac{\partial \mathcal{L}}{\partial y}.$$

Attributes

schema_v1OpSchema
Registered as "tanh" with AmpPolicy::Promote.
kSavesInputbool
false.
kSavesOutputbool
true — the saved tensor is y=tanh⁡(x)y = \tanh(x).

Notes

Dispatch: Accelerate vvtanhf/vvtanh (CPU) / MLX tanh (GPU).

Static methods

1
static

dispatch

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

Forward — calls IBackend::tanh to compute y=tanh⁡xy = \tanh x.

Methods

2
fn

grad_formula

int grad_formula(const int & g)

Backward — ∂y/∂x=1−y2\partial y/\partial x = 1 - y^2 using the saved output, scaled by grad_out.

fn

grad_formula_impl

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

Graph-mode backward: ∂x=(1−out2)⋅g\partial x = (1 - \mathrm{out}^2) \cdot g where out=tanh⁡x\mathrm{out} = \tanh x; kept inside the autograd graph.