Skip to content

Commit 49b5ab9

Browse files
committed
Add Polyester approximate sparsity detection
1 parent 01a5e5f commit 49b5ab9

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

Project.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SparseDiffTools"
22
uuid = "47a9eef4-7e08-11e9-0b38-333d64bd3804"
33
authors = ["Pankaj Mishra <pankajmishra1511@gmail.com>", "Chris Rackauckas <contact@chrisrackauckas.com>"]
4-
version = "2.16.0"
4+
version = "2.17.0"
55

66
[deps]
77
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
@@ -27,12 +27,14 @@ VertexSafeGraphs = "19fa3120-7c27-5ec5-8db8-b0b0aa330d6f"
2727

2828
[weakdeps]
2929
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
30+
Polyester = "f517fe37-dbe3-4b94-8317-1923a5111588"
3031
PolyesterForwardDiff = "98d1487c-24ca-40b6-b7ab-df2af84e126b"
3132
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
3233
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
3334

3435
[extensions]
3536
SparseDiffToolsEnzymeExt = "Enzyme"
37+
SparseDiffToolsPolyesterExt = "Polyester"
3638
SparseDiffToolsPolyesterForwardDiffExt = "PolyesterForwardDiff"
3739
SparseDiffToolsSymbolicsExt = "Symbolics"
3840
SparseDiffToolsZygoteExt = "Zygote"
@@ -49,6 +51,7 @@ ForwardDiff = "0.10"
4951
Graphs = "1"
5052
LinearAlgebra = "<0.0.1, 1"
5153
PackageExtensionCompat = "1"
54+
Polyester = "0.7.9"
5255
PolyesterForwardDiff = "0.1.1"
5356
Random = "1.6"
5457
Reexport = "1"
@@ -70,6 +73,7 @@ BlockBandedMatrices = "ffab5731-97b5-5995-9138-79e8c1846df0"
7073
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
7174
IterativeSolvers = "42fd0dbc-a981-5370-80f2-aaf504508153"
7275
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
76+
Polyester = "f517fe37-dbe3-4b94-8317-1923a5111588"
7377
PolyesterForwardDiff = "98d1487c-24ca-40b6-b7ab-df2af84e126b"
7478
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
7579
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"

ext/SparseDiffToolsPolyesterExt.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module SparseDiffToolsPolyesterExt
2+
3+
end

ext/SparseDiffToolsPolyesterForwardDiffExt.jl

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module SparseDiffToolsPolyesterForwardDiffExt
22

3-
using ADTypes, SparseDiffTools, PolyesterForwardDiff
3+
using ADTypes, SparseDiffTools, PolyesterForwardDiff, UnPack, Random, SparseArrays
44
import ForwardDiff
55
import SparseDiffTools: AbstractMaybeSparseJacobianCache, AbstractMaybeSparsityDetection,
66
ForwardColorJacCache, NoMatrixColoring, sparse_jacobian_cache,
@@ -17,10 +17,8 @@ struct PolyesterForwardDiffJacobianCache{CO, CA, J, FX, X} <:
1717
end
1818

