{"record":{"id":"eca7458be04baf4b","repo":"tracel-ai/burn","slug":"should-be-float-got-int","errorCode":null,"errorMessage":"Should be float, got int","messagePattern":"Should be float, got int","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dispatch/src/tensor.rs","lineNumber":41,"sourceCode":"    /// Float tensor handle.\n    Float(B::FloatTensorPrimitive),\n    /// Int tensor handle.\n    Int(B::IntTensorPrimitive),\n    /// Bool tensor handle.\n    Bool(B::BoolTensorPrimitive),\n    /// Quantized tensor handle.\n    Quantized(B::QuantizedTensorPrimitive),\n    #[cfg(feature = \"autodiff\")]\n    /// Autodiff float tensor handle.\n    Autodiff(FloatTensor<Autodiff<B>>),\n}\n\nimpl<B: Backend> BackendTensor<B> {\n    /// Returns the inner float tensor primitive.\n    pub fn float(self) -> B::FloatTensorPrimitive {\n        match self {\n            BackendTensor::Float(tensor) => tensor,\n            BackendTensor::Int(_) => panic!(\"Should be float, got int\"),\n            BackendTensor::Bool(_) => panic!(\"Should be float, got bool\"),\n            BackendTensor::Quantized(_) => panic!(\"Should be float, got quantized\"),\n            #[cfg(feature = \"autodiff\")]\n            BackendTensor::Autodiff(_) => panic!(\"Should be float, got autodiff\"),\n        }\n    }\n    /// Returns the inner float tensor primitive.\n    pub fn as_float(&self) -> &B::FloatTensorPrimitive {\n        match self {\n            BackendTensor::Float(tensor) => tensor,\n            BackendTensor::Int(_) => panic!(\"Should be float, got int\"),\n            BackendTensor::Bool(_) => panic!(\"Should be float, got bool\"),\n            BackendTensor::Quantized(_) => panic!(\"Should be float, got quantized\"),\n            #[cfg(feature = \"autodiff\")]\n            BackendTensor::Autodiff(_) => panic!(\"Should be float, got autodiff\"),\n        }\n    }\n","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dispatch/src/tensor.rs#L23-L59","documentation":"BackendTensor::float() extracts the inner float primitive from a BackendTensor, and this panic fires when the tensor is actually an Int tensor. Downcasting to the wrong dtype variant is a programming error, so burn panics with a message naming the actual variant received.","triggerScenarios":"Calling BackendTensor::float() (public) on a value holding BackendTensor::Int(_), e.g., after ops that produce integer tensors (arange, indexing, comparisons cast to int) being fed to float-only APIs.","commonSituations":"Passing integer tensors (indices, masks from integer ops, counters) into code that unconditionally calls .float() on a generic BackendTensor; dtype changes in refactors where an op's output switched from float to int.","solutions":["Convert the Int tensor to float explicitly with the backend's float cast op before calling .float().","Match on the BackendTensor variant first and handle Int (cast or error) instead of blindly calling .float().","Trace where the Int tensor originates (arange, nonzero, casts) and fix the upstream dtype.","Use as_float() or a checked accessor when the dtype may legitimately vary."],"exampleFix":"// before\nlet f = tensor.float(); // panics if tensor is Int\n// after\nlet f = match tensor {\n    BackendTensor::Int(i) => BackendTensor::Float(int_to_float_cast::<B>(i)).float(),\n    t => t.float(),\n};","handlingStrategy":"type-guard","validationCode":"// before calling .float()\nif let BackendTensor::Int(_) = &tensor {\n    // cast to float first or handle error\n}","typeGuard":"fn as_float_tensor<B: Backend>(t: BackendTensor<B>) -> Option<B::FloatTensorPrimitive> {\n    match t {\n        BackendTensor::Float(f) => Some(f),\n        _ => None,\n    }\n}","tryCatchPattern":"// panics cannot be caught idiomatically in Rust; guard instead\nlet f = match tensor {\n    BackendTensor::Float(f) => f,\n    other => int_to_float_cast::<B>(other_tensor_to_int(other)),\n};","preventionTips":["Prefer matching on BackendTensor variants over calling .float() directly in generic code.","Explicitly cast Int tensors (arange, indices) to float before float-only APIs.","Run dtype-sensitive tests after refactors that change an op's output dtype."],"tags":["rust","dtype","tensor","panic","dispatch"],"backgroundTag":"tensor-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"}