tracel-ai/burn · error
Expected float handle, got {}
Error message
Expected float handle, got {} What it means
`NdArray::float_tensor` (BackendIr) unwraps a type-erased `HandleKind` and panics if the variant is not `HandleKind::Float`. It is called when the burn runtime converts a tensor handle into the NdArray float tensor primitive; receiving a non-float handle means the runtime handed a float-typed operation a tensor registered as int, bool, or quantized.
Source
Thrown at crates/burn-ndarray/src/backend.rs:137
}
}
}
}
fn device_count(_: u16) -> usize {
1
}
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> {View on GitHub (pinned to d16f7ba2ed)
Solutions
- Check the dtype/kind of the tensor being passed; convert with `.float()` / `cast(DType::F32)` in the frontend before the op.
- Fix the `TensorKind` used when registering the tensor (TensorDescription) so it matches the actual handle variant.
- In custom BackendIr code, ensure you call `int_tensor`/`bool_tensor`/`quantized_tensor` for their respective kinds instead of `float_tensor`.
- If the mix comes from a burn-ir graph, dump the graph (tensor descriptions) and find the op whose output kind is wrong.
Example fix
// before let out = NdArray::float_tensor(handle_of_int_tensor); // panics // after let out = NdArray::int_tensor(handle_of_int_tensor); // or cast the tensor to float first
Defensive patterns
Strategy: type-guard
Validate before calling
fn ensure_float<B: BackendIr>(t: &TensorIr) -> Result<(), String> {
(t.kind == TensorKind::Float && matches!(t.dtype, DType::F32 | DType::F64))
.then_some(())
.ok_or_else(|| format!("expected float tensor, got kind {:?} dtype {:?}", t.kind, t.dtype))
} Type guard
fn as_float_handle(h: HandleKind<NdArray>) -> Option<ArrayHandle> {
match h { HandleKind::Float(a) => Some(a), _ => None }
} Try / catch
let f = std::panic::catch_unwind(|| NdArray::float_tensor(th))
.map_err(|_| anyhow!("handle is not float; expected {}", th.handle.name()))?; Prevention
- Match the accessor to the tensor kind: float_tensor/int_tensor/bool_tensor/quantized_tensor
- Cast frontend tensors to float before float-only ops
- Verify TensorKind in TensorIr matches the stored HandleKind variant
- Dump the graph's tensor descriptions when a mismatch appears to find the mis-registered op
When it happens
Trigger: Calling `NdArray::float_tensor(handle)` (or `HandleStore::get_float_tensor`, or float tensor ops via burn-ir graph execution) with a TensorIr/handle whose registered kind is Int, Bool, or Quantized.
Common situations: Mixing integer and float tensors in a frontend op that assumes float; a mis-typed graph in custom IR code passing an int tensor where the descriptor says float; wrong `TensorKind` used when registering the tensor in the handle store.
Related errors
- Expected int handle, got {}
- Expected bool handle, got {}
- Expected quantized handle, got {}
- float_storage_as_f32: unsupported dtype {:?}
- conv1d: unsupported dtype {:?}
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/9f7b92a7729701c5.
Report an issue: GitHub.