tracel-ai/burn · error

Expected int handle, got {}

Error message

Expected int handle, got {}

What it means

`NdArray::int_tensor` (BackendIr) panics when the given TensorHandle wraps a `HandleKind` other than `HandleKind::Int`. The runtime uses it to materialize integer tensor primitives; a Float/Bool/Quantized handle reaching it is a type-erased handle mismatch between the declared tensor kind and the stored resource.

Source

Thrown at crates/burn-ndarray/src/backend.rs:144

    }

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

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

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

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

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

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

    fn float_tensor_handle(tensor: FloatTensor<Self>) -> Self::Handle {

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Cast the tensor to an integer dtype in the frontend before the op (`tensor.int()` / `cast(DType::I64)`).
  2. Call the matching accessor (`float_tensor`, `bool_tensor`, `quantized_tensor`) for the handle's actual kind in custom backend code.
  3. Verify the TensorKind in the TensorIr/TensorDescription matches the resource stored in the handle store.
  4. Trace where the tensor was created and ensure it was registered as an int tensor.

Example fix

// before
let ids = NdArray::int_tensor(float_handle); // panics
// after
let ids = NdArray::int_tensor(int_handle); // or: let f = NdArray::float_tensor(float_handle);
Defensive patterns

Strategy: type-guard

Validate before calling

fn ensure_int(t: &TensorIr) -> Result<(), String> {
    (t.kind == TensorKind::Int && matches!(t.dtype, DType::I8 | DType::I16 | DType::I32 | DType::I64))
        .then_some(())
        .ok_or_else(|| format!("expected int tensor, got kind {:?} dtype {:?}", t.kind, t.dtype))
}

Type guard

fn as_int_handle(h: HandleKind<NdArray>) -> Option<ArrayHandle> {
    match h { HandleKind::Int(a) => Some(a), _ => None }
}

Try / catch

let i = std::panic::catch_unwind(|| NdArray::int_tensor(th))
    .map_err(|_| anyhow!("handle is not int; expected {}", th.handle.name()))?;

Prevention

When it happens

Trigger: Calling `NdArray::int_tensor(handle)` (or `get_int_tensor`) with a handle registered as Float, Bool, or Quantized — e.g. passing a float tensor to an integer op.

Common situations: Frontend op expecting an int tensor receives a float one (missing cast before `topk` indices, arange, embedding ids); custom IR code registering tensors with the wrong TensorKind; version mismatch where a backend changed handle wrapping.

Related errors


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