tracel-ai/burn · error
Should be bool, got quantized
Error message
Should be bool, got quantized
What it means
`BackendTensor::bool()` extracts the inner bool tensor primitive from the `BackendTensor` enum. When the wrapper actually holds a quantized tensor, no bool primitive exists to return, so the code panics with 'Should be bool, got quantized'. This prevents silently reading a quantized payload as a bool tensor.
Source
Thrown at crates/burn-dispatch/src/tensor.rs:78
/// 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"),
}
}
/// Returns the inner quantized tensor primitive.
pub fn quantized(self) -> B::QuantizedTensorPrimitive {
match self {
BackendTensor::Quantized(tensor) => tensor,
_ => unreachable!(),
}
}
#[cfg(feature = "autodiff")]
/// Returns the inner autodiff tensor primitive.
pub fn autodiff(self) -> FloatTensor<Autodiff<B>> {
match self {
BackendTensor::Autodiff(tensor) => tensor,View on GitHub (pinned to d16f7ba2ed)
Solutions
- Dequantize the quantized tensor (or recompute the op in bool dtype) before accessing it as bool.
- Match on `BackendTensor` variants and reject/handle the Quantized case explicitly.
- Fix the upstream op dispatch so the operation produces a bool tensor instead of a quantized one.
Example fix
// before let b = qtensor.bool(); // panics: got quantized // after let float = qtensor.quantized().dequantize(); let b = float.greater_elem(0); // bool tensor from a proper op
Defensive patterns
Strategy: type-guard
Validate before calling
if !matches!(tensor, BackendTensor::Bool(_)) { /* dequantize or recompute before .bool() */ } Type guard
fn is_bool_tensor<B: Backend>(t: &BackendTensor<B>) -> bool {
matches!(t, BackendTensor::Bool(_))
} Try / catch
let result = std::panic::catch_unwind(AssertUnwindSafe(|| tensor.clone().bool()));
match result {
Ok(b) => use_bool(b),
Err(_) => eprintln!("tensor was quantized, not bool"),
} Prevention
- Keep quantized tensors in dedicated types; never route them through generic bool accessors.
- Dequantize immediately after quantized kernels and re-tag dtypes.
- Audit quantized inference pipelines for downstream consumers assuming bool outputs.
When it happens
Trigger: Calling `BackendTensor::bool()` on a wrapper whose inner variant is `BackendTensor::Quantized(_)` — typically when pulling results from a quantized kernel or reinterpreting a quantized tensor output as bool.
Common situations: Working with quantized inference backends where op outputs are quantized tensors but downstream code (e.g. mask/condition handling) assumes bool; often an upstream op-selection or dtype-planning mistake.
Related errors
- Should be float, got quantized
- Should be int, got quantized
- Should be bool, got int
- Should be bool, got autodiff
- Expected quantized handle, got {}
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/9861408cc9a11f65.
Report an issue: GitHub.