tracel-ai/burn · error

Should be bool, got autodiff

Error message

Should be bool, got autodiff

What it means

`BackendTensor::bool()` extracts the inner `Bool` variant's primitive. When autodiff is enabled and the wrapper holds an `Autodiff(_)` variant instead, there is no bool primitive available, so the code panics with 'Should be bool, got autodiff'. The variant only exists when the `autodiff` feature is compiled in.

Source

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

        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,
            // NOTE: this is the panicking code reached in tensor.rs:74:18:
            _ => unreachable!(),

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Strip the autodiff wrapper (e.g. take the inner primitive via the autodiff API) and access the bool tensor through the appropriate method for that primitive.
  2. Disable/avoid the autodiff path (run in a non-autodiff context) if only bool results are needed.
  3. Match on all `BackendTensor` variants including Autodiff and handle the bool extraction correctly.

Example fix

// before
let b = tensor.bool(); // panics: got autodiff
// after
let b = match tensor {
    BackendTensor::Bool(t) => t,
    BackendTensor::Autodiff(t) => t.inner().bool(), // unwrap autodiff first
    other => panic!("unexpected variant"),
};
Defensive patterns

Strategy: type-guard

Validate before calling

if !matches!(tensor, BackendTensor::Bool(_)) { /* unwrap autodiff or convert 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 autodiff-wrapped, not bool"),
}

Prevention

When it happens

Trigger: Calling `BackendTensor::bool()` on a wrapper whose inner variant is `BackendTensor::Autodiff(_)` — typically when reading tensor results inside an autodiff-enabled session/graph where floats are wrapped in the autodiff variant.

Common situations: Running training or gradient code with the autodiff feature enabled and expecting plain bool primitives from ops; often after switching on the autodiff feature or migrating inference code to training code.

Related errors


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