Skip to content
class

MeanBackward

MeanBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

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

Backward broadcasts the upstream gradient back to the input shape and divides by NN, the product of the reduced dimension sizes.

Math

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

Attributes

schema_v1OpSchema
Registered as "mean" with AmpPolicy::KeepInput.
kSavesInputbool
false. Backward depends only on NN and shape metadata.

Notes

Dispatch: Accelerate kernel (CPU) / MLX mean (GPU). Empty reductions return NaN (0/00/0).

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

Methods

2
fn

grad_formula

int grad_formula(const int & grad_out)

Backward — broadcasts grad_out/N\mathrm{grad\_out} / N back to input shape.

fn

scale_graph_grad

int scale_graph_grad(const int & g)

Graph-mode mean backward: divides the broadcast-expanded gradient by the number of reduced elements so second-order graphs flow through the division correctly.