{"record":{"id":"138beb2fcb7029b6","repo":"huggingface/candle","slug":"metal-where-cond-left-right-not-implemente","errorCode":null,"errorMessage":"Metal where_cond {left:?} {right:?} not implemented","messagePattern":"Metal where_cond (.+?) (.+?) not implemented","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/metal_backend/mod.rs","lineNumber":880,"sourceCode":"            .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);\n        candle_metal_kernels::call_where_cond(\n            &device.device,\n            &encoder,\n            &device.kernels,\n            name,\n            dtype.size_in_bytes(),\n            dims,\n            src,\n            layout.stride(),\n            layout.is_contiguous(),\n            t,\n            t_l.stride(),\n            t_l.is_contiguous(),\n            f,","sourceCodeStart":862,"sourceCodeEnd":898,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/metal_backend/mod.rs#L862-L898","documentation":"Beyond matching branch dtypes, where_cond on Metal needs a kernel for the (condition-dtype, value-dtype) pair. Pairs outside the dispatch table (e.g. an I64 condition or unsupported combination) hit this bail, since no Metal kernel implements that selection.","triggerScenarios":"Calling where_cond on Metal with a condition/value dtype combination missing from the table, such as an I64 mask with F16 values, or U32 condition with BF16 values.","commonSituations":"Using comparison results (which may be U8/U32) with float tensors of unusual dtype; masks produced in one dtype and applied to tensors of another; porting CUDA-working code to Metal.","solutions":["Ensure the condition tensor is a supported dtype (U8/U32/F32 etc. per table) via to_dtype on the mask","Align value-tensor dtype to a supported pair (F32 is broadly supported)","Compute where_cond on CPU and move the result to Metal","Upgrade candle for new Metal where_cond kernels"],"exampleFix":"// before\nlet out = mask_i64.where_cond(&a, &b)?; // i64 mask unsupported\n// after\nlet out = mask_i64.to_dtype(DType::U8)?.where_cond(&a, &b)?;","handlingStrategy":"validation","validationCode":"let mask = if matches!(mask.dtype(), DType::I64 | DType::F32) {\n    mask.to_dtype(DType::U8)?\n} else { mask };\nif !matches!(a.dtype(), DType::F32 | DType::F16 | DType::BF16 | DType::U32 | DType::U8 | DType::I64) {\n    anyhow::bail!(\"where_cond value dtype unsupported on Metal\");\n}","typeGuard":null,"tryCatchPattern":"match mask.where_cond(&a, &b) {\n    Ok(y) => y,\n    Err(e) if e.to_string().contains(\"where_cond\") && e.to_string().contains(\"not implemented\") => {\n        mask.to_dtype(DType::U8)?.where_cond(&a.to_dtype(DType::F32)?, &b.to_dtype(DType::F32)?)\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Use U8/U32 masks for where_cond on Metal","Keep selected values in broadly supported dtypes (F32)","Prefer comparison ops that emit U8 masks"],"tags":["metal","gpu","where-cond","dtype","candle"],"backgroundTag":"unsupported-dtype-for-op","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}