Skip to content

Commit 63598b3

Browse files
authored
Merge pull request #118 from JuliaDiff/ox/howthunkdoc
Move some stuff about using thunks to the docs
2 parents 7ee09f3 + 2fbf436 commit 63598b3

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

docs/src/writing_good_rules.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ return @thunk(∂A)
2626
```
2727
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.
2828

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+
2940
## Be careful with using `adjoint` when you mean `transpose`
3041

3142
Remember for complex numbers `a'` (i.e. `adjoint(a)`) takes the complex conjugate.

src/differentials/thunks.jl

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,8 @@ Also if we did `Zero() * res[1]` then the result would be `Zero()` and `f(x)` wo
5757
with a field for each variable used in the expression, and call overloaded.
5858
5959
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)
6862
"""
6963
struct Thunk{F} <: AbstractThunk
7064
f::F

0 commit comments

Comments
 (0)