Skip to content
class

MatmulBackward

MatmulBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for the generalised matrix multiplication (matmul) with NumPy broadcasting on leading batch dimensions.

Saves both inputs. Forward dispatches to cblas_*gemm (Accelerate) for plain 2-D inputs, the batched-GEMM kernel for any leading-batch shape, and MLX's matmul primitive on the GPU stream. Backward computes the two factor gradients via two further matmuls, then reduce_grad_to_shapes each back to the original (pre-broadcast) batch shape.

Math

2-D case (A∈RM×KA \in \mathbb{R}^{M \times K}, B∈RK×NB \in \mathbb{R}^{K \times N}):

C=AB,∂L∂A=∂L∂C B⊤,∂L∂B=A⊤ ∂L∂C C = A B, \qquad \frac{\partial L}{\partial A} = \frac{\partial L}{\partial C}\, B^\top, \qquad \frac{\partial L}{\partial B} = A^\top\, \frac{\partial L}{\partial C}

Batched case (A,BA,B rank ≥2\ge 2 with broadcastable leading dims):

C[…,i,j]=∑kA[…,i,k] B[…,k,j] C[\ldots, i, j] = \sum_k A[\ldots, i, k]\, B[\ldots, k, j]

Shape

  • Both inputs must be at least 2-D; plan_nd_matmul rejects rank < 2.
  • Trailing two dims drive the GEMM: a.shape[-1] == b.shape[-2] == K.
  • Leading dims broadcast NumPy-style; output shape is broadcast(a.shape[:-2], b.shape[:-2]) + (M, N).

References

NumPy numpy.matmul semantics (which Lucid mirrors).

See Also

dot_op : 1-D × 1-D / 2-D × 2-D specialised dispatch without broadcasting. inner_op : Last-axis contraction (NumPy inner). outer_op : 1-D × 1-D outer product.

Attributes

schema_v1OpSchema
("matmul", v1, AmpPolicy::Promote, deterministic=true).

Notes

MatmulBackward is a FuncOp<..., 2> (alias for AutogradNode<..., 2>) rather than a BinaryOp because matmul requires a custom forward that cannot fit into the generic BinaryKernel dispatch path: it calls plan_nd_matmul, handles batch broadcasting, and wires the autograd graph manually.

Under AutocastGuard(F16) the schema's AmpPolicy::Promote resolves to the autocast target (F32 on CPU per the Accelerate-no-F16 carve-out; F16 on GPU where MLX runs matmul natively at half throughput). The AMP cast is wired through autograd-aware astype_op so the backward chain stays intact.

Static methods

1
static

forward

→TensorImplPtr
int forward(const int & a, const int & b)

Validate inputs, plan the batched matrix multiply, execute the forward kernel, and wire the autograd graph if gradient tracking is active.

Parameters

aTensorImplPtr
Left operand, rank ≥2\ge 2. Trailing two dims are (M,K)(M, K).
bTensorImplPtr
Right operand, rank ≥2\ge 2. Trailing two dims are (K,N)(K, N).

Returns

TensorImplPtr

Output tensor with shape broadcast(a.shape[:-2], b.shape[:-2]) + (M, N).

Raises

ShapeMismatch
If either input has rank < 2 or the leading batch dims fail NumPy broadcasting.
DeviceMismatch
If a and b live on different devices.
DtypeMismatch
If the AMP-promoted dtypes of a and b still disagree.

Methods

3
fn

apply

→std::vector<Storage>
int apply(int grad_out)

Eager-mode backward: compute ∂L/∂A\partial L/\partial A and ∂L/∂B\partial L/\partial B from the upstream gradient and reduce any batch dims that were broadcast-expanded during forward.

Parameters

grad_outStorage
Upstream gradient ∂L/∂C\partial L/\partial C with the output's broadcasted batch shape and trailing (M,N)(M, N).

Returns

std::vector<Storage>

{dA, dB} reduced to the original input shapes.

fn

apply_for_graph

→std::vector<TensorImplPtr>
int apply_for_graph(const int & grad_out)

Graph-mode backward: build a symbolic backward subgraph ∂L/∂A=∂L/∂C⋅B⊤\partial L/\partial A = \partial L/\partial C \cdot B^\top and ∂L/∂B=A⊤⋅∂L/∂C\partial L/\partial B = A^\top \cdot \partial L/\partial C using matmul_op, mT_op, and sum_op.

Parameters

grad_outTensorImplPtr
Symbolic upstream gradient.

Returns

std::vector<TensorImplPtr>

{dA, dB} as graph tensors reduced to the original input shapes.

fnconst

node_name

int node_name()

Human-readable node label used by the autograd profiler.