You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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,
0 commit comments