Skip to content
class

MaxBackward

MaxBackward

Autograd node for the multi-axis reduction maximum y=max⁡i∈axesxiy = \max_{i \in \text{axes}} x_i.

The gradient routes only to positions where xix_i equals the reduced maximum. The equality mask is built without a dedicated equality kernel by composing two ge masks (a=b  ⟺  a≥b∧b≥aa = b \iff a \ge b \wedge b \ge a). Ties (multiple equal maxima) receive equal gradient — matching the reference framework's behaviour. The forward output is saved so the mask can be formed without rerunning the reduction.

Math

y=max⁡i∈axesxi,∂L∂xi=1[xi=y]#{j:xj=y}⋅broadcast ⁣(∂L∂y). y = \max_{i \in \text{axes}} x_i, \qquad \frac{\partial \mathcal{L}}{\partial x_i} = \frac{\mathbb{1}[x_i = y]}{\#\{j : x_j = y\}}\cdot \mathrm{broadcast}\!\left(\frac{\partial \mathcal{L}}{\partial y} \right).

Attributes

schema_v1OpSchema
Registered as "max" with AmpPolicy::KeepInput — integer dtypes pass through unchanged.
kSavesOutputbool
true.

Notes

Empty reductions propagate NaN / raise (backend-dependent); callers should guard against zero-sized reduction axes.

Static methods

1
static

dispatch

int dispatch(backend::IBackend & be, const int & a, const int & in_shape, const int & axes, bool keepdims, Dtype dt)

Forward — reduces a along axes via IBackend::reduce_max.

Methods

2
fn

grad_formula

int grad_formula(const int & grad_out)

Backward — argmax-indicator gradient: routes grad_out only to positions where xix_i equals the reduced max (ties share equally).

fn

scale_graph_grad

int scale_graph_grad(const int & g)

Graph-mode scaling. Without this the base class returns the broadcast gradient unchanged — the sum rule — so create_graph=True silently produced the incoming seed at every position while eager backward was correct.