Multiply batches of matrices with a single np.einsum call

Quick Overview

Multiply a stack of matrices pairwise with a single Einstein summation call in NumPy, returning one product per batch entry. It tests reading einsum subscripts, which axes are summed and which are kept, the shape rules, and how einsum speed compares with the matrix multiply operator.

Multiply batches of matrices with a single np.einsum call

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `batched_matmul(A, B)` for `A` of shape `(batch, m, k)` and `B` of shape `(batch, k, n)`. Return shape `(batch, m, n)` where `result[i] == A[i] @ B[i]`. Use exactly one `np.einsum` call with subscripts `'bij,bjk->bik'`. Do not loop over the batch dimension or call `@`/`np.matmul`. ```python A = np.arange(2*2*3).reshape(2, 2, 3) # (batch=2, m=2, k=3) B = np.arange(2*3*4).reshape(2, 3, 4) # (batch=2, k=3, n=4) out = batched_matmul(A, B) out.shape # (2, 2, 4) np.allclose(out, A @ B) # True ``` ```hint Read the subscripts axis by axis Write down which axis of each input every letter names, and which letters are missing from the output. The letters do not line up one to one with the shape names `m`, `k`, `n`. ``` ### Constraints & Assumptions - `A` is 3-D with shape `(batch, m, k)` and `B` is 3-D with shape `(batch, k, n)`. - Both inputs have the same batch size and the same inner dimension `k`. ### What a Strong Answer Covers - Maps each subscript letter to an axis of each input, including that `j` is the size-`k` axis and `k` is the size-`n` axis. - Explains which index is summed, which are kept, and why that equals a per-batch matrix product. - Gives the time and memory cost. - Says how the input shapes must line up and what happens when they do not. ### Follow-up Questions - How does `np.einsum` performance compare to `A @ B` here? - How would you extend this to broadcast a shared `B` across all batches? - How would you support any number of leading batch axes?

Overview: Multiply a stack of matrices pairwise with a single Einstein summation call in NumPy, returning one product per batch entry. It tests reading einsum subscripts, which axes are summed and which are kept, the shape rules, and how einsum speed compares with the matrix multiply operator.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement batched_matmul(A, B) for A of shape (batch, m, k) and B of shape (batch, k, n). Return shape (batch, m, n) where result[i] == A[i] @ B[i].

Use exactly one np.einsum call with subscripts 'bij,bjk->bik'. Do not loop over the batch dimension or call @/np.matmul.

A = np.arange(2*2*3).reshape(2, 2, 3)   # (batch=2, m=2, k=3)
B = np.arange(2*3*4).reshape(2, 3, 4)   # (batch=2, k=3, n=4)
out = batched_matmul(A, B)
out.shape        # (2, 2, 4)
np.allclose(out, A @ B)   # True

Constraints & Assumptions

  • A is 3-D with shape (batch, m, k) and B is 3-D with shape (batch, k, n) .
  • Both inputs have the same batch size and the same inner dimension k .

What a Strong Answer Covers Guidance

  • Maps each subscript letter to an axis of each input, including that j is the size- k axis and k is the size- n axis.
  • Explains which index is summed, which are kept, and why that equals a per-batch matrix product.
  • Gives the time and memory cost.
  • Says how the input shapes must line up and what happens when they do not.

Follow-up Questions Guidance

  • How does np.einsum performance compare to A @ B here?
  • How would you extend this to broadcast a shared B across all batches?
  • How would you support any number of leading batch axes?