Skip to content

Commit 1cc1741

Browse files
authored
Switch to SCT in the tutorial, make coloring part of API (#258)
1 parent f66c0bc commit 1cc1741

7 files changed

Lines changed: 31 additions & 29 deletions

File tree

DifferentiationInterface/docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
1616
PolyesterForwardDiff = "98d1487c-24ca-40b6-b7ab-df2af84e126b"
1717
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
1818
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
19+
SparseConnectivityTracer = "9f842d2f-2579-4b1d-911e-f412cf18a3f5"
1920
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
2021
Tapir = "07d77754-e150-4737-8c94-cd238a1fb45b"
2122
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"

DifferentiationInterface/docs/src/api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,11 @@ check_twoarg
9292
check_hessian
9393
```
9494

95-
## Translation
95+
## Miscellaneous
9696

9797
```@docs
9898
DifferentiateWith
99+
GreedyColoringAlgorithm
99100
```
100101

101102
## Internals

DifferentiationInterface/docs/src/tutorial.md

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -143,32 +143,26 @@ It provides benchmarking utilities to compare backends and help you select the o
143143

144144
To compute sparse Jacobians or Hessians, you need three ingredients (read [this survey](https://epubs.siam.org/doi/10.1137/S0036144504444711) to understand why):
145145

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. [`DifferentiationInterface.SymbolicsSparsityDetector`](@ref) (requires [Symbolics.jl](https://github.com/JuliaSymbolics/Symbolics.jl) to be loaded)
154-
2. [`DifferentiationInterface.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-
146+
1. An underlying (dense) AD backend
147+
2. A sparsity pattern detector like:
148+
- `TracerSparsityDetector`, implemented by [SparseConnectivityTracer.jl](https://github.com/adrhill/SparseConnectivityTracer.jl) (our default recommendation)
149+
- [`DifferentiationInterface.SymbolicsSparsityDetector`](@ref), implemented by DifferentiationInterface.jl with [Symbolics.jl](https://github.com/JuliaSymbolics/Symbolics.jl) but not part of the public API (it will soon be [transferred](https://github.com/JuliaSymbolics/Symbolics.jl/pull/1134))
150+
3. A coloring algorithm like:
151+
- [`GreedyColoringAlgorithm`](@ref), implemented by DifferentiationInterface.jl
152+
153+
ADTypes.jl v1.0 provides the [`AutoSparse`](@ref) wrapper to combine these three ingredients, and DifferentiationInterface.jl re-exports it.
162154
Here's an example:
163155

164156
```@example tuto
165-
import Symbolics
157+
using SparseConnectivityTracer
166158
167-
sparsity_detector = DifferentiationInterface.SymbolicsSparsityDetector()
168-
coloring_algorithm = DifferentiationInterface.GreedyColoringAlgorithm()
169159
dense_backend = AutoForwardDiff()
170160
171-
sparse_backend = AutoSparse(dense_backend; sparsity_detector, coloring_algorithm)
161+
sparse_backend = AutoSparse(
162+
dense_backend;
163+
sparsity_detector=TracerSparsityDetector(),
164+
coloring_algorithm=GreedyColoringAlgorithm(),
165+
)
172166
```
173167

174168
See how the computed Hessian is sparse, whereas the underlying backend alone would give us a dense matrix:
@@ -183,4 +177,8 @@ hessian(f, dense_backend, x)
183177

184178
The sparsity detector and coloring algorithm are called during the preparation step, which can be fairly expensive.
185179
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.
180+
After preparation, the sparse computation itself will be much faster than the dense one, and require fewer calls to the function.
181+
182+
!!! info
183+
The symbolic backends have built-in sparsity handling, so `AutoSparse(AutoSymbolics())` and `AutoSparse(AutoFastDifferentiation())` do not need additional configuration for detection or coloring.
184+
However they still benefit from preparation.

DifferentiationInterface/src/DifferentiationInterface.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ export check_available, check_twoarg, check_hessian
100100

101101
export DifferentiateWith
102102

103+
export GreedyColoringAlgorithm
104+
103105
# Re-export backends from ADTypes
104106
export AutoChainRules
105107
export AutoDiffractor

DifferentiationInterface/src/sparse/detector.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Sparsity detection algorithm based on the [Symbolics.jl tracing system](https://
66
Compatible with the [ADTypes.jl sparsity detection framework](https://sciml.github.io/ADTypes.jl/stable/#Sparsity-detector).
77
88
!!! danger
9-
This functionality is implemented in an extension, and requires Symbolics.jl to be loaded.
9+
This functionality is implemented in an extension, and requires [Symbolics.jl](https://github.com/JuliaSymbolics/Symbolics.jl) to be loaded.
1010
1111
# See also
1212

DifferentiationInterface/test/runtests.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using ADTypes
22
using DifferentiationInterface
33
using Pkg
4-
using SparseConnectivityTracer: SparseConnectivityTracer
4+
using SparseConnectivityTracer
55
using Test
66

77
push!(Base.LOAD_PATH, Base.active_project())
@@ -41,8 +41,8 @@ else
4141
end
4242

4343
function MyAutoSparse(backend::AbstractADType)
44-
coloring_algorithm = DifferentiationInterface.GreedyColoringAlgorithm()
45-
sparsity_detector = SparseConnectivityTracer.TracerSparsityDetector()
44+
coloring_algorithm = GreedyColoringAlgorithm()
45+
sparsity_detector = TracerSparsityDetector()
4646
return AutoSparse(backend; sparsity_detector, coloring_algorithm)
4747
end
4848

DifferentiationInterfaceTest/test/runtests.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using ADTypes
22
using DifferentiationInterface
33
using DifferentiationInterfaceTest
44
using Pkg
5-
using SparseConnectivityTracer: SparseConnectivityTracer
5+
using SparseConnectivityTracer
66
using Test
77

88
DI_PATH = joinpath(@__DIR__, "..", "..", "DifferentiationInterface")
@@ -11,8 +11,8 @@ if isdir(DI_PATH)
1111
end
1212

1313
function MyAutoSparse(backend::AbstractADType)
14-
coloring_algorithm = DifferentiationInterface.GreedyColoringAlgorithm()
15-
sparsity_detector = SparseConnectivityTracer.TracerSparsityDetector()
14+
coloring_algorithm = GreedyColoringAlgorithm()
15+
sparsity_detector = TracerSparsityDetector()
1616
return AutoSparse(backend; sparsity_detector, coloring_algorithm)
1717
end
1818

0 commit comments

Comments
 (0)