-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BREAKING] Move sparse functionality into package extensions (#448)
* Move extras in core code * Update backend extensions * Update docs * Typos * Typos * Fixes * Typos * Fix * Fix ForwardDiff * Fixes * Fixes * Fix Enzyme * Bump versions and compats * Move sparse functionality to extensions * Remove prefixes and add docs
- Loading branch information
Showing
13 changed files
with
226 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...ce/ext/DifferentiationInterfaceSparseArraysExt/DifferentiationInterfaceSparseArraysExt.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module DifferentiationInterfaceSparseArraysExt | ||
|
||
using ADTypes: ADTypes | ||
using Compat | ||
using DifferentiationInterface | ||
using DifferentiationInterface: | ||
DenseSparsityDetector, PushforwardFast, PushforwardSlow, basis, pushforward_performance | ||
import DifferentiationInterface as DI | ||
using SparseArrays: SparseMatrixCSC, nonzeros, nzrange, rowvals, sparse | ||
|
||
include("sparsity_detector.jl") | ||
|
||
end |
114 changes: 114 additions & 0 deletions
114
DifferentiationInterface/ext/DifferentiationInterfaceSparseArraysExt/sparsity_detector.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
## Direct | ||
|
||
function ADTypes.jacobian_sparsity(f, x, detector::DenseSparsityDetector{:direct}) | ||
@compat (; backend, atol) = detector | ||
J = jacobian(f, backend, x) | ||
return sparse(abs.(J) .> atol) | ||
end | ||
|
||
function ADTypes.jacobian_sparsity(f!, y, x, detector::DenseSparsityDetector{:direct}) | ||
@compat (; backend, atol) = detector | ||
J = jacobian(f!, y, backend, x) | ||
return sparse(abs.(J) .> atol) | ||
end | ||
|
||
function ADTypes.hessian_sparsity(f, x, detector::DenseSparsityDetector{:direct}) | ||
@compat (; backend, atol) = detector | ||
H = hessian(f, backend, x) | ||
return sparse(abs.(H) .> atol) | ||
end | ||
|
||
## Iterative | ||
|
||
function ADTypes.jacobian_sparsity(f, x, detector::DenseSparsityDetector{:iterative}) | ||
@compat (; backend, atol) = detector | ||
y = f(x) | ||
n, m = length(x), length(y) | ||
I, J = Int[], Int[] | ||
if pushforward_performance(backend) isa PushforwardFast | ||
p = similar(y) | ||
extras = prepare_pushforward_same_point( | ||
f, backend, x, basis(backend, x, first(CartesianIndices(x))) | ||
) | ||
for (kj, j) in enumerate(CartesianIndices(x)) | ||
pushforward!(f, p, extras, backend, x, basis(backend, x, j)) | ||
for ki in LinearIndices(p) | ||
if abs(p[ki]) > atol | ||
push!(I, ki) | ||
push!(J, kj) | ||
end | ||
end | ||
end | ||
else | ||
p = similar(x) | ||
extras = prepare_pullback_same_point( | ||
f, backend, x, basis(backend, y, first(CartesianIndices(y))) | ||
) | ||
for (ki, i) in enumerate(CartesianIndices(y)) | ||
pullback!(f, p, extras, backend, x, basis(backend, y, i)) | ||
for kj in LinearIndices(p) | ||
if abs(p[kj]) > atol | ||
push!(I, ki) | ||
push!(J, kj) | ||
end | ||
end | ||
end | ||
end | ||
return sparse(I, J, ones(Bool, length(I)), m, n) | ||
end | ||
|
||
function ADTypes.jacobian_sparsity(f!, y, x, detector::DenseSparsityDetector{:iterative}) | ||
@compat (; backend, atol) = detector | ||
n, m = length(x), length(y) | ||
I, J = Int[], Int[] | ||
if pushforward_performance(backend) isa PushforwardFast | ||
p = similar(y) | ||
extras = prepare_pushforward_same_point( | ||
f!, y, backend, x, basis(backend, x, first(CartesianIndices(x))) | ||
) | ||
for (kj, j) in enumerate(CartesianIndices(x)) | ||
pushforward!(f!, y, p, extras, backend, x, basis(backend, x, j)) | ||
for ki in LinearIndices(p) | ||
if abs(p[ki]) > atol | ||
push!(I, ki) | ||
push!(J, kj) | ||
end | ||
end | ||
end | ||
else | ||
p = similar(x) | ||
extras = prepare_pullback_same_point( | ||
f!, y, backend, x, basis(backend, y, first(CartesianIndices(y))) | ||
) | ||
for (ki, i) in enumerate(CartesianIndices(y)) | ||
pullback!(f!, y, p, extras, backend, x, basis(backend, y, i)) | ||
for kj in LinearIndices(p) | ||
if abs(p[kj]) > atol | ||
push!(I, ki) | ||
push!(J, kj) | ||
end | ||
end | ||
end | ||
end | ||
return sparse(I, J, ones(Bool, length(I)), m, n) | ||
end | ||
|
||
function ADTypes.hessian_sparsity(f, x, detector::DenseSparsityDetector{:iterative}) | ||
@compat (; backend, atol) = detector | ||
n = length(x) | ||
I, J = Int[], Int[] | ||
p = similar(x) | ||
extras = prepare_hvp_same_point( | ||
f, backend, x, basis(backend, x, first(CartesianIndices(x))) | ||
) | ||
for (kj, j) in enumerate(CartesianIndices(x)) | ||
hvp!(f, p, extras, backend, x, basis(backend, x, j)) | ||
for ki in LinearIndices(p) | ||
if abs(p[ki]) > atol | ||
push!(I, ki) | ||
push!(J, kj) | ||
end | ||
end | ||
end | ||
return sparse(I, J, ones(Bool, length(I)), n, n) | ||
end |
47 changes: 47 additions & 0 deletions
47
...tionInterfaceSparseMatrixColoringsExt/DifferentiationInterfaceSparseMatrixColoringsExt.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module DifferentiationInterfaceSparseMatrixColoringsExt | ||
|
||
using ADTypes: | ||
ADTypes, | ||
AbstractADType, | ||
AutoSparse, | ||
dense_ad, | ||
coloring_algorithm, | ||
sparsity_detector, | ||
jacobian_sparsity, | ||
hessian_sparsity | ||
using Compat | ||
using DifferentiationInterface | ||
using DifferentiationInterface: | ||
GradientExtras, | ||
HessianExtras, | ||
HVPExtras, | ||
JacobianExtras, | ||
PullbackExtras, | ||
PushforwardExtras, | ||
PushforwardFast, | ||
PushforwardSlow, | ||
Tangents, | ||
dense_ad, | ||
maybe_dense_ad, | ||
maybe_inner, | ||
maybe_outer, | ||
multibasis, | ||
pick_batchsize, | ||
pushforward_performance | ||
import DifferentiationInterface as DI | ||
using SparseMatrixColorings: | ||
AbstractColoringResult, | ||
ColoringProblem, | ||
GreedyColoringAlgorithm, | ||
coloring, | ||
column_colors, | ||
row_colors, | ||
column_groups, | ||
row_groups, | ||
decompress, | ||
decompress! | ||
|
||
include("jacobian.jl") | ||
include("hessian.jl") | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.