{"record":{"id":"ecc962e8c8701540","repo":"tracel-ai/burn","slug":"sum-dim-unsupported-dtype","errorCode":null,"errorMessage":"sum_dim: unsupported dtype {:?}","messagePattern":"sum_dim: unsupported dtype (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/reduce.rs","lineNumber":274,"sourceCode":"            f16::from_f32,\n        ),\n        DType::BF16 => reduce_dim_half(\n            &tensor,\n            dim,\n            0.0,\n            |acc, x| acc + x,\n            bf16::to_f32,\n            bf16::from_f32,\n        ),\n        DType::I8 => reduce_dim_widening::<i8, _>(&tensor, dim, 0, |acc, x| acc.wrapping_add(x)),\n        DType::I16 => reduce_dim_widening::<i16, _>(&tensor, dim, 0, |acc, x| acc.wrapping_add(x)),\n        DType::I32 => reduce_dim_widening::<i32, _>(&tensor, dim, 0, |acc, x| acc.wrapping_add(x)),\n        DType::I64 => reduce_dim_impl::<i64, _>(&tensor, dim, 0, |acc, x| acc + x),\n        DType::U8 => reduce_dim_widening::<u8, _>(&tensor, dim, 0, |acc, x| acc.wrapping_add(x)),\n        DType::U16 => reduce_dim_widening::<u16, _>(&tensor, dim, 0, |acc, x| acc.wrapping_add(x)),\n        DType::U32 => reduce_dim_widening::<u32, _>(&tensor, dim, 0, |acc, x| acc.wrapping_add(x)),\n        DType::U64 => reduce_dim_impl::<u64, _>(&tensor, dim, 0, |acc, x| acc + x),\n        _ => panic!(\"sum_dim: unsupported dtype {:?}\", tensor.dtype()),\n    }\n}\n\n/// Mean along a dimension, keeping the dimension with size 1.\npub fn mean_dim(tensor: FlexTensor, dim: usize) -> FlexTensor {\n    let dim_size = tensor.layout().shape()[dim];\n    let dtype = tensor.dtype();\n    // Floats divide by a zero `dim_size` to `NaN`, which is what `mean()` already returns for an\n    // empty input and what the other backends return here. Only the integer arms below have no\n    // such value, so only they are rejected.\n    assert!(\n        dim_size > 0 || dtype.is_float(),\n        \"mean_dim: cannot take mean of an empty dimension for the integer type {dtype:?}\"\n    );\n\n    // Half-precision types fuse sum+divide in f32 to avoid overflow when the\n    // intermediate sum exceeds f16::MAX, so they don't go through sum_dim.\n    match dtype {","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/reduce.rs#L256-L292","documentation":"burn-flex's `sum_dim` reduces along one dimension and supports float dtypes plus I8–I64 and U8–U64 (widening accumulators for narrow ints). Other dtypes such as Bool or quantized have no implementation and the backend panics.","triggerScenarios":"Calling `Tensor::sum_dim(dim)` (or `mean_dim`, which calls sum_dim) on a Bool or quantized tensor — e.g. summing a boolean mask along a dim for per-row counts.","commonSituations":"Per-batch accuracy counts computed from boolean equality masks without casting; summing dequantization-pending quantized tensors; dtype inferred from comparisons in generic code.","solutions":["Cast the tensor first: `mask.cast(DType::I32).sum_dim(1)`.","Dequantize quantized tensors before dim-reducing.","In generic/trait code, constrain inputs to numeric dtypes or cast defensively."],"exampleFix":"// before\nlet counts = mask.sum_dim(1); // mask: Bool\n// after\nlet counts = mask.cast(DType::I32).sum_dim(1);","handlingStrategy":"validation","validationCode":"assert!(!matches!(t.dtype(), DType::Bool | DType::QFloat(_)), \"sum_dim 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 masks to I32 before sum_dim (e.g. per-row counts).","Dequantize before dim-reductions on quantized tensors.","In generic code, assert numeric dtype at function entry."],"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"}