Skip to content
class

LogSoftmaxBackward

LogSoftmaxBackward

Python wrappers

Public Python APIs implemented by this engine symbols.

Autograd node for the numerically stable log-softmax along a single axis y=x−logsumexp(x,axis)y = x - \mathrm{logsumexp}(x, \text{axis}).

Computing log⁡softmax\log\mathrm{softmax} directly (rather than log⁡\log of softmax\mathrm{softmax}) is the standard numerical-stability win when the downstream loss is NLL / cross-entropy: it avoids the catastrophic cancellation that arises when pip_i underflows to zero before the log step. Forward saves the log-softmax output on the node, which the backward reuses to recover the probabilities as exp⁡(y)\exp(y).

Math

\qquad \frac{\partial L}{\partial x_i} = \frac{\partial L}{\partial y_i} - e^{y_i}\,\sum_j \frac{\partial L}{\partial y_j}$$ Here $e^{y_i} = p_i$ is the underlying softmax probability. **Shape** Input and output share the same shape; normalisation contracts along `axis_` only.

Attributes

schema_v1OpSchema
"log_softmax", AmpPolicy::ForceFP32.
axis_int
Canonical (non-negative) axis stored after normalisation by forward.
saved_output_Storage
Log-softmax output yy saved for backward.

Raises

IndexError
If the normalised axis falls outside [0, ndim).

Notes

LogSoftmax is preferred over log(softmax(x)) because it remains finite when probabilities underflow: the subtraction is performed in the log domain via logsumexp rather than producing zero and then taking the log.

Static methods

1
static

forward

→TensorImplPtr
int forward(const int & a, int axis)

Validate the axis, dispatch the forward log-softmax, save its output, and wire autograd back to a.

Parameters

aTensorImplPtr
Input logits.
axisint
Axis along which to normalise; negative values are wrapped.

Returns

TensorImplPtr

Log-softmax output yy with the same shape and dtype as a.

Methods

2
fn

apply

int apply(int grad_out)

Eager-mode backward: dispatches to IBackend::log_softmax_backward which computes ∂L/∂x=∂L/∂y−ey ∑axis∂L/∂y\partial L/\partial x = \partial L/\partial y - e^{y}\,\sum_{\text{axis}}\partial L/\partial y.

fn

apply_for_graph

int apply_for_graph(const int & grad_out)

Graph-mode backward. Recomputes the forward from the saved input rather than reusing the saved output Storage: the adjoint depends on that output, so a data-only stand-in would silently drop the whole d(out)/d(in) path from the second derivative.