Skip to content
class

AddBackward

AddBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for element-wise addition y=a+by = a + b with NumPy broadcasting.

Inherits forward dispatch, broadcasting logic, and the apply() trampoline from BinaryOp<AddBackward> (alias for BinaryKernel<AddBackward>); only the backend hook and the gradient formula are defined locally. No forward inputs are saved because the gradient is a pass-through: the upstream gradient is delivered unchanged to each operand (modulo broadcast reduction).

Math

y=a+b,∂L∂a=∂L∂y,∂L∂b=∂L∂y y = a + b, \qquad \frac{\partial L}{\partial a} = \frac{\partial L}{\partial y}, \qquad \frac{\partial L}{\partial b} = \frac{\partial L}{\partial y}

When aa or bb was broadcast against the other operand, the corresponding branch is reduced back to the original input shape via sum_to_shape inside BinaryKernel::apply.

Shape

Inputs follow NumPy broadcasting; output has the broadcasted shape. Each gradient is then reduced to the original input's shape.

See Also

SubBackward, MulBackward, DivBackward

Attributes

kSavesInputsbool
false — the additive gradient is independent of input values, so no Storage is retained.
schema_v1OpSchema
"add", schema version 1, AmpPolicy::Promote (operands are up-cast to the wider dtype), deterministic.

Notes

CPU dispatch routes to Accelerate vDSP_vadd / vDSP_vaddD for F32 / F64. GPU dispatch uses MLX's broadcast-aware add primitive.

Static methods

2
static

dispatch

→Storage
int dispatch(backend::IBackend & be, const int & a, const int & b, const int & shape, Dtype dt)

Forward dispatch hook called by BinaryKernel::forward.

Parameters

bebackend::IBackend&
Active backend (CPU Accelerate or GPU MLX).
a, bconst Storage&
Operands already broadcast-expanded to shape.
shapeconst Shape&
Broadcasted output shape.
dtDtype
Promoted output dtype.

Returns

Storage

Newly allocated output storage holding a+ba + b.

static

dispatch_scalar

int dispatch_scalar(backend::IBackend & be, const int & a, const int & shape, Dtype dt, double s)

Methods

2
fn

grad_formula

→std::pair<Storage, Storage>
int grad_formula(const int & grad_out)

Compute the storage-level gradients for both operands.

Math

dA=∂L∂y,dB=∂L∂y \mathrm{dA} = \frac{\partial L}{\partial y}, \qquad \mathrm{dB} = \frac{\partial L}{\partial y}

Parameters

grad_outconst Storage&
Upstream gradient ∂L/∂y\partial L / \partial y at the broadcasted output shape.

Returns

std::pair<Storage, Storage>

(dA, dB) where both equal grad_out (still at output shape; BinaryKernel::apply performs the broadcast reduction).

fn

grad_formula_impl

→std::pair<TensorImplPtr, TensorImplPtr>
int grad_formula_impl(const int & grad_out, const int &, const int &)

Graph-mode (TensorImpl) variant of the gradient.

Used when autograd is built as a dataflow graph rather than executed eagerly on storages. Returns the upstream gradient unchanged for both branches — broadcast reduction is handled by the caller.

Parameters

grad_outconst TensorImplPtr&
Upstream gradient node.
a, bconst TensorImplPtr&
Forward inputs (unused for addition; retained for signature uniformity across binary ops).

Returns

std::pair<TensorImplPtr, TensorImplPtr>

(grad_out, grad_out) — identity in graph form.