{"record":{"id":"e203182dbc17a5e2","repo":"huggingface/candle","slug":"invalid-where-different-dtypes-for-values","errorCode":null,"errorMessage":"Invalid where: different dtypes for values {:?} != {:?}","messagePattern":"Invalid where: different dtypes for values (.+?) != (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/metal_backend/mod.rs","lineNumber":866,"sourceCode":"        t: &Self,\n        t_l: &Layout,\n        f: &Self,\n        f_l: &Layout,\n    ) -> Result<Self> {\n        let device = self.device.clone();\n        let shape = t_l.shape();\n        let dims = shape.dims();\n        let el = shape.elem_count();\n        let dtype = t.dtype;\n        let buffer = self\n            .device\n            .new_buffer_builder()\n            .with_size_for(el, dtype)\n            .with_label(\"where\")\n            .build()?;\n        let encoder = self.device.command_encoder()?;\n        if t.dtype() != f.dtype() {\n            crate::bail!(\n                \"Invalid where: different dtypes for values {:?} != {:?}\",\n                t.dtype(),\n                f.dtype()\n            );\n        }\n        let name = match (self.dtype, t.dtype()) {\n            (DType::U8, DType::F32) => \"where_u8_f32\",\n            (DType::U32, DType::F32) => \"where_u32_f32\",\n            (DType::U8, DType::BF16) => \"where_u8_bf16\",\n            (DType::U8, DType::F16) => \"where_u8_f16\",\n            (DType::U8, DType::I64) => \"where_u8_i64\",\n            (DType::U8, DType::U32) => \"where_u8_u32\",\n            (DType::U8, DType::U8) => \"where_u8_u8\",\n            (left, right) => crate::bail!(\"Metal where_cond {left:?} {right:?} not implemented\"),\n        };\n        let src = buffer_o(&self.buffer, layout, self.dtype);\n        let t = buffer_o(&t.buffer, t_l, t.dtype);\n        let f = buffer_o(&f.buffer, f_l, f.dtype);","sourceCodeStart":848,"sourceCodeEnd":884,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/metal_backend/mod.rs#L848-L884","documentation":"where_cond selects elements from a true-branch and false-branch tensor based on a condition; both value tensors must have the same dtype. The Metal backend explicitly checks t.dtype() != f.dtype() and bails with this message before dispatching the kernel.","triggerScenarios":"Calling Tensor::where_cond on Metal with a true tensor and false tensor of different dtypes, e.g. cond.where_cond(&f32_tensor, &f16_tensor)?.","commonSituations":"Mixing an fp32 bias branch with an fp16 model output; merging an integer index tensor with a float tensor; results of differently-typed branches in conditional logic.","solutions":["Cast one branch to match the other with to_dtype before where_cond","Ensure both branches come from the same compute pipeline/dtype","Cast both branches to F32, run where_cond, cast back"],"exampleFix":"// before\nlet out = cond.where_cond(&a, &b.to_dtype(DType::F16)?)?; // a is F32\n// after\nlet out = cond.where_cond(&a, &b.to_dtype(DType::F32)?)?;","handlingStrategy":"validation","validationCode":"if cond_t.dtype() != a.dtype() || cond_t.dtype() != b.dtype() {\n    anyhow::bail!(\"where_cond branches must share dtype\");\n}\n// or normalize:\nlet b = b.to_dtype(a.dtype())?;\nlet out = cond_t.where_cond(&a, &b)?;","typeGuard":null,"tryCatchPattern":"match cond.where_cond(&a, &b) {\n    Ok(y) => y,\n    Err(e) if e.to_string().contains(\"different dtypes for values\") => {\n        cond.where_cond(&a, &b.to_dtype(a.dtype())?)\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Cast both where_cond branches to the model dtype before selection","Derive both branches from tensors of the same pipeline/dtype","Add dtype assertions in layer wrappers around conditional logic"],"tags":["metal","gpu","where-cond","dtype-mismatch","candle"],"backgroundTag":"dtype-mismatch","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}