diff --git a/DifferentiationInterface/Project.toml b/DifferentiationInterface/Project.toml index 4a4026652..e5a8d7cf6 100644 --- a/DifferentiationInterface/Project.toml +++ b/DifferentiationInterface/Project.toml @@ -103,4 +103,21 @@ Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c" Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" [targets] -test = ["ADTypes", "Aqua", "ComponentArrays", "DataFrames", "ExplicitImports", "ForwardDiff", "JET", "JLArrays", "JuliaFormatter", "Pkg", "Random", "SparseArrays", "SparseConnectivityTracer", "SparseMatrixColorings", "StableRNGs", "StaticArrays", "Test", "Zygote"] +test = [ + "ADTypes", + "Aqua", + "ComponentArrays", + "DataFrames", + "ExplicitImports", + "JET", + "JLArrays", + "JuliaFormatter", + "Pkg", + "Random", + "SparseArrays", + "SparseConnectivityTracer", + "SparseMatrixColorings", + "StableRNGs", + "StaticArrays", + "Test", +] diff --git a/DifferentiationInterface/test/Core/Internals/backends.jl b/DifferentiationInterface/test/Core/Internals/backends.jl index 7e334c15b..5ce73706f 100644 --- a/DifferentiationInterface/test/Core/Internals/backends.jl +++ b/DifferentiationInterface/test/Core/Internals/backends.jl @@ -2,6 +2,8 @@ using ADTypes using ADTypes: mode using DifferentiationInterface using DifferentiationInterface: + AutoSimpleFiniteDiff, + AutoReverseFromPrimitive, inner, outer, forward_backend, @@ -11,39 +13,38 @@ using DifferentiationInterface: pullback_performance, hvp_mode import DifferentiationInterface as DI -using ForwardDiff: ForwardDiff -using Zygote: Zygote using Test +fb = AutoSimpleFiniteDiff() +rb = AutoReverseFromPrimitive(AutoSimpleFiniteDiff()) + @testset "SecondOrder" begin - backend = SecondOrder(AutoForwardDiff(), AutoZygote()) - @test ADTypes.mode(backend) isa ADTypes.ForwardMode - @test outer(backend) isa AutoForwardDiff - @test inner(backend) isa AutoZygote + backend = SecondOrder(fb, rb) + @test check_available(backend) + @test outer(backend) isa AutoSimpleFiniteDiff + @test inner(backend) isa AutoReverseFromPrimitive @test mode(backend) isa ADTypes.ForwardMode - @test !Bool(inplace_support(backend)) + @test Bool(inplace_support(backend)) @test_throws ArgumentError pushforward_performance(backend) @test_throws ArgumentError pullback_performance(backend) - @test check_available(backend) end @testset "MixedMode" begin - backend = MixedMode(AutoForwardDiff(), AutoZygote()) - @test ADTypes.mode(backend) isa DifferentiationInterface.ForwardAndReverseMode - @test forward_backend(backend) isa AutoForwardDiff - @test reverse_backend(backend) isa AutoZygote - @test !Bool(inplace_support(backend)) + backend = MixedMode(fb, rb) + @test check_available(backend) + @test mode(backend) isa DifferentiationInterface.ForwardAndReverseMode + @test forward_backend(backend) isa AutoSimpleFiniteDiff + @test reverse_backend(backend) isa AutoReverseFromPrimitive + @test Bool(inplace_support(backend)) @test_throws MethodError pushforward_performance(backend) @test_throws MethodError pullback_performance(backend) - @test check_available(backend) end @testset "Sparse" begin - for dense_backend in [AutoForwardDiff(), AutoZygote()] + for dense_backend in [fb, rb] backend = AutoSparse(dense_backend) - @test ADTypes.mode(backend) == ADTypes.mode(dense_backend) - @test check_available(backend) == check_available(dense_backend) - @test inplace_support(backend) == inplace_support(dense_backend) + @test mode(backend) == ADTypes.mode(dense_backend) + @test Bool(inplace_support(backend)) @test_throws ArgumentError pushforward_performance(backend) @test_throws ArgumentError pullback_performance(backend) @test_throws ArgumentError hvp_mode(backend) diff --git a/DifferentiationInterface/test/Core/Internals/display.jl b/DifferentiationInterface/test/Core/Internals/display.jl index 419499972..5ad013925 100644 --- a/DifferentiationInterface/test/Core/Internals/display.jl +++ b/DifferentiationInterface/test/Core/Internals/display.jl @@ -3,16 +3,14 @@ using DifferentiationInterface using Test backend = SecondOrder(AutoForwardDiff(), AutoZygote()) -@test startswith(string(backend), "SecondOrder(") -@test endswith(string(backend), ")") +@test string(backend) == "SecondOrder(AutoForwardDiff(), AutoZygote())" detector = DenseSparsityDetector(AutoForwardDiff(); atol=1e-23) -@test startswith(string(detector), "DenseSparsityDetector(") -@test endswith(string(detector), ")") +@test string(detector) == + "DenseSparsityDetector(AutoForwardDiff(); atol=1.0e-23, method=:iterative)" diffwith = DifferentiateWith(exp, AutoForwardDiff()) -@test startswith(string(diffwith), "DifferentiateWith(") -@test endswith(string(diffwith), ")") +@test string(diffwith) == "DifferentiateWith(exp, AutoForwardDiff())" @test DifferentiationInterface.package_name(AutoForwardDiff()) == "ForwardDiff" @test DifferentiationInterface.package_name(AutoZygote()) == "Zygote" diff --git a/DifferentiationInterface/test/Core/SimpleFiniteDiff/test.jl b/DifferentiationInterface/test/Core/SimpleFiniteDiff/test.jl index 3999f2656..cbf42d9f3 100644 --- a/DifferentiationInterface/test/Core/SimpleFiniteDiff/test.jl +++ b/DifferentiationInterface/test/Core/SimpleFiniteDiff/test.jl @@ -35,44 +35,44 @@ end ## Dense scenarios -test_differentiation( - vcat(backends, second_order_backends), - default_scenarios(; include_constantified=true); - logging=LOGGING, -); - -## Sparse scenarios - -test_differentiation( - MyAutoSparse.(adaptive_backends), - default_scenarios(; include_constantified=true); - logging=LOGGING, -); - -test_differentiation( - MyAutoSparse.( - vcat(adaptive_backends, MixedMode(adaptive_backends[1], adaptive_backends[2])) - ), - sparse_scenarios(; include_constantified=true); - sparsity=true, - logging=LOGGING, -); +@testset "Dense" begin + test_differentiation( + vcat(backends, second_order_backends), + default_scenarios(; include_constantified=true); + logging=LOGGING, + ) +end -## Misc +@testset "Sparse" begin + test_differentiation( + MyAutoSparse.(adaptive_backends), + default_scenarios(; include_constantified=true); + logging=LOGGING, + ) -@testset "SparseMatrixColorings access" begin - jac_for_prep = prepare_jacobian(copy, MyAutoSparse(adaptive_backends[1]), rand(10)) - jac_rev_prep = prepare_jacobian(copy, MyAutoSparse(adaptive_backends[2]), rand(10)) - hess_prep = prepare_hessian( - x -> sum(abs2, x), MyAutoSparse(adaptive_backends[1]), rand(10) + test_differentiation( + MyAutoSparse.( + vcat(adaptive_backends, MixedMode(adaptive_backends[1], adaptive_backends[2])) + ), + sparse_scenarios(; include_constantified=true); + sparsity=true, + logging=LOGGING, ) - @test all(==(1), column_colors(jac_for_prep)) - @test all(==(1), row_colors(jac_rev_prep)) - @test all(==(1), column_colors(hess_prep)) - @test ncolors(jac_for_prep) == 1 - @test ncolors(hess_prep) == 1 - @test only(column_groups(jac_for_prep)) == 1:10 - @test only(row_groups(jac_rev_prep)) == 1:10 - @test only(column_groups(hess_prep)) == 1:10 + @testset "SparseMatrixColorings access" begin + jac_for_prep = prepare_jacobian(copy, MyAutoSparse(adaptive_backends[1]), rand(10)) + jac_rev_prep = prepare_jacobian(copy, MyAutoSparse(adaptive_backends[2]), rand(10)) + hess_prep = prepare_hessian( + x -> sum(abs2, x), MyAutoSparse(adaptive_backends[1]), rand(10) + ) + + @test all(==(1), column_colors(jac_for_prep)) + @test all(==(1), row_colors(jac_rev_prep)) + @test all(==(1), column_colors(hess_prep)) + @test ncolors(jac_for_prep) == 1 + @test ncolors(hess_prep) == 1 + @test only(column_groups(jac_for_prep)) == 1:10 + @test only(row_groups(jac_rev_prep)) == 1:10 + @test only(column_groups(hess_prep)) == 1:10 + end end diff --git a/DifferentiationInterface/test/Core/ZeroBackends/test.jl b/DifferentiationInterface/test/Core/ZeroBackends/test.jl index b352fee87..0571a73b3 100644 --- a/DifferentiationInterface/test/Core/ZeroBackends/test.jl +++ b/DifferentiationInterface/test/Core/ZeroBackends/test.jl @@ -16,56 +16,51 @@ for backend in zero_backends @test check_inplace(backend) end -## Type stability +@testset "Type stability" begin + test_differentiation( + AutoZeroForward(), + default_scenarios(; include_batchified=false, include_constantified=true); + correctness=false, + type_stability=:full, + logging=LOGGING, + ) -test_differentiation( - AutoZeroForward(), - default_scenarios(; include_batchified=false, include_constantified=true); - correctness=false, - type_stability=:full, - logging=LOGGING, -) + test_differentiation( + AutoZeroReverse(), + default_scenarios(; include_batchified=false, include_constantified=true); + correctness=false, + type_stability=:full, + logging=LOGGING, + ) -test_differentiation( - AutoZeroReverse(), - default_scenarios(; include_batchified=false, include_constantified=true); - correctness=false, - type_stability=:full, - logging=LOGGING, -) + test_differentiation( + [ + SecondOrder(AutoZeroForward(), AutoZeroReverse()), + SecondOrder(AutoZeroReverse(), AutoZeroForward()), + ], + default_scenarios(; include_batchified=false, include_constantified=true); + correctness=false, + type_stability=:full, + logging=LOGGING, + ) -test_differentiation( - [ - SecondOrder(AutoZeroForward(), AutoZeroReverse()), - SecondOrder(AutoZeroReverse(), AutoZeroForward()), - ], - default_scenarios(; include_batchified=false, include_constantified=true); - correctness=false, - type_stability=:full, - logging=LOGGING, -) - -test_differentiation( - AutoSparse.(zero_backends, coloring_algorithm=GreedyColoringAlgorithm()), - default_scenarios(; include_constantified=true); - correctness=false, - type_stability=:full, - excluded=[:pushforward, :pullback, :gradient, :derivative, :hvp, :second_derivative], - logging=LOGGING, -) - -## Weird arrays - -test_differentiation( - [AutoZeroForward(), AutoZeroReverse()], - zero.(vcat(component_scenarios(), static_scenarios())); - correctness=true, - logging=LOGGING, -) + test_differentiation( + AutoSparse.(zero_backends, coloring_algorithm=GreedyColoringAlgorithm()), + default_scenarios(; include_constantified=true); + correctness=false, + type_stability=:full, + excluded=[ + :pushforward, :pullback, :gradient, :derivative, :hvp, :second_derivative + ], + logging=LOGGING, + ) +end -test_differentiation( - [AutoZeroForward(), AutoZeroReverse()], - zero.(gpu_scenarios()); - correctness=true, - logging=LOGGING, -) +@testset "Weird arrays" begin + test_differentiation( + [AutoZeroForward(), AutoZeroReverse()], + zero.(vcat(component_scenarios(), static_scenarios(), gpu_scenarios())); + correctness=true, + logging=LOGGING, + ) +end diff --git a/DifferentiationInterface/test/runtests.jl b/DifferentiationInterface/test/runtests.jl index a1758105b..0ea5f4bdc 100644 --- a/DifferentiationInterface/test/runtests.jl +++ b/DifferentiationInterface/test/runtests.jl @@ -1,15 +1,6 @@ using DifferentiationInterface using Pkg using Test -using SparseMatrixColorings, SparseConnectivityTracer - -function MyAutoSparse(backend) - return AutoSparse( - backend; - sparsity_detector=TracerSparsityDetector(), - coloring_algorithm=GreedyColoringAlgorithm(), - ) -end DIT_PATH = joinpath(@__DIR__, "..", "..", "DifferentiationInterfaceTest") if isdir(DIT_PATH) @@ -18,36 +9,36 @@ else Pkg.add("DifferentiationInterfaceTest") end -GROUP = get(ENV, "JULIA_DI_TEST_GROUP", "All") +include("testutils.jl") ## Main tests @testset verbose = true "DifferentiationInterface.jl" begin - if GROUP == "All" - @testset verbose = true for category in readdir(@__DIR__) - isdir(joinpath(@__DIR__, category)) || continue - @testset verbose = true for folder in readdir(joinpath(@__DIR__, category)) - isdir(joinpath(@__DIR__, category, folder)) || continue + if haskey(ENV, "JULIA_DI_TEST_GROUP") + category, folder = split(ENV["JULIA_DI_TEST_GROUP"], '/') + @testset verbose = true "$category" begin + @testset verbose = true "$folder" begin @testset verbose = true "$file" for file in readdir( joinpath(@__DIR__, category, folder) ) endswith(file, ".jl") || continue @info "Testing $category/$folder/$file" include(joinpath(@__DIR__, category, folder, file)) + yield() end end end else - category, folder = split(GROUP, '/') - @testset verbose = true "$category" begin - @testset verbose = true "$folder" begin - @testset verbose = true "$file" for file in readdir( - joinpath(@__DIR__, category, folder) - ) - endswith(file, ".jl") || continue - @info "Testing $category/$folder/$file" - include(joinpath(@__DIR__, category, folder, file)) - end + category = "Core" + @testset verbose = true for folder in readdir(joinpath(@__DIR__, category)) + isdir(joinpath(@__DIR__, category, folder)) || continue + @testset verbose = true "$file" for file in readdir( + joinpath(@__DIR__, category, folder) + ) + endswith(file, ".jl") || continue + @info "Testing $category/$folder/$file" + include(joinpath(@__DIR__, category, folder, file)) + yield() end end end diff --git a/DifferentiationInterface/test/testutils.jl b/DifferentiationInterface/test/testutils.jl new file mode 100644 index 000000000..45b7758a4 --- /dev/null +++ b/DifferentiationInterface/test/testutils.jl @@ -0,0 +1,11 @@ +using ADTypes +using SparseConnectivityTracer +using SparseMatrixColorings + +function MyAutoSparse(backend::AbstractADType) + return AutoSparse( + backend; + sparsity_detector=TracerSparsityDetector(), + coloring_algorithm=GreedyColoringAlgorithm(), + ) +end