tracel-ai/burn · error
Tensors are not broadcastable along dimension {}
Error message
Tensors are not broadcastable along dimension {} What it means
Broadcast-check panic in the ndarray backend's batched cross product: along every dimension other than the batch dim (dim == 3's axis), the two operand shapes must be equal or one must be 1; otherwise they cannot be broadcast for the batched matmul and this panic fires. The failing input is a pair of tensors with incompatible non-batch dimensions.
Source
Thrown at crates/burn-ndarray/src/ops/matmul.rs:211
let shape_rhs = rhs.shape();
let ndims = shape_lhs.num_dims();
// Broadcast the shapes except along dim
let mut broadcast_shape = vec![0; ndims];
for i in 0..ndims {
if i == dim {
broadcast_shape[i] = shape_lhs[i]; // already checked to be 3
} else {
let l = shape_lhs[i];
let r = shape_rhs[i];
if l == r {
broadcast_shape[i] = l;
} else if l == 1 {
broadcast_shape[i] = r;
} else if r == 1 {
broadcast_shape[i] = l;
} else {
panic!("Tensors are not broadcastable along dimension {}", i);
}
}
}
// Broadcast lhs and rhs
let lhs_broadcast = if shape_lhs == broadcast_shape.as_slice() {
lhs
} else {
NdArrayOps::expand(lhs, Shape::from(broadcast_shape.clone()))
};
let rhs_broadcast = if shape_rhs == broadcast_shape.as_slice() {
rhs
} else {
NdArrayOps::expand(rhs, Shape::from(broadcast_shape.clone()))
};
// Now, move dim to the last dimension
let mut perm = (0..ndims).collect::<Vec<_>>();View on GitHub (pinned to d16f7ba2ed)
Solutions
- Make both tensors the same rank and shape before cross, e.g. unsqueeze the single vector to [1,3] so it broadcasts.
- Reshape/expand one tensor so all leading dims match or equal 1.
- Verify both inputs come from the same batched source.
Example fix
// before: a [3], b [4,3] let c = a.cross(b, -1); // panic along dim 0 // after let a2 = a.unsqueeze().expand([4, 3]); // or a.unsqueeze::<2>() broadcast let c = a2.cross(b, -1);
Defensive patterns
Strategy: validation
Validate before calling
fn ensure_cross_shapes(a: &[usize], b: &[usize]) {
assert_eq!(a.len(), b.len(), "cross requires same rank");
for (l, r) in a.iter().zip(b) {
assert!(l == r || *l == 1 || *r == 1, "cross dims {l} vs {r} not broadcastable");
}
} Prevention
- Unsqueeze single vectors before cross with batched vectors.
- Keep cross-product inputs from the same batched source.
- Assert shapes equal before geometric ops.
When it happens
Trigger: Calling tensor.cross(other, dim) with tensors whose shapes differ in a leading dimension where both sizes are >1, e.g. [2,3] x [4,3].
Common situations: Cross products of vectors from different batch sizes; mixing single vector [3] with batched vectors [N,3] without reshaping.
Related errors
- Dimensions differ and cannot be broadcasted.
- broadcast_shape: incompatible dimensions {} and {} at positi
- Matrix multiplication requires an array with at least 2 dime
- Dimensions are incompatible for matrix multiplication: LHS c
- Optional argument type mismatch
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/1e8a84a1ab5dee38.
Report an issue: GitHub.