Repository navigation
Make the Float16 statistics and ComplexF16 product tests robust to summation order - #834
Merged
Merged
Conversation
Since #662 the Float16 statistics tests draw integers from 1:100, which keeps std and var stable but lets the sines cancel: the mean of four can land near zero, where the GPU's and the CPU's summation order differ by more than isapprox allows. Over random inputs that is rare (about 0.5% on Metal), but the test runner seeds the RNG per file and Metal draws a seed on every kernel launch, so the input depends on how many kernels the preceding tests launched. #825 adds one to `mean(A; dims)`, which makes Metal on Julia 1.10 pick a cancelling input every time. Draw the sin inputs from [0, 1) again, as before #662.
The product of ten ComplexF16 values from [0, 1) is typically around 1e-4. Its squared components underflow, so `norm` of the one-element result is zero and the array `isapprox` accepts no difference at all; products that land in the subnormal range also round differently on the CPU and the GPU. Either made the whole-vector `*` reductions fail in about 0.1% of runs on Metal. Values on the unit circle keep the product near one.
#662 drew the Float16 cov and cor inputs from 1:100, which avoids the subnormal variances of two-element rows in `cor(A; dims=2)` but makes `cov` of 100 samples overflow: the diagonal is Inf on both sides, so the array `isapprox` compares element by element, and the near-zero off-diagonal then differs with the summation order (about 1.7% of runs on Metal). Integers from 1:10 avoid both problems.
This was referenced Oct 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three testsuite comparisons can fail on correct results, because CPU and GPU reductions sum in a different order and the test data leaves no room for that:
mean(sin, A)on Float16: since Make stats tests numerically more stable for oneAPI #662, the Float16 inputs are integers from 1:100. Their sines have mixed signs, so a mean of four can land near zero, where rounding in the order of summation exceedsisapprox's relative tolerance. Thesintests now draw from[0, 1)again, as before Make stats tests numerically more stable for oneAPI #662.covon Float16: with 100 samples from 1:100, the diagonal overflows toInf. The arrayisapproxthen compares element by element, and the off-diagonal is close to zero. With 1:10 the diagonal stays finite. Integers also still avoid the subnormal variances incor(A; dims=2)that Make stats tests numerically more stable for oneAPI #662 was fixing.*in themapreducedim!tests: ten values from[0, 1)multiply to around 1e-4.normof that one-element result underflows to zero, so the comparison tolerates no error at all. Products in the subnormal range also round differently on the CPU and the GPU. These tests now draw values on the unit circle.Measured failure rates on Metal over random inputs:
cov1.7%,mean(sin)0.5%, ComplexF16 products 0.1%. All are 0 in several thousand trials after the change.On CI the rates are misleading. ParallelTestRunner seeds the RNG per test file, and Metal.jl draws a host random number on every kernel launch to seed the device RNG. So each test's input is fixed by how many kernels ran before it. #825 adds a launch to
mean(A; dims), and with that Metal on Julia 1.10 gets a cancellingmean(sin, A)input on every run. With this PR on top, #825 passes thestatisticsandreductions/mapreducedim!testsuites on Metal under the CI seeding.