tracel-ai/burn · error
Should be float, got bool
Error message
Should be float, got bool
What it means
BackendTensor::float() panics with this message when called on a Bool tensor. The method only unwraps the Float variant; boolean tensors (from comparisons/logical ops) must be converted to a numeric dtype before float extraction, so receiving one indicates a dtype-handling bug.
Source
Thrown at crates/burn-dispatch/src/tensor.rs:42
Float(B::FloatTensorPrimitive),
/// Int tensor handle.
Int(B::IntTensorPrimitive),
/// Bool tensor handle.
Bool(B::BoolTensorPrimitive),
/// Quantized tensor handle.
Quantized(B::QuantizedTensorPrimitive),
#[cfg(feature = "autodiff")]
/// Autodiff float tensor handle.
Autodiff(FloatTensor<Autodiff<B>>),
}
impl<B: Backend> BackendTensor<B> {
/// Returns the inner float tensor primitive.
pub fn 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 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.View on GitHub (pinned to d16f7ba2ed)
Solutions
- Cast the Bool tensor to float first (bool -> {0.0, 1.0} cast op) before calling .float().
- Match on the BackendTensor variant and convert Bool explicitly with a numeric cast.
- If the tensor should never be bool, fix the upstream op producing it (e.g., use a numeric comparison result instead of a mask).
- Use checked accessors like as_float() with a variant check for mixed-dtype paths.
Example fix
// before let f = mask_tensor.float(); // panics: Bool // after let f = BackendTensor::Float(bool_to_float_cast::<B>(mask_bool)).float(); // or mask_bool.cast::<f32>() upstream
Defensive patterns
Strategy: type-guard
Validate before calling
// before calling .float()
if let BackendTensor::Bool(_) = &tensor {
// cast bool to float first
} Type guard
fn is_bool_tensor<B: Backend>(t: &BackendTensor<B>) -> bool {
matches!(t, BackendTensor::Bool(_))
} Try / catch
// guard rather than catch the panic
let f = match tensor {
BackendTensor::Bool(b) => BackendTensor::Float(bool_cast::<B>(b)),
BackendTensor::Float(f) => f,
other => bail!("expected float or bool, got {other:?}"),
}; Prevention
- Cast boolean masks to numeric dtype before arithmetic.
- Track mask-producing ops (comparisons, logical ops) and convert them at the boundary.
- Use typed tensor APIs where the dtype is encoded in the type to get compile-time errors instead.
When it happens
Trigger: Calling BackendTensor::float() on a tensor holding BackendTensor::Bool(_), typically the result of comparison (==, <, >), logical ops, or mask creation flowing into float-only code.
Common situations: Using boolean masks as if they were float tensors (e.g., multiplying without casting); APIs whose signatures changed to accept generic BackendTensor where callers pass masks directly.
Related errors
- Should be float, got int
- Should be float, got quantized
- Should be float, got autodiff
- float_into_int: unsupported source dtype {:?}
- float_gather: unsupported dtype {:?}
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/50908bf62df5f975.
Report an issue: GitHub.