tracel-ai/burn · error

int_cast: unsupported conversion from {:?} to {:?}

Error message

int_cast: unsupported conversion from {:?} to {:?}

What it means

int_cast converts an integer tensor between integer dtypes via a large source-dtype x target-dtype match table. When no combination matches (bytes == None) it panics. This happens when the source or target dtype is not an integer type — e.g. casting an int tensor to a float dtype through this path, or casting a float/bool tensor to int — because those conversions are handled elsewhere (int_into_float / float ops).

Source

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

            // From U8
            DType::U8 => {
                let storage: &[u8] = tensor.storage();
                match target_dtype {
                    DType::I64 => cast_impl!(storage, i64),
                    DType::I32 => cast_impl!(storage, i32),
                    DType::I16 => cast_impl!(storage, i16),
                    DType::I8 => cast_impl!(storage, i8),
                    DType::U64 => cast_impl!(storage, u64),
                    DType::U32 => cast_impl!(storage, u32),
                    DType::U16 => cast_impl!(storage, u16),
                    _ => None,
                }
            }

            _ => None,
        };
        let Some(bytes) = bytes else {
            panic!(
                "int_cast: unsupported conversion from {:?} to {:?}",
                tensor.dtype(),
                target_dtype
            )
        };
        FlexTensor::new(bytes, Layout::contiguous(shape), target_dtype)
    }

    fn int_unfold(
        tensor: IntTensor<Flex>,
        dim: usize,
        size: usize,
        step: usize,
    ) -> IntTensor<Flex> {
        crate::ops::unfold::unfold_int(tensor, dim, size, step)
    }

    fn int_neg(tensor: IntTensor<Flex>) -> IntTensor<Flex> {

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. For int-to-float conversion, ensure the call routes through the float cast path (int_into_float), not int_cast.
  2. For float/bool sources, convert to an integer dtype using the appropriate op before int_cast.
  3. Check both tensor.dtype() and the target dtype at the call site; log them if unsure.
  4. Restrict dynamic dtype parameters to integer dtypes when the tensor is known to be int.

Example fix

// before
let f = int_tensor.cast::<f32>(); // routed to int_cast -> panic
// after
let f = int_tensor.float_cast::<f32>(); // or use the float conversion API (int_into_float path)
Defensive patterns

Strategy: validation

Validate before calling

assert!(t.dtype().is_int() && target_dtype.is_int(), "int_cast requires int-to-int conversion, got {:?} -> {:?}", t.dtype(), target_dtype);

Type guard

fn is_int_to_int(src: DType, dst: DType) -> bool { src.is_int() && dst.is_int() }

Prevention

When it happens

Trigger: Calling tensor.cast::<T>() where the source tensor is int but the target is a float dtype (should go through int_into_float), or where the source is a float/bool tensor being cast to int, or an int-to-int pair absent from the table.

Common situations: Mixing int and float pipelines: casting counts/indices to f32 for division; casting boolean masks to i64 via the generic cast; using a dtype constant (e.g. DType::F32) computed dynamically so the non-int case isn't visible at compile time.

Related errors


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