Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit ae88c9b

Browse files
Merge pull request #268 from avik-pal/patch-1
Fix Chunking
2 parents b4b816b + e4581ec commit ae88c9b

File tree

9 files changed

+47
-25
lines changed

9 files changed

+47
-25
lines changed

.github/workflows/CI.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ jobs:
1717
- InterfaceI
1818
version:
1919
- '1' # Latest Release
20-
- '~1.6' # Current LTS
21-
- '1.8'
20+
- '1.6' # Current LTS
2221
steps:
2322
- uses: actions/checkout@v4
2423
- uses: julia-actions/setup-julia@v1

.github/workflows/Downstream.yml

+3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ jobs:
1212
env:
1313
GROUP: ${{ matrix.package.group }}
1414
strategy:
15+
fail-fast: false
1516
matrix:
1617
julia-version: [1,1.6]
1718
os: [ubuntu-latest]
1819
package:
1920
- {user: SciML, repo: OrdinaryDiffEq.jl, group: InterfaceII}
21+
- {user: SciML, repo: NonlinearSolve.jl, group: All}
22+
- {user: SciML, repo: BoundaryValueDiffEq.jl, group: All}
2023

2124
steps:
2225
- uses: actions/checkout@v4

Project.toml

+1-1
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.9.0"
4+
version = "2.9.1"
55

66
[deps]
77
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"

src/highlevel/coloring.jl

+4-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ end
3232

3333
# Approximate Jacobian Sparsity Detection
3434
## Right now we hardcode it to use `ForwardDiff`
35-
function (alg::ApproximateJacobianSparsity)(ad::AbstractSparseADType, f, x; fx = nothing,
36-
kwargs...)
35+
function (alg::ApproximateJacobianSparsity)(ad::AbstractSparseADType, f::F, x; fx = nothing,
36+
kwargs...) where {F}
3737
@unpack ntrials, rng = alg
3838
fx = fx === nothing ? f(x) : fx
3939
J = fill!(similar(fx, length(fx), length(x)), 0)
@@ -47,7 +47,8 @@ function (alg::ApproximateJacobianSparsity)(ad::AbstractSparseADType, f, x; fx =
4747
fx, kwargs...)
4848
end
4949

50-
function (alg::ApproximateJacobianSparsity)(ad::AbstractSparseADType, f!, fx, x; kwargs...)
50+
function (alg::ApproximateJacobianSparsity)(ad::AbstractSparseADType, f!::F, fx, x;
51+
kwargs...) where {F}
5152
@unpack ntrials, rng = alg
5253
cfg = ForwardDiff.JacobianConfig(f!, fx, x)
5354
J = fill!(similar(fx, length(fx), length(x)), 0)

src/highlevel/common.jl

+16-5
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,23 @@ function init_jacobian end
240240
const __init_𝒥 = init_jacobian
241241

242242
# Misc Functions
243-
function __chunksize(::AutoSparseForwardDiff{C}, x) where {C}
244-
return C === nothing ? ForwardDiff.Chunk(x) : C
243+
function __chunksize(::Union{AutoSparseForwardDiff{C}, AutoForwardDiff{C}}, x) where {C}
244+
C isa ForwardDiff.Chunk && return C
245+
return __chunksize(Val(C), x)
246+
end
247+
__chunksize(::Val{nothing}, x) = ForwardDiff.Chunk(x)
248+
function __chunksize(::Val{C}, x) where {C}
249+
if C isa Integer && !(C isa Bool)
250+
return C 0 ? ForwardDiff.Chunk(x) : ForwardDiff.Chunk{C}()
251+
else
252+
error("$(C)::$(typeof(C)) is not a valid chunksize!")
253+
end
254+
end
255+
function __chunksize(::Union{AutoSparseForwardDiff{C}, AutoForwardDiff{C}}) where {C}
256+
C === nothing && return nothing
257+
C isa Integer && !(C isa Bool) && return C 0 ? nothing : Val(C)
258+
return nothing
245259
end
246-
__chunksize(::AutoSparseForwardDiff{C}) where {C} = C
247-
__chunksize(::AutoForwardDiff{C}, x) where {C} = C === nothing ? ForwardDiff.Chunk(x) : C
248-
__chunksize(::AutoForwardDiff{C}) where {C} = C
249260

250261
__f̂(f, x, idxs) = dot(vec(f(x)), idxs)
251262

src/highlevel/finite_diff.jl

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct FiniteDiffJacobianCache{CO, CA, J, FX, X} <: AbstractMaybeSparseJacobianC
77
end
88

