Skip to content
class

ProdBackward

ProdBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for the multi-axis reduction product y=∏i∈axesxiy = \prod_{i \in \text{axes}} x_i.

The gradient is the "product of all other elements" of xix_i's slice. It is y/xiy / x_i only where xi≠0x_i \ne 0, so it is formed from the saved input with the slice's zeros split out rather than divided by: a zero gets the product of the rest (0 when the slice has another zero).

Because no single IBackend overload spans every Accelerate / MLX combination, prod provides explicit cpu_kernel and gpu_kernel overloads instead of a shared dispatch.

Math

y=∏i∈axesxi,∂L∂xi=(∏j≠ixj) broadcast ⁣(∂L∂y). y = \prod_{i \in \text{axes}} x_i, \qquad \frac{\partial \mathcal{L}}{\partial x_i} = \Bigl(\prod_{j \ne i} x_j\Bigr)\, \mathrm{broadcast}\!\left(\frac{\partial \mathcal{L}}{\partial y} \right).

Attributes

schema_v1OpSchema
Registered as "prod" with AmpPolicy::KeepInput.
kSavesInputbool
true.
kSavesOutputbool
false.

Notes

CPU path iterates axes in descending order so the lower indices remain valid as dimensions are successively collapsed. GPU path delegates to mlx::core::prod with keepdims. Empty reductions return 11 (multiplicative identity).

Static methods

2
static

cpu_kernel

CpuStorage cpu_kernel(const CpuStorage & a, const int & in_shape, const int & axes, bool keepdims, Dtype dt)

CPU path: iterates axes in descending order (innermost last) to produce a correct sequential multi-axis product using Accelerate primitives.

static

gpu_kernel

GpuStorage gpu_kernel(const GpuStorage & a, const int & in_shape, const int & axes, bool keepdims, Dtype dt)

GPU path: delegates to mlx::core::prod with keepdims.

Methods

2
fn

grad_formula

int grad_formula(const int & grad_out)

Backward — ∂L/∂xi=grad_out⋅∏j≠ixj\partial L/\partial x_i = \mathrm{grad\_out} \cdot \prod_{j \ne i} x_j, formed from the saved input.

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.