tracel-ai/burn · error
Cannot compute min of empty tensor
Error message
Cannot compute min of empty tensor
What it means
min_view reduces a tensor view to its minimum element with reduce(); an empty iterator yields None and the expect() panics because the minimum of zero elements is undefined. Triggered by full min reductions on the ndarray backend.
Source
Thrown at crates/burn-ndarray/src/ops/base.rs:1296
.copied()
.reduce(|a, b| {
if a.partial_cmp(&a).is_none() || a > b {
a
} else {
b
}
})
.expect("Cannot compute max of empty tensor");
ArrayD::from_elem(IxDyn(&[1]), max).into_shared()
}
/// Min of all elements - zero-copy for borrowed storage.
pub fn min_view(view: ArrayView<'_, E, IxDyn>) -> SharedArray<E> {
let min = view
.iter()
.copied()
.reduce(|a, b| if a < b { a } else { b })
.expect("Cannot compute min of empty tensor");
ArrayD::from_elem(IxDyn(&[1]), min).into_shared()
}
/// Min of all floating-point elements with NaN propagation.
pub fn min_float_view(view: ArrayView<'_, E, IxDyn>) -> SharedArray<E>
where
E: FloatNdArrayElement,
{
let min = view
.iter()
.copied()
.reduce(|a, b| {
if a.partial_cmp(&a).is_none() || a < b {
a
} else {
b
}
})View on GitHub (pinned to d16f7ba2ed)
Solutions
- Guard with a num_elements() == 0 check before calling min and handle the empty case
- Correct upstream slicing so no dimension becomes 0
- Skip empty tensors in metric/loss aggregation loops
Example fix
// before
let lo = empty.min(); // panics
// after
if empty.num_elements() > 0 {
let lo = empty.min();
} else {
// handle empty case
} Defensive patterns
Strategy: validation
Validate before calling
fn safe_min<E: burn_ndarray::FloatElement, const D: usize>(t: &Tensor<NdArray<E>, D>) -> Option<Tensor<NdArray<E>, 1>> {
(t.num_elements() > 0).then(|| t.min())
} Prevention
- Check num_elements() > 0 before min reductions
- Fix upstream slicing so no dim becomes 0
- Skip empty tensors in clipping/normalization pipelines
- Add a debug assertion on shape before reductions in helpers
When it happens
Trigger: Calling Tensor::min_dim / min reduction on a tensor containing zero elements - any dimension of size 0, e.g. from an empty slice range or an empty batch.
Common situations: Empty data batch reaching a min-based normalization; clipping via min on an empty intermediate tensor; dynamic/conditional code paths that produce 0-sized tensors.
Related errors
- Cannot compute max of empty tensor
- any_float: unsupported dtype {:?}
- all_float: unsupported dtype {:?}
- any_int: unsupported dtype {:?}
- all_int: unsupported dtype {:?}
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/6df93fe60b595593.
Report an issue: GitHub.