Skip to content
fn

cumsum_op

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

Python wrappers

Public Python APIs implemented by this engine symbol.

Inclusive cumulative sum along a single axis, yk=∑j≤kxjy_k = \sum_{j \le k} x_j.

The output has the same shape and dtype as the input. Backward uses the reverse-cumsum trick to avoid an explicit O(n2)O(n^2) summation: reverse the upstream gradient along the axis, take its cumsum, then reverse again.

Math

yk=∑j≤kxj,∂L∂xi=∑j≥i∂L∂yj. y_k = \sum_{j \le k} x_j, \qquad \frac{\partial \mathcal{L}}{\partial x_i} = \sum_{j \ge i} \frac{\partial \mathcal{L}}{\partial y_j}.

Shape

Output shape equals a.shape.

Parameters

aTensorImplPtr
Input tensor of any non-scalar shape and real dtype.
axisint
Axis along which to accumulate. May be negative; in that case it is interpreted as axis + ndim.

Returns

TensorImplPtr

Output tensor of the same shape and dtype as a.

Raises

Error
If a is a scalar (ndim == 0) or axis is out of range.

Notes

Dispatch: Accelerate strided cumsum (CPU) / MLX cumsum (GPU). Backward is O(n) per axis-stride.