tracel-ai/burn · error

Should be bool, got int

Error message

Should be bool, got int

What it means

`BackendTensor::bool()` unwraps the enum variant holding a bool tensor primitive and returns it. Because the enum can hold Float, Int, Quantized, or Autodiff variants, calling `bool()` on a tensor that is actually an Int tensor cannot return a value, so the library panics with 'Should be bool, got int'. It is a guard against mis-typed tensor access in the dispatch layer.

Source

Thrown at crates/burn-dispatch/src/tensor.rs:77

    /// 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 {

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Check the tensor's dtype/variant before calling `bool()` and convert explicitly (e.g. cast int -> bool via a comparison or `bool_cast` op) first.
  2. Match on `BackendTensor` yourself and handle each variant so the wrong dtype is handled gracefully.
  3. Trace upstream op calls to fix the source that produced an Int tensor where a Bool tensor was expected.

Example fix

// before
let b = tensor.bool(); // panics: got int
// after
let b = match tensor {
    BackendTensor::Bool(t) => t,
    BackendTensor::Int(t) => t.equal_elem(0).bool(), // explicit conversion path
    other => panic!("unexpected variant"),
};
Defensive patterns

Strategy: type-guard

Validate before calling

if !matches!(tensor, BackendTensor::Bool(_)) { /* handle wrong dtype before calling .bool() */ }

Type guard

fn is_bool_tensor<B: Backend>(t: &BackendTensor<B>) -> bool {
    matches!(t, BackendTensor::Bool(_))
}

Try / catch

// Rust panics are not catchable in stable without panic::catch_unwind
let result = std::panic::catch_unwind(AssertUnwindSafe(|| tensor.clone().bool()));
match result {
    Ok(b) => use_bool(b),
    Err(_) => eprintln!("tensor was not bool"),
}

Prevention

When it happens

Trigger: Calling `BackendTensor::bool()` on a wrapper whose inner variant is `BackendTensor::Int(_)` — e.g. retrieving a tensor result from a kernel/op dispatch assuming it is bool when the op produced an integer tensor.

Common situations: Developers mixing up dtype when reading op outputs (e.g. treating an `int` mask/comparison result as bool), or generic code that downcasts tensors without checking the variant first.

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/99c6636c42e9756e. Report an issue: GitHub.