Skip to content
fn

dot_op

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

Python wrappers

Public Python APIs implemented by this engine symbol.

Compute the dot product of two vectors or the matrix product of two matrices, with autograd support for both cases.

Dispatches on rank:

  • 1-D × 1-D — inner product c=∑iaibic = \sum_i a_i b_i producing a scalar (shape {}). Backed by Accelerate's cblas_*dot via the element-wise multiply and reduce_sum backend primitives. Backward node: Dot1DBackward (saves both operands).
    • 2-D × 2-D — matrix product C=ABC = A B. Backed by cblas_*gemm (Accelerate) on CPU, mlx::matmul on GPU. Backward node: Dot2DBackward (saves both operands).

Math

1-D case:

c=∑i=0N−1aibi,∂L∂ai=∂L∂c bi,∂L∂bi=∂L∂c ai c = \sum_{i=0}^{N-1} a_i b_i, \qquad \frac{\partial L}{\partial a_i} = \frac{\partial L}{\partial c}\, b_i, \qquad \frac{\partial L}{\partial b_i} = \frac{\partial L}{\partial c}\, a_i

2-D case:

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}

Shape

  • 1-D: a.shape[0] == b.shape[0]; output is rank-0.
  • 2-D: a.shape[1] == b.shape[0]; output is (M,N)(M, N).

References

NumPy numpy.dot semantics for the 1-D and 2-D cases.

See Also

matmul_op : N-D matmul with NumPy broadcasting on batch dims. inner_op : Last-axis contraction (NumPy inner). outer_op : 1-D × 1-D outer product.

Parameters

aTensorImplPtr
First operand. Must be 1-D or 2-D.
bTensorImplPtr
Second operand. Must have the same rank as a.

Returns

TensorImplPtr
  • 1-D inputs → scalar tensor with shape {}. - 2-D inputs of shape (M,K)(M, K) and (K,N)(K, N) → matrix of shape (M,N)(M, N).

Raises

ShapeMismatch
If the contracted dimensions disagree (1-D length mismatch, or 2-D a.shape[1] != b.shape[0]).
NotImplemented
If either input has rank other than 1 or 2.

Notes

Unlike matmul_op this entry point uses AmpPolicy::KeepInput — integer dot products are valid and must not be promoted silently.