Skip to content

Commit a66f5e7

Browse files
committed
improve docs
1 parent a8ffabc commit a66f5e7

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

docs/src/opting_out_of_rules.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Opting out of rules
1+
# [Opting out of rules](@id opt_out)
22

33
It is common to define rules fairly generically.
44
Often matching (or exceeding) how generic the matching original primal method is.
@@ -53,6 +53,12 @@ Similar can be done `@opt_out frule`.
5353
It can also be done passing in a [`RuleConfig`](@ref config).
5454

5555

56+
!!! warning "If the general rule uses a config, the opt-out must also"
57+
Following the same principles as for [rules with config](@ref config), a rule with a `RuleConfig` argument will take precedence over one without, including if that one is a opt-out rule.
58+
But if the general rule does not use a config, then the opt-out rule *can* use a config.
59+
This allows, for example, you to use opt-out to avoid a particular AD system using a opt-out rule that takes that particular AD's config.
60+
61+
5662
## How to support this (for AD implementers)
5763

5864
We provide two ways to know that a rule has been opted out of.

src/rule_definition_tools.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ This will generate an [`rrule`](@ref) that returns `nothing`,
428428
and will also add a similar entry to [`ChainRulesCore.no_rrule`](@ref).
429429
430430
Similar applies for [`frule`](@ref) and [`ChainRulesCore.no_frule`](@ref)
431+
432+
For more information see the [documentation on opting out of rules](@ref opt_out).
431433
"""
432434
macro opt_out(expr)
433435
no_rule_target = _no_rule_target_rewrite!(deepcopy(expr))

src/rules.jl

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,26 @@ end
146146
const NO_RRULE_DOC = """
147147
no_rrule
148148
149-
This is an implementation detail for opting out of [`rrule`](@ref).
149+
This is an piece of infastructure supporting opting out of [`rrule`](@ref).
150150
It follows the signature for `rrule` exactly.
151-
We use it as a way to store a collection of type-tuples in its method-table.
152-
If something has this defined, it means that it must having a must also have a `rrule`,
153-
that returns `nothing`.
151+
A collection of type-tuples is stored in its method-table.
152+
If something has this defined, it means that it must having a must also have a `rrule`,
153+
defined that returns `nothing`.
154+
155+
!!! warning "do not overload no_rrule directly
156+
It is fine and intended to query the method table of `no_rrule`.
157+
It is not safe to add to that directly, as corresponding changes also need to be made to
158+
`rrule`.
159+
The [`@opt_out`](@ref) macro does both these things, and so should almost always be used
160+
rather than defining a method of `no_rrule` directly.
154161
155162
### Mechanics
156-
note: when the text below says methods `==` or `<:` it actually means:
163+
note: when the text below says methods `==` it actually means:
157164
`parameters(m.sig)[2:end]` (i.e. the signature type tuple) rather than the method object `m` itself.
158165
159166
To decide if should opt-out using this mechanism.
160-
- find the most specific method of `rrule`
161-
- find the most specific method of `no_rrule`
162-
- if the method of `no_rrule` `<:` the method of `rrule`, then should opt-out
167+
- find the most specific method of `rrule` and `no_rule` e.g with `Base.which`
168+
- if the method of `no_rrule` `==` the method of `rrule`, then should opt-out
163169
164170
To just ignore the fact that rules can be opted-out from, and that some rules thus return
165171
`nothing`, then filter the list of methods of `rrule` to remove those that are `==` to ones
@@ -171,6 +177,8 @@ rule without config.
171177
On the other-hand if your AD can work with `rrule`s that return `nothing`, then it is
172178
simpler to just use that mechanism for opting out; and you don't need to worry about this
173179
at all.
180+
181+
For more information see the [documentation on opting out of rules](@ref opt_out)
174182
"""
175183

176184
"""

0 commit comments

Comments
 (0)