Batched linear algebra
Compute many independent matrix products in one tensor operation.
PyTorch · Function
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).
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.
torch.bmm(input, mat2, *, out=None) → Tensor| Name | Type | Description |
|---|---|---|
input | Tensor | First batch of matrices to multiply. |
mat2 | Tensor | Second batch of matrices to multiply. |
out | Tensor, optional | Optional destination tensor for the result. |
A Tensor containing the batched matrix products, with shape (b, n, p) when the inputs have shapes (b, n, m) and (b, m, p).
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.
Compute many independent matrix products in one tensor operation.
Apply a distinct transformation matrix to each item in a batch.
Multiply batched feature or embedding matrices with batched weights.
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.
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.
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()