Skip to content
fn

cumprod_op

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

Python wrappers

Public Python APIs implemented by this engine symbol.

Inclusive cumulative product along a single axis, yk=∏j≤kxjy_k = \prod_{j \le k} x_j.

The output has the same shape and dtype as the input. Backward reuses the reverse-cumsum primitive against the saved cumprod output and divides by the saved input, dx=reverse(cumsum(reverse(dy⋅y)))/xdx = \mathrm{reverse}(\mathrm{cumsum}(\mathrm{reverse}(dy \cdot y))) / x, before the first zero of xx along axis.

Math

yk=∏j≤kxj,∂L∂xi=∑j≥i∂L∂yj⋅∏k≤j, k≠ixk. y_k = \prod_{j \le k} x_j, \qquad \frac{\partial \mathcal{L}}{\partial x_i} = \sum_{j \ge i} \frac{\partial \mathcal{L}}{\partial y_j} \cdot \prod_{k \le j,\, k \ne i} x_k.

Parameters

aTensorImplPtr
Input tensor of any non-scalar shape and real dtype.
axisint
Axis along which to accumulate (negative wraps).

Returns

TensorImplPtr

Output tensor of the same shape and dtype as a.

Notes

At the first zero xzx_z the gradient is ∏k<zxk∑j≥zdyj∏z<k≤jxk\prod_{k<z} x_k \sum_{j \ge z} dy_j \prod_{z<k \le j} x_k, and after it 00, as the reference has them. Forward saves both xx and yy on the backward node.