Skip to content
class

SumBackward

SumBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for the multi-axis reduction sum y=∑i∈axesxiy = \sum_{i \in \text{axes}} x_i.

The gradient broadcasts unchanged from the reduced shape back to the input shape because ∂y/∂xi=1\partial y / \partial x_i = 1 for every element participating in the sum. kSavesInput = false and no output is saved — the rule is independent of both the forward input and output.

Math

y=∑i∈axesxi,∂L∂xi=broadcast ⁣(∂L∂y). y = \sum_{i \in \text{axes}} x_i, \qquad \frac{\partial \mathcal{L}}{\partial x_i} = \mathrm{broadcast}\!\left(\frac{\partial \mathcal{L}}{\partial y} \right).

Attributes

schema_v1OpSchema
Registered as "sum" with AmpPolicy::KeepInput — integer and boolean inputs are promoted to int64 before reduction so the sum cannot overflow narrow integer dtypes (parity fix found during the M4 Max training sanity check, where bool.sum() previously dropped to any() semantics on GPU).
kSavesInputbool
false. Backward depends only on shape metadata.

Notes

Dispatch: Accelerate strided reductions (CPU) / MLX sum (GPU). For empty reductions the output is 00 (additive identity).

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_sum.

Methods

2
fn

grad_formula

int grad_formula(const int & grad_out)

Backward — broadcasts grad_out back to the input shape (ones gradient).

fn

scale_graph_grad

int scale_graph_grad(const int & g)

Graph-mode scaling helper — sum requires no scaling, so the broadcast-expanded gradient is returned unchanged.