tracel-ai/burn · error
broadcast_shape: incompatible dimensions {} and {} at positi
Error message
broadcast_shape: incompatible dimensions {} and {} at position {} What it means
broadcast_shape computes the output shape for a broadcasting binary op. When two non-1 dimensions at the same position differ, broadcasting is impossible and the function panics with both dim sizes and the position.
Source
Thrown at crates/burn-flex/src/ops/expand.rs:38
let lhs_dim = if lhs_idx >= 0 {
lhs[lhs_idx as usize]
} else {
1
};
let rhs_dim = if rhs_idx >= 0 {
rhs[rhs_idx as usize]
} else {
1
};
if lhs_dim == rhs_dim {
*out = lhs_dim;
} else if lhs_dim == 1 {
*out = rhs_dim;
} else if rhs_dim == 1 {
*out = lhs_dim;
} else {
panic!(
"broadcast_shape: incompatible dimensions {} and {} at position {}",
lhs_dim, rhs_dim, i
);
}
}
Shape::from(result)
}
/// Broadcast two tensors to the same shape for binary operations.
pub fn broadcast_binary(lhs: FlexTensor, rhs: FlexTensor) -> (FlexTensor, FlexTensor) {
let lhs_shape = lhs.layout().shape().clone();
let rhs_shape = rhs.layout().shape().clone();
if lhs_shape == rhs_shape {
return (lhs, rhs);
}
View on GitHub (pinned to d16f7ba2ed)
Solutions
- Check shapes before the op: for each aligned (from the right) position, dims must be equal or one must be 1
- Reshape/expand one tensor to a compatible shape before the binary op
- Fix upstream shape computations so the operands are produced with compatible shapes
Example fix
// before let c = broadcast_binary(a /* [4,3] */, b /* [8,3] */); // after let b2 = b.reshape([4, 3]); // or fix upstream so shapes align let c = broadcast_binary(a, b2);
Defensive patterns
Strategy: validation
Validate before calling
fn broadcastable(l: &[usize], r: &[usize]) -> bool {
let n = l.len().max(r.len());
(0..n).all(|i| {
let a = if i < l.len() { l[l.len() - 1 - i] } else { 1 };
let b = if i < r.len() { r[r.len() - 1 - i] } else { 1 };
a == b || a == 1 || b == 1
})
}
assert!(broadcastable(&a.shape().dims, &b.shape().dims), "shapes {:?} and {:?} are not broadcastable", a.shape().dims, b.shape().dims); Prevention
- Log both operand shapes before every broadcasting binary op in debug builds
- Reshape/expand explicitly instead of relying on implicit broadcasting for tricky ranks
- Right-align shapes mentally when mixing ranks (e.g. [B] with [B,N])
- Centralize shape assertions in a helper used by all elementwise ops
When it happens
Trigger: Calling any broadcasting binary op (through broadcast_binary) with two tensors whose shapes have mismatched non-1 dimensions at some position, e.g. [2,3] vs [4,3] or [8] vs [6].
Common situations: Matrix-with-vector shape mistakes (batch dim mismatch), off-by-one rank handling, mixing tensors from differently-shaped intermediate results, forgetting to reshape/expand before an elementwise op.
Related errors
- Broadcast arguments must be greater than the number of dimen
- Broadcast arguments must be positive or -1! Got {}
- Cannot substitute -1 for a non-existing dimension! Got {:?}
- The shapes should be broadcastable
- Failed to broadcast lhs
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/77027a852a18bbe7.
Report an issue: GitHub.