1919
function sparse_jacobian_cache(
20-
ad::Union{AutoSparsePolyesterForwardDiff,
21-
AutoPolyesterForwardDiff},
22-
sd::AbstractMaybeSparsityDetection, f::F, x;
23-
fx = nothing) where {F}
20+
ad::Union{AutoSparsePolyesterForwardDiff, AutoPolyesterForwardDiff},
21+
sd::AbstractMaybeSparsityDetection, f::F, x; fx = nothing) where {F}
2422
coloring_result = sd(ad, f, x)
2523
fx = fx === nothing ? similar(f(x)) : fx
2624
if coloring_result isa NoMatrixColoring
@@ -39,10 +37,8 @@ function sparse_jacobian_cache(
3937
end
4038

4139
function sparse_jacobian_cache(
42-
ad::Union{AutoSparsePolyesterForwardDiff,
43-
AutoPolyesterForwardDiff},
44-
sd::AbstractMaybeSparsityDetection, f!::F, fx,
45-
x) where {F}
40+
ad::Union{AutoSparsePolyesterForwardDiff, AutoPolyesterForwardDiff},
41+
sd::AbstractMaybeSparsityDetection, f!::F, fx, x) where {F}
4642
coloring_result = sd(ad, f!, fx, x)
4743
if coloring_result isa NoMatrixColoring
4844
cache = __chunksize(ad, x)
@@ -79,4 +75,38 @@ function sparse_jacobian!(J::AbstractMatrix, _, cache::PolyesterForwardDiffJacob
7975
return J
8076
end
8177

78+
## Approximate Sparsity Detection
79+
function (alg::ApproximateJacobianSparsity)(
80+
ad::AutoSparsePolyesterForwardDiff, f::F, x; fx = nothing, kwargs...) where {F}
81+
@unpack ntrials, rng = alg
82+
fx = fx === nothing ? f(x) : fx
83+
ck = __chunksize(ad, x)
84+
J = fill!(similar(fx, length(fx), length(x)), 0)
85+
J_cache = similar(J)
86+
x_ = similar(x)
87+
for _ in 1:ntrials
88+
randn!(rng, x_)
89+
PolyesterForwardDiff.threaded_jacobian!(f, J_cache, x_, ck)
90+
@. J += abs(J_cache)
91+
end
92+
return (JacPrototypeSparsityDetection(; jac_prototype = sparse(J), alg.alg))(ad, f, x;
93+
fx, kwargs...)
94+
end
95+
96+
function (alg::ApproximateJacobianSparsity)(ad::AutoSparsePolyesterForwardDiff, f::F, fx, x;
97+
kwargs...) where {F}
98+
@unpack ntrials, rng = alg
99+
ck = __chunksize(ad, x)
100+
J = fill!(similar(fx, length(fx), length(x)), 0)
101+
J_cache = similar(J)
102+
x_ = similar(x)
103+
for _ in 1:ntrials
104+
randn!(rng, x_)
105+
PolyesterForwardDiff.threaded_jacobian!(f, fx, J_cache, x_, ck)
106+
@. J += abs(J_cache)
107+
end
108+
return (JacPrototypeSparsityDetection(; jac_prototype = sparse(J), alg.alg))(ad, f, x;
109+
fx, kwargs...)
110+
end
111+
82112
end

src/highlevel/coloring.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ function (alg::ApproximateJacobianSparsity)(
3636
ad::AbstractSparseADType, f::F, x; fx = nothing,
3737
kwargs...) where {F}
3838
if !(ad isa AutoSparseForwardDiff)
39-
@warn "$(ad) support for approximate jacobian not implemented. Using ForwardDiff instead." maxlog=1
39+
if ad isa AutoSparsePolyesterForwardDiff
40+
@warn "$(ad) is only supported if `PolyesterForwardDiff` is explicitly loaded. Using ForwardDiff instead." maxlog=1
41+
else
42+
@warn "$(ad) support for approximate jacobian not implemented. Using ForwardDiff instead." maxlog=1
43+
end
4044
end
4145
@unpack ntrials, rng = alg
4246
fx = fx === nothing ? f(x) : fx
@@ -56,7 +60,11 @@ end
5660
function (alg::ApproximateJacobianSparsity)(ad::AbstractSparseADType, f::F, fx, x;
5761
kwargs...) where {F}
5862
if !(ad isa AutoSparseForwardDiff)
59-
@warn "$(ad) support for approximate jacobian not implemented. Using ForwardDiff instead." maxlog=1
63+
if ad isa AutoSparsePolyesterForwardDiff
64+
@warn "$(ad) is only supported if `PolyesterForwardDiff` is explicitly loaded. Using ForwardDiff instead." maxlog=1
65+
else
66+
@warn "$(ad) support for approximate jacobian not implemented. Using ForwardDiff instead." maxlog=1
67+
end
6068
end
6169
@unpack ntrials, rng = alg
6270
cfg = ForwardDiff.JacobianConfig(f, fx, x)

0 commit comments

Comments
 (0)