Skip to content

Commit e9996d9

Browse files
authored
Merge pull request #75 from JuliaDiff/ox/thunkextradoc
Extra comment on thunks
2 parents ab0386c + ffd2e35 commit e9996d9

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/differentials/thunks.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,25 @@ julia> t()()
4545
### When to `@thunk`?
4646
When writing `rrule`s (and to a lesser exent `frule`s), it is important to `@thunk`
4747
appropriately.
48-
Propagation rule's that return multiple derivatives are not able to do all the computing themselves.
49-
By `@thunk`ing the work required for each, they then compute only what is needed.
48+
Propagation rules that return multiple derivatives may not have all deriviatives used.
49+
By `@thunk`ing the work required for each derivative, they then compute only what is needed.
50+
51+
#### How do thunks prevent work?
52+
If we have `res = pullback(...) = @thunk(f(x)), @thunk(g(x))`
53+
then if we did `dx + res[1]` then only `f(x)` would be evaluated, not `g(x)`.
54+
Also if we did `Zero() * res[1]` then the result would be `Zero()` and `f(x)` would not be evaluated.
5055
5156
#### So why not thunk everything?
5257
`@thunk` creates a closure over the expression, which (effectively) creates a `struct`
5358
with a field for each variable used in the expression, and call overloaded.
5459
5560
Do not use `@thunk` if this would be equal or more work than actually evaluating the expression itself. Examples being:
56-
- The expression wrapping something in a `struct`, such as `Adjoint(x)` or `Diagonal(x)`
5761
- The expression being a constant
62+
- The expression is merely wrapping something in a `struct`, such as `Adjoint(x)` or `Diagonal(x)`
5863
- The expression being itself a `thunk`
5964
- The expression being from another `rrule` or `frule` (it would be `@thunk`ed if required by the defining rule already)
65+
- There is only one derivative being returned, so from the fact that the user called `frule`/`rrule`
66+
they clearly will want to use that one.
6067
"""
6168
struct Thunk{F} <: AbstractThunk
6269
f::F

0 commit comments

Comments
 (0)