tracel-ai/burn · error

Shape should be compatible shape={dim:?}: {err:?}

Error message

Shape should be compatible shape={dim:?}: {err:?}

What it means

Reshape in burn-ndarray uses into_shape_with_order when no data copy is required; if the target shape's element count or layout is incompatible with the array, it panics including the shape and the underlying ndarray error.

Source

Thrown at crates/burn-ndarray/src/tensor.rs:561

macro_rules! reshape {
    (
        ty $ty:ty,
        n $n:expr,
        shape $shape:expr,
        array $array:expr
    ) => {{
        let dim = $crate::to_typed_dims!($n, $shape, justdim);
        let array = match $array.is_standard_layout() {
            // Move the array into the new shape rather than going through
            // `to_shape`: the latter returns a borrowed view here, which
            // `into_shared` then clones, copying the buffer on every reshape.
            // Moving rewrites the dimensions in place, and the buffer stays
            // shared for copy-on-write like in any other operation.
            true => {
                match $array.into_shape_with_order(dim) {
                    Ok(val) => val,
                    Err(err) => {
                        core::panic!("Shape should be compatible shape={dim:?}: {err:?}");
                    }
                }
            },
            false => $array.to_shape(dim).unwrap().as_standard_layout().into_shared(),
        };
        array.into_dyn()
    }};
    (
        ty $ty:ty,
        shape $shape:expr,
        array $array:expr,
        d $D:expr
    ) => {{
        match $D {
            1 => reshape!(ty $ty, n 1, shape $shape, array $array),
            2 => reshape!(ty $ty, n 2, shape $shape, array $array),
            3 => reshape!(ty $ty, n 3, shape $shape, array $array),
            4 => reshape!(ty $ty, n 4, shape $shape, array $array),

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Fix the target shape so the product of dimensions equals the tensor's element count (tensor.shape() to confirm)
  2. Compute shapes programmatically from tensor.dims() instead of hard-coding
  3. Use flatten/squeeze/expand APIs appropriate for the intended transformation

Example fix

// before
let x = x.reshape([4, 2]); // x is [2, 3] (6 elems) -> panic
// after
let x = x.reshape([2, 3]); // 2*3 == 6
Defensive patterns

Strategy: validation

Validate before calling

let cur: usize = tensor.shape().iter().product();
let new: usize = new_shape.iter().product();
assert_eq!(cur, new, "reshape changes element count");

Type guard

fn reshape_ok(shape: &[usize], new_shape: &[usize]) -> bool {
    shape.iter().product::<usize>() == new_shape.iter().product::<usize>()
}

Prevention

When it happens

Trigger: Calling tensor.reshape(shape) or tensor.flatten / view-like ops where the new shape's total element count differs from the current one (e.g. reshaping [2,3] into [4,2]).

Common situations: Hard-coded shape constants that no longer match the model's actual feature sizes; batch dimension mismatches; typos in reshape dimensions; changing an input image size without updating downstream reshape layers.

Related errors


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