|
| 1 | +# Advanced features |
| 2 | + |
| 3 | +## Contexts |
| 4 | + |
| 5 | +### Additional arguments |
| 6 | + |
| 7 | +For all operators provided DifferentiationInterface, there can be only one differentiated (or "active") argument, which we call `x`. |
| 8 | +However, the release v0.6 introduced the possibility of additional "context" arguments, which are not differentiated but still passed to the function after `x`. |
| 9 | + |
| 10 | +Contexts can be useful if you have a function `y = f(x, a, b, c, ...)` or `f!(y, x, a, b, c, ...)` and you want derivatives of `y` with respect to `x` only. |
| 11 | +Another option would be creating a closure, but that is sometimes undesirable. |
| 12 | + |
| 13 | +!!! warning |
| 14 | + This feature is still experimental, and will likely not be supported by all backends. |
| 15 | + At the moment, it only works with ForwardDiff. |
| 16 | + |
| 17 | +### Types of contexts |
| 18 | + |
| 19 | +Every context argument must be wrapped in a subtype of [`Context`](@ref) and come after the differentiated input `x`. |
| 20 | +Right now, there is only one kind of context, namely [`Constant`](@ref), but we might add more. |
| 21 | +Semantically, calling |
| 22 | + |
| 23 | +```julia |
| 24 | +gradient(f, backend, x, Constant(c)) |
| 25 | +``` |
| 26 | + |
| 27 | +computes the partial gradient of `f(x, c)` with respect to `x`, while keeping `c` constant. |
| 28 | +Importantly, one can prepare an operator with an arbitrary value `c'` of the constant (subject to the usual restrictions on preparation). |
| 29 | + |
| 30 | +## Sparsity |
| 31 | + |
| 32 | +When faced with sparse Jacobian or Hessian matrices, one can take advantage of their sparsity pattern to speed up the computation. |
| 33 | +DifferentiationInterface does this automatically if you pass a backend of type [`AutoSparse`](@extref ADTypes.AutoSparse). |
| 34 | + |
| 35 | +!!! tip |
| 36 | + To know more about sparse AD, read the survey [_What Color Is Your Jacobian? Graph Coloring for Computing Derivatives_](https://epubs.siam.org/doi/10.1137/S0036144504444711) (Gebremedhin et al., 2005). |
| 37 | + |
| 38 | +### `AutoSparse` object |
| 39 | + |
| 40 | +An `AutoSparse` backend must be constructed from three ingredients: |
| 41 | + |
| 42 | +1. An underlying (dense) backend |
| 43 | +2. A sparsity pattern detector like: |
| 44 | + - [`TracerSparsityDetector`](@extref SparseConnectivityTracer.TracerSparsityDetector) from [SparseConnectivityTracer.jl](https://github.com/adrhill/SparseConnectivityTracer.jl) |
| 45 | + - [`SymbolicsSparsityDetector`](@extref Symbolics.SymbolicsSparsityDetector) from [Symbolics.jl](https://github.com/JuliaSymbolics/Symbolics.jl) |
| 46 | + - [`DenseSparsityDetector`](@ref) from DifferentiationInterface.jl (beware that this detector only gives a locally valid pattern) |
| 47 | +3. A coloring algorithm: [`GreedyColoringAlgorithm`](@extref SparseMatrixColorings.GreedyColoringAlgorithm) from [SparseMatrixColorings.jl](https://github.com/gdalle/SparseMatrixColorings.jl) is the only one we support. As a result, sparse AD is now located in a package extension which depends on SparseMatrixColorings.jl. |
| 48 | + |
| 49 | +`AutoSparse` backends only support [`jacobian`](@ref) and [`hessian`](@ref) (as well as their variants), because other operators do not output matrices. |
| 50 | +To obtain sparse Hessians, you need to put the `SecondOrder` backend inside `AutoSparse`, and not the other way around. |
| 51 | + |
| 52 | +!!! note |
| 53 | + Symbolic backends have built-in sparsity handling, so `AutoSparse(AutoSymbolics())` and `AutoSparse(AutoFastDifferentiation())` do not need additional configuration for pattern detection or coloring. |
| 54 | + |
| 55 | +### Cost of sparse preparation |
| 56 | + |
| 57 | +The preparation step of `jacobian` or `hessian` with an `AutoSparse` backend can be long, because it needs to detect the sparsity pattern and perform a matrix coloring. |
| 58 | +But after preparation, the more zeros are present in the matrix, the greater the speedup will be compared to dense differentiation. |
| 59 | + |
| 60 | +!!! danger |
| 61 | + The result of preparation for an `AutoSparse` backend cannot be reused if the sparsity pattern changes. |
0 commit comments