{"record":{"id":"c90b387e12e75e57","repo":"tracel-ai/burn","slug":"not-a-valid-float-kind","errorCode":null,"errorMessage":"Not a valid float kind","messagePattern":"Not a valid float kind","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-tch/src/ops/tensor.rs","lineNumber":89,"sourceCode":"        let tensor = Self::float_reshape(tensor.clone(), Shape::new([shape.num_elements()]));\n        Ok(match tensor.tensor.kind() {\n            tch::Kind::Half => {\n                let values = Vec::<f16>::try_from(&tensor).unwrap();\n                TensorData::new(values, shape)\n            }\n            tch::Kind::Float => {\n                let values = Vec::<f32>::try_from(&tensor).unwrap();\n                TensorData::new(values, shape)\n            }\n            tch::Kind::Double => {\n                let values = Vec::<f64>::try_from(&tensor).unwrap();\n                TensorData::new(values, shape)\n            }\n            tch::Kind::BFloat16 => {\n                let values = Vec::<bf16>::try_from(&tensor).unwrap();\n                TensorData::new(values, shape)\n            }\n            _ => panic!(\"Not a valid float kind\"),\n        })\n    }\n\n    fn float_to_device(tensor: TchTensor, device: &LibTorchDevice) -> TchTensor {\n        TchOps::to_device(tensor, device)\n    }\n\n    fn float_empty(shape: Shape, device: &LibTorchDevice, dtype: FloatDType) -> TchTensor {\n        let tensor = tch::Tensor::empty(\n            TchShape::from(shape).dims,\n            (dtype.into_kind(), (*device).into()),\n        );\n\n        TchTensor::new(tensor)\n    }\n\n    fn float_add(lhs: TchTensor, rhs: TchTensor) -> TchTensor {\n        TchOps::add(lhs, rhs)","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-tch/src/ops/tensor.rs#L71-L107","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the tensor dtype is a float kind before extracting data: call .to_dtype(tch::Kind::Float) on the tch tensor first","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>())","Use the correctly typed backend API: integer tensors belong to the Int backend, not Float — route the call through the right tensor type","If extracting generic data, use the non-float data path that handles all kinds instead of float_into_data"],"exampleFix":"// before\nlet data = float_tensor.into_data(); // tensor is Kind::Int64\n// after\nlet tensor = tensor.tensor.to_dtype(tch::Kind::Float);\nlet data = TchTensor::new(tensor).into_data();","handlingStrategy":"type-guard","validationCode":"fn is_float_kind(kind: tch::Kind) -> bool {\n    matches!(kind, tch::Kind::Float | tch::Kind::Double | tch::Kind::Half | tch::Kind::BFloat16)\n}\n// before extracting data:\nif !is_float_kind(tensor.tensor.kind()) { tensor = tensor.tensor.to_dtype(tch::Kind::Float).into(); }","typeGuard":"fn as_float_tensor(t: TchTensor) -> Option<TchTensor> {\n    matches!(t.tensor.kind(), tch::Kind::Float | tch::Kind::Double | tch::Kind::Half | tch::Kind::BFloat16)\n        .then(|| t)\n}","tryCatchPattern":"let data = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| tensor.into_data()))\n    .map_err(|_| \"tensor dtype is not a float kind; convert first\")?;","preventionTips":["Convert weights/data to the model's float dtype at load time (TensorData::convert::<f32>())","Check tch::Kind before bridging raw tch tensors into burn","Keep integer tensors on the Int backend and float tensors on the Float backend","Log tensor.kind() when debugging dtype mismatches"],"tags":["rust","burn","tch","dtype-mismatch","panic"],"backgroundTag":"dtype-mismatch","analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}