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
- Convert the data to a native bool dtype before constructing the tensor (TensorData::convert::<bool>())
- Create the tensor with the original dtype and cast afterwards via tensor.cast::<bool>() or .to_dtype()
- Check data.dtype with a type guard before the call
- 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
- Always convert TensorData to bool before creating bool tensors on LibTorch
- Check dtype when loading masks from files/numpy (often u8-backed)
- Add dtype assertions at deserialization boundaries
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
- Unsupported dtype for `int_from_data`: {:?}
- int_scatter with {other:?} update is not implemented
- int_select_assign with {other:?} update is not implemented
- Operation not marked for autodiff.
- Cannot move a tensor from a capture device
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/0bc15f20182953d9.
Report an issue: GitHub.