Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.bmm

Performs matrix multiplication independently on each corresponding pair of matrices in two 3-D tensor batches. The batches must have equal sizes, and the result has shape (b, n, p) for inputs shaped (b, n, m) and (b, m, p).

How it works

torch.bmm multiplies corresponding matrices in two batches independently. Both inputs must be rank-3 tensors with equal batch sizes; their matrix dimensions must align, and the result preserves the batch count while combining the remaining compatible dimensions.

Syntax

torch.bmm(input, mat2, *, out=None) → Tensor

Parameters

NameTypeDescription
inputTensorFirst batch of matrices to multiply.
mat2TensorSecond batch of matrices to multiply.
outTensor, optionalOptional destination tensor for the result.

Returns

A Tensor containing the batched matrix products, with shape (b, n, p) when the inputs have shapes (b, n, m) and (b, m, p).

Key points

  • It computes one matrix product for every corresponding pair in the two input batches.
  • Both operands must be rank-3 tensors and must have matching batch counts.
  • TensorFloat32 execution is supported.
  • Use torch.matmul when matrix-product broadcasting is needed.

Common mistakes

  • Inputs are paired by batch index; unlike torch.matmul, torch.bmm does not broadcast batch dimensions.
  • On some ROCm hardware, float16 backward computation can use different precision.
  • For CUDA and XPU inputs of float16 or bfloat16, the documented output-dtype support is restricted to float32.

Example: Batched matrix multiplication

import torch

input = torch.randn(10, 3, 4)
mat2 = torch.randn(10, 4, 5)
result = torch.bmm(input, mat2)
print(result.shape)  # torch.Size([10, 3, 5])

Multiplies each corresponding pair of matrices and prints the resulting batch shape.

When to use it

Batched linear algebra

Compute many independent matrix products in one tensor operation.

Per-item transformations

Apply a distinct transformation matrix to each item in a batch.

Batch model operations

Multiply batched feature or embedding matrices with batched weights.

Interview questions

QWhat shapes does torch.bmm accept and produce?

torch.bmm performs a batch matrix-matrix product. For input shaped b, n, m and mat2 shaped b, m, p , it returns b, n, p .

Follow-up: How would you handle differing batch sizes? Use torch.matmul instead when broadcasting matrix products is required.

QWhat input constraints must be satisfied?

Both arguments must be three-dimensional tensors, and each must contain the same number of matrices. The inner matrix dimensions must align: the first matrix's column count equals the second matrix's row count.

Follow-up: What happens if one input is two-dimensional? It does not satisfy torch.bmm's documented 3-D input requirement; reshape or unsqueeze it first if a batch interpretation is intended.

QWhat options and numerical considerations are documented?

The optional out argument specifies the output tensor. The operation supports TensorFloat32, and its supported dtype combinations can depend on the backend.

Follow-up: What backend-specific caveat is documented? CUDA and XPU have a documented restriction for float16 or bfloat16 inputs unless the output is float32.

  • torch.matmul()

Source: torch.bmm — PyTorch main documentation. Checked 2026-10-16.