Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Mooncake = "0.4, 0.5"
ReverseDiff = "1.14"
SafeTestsets = "0.1, 1"
SciMLTesting = "1"
StableRNGs = "1"
Test = "1"
Tracker = "0.2"
julia = "1.10"
Expand All @@ -45,8 +46,9 @@ Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
SciMLTesting = "09d9d899-5365-40a9-917a-5f67fddea283"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"

[targets]
test = ["Enzyme", "EnzymeTestUtils", "ForwardDiff", "Mooncake", "ReverseDiff", "SafeTestsets", "SciMLTesting", "Test", "Tracker"]
test = ["Enzyme", "EnzymeTestUtils", "ForwardDiff", "Mooncake", "ReverseDiff", "SafeTestsets", "SciMLTesting", "StableRNGs", "Test", "Tracker"]
19 changes: 18 additions & 1 deletion test/Enzyme/enzyme_forward_tests.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
using FastPower: fastpower
using Enzyme, EnzymeTestUtils
using StableRNGs
using Test

# `test_forward` compares the rule (which returns the *exact* `^` derivative) 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 (even where the primal value itself is exact), so the FD reference
# is off from the exact rule by that inherent approximation error rather than by any rule bug.
# The previous atol=1e-4, rtol=1e-3 sat below that gap, so whether the lane passed depended on
# the random tangents `test_forward` drew from the global RNG (it went red ~4% of the time).
#
# Fix: draw the tangents from a StableRNG (a fixed seed gives the *same* stream on every Julia
# version, unlike the global RNG / Xoshiro whose stream can change across versions) so the test
# is genuinely deterministic, and use atol=1e-3, rtol=1e-2 matched to `fastpower`'s documented
# accuracy envelope (see test/fast_pow_tests.jl). That tolerance is ~5x above the measured
# worst-case relative discrepancy in this grid (~2e-3) yet far below the O(1) relative error a
# genuinely wrong derivative rule would produce, so real regressions are still caught. Reverting
# to rtol=1e-3 is not possible without cherry-picking a lucky seed to hide the inherent gap.
rng = StableRNG(123)
@testset for RT in (Duplicated, DuplicatedNoNeed),
Tx in (Const, Duplicated),
Ty in (Const, Duplicated)
x = 1.0
y = 0.5
test_forward(fastpower, RT, (x, Tx), (y, Ty), atol = 1.0e-4, rtol = 1.0e-3)
test_forward(fastpower, RT, (x, Tx), (y, Ty), rng = rng, atol = 1.0e-3, rtol = 1.0e-2)
end
8 changes: 7 additions & 1 deletion test/Enzyme/enzyme_reverse_tests.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using FastPower: fastpower
using Enzyme, EnzymeTestUtils
using StableRNGs
using Test

# See test/Enzyme/enzyme_forward_tests.jl: the finite-difference reference is taken on the
# approximate `fastpower` primal, so the tolerance must cover its inherent approximation error.
# Draw the cotangents from a StableRNG (stream is identical across Julia versions, so the test
# is deterministic) and match `fastpower`'s documented accuracy with atol=1e-3, rtol=1e-2.
rng = StableRNG(123)
@testset for RT in (Active,), Tx in (Active, Const), Ty in (Active, Const)
x = 1.0
y = 0.5
test_reverse(fastpower, RT, (x, Tx), (y, Ty), atol = 1.0e-4, rtol = 1.0e-3)
test_reverse(fastpower, RT, (x, Tx), (y, Ty), rng = rng, atol = 1.0e-3, rtol = 1.0e-2)
end
Loading