Skip to content
class

SubBackward

SubBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for element-wise subtraction y=a−by = a - b with NumPy broadcasting.

Inherits forward dispatch, broadcasting logic, and the apply() trampoline from BinaryOp<SubBackward>. No forward inputs are saved because the gradient depends only on the upstream gradient: aa receives it unchanged and bb receives it negated. Broadcast-reduces each branch back to the original input shape via sum_to_shape inside BinaryKernel::apply.

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}

Shape

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

See Also

AddBackward, MulBackward, DivBackward

Attributes

kSavesInputsbool
false — gradient is value-independent.
schema_v1OpSchema
"sub", schema version 1, AmpPolicy::Promote, deterministic.

Notes

CPU dispatch uses Accelerate vDSP_vsub / vDSP_vsubD for F32 / F64. GPU dispatch uses MLX's broadcast-aware subtract primitive.

Static methods

1
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

Output storage holding a−ba - b.

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 at the broadcasted output shape.

Returns

std::pair<Storage, Storage>

(dA, dB) where dA is a clone of grad_out and dB is its element-wise negation.

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.

Parameters

grad_outconst TensorImplPtr&
Upstream gradient node.
a, bconst TensorImplPtr&
Forward inputs (unused — values irrelevant to subtraction gradient).

Returns

std::pair<TensorImplPtr, TensorImplPtr>

(grad_out, -grad_out).