Skip to content

Commit 126b784

Browse files
authored
Add details on sparsity (#237)
* Add details on sparsity * Sparsity tutorial
1 parent 7d3b258 commit 126b784

6 files changed

Lines changed: 57 additions & 9 deletions

File tree

DifferentiationInterface/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DifferentiationInterface"
22
uuid = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
33
authors = ["Guillaume Dalle", "Adrian Hill"]
4-
version = "0.3.0"
4+
version = "0.3.1"
55

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

DifferentiationInterface/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ This package provides a backend-agnostic syntax to differentiate functions of th
2222

2323
## Features
2424

25-
- First- and second-order operators (gradients, Jacobians, Hessians and [more](https://gdalle.github.io/DifferentiationInterface.jl/DifferentiationInterface/stable/overview/))
25+
- First- and second-order operators (gradients, Jacobians, Hessians and more)
2626
- In-place and out-of-place differentiation
2727
- Preparation mechanism (e.g. to create a config or tape)
28+
- Built-in sparsity handling
2829
- Thorough validation on standard inputs and outputs (numbers, vectors, matrices)
2930
- Testing and benchmarking utilities accessible to users with [DifferentiationInterfaceTest](https://github.com/gdalle/DifferentiationInterface.jl/tree/main/DifferentiationInterfaceTest)
3031

DifferentiationInterface/docs/src/backends.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const backend_examples = (
2626
2727
checkmark(x::Bool) = x ? '✅' : '❌'
2828
unicode_check_available(backend) = checkmark(check_available(backend))
29-
unicode_check_hessian(backend) = checkmark(check_hessian(backend))
29+
unicode_check_hessian(backend) = checkmark(check_hessian(backend; verbose=false))
3030
unicode_check_twoarg(backend) = checkmark(check_twoarg(backend))
3131
3232
io = IOBuffer()

DifferentiationInterface/docs/src/overview.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,9 @@ We offer two ways to perform second-order differentiation (for [`second_derivati
111111

112112
### Sparsity
113113

114-
[ADTypes.jl](https://github.com/SciML/ADTypes.jl) provides `AutoSparse` to accelerate the computation of sparse Jacobians and Hessians:
115-
116-
- for sparse Jacobians, wrap `AutoSparse` around a first-order backend.
117-
- for sparse Hessians, wrap `AutoSparse` around a [`SecondOrder`](@ref) backend.
114+
[ADTypes.jl](https://github.com/SciML/ADTypes.jl) provides [`AutoSparse`](@ref) to accelerate the computation of sparse Jacobians and Hessians.
115+
Just wrap it around any backend, with an appropriate choice of sparsity detector and coloring algorithm, and call `jacobian` or `hessian`: the result will be sparse.
116+
See the [tutorial section on sparsity](@ref sparsity-tutorial) for details.
118117

119118
### Split reverse mode
120119

DifferentiationInterface/docs/src/tutorial.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,49 @@ And you can run the same benchmarks to see what you gained (although such a smal
138138
In short, DifferentiationInterface.jl allows for easy testing and comparison of AD backends.
139139
If you want to go further, check out the [DifferentiationInterfaceTest.jl tutorial](https://gdalle.github.io/DifferentiationInterface.jl/DifferentiationInterfaceTest/dev/tutorial/).
140140
It provides benchmarking utilities to compare backends and help you select the one that is best suited for your problem.
141+
142+
## [Handling sparsity](@id sparsity-tutorial)
143+
144+
To compute sparse Jacobians or Hessians, you need three ingredients (read [this survey](https://epubs.siam.org/doi/10.1137/S0036144504444711) to understand why):
145+
146+
1. A sparsity pattern detector
147+
2. A coloring algorithm
148+
3. An underlying AD backend
149+
150+
ADTypes.jl v1.0 defines the [`AutoSparse`](@ref) wrapper, which brings together these three ingredients.
151+
At the moment, this new wrapper is not well-supported in the ecosystem, which is why DifferentiationInterface.jl provides the necessary objects to get you started:
152+
153+
1. [`SymbolicsSparsityDetector`](@ref) (requires [Symbolics.jl](https://github.com/JuliaSymbolics/Symbolics.jl) to be loaded)
154+
2. [`GreedyColoringAlgorithm`](@ref)
155+
156+
!!! warning
157+
These objects are not part of the public API, so they can change unexpectedly between versions.
158+
159+
!!! info
160+
The symbolic backends have built-in sparsity handling, so `AutoSparse(AutoSymbolics())` and `AutoSparse(AutoFastDifferentiation())` do not need additional configuration.
161+
162+
Here's an example:
163+
164+
```@example tuto
165+
import Symbolics
166+
167+
sparsity_detector = DifferentiationInterface.SymbolicsSparsityDetector()
168+
coloring_algorithm = DifferentiationInterface.GreedyColoringAlgorithm()
169+
dense_backend = AutoForwardDiff()
170+
171+
sparse_backend = AutoSparse(dense_backend; sparsity_detector, coloring_algorithm)
172+
```
173+
174+
See how the computed Hessian is sparse, whereas the underlying backend alone would give us a dense matrix:
175+
176+
```@example tuto
177+
hessian(f, sparse_backend, x)
178+
```
179+
180+
```@example tuto
181+
hessian(f, dense_backend, x)
182+
```
183+
184+
The sparsity detector and coloring algorithm are called during the preparation step, which can be fairly expensive.
185+
If you plan to compute several Jacobians or Hessians with the same pattern but different input vectors, you should reuse the `extras` object created by `prepare_jacobian` or `prepare_hessian`.
186+
After preparation, the sparse computation itself will be much faster than the dense one, and require fewer calls to the function.

DifferentiationInterface/src/utils/check.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ Check whether `backend` supports second order differentiation by trying to compu
2626
!!! warning
2727
Might take a while due to compilation time.
2828
"""
29-
function check_hessian(backend::AbstractADType)
29+
function check_hessian(backend::AbstractADType; verbose=true)
3030
try
3131
x = [1.0, 3.0]
3232
hess = hessian(sqnorm, backend, x)
3333
return isapprox(hess, [2.0 0.0; 0.0 2.0]; rtol=1e-3)
3434
catch exception
35-
@warn "Backend $backend does not support hessian" exception
35+
if verbose
36+
@warn "Backend $backend does not support hessian" exception
37+
end
3638
return false
3739
end
3840
end

0 commit comments

Comments
 (0)