tracel-ai/burn · error

prod: unsupported dtype {:?}

Error message

prod: unsupported dtype {:?}

What it means

burn-flex's `prod` multiplies all elements; it supports float dtypes and integer dtypes I8–I64 / U8–U64 (with widening accumulation to reduce overflow risk). Any other dtype (Bool, quantized) triggers the panic.

Source

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

    }
}

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

fn prod_impl<E: Element + bytemuck::Pod + Default + core::iter::Product>(
    tensor: &FlexTensor,
) -> FlexTensor {
    let result: E = match tensor.layout().contiguous_offsets() {
        Some((start, end)) => {
            let data: &[E] = tensor.storage();
            data[start..end].iter().copied().product()
        }
        None => {
            let data: &[E] = tensor.storage();
            StridedIter::new(tensor.layout())
                .map(|idx| data[idx])
                .product()
        }
    };

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Cast before product: `mask.cast(DType::I32).prod()`, or use `all`-style logic for booleans.
  2. For quantized tensors, dequantize first.
  3. Beware integer overflow — prefer widening dtypes (I32/I64) before prod.

Example fix

// before
let all_match = mask.prod(); // Bool
// after
let all_match = mask.cast(DType::I32).prod();
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

fn is_prod_capable(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::prod()` on a Bool or quantized tensor — e.g. computing a conjunction over a boolean mask with product instead of logical all.

Common situations: Using prod as a logical AND over bool masks without casting; product over quantized values; dtype inferred from upstream ops in generic code.

Related errors


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