{"record":{"id":"9d3e9bfc1fb400e5","repo":"tracel-ai/burn","slug":"data-should-have-as-many-elements-as-the-shape","errorCode":null,"errorMessage":"Data should have as many elements as the shape","messagePattern":"Data should have as many elements as the shape","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-ndarray/src/tensor.rs","lineNumber":683,"sourceCode":"            dtype,\n        })\n    }\n\n    /// Create a tensor with owned storage.\n    ///\n    /// This may or may not copy data depending on whether the underlying bytes\n    /// can be reclaimed (via `try_into_vec`). If bytes are uniquely owned,\n    /// no copy occurs; otherwise data is copied to a new allocation.\n    fn from_data_owned(data: TensorData) -> NdArrayTensor {\n        let shape = data.shape.to_vec(); // TODO: into_vec\n\n        macro_rules! execute {\n            ($data: expr, [$($dtype: pat => $ty: ty),*]) => {\n                match $data.dtype {\n                    $( $dtype => {\n                        match data.try_into_vec::<$ty>() {\n                            Ok(vec) => ArrayD::from_shape_vec(shape, vec)\n                                .expect(\"Data should have as many elements as the shape\")\n                                .into_shared(),\n                            Err(err) => panic!(\"Data should have the same element type as the tensor {err:?}\"),\n                        }.into()\n                    }, )*\n                    other => unimplemented!(\"Unsupported dtype {other:?}\"),\n                }\n            };\n        }\n\n        execute!(data, [\n            DType::F64 => f64, DType::F32 => f32,\n            DType::I64 => i64, DType::I32 => i32, DType::I16 => i16, DType::I8 => i8,\n            DType::U64 => u64, DType::U32 => u32, DType::U16 => u16, DType::U8 => u8,\n            DType::Bool(BoolStore::Native) => bool\n        ])\n    }\n}\n","sourceCodeStart":665,"sourceCodeEnd":701,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-ndarray/src/tensor.rs#L665-L701","documentation":"Shape/length invariant check in `NdArrayTensor::from_data_owned`: the flattened element count of `TensorData` does not match the product of the declared shape, so ndarray cannot view the buffer with that shape and conversion panics.","triggerScenarios":"Thrown at crates/burn-ndarray/src/tensor.rs:683 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Verify `TensorData.shape` matches `data.len()` (or vec length) before conversion","Recompute the shape from the data instead of hardcoding it","Check for off-by-one or truncated reads when the data was produced"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}