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
Copy file name to clipboardExpand all lines: docs/src/writing_good_rules.md
+11Lines changed: 11 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -26,6 +26,17 @@ return @thunk(∂A)
26
26
```
27
27
In the bad example `foo(x)` gets computed eagerly, and all that the thunk is doing is wrapping the already calculated result in a function that returns it.
28
28
29
+
Do not use `@thunk` if this would be equal or more work than actually evaluating the expression itself.
30
+
Examples being:
31
+
- The expression being a constant
32
+
- The expression is merely wrapping something in a `struct`, such as `Adjoint(x)` or `Diagonal(x)`
33
+
- The expression being itself a `thunk`
34
+
- The expression being from another `rrule` or `frule`;
35
+
it would be `@thunk`ed if required by the defining rule already.
36
+
- There is only one derivative being returned, so from the fact that the user called
37
+
`frule`/`rrule` they clearly will want to use that one.
38
+
39
+
29
40
## Be careful with using `adjoint` when you mean `transpose`
30
41
31
42
Remember for complex numbers `a'` (i.e. `adjoint(a)`) takes the complex conjugate.
Copy file name to clipboardExpand all lines: src/differentials/thunks.jl
+2-8Lines changed: 2 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -57,14 +57,8 @@ Also if we did `Zero() * res[1]` then the result would be `Zero()` and `f(x)` wo
57
57
with a field for each variable used in the expression, and call overloaded.
58
58
59
59
Do not use `@thunk` if this would be equal or more work than actually evaluating the expression itself.
60
-
Examples being:
61
-
- The expression being a constant
62
-
- The expression is merely wrapping something in a `struct`, such as `Adjoint(x)` or `Diagonal(x)`
63
-
- The expression being itself a `thunk`
64
-
- The expression being from another `rrule` or `frule`;
65
-
it would be `@thunk`ed if required by the defining rule already.
66
-
- There is only one derivative being returned, so from the fact that the user called
67
-
`frule`/`rrule` they clearly will want to use that one.
60
+
61
+
For more details see the manual section [on using thunks effectively](http://www.juliadiff.org/ChainRulesCore.jl/dev/writing_good_rules.html#Use-Thunks-appropriately-1)
0 commit comments