tracel-ai/burn · error

float_into_int: unsupported source dtype {:?}

Error message

float_into_int: unsupported source dtype {:?}

What it means

float_into_int converts a float tensor's data to an integer dtype. The read_floats! macro only handles F32, F64, F16, and BF16 source dtypes; passing a tensor whose dtype is anything else (e.g. a bool or int tensor routed here by mistake) hits the catch-all panic. It is a defensive guard against backend misuse, not a user-facing conversion failure.

Source

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

                        })
                        .collect(),
                    DType::F16 => tensor
                        .storage::<f16>()
                        .iter()
                        .map(|v| {
                            let $x = f32::from(*v) as f64;
                            $conv
                        })
                        .collect(),
                    DType::BF16 => tensor
                        .storage::<bf16>()
                        .iter()
                        .map(|v| {
                            let $x = f32::from(*v) as f64;
                            $conv
                        })
                        .collect(),
                    _ => panic!("float_into_int: unsupported source dtype {:?}", src),
                }
            };
        }

        macro_rules! convert {
            ($int_ty:ty) => {{
                let data: Vec<$int_ty> = read_floats!(|x| x as $int_ty);
                FlexTensor::new(Bytes::from_elems(data), Layout::contiguous(shape), out_dt)
            }};
        }

        match out_dtype {
            IntDType::I64 => convert!(i64),
            IntDType::I32 => convert!(i32),
            IntDType::I16 => convert!(i16),
            IntDType::I8 => convert!(i8),
            IntDType::U64 => convert!(u64),
            IntDType::U32 => convert!(u32),

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Check the tensor's dtype before casting; only float tensors (F32/F64/F16/BF16) can go through float_into_int
  2. If the tensor is bool/int, use the appropriate int/bool conversion op instead of the float cast
  3. If the source op unexpectedly returned a non-float dtype, fix the upstream op or add an explicit .float() cast first
  4. If a new DType variant was added to the backend, add a matching arm in the read_floats! macro

Example fix

// before: mask is Bool -> panic in float_into_int
let ints = mask.cast(IntDType::I32);
// after: cast bool to float first
let ints = mask.cast(FloatDType::F32).cast(IntDType::I32);
Defensive patterns

Strategy: type-guard

Validate before calling

use burn_tensor::DType;
fn ensure_float(dt: DType) -> Result<(), String> {
    match dt {
        DType::F32 | DType::F64 | DType::F16 | DType::BF16 => Ok(()),
        other => Err(format!("float_into_int requires a float dtype, got {:?}", other)),
    }
}

Type guard

fn is_float_dtype(dt: DType) -> bool {
    matches!(dt, DType::F32 | DType::F64 | DType::F16 | DType::BF16)
}

Prevention

When it happens

Trigger: Calling float_into_int (via dtype cast/conversion APIs) with a source tensor whose dtype is not a floating-point type, e.g. Bool or an Int dtype. In normal use this only happens if another op mis-dispatches a non-float tensor into the float cast path.

Common situations: Backend bug or an op that returns an int/bool tensor where a float tensor was expected; casting a boolean mask tensor through the float cast path; version drift where a new DType variant was added without updating this match.

Related errors


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