High severity beginner · Fix: 2-5 min

RuntimeError

torch._C._RuntimeError

What this error means
PyTorch throws RuntimeError when attempting matrix multiplication with tensors whose inner dimensions do not align for multiplication.

Stack trace

traceback
RuntimeError: mat1 and mat2 shapes cannot be multiplied (e.g., mat1: [2,3], mat2: [4,5])
QUICK FIX
Check and align tensor shapes so mat1.shape[-1] == mat2.shape[-2] before calling torch.matmul or torch.mm.

Why it happens

This error occurs because PyTorch requires the inner dimension of the first matrix (mat1) to match the outer dimension of the second matrix (mat2) for matrix multiplication. If these dimensions differ, the operation is mathematically invalid and triggers this runtime error.

Detection

Check tensor shapes before matrix multiplication using tensor.shape or tensor.size() and assert that mat1.shape[-1] == mat2.shape[-2] to catch mismatches early.

Causes & fixes

1

Input tensors have incompatible shapes for matrix multiplication (e.g., mat1 shape [2,3], mat2 shape [4,5])

✓ Fix

Ensure the last dimension of mat1 matches the second-to-last dimension of mat2 before multiplication, e.g., reshape or transpose tensors accordingly.

2

Incorrect use of torch.matmul or torch.mm with tensors that are not 2D or have mismatched batch dimensions

✓ Fix

Use torch.matmul for batched matrix multiplication and verify batch dimensions align, or reshape tensors to 2D if using torch.mm.

3

Confusing element-wise multiplication (torch.mul or *) with matrix multiplication

✓ Fix

Use torch.matmul or the @ operator for matrix multiplication instead of element-wise operators.

Code: broken vs fixed

Broken - triggers the error
python
import torch

mat1 = torch.randn(2, 3)
mat2 = torch.randn(4, 5)
result = torch.matmul(mat1, mat2)  # RuntimeError here
print(result)
Fixed - works correctly
python
import torch

mat1 = torch.randn(2, 3)
mat2 = torch.randn(3, 5)  # Fixed shape to match mat1's last dim
result = torch.matmul(mat1, mat2)  # Works now
print(result)
Adjusted mat2's shape so its first dimension matches mat1's last dimension, enabling valid matrix multiplication.

Workaround

If you cannot fix shapes immediately, reshape or transpose tensors dynamically before multiplication using tensor.view() or tensor.transpose() to align dimensions.

Prevention

Design tensor operations with explicit shape checks and use assertions or helper functions to validate dimensions before matrix multiplication calls.

Python 3.7+ · torch >=1.0.0 · tested on 2.0.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.