{"record":{"id":"1e173c897b1d7c14","repo":"tracel-ai/burn","slug":"mean-dim-unsupported-dtype","errorCode":null,"errorMessage":"mean_dim: unsupported dtype {:?}","messagePattern":"mean_dim: unsupported dtype (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/reduce.rs","lineNumber":344,"sourceCode":"            let mut tensor = sum_result;\n            let data: &mut [u8] = tensor.storage_mut();\n            for x in data.iter_mut() {\n                *x = ((*x as u32) / divisor) as u8;\n            }\n            tensor\n        }\n        DType::U16 => {\n            let divisor = dim_size as u32;\n            let mut tensor = sum_result;\n            let data: &mut [u16] = tensor.storage_mut();\n            for x in data.iter_mut() {\n                *x = ((*x as u32) / divisor) as u16;\n            }\n            tensor\n        }\n        DType::U32 => scalar_div::<u32>(sum_result, dim_size as u32),\n        DType::U64 => scalar_div::<u64>(sum_result, dim_size as u64),\n        _ => panic!(\"mean_dim: unsupported dtype {:?}\", dtype),\n    }\n}\n\n/// Product of all elements in a tensor, returning a scalar tensor.\npub fn prod(tensor: FlexTensor) -> FlexTensor {\n    match tensor.dtype() {\n        DType::F32 => prod_impl::<f32>(&tensor),\n        DType::F64 => prod_impl::<f64>(&tensor),\n        DType::F16 => reduce_scalar_half(&tensor, |a, b| a * b, 1.0, f16::to_f32, f16::from_f32),\n        DType::BF16 => reduce_scalar_half(&tensor, |a, b| a * b, 1.0, bf16::to_f32, bf16::from_f32),\n        DType::I8 => prod_impl_widening::<i8>(&tensor),\n        DType::I16 => prod_impl_widening::<i16>(&tensor),\n        DType::I32 => prod_impl_widening::<i32>(&tensor),\n        DType::I64 => prod_impl::<i64>(&tensor),\n        DType::U8 => prod_impl_widening::<u8>(&tensor),\n        DType::U16 => prod_impl_widening::<u16>(&tensor),\n        DType::U32 => prod_impl_widening::<u32>(&tensor),\n        DType::U64 => prod_impl::<u64>(&tensor),","sourceCodeStart":326,"sourceCodeEnd":362,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/reduce.rs#L326-L362","documentation":"burn-flex's `mean_dim` computes a sum along a dimension then divides by the dim size; it supports float dtypes and integer dtypes (via widening sum + scalar divide). Unsupported dtypes like Bool or quantized hit the catch-all panic.","triggerScenarios":"Calling `Tensor::mean_dim(dim)` on a Bool or quantized tensor, or any dtype outside the float/I/U integer set — e.g. averaging a boolean mask per row.","commonSituations":"Computing per-sample mean of boolean correctness masks without casting; mean over quantized activations; generic kernels that assume float inputs but receive bools.","solutions":["Cast first: `mask.cast(DType::F32).mean_dim(1)` (float is usually what you want for means).","For integer means where truncation matters, sum as wider int then divide explicitly.","Dequantize quantized tensors before calling mean_dim."],"exampleFix":"// before\nlet acc = mask.mean_dim(1); // Bool\n// after\nlet acc = mask.cast(DType::F32).mean_dim(1);","handlingStrategy":"validation","validationCode":"assert!(matches!(t.dtype(), DType::F32 | DType::F64 | DType::F16 | DType::BF16 | DType::I8 | DType::I16 | DType::I32 | DType::I64 | DType::U8 | DType::U16 | DType::U32 | DType::U64), \"mean_dim unsupported for {:?}\", t.dtype());","typeGuard":"fn is_mean_capable(d: DType) -> bool {\n    !matches!(d, DType::Bool | DType::QFloat(_))\n}","tryCatchPattern":null,"preventionTips":["Cast to F32 before mean_dim for float-accurate averages.","Never call mean on Bool or quantized tensors directly.","Centralize reduction calls behind dtype-checking helpers."],"tags":["rust","burn","reduce","mean","dtype","panic"],"backgroundTag":"unsupported-dtype","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"}