{"record":{"id":"6df48c1365490489","repo":"tracel-ai/burn","slug":"data-type-mismatch-lhs-rhs","errorCode":null,"errorMessage":"Data type mismatch (lhs: {:?}, rhs: {:?})","messagePattern":"Data type mismatch \\(lhs: (.+?), rhs: (.+?)\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-ndarray/src/tensor.rs","lineNumber":134,"sourceCode":"///\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! execute_with_dtype {\n    (($lhs:expr, $rhs:expr),$element:ident,  $op:expr, [$($dtype: ident => $ty: ty),*]) => {{\n        let lhs_dtype = burn_backend::TensorMetadata::dtype(&$lhs);\n        let rhs_dtype = burn_backend::TensorMetadata::dtype(&$rhs);\n        match ($lhs, $rhs) {\n            $(\n                ($crate::NdArrayTensor::$dtype(lhs), $crate::NdArrayTensor::$dtype(rhs)) => {\n                    #[allow(unused)]\n                    type $element = $ty;\n                    // Convert storage to SharedArray for compatibility with existing operations\n                    $op(lhs.into_shared(), rhs.into_shared()).into()\n                }\n            )*\n            _ => panic!(\n                \"Data type mismatch (lhs: {:?}, rhs: {:?})\",\n                lhs_dtype, rhs_dtype\n            ),\n        }\n    }};\n    // Binary op: type automatically inferred by the compiler\n    (($lhs:expr, $rhs:expr), $op:expr) => {{\n        $crate::execute_with_dtype!(($lhs, $rhs), E, $op)\n    }};\n\n    // Binary op: generic type cannot be inferred for an operation\n    (($lhs:expr, $rhs:expr), $element:ident, $op:expr) => {{\n        $crate::execute_with_dtype!(($lhs, $rhs), $element, $op, [\n            F64 => f64, F32 => f32,\n            I64 => i64, I32 => i32, I16 => i16, I8 => i8,\n            U64 => u64, U32 => u32, U16 => u16, U8 => u8,\n            Bool => bool\n        ])","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-ndarray/src/tensor.rs#L116-L152","documentation":"A macro in burn-ndarray for binary tensor ops matches on the (lhs, rhs) dtype pair and falls through to a panic when both operands don't share a supported matching element type. Mixed-dtype arithmetic (e.g. f32 tensor + i64 tensor) is not performed implicitly.","triggerScenarios":"Any binary tensor operation (add, mul, matmul, etc.) where lhs.dtype() != rhs.dtype(), e.g. adding an f32 tensor to an i64 tensor on the ndarray backend.","commonSituations":"Mixing integer masks/indices with float activations; loading weights in a different precision than activations; combining a tensor created from integer data with float computation.","solutions":["Cast one operand so both dtypes match: lhs.cast(DType::F32) (or rhs)","Ensure model weights and activations load with the same dtype (set the generic param E of the backend accordingly)","Where the API allows, use explicit numeric conversions on scalar literals so they adopt the tensor's dtype"],"exampleFix":"// before\nlet c = lhs + rhs; // f32 + i64 -> panic\n// after\nlet c = lhs + rhs.cast(DType::F32);","handlingStrategy":"type-guard","validationCode":"if lhs.dtype() != rhs.dtype() {\n    return Err(...);\n}","typeGuard":"fn same_dtype<T: TensorOps>(a: &T, b: &T) -> bool {\n    a.dtype() == b.dtype()\n}","tryCatchPattern":null,"preventionTips":["Insert explicit casts at model boundaries where dtypes may differ","Keep integer control tensors (masks/indices) converted to float before arithmetic","Use a single generic element type parameter for the whole model"],"tags":["rust","burn-ndarray","dtype","binary-op"],"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"}