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

  1. Reduce the number of tensor dimensions to 6 or fewer by reshaping or merging axes.
  2. Use the burn-tch or burn-cubecl backends if higher-rank support is needed.
  3. Ensure you haven't accidentally added extra dimensions when constructing the tensor.
  4. 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

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


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/ed1e6a4a9669b32c. Report an issue: GitHub.