Skip to content

Make the Float16 statistics and ComplexF16 product tests robust to summation order - #834

Merged
maleadt merged 3 commits into
mainfrom
tb/flaky-tests
Oct 23, 2026
Merged

maleadt merged 3 commits into
mainfrom
tb/flaky-tests

Conversation

@maleadt

@maleadt maleadt commented Oct 22, 2026

Copy link
Copy Markdown
Member

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 exceeds isapprox's relative tolerance. The sin tests now draw from [0, 1) again, as before Make stats tests numerically more stable for oneAPI #662.
  • cov on Float16: with 100 samples from 1:100, the diagonal overflows to Inf. The array isapprox then 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 in cor(A; dims=2) that Make stats tests numerically more stable for oneAPI #662 was fixing.
  • ComplexF16 * in the mapreducedim! tests: ten values from [0, 1) multiply to around 1e-4. norm of 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: cov 1.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 cancelling mean(sin, A) input on every run. With this PR on top, #825 passes the statistics and reductions/mapreducedim! testsuites on Metal under the CI seeding.

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.
@maleadt
maleadt merged commit 440f640 into main Oct 23, 2026
18 checks passed
@maleadt
maleadt deleted the tb/flaky-tests branch October 23, 2026 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant