From 7bd69fb4aa83527926c6b6c7f874cb8bca9f1446 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 10 Jun 2025 17:25:12 +0200 Subject: [PATCH 1/3] Add tests --- .../onearg.jl | 10 +++--- .../twoarg.jl | 6 ++-- .../test/Back/FiniteDiff/test.jl | 32 +++++++++++++++++++ 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/DifferentiationInterface/ext/DifferentiationInterfaceFiniteDiffExt/onearg.jl b/DifferentiationInterface/ext/DifferentiationInterfaceFiniteDiffExt/onearg.jl index cf298f263..46ac5e37c 100644 --- a/DifferentiationInterface/ext/DifferentiationInterfaceFiniteDiffExt/onearg.jl +++ b/DifferentiationInterface/ext/DifferentiationInterfaceFiniteDiffExt/onearg.jl @@ -27,7 +27,7 @@ function DI.prepare_pushforward_nokwarg( absstep = if isnothing(backend.absstep) relstep else - backend.relstep + backend.absstep end dir = backend.dir return FiniteDiffOneArgPushforwardPrep(_sig, cache, relstep, absstep, dir) @@ -144,7 +144,7 @@ function DI.prepare_derivative_nokwarg( absstep = if isnothing(backend.absstep) relstep else - backend.relstep + backend.absstep end dir = backend.dir return FiniteDiffOneArgDerivativePrep(_sig, cache, relstep, absstep, dir) @@ -269,7 +269,7 @@ function DI.prepare_gradient_nokwarg( absstep = if isnothing(backend.absstep) relstep else - backend.relstep + backend.absstep end dir = backend.dir return FiniteDiffGradientPrep(_sig, cache, relstep, absstep, dir) @@ -359,7 +359,7 @@ function DI.prepare_jacobian_nokwarg( absstep = if isnothing(backend.absstep) relstep else - backend.relstep + backend.absstep end dir = backend.dir return FiniteDiffOneArgJacobianPrep(_sig, cache, relstep, absstep, dir) @@ -463,7 +463,7 @@ function DI.prepare_hessian_nokwarg( relstep_h = if isnothing(backend.relstep) default_relstep(fdhtype(backend), eltype(x)) else - backend.relstep + backend.absstep end absstep_g = isnothing(backend.absstep) ? relstep_g : backend.absstep absstep_h = isnothing(backend.absstep) ? relstep_h : backend.absstep diff --git a/DifferentiationInterface/ext/DifferentiationInterfaceFiniteDiffExt/twoarg.jl b/DifferentiationInterface/ext/DifferentiationInterfaceFiniteDiffExt/twoarg.jl index 35e2fb047..f8b70c6dd 100644 --- a/DifferentiationInterface/ext/DifferentiationInterfaceFiniteDiffExt/twoarg.jl +++ b/DifferentiationInterface/ext/DifferentiationInterfaceFiniteDiffExt/twoarg.jl @@ -31,7 +31,7 @@ function DI.prepare_pushforward_nokwarg( absstep = if isnothing(backend.absstep) relstep else - backend.relstep + backend.absstep end dir = backend.dir return FiniteDiffTwoArgPushforwardPrep(_sig, cache, relstep, absstep, dir) @@ -175,7 +175,7 @@ function DI.prepare_derivative_nokwarg( absstep = if isnothing(backend.absstep) relstep else - backend.relstep + backend.absstep end dir = backend.dir return FiniteDiffTwoArgDerivativePrep(_sig, cache, relstep, absstep, dir) @@ -295,7 +295,7 @@ function DI.prepare_jacobian_nokwarg( absstep = if isnothing(backend.absstep) relstep else - backend.relstep + backend.absstep end dir = backend.dir return FiniteDiffTwoArgJacobianPrep(_sig, cache, relstep, absstep, dir) diff --git a/DifferentiationInterface/test/Back/FiniteDiff/test.jl b/DifferentiationInterface/test/Back/FiniteDiff/test.jl index 911dab203..73b5be088 100644 --- a/DifferentiationInterface/test/Back/FiniteDiff/test.jl +++ b/DifferentiationInterface/test/Back/FiniteDiff/test.jl @@ -72,3 +72,35 @@ end logging=LOGGING, ) end; + +@testset "Step size" begin # fix 811 + backend = AutoFiniteDiff(; absstep=1000, relstep=0.1) + preps = [ + prepare_pushforward(identity, backend, 1.0, (1.0,)) + prepare_pushforward(copyto!, [0.0], backend, [1.0], ([1.0],)) + prepare_derivative(identity, backend, 1.0) + prepare_derivative((y, x) -> y .= x, [0.0], backend, 1.0) + prepare_gradient(sum, backend, [1.0]) + prepare_jacobian(identity, backend, [1.0]) + prepare_jacobian(copyto!, [0.0], backend, [1.0]) + ] + for prep in preps + @test prep.relstep == 0.1 + @test prep.absstep == 1000 + end + + backend = AutoFiniteDiff(; relstep=0.1) + preps = [ + prepare_pushforward(identity, backend, 1.0, (1.0,)) + prepare_pushforward(copyto!, [0.0], backend, [1.0], ([1.0],)) + prepare_derivative(identity, backend, 1.0) + prepare_derivative((y, x) -> y .= x, [0.0], backend, 1.0) + prepare_gradient(sum, backend, [1.0]) + prepare_jacobian(identity, backend, [1.0]) + prepare_jacobian(copyto!, [0.0], backend, [1.0]) + ] + for prep in preps + @test prep.relstep == 0.1 + @test prep.absstep == 0.1 + end +end From 3b9665d21ea179322985181ee37a41761a12b71b Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 10 Jun 2025 19:18:57 +0200 Subject: [PATCH 2/3] Fix --- .../DifferentiationInterfaceFiniteDiffExt/onearg.jl | 12 ++++++++++-- .../test/Back/FiniteDiff/test.jl | 10 ++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/DifferentiationInterface/ext/DifferentiationInterfaceFiniteDiffExt/onearg.jl b/DifferentiationInterface/ext/DifferentiationInterfaceFiniteDiffExt/onearg.jl index 46ac5e37c..b16edaa05 100644 --- a/DifferentiationInterface/ext/DifferentiationInterfaceFiniteDiffExt/onearg.jl +++ b/DifferentiationInterface/ext/DifferentiationInterfaceFiniteDiffExt/onearg.jl @@ -462,11 +462,19 @@ function DI.prepare_hessian_nokwarg( end relstep_h = if isnothing(backend.relstep) default_relstep(fdhtype(backend), eltype(x)) + else + backend.relstep + end + absstep_g = if isnothing(backend.absstep) + relstep_g + else + backend.absstep + end + absstep_h = if isnothing(backend.absstep) + relstep_h else backend.absstep end - absstep_g = isnothing(backend.absstep) ? relstep_g : backend.absstep - absstep_h = isnothing(backend.absstep) ? relstep_h : backend.absstep return FiniteDiffHessianPrep( _sig, gradient_cache, hessian_cache, relstep_g, absstep_g, relstep_h, absstep_h ) diff --git a/DifferentiationInterface/test/Back/FiniteDiff/test.jl b/DifferentiationInterface/test/Back/FiniteDiff/test.jl index 73b5be088..0a4be67fb 100644 --- a/DifferentiationInterface/test/Back/FiniteDiff/test.jl +++ b/DifferentiationInterface/test/Back/FiniteDiff/test.jl @@ -88,6 +88,11 @@ end; @test prep.relstep == 0.1 @test prep.absstep == 1000 end + prep = prepare_hessian(sum, backend, [1.0]) + @test prep.absstep_g == 1000 + @test prep.absstep_h == 1000 + @test prep.relstep_g == 0.1 + @test prep.relstep_h == 0.1 backend = AutoFiniteDiff(; relstep=0.1) preps = [ @@ -103,4 +108,9 @@ end; @test prep.relstep == 0.1 @test prep.absstep == 0.1 end + prep = prepare_hessian(sum, backend, [1.0]) + @test prep.absstep_g == 0.1 + @test prep.absstep_h == 0.1 + @test prep.relstep_g == 0.1 + @test prep.relstep_h == 0.1 end From ec37c309fd43113dff94091985aa365f2be4dc28 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 10 Jun 2025 20:50:57 +0200 Subject: [PATCH 3/3] Changelog --- DifferentiationInterface/CHANGELOG.md | 2 ++ .../test/Back/FiniteDiff/test.jl | 28 +++++++++---------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/DifferentiationInterface/CHANGELOG.md b/DifferentiationInterface/CHANGELOG.md index 7dee7f9ba..22d83c275 100644 --- a/DifferentiationInterface/CHANGELOG.md +++ b/DifferentiationInterface/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Take `absstep` into account for FiniteDiff ([#812]) - Make basis work for `CuArray` ([#810]) ## [0.7.0] @@ -39,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [0.6.54]: https://github.com/JuliaDiff/DifferentiationInterface.jl/compare/DifferentiationInterface-v0.6.53...DifferentiationInterface-v0.6.54 [0.6.53]: https://github.com/JuliaDiff/DifferentiationInterface.jl/compare/DifferentiationInterface-v0.6.52...DifferentiationInterface-v0.6.53 +[#812]: https://github.com/JuliaDiff/DifferentiationInterface.jl/pull/812 [#810]: https://github.com/JuliaDiff/DifferentiationInterface.jl/pull/810 [#799]: https://github.com/JuliaDiff/DifferentiationInterface.jl/pull/799 [#795]: https://github.com/JuliaDiff/DifferentiationInterface.jl/pull/795 diff --git a/DifferentiationInterface/test/Back/FiniteDiff/test.jl b/DifferentiationInterface/test/Back/FiniteDiff/test.jl index 0a4be67fb..9681bcab5 100644 --- a/DifferentiationInterface/test/Back/FiniteDiff/test.jl +++ b/DifferentiationInterface/test/Back/FiniteDiff/test.jl @@ -76,13 +76,13 @@ end; @testset "Step size" begin # fix 811 backend = AutoFiniteDiff(; absstep=1000, relstep=0.1) preps = [ - prepare_pushforward(identity, backend, 1.0, (1.0,)) - prepare_pushforward(copyto!, [0.0], backend, [1.0], ([1.0],)) - prepare_derivative(identity, backend, 1.0) - prepare_derivative((y, x) -> y .= x, [0.0], backend, 1.0) - prepare_gradient(sum, backend, [1.0]) - prepare_jacobian(identity, backend, [1.0]) - prepare_jacobian(copyto!, [0.0], backend, [1.0]) + prepare_pushforward(identity, backend, 1.0, (1.0,)), + prepare_pushforward(copyto!, [0.0], backend, [1.0], ([1.0],)), + prepare_derivative(identity, backend, 1.0), + prepare_derivative((y, x) -> y .= x, [0.0], backend, 1.0), + prepare_gradient(sum, backend, [1.0]), + prepare_jacobian(identity, backend, [1.0]), + prepare_jacobian(copyto!, [0.0], backend, [1.0]), ] for prep in preps @test prep.relstep == 0.1 @@ -96,13 +96,13 @@ end; backend = AutoFiniteDiff(; relstep=0.1) preps = [ - prepare_pushforward(identity, backend, 1.0, (1.0,)) - prepare_pushforward(copyto!, [0.0], backend, [1.0], ([1.0],)) - prepare_derivative(identity, backend, 1.0) - prepare_derivative((y, x) -> y .= x, [0.0], backend, 1.0) - prepare_gradient(sum, backend, [1.0]) - prepare_jacobian(identity, backend, [1.0]) - prepare_jacobian(copyto!, [0.0], backend, [1.0]) + prepare_pushforward(identity, backend, 1.0, (1.0,)), + prepare_pushforward(copyto!, [0.0], backend, [1.0], ([1.0],)), + prepare_derivative(identity, backend, 1.0), + prepare_derivative((y, x) -> y .= x, [0.0], backend, 1.0), + prepare_gradient(sum, backend, [1.0]), + prepare_jacobian(identity, backend, [1.0]), + prepare_jacobian(copyto!, [0.0], backend, [1.0]), ] for prep in preps @test prep.relstep == 0.1