tracel-ai/burn · error
Should be int, got bool
Error message
Should be int, got bool
What it means
BackendTensor::int() was called on a Bool tensor handle. Only the Int variant yields an int primitive, so the code panics. A boolean tensor (typically a mask or comparison result) reached a path expecting integer values.
Source
Thrown at crates/burn-dispatch/src/tensor.rs:65
}
/// Returns the inner float tensor primitive.
pub fn as_float(&self) -> &B::FloatTensorPrimitive {
match self {
BackendTensor::Float(tensor) => tensor,
BackendTensor::Int(_) => panic!("Should be float, got int"),
BackendTensor::Bool(_) => panic!("Should be float, got bool"),
BackendTensor::Quantized(_) => panic!("Should be float, got quantized"),
#[cfg(feature = "autodiff")]
BackendTensor::Autodiff(_) => panic!("Should be float, got autodiff"),
}
}
/// Returns the inner int tensor primitive.
pub fn int(self) -> B::IntTensorPrimitive {
match self {
BackendTensor::Int(tensor) => tensor,
BackendTensor::Float(_) => panic!("Should be int, got float"),
BackendTensor::Bool(_) => panic!("Should be int, got bool"),
BackendTensor::Quantized(_) => panic!("Should be int, got quantized"),
#[cfg(feature = "autodiff")]
BackendTensor::Autodiff(_) => panic!("Should be int, got autodiff"),
}
}
/// Returns the inner bool tensor primitive.
pub fn bool(self) -> B::BoolTensorPrimitive {
match self {
BackendTensor::Bool(tensor) => tensor,
BackendTensor::Float(_) => panic!("Should be bool, got float"),
BackendTensor::Int(_) => panic!("Should be bool, got int"),
BackendTensor::Quantized(_) => panic!("Should be bool, got quantized"),
#[cfg(feature = "autodiff")]
BackendTensor::Autodiff(_) => panic!("Should be bool, got autodiff"),
}
}
View on GitHub (pinned to d16f7ba2ed)
Solutions
- Convert the bool tensor to int explicitly (bool_tensor.int() via the backend cast op) before the int-only call
- Confirm the upstream op actually outputs Int rather than Bool
- Match the BackendTensor variant at the call site and handle Bool deliberately (e.g. where/select semantics)
- Validate dtype via TensorMetadata at the boundary
Example fix
// before let idx = mask_handle.int(); // panics: Bool variant // after let idx = mask_handle.bool().int(); // explicit bool -> int cast (0/1 values)
Defensive patterns
Strategy: type-guard
Validate before calling
if matches!(handle, BackendTensor::Bool(_)) {
let idx = handle.bool().int(); // explicit 0/1 conversion
} Type guard
fn is_int<B: BackendTypes>(t: &BackendTensor<B>) -> bool {
matches!(t, BackendTensor::Int(_))
} Prevention
- Convert masks to ints explicitly when used as numeric values
- Confirm the producing op outputs Int (comparison ops output Bool)
- Never rely on implicit bool-to-int promotion
When it happens
Trigger: Calling int() on a bool tensor from comparison ops (==, <, >) or logical ops; using a bool mask where integer indices/values are required; custom code treating bools as 0/1 ints without conversion.
Common situations: Using a comparison result as an index directly; PyTorch-style implicit bool->int promotion assumptions; passing mask tensors into ops implemented only for int primitives.
Related errors
- Should be bool, got float
- Should be float, got bool
- Should be int, got float
- Should be int, got quantized
- Should be int, got autodiff
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/22b3138ab37c2929.
Report an issue: GitHub.