{"record":{"id":"ad41f3d1ece42b81","repo":"tracel-ai/burn","slug":"shape-should-be-compatible-shape-dim-err","errorCode":null,"errorMessage":"Shape should be compatible shape={dim:?}: {err:?}","messagePattern":"Shape should be compatible shape=(.+?): (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-ndarray/src/tensor.rs","lineNumber":561,"sourceCode":"macro_rules! reshape {\n    (\n        ty $ty:ty,\n        n $n:expr,\n        shape $shape:expr,\n        array $array:expr\n    ) => {{\n        let dim = $crate::to_typed_dims!($n, $shape, justdim);\n        let array = match $array.is_standard_layout() {\n            // Move the array into the new shape rather than going through\n            // `to_shape`: the latter returns a borrowed view here, which\n            // `into_shared` then clones, copying the buffer on every reshape.\n            // Moving rewrites the dimensions in place, and the buffer stays\n            // shared for copy-on-write like in any other operation.\n            true => {\n                match $array.into_shape_with_order(dim) {\n                    Ok(val) => val,\n                    Err(err) => {\n                        core::panic!(\"Shape should be compatible shape={dim:?}: {err:?}\");\n                    }\n                }\n            },\n            false => $array.to_shape(dim).unwrap().as_standard_layout().into_shared(),\n        };\n        array.into_dyn()\n    }};\n    (\n        ty $ty:ty,\n        shape $shape:expr,\n        array $array:expr,\n        d $D:expr\n    ) => {{\n        match $D {\n            1 => reshape!(ty $ty, n 1, shape $shape, array $array),\n            2 => reshape!(ty $ty, n 2, shape $shape, array $array),\n            3 => reshape!(ty $ty, n 3, shape $shape, array $array),\n            4 => reshape!(ty $ty, n 4, shape $shape, array $array),","sourceCodeStart":543,"sourceCodeEnd":579,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-ndarray/src/tensor.rs#L543-L579","documentation":"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.","triggerScenarios":"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]).","commonSituations":"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.","solutions":["Fix the target shape so the product of dimensions equals the tensor's element count (tensor.shape() to confirm)","Compute shapes programmatically from tensor.dims() instead of hard-coding","Use flatten/squeeze/expand APIs appropriate for the intended transformation"],"exampleFix":"// before\nlet x = x.reshape([4, 2]); // x is [2, 3] (6 elems) -> panic\n// after\nlet x = x.reshape([2, 3]); // 2*3 == 6","handlingStrategy":"validation","validationCode":"let cur: usize = tensor.shape().iter().product();\nlet new: usize = new_shape.iter().product();\nassert_eq!(cur, new, \"reshape changes element count\");","typeGuard":"fn reshape_ok(shape: &[usize], new_shape: &[usize]) -> bool {\n    shape.iter().product::<usize>() == new_shape.iter().product::<usize>()\n}","tryCatchPattern":null,"preventionTips":["Derive reshape dims from tensor.dims() instead of constants","Assert element counts in debug builds around reshape calls","Update all downstream reshapes when changing input sizes"],"tags":["rust","burn-ndarray","shape","reshape"],"backgroundTag":"shape-mismatch","analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}