ONNX ReduceMean overflows before reaching a finite mean
Two finite equal inputs have a finite mean, yet both the conformance reference and Runtime CPU provider return infinity because they sum in the input type before dividing.
Finding
ReduceMean should divide the mathematical sum of a slice by its element count. Released versions of the ONNX Python ReferenceEvaluator and ONNX Runtime CPU provider first accumulate that sum in the input floating-point type. The avoidable intermediate can overflow even when every input and the final mean are finite and representable.
Minimal finite counterexamples
dtype input exact mean reference runtime CPU
float32 [3e38, 3e38] 3e38 inf inf
float64 [1e308, 1e308] 1e308 inf inf
For either equal pair, the exact mean is simply the repeated input. The failure therefore does not depend on a delicate external approximation: only the unnecessary intermediate sum exceeds the corresponding floating-point range.
Independent oracle
For a finite nonzero scale s = max(abs(x)), the same mean can be written as:
mean(x) = s · mean(x / s)
The public reproducer uses this scale-first identity. It compares both released implementations at operator versions 17 and 18. Ordinary [3, 5], all-zero and exact-cancellation controls are checked separately.
Verification
- The executable checks float32 and float64 examples at both operator versions.
- All eight implementation comparisons mismatch the independent oracle. This is a comparison count, not a claim of eight distinct defects.
- The three control cases pass in both implementations.
- A separate 41-case candidate matrix covered float16, float32 and float64; multiple axes and
keepdimschoices; empty slices; zeros; and nonfinite inputs. - Bounded GitHub searches combining
ReduceMeanwith overflow, finite range and numerical stability found no direct duplicate in the reviewed ONNX and ONNX Runtime results. This is not an exhaustive priority claim.
Corrections and falsification
The ONNX reference correction scales each nonempty floating or complex slice before taking its mean, retains dimensions internally for correct broadcasting, and then applies the requested dimension behavior. The full reference-evaluator test module reported 461 passes and 4 skips; the 41 additional candidate checks also passed. Restoring the former direct np.mean path makes both new finite-range tests fail. The correction is submitted as onnx/onnx#8456; DCO, initial lint and several platform jobs are green, while the remaining matrix is still running.
The ONNX Runtime CPU correction promotes float accumulation to double and uses a dynamically scaled signed sum for double, where no portable wider floating type is available. Existing integer and custom-type behavior is left unchanged. All 38 local ReductionOpTest.ReduceMean* tests pass, including new coverage of all-axis, KR, RK, KRK and RKR layouts. Disabling the new finite-range path makes both new tests fail across the covered layouts; restoring it returns 38 passes. The correction is submitted as microsoft/onnxruntime#32576; it is mergeable and the CLA check is green.
Impact and boundary
A reduction mean is a basic graph primitive. In the demonstrated cases its finite result is replaced completely by infinity in both a conformance reference and a deployed CPU runtime. That shared behavior can also make a correct backend appear inconsistent when it is checked against the reference.
This report covers only the released ONNX ReferenceEvaluator and ONNX Runtime CPU provider on the stated cases. Other execution providers, gradients, production-model prevalence and the performance cost of the Runtime correction were not measured. The corrected algorithms improve this finite-range failure; they are not a claim of exact summation for every cancellation pattern. This is an ordinary numerical-correctness report, not a security claim.
