tracel-ai/burn · error

Expected float dtype, got {dtype:?}

Error message

Expected float dtype, got {dtype:?}

What it means

This panic fires in Burn's tensor bridge when a tensor is created from raw data (from_data) with a dtype that is not a floating-point type. The bridge routes data to the float constructor only when `dtype.is_float()` is true; integer, bool, or other dtypes fall through to an unconditional panic. It is an internal API-contract violation: a non-float dtype reached the float-only construction path.

Source

Thrown at crates/burn-tensor/src/bridge/ops/float.rs:307

        let (kind, tensor) = tensor.into_parts();
        match kind {
            BridgeKind::Float => Dispatch::float_into_data(tensor).await,
            BridgeKind::QFloat => Dispatch::q_into_data(tensor).await,
            _ => panic!("Should be Float primitive kind"),
        }
    }

    fn from_data(data: TensorData, device: &Device, dtype: DType) -> BridgeTensor {
        if matches!(data.dtype, DType::QFloat(_)) {
            // When the source is QFloat, there is no conversion path possible.
            BridgeTensor::qfloat(Dispatch::q_from_data(data, device.as_dispatch()))
        } else if dtype.is_float() {
            BridgeTensor::float(Dispatch::float_from_data(
                data.convert_dtype(dtype),
                device.as_dispatch(),
            ))
        } else {
            panic!("Expected float dtype, got {dtype:?}")
        }
    }

    fn repeat_dim(tensor: BridgeTensor, dim: usize, times: usize) -> BridgeTensor {
        let (kind, tensor) = tensor.into_parts();
        match kind {
            BridgeKind::Float => {
                BridgeTensor::float(Dispatch::float_repeat_dim(tensor, dim, times))
            }
            BridgeKind::QFloat => BridgeTensor::qfloat(Dispatch::q_repeat_dim(tensor, dim, times)),
            _ => panic!("Should be Float primitive kind"),
        }
    }

    fn cat(vectors: Vec<BridgeTensor>, dim: usize) -> BridgeTensor {
        match vectors.first().unwrap().kind() {
            BridgeKind::Float => BridgeTensor::float(Dispatch::float_cat(
                BridgeTensor::into_dispatch_vec(vectors),

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Cast the data to a float dtype before constructing the tensor, e.g. data.convert_dtype(DType::F32) or convert the source array with .into_dtype()/astype(f32)
  2. Check the DType you pass to the constructor; use Tensor::<B, D>::from_data for the numeric kind you actually have instead of the float path
  3. If you are writing a backend/bridge adapter, ensure from_data dispatches on dtype.is_float() vs int/bool branches rather than always calling float_from_data
  4. Verify the upstream data source (checkpoint, dataset) is being decoded with the intended dtype

Example fix

// before
let data = TensorData::from(vec![1i64, 2, 3]);
let t = BridgeTensor::from_data(data, dtype, &device); // dtype = F32 but data is I64 -> panic
// after
let data = TensorData::from(vec![1i64, 2, 3]).convert_dtype(DType::F32);
let t = BridgeTensor::from_data(data, DType::F32, &device);
Defensive patterns

Strategy: validation

Validate before calling

fn ensure_float(dtype: burn_tensor::DType) {
    assert!(dtype.is_float(), "from_data requires a float dtype, got {dtype:?}");
}

Type guard

fn is_float_dtype(dtype: burn_tensor::DType) -> bool {
    dtype.is_float()
}

Try / catch

// Panicking internal API; no catch pattern. Guard the dtype before the call:
if !dtype.is_float() { let data = data.convert_dtype(burn_tensor::DType::F32); }

Prevention

When it happens

Trigger: Calling from_data (or an API that lowers to it, e.g. Tensor::from_data on a bridge backend) with data whose inferred dtype is an integer or bool while expecting a float tensor; passing a DType like I64/Bool into the float dispatch path; a backend adapter mislabeling the tensor kind so float_from_data receives int data.

Common situations: Loading a checkpoint or dataset of integer labels/indices and feeding it into a float tensor constructor; converting numpy/ndarray integer arrays without casting to f32/f64 first; recent dtype refactors (Burn's explicit DType migration) where code that previously inferred float now passes a concrete int dtype.

Related errors


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