diff --git a/NEWS.md b/NEWS.md index 91f3cb7..b4e1b9f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,21 @@ # FindFirstFunctions.jl NEWS +## 3.3.0 + +New `FindFirstFunctionsEnzymeCoreExt` weak-dependency extension (loads with +`EnzymeCore`). It declares every search entry point — `searchsorted_last`, +`searchsorted_first`, the batched `searchsortedlast!` / `searchsortedfirst!` / +`searchsortedrange`, and the equality searches `findequal` / +`findfirstequal` / `findfirstsortedequal` — as `EnzymeRules.inactive`. + +The searches return or fill indices and see their arguments only through +comparisons, so no derivative flows through them and the declaration is exact. +It also keeps Enzyme's reverse pass out of the early-exit search loops, whose +`gc_preserve` token pairs currently trip an LLVM verifier abort in reverse mode +(EnzymeAD/Enzyme.jl#3404) — previously any reverse-mode differentiation +through an interpolation interval lookup, e.g. DataInterpolations inside an ODE +right-hand side under `EnzymeVJP`, aborted the process. + ## 3.0.0 The 2.x sorted-search API was a single generic diff --git a/Project.toml b/Project.toml index 0536491..ba40a16 100644 --- a/Project.toml +++ b/Project.toml @@ -1,12 +1,20 @@ name = "FindFirstFunctions" uuid = "64ca27bc-2ba2-4a57-88aa-44e436879224" -version = "3.2.1" +version = "3.3.0" authors = ["Chris Elrod and contributors"] [deps] PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" +[weakdeps] +EnzymeCore = "f151be2c-9106-41f4-ab19-57ee4f262869" + +[extensions] +FindFirstFunctionsEnzymeCoreExt = "EnzymeCore" + [compat] +Enzyme = "0.13" +EnzymeCore = "0.8" ForwardDiff = "1" PrecompileTools = "1.0.1" SafeTestsets = "0.1" @@ -16,6 +24,7 @@ Test = "1.10" julia = "1.10" [extras] +Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" SciMLTesting = "09d9d899-5365-40a9-917a-5f67fddea283" @@ -23,4 +32,4 @@ StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["ForwardDiff", "SafeTestsets", "SciMLTesting", "StableRNGs", "Test"] +test = ["Enzyme", "ForwardDiff", "SafeTestsets", "SciMLTesting", "StableRNGs", "Test"] diff --git a/ext/FindFirstFunctionsEnzymeCoreExt.jl b/ext/FindFirstFunctionsEnzymeCoreExt.jl new file mode 100644 index 0000000..6acce3e --- /dev/null +++ b/ext/FindFirstFunctionsEnzymeCoreExt.jl @@ -0,0 +1,14 @@ +module FindFirstFunctionsEnzymeCoreExt + +using FindFirstFunctions +using EnzymeCore: EnzymeRules + +for f in ( + searchsorted_last, searchsorted_first, + searchsortedlast!, searchsortedfirst!, searchsortedrange, + findequal, findfirstequal, findfirstsortedequal, + ) + @eval EnzymeRules.inactive(::typeof($f), args...; kwargs...) = nothing +end + +end diff --git a/test/Enzyme/enzyme_tests.jl b/test/Enzyme/enzyme_tests.jl new file mode 100644 index 0000000..26ea933 --- /dev/null +++ b/test/Enzyme/enzyme_tests.jl @@ -0,0 +1,51 @@ +@safetestset "EnzymeCore extension: searches are inactive" begin + using FindFirstFunctions, Enzyme, Test + + function interp(u, knots, x) + i0 = searchsorted_last(KIND_BINARY_BRACKET, knots, x) + i = clamp(i0, 1, length(knots) - 1) + w = (x - knots[i]) / (knots[i + 1] - knots[i]) + return (1 - w) * u[i] + w * u[i + 1] + end + + knots = collect(0.0:0.5:10.0) + u = sin.(knots) + x = 3.3 + + iref = searchsorted_last(KIND_BINARY_BRACKET, knots, x) + wref = (x - knots[iref]) / (knots[iref + 1] - knots[iref]) + + @testset "reverse mode, gradient w.r.t. the knot values" begin + du = zero(u) + Enzyme.autodiff(Reverse, interp, Active, Duplicated(u, du), Const(knots), Const(x)) + expected = zero(u) + expected[iref] = 1 - wref + expected[iref + 1] = wref + @test du ≈ expected + end + + @testset "reverse mode, derivative w.r.t. the query point" begin + # The search runs on the active input `x`; only the linear weight + # contributes, with slope (u[i+1] - u[i]) / h. + dx = Enzyme.autodiff(Reverse, interp, Active, Const(u), Const(knots), Active(x))[1][3] + @test dx ≈ (u[iref + 1] - u[iref]) / (knots[iref + 1] - knots[iref]) + end + + @testset "forward mode agrees" begin + dx = Enzyme.autodiff(Forward, interp, Duplicated, Const(u), Const(knots), Duplicated(x, 1.0))[1] + @test dx ≈ (u[iref + 1] - u[iref]) / (knots[iref + 1] - knots[iref]) + end + + @testset "batched and equality searches accept the rule" begin + function g(u, knots, xs) + idx = similar(xs, Int) + searchsortedlast!(idx, knots, xs) + j = findfirstsortedequal(knots[3], knots) + return sum(u) + zero(eltype(u)) * (idx[1] + something(j, 0)) + end + xs = [0.7, 2.2, 9.1] + du = zero(u) + Enzyme.autodiff(Reverse, g, Active, Duplicated(u, du), Const(knots), Const(xs)) + @test all(==(1.0), du) + end +end diff --git a/test/test_groups.toml b/test/test_groups.toml index 09a4f24..0c58956 100644 --- a/test/test_groups.toml +++ b/test/test_groups.toml @@ -1,6 +1,6 @@ # Root test-group matrix for FindFirstFunctions.jl, consumed by the canonical # SciML/.github grouped-tests.yml@v1 caller (.github/workflows/Tests.yml). -# runtests.jl reads ENV["GROUP"] and dispatches Core / QA. +# runtests.jl reads ENV["GROUP"] and dispatches Core / QA / Enzyme. [Core] versions = ["1", "lts", "pre"] @@ -18,3 +18,6 @@ arch = "x86" [QA] versions = ["lts", "1"] + +[Enzyme] +versions = ["1"]