{"record":{"id":"3dd828bfa12dff67","repo":"tracel-ai/burn","slug":"optional-argument-type-mismatch","errorCode":null,"errorMessage":"Optional argument type mismatch","messagePattern":"Optional argument type mismatch","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-ndarray/src/ops/module.rs","lineNumber":39,"sourceCode":"};\nuse burn_backend::{\n    TensorMetadata,\n    ops::{attention::attention_fallback, conv::pad_asymmetric_conv_input, *},\n    tensor::FloatTensor,\n};\nuse burn_std::IntDType;\n\nmacro_rules! module_op {\n    // Module op with inputs (inp), optional (opt) and arguments (args).\n    // Converts NdArrayStorage to SharedArray for compatibility with existing operations.\n    (inp($($x:tt),+), opt($($opt:tt),*), $element:ident, $op:expr) => {{\n        #[allow(unused_parens, unreachable_patterns)]\n        match ($($x),+) {\n            ($(NdArrayTensor::F32($x)),+) => {\n                type $element = f32;\n                $op(\n                    $($x.into_shared()),+\n                    $(, $opt.map(|o| match o { NdArrayTensor::F32(val) => val.into_shared(), _ => panic!(\"Optional argument type mismatch\") }))*\n                )\n            }\n            ($(NdArrayTensor::F64($x)),+) => {\n                type $element = f64;\n                $op(\n                    $($x.into_shared()),+\n                    $(, $opt.map(|o| match o { NdArrayTensor::F64(val) => val.into_shared(), _ => panic!(\"Optional argument type mismatch\") }))*\n                )\n            }\n            _ => panic!(\"Data type mismatch\"),\n        }\n    }};\n}\n\nimpl ModuleOps<Self> for NdArray {\n    fn conv2d(\n        x: NdArrayTensor,\n        weight: NdArrayTensor,","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-ndarray/src/ops/module.rs#L21-L57","documentation":"The module_op! macro dispatches tensors by dtype (F32/F64) and maps an optional argument to the same dtype. If the optional argument tensor's dtype variant doesn't match the primary tensor's matched variant (e.g. bias f64 while input f32), the macro panics with 'Optional argument type mismatch'.","triggerScenarios":"Calling a module op (conv2d, conv_transpose2d, interpolate, etc.) where the main tensor(s) are F32 but the optional argument (like a bias tensor) is F64 (or vice versa).","commonSituations":"Creating bias with Tensor::full/ones defaulting to a different element type; loading weights from a checkpoint saved with f64; mixing Tensor<NdArray<f32>> and Tensor<NdArray<f64>> in one op call.","solutions":["Convert the optional argument to the same dtype as the input, e.g. bias.cast::<f32>() / create it with the same element type parameter.","Declare all tensors with the same element type: Tensor<NdArray<f32>, _> for both input and bias.","Check checkpoint/config for f64 weights and cast on load.","Make helper functions generic but instantiate with one E type."],"exampleFix":"// before\nlet x: Tensor<NdArray<f32>, 3> = input;\nlet bias: Tensor<NdArray<f64>, 1> = Tensor::ones([c]);\nconv2d(x, weight, Some(bias), options); // panic\n// after\nlet bias: Tensor<NdArray<f32>, 1> = Tensor::ones([c]);\nconv2d(x, weight, Some(bias), options);","handlingStrategy":"type-guard","validationCode":"fn ensure_bias_dtype<E>(bias: &Tensor<NdArray<E>, 1>) { /* instantiate with same E as input */ }","typeGuard":"fn f64_to_f32(opt: Option<Tensor<NdArray<f64>, 1>>) -> Option<Tensor<NdArray<f32>, 1>> {\n    opt.map(|b| b.cast::<f32>())\n}","tryCatchPattern":null,"preventionTips":["Always construct bias/weights with the same element type as the model backend.","Avoid mixing f32 and f64 tensors in one model.","Cast checkpoint weights at load time to the model dtype."],"tags":["rust","burn-ndarray","dtype","macro"],"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"}