tracel-ai/burn · error

int_into_float: unsupported source dtype {:?}

Error message

int_into_float: unsupported source dtype {:?}

What it means

int_into_float converts an integer tensor to a floating-point tensor. The read_ints! macro matches on the source dtype and covers all integer dtypes (I64/I32/I16/I8/U64/U32/U16/U8); the panic fires only when the source tensor is a non-integer dtype (e.g. F32, Bool), meaning a float or bool tensor reached the int->float conversion path. This is an internal invariant check: callers should never pass a non-int tensor here.

Source

Thrown at crates/burn-flex/src/ops/int.rs:599

        let tensor = tensor.to_contiguous();
        let shape = tensor.layout().shape().clone();
        let src = tensor.dtype();
        let out_dt = DType::from(out_dtype);

        // Read source ints, applying conversion per-element.
        // Each arm binds `$x` to the native int value; `$conv` must work for all int types.
        macro_rules! read_ints {
            (|$x:ident| $conv:expr) => {
                match src {
                    DType::I64 => tensor.storage::<i64>().iter().map(|&$x| $conv).collect(),
                    DType::I32 => tensor.storage::<i32>().iter().map(|&$x| $conv).collect(),
                    DType::I16 => tensor.storage::<i16>().iter().map(|&$x| $conv).collect(),
                    DType::I8 => tensor.storage::<i8>().iter().map(|&$x| $conv).collect(),
                    DType::U64 => tensor.storage::<u64>().iter().map(|&$x| $conv).collect(),
                    DType::U32 => tensor.storage::<u32>().iter().map(|&$x| $conv).collect(),
                    DType::U16 => tensor.storage::<u16>().iter().map(|&$x| $conv).collect(),
                    DType::U8 => tensor.storage::<u8>().iter().map(|&$x| $conv).collect(),
                    _ => panic!("int_into_float: unsupported source dtype {:?}", src),
                }
            };
        }

        match out_dtype {
            FloatDType::F64 => {
                let data: Vec<f64> = read_ints!(|x| x as f64);
                FlexTensor::new(Bytes::from_elems(data), Layout::contiguous(shape), out_dt)
            }
            FloatDType::F32 | FloatDType::Flex32 => {
                let data: Vec<f32> = read_ints!(|x| x as f32);
                FlexTensor::new(Bytes::from_elems(data), Layout::contiguous(shape), out_dt)
            }
            FloatDType::F16 => {
                let data: Vec<f16> = read_ints!(|x| f16::from_f32(x as f32));
                FlexTensor::new(Bytes::from_elems(data), Layout::contiguous(shape), out_dt)
            }
            FloatDType::BF16 => {

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Check the source tensor's dtype before casting and only call the float cast on integer tensors.
  2. If the tensor is Bool, convert it to an integer dtype explicitly first (e.g. via int cast ops) before casting to float.
  3. Remove redundant casts in the pipeline so an already-float tensor is not cast again.
  4. Verify data-loading code produces the dtype you expect (log or assert tensor.dtype() upstream).

Example fix

// before: casting a bool mask directly
let mask_f = mask.cast::<f32>(); // panics: source dtype is Bool
// after
let mask_i = mask.bool_to_int(); // or ensure mask was created as Int dtype
let mask_f = mask_i.cast::<f32>();
Defensive patterns

Strategy: validation

Validate before calling

assert!(t.dtype().is_int(), "int_into_float requires an int tensor, got {:?}", t.dtype());

Type guard

fn is_int_tensor(t: &FlexTensor) -> bool { matches!(t.dtype(), DType::I64 | DType::I32 | DType::I16 | DType::I8 | DType::U64 | DType::U32 | DType::U16 | DType::U8) }

Prevention

When it happens

Trigger: Calling tensor.cast::<f32>() (or another float cast) on a Burn Flex tensor whose dtype is not an integer type — e.g. a F32/F64/BF16/F16 or Bool tensor produced by a comparison or already-float pipeline.

Common situations: Casting a boolean mask tensor (result of ==, >, etc.) to float; double-casting a tensor that was already converted; dtype mismatch after loading a checkpoint or external data where the tensor dtype was inferred differently than expected.

Related errors


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