tracel-ai/burn · error

Expected bool data type, got {dtype:?}

Error message

Expected bool data type, got {dtype:?}

What it means

Bool::empty in the tensor bridge validates that the requested DType is Bool before creating an empty bool tensor; any other dtype panics. This is an internal consistency guard: the bool bridge implementation must only ever be asked for bool tensors, so receiving a non-bool dtype is a dispatch/typing bug.

Source

Thrown at crates/burn-tensor/src/bridge/ops/bool.rs:24

use burn_dispatch::Dispatch;
use burn_std::{DType, ExecutionError, IndexingUpdateOp, Shape, Slice};

use crate::{
    Bool, Device,
    bridge::{BasicAutodiffOps, BasicOps, TransactionOp},
    ops::BridgeTensor,
};

impl TransactionOp for Bool {
    fn register_transaction(tr: &mut TransactionPrimitive<Dispatch>, tensor: BridgeTensor) {
        tr.register_bool(tensor.into());
    }
}

impl BasicOps for Bool {
    fn empty(shape: Shape, device: &Device, dtype: DType) -> BridgeTensor {
        if !dtype.is_bool() {
            panic!("Expected bool data type, got {dtype:?}");
        }
        BridgeTensor::bool(Dispatch::bool_empty(
            shape,
            device.as_dispatch(),
            dtype.into(),
        ))
    }

    fn zeros(shape: Shape, device: &Device, dtype: DType) -> BridgeTensor {
        if !dtype.is_bool() {
            panic!("Expected bool data type, got {dtype:?}");
        }
        BridgeTensor::bool(Dispatch::bool_zeros(
            shape,
            device.as_dispatch(),
            dtype.into(),
        ))
    }

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Pass DType::BOOL (or no explicit dtype, letting Bool default to it) when creating boolean tensors via Tensor::<B, D, Bool>::empty
  2. Audit generic code that threads a DType through tensor constructors and ensure it matches the element type parameter E
  3. If you need an integer/float tensor, use Tensor::<B, D, Float or Int>::empty instead of the Bool kind

Example fix

// before
let t = Tensor::<B, 2, Bool>::empty([2, 2], &device); // if DType got set to Float32 upstream
let t = Tensor::<B, 2, Bool>::empty_dtype(&device, DType::BOOL, [2, 2]);
// after: ensure dtype is DType::BOOL when constructing Bool tensors
Defensive patterns

Strategy: validation

Validate before calling

fn create_bool_empty_checked<B: Backend, const D: usize>(shape: [usize; D], device: &B::Device, dtype: DType) -> Tensor<B, D, Bool> {
    assert!(dtype.is_bool(), "Bool::empty requires DType::BOOL, got {dtype:?}");
    Tensor::<B, D, Bool>::empty(shape, device)
}

Type guard

fn is_bool_dtype(dtype: DType) -> bool { dtype.is_bool() }

Try / catch

std::panic::catch_unwind(|| Tensor::<B, 2, Bool>::empty(shape, &device))
    .map_err(|_| "dtype passed to Bool::empty was not Bool")?;

Prevention

When it happens

Trigger: Calling Tensor::<B, D, Bool>::empty(shape, device) — or any API resolving to BasicOps::empty on the Bool element type — with a dtype that is not Bool (e.g. via a generic helper that passes Float32/Int64 as the dtype).

Common situations: Generic tensor-building code parameterized over dtype that constructs Bool tensors with the wrong DType; a dispatch bug after changing dtype handling in the bridge; hand-written code that mixes the float/int/bool tensor kinds.

Related errors


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