tracel-ai/burn · error

sum_dim: unsupported dtype {:?}

Error message

sum_dim: unsupported dtype {:?}

What it means

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.

Source

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

            f16::from_f32,
        ),
        DType::BF16 => reduce_dim_half(
            &tensor,
            dim,
            0.0,
            |acc, x| acc + x,
            bf16::to_f32,
            bf16::from_f32,
        ),
        DType::I8 => reduce_dim_widening::<i8, _>(&tensor, dim, 0, |acc, x| acc.wrapping_add(x)),
        DType::I16 => reduce_dim_widening::<i16, _>(&tensor, dim, 0, |acc, x| acc.wrapping_add(x)),
        DType::I32 => reduce_dim_widening::<i32, _>(&tensor, dim, 0, |acc, x| acc.wrapping_add(x)),
        DType::I64 => reduce_dim_impl::<i64, _>(&tensor, dim, 0, |acc, x| acc + x),
        DType::U8 => reduce_dim_widening::<u8, _>(&tensor, dim, 0, |acc, x| acc.wrapping_add(x)),
        DType::U16 => reduce_dim_widening::<u16, _>(&tensor, dim, 0, |acc, x| acc.wrapping_add(x)),
        DType::U32 => reduce_dim_widening::<u32, _>(&tensor, dim, 0, |acc, x| acc.wrapping_add(x)),
        DType::U64 => reduce_dim_impl::<u64, _>(&tensor, dim, 0, |acc, x| acc + x),
        _ => panic!("sum_dim: unsupported dtype {:?}", tensor.dtype()),
    }
}

/// Mean along a dimension, keeping the dimension with size 1.
pub fn mean_dim(tensor: FlexTensor, dim: usize) -> FlexTensor {
    let dim_size = tensor.layout().shape()[dim];
    let dtype = tensor.dtype();
    // Floats divide by a zero `dim_size` to `NaN`, which is what `mean()` already returns for an
    // empty input and what the other backends return here. Only the integer arms below have no
    // such value, so only they are rejected.
    assert!(
        dim_size > 0 || dtype.is_float(),
        "mean_dim: cannot take mean of an empty dimension for the integer type {dtype:?}"
    );

    // Half-precision types fuse sum+divide in f32 to avoid overflow when the
    // intermediate sum exceeds f16::MAX, so they don't go through sum_dim.
    match dtype {

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Cast the tensor first: `mask.cast(DType::I32).sum_dim(1)`.
  2. Dequantize quantized tensors before dim-reducing.
  3. In generic/trait code, constrain inputs to numeric dtypes or cast defensively.

Example fix

// before
let counts = mask.sum_dim(1); // mask: Bool
// after
let counts = mask.cast(DType::I32).sum_dim(1);
Defensive patterns

Strategy: validation

Validate before calling

assert!(!matches!(t.dtype(), DType::Bool | DType::QFloat(_)), "sum_dim 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_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.

Common situations: Per-batch accuracy counts computed from boolean equality masks without casting; summing dequantization-pending quantized tensors; dtype inferred from comparisons in generic code.

Related errors


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