tracel-ai/burn · error

Unsupported dtype for `bool_from_data`

Error message

Unsupported dtype for `bool_from_data`

What it means

LibTorch's bool_from_data in burn-tch only supports TensorData whose dtype is Bool(Native); any other dtype falls into a catch-all unimplemented!(). It converts tensor data into a torch bool tensor and simply does not handle casts from other element types.

Source

Thrown at crates/burn-tch/src/ops/bool_tensor.rs:19

use super::TchOps;
use crate::IntoKind;
use crate::{LibTorch, LibTorchDevice, TchShape, TchTensor};
use burn_backend::BoolStore;
use burn_backend::ExecutionError;
use burn_backend::IntDType;
use burn_backend::Scalar;
use burn_backend::tensor::BoolTensor;
use burn_backend::tensor::IntTensor;
use burn_backend::{BoolDType, FloatDType};
use burn_backend::{Shape, TensorData, TensorMetadata, ops::BoolTensorOps};

impl BoolTensorOps<Self> for LibTorch {
    fn bool_from_data(data: TensorData, device: &LibTorchDevice) -> TchTensor {
        match data.dtype {
            burn_backend::DType::Bool(BoolStore::Native) => {
                TchTensor::from_data::<bool>(data, (*device).into())
            }
            _ => unimplemented!("Unsupported dtype for `bool_from_data`"),
        }
    }

    fn bool_repeat_dim(tensor: TchTensor, dim: usize, times: usize) -> TchTensor {
        TchOps::repeat_dim(tensor, dim, times)
    }

    async fn bool_into_data(tensor: TchTensor) -> Result<TensorData, ExecutionError> {
        let shape = tensor.shape();
        let tensor = Self::bool_reshape(tensor.clone(), Shape::new([shape.num_elements()]));
        let values: Result<Vec<bool>, tch::TchError> = tensor.tensor.shallow_clone().try_into();
        Ok(TensorData::new(values.unwrap(), shape))
    }

    fn bool_to_device(tensor: TchTensor, device: &LibTorchDevice) -> TchTensor {
        TchOps::to_device(tensor, device)
    }

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Convert the data to a native bool dtype before constructing the tensor (TensorData::convert::<bool>())
  2. Create the tensor with the original dtype and cast afterwards via tensor.cast::<bool>() or .to_dtype()
  3. Check data.dtype with a type guard before the call
  4. File/patch burn-tch to reinterpret non-native bool storage instead of panicking

Example fix

// before
let mask = Tensor::<LibTorch, _, _>::from_data(data_u8, &device);

// after
let mask = Tensor::<LibTorch, _, _>::from_data(
    data_u8.convert::<bool>(), &device
);
Defensive patterns

Strategy: validation

Validate before calling

if data.dtype != burn_backend::DType::Bool(BoolStore::Native) {
    data = data.convert::<bool>();
}
let mask = Tensor::<LibTorch, D, Bool>::from_data(data, &device);

Type guard

fn is_native_bool(dtype: burn_backend::DType) -> bool {
    dtype == burn_backend::DType::Bool(BoolStore::Native)
}

Prevention

When it happens

Trigger: Calling tensor creation APIs that route to bool_from_data (e.g. Tensor::from_data into a bool tensor on the LibTorch backend) with data whose dtype is not burn_backend::DType::Bool(BoolStore::Native).

Common situations: Passing byte/u8-backed boolean masks (common from file formats or numpy arrays) into LibTorch-backed bool tensors; differences in how bools are stored across backends or serialized records.

Related errors


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