From fea0d07d03b2a56c9c1512fa374509af3969214b Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Fri, 7 Feb 2025 10:29:44 +0100 Subject: [PATCH 01/15] test: faster compilation with -O1 option --- .github/workflows/Test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 57916a9d3..c141b291d 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -72,7 +72,7 @@ jobs: run: julia --project=./DifferentiationInterface --color=yes -e ' using Pkg; Pkg.Registry.update(); - Pkg.test("DifferentiationInterface"; coverage=true);' + Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O1"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterface/src,./DifferentiationInterface/ext,./DifferentiationInterface/test @@ -121,7 +121,7 @@ jobs: using Pkg; Pkg.Registry.update(); Pkg.develop(path="./DifferentiationInterface"); - Pkg.test("DifferentiationInterfaceTest"; coverage=true);' + Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O1"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterfaceTest/src,./DifferentiationInterfaceTest/ext,./DifferentiationInterfaceTest/test From 2f948e3700bc8257d4d05b30d7db7830dc92e577 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Fri, 7 Feb 2025 10:30:12 +0100 Subject: [PATCH 02/15] Add timing --- .github/workflows/Test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index c141b291d..6a61199dc 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -72,7 +72,7 @@ jobs: run: julia --project=./DifferentiationInterface --color=yes -e ' using Pkg; Pkg.Registry.update(); - Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O1"]);' + @time Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O1"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterface/src,./DifferentiationInterface/ext,./DifferentiationInterface/test @@ -121,7 +121,7 @@ jobs: using Pkg; Pkg.Registry.update(); Pkg.develop(path="./DifferentiationInterface"); - Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O1"]);' + @time Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O1"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterfaceTest/src,./DifferentiationInterfaceTest/ext,./DifferentiationInterfaceTest/test From b49923e883453f58e33b92d29199df175ff37ef8 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Fri, 7 Feb 2025 13:15:08 +0100 Subject: [PATCH 03/15] Time O3 --- .github/workflows/Test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 6a61199dc..fe0614c95 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -72,7 +72,7 @@ jobs: run: julia --project=./DifferentiationInterface --color=yes -e ' using Pkg; Pkg.Registry.update(); - @time Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O1"]);' + @time Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O3"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterface/src,./DifferentiationInterface/ext,./DifferentiationInterface/test @@ -121,7 +121,7 @@ jobs: using Pkg; Pkg.Registry.update(); Pkg.develop(path="./DifferentiationInterface"); - @time Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O1"]);' + @time Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O3"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterfaceTest/src,./DifferentiationInterfaceTest/ext,./DifferentiationInterfaceTest/test From 0bc52def55ca022beeedeebc7530bef8b0abb1ac Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Fri, 7 Feb 2025 13:44:05 +0100 Subject: [PATCH 04/15] Time in the correct place --- .github/workflows/Test.yml | 4 ++-- DifferentiationInterface/test/runtests.jl | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index fe0614c95..b8ff35f88 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -72,7 +72,7 @@ jobs: run: julia --project=./DifferentiationInterface --color=yes -e ' using Pkg; Pkg.Registry.update(); - @time Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O3"]);' + Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O3"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterface/src,./DifferentiationInterface/ext,./DifferentiationInterface/test @@ -121,7 +121,7 @@ jobs: using Pkg; Pkg.Registry.update(); Pkg.develop(path="./DifferentiationInterface"); - @time Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O3"]);' + Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O3"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterfaceTest/src,./DifferentiationInterfaceTest/ext,./DifferentiationInterfaceTest/test diff --git a/DifferentiationInterface/test/runtests.jl b/DifferentiationInterface/test/runtests.jl index 0ea5f4bdc..e50d4839d 100644 --- a/DifferentiationInterface/test/runtests.jl +++ b/DifferentiationInterface/test/runtests.jl @@ -13,7 +13,7 @@ include("testutils.jl") ## Main tests -@testset verbose = true "DifferentiationInterface.jl" begin +@time @testset verbose = true "DifferentiationInterface.jl" begin if haskey(ENV, "JULIA_DI_TEST_GROUP") category, folder = split(ENV["JULIA_DI_TEST_GROUP"], '/') @testset verbose = true "$category" begin From cbb241dc8ef87c20045a6692393434c45e6e97bf Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Fri, 7 Feb 2025 14:53:23 +0100 Subject: [PATCH 05/15] O2 --- .github/workflows/Test.yml | 4 ++-- DifferentiationInterfaceTest/test/runtests.jl | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index b8ff35f88..854f7097b 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -72,7 +72,7 @@ jobs: run: julia --project=./DifferentiationInterface --color=yes -e ' using Pkg; Pkg.Registry.update(); - Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O3"]);' + Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O2"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterface/src,./DifferentiationInterface/ext,./DifferentiationInterface/test @@ -121,7 +121,7 @@ jobs: using Pkg; Pkg.Registry.update(); Pkg.develop(path="./DifferentiationInterface"); - Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O3"]);' + Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O2"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterfaceTest/src,./DifferentiationInterfaceTest/ext,./DifferentiationInterfaceTest/test diff --git a/DifferentiationInterfaceTest/test/runtests.jl b/DifferentiationInterfaceTest/test/runtests.jl index 0dffa2e9a..a5ed1a48f 100644 --- a/DifferentiationInterfaceTest/test/runtests.jl +++ b/DifferentiationInterfaceTest/test/runtests.jl @@ -9,7 +9,7 @@ GROUP = get(ENV, "JULIA_DIT_TEST_GROUP", "All") ## Main tests -@testset verbose = true "DifferentiationInterfaceTest.jl" begin +@time @testset verbose = true "DifferentiationInterfaceTest.jl" begin if GROUP == "Formalities" || GROUP == "All" @testset verbose = true "Formalities" begin include("formalities.jl") From 974f6294d16080b99e477169a72ddd0e150e6667 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Fri, 7 Feb 2025 15:33:16 +0100 Subject: [PATCH 06/15] O1 --- .github/workflows/Test.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 854f7097b..ce4d0b738 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -25,7 +25,7 @@ jobs: actions: write contents: read strategy: - fail-fast: true # TODO: toggle + fail-fast: false # TODO: toggle matrix: version: - "1.10" @@ -72,7 +72,7 @@ jobs: run: julia --project=./DifferentiationInterface --color=yes -e ' using Pkg; Pkg.Registry.update(); - Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O2"]);' + Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O1"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterface/src,./DifferentiationInterface/ext,./DifferentiationInterface/test @@ -121,7 +121,7 @@ jobs: using Pkg; Pkg.Registry.update(); Pkg.develop(path="./DifferentiationInterface"); - Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O2"]);' + Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O1"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterfaceTest/src,./DifferentiationInterfaceTest/ext,./DifferentiationInterfaceTest/test From ad170d95400e5eadb511f042f8954a61eceb2cac Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Fri, 7 Feb 2025 16:14:07 +0100 Subject: [PATCH 07/15] O0 --- .github/workflows/Test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index ce4d0b738..d2566fb86 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -72,7 +72,7 @@ jobs: run: julia --project=./DifferentiationInterface --color=yes -e ' using Pkg; Pkg.Registry.update(); - Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O1"]);' + Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O0"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterface/src,./DifferentiationInterface/ext,./DifferentiationInterface/test @@ -121,7 +121,7 @@ jobs: using Pkg; Pkg.Registry.update(); Pkg.develop(path="./DifferentiationInterface"); - Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O1"]);' + Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O0"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterfaceTest/src,./DifferentiationInterfaceTest/ext,./DifferentiationInterfaceTest/test From 291ed3a09adc605d0eb31421f54a7a0c38559e42 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Fri, 7 Feb 2025 16:57:09 +0100 Subject: [PATCH 08/15] Back to O1 --- .github/workflows/Test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index d2566fb86..ce4d0b738 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -72,7 +72,7 @@ jobs: run: julia --project=./DifferentiationInterface --color=yes -e ' using Pkg; Pkg.Registry.update(); - Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O0"]);' + Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O1"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterface/src,./DifferentiationInterface/ext,./DifferentiationInterface/test @@ -121,7 +121,7 @@ jobs: using Pkg; Pkg.Registry.update(); Pkg.develop(path="./DifferentiationInterface"); - Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O0"]);' + Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O1"]);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterfaceTest/src,./DifferentiationInterfaceTest/ext,./DifferentiationInterfaceTest/test From 5264b567e7e535c054a2fff8babda0935aa24356 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Fri, 7 Feb 2025 18:02:05 +0100 Subject: [PATCH 09/15] No benchmark test on 1.10 --- DifferentiationInterface/test/Back/FiniteDiff/benchmark.jl | 2 +- DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/DifferentiationInterface/test/Back/FiniteDiff/benchmark.jl b/DifferentiationInterface/test/Back/FiniteDiff/benchmark.jl index f2783696c..c3146f634 100644 --- a/DifferentiationInterface/test/Back/FiniteDiff/benchmark.jl +++ b/DifferentiationInterface/test/Back/FiniteDiff/benchmark.jl @@ -27,7 +27,7 @@ LOGGING = get(ENV, "CI", "false") == "false" ) @testset "Analyzing benchmark results" begin @testset "$(row[:scenario])" for row in eachrow(data) - @test row[:allocs] == 0 + VERSION > v"1.10" && @test row[:allocs] == 0 end end end diff --git a/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl b/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl index aea81d03b..bc14f5866 100644 --- a/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl +++ b/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl @@ -24,7 +24,7 @@ LOGGING = get(ENV, "CI", "false") == "false" ) @testset "Analyzing benchmark results" begin @testset "$(row[:scenario])" for row in eachrow(data) - @test row[:allocs] == 0 + VERSION > v"1.10" && @test row[:allocs] == 0 end end end @@ -46,7 +46,7 @@ end ) @testset "Analyzing benchmark results" begin @testset "$(row[:scenario])" for row in eachrow(data) - @test row[:allocs] == 0 + VERSION > v"1.10" && @test row[:allocs] == 0 end end end From 241a725ed395a6060234d330ff74d3762db6a2bf Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Fri, 7 Feb 2025 22:41:56 +0100 Subject: [PATCH 10/15] Benchmark on 1.11 and higher --- DifferentiationInterface/test/Back/FiniteDiff/benchmark.jl | 2 +- DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/DifferentiationInterface/test/Back/FiniteDiff/benchmark.jl b/DifferentiationInterface/test/Back/FiniteDiff/benchmark.jl index c3146f634..ee954a53a 100644 --- a/DifferentiationInterface/test/Back/FiniteDiff/benchmark.jl +++ b/DifferentiationInterface/test/Back/FiniteDiff/benchmark.jl @@ -27,7 +27,7 @@ LOGGING = get(ENV, "CI", "false") == "false" ) @testset "Analyzing benchmark results" begin @testset "$(row[:scenario])" for row in eachrow(data) - VERSION > v"1.10" && @test row[:allocs] == 0 + VERSION >= v"1.11" && @test row[:allocs] == 0 end end end diff --git a/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl b/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl index bc14f5866..5d164251a 100644 --- a/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl +++ b/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl @@ -24,7 +24,7 @@ LOGGING = get(ENV, "CI", "false") == "false" ) @testset "Analyzing benchmark results" begin @testset "$(row[:scenario])" for row in eachrow(data) - VERSION > v"1.10" && @test row[:allocs] == 0 + VERSION >= v"1.11" && @test row[:allocs] == 0 end end end @@ -46,7 +46,7 @@ end ) @testset "Analyzing benchmark results" begin @testset "$(row[:scenario])" for row in eachrow(data) - VERSION > v"1.10" && @test row[:allocs] == 0 + VERSION >= v"1.11" && @test row[:allocs] == 0 end end end From f2596c8a44a71d62872cc6295b64a80203fd2659 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Sun, 9 Feb 2025 23:10:32 +0100 Subject: [PATCH 11/15] fix: replace copy with deepcopy in Mooncake pullbacks --- .../ext/DifferentiationInterfaceMooncakeExt/onearg.jl | 2 +- .../ext/DifferentiationInterfaceMooncakeExt/twoarg.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/onearg.jl b/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/onearg.jl index 515cd94db..065e658d6 100644 --- a/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/onearg.jl +++ b/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/onearg.jl @@ -30,7 +30,7 @@ function DI.value_and_pullback( new_y, (_, new_dx) = Mooncake.value_and_pullback!!( prep.cache, dy_righttype, f, x, map(DI.unwrap, contexts)... ) - return new_y, (copy(new_dx),) + return new_y, (deepcopy(new_dx),) end function DI.value_and_pullback!( diff --git a/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/twoarg.jl b/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/twoarg.jl index 57cb5ae07..a069be7ad 100644 --- a/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/twoarg.jl +++ b/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/twoarg.jl @@ -44,7 +44,7 @@ function DI.value_and_pullback( prep.cache, dy_righttype_after, prep.target_function, f!, y, x, contexts... ) copyto!(y, y_after) - return y, (copy(dx),) # TODO: remove this allocation in `value_and_pullback!` + return y, (deepcopy(dx),) # TODO: remove this allocation in `value_and_pullback!` end function DI.value_and_pullback( From c2583635066cb59cd4b59c2610f6d8d93bfa4018 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Sun, 9 Feb 2025 23:23:54 +0100 Subject: [PATCH 12/15] Run -O1 only for draft PRs --- .github/workflows/Test.yml | 14 ++++++++++++-- .../test/Back/FiniteDiff/benchmark.jl | 2 +- .../test/Back/ForwardDiff/benchmark.jl | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index ce4d0b738..e0cceb45e 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -60,6 +60,7 @@ jobs: group: Back/ChainRules env: JULIA_DI_TEST_GROUP: ${{ matrix.group }} + JULIA_DI_PR_DRAFT: ${{ github.event.pull_request.draft }} steps: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v2 @@ -72,7 +73,11 @@ jobs: run: julia --project=./DifferentiationInterface --color=yes -e ' using Pkg; Pkg.Registry.update(); - Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O1"]);' + if ENV["JULIA_DI_PR_DRAFT"]; + Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O1"]); + else; + Pkg.test("DifferentiationInterface"; coverage=true); + end;' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterface/src,./DifferentiationInterface/ext,./DifferentiationInterface/test @@ -109,6 +114,7 @@ jobs: version: "1.10" env: JULIA_DIT_TEST_GROUP: ${{ matrix.group }} + JULIA_DI_PR_DRAFT: ${{ github.event.pull_request.draft }} steps: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v2 @@ -121,7 +127,11 @@ jobs: using Pkg; Pkg.Registry.update(); Pkg.develop(path="./DifferentiationInterface"); - Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O1"]);' + if ENV["JULIA_DI_PR_DRAFT"]; + Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O1"]); + else; + Pkg.test("DifferentiationInterfaceTest"; coverage=true); + end;' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterfaceTest/src,./DifferentiationInterfaceTest/ext,./DifferentiationInterfaceTest/test diff --git a/DifferentiationInterface/test/Back/FiniteDiff/benchmark.jl b/DifferentiationInterface/test/Back/FiniteDiff/benchmark.jl index ee954a53a..f2783696c 100644 --- a/DifferentiationInterface/test/Back/FiniteDiff/benchmark.jl +++ b/DifferentiationInterface/test/Back/FiniteDiff/benchmark.jl @@ -27,7 +27,7 @@ LOGGING = get(ENV, "CI", "false") == "false" ) @testset "Analyzing benchmark results" begin @testset "$(row[:scenario])" for row in eachrow(data) - VERSION >= v"1.11" && @test row[:allocs] == 0 + @test row[:allocs] == 0 end end end diff --git a/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl b/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl index 5d164251a..aea81d03b 100644 --- a/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl +++ b/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl @@ -24,7 +24,7 @@ LOGGING = get(ENV, "CI", "false") == "false" ) @testset "Analyzing benchmark results" begin @testset "$(row[:scenario])" for row in eachrow(data) - VERSION >= v"1.11" && @test row[:allocs] == 0 + @test row[:allocs] == 0 end end end @@ -46,7 +46,7 @@ end ) @testset "Analyzing benchmark results" begin @testset "$(row[:scenario])" for row in eachrow(data) - VERSION >= v"1.11" && @test row[:allocs] == 0 + @test row[:allocs] == 0 end end end From 46f90ff617c5a76558efb90eb9a0b9ecdc92b35e Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Sun, 9 Feb 2025 23:47:31 +0100 Subject: [PATCH 13/15] use value_and_gradient!! --- DifferentiationInterface/Project.toml | 2 +- .../DifferentiationInterfaceMooncakeExt.jl | 5 + .../onearg.jl | 91 ++++++++++++++++--- .../twoarg.jl | 28 +++--- 4 files changed, 97 insertions(+), 29 deletions(-) diff --git a/DifferentiationInterface/Project.toml b/DifferentiationInterface/Project.toml index cec94945d..619813c33 100644 --- a/DifferentiationInterface/Project.toml +++ b/DifferentiationInterface/Project.toml @@ -62,7 +62,7 @@ ForwardDiff = "0.10.36" GTPSA = "1.4.0" JuliaFormatter = "1" LinearAlgebra = "<0.0.1,1" -Mooncake = "0.4.52" +Mooncake = "0.4.83" PolyesterForwardDiff = "0.1.2" ReverseDiff = "1.15.1" SparseArrays = "<0.0.1,1" diff --git a/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/DifferentiationInterfaceMooncakeExt.jl b/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/DifferentiationInterfaceMooncakeExt.jl index 667aadee9..bc53e6f75 100644 --- a/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/DifferentiationInterfaceMooncakeExt.jl +++ b/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/DifferentiationInterfaceMooncakeExt.jl @@ -9,6 +9,7 @@ using Mooncake: tangent, tangent_type, value_and_pullback!!, + value_and_gradient!!, zero_tangent, prepare_pullback_cache, Mooncake @@ -21,6 +22,10 @@ copyto!!(dst, src) = DI.ismutable_array(dst) ? copyto!(dst, src) : convert(typeo get_config(::AutoMooncake{Nothing}) = Config() get_config(backend::AutoMooncake{<:Config}) = backend.config +# tangents need to be copied before returning, otherwise they are still aliased in the cache +mycopy(x::Union{Number,AbstractArray}) = copy(x) +mycopy(x) = deepcopy(x) + include("onearg.jl") include("twoarg.jl") diff --git a/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/onearg.jl b/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/onearg.jl index 065e658d6..81acd8673 100644 --- a/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/onearg.jl +++ b/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/onearg.jl @@ -1,11 +1,13 @@ +## Pullback + struct MooncakeOneArgPullbackPrep{Tcache,DY} <: DI.PullbackPrep cache::Tcache dy_righttype::DY end function DI.prepare_pullback( - f, backend::AutoMooncake, x, ty::NTuple, contexts::Vararg{DI.Context,C} -) where {C} + f::F, backend::AutoMooncake, x, ty::NTuple, contexts::Vararg{DI.Context,C} +) where {F,C} config = get_config(backend) cache = prepare_pullback_cache( f, x, map(DI.unwrap, contexts)...; config.debug_mode, config.silence_debug_messages @@ -27,34 +29,34 @@ function DI.value_and_pullback( ) where {F,Y,C} dy = only(ty) dy_righttype = dy isa tangent_type(Y) ? dy : copyto!!(prep.dy_righttype, dy) - new_y, (_, new_dx) = Mooncake.value_and_pullback!!( + new_y, (_, new_dx) = value_and_pullback!!( prep.cache, dy_righttype, f, x, map(DI.unwrap, contexts)... ) - return new_y, (deepcopy(new_dx),) + return new_y, (mycopy(new_dx),) end function DI.value_and_pullback!( - f, + f::F, tx::NTuple{1}, prep::MooncakeOneArgPullbackPrep{Y}, backend::AutoMooncake, x, ty::NTuple{1}, contexts::Vararg{DI.Context,C}, -) where {Y,C} +) where {F,Y,C} y, (new_dx,) = DI.value_and_pullback(f, prep, backend, x, ty, contexts...) copyto!(only(tx), new_dx) return y, tx end function DI.value_and_pullback( - f, + f::F, prep::MooncakeOneArgPullbackPrep, backend::AutoMooncake, x, ty::NTuple, contexts::Vararg{DI.Context,C}, -) where {C} +) where {F,C} ys_and_tx = map(ty) do dy y, tx = DI.value_and_pullback(f, prep, backend, x, (dy,), contexts...) y, only(tx) @@ -65,14 +67,14 @@ function DI.value_and_pullback( end function DI.value_and_pullback!( - f, + f::F, tx::NTuple, prep::MooncakeOneArgPullbackPrep, backend::AutoMooncake, x, ty::NTuple, contexts::Vararg{DI.Context,C}, -) where {C} +) where {F,C} ys = map(tx, ty) do dx, dy y, _ = DI.value_and_pullback!(f, (dx,), prep, backend, x, (dy,), contexts...) y @@ -82,24 +84,85 @@ function DI.value_and_pullback!( end function DI.pullback( - f, + f::F, prep::MooncakeOneArgPullbackPrep, backend::AutoMooncake, x, ty::NTuple, contexts::Vararg{DI.Context,C}, -) where {C} +) where {F,C} return DI.value_and_pullback(f, prep, backend, x, ty, contexts...)[2] end function DI.pullback!( - f, + f::F, tx::NTuple, prep::MooncakeOneArgPullbackPrep, backend::AutoMooncake, x, ty::NTuple, contexts::Vararg{DI.Context,C}, -) where {C} +) where {F,C} return DI.value_and_pullback!(f, tx, prep, backend, x, ty, contexts...)[2] end + +## Gradient + +struct MooncakeGradientPrep{Tcache} <: DI.GradientPrep + cache::Tcache +end + +function DI.prepare_gradient( + f::F, backend::AutoMooncake, x, contexts::Vararg{DI.Context,C} +) where {F,C} + config = get_config(backend) + cache = prepare_pullback_cache( + f, x, map(DI.unwrap, contexts)...; config.debug_mode, config.silence_debug_messages + ) + prep = MooncakeGradientPrep(cache) + DI.value_and_gradient(f, prep, backend, x, contexts...) + return prep +end + +function DI.value_and_gradient( + f::F, prep::MooncakeGradientPrep, ::AutoMooncake, x, contexts::Vararg{DI.Context,C} +) where {F,C} + y, (_, new_grad) = value_and_gradient!!(prep.cache, f, x, map(DI.unwrap, contexts)...) + return y, mycopy(new_grad) +end + +function DI.value_and_gradient!( + f::F, + grad, + prep::MooncakeGradientPrep, + ::AutoMooncake, + x, + contexts::Vararg{DI.Context,C}, +) where {F,C} + y, (_, new_grad) = value_and_gradient!!(prep.cache, f, x, map(DI.unwrap, contexts)...) + copyto!(grad, new_grad) + return y, grad +end + +function DI.gradient( + f::F, + prep::MooncakeGradientPrep, + backend::AutoMooncake, + x, + contexts::Vararg{DI.Context,C}, +) where {F,C} + _, grad = DI.value_and_gradient(f, prep, backend, x, contexts...) + return grad +end + +function DI.gradient!( + f::F, + grad, + prep::MooncakeGradientPrep, + backend::AutoMooncake, + x, + contexts::Vararg{DI.Context,C}, +) where {F,C} + DI.value_and_gradient!(f, grad, prep, backend, x, contexts...) + return grad +end diff --git a/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/twoarg.jl b/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/twoarg.jl index a069be7ad..bd86b34f8 100644 --- a/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/twoarg.jl +++ b/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/twoarg.jl @@ -5,8 +5,8 @@ struct MooncakeTwoArgPullbackPrep{Tcache,DY,F} <: DI.PullbackPrep end function DI.prepare_pullback( - f!, y, backend::AutoMooncake, x, ty::NTuple, contexts::Vararg{DI.Context,C} -) where {C} + f!::F, y, backend::AutoMooncake, x, ty::NTuple, contexts::Vararg{DI.Context,C} +) where {F,C} target_function = function (f!, y, x, contexts...) f!(y, x, contexts...) return y @@ -26,36 +26,36 @@ function DI.prepare_pullback( end function DI.value_and_pullback( - f!, + f!::F, y, prep::MooncakeTwoArgPullbackPrep, ::AutoMooncake, x, ty::NTuple{1}, contexts::Vararg{DI.Context,C}, -) where {C} +) where {F,C} # Prepare cotangent to add after the forward pass. dy = only(ty) dy_righttype_after = copyto!(prep.dy_righttype, dy) # Run the reverse-pass and return the results. contexts = map(DI.unwrap, contexts) - y_after, (_, _, _, dx) = Mooncake.value_and_pullback!!( + y_after, (_, _, _, dx) = value_and_pullback!!( prep.cache, dy_righttype_after, prep.target_function, f!, y, x, contexts... ) copyto!(y, y_after) - return y, (deepcopy(dx),) # TODO: remove this allocation in `value_and_pullback!` + return y, (mycopy(dx),) end function DI.value_and_pullback( - f!, + f!::F, y, prep::MooncakeTwoArgPullbackPrep, backend::AutoMooncake, x, ty::NTuple, contexts::Vararg{DI.Context,C}, -) where {C} +) where {F,C} tx = map(ty) do dy _, tx = DI.value_and_pullback(f!, y, prep, backend, x, (dy,), contexts...) only(tx) @@ -64,7 +64,7 @@ function DI.value_and_pullback( end function DI.value_and_pullback!( - f!, + f!::F, y, tx::NTuple, prep::MooncakeTwoArgPullbackPrep, @@ -72,26 +72,26 @@ function DI.value_and_pullback!( x, ty::NTuple, contexts::Vararg{DI.Context,C}, -) where {C} +) where {F,C} _, new_tx = DI.value_and_pullback(f!, y, prep, backend, x, ty, contexts...) foreach(copyto!, tx, new_tx) return y, tx end function DI.pullback( - f!, + f!::F, y, prep::MooncakeTwoArgPullbackPrep, backend::AutoMooncake, x, ty::NTuple, contexts::Vararg{DI.Context,C}, -) where {C} +) where {F,C} return DI.value_and_pullback(f!, y, prep, backend, x, ty, contexts...)[2] end function DI.pullback!( - f!, + f!::F, y, tx::NTuple, prep::MooncakeTwoArgPullbackPrep, @@ -99,6 +99,6 @@ function DI.pullback!( x, ty::NTuple, contexts::Vararg{DI.Context,C}, -) where {C} +) where {F,C} return DI.value_and_pullback!(f!, y, tx, prep, backend, x, ty, contexts...)[2] end From 0d99ca21c0ecdf5b4e76083cface1c1fdfea8cd0 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Sun, 9 Feb 2025 23:59:47 +0100 Subject: [PATCH 14/15] Remove o1 changes --- .github/workflows/Test.yml | 16 +++------------- DifferentiationInterface/test/runtests.jl | 2 +- DifferentiationInterfaceTest/test/runtests.jl | 2 +- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index e0cceb45e..57916a9d3 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -25,7 +25,7 @@ jobs: actions: write contents: read strategy: - fail-fast: false # TODO: toggle + fail-fast: true # TODO: toggle matrix: version: - "1.10" @@ -60,7 +60,6 @@ jobs: group: Back/ChainRules env: JULIA_DI_TEST_GROUP: ${{ matrix.group }} - JULIA_DI_PR_DRAFT: ${{ github.event.pull_request.draft }} steps: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v2 @@ -73,11 +72,7 @@ jobs: run: julia --project=./DifferentiationInterface --color=yes -e ' using Pkg; Pkg.Registry.update(); - if ENV["JULIA_DI_PR_DRAFT"]; - Pkg.test("DifferentiationInterface"; coverage=true, julia_args=["-O1"]); - else; - Pkg.test("DifferentiationInterface"; coverage=true); - end;' + Pkg.test("DifferentiationInterface"; coverage=true);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterface/src,./DifferentiationInterface/ext,./DifferentiationInterface/test @@ -114,7 +109,6 @@ jobs: version: "1.10" env: JULIA_DIT_TEST_GROUP: ${{ matrix.group }} - JULIA_DI_PR_DRAFT: ${{ github.event.pull_request.draft }} steps: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v2 @@ -127,11 +121,7 @@ jobs: using Pkg; Pkg.Registry.update(); Pkg.develop(path="./DifferentiationInterface"); - if ENV["JULIA_DI_PR_DRAFT"]; - Pkg.test("DifferentiationInterfaceTest"; coverage=true, julia_args=["-O1"]); - else; - Pkg.test("DifferentiationInterfaceTest"; coverage=true); - end;' + Pkg.test("DifferentiationInterfaceTest"; coverage=true);' - uses: julia-actions/julia-processcoverage@v1 with: directories: ./DifferentiationInterfaceTest/src,./DifferentiationInterfaceTest/ext,./DifferentiationInterfaceTest/test diff --git a/DifferentiationInterface/test/runtests.jl b/DifferentiationInterface/test/runtests.jl index e50d4839d..0ea5f4bdc 100644 --- a/DifferentiationInterface/test/runtests.jl +++ b/DifferentiationInterface/test/runtests.jl @@ -13,7 +13,7 @@ include("testutils.jl") ## Main tests -@time @testset verbose = true "DifferentiationInterface.jl" begin +@testset verbose = true "DifferentiationInterface.jl" begin if haskey(ENV, "JULIA_DI_TEST_GROUP") category, folder = split(ENV["JULIA_DI_TEST_GROUP"], '/') @testset verbose = true "$category" begin diff --git a/DifferentiationInterfaceTest/test/runtests.jl b/DifferentiationInterfaceTest/test/runtests.jl index a5ed1a48f..0dffa2e9a 100644 --- a/DifferentiationInterfaceTest/test/runtests.jl +++ b/DifferentiationInterfaceTest/test/runtests.jl @@ -9,7 +9,7 @@ GROUP = get(ENV, "JULIA_DIT_TEST_GROUP", "All") ## Main tests -@time @testset verbose = true "DifferentiationInterfaceTest.jl" begin +@testset verbose = true "DifferentiationInterfaceTest.jl" begin if GROUP == "Formalities" || GROUP == "All" @testset verbose = true "Formalities" begin include("formalities.jl") From 6778b177804da5b66942dc3b0eccfe8953b03b18 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Mon, 10 Feb 2025 00:01:39 +0100 Subject: [PATCH 15/15] Only arrays of numbers skip deepcopy --- .../DifferentiationInterfaceMooncakeExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/DifferentiationInterfaceMooncakeExt.jl b/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/DifferentiationInterfaceMooncakeExt.jl index bc53e6f75..cdcf8d1fd 100644 --- a/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/DifferentiationInterfaceMooncakeExt.jl +++ b/DifferentiationInterface/ext/DifferentiationInterfaceMooncakeExt/DifferentiationInterfaceMooncakeExt.jl @@ -23,7 +23,7 @@ get_config(::AutoMooncake{Nothing}) = Config() get_config(backend::AutoMooncake{<:Config}) = backend.config # tangents need to be copied before returning, otherwise they are still aliased in the cache -mycopy(x::Union{Number,AbstractArray}) = copy(x) +mycopy(x::Union{Number,AbstractArray{<:Number}}) = copy(x) mycopy(x) = deepcopy(x) include("onearg.jl")