99
function sparse_jacobian_cache(fd::Union{AutoSparseFiniteDiff, AutoFiniteDiff},
10-
sd::AbstractMaybeSparsityDetection, f, x; fx = nothing)
10+
sd::AbstractMaybeSparsityDetection, f::F, x; fx = nothing) where {F}
1111
coloring_result = sd(fd, f, x)
1212
fx = fx === nothing ? similar(f(x)) : fx
1313
if coloring_result isa NoMatrixColoring
@@ -22,7 +22,7 @@ function sparse_jacobian_cache(fd::Union{AutoSparseFiniteDiff, AutoFiniteDiff},
2222
end
2323

2424
function sparse_jacobian_cache(fd::Union{AutoSparseFiniteDiff, AutoFiniteDiff},
25-
sd::AbstractMaybeSparsityDetection, f!, fx, x)
25+
sd::AbstractMaybeSparsityDetection, f!::F, fx, x) where {F}
2626
coloring_result = sd(fd, f!, fx, x)
2727
if coloring_result isa NoMatrixColoring
2828
cache = FiniteDiff.JacobianCache(x, fx)
@@ -35,12 +35,14 @@ function sparse_jacobian_cache(fd::Union{AutoSparseFiniteDiff, AutoFiniteDiff},
3535
return FiniteDiffJacobianCache(coloring_result, cache, jac_prototype, fx, x)
3636
end
3737

38-
function sparse_jacobian!(J::AbstractMatrix, fd, cache::FiniteDiffJacobianCache, f, x)
38+
function sparse_jacobian!(J::AbstractMatrix, fd, cache::FiniteDiffJacobianCache, f::F,
39+
x) where {F}
3940
f!(y, x) = (y .= f(x))
4041
return sparse_jacobian!(J, fd, cache, f!, cache.fx, x)
4142
end
4243

43-
function sparse_jacobian!(J::AbstractMatrix, _, cache::FiniteDiffJacobianCache, f!, _, x)
44+
function sparse_jacobian!(J::AbstractMatrix, _, cache::FiniteDiffJacobianCache, f!::F, _,
45+
x) where {F}
4446
FiniteDiff.finite_difference_jacobian!(J, f!, x, cache.cache)
4547
return J
4648
end

src/highlevel/forward_mode.jl

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ end
99
struct SparseDiffToolsTag end
1010

1111
function sparse_jacobian_cache(ad::Union{AutoSparseForwardDiff, AutoForwardDiff},
12-
sd::AbstractMaybeSparsityDetection, f, x; fx = nothing)
12+
sd::AbstractMaybeSparsityDetection, f::F, x; fx = nothing) where {F}
1313
coloring_result = sd(ad, f, x)
1414
fx = fx === nothing ? similar(f(x)) : fx
1515
if coloring_result isa NoMatrixColoring
@@ -25,7 +25,7 @@ function sparse_jacobian_cache(ad::Union{AutoSparseForwardDiff, AutoForwardDiff}
2525
end
2626

2727
function sparse_jacobian_cache(ad::Union{AutoSparseForwardDiff, AutoForwardDiff},
28-
sd::AbstractMaybeSparsityDetection, f!, fx, x)
28+
sd::AbstractMaybeSparsityDetection, f!::F, fx, x) where {F}
2929
coloring_result = sd(ad, f!, fx, x)
3030
if coloring_result isa NoMatrixColoring
3131
cache = ForwardDiff.JacobianConfig(f!, fx, x, __chunksize(ad, x),
@@ -39,7 +39,8 @@ function sparse_jacobian_cache(ad::Union{AutoSparseForwardDiff, AutoForwardDiff}
3939
return ForwardDiffJacobianCache(coloring_result, cache, jac_prototype, fx, x)
4040
end
4141

42-
function sparse_jacobian!(J::AbstractMatrix, _, cache::ForwardDiffJacobianCache, f, x)
42+
function sparse_jacobian!(J::AbstractMatrix, _, cache::ForwardDiffJacobianCache, f::F,
43+
x) where {F}
4344
if cache.cache isa ForwardColorJacCache
4445
forwarddiff_color_jacobian(J, f, x, cache.cache) # Use Sparse ForwardDiff
4546
else
@@ -48,7 +49,8 @@ function sparse_jacobian!(J::AbstractMatrix, _, cache::ForwardDiffJacobianCache,
4849
return J
4950
end
5051

51-
function sparse_jacobian!(J::AbstractMatrix, _, cache::ForwardDiffJacobianCache, f!, fx, x)
52+
function sparse_jacobian!(J::AbstractMatrix, _, cache::ForwardDiffJacobianCache, f!::F, fx,
53+
x) where {F}
5254
if cache.cache isa ForwardColorJacCache
5355
forwarddiff_color_jacobian!(J, f!, x, cache.cache) # Use Sparse ForwardDiff
5456
else

src/highlevel/reverse_mode.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct ReverseModeJacobianCache{CO, CA, J, FX, X, I} <: AbstractMaybeSparseJacob
88
end
99

1010
function sparse_jacobian_cache(ad::Union{AutoEnzyme, AbstractReverseMode},
11-
sd::AbstractMaybeSparsityDetection, f, x; fx = nothing)
11+
sd::AbstractMaybeSparsityDetection, f::F, x; fx = nothing) where {F}
1212
fx = fx === nothing ? similar(f(x)) : fx
1313
coloring_result = sd(ad, f, x)
1414
jac_prototype = __getfield(coloring_result, Val(:jacobian_sparsity))
@@ -17,7 +17,7 @@ function sparse_jacobian_cache(ad::Union{AutoEnzyme, AbstractReverseMode},
1717
end
1818

1919
function sparse_jacobian_cache(ad::Union{AutoEnzyme, AbstractReverseMode},
20-
sd::AbstractMaybeSparsityDetection, f!, fx, x)
20+
sd::AbstractMaybeSparsityDetection, f!::F, fx, x) where {F}
2121
coloring_result = sd(ad, f!, fx, x)
2222
jac_prototype = __getfield(coloring_result, Val(:jacobian_sparsity))
2323
return ReverseModeJacobianCache(coloring_result, nothing, jac_prototype, fx, x,
@@ -34,12 +34,12 @@ function sparse_jacobian!(J::AbstractMatrix, ad, cache::ReverseModeJacobianCache
3434
end
3535

3636
function __sparse_jacobian_reverse_impl!(J::AbstractMatrix, ad, idx_vec,
37-
cache::MatrixColoringResult, f, x)
37+
cache::MatrixColoringResult, f::F, x) where {F}
3838
return __sparse_jacobian_reverse_impl!(J, ad, idx_vec, cache, f, nothing, x)
3939
end
4040

4141
function __sparse_jacobian_reverse_impl!(J::AbstractMatrix, ad, idx_vec,
42-
cache::MatrixColoringResult, f, fx, x)
42+
cache::MatrixColoringResult, f::F, fx, x) where {F}
4343
# If `fx` is `nothing` then assume `f` is not in-place
4444
x_ = __maybe_copy_x(ad, x)
4545
fx_ = __maybe_copy_x(ad, fx)

test/test_sparse_jacobian.jl

+6-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
4040

4141
@testset "sparse_jacobian $(nameof(typeof(difftype))): Out of Place" for difftype in (AutoSparseZygote(),
4242
AutoZygote(), AutoSparseForwardDiff(), AutoForwardDiff(),
43+
AutoSparseForwardDiff(; chunksize = 0), AutoForwardDiff(; chunksize = 0),
44+
AutoSparseForwardDiff(; chunksize = 4), AutoForwardDiff(; chunksize = 4),
4345
AutoSparseFiniteDiff(), AutoFiniteDiff(), AutoEnzyme(), AutoSparseEnzyme())
4446
@testset "Cache & Reuse" begin
4547
cache = sparse_jacobian_cache(difftype, sd, fdiff, x)
@@ -92,8 +94,10 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
9294
@info "Inplace Place Function"
9395

9496
@testset "sparse_jacobian $(nameof(typeof(difftype))): In place" for difftype in (AutoSparseForwardDiff(),
95-
AutoForwardDiff(), AutoSparseFiniteDiff(), AutoFiniteDiff(), AutoEnzyme(),
96-
AutoSparseEnzyme())
97+
AutoForwardDiff(), AutoSparseForwardDiff(; chunksize = 0),
98+
AutoForwardDiff(; chunksize = 0), AutoSparseForwardDiff(; chunksize = 4),
99+
AutoForwardDiff(; chunksize = 4), AutoSparseFiniteDiff(), AutoFiniteDiff(),
100+
AutoEnzyme(), AutoSparseEnzyme())
97101
y = similar(x)
98102
cache = sparse_jacobian_cache(difftype, sd, fdiff, y, x)
99103

0 commit comments

Comments
 (0)