tracel-ai/burn · error

sum: unsupported dtype {:?}

Error message

sum: unsupported dtype {:?}

What it means

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.

Source

Thrown at crates/burn-flex/src/ops/reduce.rs:71

// Sum (all elements)
// ============================================================================

/// Sum all elements in a tensor, returning a scalar tensor.
pub fn sum(tensor: FlexTensor) -> FlexTensor {
    match tensor.dtype() {
        DType::F32 => sum_f32(&tensor),
        DType::F64 => sum_impl::<f64>(&tensor),
        DType::F16 => reduce_scalar_half(&tensor, |a, b| a + b, 0.0, f16::to_f32, f16::from_f32),
        DType::BF16 => reduce_scalar_half(&tensor, |a, b| a + b, 0.0, bf16::to_f32, bf16::from_f32),
        DType::I8 => sum_impl_widening::<i8>(&tensor),
        DType::I16 => sum_impl_widening::<i16>(&tensor),
        DType::I32 => sum_impl_widening::<i32>(&tensor),
        DType::I64 => sum_impl::<i64>(&tensor),
        DType::U8 => sum_impl_widening::<u8>(&tensor),
        DType::U16 => sum_impl_widening::<u16>(&tensor),
        DType::U32 => sum_impl_widening::<u32>(&tensor),
        DType::U64 => sum_impl::<u64>(&tensor),
        _ => panic!("sum: unsupported dtype {:?}", tensor.dtype()),
    }
}

/// Optimized f32 sum with SIMD and parallelism.
fn sum_f32(tensor: &FlexTensor) -> FlexTensor {
    let result = match tensor.layout().contiguous_offsets() {
        Some((start, end)) => {
            let data: &[f32] = tensor.storage();
            let slice = &data[start..end];
            sum_f32_contiguous(slice)
        }
        None => {
            // Non-contiguous: check if we can sum the buffer directly.
            // For transposed tensors that use all elements (no slicing),
            // the sum is the same regardless of element order.
            let data: &[f32] = tensor.storage();
            let elem_count = tensor.layout().num_elements();

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Cast before summing: `mask.cast(DType::I32).sum()` or `.float().sum()` for floats.
  2. For quantized tensors, dequantize (`.dequantize()`) before reducing.
  3. Check `tensor.dtype()` and add the cast at the call site.

Example fix

// before
let count = (pred.equal(target)).sum(); // Bool tensor
// after
let count = pred.equal(target).cast(DType::I32).sum();
Defensive patterns

Strategy: validation

Validate before calling

assert!(!matches!(t.dtype(), DType::Bool | DType::QFloat(_)), "sum unsupported for {:?}; cast or dequantize first", t.dtype());

Type guard

fn is_summable(d: DType) -> bool {
    matches!(d, DType::F32 | DType::F64 | DType::F16 | DType::BF16
        | DType::I8 | DType::I16 | DType::I32 | DType::I64
        | DType::U8 | DType::U16 | DType::U32 | DType::U64)
}

Prevention

When it happens

Trigger: 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.

Common situations: Summing a bool comparison mask (`tensor.equal(...).sum()`) without casting; summing quantized tensors before dequantization; dtype leaks from data loaders.

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/7a07f2dab3802cc0. Report an issue: GitHub.