Skip to content

Commit 99d56b1

Browse files
authored
Reorganise the docs (#509)
1 parent c167fd6 commit 99d56b1

22 files changed

+463
-440
lines changed

docs/Manifest.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
1313
deps = ["Compat", "LinearAlgebra", "SparseArrays"]
1414
path = ".."
1515
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
16-
version = "1.11.0"
16+
version = "1.11.1"
1717

1818
[[Compat]]
1919
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]

docs/make.jl

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,37 @@ makedocs(;
4646
authors="Jarrett Revels and other contributors",
4747
pages=[
4848
"Introduction" => "index.md",
49-
"FAQ" => "FAQ.md",
50-
"Rule configurations and calling back into AD" => "config.md",
51-
"Opting out of rules" => "opting_out_of_rules.md",
52-
"Writing Good Rules" => "writing_good_rules.md",
53-
"Complex Numbers" => "complex.md",
54-
"Deriving Array Rules" => "arrays.md",
55-
"Debug Mode" => "debug_mode.md",
56-
"Gradient Accumulation" => "gradient_accumulation.md",
57-
"Usage in AD" => "use_in_ad_system.md",
58-
"Converting ZygoteRules" => "converting_zygoterules.md",
59-
"Tips for making packages work with AD" => "tips_for_packages.md",
60-
"Videos" => "videos.md",
49+
"How to use ChainRules as a rule author" => [
50+
"Introduction" => "rule_author/intro.md",
51+
"Differentials" => "rule_author/differentials.md",
52+
#"`frule` and `rrule`" => "rule_author/rules.md", # TODO: a complete example
53+
"Writing good rules" => "rule_author/writing_good_rules.md",
54+
"Testing your rules" => "rule_author/testing.md",
55+
"Superpowers" => [
56+
"`ProjectTo`" => "rule_author/superpowers/projectto.md",
57+
"`@opt_out`" => "rule_author/superpowers/opt_out.md",
58+
"`RuleConfig`" => "rule_author/superpowers/ruleconfig.md",
59+
"Gradient accumulation" => "rule_author/superpowers/gradient_accumulation.md",
60+
],
61+
"Converting ZygoteRules.@adjoint to rrules" => "rule_author/converting_zygoterules.md",
62+
"Tips for making your package work with AD" => "rule_author/tips_for_packages.md",
63+
"Debug mode" => "rule_author/debug_mode.md",
64+
],
65+
"How to support ChainRules rules as an AD package author" => [
66+
"Usage in AD" => "ad_author/use_in_ad_system.md",
67+
"Suport calling back into ADs" => "ad_author/call_back_into_ad.md",
68+
"Support opting out of rules" => "ad_author/opt_out.md",
69+
],
70+
"The maths" => [
71+
"The propagators: pushforward and pullback" => "maths/propagators.md",
72+
"Complex numbers" => "maths/complex.md",
73+
"Deriving array rules" => "maths/arrays.md",
74+
],
6175
"Design" => [
6276
"Changing the Primal" => "design/changing_the_primal.md",
6377
"Many Differential Types" => "design/many_differentials.md",
6478
],
79+
"FAQ" => "FAQ.md",
6580
"API" => "api.md",
6681
],
6782
strict=true,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Declaring support for calling back into ADs
2+
3+
To declare support or lack of support for forward and reverse-mode, use the two pairs of complementary types.
4+
For reverse mode: [`HasReverseMode`](@ref), [`NoReverseMode`](@ref).
5+
For forwards mode: [`HasForwardsMode`](@ref), [`NoForwardsMode`](@ref).
6+
AD systems that support any calling back into AD should have one from each set.
7+
8+
If an AD `HasReverseMode`, then it must define [`rrule_via_ad`](@ref) for that RuleConfig subtype.
9+
Similarly, if an AD `HasForwardsMode` then it must define [`frule_via_ad`](@ref) for that RuleConfig subtype.
10+
11+
For example:
12+
```julia
13+
struct MyReverseOnlyADRuleConfig <: RuleConfig{Union{HasReverseMode, NoForwardsMode}} end
14+
15+
function ChainRulesCore.rrule_via_ad(::MyReverseOnlyADRuleConfig, f, args...)
16+
...
17+
return y, pullback
18+
end
19+
```
20+
21+
Note that it is not actually required that the same AD is used for forward and reverse.
22+
For example [Nabla.jl](https://github.com/invenia/Nabla.jl/) is a reverse mode AD.
23+
It might declare that it `HasForwardsMode`, and then define a wrapper around [ForwardDiff.jl](https://github.com/JuliaDiff/ForwardDiff.jl) in order to provide that capacity.

docs/src/ad_author/opt_out.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Support opting out of rules
2+
3+
We provide two ways to know that a rule has been opted out of.
4+
5+
### `rrule` / `frule` returns `nothing`
6+
7+
`@opt_out` defines a `frule` or `rrule` matching the signature that returns `nothing`.
8+
9+
If you are in a position to generate code, in response to values returned by function calls then you can do something like:
10+
```@julia
11+
res = rrule(f, xs)
12+
if res === nothing
13+
y, pullback = perform_ad_via_decomposition(r, xs) # do AD without hitting the rrule
14+
else
15+
y, pullback = res
16+
end
17+
```
18+
The Julia compiler will specialize based on inferring the return type of `rrule`, and so can remove that branch.
19+
20+
### `no_rrule` / `no_frule` has a method
21+
22+
`@opt_out` also defines a method for [`ChainRulesCore.no_frule`](@ref) or [`ChainRulesCore.no_rrule`](@ref).
23+
The body of this method doesn't matter, what matters is that it is a method-table.
24+
A simple thing you can do with this is not support opting out.
25+
To do this, filter all methods from the `rrule`/`frule` method table that also occur in the `no_frule`/`no_rrule` table.
26+
This will thus avoid ever hitting an `rrule`/`frule` that returns `nothing` (and thus prevents your library from erroring).
27+
This is easily done, though it does mean ignoring the user's stated desire to opt out of the rule.
28+
29+
More complex you can use this to generate code that triggers your AD.
30+
If for a given signature there is a more specific method in the `no_rrule`/`no_frule` method-table, than the one that would be hit from the `rrule`/`frule` table
31+
(Excluding the one that exactly matches which will return `nothing`) then you know that the rule should not be used.
32+
You can, likely by looking at the primal method table, workout which method you would have it if the rule had not been defined,
33+
and then `invoke` it.
File renamed without changes.

0 commit comments

Comments
 (0)