tracel-ai/burn · error

float_cast: unsupported source dtype {:?}

Error message

float_cast: unsupported source dtype {:?}

What it means

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.

Source

Thrown at crates/burn-flex/src/ops/float.rs:851

        // Convert to f64 intermediate, then to target
        let f64_values: Vec<f64> = match src_dtype {
            DType::F32 => {
                let src: &[f32] = tensor.storage();
                src.iter().map(|&v| v as f64).collect()
            }
            DType::F64 => {
                let src: &[f64] = tensor.storage();
                src.to_vec()
            }
            DType::F16 => {
                let src: &[f16] = tensor.storage();
                src.iter().map(|&v| v.to_f32() as f64).collect()
            }
            DType::BF16 => {
                let src: &[bf16] = tensor.storage();
                src.iter().map(|&v| v.to_f32() as f64).collect()
            }
            _ => panic!("float_cast: unsupported source dtype {:?}", src_dtype),
        };

        // Convert from f64 to target dtype
        match target_dtype {
            DType::F32 => {
                let result: Vec<f32> = f64_values.iter().map(|&v| v as f32).collect();
                let bytes = Bytes::from_elems(result);
                FlexTensor::new(bytes, Layout::contiguous(shape), DType::F32)
            }
            DType::F64 => {
                let bytes = Bytes::from_elems(f64_values);
                FlexTensor::new(bytes, Layout::contiguous(shape), DType::F64)
            }
            DType::F16 => {
                let result: Vec<f16> = f64_values.iter().map(|&v| f16::from_f64(v)).collect();
                let bytes = Bytes::from_elems(result);
                FlexTensor::new(bytes, Layout::contiguous(shape), DType::F16)
            }

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Use the integer tensor cast path (int_cast / Tensor<Int,...>.to_dtype) instead of the float cast for Int tensors.
  2. Ensure the tensor kind matches: cast Int tensors via int-specific APIs, Bool via bool-specific APIs.
  3. Check that an earlier op did not change the tensor from Float to Int unexpectedly.
  4. Add support for the source dtype in float_cast's source match in crates/burn-flex/src/ops/float.rs.

Example fix

// before
let f: Tensor<B, 2> = int_tensor.to_dtype(FloatDType::F32); // wrong kind path
// after
let f: Tensor<B, 2> = int_tensor.cast::<burn::tensor::f32>(); // int->float via correct path
Defensive patterns

Strategy: type-guard

Validate before calling

if !matches!(src_dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16) { /* use the int/bool cast path instead of float_cast */ }

Type guard

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) }

Try / catch

// Panics are not catchable; branch on dtype first:
match tensor.dtype() { DType::F32 | DType::F64 | DType::F16 | DType::BF16 => { /* float cast */ }, _ => { /* int/bool cast path */ } }

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/7fc630184d68d09d. Report an issue: GitHub.