{"record":{"id":"6df93fe60b595593","repo":"tracel-ai/burn","slug":"cannot-compute-min-of-empty-tensor","errorCode":null,"errorMessage":"Cannot compute min of empty tensor","messagePattern":"Cannot compute min of empty tensor","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-ndarray/src/ops/base.rs","lineNumber":1296,"sourceCode":"            .copied()\n            .reduce(|a, b| {\n                if a.partial_cmp(&a).is_none() || a > b {\n                    a\n                } else {\n                    b\n                }\n            })\n            .expect(\"Cannot compute max of empty tensor\");\n        ArrayD::from_elem(IxDyn(&[1]), max).into_shared()\n    }\n\n    /// Min of all elements - zero-copy for borrowed storage.\n    pub fn min_view(view: ArrayView<'_, E, IxDyn>) -> SharedArray<E> {\n        let min = view\n            .iter()\n            .copied()\n            .reduce(|a, b| if a < b { a } else { b })\n            .expect(\"Cannot compute min of empty tensor\");\n        ArrayD::from_elem(IxDyn(&[1]), min).into_shared()\n    }\n\n    /// Min of all floating-point elements with NaN propagation.\n    pub fn min_float_view(view: ArrayView<'_, E, IxDyn>) -> SharedArray<E>\n    where\n        E: FloatNdArrayElement,\n    {\n        let min = view\n            .iter()\n            .copied()\n            .reduce(|a, b| {\n                if a.partial_cmp(&a).is_none() || a < b {\n                    a\n                } else {\n                    b\n                }\n            })","sourceCodeStart":1278,"sourceCodeEnd":1314,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-ndarray/src/ops/base.rs#L1278-L1314","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nlet lo = empty.min(); // panics\n// after\nif empty.num_elements() > 0 {\n    let lo = empty.min();\n} else {\n    // handle empty case\n}","handlingStrategy":"validation","validationCode":"fn safe_min<E: burn_ndarray::FloatElement, const D: usize>(t: &Tensor<NdArray<E>, D>) -> Option<Tensor<NdArray<E>, 1>> {\n    (t.num_elements() > 0).then(|| t.min())\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["rust","ndarray","panic","min","empty-tensor","reduction"],"backgroundTag":"empty-tensor-reduction","analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}