tracel-ai/burn · error

Not a valid float kind

Error message

Not a valid float kind

What it means

float_into_data converts a tch tensor's contents into a Burn TensorData buffer and only handles floating-point dtypes (Float32, Float64, Float16, BFloat16). If the tensor's tch Kind is anything else (e.g. Int64, Bool), it panics with 'Not a valid float kind'. It is an internal invariant check reached when a non-float tensor flows into a float-typed data extraction path.

Source

Thrown at crates/burn-tch/src/ops/tensor.rs:89

        let tensor = Self::float_reshape(tensor.clone(), Shape::new([shape.num_elements()]));
        Ok(match tensor.tensor.kind() {
            tch::Kind::Half => {
                let values = Vec::<f16>::try_from(&tensor).unwrap();
                TensorData::new(values, shape)
            }
            tch::Kind::Float => {
                let values = Vec::<f32>::try_from(&tensor).unwrap();
                TensorData::new(values, shape)
            }
            tch::Kind::Double => {
                let values = Vec::<f64>::try_from(&tensor).unwrap();
                TensorData::new(values, shape)
            }
            tch::Kind::BFloat16 => {
                let values = Vec::<bf16>::try_from(&tensor).unwrap();
                TensorData::new(values, shape)
            }
            _ => panic!("Not a valid float kind"),
        })
    }

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

    fn float_empty(shape: Shape, device: &LibTorchDevice, dtype: FloatDType) -> TchTensor {
        let tensor = tch::Tensor::empty(
            TchShape::from(shape).dims,
            (dtype.into_kind(), (*device).into()),
        );

        TchTensor::new(tensor)
    }

    fn float_add(lhs: TchTensor, rhs: TchTensor) -> TchTensor {
        TchOps::add(lhs, rhs)

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Ensure the tensor dtype is a float kind before extracting data: call .to_dtype(tch::Kind::Float) on the tch tensor first
  2. Check where the tensor was created (from_data, checkpoint load, tch interop) and give it the float dtype the model expects (e.g. convert weights with TensorData::convert::<f32>())
  3. Use the correctly typed backend API: integer tensors belong to the Int backend, not Float — route the call through the right tensor type
  4. If extracting generic data, use the non-float data path that handles all kinds instead of float_into_data

Example fix

// before
let data = float_tensor.into_data(); // tensor is Kind::Int64
// after
let tensor = tensor.tensor.to_dtype(tch::Kind::Float);
let data = TchTensor::new(tensor).into_data();
Defensive patterns

Strategy: type-guard

Validate before calling

fn is_float_kind(kind: tch::Kind) -> bool {
    matches!(kind, tch::Kind::Float | tch::Kind::Double | tch::Kind::Half | tch::Kind::BFloat16)
}
// before extracting data:
if !is_float_kind(tensor.tensor.kind()) { tensor = tensor.tensor.to_dtype(tch::Kind::Float).into(); }

Type guard

fn as_float_tensor(t: TchTensor) -> Option<TchTensor> {
    matches!(t.tensor.kind(), tch::Kind::Float | tch::Kind::Double | tch::Kind::Half | tch::Kind::BFloat16)
        .then(|| t)
}

Try / catch

let data = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| tensor.into_data()))
    .map_err(|_| "tensor dtype is not a float kind; convert first")?;

Prevention

When it happens

Trigger: Calling float_into_data (directly or via tensor.into_data()/to_data on a float tensor API) with a tch tensor whose Kind is integer, bool, or quantized — e.g. a tensor created via from_data with an int dtype but cast/typed incorrectly, or kind mismatch after loading weights with the wrong dtype.

Common situations: Loading checkpoint/weights whose dtype differs from the model's float dtype; manually constructing TchTensor with tch::Kind::Int64 and passing it to a FloatTensor API; dtype mixups after quantization or when bridging raw tch code into burn.

Related errors


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