tracel-ai/burn · error
Unsupported dtype for `float_from_data`
Error message
Unsupported dtype for `float_from_data`
What it means
burn-tch's `float_from_data` converts TensorData into a LibTorch float tensor, but only F64, F32, F16 and BF16 are handled. Any other dtype (integer, bool, quantized) falls into the `_` arm and panics with this message.
Source
Thrown at crates/burn-tch/src/ops/tensor.rs:17
use super::TchOps;
use crate::{IntoKind, LibTorch, LibTorchDevice, TchShape, TchTensor};
use burn_backend::backend::ExecutionError;
use burn_backend::tensor::{BoolTensor, FloatTensor, IntTensor};
use burn_backend::{BoolDType, IntDType, Scalar, bf16, f16};
use burn_backend::{
DType, Distribution, FloatDType, Shape, TensorData, TensorMetadata, ops::FloatTensorOps,
};
impl FloatTensorOps<Self> for LibTorch {
fn float_from_data(data: TensorData, device: &LibTorchDevice) -> TchTensor {
match data.dtype {
DType::F64 => TchTensor::from_data::<f64>(data, (*device).into()),
DType::F32 => TchTensor::from_data::<f32>(data, (*device).into()),
DType::F16 => TchTensor::from_data::<f16>(data, (*device).into()),
DType::BF16 => TchTensor::from_data::<bf16>(data, (*device).into()),
_ => unimplemented!("Unsupported dtype for `float_from_data`"),
}
}
fn float_random(
shape: Shape,
distribution: Distribution,
device: &LibTorchDevice,
dtype: FloatDType,
) -> TchTensor {
match distribution {
Distribution::Default => {
let mut tensor = TchTensor::empty(shape, *device, dtype.into());
tensor
.mut_ops(|tensor| tensor.rand_like_out(tensor))
.unwrap()
}
Distribution::Bernoulli(prob) => {
let mut tensor = TchTensor::empty(shape, *device, dtype.into());View on GitHub (pinned to d16f7ba2ed)
Solutions
- Convert the data to a float dtype before calling from_data (e.g. map the Vec to f32 and rebuild TensorData)
- Fix the producer/exporter so the TensorData carries F32/F16/BF16/F64
- Use the appropriate integer tensor constructor (int_from_data) instead of the float one
- Add an explicit dtype check with a clear error before the call
Example fix
// before let data = TensorData::new(vec![1i64, 2, 3]); let t = Tensor::<B, 1>::from_data(data.convert::<f32>(), &device); // panics only if not converted; ensure conversion: // after let t = Tensor::<B, 1>::from_data(data.convert::<f32>(), &device);
Defensive patterns
Strategy: validation
Validate before calling
assert!(is_float_data(&data));
Type guard
fn is_float_data(d: &TensorData) -> bool {
matches!(d.dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16)
} Prevention
- Check TensorData::dtype before tensor construction
- Convert at the data boundary with data.convert::<f32>()
- Route int data to integer tensor APIs
When it happens
Trigger: Calling `Tensor::<TchBackend>::from_data(...)` (or from_data on a float tensor type) with TensorData whose dtype is I64, I32, U8, BOOL, QFloat, etc., instead of a float dtype.
Common situations: Loading data saved as int/uint arrays (e.g. from numpy or raw files) and passing it straight to from_data; dtype drift after changing how data is serialized; quantized data accidentally fed to a float tensor constructor.
Related errors
- Should be float, got int
- Should be float, got bool
- Should be float, got quantized
- Should be float, got autodiff
- burn-flex does not support Bool(U32) storage (only Native an
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/f228419b8baabce3.
Report an issue: GitHub.