{"record":{"id":"b692e19de2ff5c17","repo":"tracel-ai/burn","slug":"compare-elem-unsupported-dtype","errorCode":null,"errorMessage":"compare_elem: unsupported dtype {:?}","messagePattern":"compare_elem: unsupported dtype (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/comparison.rs","lineNumber":336,"sourceCode":"{\n    let dtype = lhs.dtype();\n\n    match dtype {\n        DType::F32 => compare_elem_f32(lhs, rhs as f32, out_dtype, f32_cmp, simd_hint),\n        DType::F64 => compare_elem_typed(lhs, rhs, out_dtype, f64_cmp),\n        DType::F16 => {\n            let scalar = f16::from_f64(rhs);\n            compare_elem_typed(lhs, scalar, out_dtype, |a: f16, b: f16| {\n                f32_cmp(a.to_f32(), b.to_f32())\n            })\n        }\n        DType::BF16 => {\n            let scalar = bf16::from_f64(rhs);\n            compare_elem_typed(lhs, scalar, out_dtype, |a: bf16, b: bf16| {\n                f32_cmp(a.to_f32(), b.to_f32())\n            })\n        }\n        _ => panic!(\"compare_elem: unsupported dtype {:?}\", dtype),\n    }\n}\n\n/// Specialized scalar comparison for f32 with SIMD fast path.\n#[cfg(feature = \"simd\")]\nfn compare_elem_f32<Cmp>(\n    lhs: FlexTensor,\n    rhs: f32,\n    out_dtype: BoolDType,\n    cmp: Cmp,\n    simd_hint: Option<CompareOp>,\n) -> FlexTensor\nwhere\n    Cmp: Fn(f32, f32) -> bool,\n{\n    // SIMD fast path: tensor is contiguous\n    if let Some((start, end)) = lhs.layout().contiguous_offsets()\n        && let Some(simd_op) = simd_hint","sourceCodeStart":318,"sourceCodeEnd":354,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/comparison.rs#L318-L354","documentation":"compare_elem performs tensor-vs-scalar comparisons (greater_elem, greater_equal_elem, lower_elem, lower_equal_elem, equal_elem, not_equal_elem). The scalar is converted into the tensor's element type — F16/BF16 scalars go through from_f64 with an f32 comparison — and any non-float tensor dtype hits the panic arm. out_dtype controls the output boolean representation.","triggerScenarios":"Calling any *_elem comparison on an integer, unsigned or bool tensor (only float branches are matched); passing a scalar that cannot be represented in f16/bf16 is fine (it rounds), but a non-float tensor dtype always panics.","commonSituations":"Thresholding integer predictions/labels against a scalar; comparing bool tensors against 0/1; porting NumPy/PyTorch scalar-comparison code that supports all dtypes.","solutions":["Cast the tensor to a float dtype first: x.cast(DType::F32).greater_elem(0.5).","Use an integer-aware comparison helper if one exists in the ops module for int tensors.","Compute the threshold as an integer scalar and use int scalar comparison ops when the tensor is integral."],"exampleFix":"// before\nlet mask = greater_elem(preds_i64, 0); // panics\n// after\nlet mask = preds_i64.cast(DType::F32).greater_elem(0.0);","handlingStrategy":"validation","validationCode":"if !matches!(tensor.dtype(), DType::F32 | DType::F64 | DType::F16 | DType::BF16) {\n    tensor = tensor.cast(DType::F32);\n}\nlet mask = tensor.greater_elem(threshold);","typeGuard":"fn is_float_dtype(d: DType) -> bool {\n    matches!(d, DType::F32 | DType::F64 | DType::F16 | DType::BF16)\n}","tryCatchPattern":"let mask = std::panic::catch_unwind(|| greater_elem(tensor.clone(), t))\n    .unwrap_or_else(|_| greater_elem(tensor.cast(DType::F32), t as f32));","preventionTips":["Cast integer/bool tensors to float before scalar comparisons.","Remember f16/bf16 scalars are converted via f64->half — expect rounding.","Threshold int predictions with integer comparison ops instead.","Validate dtype at model boundaries where predictions and thresholds meet."],"tags":["panic","dtype","comparison","scalar","unsupported-dtype"],"backgroundTag":"unsupported-dtype","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"}