tracel-ai/burn · error

int_binary_op: unsupported dtype {:?}

Error message

int_binary_op: unsupported dtype {:?}

What it means

int_binary_op dispatches integer elementwise ops (int_add, int_sub, int_mul, int_div, int_remainder, bitwise_and) on the tensor dtype. Signed ints and U64/U32/U16/U8 are handled (unsigned values are computed through i64 two's-complement, with div/rem caveats at the call site); signed I64/I32 and float/bool dtypes hit the panic arm. Float tensors must go through binary_op instead.

Source

Thrown at crates/burn-flex/src/ops/binary.rs:379

    debug_assert_eq!(lhs.dtype(), rhs.dtype(), "int_binary_op: dtype mismatch");

    // Broadcast tensors to the same shape if needed
    let (lhs, rhs) = crate::ops::expand::broadcast_binary(lhs, rhs);

    let dtype = lhs.dtype();

    match dtype {
        DType::I64 => binary_op_typed(lhs, rhs, op),
        DType::I32 => binary_op_typed(lhs, rhs, |a: i32, b: i32| op(a as i64, b as i64) as i32),
        DType::I16 => binary_op_typed(lhs, rhs, |a: i16, b: i16| op(a as i64, b as i64) as i16),
        DType::I8 => binary_op_typed(lhs, rhs, |a: i8, b: i8| op(a as i64, b as i64) as i8),
        // u64 values > i64::MAX wrap to negative i64. This is correct for
        // add/sub/mul/bitwise (two's complement). Div/rem are handled at the call site.
        DType::U64 => binary_op_typed(lhs, rhs, |a: u64, b: u64| op(a as i64, b as i64) as u64),
        DType::U32 => binary_op_typed(lhs, rhs, |a: u32, b: u32| op(a as i64, b as i64) as u32),
        DType::U16 => binary_op_typed(lhs, rhs, |a: u16, b: u16| op(a as i64, b as i64) as u16),
        DType::U8 => binary_op_typed(lhs, rhs, |a: u8, b: u8| op(a as i64, b as i64) as u8),
        _ => panic!("int_binary_op: unsupported dtype {:?}", dtype),
    }
}

/// Apply a scalar operation to each element of an integer tensor.
/// Note: scalar is truncated to target dtype (matches PyTorch).
pub fn int_scalar_op<Op>(tensor: FlexTensor, scalar: i64, op: Op) -> FlexTensor
where
    Op: Fn(i64, i64) -> i64 + Copy,
{
    let dtype = tensor.dtype();

    match dtype {
        DType::I64 => scalar_op_typed(tensor, scalar, op),
        DType::I32 => scalar_op_typed(tensor, scalar as i32, |a: i32, b: i32| {
            op(a as i64, b as i64) as i32
        }),
        DType::I16 => scalar_op_typed(tensor, scalar as i16, |a: i16, b: i16| {
            op(a as i64, b as i64) as i16

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Use binary_op / float ops for float tensors: cast to float or call the float variant of the op.
  2. Cast Bool tensors to an int dtype (e.g. tensor.cast(DType::U8) or bool_into_int) before int ops.
  3. Check the dtype at the call site and route signed dtypes through the supported arms or extend the match.

Example fix

// before
let z = int_add(a_f32, b_f32); // panics: floats
// after
let z = binary_add(a_f32, b_f32); // float path
Defensive patterns

Strategy: validation

Validate before calling

if !matches!(lhs.dtype(), DType::U64 | DType::U32 | DType::U16 | DType::U8 | DType::I64 | DType::I32) {
    panic!("int_binary_op needs integer tensors, got {:?}", lhs.dtype());
}

Type guard

fn is_supported_int(d: DType) -> bool {
    matches!(d, DType::U64 | DType::U32 | DType::U16 | DType::U8 | DType::I64 | DType::I32)
}

Try / catch

let z = std::panic::catch_unwind(|| int_add(lhs.clone(), rhs.clone()))
    .unwrap_or_else(|_| binary_add(lhs.cast(DType::F32), rhs.cast(DType::F32)).cast(lhs.dtype()));

Prevention

When it happens

Trigger: Calling int_add/int_sub/int_mul/int_div/int_remainder/bitwise_and on a float tensor (F32/F64/F16/BF16), a Bool tensor, or a signed dtype branch not covered by the match; dividing or taking remainder of u64 values where the call site must special-case.

Common situations: Mixing float and int arithmetic expecting promotion; bitwise ops on bool tensors; using int_* entry points on float counters or indices converted from float.

Related errors


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