{"record":{"id":"ca44fbfb376e7e1e","repo":"tracel-ai/burn","slug":"concatenate-data-type-mismatch-expected-got","errorCode":null,"errorMessage":"Concatenate data type mismatch (expected {:?}, got {:?})","messagePattern":"Concatenate data type mismatch \\(expected (.+?), got (.+?)\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-ndarray/src/tensor.rs","lineNumber":309,"sourceCode":"///\n/// Uses zero-copy views from storage for concatenation.\n///\n/// # Panics\n/// Since there is no automatic type cast at this time, binary operations for different\n/// floating point precision data types will panic with a data type mismatch.\n#[macro_export]\nmacro_rules! cat_with_dtype {\n    ($tensors: expr, $dim: expr, [$($dtype: ident),*]) => {\n        match &$tensors[0] {\n            $(NdArrayTensor::$dtype(_) => {\n                let tensors = $tensors\n                    .iter()\n                    .map(|t| {\n                        if let NdArrayTensor::$dtype(storage) = t {\n                            // Use storage.view() for zero-copy access\n                            storage.view()\n                        } else {\n                            panic!(\"Concatenate data type mismatch (expected {:?}, got {:?})\", $tensors[0].dtype(), t.dtype())\n                        }\n                    })\n                    .collect::<Vec<_>>();\n                NdArrayOps::concatenate(&tensors, $dim).into()\n            })*\n            _ => panic!(\"Unsupported dtype: {:?}\", $tensors[0].dtype())\n        }\n    };\n}\n\n/// Macro to execute an operation that returns a given element type.\n#[macro_export]\nmacro_rules! execute_with_float_out_dtype {\n    ($out_dtype:expr, $element:ident, $op:expr, [$($dtype: ident => $ty: ty),*]) => {{\n        match $out_dtype {\n            $(\n                burn_std::FloatDType::$dtype => {\n                    #[allow(unused)]","sourceCodeStart":291,"sourceCodeEnd":327,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-ndarray/src/tensor.rs#L291-L327","documentation":"The ndarray concatenate macro validates each input tensor's storage variant matches the expected dtype (taken from the first tensor). If any tensor in the list has a different dtype, it panics with expected vs got dtypes.","triggerScenarios":"Calling tensor.cat(tensors, dim) / NdArray::cat where the tensors in the slice have heterogeneous dtypes (e.g. mostly f32 but one i32 tensor).","commonSituations":"Concatenating feature tensors produced by different sub-graphs with mismatched precision; appending a placeholder tensor created from integer data; model-export bugs where one weight loads in the wrong dtype.","solutions":["Cast all tensors to the same dtype before concatenating: tensors.iter().map(|t| t.cast(DType::F32))","Verify how each input tensor was created/loaded and fix the one with the wrong dtype","Concatenate only tensors coming from the same backend generic parameter E"],"exampleFix":"// before\nlet out = Tensor::cat(vec![a_f32, b_i32], 0); // panics\n// after\nlet out = Tensor::cat(vec![a_f32, b_i32.cast(DType::F32)], 0);","handlingStrategy":"validation","validationCode":"let dt = tensors[0].dtype();\nassert!(tensors.iter().all(|t| t.dtype() == dt), \"cat requires uniform dtype\");","typeGuard":"fn all_same_dtype<T: TensorOps>(ts: &[T]) -> bool {\n    ts.iter().all(|t| t.dtype() == ts[0].dtype())\n}","tryCatchPattern":null,"preventionTips":["Normalize dtypes right after loading weights/data","Write a helper cat_same_dtype that casts before concatenating"],"tags":["rust","burn-ndarray","dtype","concat"],"backgroundTag":"dtype-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"}