tracel-ai/burn · error

Unsupported dtype for `bool_from_data`

Error message

Unsupported dtype for `bool_from_data`

What it means

burn-ndarray's `bool_from_data` validates that incoming `TensorData` has a bool dtype before constructing the tensor; anything else (int, float, uint) panics with `unimplemented!("Unsupported dtype for `bool_from_data`")`. The ndarray backend does not implicitly convert data types on tensor creation.

Source

Thrown at crates/burn-ndarray/src/ops/bool_tensor.rs:28

};
use burn_std::{BoolDType, FloatDType, IntDType};
use ndarray::IntoDimension;

// Current crate
use crate::{NdArray, execute_with_int_dtype, tensor::NdArrayTensor};
use crate::{
    NdArrayDevice, SharedArray, execute_with_float_out_dtype, execute_with_int_out_dtype, slice,
};

// Workspace crates
use burn_backend::{Shape, TensorData};

use super::{NdArrayBoolOps, NdArrayOps};

impl BoolTensorOps<Self> for NdArray {
    fn bool_from_data(data: TensorData, _device: &NdArrayDevice) -> NdArrayTensor {
        if !data.dtype.is_bool() {
            unimplemented!("Unsupported dtype for `bool_from_data`")
        }
        NdArrayTensor::from_data(data)
    }

    async fn bool_into_data(tensor: NdArrayTensor) -> Result<TensorData, ExecutionError> {
        Ok(tensor.into_data())
    }

    fn bool_to_device(tensor: NdArrayTensor, _device: &NdArrayDevice) -> NdArrayTensor {
        tensor
    }

    fn bool_reshape(tensor: NdArrayTensor, shape: Shape) -> NdArrayTensor {
        NdArrayOps::reshape(tensor.bool(), shape).into()
    }

    fn bool_slice(tensor: NdArrayTensor, slices: &[burn_backend::Slice]) -> NdArrayTensor {
        slice!(tensor, slices)

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Convert the data to bool before calling: `data.convert::<bool>()`
  2. On the Tensor API, cast first: `tensor.bool()` / `tensor.cast(DType::Bool)` then take data
  3. Fix the data producer so masks are stored as bool
  4. Explicitly compare/re-derive the mask with bool ops instead of converting raw data

Example fix

// before
let t = Tensor::<NdArray, 2, Bool>::from_data(data_u8, &device);
// after
let t = Tensor::<NdArray, 2, Bool>::from_data(data_u8.convert::<bool>(), &device);
Defensive patterns

Strategy: validation

Validate before calling

if !data.dtype.is_bool() {
    data = data.convert::<bool>();
}
let t = Tensor::<NdArray, 2, Bool>::from_data(data, &device);

Type guard

fn is_bool_data(data: &TensorData) -> bool {
    data.dtype.is_bool()
}

Try / catch

// panic is unrecoverable; normalize data before the API call
let safe_data = if is_bool_data(&data) { data } else { data.convert::<bool>() };
let t = Tensor::<NdArray, 2, Bool>::from_data(safe_data, &device);

Prevention

When it happens

Trigger: Creating a bool tensor from data via `bool_from_data` / `Tensor::<NdArray,_,Bool>::from_data(...)` where the TensorData dtype is not Bool, e.g. data loaded from a file or produced by an int/float op.

Common situations: Loading masks from numpy/pickle files saved as uint8; building tensors from raw buffers whose dtype metadata is I32 or F32; backend migration where another backend auto-converted.

Related errors


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