tracel-ai/burn · error
Dim not supported {ndims}
Error message
Dim not supported {ndims} What it means
mean_dim reduces a tensor along a dimension and keeps dims; the ndarray backend's keepdim! macro only supports tensors with 1 to 6 dimensions. Calling mean_dim on a tensor with 0 or more than 6 dimensions panics with 'Dim not supported'.
Source
Thrown at crates/burn-ndarray/src/ops/base.rs:1017
/// Mean of all elements - zero-copy for borrowed storage.
pub fn mean_view(view: ArrayView<'_, E, IxDyn>) -> SharedArray<E> {
// `ndarray::mean` returns `None` for an empty view.
let mean = view.mean().unwrap_or_else(empty_mean);
ArrayD::from_elem(IxDyn(&[1]), mean).into_shared()
}
/// Product of all elements - zero-copy for borrowed storage.
pub fn prod_view(view: ArrayView<'_, E, IxDyn>) -> SharedArray<E> {
let prod = view.iter().fold(E::one(), |acc, &x| acc * x);
ArrayD::from_elem(IxDyn(&[1]), prod).into_shared()
}
pub fn mean_dim(tensor: SharedArray<E>, dim: usize) -> SharedArray<E> {
let ndims = tensor.shape().num_dims();
match ndims {
d if (1..=6).contains(&d) => keepdim!(dim, tensor, mean),
_ => panic!("Dim not supported {ndims}"),
}
}
pub fn sum_dim(tensor: SharedArray<E>, dim: usize) -> SharedArray<E> {
let ndims = tensor.shape().num_dims();
match ndims {
d if (1..=6).contains(&d) => keepdim!(dim, tensor, sum),
_ => panic!("Dim not supported {ndims}"),
}
}
pub fn prod_dim(tensor: SharedArray<E>, dim: usize) -> SharedArray<E> {
let ndims = tensor.shape().num_dims();
match ndims {
d if (1..=6).contains(&d) => keepdim!(dim, tensor, prod),
_ => panic!("Dim not supported {ndims}"),
}
}View on GitHub (pinned to d16f7ba2ed)
Solutions
- Reduce the number of tensor dimensions to 6 or fewer by reshaping or merging axes.
- Use the burn-tch or burn-cubecl backends if higher-rank support is needed.
- Ensure you haven't accidentally added extra dimensions when constructing the tensor.
- Check tensor.dims().len() before calling mean_dim.
Example fix
// before let t: Tensor<_,_,NdArray> = ...; // shape [2,2,2,2,2,2,2] (7 dims) let m = t.mean_dim(0); // after let t = t.reshape([2, 4, 2, 2, 2, 2]); // merge two axes -> 6 dims let m = t.mean_dim(0);
Defensive patterns
Strategy: validation
Validate before calling
assert!((1..=6).contains(&tensor.dims().len()), "mean_dim supports rank 1-6");
Type guard
fn mean_dim_supported(t: &[usize]) -> bool { (1..=6).contains(&t.len()) } Prevention
- Keep tensors at 6 or fewer dims in ndarray backend
- Merge axes with reshape before reductions
- Check rank after any op that may add dimensions
When it happens
Trigger: Calling Tensor::mean_dim (or mean along dim) on a rank-7+ tensor or a scalar (rank 0).
Common situations: Very deep nested inputs (e.g. video + batch + channels + extra axes) exceeding 6 dims; accidentally passing nested Vec structures that produce extra dimensions; scalar tensors from squeezing all dims.
Related errors
- Data should have the same element type as the tensor {err:?}
- todo!("grid_sample_2d with {:?} mode is not implemented", op
- todo!("rfft is not supported for ndarray")
- todo!("irfft is not supported for ndarray")
- capture tensor operations must run inside CaptureDevice::cap
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/ed1e6a4a9669b32c.
Report an issue: GitHub.