{"record":{"id":"7fc630184d68d09d","repo":"tracel-ai/burn","slug":"float-cast-unsupported-source-dtype","errorCode":null,"errorMessage":"float_cast: unsupported source dtype {:?}","messagePattern":"float_cast: unsupported source dtype (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/float.rs","lineNumber":851,"sourceCode":"        // Convert to f64 intermediate, then to target\n        let f64_values: Vec<f64> = match src_dtype {\n            DType::F32 => {\n                let src: &[f32] = tensor.storage();\n                src.iter().map(|&v| v as f64).collect()\n            }\n            DType::F64 => {\n                let src: &[f64] = tensor.storage();\n                src.to_vec()\n            }\n            DType::F16 => {\n                let src: &[f16] = tensor.storage();\n                src.iter().map(|&v| v.to_f32() as f64).collect()\n            }\n            DType::BF16 => {\n                let src: &[bf16] = tensor.storage();\n                src.iter().map(|&v| v.to_f32() as f64).collect()\n            }\n            _ => panic!(\"float_cast: unsupported source dtype {:?}\", src_dtype),\n        };\n\n        // Convert from f64 to target dtype\n        match target_dtype {\n            DType::F32 => {\n                let result: Vec<f32> = f64_values.iter().map(|&v| v as f32).collect();\n                let bytes = Bytes::from_elems(result);\n                FlexTensor::new(bytes, Layout::contiguous(shape), DType::F32)\n            }\n            DType::F64 => {\n                let bytes = Bytes::from_elems(f64_values);\n                FlexTensor::new(bytes, Layout::contiguous(shape), DType::F64)\n            }\n            DType::F16 => {\n                let result: Vec<f16> = f64_values.iter().map(|&v| f16::from_f64(v)).collect();\n                let bytes = Bytes::from_elems(result);\n                FlexTensor::new(bytes, Layout::contiguous(shape), DType::F16)\n            }","sourceCodeStart":833,"sourceCodeEnd":869,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/float.rs#L833-L869","documentation":"float_cast converts a float tensor to another dtype by round-tripping through f64. The source-side match supports F32, F64, F16 and BF16; any other source dtype reaching the float cast panics. This usually means an Int or Bool tensor was passed to a float-cast path.","triggerScenarios":"Calling tensor.to_dtype / float_cast on the burn-flex backend where the SOURCE tensor dtype is not one of the four float dtypes (e.g. casting an Int tensor through the float cast entry point).","commonSituations":"Casting integer tensors (e.g. after argmax or comparisons) using the float cast API; generic code calling to_dtype on Tensor<AnyKind> without knowing the kind; mismatches between the int cast and float cast entry points.","solutions":["Use the integer tensor cast path (int_cast / Tensor<Int,...>.to_dtype) instead of the float cast for Int tensors.","Ensure the tensor kind matches: cast Int tensors via int-specific APIs, Bool via bool-specific APIs.","Check that an earlier op did not change the tensor from Float to Int unexpectedly.","Add support for the source dtype in float_cast's source match in crates/burn-flex/src/ops/float.rs."],"exampleFix":"// before\nlet f: Tensor<B, 2> = int_tensor.to_dtype(FloatDType::F32); // wrong kind path\n// after\nlet f: Tensor<B, 2> = int_tensor.cast::<burn::tensor::f32>(); // int->float via correct path","handlingStrategy":"type-guard","validationCode":"if !matches!(src_dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16) { /* use the int/bool cast path instead of float_cast */ }","typeGuard":"fn is_float_kind<B: Backend, const D: usize>(t: &Tensor<B, D>) -> bool { matches!(t.dtype(), DType::F32 | DType::F64 | DType::F16 | DType::BF16) }","tryCatchPattern":"// Panics are not catchable; branch on dtype first:\nmatch tensor.dtype() { DType::F32 | DType::F64 | DType::F16 | DType::BF16 => { /* float cast */ }, _ => { /* int/bool cast path */ } }","preventionTips":["Match cast calls to tensor kind: int_cast for Int tensors, float paths for Float tensors.","Avoid calling to_dtype on Tensor<AnyKind> without checking the kind first.","Convert Int->Float via the int tensor's cast, not the float cast entry point.","Keep casts at explicit boundaries rather than inside generic code."],"tags":["rust","dtype","panic","burn","cast"],"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"}