{"record":{"id":"a331014c6f417c17","repo":"tracel-ai/burn","slug":"int-mean-unsupported-dtype","errorCode":null,"errorMessage":"int_mean: unsupported dtype {:?}","messagePattern":"int_mean: unsupported dtype (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/int.rs","lineNumber":1046,"sourceCode":"        let sum_result = crate::ops::reduce::sum(tensor);\n        // Compute in i64 to avoid truncation of n for small int types\n        macro_rules! compute_mean {\n            ($ty:ty) => {{\n                let data: &[$ty] = sum_result.storage();\n                let mean_val = (data[0] as i64 / n as i64) as $ty;\n                FlexTensor::new(\n                    Bytes::from_elems(alloc::vec![mean_val]),\n                    Layout::contiguous(Shape::from(alloc::vec![1])),\n                    dtype,\n                )\n            }};\n        }\n        match dtype {\n            DType::I64 => compute_mean!(i64),\n            DType::I32 => compute_mean!(i32),\n            DType::I16 => compute_mean!(i16),\n            DType::I8 => compute_mean!(i8),\n            other => panic!(\"int_mean: unsupported dtype {:?}\", other),\n        }\n    }\n\n    fn int_max(tensor: IntTensor<Flex>) -> IntTensor<Flex> {\n        crate::ops::reduce::max(tensor)\n    }\n\n    fn int_max_dim(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {\n        crate::ops::reduce::max_dim(tensor, dim)\n    }\n\n    fn int_min(tensor: IntTensor<Flex>) -> IntTensor<Flex> {\n        crate::ops::reduce::min(tensor)\n    }\n\n    fn int_min_dim(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {\n        crate::ops::reduce::min_dim(tensor, dim)\n    }","sourceCodeStart":1028,"sourceCodeEnd":1064,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/int.rs#L1028-L1064","documentation":"int_mean computes the mean of an integer tensor as a scalar in the same dtype. It only supports signed integer dtypes I64/I32/I16/I8 — note that unsigned dtypes (U8/U16/U32/U64) are NOT handled and hit the panic arm. The panic fires when the tensor's dtype is unsigned or otherwise not in the match.","triggerScenarios":"Calling int_mean (mean() on an integer tensor) where the tensor dtype is U8/U16/U32/U64, a float dtype, or Bool.","commonSituations":"Taking the mean of a u8 image tensor (very common in image preprocessing) or other unsigned data; porting PyTorch code where mean works on any numeric dtype; tensors loaded from uint8 PNG/JPEG data.","solutions":["Cast the tensor to a signed dtype (i64 or i32) before calling mean: tensor.cast::<i64>().mean().","If float semantics are desired, cast to float and use the float mean (avoids integer truncation too).","Convert unsigned image data to i32/f32 at load time so later reductions are safe.","Watch out: even for supported dtypes, int_mean truncates (integer division) — prefer float mean for accurate averages."],"exampleFix":"// before: u8 image tensor\nlet m = image_tensor.mean(); // panics: dtype U8\n// after\nlet m = image_tensor.cast::<f32>().mean(); // or .cast::<i64>().mean() for int semantics","handlingStrategy":"validation","validationCode":"assert!(matches!(t.dtype(), DType::I64 | DType::I32 | DType::I16 | DType::I8), \"int_mean only supports signed ints, got {:?}\", t.dtype());","typeGuard":"fn is_signed_int(t: &FlexTensor) -> bool { matches!(t.dtype(), DType::I64 | DType::I32 | DType::I16 | DType::I8) }","tryCatchPattern":null,"preventionTips":["Cast u8/u16/u32/u64 tensors to i64 or f32 before calling mean","Prefer float mean for accurate averages (int_mean truncates)","Convert image (uint8) data to f32/i32 at load time"],"tags":["rust","burn","dtype","mean","unsigned-int"],"backgroundTag":"unsupported-dtype-for-op","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"}