Skip to content

Commit a2793bd

Browse files
authored
Improve docs, revamp checks (#449)
1 parent 1069266 commit a2793bd

43 files changed

Lines changed: 152 additions & 217 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

DifferentiationInterface/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ An interface to various automatic differentiation (AD) backends in Julia.
1717

1818
## Goal
1919

20-
This package provides a backend-agnostic syntax to differentiate functions of the following types:
21-
22-
- _one-argument functions_ (allocating): `f(x) = y`
23-
- _two-argument functions_ (mutating): `f!(y, x) = nothing`
20+
This package provides a unified syntax to differentiate functions.
2421

2522
## Features
2623

DifferentiationInterface/docs/src/api.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ value_gradient_and_hessian!
103103

104104
```@docs
105105
check_available
106-
check_twoarg
107-
check_hessian
106+
check_inplace
108107
DifferentiationInterface.outer
109108
DifferentiationInterface.inner
110109
```

DifferentiationInterface/docs/src/backends.md

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ This page is about the latter, check out [that page](@ref "Operators") to learn
55

66
## List of backends
77

8-
We support all dense backend choices from [ADTypes.jl](https://github.com/SciML/ADTypes.jl):
8+
We support the following dense backend choices from [ADTypes.jl](https://github.com/SciML/ADTypes.jl):
99

10+
- [`AutoChainRules`](@extref ADTypes.AutoChainRules)
1011
- [`AutoDiffractor`](@extref ADTypes.AutoDiffractor)
1112
- [`AutoEnzyme`](@extref ADTypes.AutoEnzyme)
1213
- [`AutoFastDifferentiation`](@extref ADTypes.AutoFastDifferentiation)
@@ -37,11 +38,17 @@ We strongly recommend that users upgrade to Julia 1.10 or above, where all backe
3738

3839
## Features
3940

41+
Given a backend object, you can use:
42+
43+
- [`check_available`](@ref) to know whether the required AD package is loaded
44+
- [`check_inplace`](@ref) to know whether the backend supports in-place functions (all backends support out-of-place functions)
45+
4046
```@setup backends
4147
using ADTypes
4248
using DifferentiationInterface
4349
import Markdown
4450
51+
import ChainRulesCore
4552
import Diffractor
4653
import Enzyme
4754
import FastDifferentiation
@@ -56,6 +63,7 @@ import Tracker
5663
import Zygote
5764
5865
backend_examples = [
66+
AutoChainRules(; ruleconfig=Zygote.ZygoteRuleConfig()),
5967
AutoDiffractor(),
6068
AutoEnzyme(),
6169
AutoFastDifferentiation(),
@@ -72,17 +80,16 @@ backend_examples = [
7280
7381
checkmark(x::Bool) = x ? '✅' : '❌'
7482
unicode_check_available(backend) = checkmark(check_available(backend))
75-
unicode_check_hessian(backend) = checkmark(check_hessian(backend; verbose=false))
76-
unicode_check_twoarg(backend) = checkmark(check_twoarg(backend))
83+
unicode_check_inplace(backend) = checkmark(check_inplace(backend))
7784
7885
io = IOBuffer()
7986
8087
# Table header
81-
println(io, "| Backend | Availability | Two-argument functions | Hessian support |")
82-
println(io, "|:--------|:------------:|:----------------------:|:---------------:|")
88+
println(io, "| Backend | Availability | In-place functions |")
89+
println(io, "|:--------|:------------:|:----------------------:|")
8390
8491
for b in backend_examples
85-
join(io, ["`$(nameof(typeof(b)))`", unicode_check_available(b), unicode_check_twoarg(b), unicode_check_hessian(b)], '|')
92+
join(io, ["`$(nameof(typeof(b)))`", unicode_check_available(b), unicode_check_inplace(b)], '|')
8693
println(io, '|' )
8794
end
8895
backend_table = Markdown.parse(String(take!(io)))
@@ -92,21 +99,6 @@ backend_table = Markdown.parse(String(take!(io)))
9299
backend_table #hide
93100
```
94101

95-
### Availability
96-
97-
You can use [`check_available`](@ref) to verify whether a given backend is loaded.
98-
99-
### Support for two-argument functions
100-
101-
All backends are compatible with one-argument functions `f(x) = y`.
102-
Only some are compatible with two-argument functions `f!(y, x) = nothing`.
103-
You can use [`check_twoarg`](@ref) to verify this compatibility.
104-
105-
### Support for Hessian
106-
107-
Only some backends are able to compute Hessians.
108-
You can use [`check_hessian`](@ref) to verify this feature (beware that it will try to compute a small Hessian, so it is not instantaneous like the other checks).
109-
110102
## Backend switch
111103

112104
The wrapper [`DifferentiateWith`](@ref) allows you to switch between backends.

DifferentiationInterface/docs/src/dev_guide.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ Most operators have 4 variants, which look like this in the first order: `operat
2424
## New operator
2525

2626
To implement a new operator for an existing backend, you need to write 5 methods: 1 for [preparation](@ref Preparation) and 4 corresponding to the variants of the operator (see above).
27-
In some cases, a subset of those methods will be enough, but most of the time, forgetting one will trigger errors.
28-
For first-order operators, you may also want to support [two-argument functions](@ref "Mutation and signatures"), which requires another 5 methods (defined on `f!` instead of `f`).
27+
For first-order operators, you may also want to support [in-place functions](@ref "Mutation and signatures"), which requires another 5 methods (defined on `f!` instead of `f`).
2928

3029
The method `prepare_operator` must output an `extras` object of the correct type.
3130
For instance, `prepare_gradient(f, backend, x)` must return a [`DifferentiationInterface.GradientExtras`](@ref).
@@ -40,7 +39,7 @@ Your AD package needs to be registered first.
4039
### Core code
4140

4241
In the main package, you should define a new struct `SuperDiffBackend` which subtypes [`ADTypes.AbstractADType`](@extref ADTypes), and endow it with the fields you need to parametrize your differentiation routines.
43-
You also have to define [`ADTypes.mode`](@extref) and [`DifferentiationInterface.twoarg_support`](@ref) on `SuperDiffBackend`.
42+
You also have to define [`ADTypes.mode`](@extref) and [`DifferentiationInterface.inplace_support`](@ref) on `SuperDiffBackend`.
4443

4544
!!! info
4645
In the end, this backend struct will need to be contributed to [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
@@ -79,5 +78,4 @@ GROUP = get(ENV, "JULIA_DI_TEST_GROUP", "Back/SuperDiff")
7978

8079
but don't forget to switch it back before pushing.
8180

82-
Finally, you need to add your backend to the documentation, modifying every page that involves a list of backends.
83-
That includes the README.
81+
Finally, you need to add your backend to the documentation, modifying every page that involves a list of backends (including the `README.md`).

DifferentiationInterface/docs/src/implementations.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# Implementations
22

3-
DifferentiationInterface.jl provides a handful of [operators](@ref "Operators") like [`gradient`](@ref) or [`jacobian`](@ref), each with several variants:
4-
5-
- **out-of-place** or **in-place** behavior
6-
- **with** or **without primal** output value
7-
- support for **one-argument functions** `y = f(x)` or **two-argument functions** `f!(y, x)`
3+
DifferentiationInterface.jl provides a handful of [operators](@ref "Operators") like [`gradient`](@ref) or [`jacobian`](@ref), each with several variants: out-of-place or in-place behavior, with or without primal output value.
84

95
While it is possible to define every operator using just [`pushforward`](@ref) and [`pullback`](@ref), some backends have more efficient implementations of high-level operators.
106
When they are available, we nearly always call these backend-specific overloads.
@@ -24,7 +20,7 @@ The cells can have three values:
2420
```@setup overloads
2521
using ADTypes: AbstractADType
2622
using DifferentiationInterface
27-
using DifferentiationInterface: twoarg_support, TwoArgSupported
23+
using DifferentiationInterface: inplace_support, InPlaceSupported
2824
using Markdown: Markdown
2925
3026
using Diffractor: Diffractor
@@ -152,16 +148,16 @@ function print_overloads(backend, ext::Symbol)
152148
io = IOBuffer()
153149
ext = Base.get_extension(DifferentiationInterface, ext)
154150
155-
println(io, "#### One-argument functions `y = f(x)`")
151+
println(io, "#### Out-of-place functions `f(x) = y`")
156152
println(io)
157153
print_overload_table(io, operators_and_types_f(backend), ext)
158154
159-
println(io, "#### Two-argument functions `f!(y, x)`")
155+
println(io, "#### In-place functions `f!(y, x) = nothing`")
160156
println(io)
161-
if twoarg_support(backend) == TwoArgSupported()
157+
if inplace_support(backend) == InPlaceSupported()
162158
print_overload_table(io, operators_and_types_f!(backend), ext)
163159
else
164-
println(io, "Backend doesn't support mutating functions.")
160+
println(io, "Backend doesn't support in-place functions.")
165161
end
166162
167163
return Markdown.parse(String(take!(io)))

0 commit comments

Comments
 (0)