From 2cabb2315b0a641d8ae78166593193e65fe316b6 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 26 May 2026 10:34:06 +0200 Subject: [PATCH 01/13] chore: debug Enzyme tests --- .github/workflows/Test.yml | 131 +----------------- .../test/Back/Enzyme/test.jl | 2 + 2 files changed, 3 insertions(+), 130 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 3a82f29a4..4de005be2 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -16,63 +16,6 @@ concurrency: cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }} jobs: - test-DI-Core: - name: ${{ matrix.version }} - DI Core (${{ matrix.group }}) - runs-on: ubuntu-latest - if: ${{ !contains(github.event.pull_request.labels.*.name, 'skipci') }} - timeout-minutes: 180 - permissions: - # needed to allow julia-actions/cache to proactively delete old caches that it has created - actions: write - contents: read - strategy: - fail-fast: false # TODO: toggle - matrix: - version: - - '1.10' - - '1.11' - - '1.12' - group: - - Internals - - SimpleFiniteDiff - - ZeroBackends - skip_past: - - ${{ github.event.pull_request.draft }} - exclude: - - skip_past: true - version: '1.10' - - skip_past: true - version: '1.11' - env: - JULIA_DI_TEST_TYPE: 'Core' - JULIA_DI_TEST_GROUP: ${{ matrix.group }} - JULIA_DI_PR_DRAFT: ${{ github.event.pull_request.draft }} - steps: - - uses: actions/checkout@v6 - - uses: julia-actions/setup-julia@v3 - with: - version: ${{ matrix.version }} - arch: x64 - - uses: julia-actions/cache@v3 - - name: Install dependencies & run tests - run: julia --color=yes -e ' using Pkg; - Pkg.activate("./DifferentiationInterface/test"); if VERSION < v"1.11"; - Pkg.rm("DifferentiationInterfaceTest"); Pkg.resolve(); else; - Pkg.develop(; path="./DifferentiationInterfaceTest"); end; - Pkg.activate("./DifferentiationInterface"); test_kwargs = (; - allow_reresolve=false, coverage=true); if ENV["JULIA_DI_PR_DRAFT"] == - "true"; Pkg.test("DifferentiationInterface"; julia_args=["-O1"], - test_kwargs...); else; Pkg.test("DifferentiationInterface"; - test_kwargs...); end;' - - uses: julia-actions/julia-processcoverage@v1 - with: - directories: ./DifferentiationInterface/src,./DifferentiationInterface/ext,./DifferentiationInterface/test - - uses: codecov/codecov-action@v6 - with: - files: lcov.info - flags: DI - token: ${{ secrets.CODECOV_TOKEN }} - fail_ci_if_error: false test-DI-Backend: name: ${{ matrix.version }} - DI Back (${{ matrix.group }}) @@ -91,24 +34,7 @@ jobs: - '1.11' - '1.12' group: - - ChainRules - - DifferentiateWith - # - Diffractor - Enzyme - - FastDifferentiation - - FiniteDiff - - FiniteDifferences - - ForwardDiff - - GTPSA - - HyperHessians - - Mooncake - - Mooncake-old - - PolyesterForwardDiff - - ReverseDiff - - SparsityDetector - - Symbolics - - Tracker - - Zygote skip_past: - ${{ github.event.pull_request.draft }} exclude: @@ -155,59 +81,4 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} fail_ci_if_error: false - test-DIT: - name: ${{ matrix.version }} - DIT (${{ matrix.group }}) - runs-on: ubuntu-latest - if: ${{ !contains(github.event.pull_request.labels.*.name, 'skipci') }} - timeout-minutes: 60 - permissions: - # needed to allow julia-actions/cache to proactively delete old caches that it has created - actions: write - contents: read - strategy: - fail-fast: false # TODO: toggle - matrix: - version: - - '1.10' - - '1.11' - - '1.12' - group: - - Formalities - - Zero - - Standard - - Weird - skip_past: - - ${{ github.event.pull_request.draft }} - exclude: - - skip_past: true - version: '1.10' - - skip_past: true - version: '1.11' - env: - JULIA_DIT_TEST_GROUP: ${{ matrix.group }} - JULIA_DI_PR_DRAFT: ${{ github.event.pull_request.draft }} - steps: - - uses: actions/checkout@v6 - - uses: julia-actions/setup-julia@v3 - with: - version: ${{ matrix.version }} - arch: x64 - - uses: julia-actions/cache@v3 - - name: Install dependencies & run tests - run: - julia --project=./DifferentiationInterfaceTest --color=yes -e ' using Pkg; - Pkg.Registry.update(); Pkg.develop(path="./DifferentiationInterface"); - if ENV["JULIA_DI_PR_DRAFT"] == "true"; - Pkg.test("DifferentiationInterfaceTest"; allow_reresolve=false, - coverage=true, julia_args=["-O1"]); else; - Pkg.test("DifferentiationInterfaceTest"; allow_reresolve=false, - coverage=true); end;' - - uses: julia-actions/julia-processcoverage@v1 - with: - directories: ./DifferentiationInterfaceTest/src,./DifferentiationInterfaceTest/ext,./DifferentiationInterfaceTest/test - - uses: codecov/codecov-action@v6 - with: - files: lcov.info - flags: DIT - token: ${{ secrets.CODECOV_TOKEN }} - fail_ci_if_error: false + \ No newline at end of file diff --git a/DifferentiationInterface/test/Back/Enzyme/test.jl b/DifferentiationInterface/test/Back/Enzyme/test.jl index d43019d43..a0c546e4b 100644 --- a/DifferentiationInterface/test/Back/Enzyme/test.jl +++ b/DifferentiationInterface/test/Back/Enzyme/test.jl @@ -1,5 +1,7 @@ include("../../testutils.jl") +LOGGING = true + using ADTypes: ADTypes using DifferentiationInterface, DifferentiationInterfaceTest import DifferentiationInterfaceTest as DIT From f25f53d0fc8480fe9b8e11ed5a9d4f4186531be2 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 26 May 2026 13:38:39 +0200 Subject: [PATCH 02/13] Fewer tests --- .../test/Back/Enzyme/test.jl | 262 +++++++++--------- 1 file changed, 134 insertions(+), 128 deletions(-) diff --git a/DifferentiationInterface/test/Back/Enzyme/test.jl b/DifferentiationInterface/test/Back/Enzyme/test.jl index a0c546e4b..e0f864b9f 100644 --- a/DifferentiationInterface/test/Back/Enzyme/test.jl +++ b/DifferentiationInterface/test/Back/Enzyme/test.jl @@ -25,6 +25,8 @@ duplicated_backends = [ AutoEnzyme(; mode = Enzyme.Reverse, function_annotation = Enzyme.Duplicated), ] +nomatrix(scens) = filter(s -> !(s.x isa AbstractMatrix) && !(s.y isa AbstractMatrix), scens) + @testset "Checks" begin @testset "Check $(typeof(backend))" for backend in backends @test check_available(backend) @@ -32,53 +34,56 @@ duplicated_backends = [ end end; -@testset "First order" begin - test_differentiation( - backends, default_scenarios(); excluded = SECOND_ORDER, logging = LOGGING - ) - - test_differentiation( - backends[1:3], - default_scenarios(; include_normal = false, include_constantified = true); - excluded = SECOND_ORDER, - logging = LOGGING, - ) - - test_differentiation( - backends[2:3], - default_scenarios(; - include_normal = false, - include_cachified = true, - include_constantorcachified = true, - use_tuples = true, - ); - excluded = SECOND_ORDER, - logging = LOGGING, - ) - - test_differentiation( - duplicated_backends, - default_scenarios(; include_normal = false, include_closurified = true); - excluded = SECOND_ORDER, - logging = LOGGING, - ) -end +# @testset "First order" begin +# test_differentiation( +# backends, default_scenarios(); excluded = SECOND_ORDER, logging = LOGGING +# ) + +# test_differentiation( +# backends[1:3], +# default_scenarios(; include_normal = false, include_constantified = true); +# excluded = SECOND_ORDER, +# logging = LOGGING, +# ) + +# test_differentiation( +# backends[2:3], +# default_scenarios(; +# include_normal = false, +# include_cachified = true, +# include_constantorcachified = true, +# use_tuples = true, +# ); +# excluded = SECOND_ORDER, +# logging = LOGGING, +# ) + +# test_differentiation( +# duplicated_backends, +# default_scenarios(; include_normal = false, include_closurified = true); +# excluded = SECOND_ORDER, +# logging = LOGGING, +# ) +# end @testset "Second order" begin + @info "Test1" test_differentiation( AutoEnzyme(), - default_scenarios(; include_constantified = true, include_cachified = true); + nomatrix(default_scenarios(; include_constantified = true, include_cachified = true)); excluded = vcat(FIRST_ORDER, :hvp, :hessian), logging = LOGGING, ) + @info "Test2" test_differentiation( AutoEnzyme(), - default_scenarios(; include_constantified = true); + nomatrix(default_scenarios(; include_constantified = true)); excluded = vcat(FIRST_ORDER, :second_derivative), logging = LOGGING, ) + @info "Test3" test_differentiation( # TODO: simplify when https://github.com/EnzymeAD/Enzyme.jl/issues/2854 and https://github.com/EnzymeAD/Enzyme.jl/issues/2925 are fixed if VERSION >= v"1.11" @@ -89,113 +94,114 @@ end else AutoEnzyme() end, - default_scenarios(; include_normal = false, include_constantified = false, include_cachified = true); + nomatrix(default_scenarios(; include_normal = false, include_constantified = false, include_cachified = true)); excluded = vcat(FIRST_ORDER, :second_derivative), logging = LOGGING, ) + @info "Test4" test_differentiation( [ SecondOrder( AutoEnzyme(; mode = Enzyme.Reverse), AutoEnzyme(; mode = Enzyme.Forward) ), ], - default_scenarios(; include_constantified = true, include_cachified = true); + nomatrix(default_scenarios(; include_constantified = true, include_cachified = true)); excluded = FIRST_ORDER, logging = LOGGING, ) end -@testset "Sparse" begin - test_differentiation( - MyAutoSparse.(AutoEnzyme(; function_annotation = Enzyme.Const)), - if VERSION < v"1.11" - sparse_scenarios() - else - filter(s -> s.x isa AbstractVector, sparse_scenarios()) - end; - sparsity = true, - logging = LOGGING, - ) -end - -@testset "Static" begin - filtered_static_scenarios = filter(static_scenarios()) do s - DIT.operator_place(s) == :out && DIT.function_place(s) == :out - end - - test_differentiation( - [AutoEnzyme(; mode = Enzyme.Forward), AutoEnzyme(; mode = Enzyme.Reverse)], - filtered_static_scenarios; - excluded = SECOND_ORDER, - logging = LOGGING, - ) -end - -@testset "Coverage" begin - # ConstantOrCache without cache - f_nocontext(x, p) = x - @test I == DifferentiationInterface.jacobian( - f_nocontext, AutoEnzyme(; mode = Enzyme.Forward), rand(10), ConstantOrCache(nothing) - ) - @test I == DifferentiationInterface.jacobian( - f_nocontext, AutoEnzyme(; mode = Enzyme.Reverse), rand(10), ConstantOrCache(nothing) - ) -end - -@testset "Hints" begin - @testset "MutabilityError" begin - f = let - cache = [0.0] - x -> sum(copyto!(cache, x)) - end - - e = nothing - try - gradient(f, AutoEnzyme(), [1.0]) - catch e - end - msg = sprint(showerror, e) - @test occursin("AutoEnzyme", msg) - @test occursin("function_annotation", msg) - @test occursin("ADTypes", msg) - end - - @testset "RuntimeActivityError" begin - function g(active_var, constant_var, cond) - if cond - return active_var - else - return constant_var - end - end - - function h(active_var, constant_var, cond) - return [g(active_var, constant_var, cond), g(active_var, constant_var, cond)] - end - - e = nothing - try - pushforward( - h, - AutoEnzyme(; mode = Enzyme.Forward), - [1.0], - ([1.0],), - Constant([1.0]), - Constant(true), - ) - catch e - end - msg = sprint(showerror, e) - @test_broken occursin("AutoEnzyme", msg) - @test_broken occursin("ADTypes", msg) - end -end - -@testset "Empty arrays" begin - test_differentiation( - [AutoEnzyme(; mode = Enzyme.Forward), AutoEnzyme(; mode = Enzyme.Reverse)], - empty_scenarios(); - excluded = [:jacobian], - ) -end; +# @testset "Sparse" begin +# test_differentiation( +# MyAutoSparse.(AutoEnzyme(; function_annotation = Enzyme.Const)), +# if VERSION < v"1.11" +# sparse_scenarios() +# else +# filter(s -> s.x isa AbstractVector, sparse_scenarios()) +# end; +# sparsity = true, +# logging = LOGGING, +# ) +# end + +# @testset "Static" begin +# filtered_static_scenarios = filter(static_scenarios()) do s +# DIT.operator_place(s) == :out && DIT.function_place(s) == :out +# end + +# test_differentiation( +# [AutoEnzyme(; mode = Enzyme.Forward), AutoEnzyme(; mode = Enzyme.Reverse)], +# filtered_static_scenarios; +# excluded = SECOND_ORDER, +# logging = LOGGING, +# ) +# end + +# @testset "Coverage" begin +# # ConstantOrCache without cache +# f_nocontext(x, p) = x +# @test I == DifferentiationInterface.jacobian( +# f_nocontext, AutoEnzyme(; mode = Enzyme.Forward), rand(10), ConstantOrCache(nothing) +# ) +# @test I == DifferentiationInterface.jacobian( +# f_nocontext, AutoEnzyme(; mode = Enzyme.Reverse), rand(10), ConstantOrCache(nothing) +# ) +# end + +# @testset "Hints" begin +# @testset "MutabilityError" begin +# f = let +# cache = [0.0] +# x -> sum(copyto!(cache, x)) +# end + +# e = nothing +# try +# gradient(f, AutoEnzyme(), [1.0]) +# catch e +# end +# msg = sprint(showerror, e) +# @test occursin("AutoEnzyme", msg) +# @test occursin("function_annotation", msg) +# @test occursin("ADTypes", msg) +# end + +# @testset "RuntimeActivityError" begin +# function g(active_var, constant_var, cond) +# if cond +# return active_var +# else +# return constant_var +# end +# end + +# function h(active_var, constant_var, cond) +# return [g(active_var, constant_var, cond), g(active_var, constant_var, cond)] +# end + +# e = nothing +# try +# pushforward( +# h, +# AutoEnzyme(; mode = Enzyme.Forward), +# [1.0], +# ([1.0],), +# Constant([1.0]), +# Constant(true), +# ) +# catch e +# end +# msg = sprint(showerror, e) +# @test_broken occursin("AutoEnzyme", msg) +# @test_broken occursin("ADTypes", msg) +# end +# end + +# @testset "Empty arrays" begin +# test_differentiation( +# [AutoEnzyme(; mode = Enzyme.Forward), AutoEnzyme(; mode = Enzyme.Reverse)], +# empty_scenarios(); +# excluded = [:jacobian], +# ) +# end; From 65ede111bf13bb89e40b15bda8e9d59855e4669b Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 26 May 2026 13:46:27 +0200 Subject: [PATCH 03/13] Only matrices --- DifferentiationInterface/test/Back/Enzyme/test.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/DifferentiationInterface/test/Back/Enzyme/test.jl b/DifferentiationInterface/test/Back/Enzyme/test.jl index e0f864b9f..86e9574bf 100644 --- a/DifferentiationInterface/test/Back/Enzyme/test.jl +++ b/DifferentiationInterface/test/Back/Enzyme/test.jl @@ -25,7 +25,7 @@ duplicated_backends = [ AutoEnzyme(; mode = Enzyme.Reverse, function_annotation = Enzyme.Duplicated), ] -nomatrix(scens) = filter(s -> !(s.x isa AbstractMatrix) && !(s.y isa AbstractMatrix), scens) +onlymatrix(scens) = filter(s -> (s.x isa AbstractMatrix) || (s.y isa AbstractMatrix), scens) @testset "Checks" begin @testset "Check $(typeof(backend))" for backend in backends @@ -70,7 +70,7 @@ end; @info "Test1" test_differentiation( AutoEnzyme(), - nomatrix(default_scenarios(; include_constantified = true, include_cachified = true)); + onlymatrix(default_scenarios(; include_constantified = true, include_cachified = true)); excluded = vcat(FIRST_ORDER, :hvp, :hessian), logging = LOGGING, ) @@ -78,7 +78,7 @@ end; @info "Test2" test_differentiation( AutoEnzyme(), - nomatrix(default_scenarios(; include_constantified = true)); + onlymatrix(default_scenarios(; include_constantified = true)); excluded = vcat(FIRST_ORDER, :second_derivative), logging = LOGGING, ) @@ -94,7 +94,7 @@ end; else AutoEnzyme() end, - nomatrix(default_scenarios(; include_normal = false, include_constantified = false, include_cachified = true)); + onlymatrix(default_scenarios(; include_normal = false, include_constantified = false, include_cachified = true)); excluded = vcat(FIRST_ORDER, :second_derivative), logging = LOGGING, ) @@ -106,7 +106,7 @@ end; AutoEnzyme(; mode = Enzyme.Reverse), AutoEnzyme(; mode = Enzyme.Forward) ), ], - nomatrix(default_scenarios(; include_constantified = true, include_cachified = true)); + onlymatrix(default_scenarios(; include_constantified = true, include_cachified = true)); excluded = FIRST_ORDER, logging = LOGGING, ) From d26101952808c77e58df35f5d48643a24f865733 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Wed, 27 May 2026 11:33:27 +0200 Subject: [PATCH 04/13] Smaller tests --- .../test/Back/Enzyme/test.jl | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/DifferentiationInterface/test/Back/Enzyme/test.jl b/DifferentiationInterface/test/Back/Enzyme/test.jl index 86e9574bf..0ca516e05 100644 --- a/DifferentiationInterface/test/Back/Enzyme/test.jl +++ b/DifferentiationInterface/test/Back/Enzyme/test.jl @@ -67,13 +67,13 @@ end; # end @testset "Second order" begin - @info "Test1" - test_differentiation( - AutoEnzyme(), - onlymatrix(default_scenarios(; include_constantified = true, include_cachified = true)); - excluded = vcat(FIRST_ORDER, :hvp, :hessian), - logging = LOGGING, - ) + # @info "Test1" + # test_differentiation( + # AutoEnzyme(), + # onlymatrix(default_scenarios(; include_constantified = true, include_cachified = true)); + # excluded = vcat(FIRST_ORDER, :hvp, :hessian), + # logging = LOGGING, + # ) @info "Test2" test_differentiation( @@ -83,33 +83,33 @@ end; logging = LOGGING, ) - @info "Test3" - test_differentiation( - # TODO: simplify when https://github.com/EnzymeAD/Enzyme.jl/issues/2854 and https://github.com/EnzymeAD/Enzyme.jl/issues/2925 are fixed - if VERSION >= v"1.11" - SecondOrder( - AutoEnzyme(; mode = Enzyme.set_runtime_activity(Enzyme.Forward)), - AutoEnzyme(; mode = Enzyme.set_runtime_activity(Enzyme.Reverse)) - ) - else - AutoEnzyme() - end, - onlymatrix(default_scenarios(; include_normal = false, include_constantified = false, include_cachified = true)); - excluded = vcat(FIRST_ORDER, :second_derivative), - logging = LOGGING, - ) - - @info "Test4" - test_differentiation( - [ - SecondOrder( - AutoEnzyme(; mode = Enzyme.Reverse), AutoEnzyme(; mode = Enzyme.Forward) - ), - ], - onlymatrix(default_scenarios(; include_constantified = true, include_cachified = true)); - excluded = FIRST_ORDER, - logging = LOGGING, - ) + # @info "Test3" + # test_differentiation( + # # TODO: simplify when https://github.com/EnzymeAD/Enzyme.jl/issues/2854 and https://github.com/EnzymeAD/Enzyme.jl/issues/2925 are fixed + # if VERSION >= v"1.11" + # SecondOrder( + # AutoEnzyme(; mode = Enzyme.set_runtime_activity(Enzyme.Forward)), + # AutoEnzyme(; mode = Enzyme.set_runtime_activity(Enzyme.Reverse)) + # ) + # else + # AutoEnzyme() + # end, + # onlymatrix(default_scenarios(; include_normal = false, include_constantified = false, include_cachified = true)); + # excluded = vcat(FIRST_ORDER, :second_derivative), + # logging = LOGGING, + # ) + + # @info "Test4" + # test_differentiation( + # [ + # SecondOrder( + # AutoEnzyme(; mode = Enzyme.Reverse), AutoEnzyme(; mode = Enzyme.Forward) + # ), + # ], + # onlymatrix(default_scenarios(; include_constantified = true, include_cachified = true)); + # excluded = FIRST_ORDER, + # logging = LOGGING, + # ) end # @testset "Sparse" begin From b8a798b184df931cfbd0b2cdc324df1c02626250 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 2 Jun 2026 09:18:07 +0200 Subject: [PATCH 05/13] Bigger? --- .../test/Back/Enzyme/test.jl | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/DifferentiationInterface/test/Back/Enzyme/test.jl b/DifferentiationInterface/test/Back/Enzyme/test.jl index 0ca516e05..ad1189392 100644 --- a/DifferentiationInterface/test/Back/Enzyme/test.jl +++ b/DifferentiationInterface/test/Back/Enzyme/test.jl @@ -67,18 +67,23 @@ end; # end @testset "Second order" begin - # @info "Test1" - # test_differentiation( - # AutoEnzyme(), - # onlymatrix(default_scenarios(; include_constantified = true, include_cachified = true)); - # excluded = vcat(FIRST_ORDER, :hvp, :hessian), - # logging = LOGGING, - # ) + @info "Test1" + test_differentiation( + AutoEnzyme(), + onlymatrix( + default_scenarios(; + include_constantified = false, + include_cachified = false + ) + ); + excluded = vcat(FIRST_ORDER, :hvp, :hessian), + logging = LOGGING, + ) @info "Test2" test_differentiation( AutoEnzyme(), - onlymatrix(default_scenarios(; include_constantified = true)); + onlymatrix(default_scenarios(; include_constantified = false)); excluded = vcat(FIRST_ORDER, :second_derivative), logging = LOGGING, ) From ad9f7390dc2714fe033c660752e0c3c615af25e9 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 2 Jun 2026 09:34:12 +0200 Subject: [PATCH 06/13] Test3 --- .../test/Back/Enzyme/test.jl | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/DifferentiationInterface/test/Back/Enzyme/test.jl b/DifferentiationInterface/test/Back/Enzyme/test.jl index ad1189392..2d2b2fc67 100644 --- a/DifferentiationInterface/test/Back/Enzyme/test.jl +++ b/DifferentiationInterface/test/Back/Enzyme/test.jl @@ -88,21 +88,21 @@ end; logging = LOGGING, ) - # @info "Test3" - # test_differentiation( - # # TODO: simplify when https://github.com/EnzymeAD/Enzyme.jl/issues/2854 and https://github.com/EnzymeAD/Enzyme.jl/issues/2925 are fixed - # if VERSION >= v"1.11" - # SecondOrder( - # AutoEnzyme(; mode = Enzyme.set_runtime_activity(Enzyme.Forward)), - # AutoEnzyme(; mode = Enzyme.set_runtime_activity(Enzyme.Reverse)) - # ) - # else - # AutoEnzyme() - # end, - # onlymatrix(default_scenarios(; include_normal = false, include_constantified = false, include_cachified = true)); - # excluded = vcat(FIRST_ORDER, :second_derivative), - # logging = LOGGING, - # ) + @info "Test3" + test_differentiation( + # TODO: simplify when https://github.com/EnzymeAD/Enzyme.jl/issues/2854 and https://github.com/EnzymeAD/Enzyme.jl/issues/2925 are fixed + if VERSION >= v"1.11" + SecondOrder( + AutoEnzyme(; mode = Enzyme.set_runtime_activity(Enzyme.Forward)), + AutoEnzyme(; mode = Enzyme.set_runtime_activity(Enzyme.Reverse)) + ) + else + AutoEnzyme() + end, + onlymatrix(default_scenarios(; include_normal = false, include_constantified = false, include_cachified = true)); + excluded = vcat(FIRST_ORDER, :second_derivative), + logging = LOGGING, + ) # @info "Test4" # test_differentiation( From 4144ca273bbae09ce443e3c3c62bb50a2be153ef Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 2 Jun 2026 10:12:49 +0200 Subject: [PATCH 07/13] Only Test3 --- .../test/Back/Enzyme/test.jl | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/DifferentiationInterface/test/Back/Enzyme/test.jl b/DifferentiationInterface/test/Back/Enzyme/test.jl index 2d2b2fc67..c1bfb1318 100644 --- a/DifferentiationInterface/test/Back/Enzyme/test.jl +++ b/DifferentiationInterface/test/Back/Enzyme/test.jl @@ -67,26 +67,26 @@ end; # end @testset "Second order" begin - @info "Test1" - test_differentiation( - AutoEnzyme(), - onlymatrix( - default_scenarios(; - include_constantified = false, - include_cachified = false - ) - ); - excluded = vcat(FIRST_ORDER, :hvp, :hessian), - logging = LOGGING, - ) + # @info "Test1" + # test_differentiation( + # AutoEnzyme(), + # onlymatrix( + # default_scenarios(; + # include_constantified = false, + # include_cachified = false + # ) + # ); + # excluded = vcat(FIRST_ORDER, :hvp, :hessian), + # logging = LOGGING, + # ) - @info "Test2" - test_differentiation( - AutoEnzyme(), - onlymatrix(default_scenarios(; include_constantified = false)); - excluded = vcat(FIRST_ORDER, :second_derivative), - logging = LOGGING, - ) + # @info "Test2" + # test_differentiation( + # AutoEnzyme(), + # onlymatrix(default_scenarios(; include_constantified = false)); + # excluded = vcat(FIRST_ORDER, :second_derivative), + # logging = LOGGING, + # ) @info "Test3" test_differentiation( @@ -99,7 +99,13 @@ end; else AutoEnzyme() end, - onlymatrix(default_scenarios(; include_normal = false, include_constantified = false, include_cachified = true)); + onlymatrix( + default_scenarios(; + include_normal = false, + include_constantified = false, + include_cachified = true + ) + ); excluded = vcat(FIRST_ORDER, :second_derivative), logging = LOGGING, ) From 07e5f184625b461c1861d0d3fd85220abd13b119 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 2 Jun 2026 10:56:28 +0200 Subject: [PATCH 08/13] Remove hessian --- DifferentiationInterface/test/Back/Enzyme/test.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DifferentiationInterface/test/Back/Enzyme/test.jl b/DifferentiationInterface/test/Back/Enzyme/test.jl index c1bfb1318..1d26720a8 100644 --- a/DifferentiationInterface/test/Back/Enzyme/test.jl +++ b/DifferentiationInterface/test/Back/Enzyme/test.jl @@ -106,7 +106,7 @@ end; include_cachified = true ) ); - excluded = vcat(FIRST_ORDER, :second_derivative), + excluded = vcat(FIRST_ORDER, :second_derivative, :hessian), logging = LOGGING, ) From 35873e87c3524a85acc7b667da200ab909a3c1f0 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 2 Jun 2026 11:14:44 +0200 Subject: [PATCH 09/13] Single scenario --- .../test/Back/Enzyme/test.jl | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/DifferentiationInterface/test/Back/Enzyme/test.jl b/DifferentiationInterface/test/Back/Enzyme/test.jl index 1d26720a8..74d52a22b 100644 --- a/DifferentiationInterface/test/Back/Enzyme/test.jl +++ b/DifferentiationInterface/test/Back/Enzyme/test.jl @@ -99,14 +99,17 @@ end; else AutoEnzyme() end, - onlymatrix( - default_scenarios(; - include_normal = false, - include_constantified = false, - include_cachified = true + filter( + onlymatrix( + default_scenarios(; + include_normal = false, + include_constantified = false, + include_cachified = true + ) ) - ); - excluded = vcat(FIRST_ORDER, :second_derivative, :hessian), + ) do s + DifferentiationInterfaceTest.operator(s) == :hvp + end[1:1], logging = LOGGING, ) From 309a4eb11c888b4ef73b470b36767ff27614baf0 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 2 Jun 2026 11:25:24 +0200 Subject: [PATCH 10/13] No O1 --- .github/workflows/Test.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 4de005be2..83239580a 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -16,7 +16,6 @@ concurrency: cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }} jobs: - test-DI-Backend: name: ${{ matrix.version }} - DI Back (${{ matrix.group }}) runs-on: ubuntu-latest @@ -65,7 +64,7 @@ jobs: include("./DifferentiationInterface/test/Back/run_backend.jl");' - name: Install dependencies & run tests (not draft) if: ${{ !github.event.pull_request.draft }} - run: julia -O1 --code-coverage=user --color=yes -e ' using Pkg; group = + run: julia --code-coverage=user --color=yes -e ' using Pkg; group = ENV["JULIA_DI_TEST_GROUP"]; Pkg.activate("./DifferentiationInterface/test/Back/$group"); Pkg.develop([PackageSpec(path="./DifferentiationInterface"), @@ -80,5 +79,3 @@ jobs: flags: DI token: ${{ secrets.CODECOV_TOKEN }} fail_ci_if_error: false - - \ No newline at end of file From f7e6ba859c3a75bd3155c040c09a73196f5e8b98 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 2 Jun 2026 11:31:36 +0200 Subject: [PATCH 11/13] MWE --- .../test/Back/Enzyme/test.jl | 231 ++---------------- 1 file changed, 17 insertions(+), 214 deletions(-) diff --git a/DifferentiationInterface/test/Back/Enzyme/test.jl b/DifferentiationInterface/test/Back/Enzyme/test.jl index 74d52a22b..2c07c1b9b 100644 --- a/DifferentiationInterface/test/Back/Enzyme/test.jl +++ b/DifferentiationInterface/test/Back/Enzyme/test.jl @@ -1,221 +1,24 @@ -include("../../testutils.jl") - -LOGGING = true - -using ADTypes: ADTypes -using DifferentiationInterface, DifferentiationInterfaceTest -import DifferentiationInterfaceTest as DIT -using Enzyme: Enzyme -using LinearAlgebra -using StaticArrays +using DifferentiationInterface +import Enzyme using Test -using ExplicitImports -check_no_implicit_imports(DifferentiationInterface) - -backends = [ - AutoEnzyme(; mode = nothing), - AutoEnzyme(; mode = Enzyme.Forward), - AutoEnzyme(; mode = Enzyme.Reverse), - AutoEnzyme(; mode = nothing, function_annotation = Enzyme.Const), -] - -duplicated_backends = [ - AutoEnzyme(; mode = Enzyme.Forward, function_annotation = Enzyme.Duplicated), - AutoEnzyme(; mode = Enzyme.Reverse, function_annotation = Enzyme.Duplicated), -] - -onlymatrix(scens) = filter(s -> (s.x isa AbstractMatrix) || (s.y isa AbstractMatrix), scens) - -@testset "Checks" begin - @testset "Check $(typeof(backend))" for backend in backends - @test check_available(backend) - @test check_inplace(backend) - end -end; - -# @testset "First order" begin -# test_differentiation( -# backends, default_scenarios(); excluded = SECOND_ORDER, logging = LOGGING -# ) - -# test_differentiation( -# backends[1:3], -# default_scenarios(; include_normal = false, include_constantified = true); -# excluded = SECOND_ORDER, -# logging = LOGGING, -# ) - -# test_differentiation( -# backends[2:3], -# default_scenarios(; -# include_normal = false, -# include_cachified = true, -# include_constantorcachified = true, -# use_tuples = true, -# ); -# excluded = SECOND_ORDER, -# logging = LOGGING, -# ) - -# test_differentiation( -# duplicated_backends, -# default_scenarios(; include_normal = false, include_closurified = true); -# excluded = SECOND_ORDER, -# logging = LOGGING, -# ) -# end - -@testset "Second order" begin - # @info "Test1" - # test_differentiation( - # AutoEnzyme(), - # onlymatrix( - # default_scenarios(; - # include_constantified = false, - # include_cachified = false - # ) - # ); - # excluded = vcat(FIRST_ORDER, :hvp, :hessian), - # logging = LOGGING, - # ) +backend = SecondOrder( + AutoEnzyme(; mode = Enzyme.set_runtime_activity(Enzyme.Forward)), + AutoEnzyme(; mode = Enzyme.set_runtime_activity(Enzyme.Reverse)) +) - # @info "Test2" - # test_differentiation( - # AutoEnzyme(), - # onlymatrix(default_scenarios(; include_constantified = false)); - # excluded = vcat(FIRST_ORDER, :second_derivative), - # logging = LOGGING, - # ) - - @info "Test3" - test_differentiation( - # TODO: simplify when https://github.com/EnzymeAD/Enzyme.jl/issues/2854 and https://github.com/EnzymeAD/Enzyme.jl/issues/2925 are fixed - if VERSION >= v"1.11" - SecondOrder( - AutoEnzyme(; mode = Enzyme.set_runtime_activity(Enzyme.Forward)), - AutoEnzyme(; mode = Enzyme.set_runtime_activity(Enzyme.Reverse)) - ) - else - AutoEnzyme() - end, - filter( - onlymatrix( - default_scenarios(; - include_normal = false, - include_constantified = false, - include_cachified = true - ) - ) - ) do s - DifferentiationInterfaceTest.operator(s) == :hvp - end[1:1], - logging = LOGGING, - ) - - # @info "Test4" - # test_differentiation( - # [ - # SecondOrder( - # AutoEnzyme(; mode = Enzyme.Reverse), AutoEnzyme(; mode = Enzyme.Forward) - # ), - # ], - # onlymatrix(default_scenarios(; include_constantified = true, include_cachified = true)); - # excluded = FIRST_ORDER, - # logging = LOGGING, - # ) +struct StoreInCache{F} + f::F end -# @testset "Sparse" begin -# test_differentiation( -# MyAutoSparse.(AutoEnzyme(; function_annotation = Enzyme.Const)), -# if VERSION < v"1.11" -# sparse_scenarios() -# else -# filter(s -> s.x isa AbstractVector, sparse_scenarios()) -# end; -# sparsity = true, -# logging = LOGGING, -# ) -# end - -# @testset "Static" begin -# filtered_static_scenarios = filter(static_scenarios()) do s -# DIT.operator_place(s) == :out && DIT.function_place(s) == :out -# end - -# test_differentiation( -# [AutoEnzyme(; mode = Enzyme.Forward), AutoEnzyme(; mode = Enzyme.Reverse)], -# filtered_static_scenarios; -# excluded = SECOND_ORDER, -# logging = LOGGING, -# ) -# end - -# @testset "Coverage" begin -# # ConstantOrCache without cache -# f_nocontext(x, p) = x -# @test I == DifferentiationInterface.jacobian( -# f_nocontext, AutoEnzyme(; mode = Enzyme.Forward), rand(10), ConstantOrCache(nothing) -# ) -# @test I == DifferentiationInterface.jacobian( -# f_nocontext, AutoEnzyme(; mode = Enzyme.Reverse), rand(10), ConstantOrCache(nothing) -# ) -# end - -# @testset "Hints" begin -# @testset "MutabilityError" begin -# f = let -# cache = [0.0] -# x -> sum(copyto!(cache, x)) -# end - -# e = nothing -# try -# gradient(f, AutoEnzyme(), [1.0]) -# catch e -# end -# msg = sprint(showerror, e) -# @test occursin("AutoEnzyme", msg) -# @test occursin("function_annotation", msg) -# @test occursin("ADTypes", msg) -# end - -# @testset "RuntimeActivityError" begin -# function g(active_var, constant_var, cond) -# if cond -# return active_var -# else -# return constant_var -# end -# end - -# function h(active_var, constant_var, cond) -# return [g(active_var, constant_var, cond), g(active_var, constant_var, cond)] -# end +function (sc::StoreInCache)(x, y_cache) + y_cache[1] = sc.f(x) + return y_cache[1] +end -# e = nothing -# try -# pushforward( -# h, -# AutoEnzyme(; mode = Enzyme.Forward), -# [1.0], -# ([1.0],), -# Constant([1.0]), -# Constant(true), -# ) -# catch e -# end -# msg = sprint(showerror, e) -# @test_broken occursin("AutoEnzyme", msg) -# @test_broken occursin("ADTypes", msg) -# end -# end +f(x::AbstractArray) = sum(vec(x .^ 4) .* transpose(vec(x .^ 6))) -# @testset "Empty arrays" begin -# test_differentiation( -# [AutoEnzyme(; mode = Enzyme.Forward), AutoEnzyme(; mode = Enzyme.Reverse)], -# empty_scenarios(); -# excluded = [:jacobian], -# ) -# end; +x = [1.0, 2.0] +dx = [3.0, 4.0] +c = similar(x) +@test_nowarn hvp(StoreInCache(f), backend, x, (dx,), Cache(x)) From 5b143d9127f2c3a9f328c30fe96f9b39604aa032 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 2 Jun 2026 11:41:24 +0200 Subject: [PATCH 12/13] Matrix --- DifferentiationInterface/test/Back/Enzyme/test.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DifferentiationInterface/test/Back/Enzyme/test.jl b/DifferentiationInterface/test/Back/Enzyme/test.jl index 2c07c1b9b..27fffd852 100644 --- a/DifferentiationInterface/test/Back/Enzyme/test.jl +++ b/DifferentiationInterface/test/Back/Enzyme/test.jl @@ -18,7 +18,7 @@ end f(x::AbstractArray) = sum(vec(x .^ 4) .* transpose(vec(x .^ 6))) -x = [1.0, 2.0] -dx = [3.0, 4.0] +x = [1.0;;] +dx = [3.0;;] c = similar(x) @test_nowarn hvp(StoreInCache(f), backend, x, (dx,), Cache(x)) From d7798a95b6fcb93cd1ecd9a6e6768636fecf9351 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 2 Jun 2026 12:12:21 +0200 Subject: [PATCH 13/13] With grad --- DifferentiationInterface/test/Back/Enzyme/test.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DifferentiationInterface/test/Back/Enzyme/test.jl b/DifferentiationInterface/test/Back/Enzyme/test.jl index 27fffd852..834acaa18 100644 --- a/DifferentiationInterface/test/Back/Enzyme/test.jl +++ b/DifferentiationInterface/test/Back/Enzyme/test.jl @@ -21,4 +21,4 @@ f(x::AbstractArray) = sum(vec(x .^ 4) .* transpose(vec(x .^ 6))) x = [1.0;;] dx = [3.0;;] c = similar(x) -@test_nowarn hvp(StoreInCache(f), backend, x, (dx,), Cache(x)) +@test_nowarn gradient_and_hvp(StoreInCache(f), backend, x, (dx,), Cache(x))