Skip to content

Commit 460a559

Browse files
Add warning about rules with config always taking precedence (#403)
* Add warning about rules with config always taking precedence * Update docs/src/config.md Co-authored-by: willtebbutt <[email protected]> * Update docs/src/config.md Co-authored-by: willtebbutt <[email protected]>
1 parent 8272f85 commit 460a559

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

docs/src/config.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ rrule(::RuleConfig{>:Feature1}, f, args...) = # rrule that should only be define
1616
frule(::RuleConfig{>:Union{Feature1,Feature2}}, f, args...) = # frule that should only be define for ADs with both `Feature1` and `Feature2`
1717
```
1818

19+
!!! warning "Rules with Config always take precedence over rules without"
20+
Even if the other arguments are more specific the rule with the config will always take precedence.
21+
For example of there is a rule `rrule(::RuleConfig, ::typeof(foo), ::Any)` and other `rrule(foo, ::Float64)`,
22+
the first will always be selected.
23+
This is because the AD will always attempt to provide its config when checking for a rule, and only if that doesn't match, will the config-less rule be tried.
24+
In practice this doesn't happen often, but when it does the solution is a little ugly -- though very similar to resolving method ambiguities.
25+
You need to manually add methods that dispatch from a rule with config to the one without.
26+
See for example the [rule for `sum(abs2, xs)` in ChainRules.jl](https://github.com/JuliaDiff/ChainRules.jl/blob/4ad975826ea0639ad709aeb36cc5051b6bf82eaa/src/rulesets/Base/mapreduce.jl#L87-L113).
27+
28+
29+
1930
A prominent use of this is in declaring that the AD system can, or cannot support being called from within the rule definitions.
2031

2132
## Declaring support for calling back into ADs

0 commit comments

Comments
 (0)