Skip to content
class

MulBackward

MulBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for element-wise multiplication y=a⋅by = a \cdot b with NumPy broadcasting.

Saves both forward inputs because the gradient w.r.t. each input depends on the other input's value (product rule). Broadcast-reduces each branch back to the original input shape via sum_to_shape inside BinaryKernel::apply.

Math

y=a⋅b,∂L∂a=b⋅∂L∂y,∂L∂b=a⋅∂L∂y y = a \cdot b, \qquad \frac{\partial L}{\partial a} = b \cdot \frac{\partial L}{\partial y}, \qquad \frac{\partial L}{\partial b} = a \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, DivBackward

Attributes

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

Notes

CPU dispatch uses Accelerate vDSP_vmul / vDSP_vmulD for F32 / F64. GPU dispatch uses MLX's broadcast-aware multiply 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

Output storage holding a⋅ba \cdot 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 (product rule).

Math

dA=b⊙∂L∂y,dB=a⊙∂L∂y \mathrm{dA} = b \odot \frac{\partial L}{\partial y}, \qquad \mathrm{dB} = a \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. Both still at output shape; BinaryKernel::apply performs the broadcast reduction.

Notes

Uses saved_input_broadcasted(k) to expand each saved input to the broadcast output shape before the element-wise multiplication.

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).