{"record":{"id":"29690a769c1dc489","repo":"huggingface/candle","slug":"dtype-mismatch-expected-got","errorCode":null,"errorMessage":"dtype mismatch, expected {:?}, got {:?}","messagePattern":"dtype mismatch, expected (.+?), got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/metal_backend/mod.rs","lineNumber":543,"sourceCode":"                    kernel_name,\n                    l.dims(),\n                    s,\n                    l.stride(),\n                    dst,\n                )\n                .map_err(MetalError::from)?;\n            }\n            Ok(())\n        }\n        match (self.dtype, s) {\n            (DType::U8, Scalar::U8(s)) => set(self, s, l),\n            (DType::U32, Scalar::U32(s)) => set(self, s, l),\n            (DType::I64, Scalar::I64(s)) => set(self, s, l),\n            (DType::F16, Scalar::F16(s)) => set(self, s, l),\n            (DType::BF16, Scalar::BF16(s)) => set(self, s, l),\n            (DType::F32, Scalar::F32(s)) => set(self, s, l),\n            (DType::F64, Scalar::F64(s)) => set(self, s, l),\n            _ => crate::bail!(\"dtype mismatch, expected {:?}, got {:?}\", self.dtype, s),\n        }\n    }\n\n    fn to_dtype(&self, layout: &Layout, dtype: DType) -> Result<Self> {\n        let device = self.device();\n        let shape = layout.shape();\n        let el_count = shape.elem_count();\n        let buffer = device\n            .new_buffer_builder()\n            .with_size_for(el_count, dtype)\n            .with_label(\"to_dtype\")\n            .build()?;\n        let encoder = device.command_encoder()?;\n        let src = buffer_o(&self.buffer, layout, self.dtype);\n        if layout.is_contiguous() {\n            let kernel_name = match (self.dtype, dtype) {\n                (DType::U32, DType::BF16) => \"cast_u32_bf16\",\n                (DType::U32, DType::F16) => \"cast_u32_f16\",","sourceCodeStart":525,"sourceCodeEnd":561,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/metal_backend/mod.rs#L525-L561","documentation":"Raised by MetalStorage::const_set when the tensor's dtype and the scalar's variant do not correspond, e.g. calling fill_ with an f32 value on a tensor whose dtype is F16 or U32. The backend matches on (dtype, Scalar) pairs and only proceeds when they agree exactly; anything else bails with the expected/got message. This is a caller-side dtype/scalar type error, not a missing-kernel issue.","triggerScenarios":"Tensor::fill_ (or const_set) with a scalar whose Rust type doesn't match the tensor's DType on Metal: e.g. fill_(0.5f32) on an F16 tensor, fill_(1i64) on a U32 tensor, or fill_ on an F8E4M3/other tensor with any scalar not listed in the match at metal_backend/mod.rs:535-543.","commonSituations":"Copy-pasted fill calls after a dtype change; numeric literals defaulting to f32/f64 while the tensor is F16/BF16; int vs uint confusion (I64 vs U32); generic helper code that fills tensors of mixed dtypes with the same literal.","solutions":["Match the scalar type to the tensor dtype: use f16::from_f32/bf16 values for F16/BF16 tensors, u32 for U32, i64 for I64.","Or pass the value as f64 — candle's fill_ accepts an impl trait/Into<f64> for float dtypes and converts internally when supported; check the fill_ signature you are using.","Inspect tensor.dtype() (printed in the error as 'expected') and adjust either the tensor dtype (to_dtype) or the scalar.","For unpaired dtypes like F8E4M3, avoid const_set entirely (see the unsupported const-set errors) and construct via from_vec."],"exampleFix":"// before\nlet t = Tensor::zeros(shape, DType::F16, &device)?;\nt.fill_(0.5f32)?; // dtype mismatch, expected F16, got F32\n// after\nlet t = Tensor::zeros(shape, DType::F16, &device)?;\nt.fill_(f16::from_f32(0.5))?;","handlingStrategy":"type-guard","validationCode":"fn check_scalar_dtype(dtype: candle_core::DType, s: &candle_core::Scalar) -> Result<(), String> {\n    use candle_core::{DType, Scalar};\n    let ok = matches!((dtype, s),\n        (DType::U8,  Scalar::U8(_))\n      | (DType::U32, Scalar::U32(_))\n      | (DType::I64, Scalar::I64(_))\n      | (DType::F16, Scalar::F16(_))\n      | (DType::BF16, Scalar::BF16(_))\n      | (DType::F32, Scalar::F32(_))\n      | (DType::F64, Scalar::F64(_)));\n    if ok { Ok(()) } else { Err(format!(\"scalar {:?} does not match tensor dtype {:?}\", s, dtype)) }\n}","typeGuard":"fn scalar_matches(dtype: candle_core::DType, s: &candle_core::Scalar) -> bool {\n    use candle_core::{DType, Scalar};\n    matches!((dtype, s),\n        (DType::U8, Scalar::U8(_)) | (DType::U32, Scalar::U32(_))\n      | (DType::I64, Scalar::I64(_)) | (DType::F16, Scalar::F16(_))\n      | (DType::BF16, Scalar::BF16(_)) | (DType::F32, Scalar::F32(_))\n      | (DType::F64, Scalar::F64(_)))\n}","tryCatchPattern":"match t.fill_(v) {\n    Ok(t) => t,\n    Err(e) if e.to_string().contains(\"dtype mismatch\") => {\n        let mut t = t.to_dtype(candle_core::DType::F32, t.device())?;\n        t.fill_(v)?;\n        t\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Always read tensor.dtype() before fill_ and use the matching scalar constructor (half::f16, half::bf16, u32, i64).","Remember Rust numeric literals default to f32/i32 — annotate them (1.0f16-style via from_f32, 1u32, etc.).","Wrap fills in a small helper that converts the scalar to the tensor's dtype first.","In generic code, match on DType explicitly instead of assuming f32."],"tags":["metal","dtype-mismatch","scalar","fill","candle"],"backgroundTag":"dtype-mismatch","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}