{"record":{"id":"7a07f2dab3802cc0","repo":"tracel-ai/burn","slug":"sum-unsupported-dtype","errorCode":null,"errorMessage":"sum: unsupported dtype {:?}","messagePattern":"sum: unsupported dtype (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/reduce.rs","lineNumber":71,"sourceCode":"// Sum (all elements)\n// ============================================================================\n\n/// Sum all elements in a tensor, returning a scalar tensor.\npub fn sum(tensor: FlexTensor) -> FlexTensor {\n    match tensor.dtype() {\n        DType::F32 => sum_f32(&tensor),\n        DType::F64 => sum_impl::<f64>(&tensor),\n        DType::F16 => reduce_scalar_half(&tensor, |a, b| a + b, 0.0, f16::to_f32, f16::from_f32),\n        DType::BF16 => reduce_scalar_half(&tensor, |a, b| a + b, 0.0, bf16::to_f32, bf16::from_f32),\n        DType::I8 => sum_impl_widening::<i8>(&tensor),\n        DType::I16 => sum_impl_widening::<i16>(&tensor),\n        DType::I32 => sum_impl_widening::<i32>(&tensor),\n        DType::I64 => sum_impl::<i64>(&tensor),\n        DType::U8 => sum_impl_widening::<u8>(&tensor),\n        DType::U16 => sum_impl_widening::<u16>(&tensor),\n        DType::U32 => sum_impl_widening::<u32>(&tensor),\n        DType::U64 => sum_impl::<u64>(&tensor),\n        _ => panic!(\"sum: unsupported dtype {:?}\", tensor.dtype()),\n    }\n}\n\n/// Optimized f32 sum with SIMD and parallelism.\nfn sum_f32(tensor: &FlexTensor) -> FlexTensor {\n    let result = match tensor.layout().contiguous_offsets() {\n        Some((start, end)) => {\n            let data: &[f32] = tensor.storage();\n            let slice = &data[start..end];\n            sum_f32_contiguous(slice)\n        }\n        None => {\n            // Non-contiguous: check if we can sum the buffer directly.\n            // For transposed tensors that use all elements (no slicing),\n            // the sum is the same regardless of element order.\n            let data: &[f32] = tensor.storage();\n            let elem_count = tensor.layout().num_elements();\n","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/reduce.rs#L53-L89","documentation":"burn-flex's `sum` reduction supports all float dtypes plus integer dtypes I8–I64 and U8–U64 (with widening accumulation to avoid overflow). Any other dtype — notably Bool, or quantized DType::QFloat — has no sum kernel and the backend panics.","triggerScenarios":"Calling `Tensor::sum()` (or `mean()`, which delegates to sum) on a Bool or quantized tensor; e.g. summing a boolean mask instead of using `int()`/`float()` cast first.","commonSituations":"Summing a bool comparison mask (`tensor.equal(...).sum()`) without casting; summing quantized tensors before dequantization; dtype leaks from data loaders.","solutions":["Cast before summing: `mask.cast(DType::I32).sum()` or `.float().sum()` for floats.","For quantized tensors, dequantize (`.dequantize()`) before reducing.","Check `tensor.dtype()` and add the cast at the call site."],"exampleFix":"// before\nlet count = (pred.equal(target)).sum(); // Bool tensor\n// after\nlet count = pred.equal(target).cast(DType::I32).sum();","handlingStrategy":"validation","validationCode":"assert!(!matches!(t.dtype(), DType::Bool | DType::QFloat(_)), \"sum unsupported for {:?}; cast or dequantize first\", t.dtype());","typeGuard":"fn is_summable(d: DType) -> bool {\n    matches!(d, DType::F32 | DType::F64 | DType::F16 | DType::BF16\n        | DType::I8 | DType::I16 | DType::I32 | DType::I64\n        | DType::U8 | DType::U16 | DType::U32 | DType::U64)\n}","tryCatchPattern":null,"preventionTips":["Cast boolean masks to I32/F32 before any reduction.","Dequantize QFloat tensors before summing.","Wrap reductions in helpers that cast defensively."],"tags":["rust","burn","reduce","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"}