tracel-ai/burn · error

Unsupported dtype for `int_from_data`: {:?}

Error message

Unsupported dtype for `int_from_data`: {:?}

What it means

burn-tch's int_from_data supports only I64, I32, I16, I8, and U8 TensorData dtypes; anything else (floats, bool, unsigned 16/32/64, etc.) hits the unimplemented!() fallback and panics with the offending dtype in the message. Torch integer tensors simply cannot be built from those element types here.

Source

Thrown at crates/burn-tch/src/ops/int_tensor.rs:22

    BoolDType, Distribution, ExecutionError, FloatDType, IntDType, Scalar, Shape, TensorData,
    TensorMetadata,
    ops::{FloatTensorOps, IntTensorOps},
    tensor::IntTensor,
};

use crate::{IntoKind, LibTorch, LibTorchDevice, TchShape, TchTensor};

use super::TchOps;

impl IntTensorOps<Self> for LibTorch {
    fn int_from_data(data: TensorData, device: &LibTorchDevice) -> TchTensor {
        match data.dtype {
            burn_backend::DType::I64 => TchTensor::from_data::<i64>(data, (*device).into()),
            burn_backend::DType::I32 => TchTensor::from_data::<i32>(data, (*device).into()),
            burn_backend::DType::I16 => TchTensor::from_data::<i16>(data, (*device).into()),
            burn_backend::DType::I8 => TchTensor::from_data::<i8>(data, (*device).into()),
            burn_backend::DType::U8 => TchTensor::from_data::<u8>(data, (*device).into()),
            _ => unimplemented!("Unsupported dtype for `int_from_data`: {:?}", data.dtype),
        }
    }

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

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

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

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Convert the TensorData to a supported dtype first: data.convert::<i64>() (or i32/i16/i8/u8)
  2. Cast the tensor after creation: Tensor::from_data(float_data,...).int() / .to_dtype(DType::I64)
  3. Check data.dtype against the supported set before calling
  4. Upgrade or patch burn-tch if a newly added DType (e.g. U64) should be supported

Example fix

// before
let t = Tensor::<LibTorch, 1, Int>::from_data(u64_data, &device); // panics on U64

// after
let t = Tensor::<LibTorch, 1, Int>::from_data(u64_data.convert::<i64>(), &device);
Defensive patterns

Strategy: validation

Validate before calling

const SUPPORTED: [DType; 5] = [DType::I64, DType::I32, DType::I16, DType::I8, DType::U8];
if !SUPPORTED.contains(&data.dtype) {
    data = data.convert::<i64>();
}
let t = Tensor::<LibTorch, D, Int>::from_data(data, &device);

Type guard

fn is_supported_int_dtype(dtype: burn_backend::DType) -> bool {
    matches!(dtype, DType::I64 | DType::I32 | DType::I16 | DType::I8 | DType::U8)
}

Prevention

When it happens

Trigger: Creating an integer tensor on the LibTorch backend from TensorData whose dtype is not one of I64/I32/I16/I8/U8 — e.g. from_data::<i64> called with float data, or U64/F64 data routed to int_from_data.

Common situations: Loading integer tensors from serialized records saved with different unsigned/64-bit dtypes; interoperating with numpy or frameworks that use u64/f64 where the caller assumed automatic casting; Burn version upgrades adding dtypes (e.g. U64) that tch's conversion hasn't been extended to.

Related errors


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