{"record":{"id":"a4f5115b4ba21ecd","repo":"tracel-ai/burn","slug":"should-be-int-got-float","errorCode":null,"errorMessage":"Should be int, got float","messagePattern":"Should be int, got float","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dispatch/src/tensor.rs","lineNumber":64,"sourceCode":"        }\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\n    /// Returns the inner int tensor primitive.\n    pub fn int(self) -> B::IntTensorPrimitive {\n        match self {\n            BackendTensor::Int(tensor) => tensor,\n            BackendTensor::Float(_) => panic!(\"Should be int, got float\"),\n            BackendTensor::Bool(_) => panic!(\"Should be int, got bool\"),\n            BackendTensor::Quantized(_) => panic!(\"Should be int, got quantized\"),\n            #[cfg(feature = \"autodiff\")]\n            BackendTensor::Autodiff(_) => panic!(\"Should be int, got autodiff\"),\n        }\n    }\n\n    /// Returns the inner bool tensor primitive.\n    pub fn bool(self) -> B::BoolTensorPrimitive {\n        match self {\n            BackendTensor::Bool(tensor) => tensor,\n            BackendTensor::Float(_) => panic!(\"Should be bool, got float\"),\n            BackendTensor::Int(_) => panic!(\"Should be bool, got int\"),\n            BackendTensor::Quantized(_) => panic!(\"Should be bool, got quantized\"),\n            #[cfg(feature = \"autodiff\")]\n            BackendTensor::Autodiff(_) => panic!(\"Should be bool, got autodiff\"),\n        }\n    }","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dispatch/src/tensor.rs#L46-L82","documentation":"BackendTensor::int() was called on a Float tensor handle. Only the Int variant can be unwrapped to the int primitive, so the dispatch layer panics. A float tensor reached a code path (indexing, scatter/gather, comparison output handling, etc.) that requires integer tensors.","triggerScenarios":"Calling int() on a tensor produced by float ops (arange with float dtype, float constants, division results); passing float values where integer indices or integer outputs are required (e.g. indices for gather/scatter, reshape with int tensor, embedding lookup).","commonSituations":"Computing indices with float math and forgetting to cast down; a config constant typed as f32 used as an index; upstream library change turned an int output into a float output; loading tensor data whose dtype inferred to float.","solutions":["Cast the float tensor to int explicitly (tensor.int() / cast to DType::I32 or I64) before the int-only call","Ensure the op generating the tensor is configured with an integer dtype (e.g. arange default dtype)","Match on the BackendTensor variant at the call site instead of assuming Int","Validate dtype at the API boundary using TensorMetadata and reject float inputs where ints are required"],"exampleFix":"// before\nlet idx = handle.int(); // panics: handle is Float\n// after\nlet idx = handle.float().int(); // explicit float -> int cast, or build indices with int dtype from the start","handlingStrategy":"type-guard","validationCode":"if !matches!(handle, BackendTensor::Int(_)) {\n    panic!(\"expected int tensor (indices), got {:?}\", handle.dtype());\n}","typeGuard":"fn is_int<B: BackendTypes>(t: &BackendTensor<B>) -> bool {\n    matches!(t, BackendTensor::Int(_))\n}","tryCatchPattern":null,"preventionTips":["Cast index computations to int explicitly before gather/scatter/embedding ops","Configure ops like arange/zeros with an integer dtype when indices are needed","Validate dtype at boundaries with TensorMetadata"],"tags":["rust","panic","dtype-mismatch","integer"],"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"}