tracel-ai/burn · error
int_mean: unsupported dtype {:?}
Error message
int_mean: unsupported dtype {:?} What it means
int_mean computes the mean of an integer tensor as a scalar in the same dtype. It only supports signed integer dtypes I64/I32/I16/I8 — note that unsigned dtypes (U8/U16/U32/U64) are NOT handled and hit the panic arm. The panic fires when the tensor's dtype is unsigned or otherwise not in the match.
Source
Thrown at crates/burn-flex/src/ops/int.rs:1046
let sum_result = crate::ops::reduce::sum(tensor);
// Compute in i64 to avoid truncation of n for small int types
macro_rules! compute_mean {
($ty:ty) => {{
let data: &[$ty] = sum_result.storage();
let mean_val = (data[0] as i64 / n as i64) as $ty;
FlexTensor::new(
Bytes::from_elems(alloc::vec![mean_val]),
Layout::contiguous(Shape::from(alloc::vec![1])),
dtype,
)
}};
}
match dtype {
DType::I64 => compute_mean!(i64),
DType::I32 => compute_mean!(i32),
DType::I16 => compute_mean!(i16),
DType::I8 => compute_mean!(i8),
other => panic!("int_mean: unsupported dtype {:?}", other),
}
}
fn int_max(tensor: IntTensor<Flex>) -> IntTensor<Flex> {
crate::ops::reduce::max(tensor)
}
fn int_max_dim(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {
crate::ops::reduce::max_dim(tensor, dim)
}
fn int_min(tensor: IntTensor<Flex>) -> IntTensor<Flex> {
crate::ops::reduce::min(tensor)
}
fn int_min_dim(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {
crate::ops::reduce::min_dim(tensor, dim)
}View on GitHub (pinned to d16f7ba2ed)
Solutions
- Cast the tensor to a signed dtype (i64 or i32) before calling mean: tensor.cast::<i64>().mean().
- If float semantics are desired, cast to float and use the float mean (avoids integer truncation too).
- Convert unsigned image data to i32/f32 at load time so later reductions are safe.
- Watch out: even for supported dtypes, int_mean truncates (integer division) — prefer float mean for accurate averages.
Example fix
// before: u8 image tensor let m = image_tensor.mean(); // panics: dtype U8 // after let m = image_tensor.cast::<f32>().mean(); // or .cast::<i64>().mean() for int semantics
Defensive patterns
Strategy: validation
Validate before calling
assert!(matches!(t.dtype(), DType::I64 | DType::I32 | DType::I16 | DType::I8), "int_mean only supports signed ints, got {:?}", t.dtype()); Type guard
fn is_signed_int(t: &FlexTensor) -> bool { matches!(t.dtype(), DType::I64 | DType::I32 | DType::I16 | DType::I8) } Prevention
- Cast u8/u16/u32/u64 tensors to i64 or f32 before calling mean
- Prefer float mean for accurate averages (int_mean truncates)
- Convert image (uint8) data to f32/i32 at load time
When it happens
Trigger: Calling int_mean (mean() on an integer tensor) where the tensor dtype is U8/U16/U32/U64, a float dtype, or Bool.
Common situations: Taking the mean of a u8 image tensor (very common in image preprocessing) or other unsigned data; porting PyTorch code where mean works on any numeric dtype; tensors loaded from uint8 PNG/JPEG data.
Related errors
- mean_dim: unsupported dtype {:?}
- burn-flex does not support Bool(U32) storage (only Native an
- compare_int: unsupported dtype {:?}
- compare_int_elem: unsupported dtype {:?}
- any_float: unsupported dtype {:?}
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/a331014c6f417c17.
Report an issue: GitHub.