Skip to content
class

SoftmaxBackward

SoftmaxBackward

Python wrappers

Public Python APIs implemented by this engine symbols.

Autograd node for the numerically stable softmax along a single axis pi=exi−m/∑jexj−mp_i = e^{x_i - m} / \sum_j e^{x_j - m} with m=max⁡jxjm = \max_j x_j.

Subtracting the per-row maximum mm before exponentiating is the standard log-sum-exp trick — it preserves the analytic result while keeping every exponentiated term in (0,1](0, 1], which is essential under ForceFP32 AMP semantics for fp16/bf16 logits. The softmax probabilities pp are saved on the node so the backward needs no re-evaluation.

Math

\qquad \frac{\partial L}{\partial x_i} = p_i\,\Bigl(\frac{\partial L}{\partial y_i} - \sum_j p_j\,\frac{\partial L}{\partial y_j}\Bigr)$$ The Jacobian-vector form sidesteps materialising the dense $N \times N$ softmax Jacobian and runs in $O(N)$ per row. **Shape** Input and output share the same shape; the normalisation contracts only along `axis_`. Negative axis values are normalised to non-negative indices by `forward` before being stored. **References** Bishop, "Pattern Recognition and Machine Learning", §4.3.4.

Attributes

schema_v1OpSchema
"softmax", AmpPolicy::ForceFP32 (probability underflow on float16 is unsafe for large logit magnitudes).
axis_int
Canonical (non-negative) axis along which the softmax is computed.
saved_output_Storage
Softmax probabilities pp produced by forward, used by apply.

Raises

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

Static methods

1
static

forward

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

Validate the axis, dispatch the forward softmax through the backend, save the resulting probabilities on the node, and wire the autograd edges back to a.

Parameters

aTensorImplPtr
Input logits.
axisint
Softmax axis; negative values are normalised to axis + ndim.

Returns

TensorImplPtr

Output probabilities pp with the same shape and dtype as a.

Raises

IndexError
If axis is out of range after normalisation.

Methods

2
fn

apply

int apply(int grad_out)

Eager-mode backward: dispatches to IBackend::softmax_backward which evaluates p⊙(∂L/∂y−⟨p,∂L/∂y⟩)p \odot (\partial L/\partial y - \langle p, \partial L/\partial y\rangle) in a single fused pass along axis_.

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.