From 881fc3f120fcd2a2901f13d05c8359499db0a9b0 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Wed, 4 Dec 2024 14:11:26 +0100 Subject: [PATCH] test: make CI faster (#655) * Make CI faster * Smaller epsilon * Use central finite diff * Import * Fix batch size * Fix batch size --- .github/workflows/Test.yml | 10 +- DifferentiationInterface/Project.toml | 2 +- .../utils.jl | 4 +- .../utils.jl | 14 +-- ...DifferentiationInterfaceStaticArraysExt.jl | 16 +++ .../src/DifferentiationInterface.jl | 1 + .../src/misc/from_primitive.jl | 4 +- .../src/misc/simple_finite_diff.jl | 87 +++++++++++++++ .../src/utils/batchsize.jl | 16 ++- .../{Misc => Back}/DifferentiateWith/test.jl | 0 .../test/Back/ForwardDiff/test.jl | 16 ++- .../{Misc => Back}/SparsityDetector/test.jl | 0 .../{Misc => Core}/Internals/_formalities.jl | 0 .../test/{Misc => Core}/Internals/backends.jl | 0 .../test/{Misc => Core}/Internals/basis.jl | 0 .../test/Core/Internals/batchsize.jl | 101 ++++++++++++++++++ .../test/{Misc => Core}/Internals/context.jl | 0 .../test/{Misc => Core}/Internals/display.jl | 0 .../{Misc => Core}/Internals/exceptions.jl | 0 .../SimpleFiniteDiff}/test.jl | 29 ++--- .../test/{Misc => Core}/ZeroBackends/test.jl | 0 .../test/Misc/Internals/batchsize.jl | 83 -------------- .../src/scenarios/scenario.jl | 4 + .../src/scenarios/sparse.jl | 2 +- DifferentiationInterfaceTest/test/weird.jl | 5 +- .../test/zero_backends.jl | 11 +- 26 files changed, 272 insertions(+), 133 deletions(-) create mode 100644 DifferentiationInterface/src/misc/simple_finite_diff.jl rename DifferentiationInterface/test/{Misc => Back}/DifferentiateWith/test.jl (100%) rename DifferentiationInterface/test/{Misc => Back}/SparsityDetector/test.jl (100%) rename DifferentiationInterface/test/{Misc => Core}/Internals/_formalities.jl (100%) rename DifferentiationInterface/test/{Misc => Core}/Internals/backends.jl (100%) rename DifferentiationInterface/test/{Misc => Core}/Internals/basis.jl (100%) create mode 100644 DifferentiationInterface/test/Core/Internals/batchsize.jl rename DifferentiationInterface/test/{Misc => Core}/Internals/context.jl (100%) rename DifferentiationInterface/test/{Misc => Core}/Internals/display.jl (100%) rename DifferentiationInterface/test/{Misc => Core}/Internals/exceptions.jl (100%) rename DifferentiationInterface/test/{Misc/FromPrimitive => Core/SimpleFiniteDiff}/test.jl (65%) rename DifferentiationInterface/test/{Misc => Core}/ZeroBackends/test.jl (100%) delete mode 100644 DifferentiationInterface/test/Misc/Internals/batchsize.jl diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 3fbb6210a..f6b0965f3 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -31,11 +31,11 @@ jobs: - "1.10" - "1" group: - - Misc/Internals - - Misc/DifferentiateWith - - Misc/FromPrimitive - - Misc/SparsityDetector - - Misc/ZeroBackends + - Core/Internals + - Back/DifferentiateWith + - Core/SimpleFiniteDiff + - Back/SparsityDetector + - Core/ZeroBackends - Back/ChainRules # - Back/Diffractor - Back/Enzyme diff --git a/DifferentiationInterface/Project.toml b/DifferentiationInterface/Project.toml index e0cb9fec2..b4d478ab2 100644 --- a/DifferentiationInterface/Project.toml +++ b/DifferentiationInterface/Project.toml @@ -1,7 +1,7 @@ name = "DifferentiationInterface" uuid = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63" authors = ["Guillaume Dalle", "Adrian Hill"] -version = "0.6.24" +version = "0.6.25" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" diff --git a/DifferentiationInterface/ext/DifferentiationInterfaceEnzymeExt/utils.jl b/DifferentiationInterface/ext/DifferentiationInterfaceEnzymeExt/utils.jl index 085e77ea4..984c9f03f 100644 --- a/DifferentiationInterface/ext/DifferentiationInterfaceEnzymeExt/utils.jl +++ b/DifferentiationInterface/ext/DifferentiationInterfaceEnzymeExt/utils.jl @@ -1,9 +1,7 @@ # until https://github.com/EnzymeAD/Enzyme.jl/pull/1545 is merged function DI.BatchSizeSettings(::AutoEnzyme, N::Integer) B = DI.reasonable_batchsize(N, 16) - singlebatch = B == N - aligned = N % B == 0 - return DI.BatchSizeSettings{B,singlebatch,aligned}(N) + return DI.BatchSizeSettings{B}(N) end to_val(::DI.BatchSizeSettings{B}) where {B} = Val(B) diff --git a/DifferentiationInterface/ext/DifferentiationInterfaceForwardDiffExt/utils.jl b/DifferentiationInterface/ext/DifferentiationInterfaceForwardDiffExt/utils.jl index 5f03f134a..aa2546812 100644 --- a/DifferentiationInterface/ext/DifferentiationInterfaceForwardDiffExt/utils.jl +++ b/DifferentiationInterface/ext/DifferentiationInterfaceForwardDiffExt/utils.jl @@ -1,18 +1,10 @@ function DI.BatchSizeSettings(::AutoForwardDiff{nothing}, N::Integer) - B = ForwardDiff.pickchunksize(N) - singlebatch = B == N - aligned = N % B == 0 - return DI.BatchSizeSettings{B,singlebatch,aligned}(N) + chunksize = ForwardDiff.pickchunksize(N) + return DI.BatchSizeSettings{chunksize}(N) end function DI.BatchSizeSettings(::AutoForwardDiff{chunksize}, N::Integer) where {chunksize} - if chunksize > N - throw(ArgumentError("Fixed chunksize $chunksize larger than input size $N")) - end - B = chunksize - singlebatch = B == N - aligned = N % B == 0 - return DI.BatchSizeSettings{B,singlebatch,aligned}(N) + return DI.BatchSizeSettings{chunksize}(N) end function DI.threshold_batchsize( diff --git a/DifferentiationInterface/ext/DifferentiationInterfaceStaticArraysExt/DifferentiationInterfaceStaticArraysExt.jl b/DifferentiationInterface/ext/DifferentiationInterfaceStaticArraysExt/DifferentiationInterfaceStaticArraysExt.jl index 9fa3d9fd8..a33ec70be 100644 --- a/DifferentiationInterface/ext/DifferentiationInterfaceStaticArraysExt/DifferentiationInterfaceStaticArraysExt.jl +++ b/DifferentiationInterface/ext/DifferentiationInterfaceStaticArraysExt/DifferentiationInterfaceStaticArraysExt.jl @@ -14,6 +14,10 @@ end DI.ismutable_array(::Type{<:SArray}) = false +function DI.BatchSizeSettings(::DI.AutoSimpleFiniteDiff{nothing}, x::StaticArray) + return DI.BatchSizeSettings{length(x),true,true}(length(x)) +end + function DI.BatchSizeSettings(::AutoForwardDiff{nothing}, x::StaticArray) return DI.BatchSizeSettings{length(x),true,true}(length(x)) end @@ -22,4 +26,16 @@ function DI.BatchSizeSettings(::AutoEnzyme, x::StaticArray) return DI.BatchSizeSettings{length(x),true,true}(length(x)) end +function DI.BatchSizeSettings( + ::DI.AutoSimpleFiniteDiff{chunksize}, x::StaticArray +) where {chunksize} + return DI.BatchSizeSettings{chunksize}(Val(length(x))) +end + +function DI.BatchSizeSettings( + ::AutoForwardDiff{chunksize}, x::StaticArray +) where {chunksize} + return DI.BatchSizeSettings{chunksize}(Val(length(x))) +end + end diff --git a/DifferentiationInterface/src/DifferentiationInterface.jl b/DifferentiationInterface/src/DifferentiationInterface.jl index fcfc25dd0..164b9f628 100644 --- a/DifferentiationInterface/src/DifferentiationInterface.jl +++ b/DifferentiationInterface/src/DifferentiationInterface.jl @@ -62,6 +62,7 @@ include("fallbacks/change_prep.jl") include("misc/differentiate_with.jl") include("misc/from_primitive.jl") include("misc/sparsity_detector.jl") +include("misc/simple_finite_diff.jl") include("misc/zero_backends.jl") ## Exported diff --git a/DifferentiationInterface/src/misc/from_primitive.jl b/DifferentiationInterface/src/misc/from_primitive.jl index 8bac59ff7..c95564866 100644 --- a/DifferentiationInterface/src/misc/from_primitive.jl +++ b/DifferentiationInterface/src/misc/from_primitive.jl @@ -19,8 +19,9 @@ function BatchSizeSettings(fromprim::FromPrimitive, N::Integer) return BatchSizeSettings(fromprim.backend, N) end -## Forward +## Forward (no longer used) +#= struct AutoForwardFromPrimitive{B} <: FromPrimitive backend::B end @@ -104,6 +105,7 @@ function value_and_pushforward!( f!, y, ty, prep.pushforward_prep, fromprim.backend, x, tx, contexts... ) end +=# ## Reverse diff --git a/DifferentiationInterface/src/misc/simple_finite_diff.jl b/DifferentiationInterface/src/misc/simple_finite_diff.jl new file mode 100644 index 000000000..535c3baeb --- /dev/null +++ b/DifferentiationInterface/src/misc/simple_finite_diff.jl @@ -0,0 +1,87 @@ +""" + AutoSimpleFiniteDiff <: ADTypes.AbstractADType + +Forward mode backend based on the finite difference `(f(x + ε) - f(x)) / ε`, with artificial chunk size to mimick ForwardDiff. + +# Constructor + + AutoSimpleFiniteDiff(ε=1e-5; chunksize=nothing) +""" +struct AutoSimpleFiniteDiff{chunksize,T<:Real} <: AbstractADType + ε::T +end + +function AutoSimpleFiniteDiff(ε=1e-5; chunksize=nothing) + return AutoSimpleFiniteDiff{chunksize,typeof(ε)}(ε) +end + +ADTypes.mode(::AutoSimpleFiniteDiff) = ForwardMode() +check_available(::AutoSimpleFiniteDiff) = true +inplace_support(::AutoSimpleFiniteDiff) = InPlaceSupported() + +function BatchSizeSettings(::AutoSimpleFiniteDiff{nothing}, N::Integer) + B = reasonable_batchsize(N, 12) + return BatchSizeSettings{B}(N) +end + +function BatchSizeSettings(::AutoSimpleFiniteDiff{chunksize}, N::Integer) where {chunksize} + return BatchSizeSettings{chunksize}(N) +end + +function threshold_batchsize( + backend::AutoSimpleFiniteDiff{chunksize1}, chunksize2::Integer +) where {chunksize1} + chunksize = isnothing(chunksize1) ? nothing : min(chunksize1, chunksize2) + return AutoSimpleFiniteDiff(backend.ε; chunksize) +end + +function prepare_pushforward( + f::F, ::AutoSimpleFiniteDiff, x, tx::NTuple, contexts::Vararg{Context,C} +) where {F,C} + return NoPushforwardPrep() +end + +function prepare_pushforward( + f!::F, y, ::AutoSimpleFiniteDiff, x, tx::NTuple, contexts::Vararg{Context,C} +) where {F,C} + return NoPushforwardPrep() +end + +function value_and_pushforward( + f::F, + ::NoPushforwardPrep, + backend::AutoSimpleFiniteDiff, + x, + tx::NTuple{B}, + contexts::Vararg{Context,C}, +) where {F,B,C} + ε = eltype(x)(backend.ε) + y = f(x, map(unwrap, contexts)...) + ty = map(tx) do dx + y1 = f(x + ε * dx, map(unwrap, contexts)...) + y0 = f(x - ε * dx, map(unwrap, contexts)...) + (y1 - y0) / 2ε + end + return y, ty +end + +function value_and_pushforward( + f!::F, + y, + ::NoPushforwardPrep, + backend::AutoSimpleFiniteDiff, + x, + tx::NTuple{B}, + contexts::Vararg{Context,C}, +) where {F,B,C} + ε = eltype(x)(backend.ε) + ty = map(tx) do dx + f!(y, x + ε * dx, map(unwrap, contexts)...) + y1 = copy(y) + f!(y, x - ε * dx, map(unwrap, contexts)...) + y0 = copy(y) + (y1 - y0) / 2ε + end + f!(y, x, map(unwrap, contexts)...) + return y, ty +end diff --git a/DifferentiationInterface/src/utils/batchsize.jl b/DifferentiationInterface/src/utils/batchsize.jl index 91639bc2c..44577696a 100644 --- a/DifferentiationInterface/src/utils/batchsize.jl +++ b/DifferentiationInterface/src/utils/batchsize.jl @@ -6,7 +6,7 @@ Configuration for the batch size deduced from a backend and a sample array of le # Type parameters - `B::Int`: batch size -- `singlebatch::Bool`: whether `B > N` +- `singlebatch::Bool`: whether `B == N` (`B > N` is not allowed) - `aligned::Bool`: whether `N % B == 0` # Fields @@ -22,11 +22,25 @@ struct BatchSizeSettings{B,singlebatch,aligned} end function BatchSizeSettings{B,singlebatch,aligned}(N::Integer) where {B,singlebatch,aligned} + B > N && throw(ArgumentError("Batch size $B larger than input size $N")) A = div(N, B, RoundUp) B_last = N % B return BatchSizeSettings{B,singlebatch,aligned}(N, A, B_last) end +function BatchSizeSettings{B}(::Val{N}) where {B,N} + singlebatch = B == N + aligned = N % B == 0 + return BatchSizeSettings{B,singlebatch,aligned}(N) +end + +function BatchSizeSettings{B}(N::Integer) where {B} + # type-unstable + singlebatch = B == N + aligned = N % B == 0 + return BatchSizeSettings{B,singlebatch,aligned}(N) +end + function BatchSizeSettings(::AbstractADType, N::Integer) B = 1 singlebatch = false diff --git a/DifferentiationInterface/test/Misc/DifferentiateWith/test.jl b/DifferentiationInterface/test/Back/DifferentiateWith/test.jl similarity index 100% rename from DifferentiationInterface/test/Misc/DifferentiateWith/test.jl rename to DifferentiationInterface/test/Back/DifferentiateWith/test.jl diff --git a/DifferentiationInterface/test/Back/ForwardDiff/test.jl b/DifferentiationInterface/test/Back/ForwardDiff/test.jl index 00ed7c5eb..5c4758fdd 100644 --- a/DifferentiationInterface/test/Back/ForwardDiff/test.jl +++ b/DifferentiationInterface/test/Back/ForwardDiff/test.jl @@ -4,9 +4,10 @@ Pkg.add("ForwardDiff") using ADTypes: ADTypes using ComponentArrays: ComponentArrays using DifferentiationInterface, DifferentiationInterfaceTest +import DifferentiationInterface as DI import DifferentiationInterfaceTest as DIT using ForwardDiff: ForwardDiff -using StaticArrays: StaticArrays +using StaticArrays: StaticArrays, @SVector using Test using ExplicitImports @@ -75,7 +76,18 @@ test_differentiation( test_differentiation(AutoForwardDiff(), static_scenarios(); logging=LOGGING) -@testset verbose = true "No allocations on StaticArrays" begin +@testset verbose = true "StaticArrays" begin + @testset "Batch size" begin + @test DI.pick_batchsize(AutoForwardDiff(), rand(7)) isa DI.BatchSizeSettings{7} + @test DI.pick_batchsize(AutoForwardDiff(; chunksize=5), rand(7)) isa + DI.BatchSizeSettings{5} + @test (@inferred DI.pick_batchsize(AutoForwardDiff(), @SVector(rand(7)))) isa + DI.BatchSizeSettings{7} + @test (@inferred DI.pick_batchsize( + AutoForwardDiff(; chunksize=5), @SVector(rand(7)) + )) isa DI.BatchSizeSettings{5} + end + filtered_static_scenarios = filter(static_scenarios(; include_batchified=false)) do scen DIT.function_place(scen) == :out && DIT.operator_place(scen) == :out end diff --git a/DifferentiationInterface/test/Misc/SparsityDetector/test.jl b/DifferentiationInterface/test/Back/SparsityDetector/test.jl similarity index 100% rename from DifferentiationInterface/test/Misc/SparsityDetector/test.jl rename to DifferentiationInterface/test/Back/SparsityDetector/test.jl diff --git a/DifferentiationInterface/test/Misc/Internals/_formalities.jl b/DifferentiationInterface/test/Core/Internals/_formalities.jl similarity index 100% rename from DifferentiationInterface/test/Misc/Internals/_formalities.jl rename to DifferentiationInterface/test/Core/Internals/_formalities.jl diff --git a/DifferentiationInterface/test/Misc/Internals/backends.jl b/DifferentiationInterface/test/Core/Internals/backends.jl similarity index 100% rename from DifferentiationInterface/test/Misc/Internals/backends.jl rename to DifferentiationInterface/test/Core/Internals/backends.jl diff --git a/DifferentiationInterface/test/Misc/Internals/basis.jl b/DifferentiationInterface/test/Core/Internals/basis.jl similarity index 100% rename from DifferentiationInterface/test/Misc/Internals/basis.jl rename to DifferentiationInterface/test/Core/Internals/basis.jl diff --git a/DifferentiationInterface/test/Core/Internals/batchsize.jl b/DifferentiationInterface/test/Core/Internals/batchsize.jl new file mode 100644 index 000000000..48ff73337 --- /dev/null +++ b/DifferentiationInterface/test/Core/Internals/batchsize.jl @@ -0,0 +1,101 @@ +using ADTypes +using DifferentiationInterface +using DifferentiationInterface: + AutoSimpleFiniteDiff, + BatchSizeSettings, + pick_batchsize, + reasonable_batchsize, + threshold_batchsize +import DifferentiationInterface as DI +using StaticArrays +using Test + +BSS = BatchSizeSettings + +@testset "Default" begin + @test (@inferred pick_batchsize(AutoZygote(), zeros(2))) isa BSS{1,false,true} + @test (@inferred pick_batchsize(AutoZygote(), zeros(100))) isa BSS{1,false,true} + @test_throws ArgumentError pick_batchsize(AutoSparse(AutoZygote()), zeros(2)) + @test_throws ArgumentError pick_batchsize( + SecondOrder(AutoZygote(), AutoZygote()), zeros(2) + ) + @test_throws ArgumentError pick_batchsize( + MixedMode(AutoSimpleFiniteDiff(), AutoZygote()), zeros(2) + ) +end + +@testset "SimpleFiniteDiff (adaptive)" begin + @test (pick_batchsize(AutoSimpleFiniteDiff(), zeros(2))) isa BSS{2,true,true} + @test (pick_batchsize(AutoSimpleFiniteDiff(), zeros(6))) isa BSS{6,true,true} + @test (pick_batchsize(AutoSimpleFiniteDiff(), zeros(12))) isa BSS{12,true,true} + @test (pick_batchsize(AutoSimpleFiniteDiff(), zeros(24))) isa BSS{12,false,true} + @test (pick_batchsize(AutoSimpleFiniteDiff(), zeros(100))) isa BSS{12,false,false} + @test (@inferred pick_batchsize(AutoSimpleFiniteDiff(), @SVector(zeros(2)))) isa + BSS{2,true,true} + @test (@inferred pick_batchsize(AutoSimpleFiniteDiff(), @SVector(zeros(6)))) isa + BSS{6,true,true} + @test (@inferred pick_batchsize(AutoSimpleFiniteDiff(), @SVector(zeros(100)))) isa + BSS{100,true,true} +end + +@testset "SimpleFiniteDiff (fixed)" begin + @test_throws ArgumentError pick_batchsize(AutoSimpleFiniteDiff(; chunksize=4), zeros(2)) + @test_throws ArgumentError pick_batchsize( + AutoSimpleFiniteDiff(; chunksize=4), @SVector(zeros(2)) + ) + @test pick_batchsize(AutoSimpleFiniteDiff(; chunksize=4), zeros(6)) isa BSS{4} + @test pick_batchsize(AutoSimpleFiniteDiff(; chunksize=4), zeros(100)) isa BSS{4} + BSS{4,true,true} + @test pick_batchsize(AutoSimpleFiniteDiff(; chunksize=4), zeros(99)) isa BSS{4} + BSS{4,true,false} + @test (@inferred pick_batchsize( + AutoSimpleFiniteDiff(; chunksize=4), @SVector(zeros(6)) + )) isa BSS{4,false,false} + @test (@inferred pick_batchsize( + AutoSimpleFiniteDiff(; chunksize=4), @SVector(zeros(100)) + )) isa BSS{4,false,true} +end + +@testset "Thresholding" begin + @test threshold_batchsize(AutoSimpleFiniteDiff(), 2) isa AutoSimpleFiniteDiff{nothing} + @test threshold_batchsize(AutoSimpleFiniteDiff(; chunksize=4), 2) isa + AutoSimpleFiniteDiff{2} + @test threshold_batchsize(AutoSimpleFiniteDiff(; chunksize=4), 6) isa + AutoSimpleFiniteDiff{4} + @test threshold_batchsize(AutoSparse(AutoSimpleFiniteDiff(; chunksize=4)), 2) isa + AutoSparse{<:AutoSimpleFiniteDiff{2}} + @test threshold_batchsize( + SecondOrder( + AutoSimpleFiniteDiff(; chunksize=4), AutoSimpleFiniteDiff(; chunksize=3) + ), + 6, + ) isa SecondOrder{<:AutoSimpleFiniteDiff{4},<:AutoSimpleFiniteDiff{3}} + @test threshold_batchsize( + SecondOrder( + AutoSimpleFiniteDiff(; chunksize=4), AutoSimpleFiniteDiff(; chunksize=3) + ), + 2, + ) isa SecondOrder{<:AutoSimpleFiniteDiff{2},<:AutoSimpleFiniteDiff{2}} + @test threshold_batchsize( + SecondOrder( + AutoSimpleFiniteDiff(; chunksize=1), AutoSimpleFiniteDiff(; chunksize=3) + ), + 2, + ) isa SecondOrder{<:AutoSimpleFiniteDiff{1},<:AutoSimpleFiniteDiff{2}} + @test threshold_batchsize( + SecondOrder( + AutoSimpleFiniteDiff(; chunksize=4), AutoSimpleFiniteDiff(; chunksize=1) + ), + 2, + ) isa SecondOrder{<:AutoSimpleFiniteDiff{2},<:AutoSimpleFiniteDiff{1}} +end + +@testset "Reasonable" begin + for Bmax in 1:5 + @test all(<=(Bmax), reasonable_batchsize.(1:10, Bmax)) + @test issorted(div.(1:10, reasonable_batchsize.(1:10, Bmax), RoundUp)) + if Bmax > 2 + @test reasonable_batchsize(Bmax + 1, Bmax) < Bmax + end + end +end diff --git a/DifferentiationInterface/test/Misc/Internals/context.jl b/DifferentiationInterface/test/Core/Internals/context.jl similarity index 100% rename from DifferentiationInterface/test/Misc/Internals/context.jl rename to DifferentiationInterface/test/Core/Internals/context.jl diff --git a/DifferentiationInterface/test/Misc/Internals/display.jl b/DifferentiationInterface/test/Core/Internals/display.jl similarity index 100% rename from DifferentiationInterface/test/Misc/Internals/display.jl rename to DifferentiationInterface/test/Core/Internals/display.jl diff --git a/DifferentiationInterface/test/Misc/Internals/exceptions.jl b/DifferentiationInterface/test/Core/Internals/exceptions.jl similarity index 100% rename from DifferentiationInterface/test/Misc/Internals/exceptions.jl rename to DifferentiationInterface/test/Core/Internals/exceptions.jl diff --git a/DifferentiationInterface/test/Misc/FromPrimitive/test.jl b/DifferentiationInterface/test/Core/SimpleFiniteDiff/test.jl similarity index 65% rename from DifferentiationInterface/test/Misc/FromPrimitive/test.jl rename to DifferentiationInterface/test/Core/SimpleFiniteDiff/test.jl index 26907aa9c..3999f2656 100644 --- a/DifferentiationInterface/test/Misc/FromPrimitive/test.jl +++ b/DifferentiationInterface/test/Core/SimpleFiniteDiff/test.jl @@ -1,38 +1,31 @@ using DifferentiationInterface, DifferentiationInterfaceTest -using DifferentiationInterface: AutoForwardFromPrimitive, AutoReverseFromPrimitive +using DifferentiationInterface: AutoSimpleFiniteDiff, AutoReverseFromPrimitive using DifferentiationInterfaceTest -using ForwardDiff: ForwardDiff using Test LOGGING = get(ENV, "CI", "false") == "false" backends = [ # - AutoForwardFromPrimitive(AutoForwardDiff(; chunksize=5)), - AutoReverseFromPrimitive(AutoForwardDiff(; chunksize=4)), + AutoSimpleFiniteDiff(; chunksize=5), + AutoReverseFromPrimitive(AutoSimpleFiniteDiff(; chunksize=4)), ] second_order_backends = [ # SecondOrder( - AutoForwardFromPrimitive(AutoForwardDiff(; chunksize=5)), - AutoReverseFromPrimitive(AutoForwardDiff(; chunksize=4)), + AutoSimpleFiniteDiff(; chunksize=5), + AutoReverseFromPrimitive(AutoSimpleFiniteDiff(; chunksize=4)), ), SecondOrder( - AutoReverseFromPrimitive(AutoForwardDiff(; chunksize=5)), - AutoForwardFromPrimitive(AutoForwardDiff(; chunksize=4)), + AutoReverseFromPrimitive(AutoSimpleFiniteDiff(; chunksize=5)), + AutoSimpleFiniteDiff(; chunksize=4), ), ] adaptive_backends = [ # - AutoForwardFromPrimitive(AutoForwardDiff()), - AutoReverseFromPrimitive(AutoForwardDiff()), - SecondOrder( - AutoForwardFromPrimitive(AutoForwardDiff()), - AutoReverseFromPrimitive(AutoForwardDiff()), - ), - SecondOrder( - AutoReverseFromPrimitive(AutoForwardDiff()), - AutoForwardFromPrimitive(AutoForwardDiff()), - ), + AutoSimpleFiniteDiff(), + AutoReverseFromPrimitive(AutoSimpleFiniteDiff()), + SecondOrder(AutoSimpleFiniteDiff(), AutoReverseFromPrimitive(AutoSimpleFiniteDiff())), + SecondOrder(AutoReverseFromPrimitive(AutoSimpleFiniteDiff()), AutoSimpleFiniteDiff()), ] for backend in vcat(backends, second_order_backends) diff --git a/DifferentiationInterface/test/Misc/ZeroBackends/test.jl b/DifferentiationInterface/test/Core/ZeroBackends/test.jl similarity index 100% rename from DifferentiationInterface/test/Misc/ZeroBackends/test.jl rename to DifferentiationInterface/test/Core/ZeroBackends/test.jl diff --git a/DifferentiationInterface/test/Misc/Internals/batchsize.jl b/DifferentiationInterface/test/Misc/Internals/batchsize.jl deleted file mode 100644 index 16bd0d221..000000000 --- a/DifferentiationInterface/test/Misc/Internals/batchsize.jl +++ /dev/null @@ -1,83 +0,0 @@ -using ADTypes -using DifferentiationInterface -using DifferentiationInterface: - BatchSizeSettings, pick_batchsize, reasonable_batchsize, threshold_batchsize -import DifferentiationInterface as DI -using ForwardDiff: ForwardDiff -using StaticArrays -using Test - -BSS = BatchSizeSettings - -@testset "Default" begin - @test (@inferred pick_batchsize(AutoZygote(), zeros(2))) isa BSS{1,false,true} - @test (@inferred pick_batchsize(AutoZygote(), zeros(100))) isa BSS{1,false,true} - @test_throws ArgumentError pick_batchsize(AutoSparse(AutoZygote()), zeros(2)) - @test_throws ArgumentError pick_batchsize( - SecondOrder(AutoZygote(), AutoZygote()), zeros(2) - ) - @test_throws ArgumentError pick_batchsize( - MixedMode(AutoForwardDiff(), AutoZygote()), zeros(2) - ) -end - -@testset "ForwardDiff (adaptive)" begin - @test (pick_batchsize(AutoForwardDiff(), zeros(2))) isa BSS{2,true,true} - @test (pick_batchsize(AutoForwardDiff(), zeros(6))) isa BSS{6,true,true} - @test (pick_batchsize(AutoForwardDiff(), zeros(12))) isa BSS{12,true,true} - @test (pick_batchsize(AutoForwardDiff(), zeros(24))) isa BSS{12,false,true} - @test (pick_batchsize(AutoForwardDiff(), zeros(100))) isa BSS{12,false,false} - @test (@inferred pick_batchsize(AutoForwardDiff(), @SVector(zeros(2)))) isa - BSS{2,true,true} - @test (@inferred pick_batchsize(AutoForwardDiff(), @SVector(zeros(6)))) isa - BSS{6,true,true} - @test (@inferred pick_batchsize(AutoForwardDiff(), @SVector(zeros(100)))) isa - BSS{100,true,true} -end - -@testset "ForwardDiff (fixed)" begin - @test_throws ArgumentError pick_batchsize(AutoForwardDiff(; chunksize=4), zeros(2)) - @test_throws ArgumentError pick_batchsize( - AutoForwardDiff(; chunksize=4), @SVector(zeros(2)) - ) - @test pick_batchsize(AutoForwardDiff(; chunksize=4), zeros(6)) isa BSS{4} - @test pick_batchsize(AutoForwardDiff(; chunksize=4), zeros(100)) isa BSS{4} - BSS{4,true,true} - @test pick_batchsize(AutoForwardDiff(; chunksize=4), zeros(99)) isa BSS{4} - BSS{4,true,false} - @test (@inferred pick_batchsize(AutoForwardDiff(; chunksize=4), @SVector(zeros(6)))) isa - BSS{4,false,false} - @test (@inferred pick_batchsize( - AutoForwardDiff(; chunksize=4), @SVector(zeros(100)) - )) isa BSS{4,false,true} -end - -@testset "Thresholding" begin - @test threshold_batchsize(AutoForwardDiff(), 2) isa AutoForwardDiff{nothing} - @test threshold_batchsize(AutoForwardDiff(; chunksize=4), 2) isa AutoForwardDiff{2} - @test threshold_batchsize(AutoForwardDiff(; chunksize=4), 6) isa AutoForwardDiff{4} - @test threshold_batchsize(AutoSparse(AutoForwardDiff(; chunksize=4)), 2) isa - AutoSparse{<:AutoForwardDiff{2}} - @test threshold_batchsize( - SecondOrder(AutoForwardDiff(; chunksize=4), AutoForwardDiff(; chunksize=3)), 6 - ) isa SecondOrder{<:AutoForwardDiff{4},<:AutoForwardDiff{3}} - @test threshold_batchsize( - SecondOrder(AutoForwardDiff(; chunksize=4), AutoForwardDiff(; chunksize=3)), 2 - ) isa SecondOrder{<:AutoForwardDiff{2},<:AutoForwardDiff{2}} - @test threshold_batchsize( - SecondOrder(AutoForwardDiff(; chunksize=1), AutoForwardDiff(; chunksize=3)), 2 - ) isa SecondOrder{<:AutoForwardDiff{1},<:AutoForwardDiff{2}} - @test threshold_batchsize( - SecondOrder(AutoForwardDiff(; chunksize=4), AutoForwardDiff(; chunksize=1)), 2 - ) isa SecondOrder{<:AutoForwardDiff{2},<:AutoForwardDiff{1}} -end - -@testset "Reasonable" begin - for Bmax in 1:5 - @test all(<=(Bmax), reasonable_batchsize.(1:10, Bmax)) - @test issorted(div.(1:10, reasonable_batchsize.(1:10, Bmax), RoundUp)) - if Bmax > 2 - @test reasonable_batchsize(Bmax + 1, Bmax) < Bmax - end - end -end diff --git a/DifferentiationInterfaceTest/src/scenarios/scenario.jl b/DifferentiationInterfaceTest/src/scenarios/scenario.jl index b360a0099..2215f6357 100644 --- a/DifferentiationInterfaceTest/src/scenarios/scenario.jl +++ b/DifferentiationInterfaceTest/src/scenarios/scenario.jl @@ -147,3 +147,7 @@ function adapt_batchsize(backend::AbstractADType, scen::Scenario) return backend end end + +function no_matrices(scens::AbstractVector{<:Scenario}) + return filter(s -> !isa(s.x, AbstractMatrix) && !isa(s.y, AbstractMatrix), scens) +end diff --git a/DifferentiationInterfaceTest/src/scenarios/sparse.jl b/DifferentiationInterfaceTest/src/scenarios/sparse.jl index 843f5eeab..125ca0049 100644 --- a/DifferentiationInterfaceTest/src/scenarios/sparse.jl +++ b/DifferentiationInterfaceTest/src/scenarios/sparse.jl @@ -327,7 +327,7 @@ Create a vector of [`Scenario`](@ref)s with sparse array types, focused on spars function sparse_scenarios(; band_sizes=[5, 10, 20], include_constantified=false) x_6 = float.(1:6) x_2_3 = float.(reshape(1:6, 2, 3)) - x_50 = float.(1:50) + x_50 = float.(range(1, 2, 50)) scens = vcat( sparse_vec_to_vec_scenarios(x_6), diff --git a/DifferentiationInterfaceTest/test/weird.jl b/DifferentiationInterfaceTest/test/weird.jl index 10afcb8a5..b2b27a8df 100644 --- a/DifferentiationInterfaceTest/test/weird.jl +++ b/DifferentiationInterfaceTest/test/weird.jl @@ -39,7 +39,10 @@ static_scenarios(; ## Weird arrays test_differentiation( - AutoForwardDiff(), static_scenarios(); benchmark=:full, logging=LOGGING + AutoForwardDiff(), + DIT.no_matrices(static_scenarios()); + benchmark=:prepared, + logging=LOGGING, ) test_differentiation(AutoForwardDiff(), component_scenarios(); logging=LOGGING) diff --git a/DifferentiationInterfaceTest/test/zero_backends.jl b/DifferentiationInterfaceTest/test/zero_backends.jl index aca429dfc..e8f5ace6c 100644 --- a/DifferentiationInterfaceTest/test/zero_backends.jl +++ b/DifferentiationInterfaceTest/test/zero_backends.jl @@ -2,7 +2,7 @@ using ADTypes using DifferentiationInterface using DifferentiationInterface: AutoZeroForward, AutoZeroReverse using DifferentiationInterfaceTest -using DifferentiationInterfaceTest: allocfree_scenarios +using DifferentiationInterfaceTest: allocfree_scenarios, no_matrices using Test LOGGING = get(ENV, "CI", "false") == "false" @@ -29,17 +29,16 @@ test_differentiation( data0 = benchmark_differentiation( AutoZeroForward(), - default_scenarios(; include_batchified=false, include_constantified=true); + no_matrices(default_scenarios(; include_batchified=false, include_constantified=true)); logging=LOGGING, - benchmark_seconds=0.1, ); data1 = benchmark_differentiation( AutoZeroForward(), - default_scenarios(; include_batchified=false); + no_matrices(default_scenarios(; include_batchified=false)); benchmark=:full, logging=LOGGING, - benchmark_seconds=0.2, + benchmark_seconds=0.05, benchmark_aggregation=maximum, ); @@ -48,7 +47,7 @@ ADTypes.mode(::FakeBackend) = ADTypes.ForwardMode() data2 = benchmark_differentiation( FakeBackend(), - default_scenarios(; include_batchified=false); + no_matrices(default_scenarios(; include_batchified=false)); logging=false, benchmark_test=false, );