Skip to content
class

DivBackward

DivBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for element-wise division y=a/by = a / b with NumPy broadcasting.

Saves both forward inputs because the quotient rule for ∂L/∂b\partial L / \partial b requires both aa and bb. Broadcast-reduces each branch back to the original input shape via sum_to_shape inside BinaryKernel::apply.

Math

y=ab,∂L∂a=1b⋅∂L∂y,∂L∂b=−ab2⋅∂L∂y y = \frac{a}{b}, \qquad \frac{\partial L}{\partial a} = \frac{1}{b} \cdot \frac{\partial L}{\partial y}, \qquad \frac{\partial L}{\partial b} = -\frac{a}{b^2} \cdot \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, SubBackward, MulBackward

Attributes

kSavesInputsbool
true (inherited default from BinaryKernel) — both forward inputs are retained for the quotient-rule backward pass.
schema_v1OpSchema
"div", schema version 1, AmpPolicy::Promote, deterministic.

Notes

The CPU quotient is correctly rounded at F32 and F64: vDSP_vdiv for F32, and a plain loop for F64 because vDSP_vdivD is not exact (backend/cpu/Vdsp.cpp). GPU dispatch uses MLX's broadcast-aware divide primitive. Division by zero follows IEEE-754 semantics (yields ±∞\pm\infty or NaN); no explicit guard is inserted.

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 (quotient rule).

Math

dA=1b⊙∂L∂y,dB=−ab2⊙∂L∂y \mathrm{dA} = \frac{1}{b} \odot \frac{\partial L}{\partial y}, \qquad \mathrm{dB} = -\frac{a}{b^2} \odot \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 = grad_out / b_broadcast and dB = -(grad_out * a_broadcast) / b_broadcast^2. Both still at output shape; BinaryKernel::apply performs the broadcast reduction.

Notes

The implementation materialises intermediates (b_sq, g_times_a, div_by_b_sq) explicitly so each transient storage can be released as soon as it is no longer needed.

fn

grad_formula_impl

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

Graph-mode (TensorImpl) variant of the gradient.

Parameters

grad_outconst TensorImplPtr&
Upstream gradient node.
a, bconst TensorImplPtr&
Forward inputs, already broadcast-expanded to out_shape_ by BinaryKernel.

Returns

std::pair<TensorImplPtr, TensorImplPtr>

(grad_out / b, -grad_out * a / b^2).