tracel-ai/burn · error
Should be float, got int
Error message
Should be float, got int
What it means
BackendTensor::float() extracts the inner float primitive from a BackendTensor, and this panic fires when the tensor is actually an Int tensor. Downcasting to the wrong dtype variant is a programming error, so burn panics with a message naming the actual variant received.
Source
Thrown at crates/burn-dispatch/src/tensor.rs:41
/// Float tensor handle.
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"),
}
}
View on GitHub (pinned to d16f7ba2ed)
Solutions
- Convert the Int tensor to float explicitly with the backend's float cast op before calling .float().
- Match on the BackendTensor variant first and handle Int (cast or error) instead of blindly calling .float().
- Trace where the Int tensor originates (arange, nonzero, casts) and fix the upstream dtype.
- Use as_float() or a checked accessor when the dtype may legitimately vary.
Example fix
// before
let f = tensor.float(); // panics if tensor is Int
// after
let f = match tensor {
BackendTensor::Int(i) => BackendTensor::Float(int_to_float_cast::<B>(i)).float(),
t => t.float(),
}; Defensive patterns
Strategy: type-guard
Validate before calling
// before calling .float()
if let BackendTensor::Int(_) = &tensor {
// cast to float first or handle error
} Type guard
fn as_float_tensor<B: Backend>(t: BackendTensor<B>) -> Option<B::FloatTensorPrimitive> {
match t {
BackendTensor::Float(f) => Some(f),
_ => None,
}
} Try / catch
// panics cannot be caught idiomatically in Rust; guard instead
let f = match tensor {
BackendTensor::Float(f) => f,
other => int_to_float_cast::<B>(other_tensor_to_int(other)),
}; Prevention
- Prefer matching on BackendTensor variants over calling .float() directly in generic code.
- Explicitly cast Int tensors (arange, indices) to float before float-only APIs.
- Run dtype-sensitive tests after refactors that change an op's output dtype.
When it happens
Trigger: Calling BackendTensor::float() (public) on a value holding BackendTensor::Int(_), e.g., after ops that produce integer tensors (arange, indexing, comparisons cast to int) being fed to float-only APIs.
Common situations: Passing integer tensors (indices, masks from integer ops, counters) into code that unconditionally calls .float() on a generic BackendTensor; dtype changes in refactors where an op's output switched from float to int.
Related errors
- an enabled float tensor must use an autodiff primitive
- Should be float, got bool
- Should be float, got quantized
- Should be float, got autodiff
- float_into_int: unsupported source dtype {:?}
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/eca7458be04baf4b.
Report an issue: GitHub.