tracel-ai/burn · error

Expected bool tensor, got {:?}

Error message

Expected bool tensor, got {:?}

What it means

Type guard in `NdArrayTensor::bool`: a caller invoked a boolean tensor operation (and/or/not/equal/slice_assign/reshape access path) on storage whose dtype is not Bool; the ndarray backend cannot reinterpret non-boolean storage as a bool array, so it panics.

Source

Thrown at crates/burn-ndarray/src/tensor.rs:41

    F64(NdArrayStorage<f64>),
    F32(NdArrayStorage<f32>),
    I64(NdArrayStorage<i64>),
    I32(NdArrayStorage<i32>),
    I16(NdArrayStorage<i16>),
    I8(NdArrayStorage<i8>),
    U64(NdArrayStorage<u64>),
    U32(NdArrayStorage<u32>),
    U16(NdArrayStorage<u16>),
    U8(NdArrayStorage<u8>),
    Bool(NdArrayStorage<bool>),
}

impl NdArrayTensor {
    /// Extract bool array, converting to owned if necessary.
    pub(crate) fn bool(self) -> SharedArray<bool> {
        match self {
            NdArrayTensor::Bool(storage) => storage.into_shared(),
            _ => unimplemented!("Expected bool tensor, got {:?}", self.dtype()),
        }
    }

    /// Returns true if this tensor uses borrowed (zero-copy) storage.
    #[inline]
    pub fn is_borrowed(&self) -> bool {
        macro_rules! check {
            ($($variant:ident),*) => {
                match self {
                    $(NdArrayTensor::$variant(s) => s.is_borrowed(),)*
                }
            };
        }
        check!(F64, F32, I64, I32, I16, I8, U64, U32, U16, U8, Bool)
    }
}

pub(crate) fn cast_to_dtype<E1: Element>(array: SharedArray<E1>, dtype: DType) -> NdArrayTensor

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Ensure the tensor was created with DType::Bool before applying boolean ops
  2. Explicitly cast/convert the tensor to bool (e.g., via a comparison op)
  3. Check the dtype reported in the message and trace where the tensor was produced
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at crates/burn-ndarray/src/tensor.rs:41 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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