tracel-ai/burn · error

Expected int handle, got {}

Error message

Expected int handle, got {}

What it means

`BackendIr::int_tensor` for the Flex backend unwraps a `TensorHandle` expecting `HandleKind::Int`. Any other handle kind causes a panic with 'Expected int handle, got <name>'. It is a boundary check so int ops never silently receive foreign handle types.

Source

Thrown at crates/burn-flex/src/backend.rs:183

    }

    fn flush(_device: &Self::Device) {}
}

impl BackendIr for Flex {
    type Handle = HandleKind<Self>;

    fn float_tensor(handle: TensorHandle<Self::Handle>) -> FlexTensor {
        match handle.handle {
            HandleKind::Float(t) => t,
            _ => panic!("Expected float handle, got {}", handle.handle.name()),
        }
    }

    fn int_tensor(handle: TensorHandle<Self::Handle>) -> FlexTensor {
        match handle.handle {
            HandleKind::Int(t) => t,
            _ => panic!("Expected int handle, got {}", handle.handle.name()),
        }
    }

    fn bool_tensor(handle: TensorHandle<Self::Handle>) -> FlexTensor {
        match handle.handle {
            HandleKind::Bool(t) => t,
            _ => panic!("Expected bool handle, got {}", handle.handle.name()),
        }
    }

    fn quantized_tensor(handle: TensorHandle<Self::Handle>) -> FlexQTensor {
        match handle.handle {
            HandleKind::Quantized(t) => t,
            _ => panic!("Expected quantized handle, got {}", handle.handle.name()),
        }
    }

    fn float_tensor_handle(tensor: FlexTensor) -> Self::Handle {

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Make the producing kernel return `HandleKind::Int` for int ops.
  2. Match on the handle kind at the call site and convert (e.g. cast float -> int via a proper op) instead of panicking.
  3. Verify the op registration maps the int op to a kernel with integer output dtype.

Example fix

// before
let t = BackendIr::int_tensor(handle); // panics: got float
// after
let t = match handle.handle {
    HandleKind::Int(t) => t,
    HandleKind::Float(f) => f.cast_to_int(), // explicit conversion
    other => panic!("unexpected handle {}", other.name()),
};
Defensive patterns

Strategy: type-guard

Validate before calling

if !matches!(handle.handle, HandleKind::Int(_)) { /* route to correct converter */ }

Type guard

fn as_int_handle(t: HandleKind<Flex>) -> Option<FlexTensor> {
    match t { HandleKind::Int(i) => Some(i), _ => None }
}

Try / catch

let result = std::panic::catch_unwind(AssertUnwindSafe(|| Flex::int_tensor(handle.clone())));
match result {
    Ok(t) => use_int(t),
    Err(_) => eprintln!("handle was not int"),
}

Prevention

When it happens

Trigger: Calling `int_tensor()` on a handle produced by a float/bool/quantized op — e.g. dispatch routed an int op's result from a kernel that yielded a Float handle, or the handle was built with `HandleKind::Float`.

Common situations: Custom Flex kernels returning float results for integer ops (e.g. index/argmax kernels accidentally float), or after refactors that renamed/moved handle constructors.

Related errors


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