Commit 9209a42
Fix flaky Enzyme test_forward/test_reverse: StableRNG + accuracy-matched tolerance (#58)
The Enzyme `@easy_rule` returns the *exact* `^` derivative, but EnzymeTestUtils
`test_forward`/`test_reverse` compare it against finite differences of the
*approximate* `fastpower` primal. Because `fastpower` routes through a Float32
`fastlog2` polynomial, the *slope* of its primal differs from the exact slope by
~1e-2 relative near x=1 (measured: exact d/dx = 0.5 vs FD-of-fastpower = 0.5066,
i.e. 1.3e-2 relative), even at points like (1.0, 0.5) where the primal *value* is
exact. So the FD reference is off from the exact rule by `fastpower`'s inherent
approximation error, not by any rule bug. The old atol=1e-4, rtol=1e-3 sat below
that gap, so whether the lane passed depended on the random tangents drawn from
the global RNG and it went red intermittently (~4% of draws).
Two-part fix:
1. Determinism via StableRNG, not Xoshiro. Seeding the global RNG / `Xoshiro`
does not actually pin the test, because those streams can change across Julia
versions, so the flake could reappear on a new Julia. `StableRNGs.StableRNG`
yields a stream guaranteed identical across Julia versions, passed as the
`rng=` keyword that EnzymeTestUtils accepts.
2. Tolerance matched to fastpower's documented accuracy (atol=1e-3, rtol=1e-2),
not reverted to the tight 1e-4/1e-3. Empirically the inherent gap is real: with
the tight tolerance, 8/10 candidate StableRNG seeds pass the forward grid 52/52
but seeds 123 and 31415 fail, and the all-seeds failure boundary is rtol~2e-3.
Reverting to rtol=1e-3 would only "pass" by cherry-picking a lucky seed, which
would hide the genuine (benign, expected) primal-approximation error. The chosen
rtol=1e-2 sits ~5x above the measured worst-case relative discrepancy yet far
below the O(1) relative error a genuinely wrong derivative rule would produce,
so real regressions are still caught. Verified deterministic forward 52/52 +
reverse 36/36 across 3 repeats on both Julia 1 and lts, and 52/52 for all 10
candidate seeds on lts (so the tolerance is seed-independent, not seed-luck).
Swap the test dep Random -> StableRNGs in [extras]/[targets].test/[compat].
Co-authored-by: ChrisRackauckas-Claude <accounts@chrisrackauckas.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 6028e01 commit 9209a42
3 files changed
Lines changed: 28 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| |||
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| 49 | + | |
48 | 50 | | |
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
52 | | - | |
| 54 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
5 | 22 | | |
6 | 23 | | |
7 | 24 | | |
8 | 25 | | |
9 | 26 | | |
10 | | - | |
| 27 | + | |
11 | 28 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
5 | 11 | | |
6 | 12 | | |
7 | 13 | | |
8 | | - | |
| 14 | + | |
9 | 15 | | |
0 commit comments