From 7da89108f33a0bc2bc92f76d297a9f0a0cc494da Mon Sep 17 00:00:00 2001 From: Qingyu Qu <2283984853@qq.com> Date: Thu, 13 Feb 2025 15:54:03 +0800 Subject: [PATCH 01/10] Add extension for SparseConnectivityTracer --- Project.toml | 8 +++- ...ocationToolsSparseConnectivityTracerExt.jl | 46 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 ext/PreallocationToolsSparseConnectivityTracerExt.jl diff --git a/Project.toml b/Project.toml index ee949b1..c6ffcb5 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "PreallocationTools" uuid = "d236fae5-4411-538c-8e31-a6e3d9e00b46" authors = ["Chris Rackauckas "] -version = "0.4.24" +version = "0.4.25" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" @@ -10,9 +10,11 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" [weakdeps] ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267" +SparseConnectivityTracer = "9f842d2f-2579-4b1d-911e-f412cf18a3f5" [extensions] PreallocationToolsReverseDiffExt = "ReverseDiff" +PreallocationToolsSparseConnectivityTracerExt = "SparseConnectivityTracer" [compat] Adapt = "3.4, 4" @@ -30,6 +32,7 @@ RecursiveArrayTools = "3.2" ReverseDiff = "1" SafeTestsets = "0.1" SparseArrays = "1" +SparseConnectivityTracer = "0.6.12" Symbolics = "5.12" Test = "1" julia = "1.10" @@ -47,8 +50,9 @@ RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267" SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" +SparseConnectivityTracer = "9f842d2f-2579-4b1d-911e-f412cf18a3f5" Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Aqua", "Random", "LabelledArrays", "LinearAlgebra", "OrdinaryDiffEq", "Test", "RecursiveArrayTools", "Pkg", "SafeTestsets", "Optimization", "OptimizationOptimJL", "SparseArrays", "Symbolics"] +test = ["Aqua", "Random", "LabelledArrays", "LinearAlgebra", "OrdinaryDiffEq", "Test", "RecursiveArrayTools", "Pkg", "SafeTestsets", "Optimization", "OptimizationOptimJL", "SparseArrays", "Symbolics", "SparseConnectivityTracer"] diff --git a/ext/PreallocationToolsSparseConnectivityTracerExt.jl b/ext/PreallocationToolsSparseConnectivityTracerExt.jl new file mode 100644 index 0000000..1701eab --- /dev/null +++ b/ext/PreallocationToolsSparseConnectivityTracerExt.jl @@ -0,0 +1,46 @@ +module PreallocationToolsSparseConnectivityTracerExt + +using PreallocationTools +isdefined(Base, :get_extension) ? (import SparseConnectivityTracer) : +(import ..SparseConnectivityTracer) + +function PreallocationTools.get_tmp( + dc::DiffCache, u::T) where {T <: SparseConnectivityTracer.Dual} + if isbitstype(T) + nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) + if nelem > length(dc.dual_du) + PreallocationTools.enlargediffcache!(dc, nelem) + end + PreallocationTools._restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem))) + else + PreallocationTools._restructure(dc.du, zeros(T, size(dc.du))) + end +end + +function PreallocationTools.get_tmp( + dc::DiffCache, ::Type{T}) where {T <: SparseConnectivityTracer.Dual} + if isbitstype(T) + nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) + if nelem > length(dc.dual_du) + PreallocationTools.enlargediffcache!(dc, nelem) + end + PreallocationTools._restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem))) + else + PreallocationTools._restructure(dc.du, zeros(T, size(dc.du))) + end +end + +function PreallocationTools.get_tmp( + dc::DiffCache, u::AbstractArray{T}) where {T <: SparseConnectivityTracer.Dual} + if isbitstype(T) + nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) + if nelem > length(dc.dual_du) + PreallocationTools.enlargediffcache!(dc, nelem) + end + PreallocationTools._restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem))) + else + PreallocationTools._restructure(dc.du, zeros(T, size(dc.du))) + end +end + +end From 99e84f916c5f2733cd35681083708eb20ed8df2a Mon Sep 17 00:00:00 2001 From: Qingyu Qu <2283984853@qq.com> Date: Thu, 13 Feb 2025 15:59:49 +0800 Subject: [PATCH 02/10] Bump SparseArrays --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index c6ffcb5..f44f30b 100644 --- a/Project.toml +++ b/Project.toml @@ -31,7 +31,7 @@ Random = "1" RecursiveArrayTools = "3.2" ReverseDiff = "1" SafeTestsets = "0.1" -SparseArrays = "1" +SparseArrays = "1.10" SparseConnectivityTracer = "0.6.12" Symbolics = "5.12" Test = "1" From 9182674b1344b84068f75b34c0f85b174971d389 Mon Sep 17 00:00:00 2001 From: Qingyu Qu <2283984853@qq.com> Date: Thu, 13 Feb 2025 16:02:47 +0800 Subject: [PATCH 03/10] bump test and pkg --- Project.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index f44f30b..75033d0 100644 --- a/Project.toml +++ b/Project.toml @@ -26,7 +26,7 @@ LinearAlgebra = "1" Optimization = "3.19" OptimizationOptimJL = "0.1.5" OrdinaryDiffEq = "6.65" -Pkg = "1" +Pkg = "1.10" Random = "1" RecursiveArrayTools = "3.2" ReverseDiff = "1" @@ -34,7 +34,7 @@ SafeTestsets = "0.1" SparseArrays = "1.10" SparseConnectivityTracer = "0.6.12" Symbolics = "5.12" -Test = "1" +Test = "1.10" julia = "1.10" [extras] From cb972de72d5b513669f5e1cac9025c107337beae Mon Sep 17 00:00:00 2001 From: Qingyu Qu <2283984853@qq.com> Date: Thu, 13 Feb 2025 16:11:41 +0800 Subject: [PATCH 04/10] Fix downgrade tests --- .github/workflows/Downgrade.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Downgrade.yml b/.github/workflows/Downgrade.yml index 0dfdc55..acbbc66 100644 --- a/.github/workflows/Downgrade.yml +++ b/.github/workflows/Downgrade.yml @@ -21,7 +21,7 @@ jobs: group: - Core version: - - '1' + - '1.10' os: - ubuntu-latest - macos-latest From 5d50bee58c9702dd291b48ead97905db6e421fab Mon Sep 17 00:00:00 2001 From: Qingyu Qu <2283984853@qq.com> Date: Thu, 13 Feb 2025 16:15:34 +0800 Subject: [PATCH 05/10] Fix LinearAlgebra --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 75033d0..8902103 100644 --- a/Project.toml +++ b/Project.toml @@ -22,7 +22,7 @@ Aqua = "0.8" ArrayInterface = "7.7" ForwardDiff = "0.10.19" LabelledArrays = "1.15" -LinearAlgebra = "1" +LinearAlgebra = "1.10" Optimization = "3.19" OptimizationOptimJL = "0.1.5" OrdinaryDiffEq = "6.65" From 7c80aadf966c0d475be46820424e956d2afef500 Mon Sep 17 00:00:00 2001 From: Qingyu Qu <2283984853@qq.com> Date: Thu, 13 Feb 2025 16:20:11 +0800 Subject: [PATCH 06/10] Fix downgrade --- Project.toml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Project.toml b/Project.toml index 8902103..64433c4 100644 --- a/Project.toml +++ b/Project.toml @@ -19,21 +19,21 @@ PreallocationToolsSparseConnectivityTracerExt = "SparseConnectivityTracer" [compat] Adapt = "3.4, 4" Aqua = "0.8" -ArrayInterface = "7.7" -ForwardDiff = "0.10.19" -LabelledArrays = "1.15" +ArrayInterface = "7.18.0" +ForwardDiff = "0.10.38" +LabelledArrays = "1.16.0" LinearAlgebra = "1.10" -Optimization = "3.19" -OptimizationOptimJL = "0.1.5" -OrdinaryDiffEq = "6.65" +Optimization = "4.1.1" +OptimizationOptimJL = "0.4.1" +OrdinaryDiffEq = "6.91.0" Pkg = "1.10" -Random = "1" -RecursiveArrayTools = "3.2" -ReverseDiff = "1" +Random = "1.10.8" +RecursiveArrayTools = "3.29.0" +ReverseDiff = "1.15.3" SafeTestsets = "0.1" SparseArrays = "1.10" SparseConnectivityTracer = "0.6.12" -Symbolics = "5.12" +Symbolics = "6.29.0" Test = "1.10" julia = "1.10" From 97f8337af0452898b7838ac80b275afaa82dcea5 Mon Sep 17 00:00:00 2001 From: Qingyu Qu <2283984853@qq.com> Date: Thu, 13 Feb 2025 16:26:21 +0800 Subject: [PATCH 07/10] Bump Adapt --- Project.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 64433c4..ae5773c 100644 --- a/Project.toml +++ b/Project.toml @@ -17,8 +17,8 @@ PreallocationToolsReverseDiffExt = "ReverseDiff" PreallocationToolsSparseConnectivityTracerExt = "SparseConnectivityTracer" [compat] -Adapt = "3.4, 4" -Aqua = "0.8" +Adapt = "4.1.1" +Aqua = "0.8.11" ArrayInterface = "7.18.0" ForwardDiff = "0.10.38" LabelledArrays = "1.16.0" From f1e630a95ea36c868e7ce580656a9be45064c82e Mon Sep 17 00:00:00 2001 From: Qingyu Qu <2283984853@qq.com> Date: Thu, 13 Feb 2025 17:28:32 +0800 Subject: [PATCH 08/10] Fix warnings --- Project.toml | 4 +++- test/core_nesteddual.jl | 6 ++++-- test/core_odes.jl | 8 ++++---- test/gpu_all.jl | 4 ++-- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Project.toml b/Project.toml index ae5773c..08501a1 100644 --- a/Project.toml +++ b/Project.toml @@ -18,6 +18,7 @@ PreallocationToolsSparseConnectivityTracerExt = "SparseConnectivityTracer" [compat] Adapt = "4.1.1" +ADTypes = "1.13" Aqua = "0.8.11" ArrayInterface = "7.18.0" ForwardDiff = "0.10.38" @@ -38,6 +39,7 @@ Test = "1.10" julia = "1.10" [extras] +ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" @@ -55,4 +57,4 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Aqua", "Random", "LabelledArrays", "LinearAlgebra", "OrdinaryDiffEq", "Test", "RecursiveArrayTools", "Pkg", "SafeTestsets", "Optimization", "OptimizationOptimJL", "SparseArrays", "Symbolics", "SparseConnectivityTracer"] +test = ["Aqua", "ADTypes", "Random", "LabelledArrays", "LinearAlgebra", "OrdinaryDiffEq", "Test", "RecursiveArrayTools", "Pkg", "SafeTestsets", "Optimization", "OptimizationOptimJL", "SparseArrays", "Symbolics", "SparseConnectivityTracer"] diff --git a/test/core_nesteddual.jl b/test/core_nesteddual.jl index 13ad1b9..2d31e5c 100644 --- a/test/core_nesteddual.jl +++ b/test/core_nesteddual.jl @@ -90,11 +90,13 @@ newtonsol = solve(optprob, Newton()) cache = DiffCache(zeros(ps, ps), [4, 4, 2]) prob = ODEProblem{true, SciMLBase.FullSpecialize}(foo, ones(ps, ps), (0.0, 1.0), (coeffs, cache)) -realsol = solve(prob, TRBDF2(chunk_size = 2), saveat = 0.0:0.1:10.0, reltol = 1e-8) +realsol = solve(prob, TRBDF2(autodiff = AutoForwardDiff(chunksize = 2)), + saveat = 0.0:0.1:10.0, reltol = 1e-8) function objfun(x, prob, realsol, cache) prob = remake(prob, u0 = eltype(x).(prob.u0), p = (x, cache)) - sol = solve(prob, TRBDF2(chunk_size = 2), saveat = 0.0:0.1:10.0, reltol = 1e-8) + sol = solve(prob, TRBDF2(autodiff = AutoForwardDiff(chunksize = 2)), + saveat = 0.0:0.1:10.0, reltol = 1e-8) ofv = 0.0 if any((s.retcode != ReturnCode.Success for s in sol)) diff --git a/test/core_odes.jl b/test/core_odes.jl index 351209f..30546c1 100644 --- a/test/core_odes.jl +++ b/test/core_odes.jl @@ -1,6 +1,6 @@ using LinearAlgebra, OrdinaryDiffEq, Test, PreallocationTools, LabelledArrays, - RecursiveArrayTools + RecursiveArrayTools, ADTypes #Base array function foo(du, u, (A, tmp), t) @@ -15,12 +15,12 @@ u0 = ones(5, 5) A = ones(5, 5) cache = DiffCache(zeros(5, 5), chunk_size) prob = ODEProblem{true, SciMLBase.FullSpecialize}(foo, u0, (0.0, 1.0), (A, cache)) -sol = solve(prob, Rodas5P(chunk_size = chunk_size)) +sol = solve(prob, Rodas5P(autodiff = AutoForwardDiff(chunksize = chunk_size))) @test sol.retcode == ReturnCode.Success cache = FixedSizeDiffCache(zeros(5, 5), chunk_size) prob = ODEProblem{true, SciMLBase.FullSpecialize}(foo, u0, (0.0, 1.0), (A, cache)) -sol = solve(prob, Rodas5P(chunk_size = chunk_size)) +sol = solve(prob, Rodas5P(autodiff = AutoForwardDiff(chunksize = chunk_size))) @test sol.retcode == ReturnCode.Success #with auto-detected chunk_size @@ -60,7 +60,7 @@ end chunk_size = 4 prob = ODEProblem{true, SciMLBase.FullSpecialize}(foo, u0, (0.0, 1.0), (A, DiffCache(c, chunk_size))) -sol = solve(prob, Rodas5P(chunk_size = chunk_size)) +sol = solve(prob, Rodas5P(autodiff = AutoForwardDiff(chunksize = chunk_size))) @test sol.retcode == ReturnCode.Success #with auto-detected chunk_size prob = ODEProblem{true, SciMLBase.FullSpecialize}(foo, u0, (0.0, 1.0), (A, DiffCache(c))) diff --git a/test/gpu_all.jl b/test/gpu_all.jl index ca6362b..657b90d 100644 --- a/test/gpu_all.jl +++ b/test/gpu_all.jl @@ -61,12 +61,12 @@ u0 = cu(rand(10, 10)) #example kept small for test purposes. A = cu(-randn(10, 10)) cache = DiffCache(cu(zeros(10, 10)), chunk_size) prob = ODEProblem{true, SciMLBase.FullSpecialize}(foo, u0, (0.0f0, 1.0f0), (A, cache)) -sol = solve(prob, TRBDF2(chunk_size = chunk_size)) +sol = solve(prob, TRBDF2(autodiff = AutoForwardDiff(chunksize = chunk_size))) @test sol.retcode == ReturnCode.Success cache = FixedSizeDiffCache(cu(zeros(10, 10)), chunk_size) prob = ODEProblem{true, SciMLBase.FullSpecialize}(foo, u0, (0.0f0, 1.0f0), (A, cache)) -sol = solve(prob, TRBDF2(chunk_size = chunk_size)) +sol = solve(prob, TRBDF2(autodiff = AutoForwardDiff(chunksize = chunk_size))) @test sol.retcode == ReturnCode.Success #with auto-detected chunk_size From a76959a1303b8c83e613320086d8c56b3d15b94b Mon Sep 17 00:00:00 2001 From: Qingyu Qu <2283984853@qq.com> Date: Thu, 13 Feb 2025 17:52:21 +0800 Subject: [PATCH 09/10] Fix GPU tests using ADTypes --- test/gpu_all.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/gpu_all.jl b/test/gpu_all.jl index 657b90d..ce8c3ac 100644 --- a/test/gpu_all.jl +++ b/test/gpu_all.jl @@ -1,5 +1,5 @@ using LinearAlgebra, - OrdinaryDiffEq, Test, PreallocationTools, CUDA, ForwardDiff + OrdinaryDiffEq, Test, PreallocationTools, CUDA, ForwardDiff, ADTypes # upstream OrdinaryDiffEq.DiffEqBase.anyeltypedual(x::FixedSizeDiffCache, counter = 0) = Any From d494f72537dc91cb011d9ff769084721d3c2421e Mon Sep 17 00:00:00 2001 From: Qingyu Qu <2283984853@qq.com> Date: Thu, 13 Feb 2025 17:57:47 +0800 Subject: [PATCH 10/10] Change chunksize --- test/core_odes.jl | 2 +- test/gpu_all.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/core_odes.jl b/test/core_odes.jl index 30546c1..536ca09 100644 --- a/test/core_odes.jl +++ b/test/core_odes.jl @@ -10,7 +10,7 @@ function foo(du, u, (A, tmp), t) nothing end #with defined chunk_size -chunk_size = 5 +chunk_size = 9 u0 = ones(5, 5) A = ones(5, 5) cache = DiffCache(zeros(5, 5), chunk_size) diff --git a/test/gpu_all.jl b/test/gpu_all.jl index ce8c3ac..81a2fbb 100644 --- a/test/gpu_all.jl +++ b/test/gpu_all.jl @@ -56,7 +56,7 @@ function foo(du, u, (A, tmp), t) nothing end #with specified chunk_size -chunk_size = 10 +chunk_size = 9 u0 = cu(rand(10, 10)) #example kept small for test purposes. A = cu(-randn(10, 10)) cache = DiffCache(cu(zeros(10, 10)), chunk_size)