@@ -201,7 +201,6 @@ A thunk is a deferred computation.
201
201
It wraps a zero argument closure that when invoked returns a differential.
202
202
`@thunk(v)` is a macro that expands into `Thunk(()->v)`.
203
203
204
-
205
204
Calling a thunk, calls the wrapped closure.
206
205
`extern`ing thunks applies recursively, it also externs the differial that the closure returns.
207
206
If you do not want that, then simply call the thunk
@@ -222,19 +221,19 @@ julia> t()()
222
221
223
222
### When to `@thunk`?
224
223
When writing `rrule`s (and to a lesser exent `frule`s), it is important to `@thunk`
225
- appropriately. Propagation rule's that return multiple deriviatives are able to not
226
- do all the work of computing all of them only to have just one used .
227
- By `@thunk`ing the work required for each, they can only be computed when needed.
224
+ appropriately.
225
+ Propagation rule's that return multiple derivatives are not able to do all the computing themselves .
226
+ By `@thunk`ing the work required for each, they then compute only what is needed.
228
227
229
228
#### So why not thunk everything?
230
- `@thunk` creates a closure over the expression, which is basically a struct with a
231
- field for each variable used in the expression (closed over) , and call overloaded.
232
- If this would be equal or more work than actually evaluating the expression then don't do
233
- it. An example would be if the expression itself is just wrapping something in a struct.
234
- Such as `Adjoint(x)` or `Diagonal(x)`. Or if the expression is a constant, or is
235
- itself a `Thunk`.
236
- If you got the expression from another `rrule` (or `frule`), you don't need to
237
- `@thunk` it since it will have been thunked if required, by the defining rule.
229
+ `@thunk` creates a closure over the expression, which (effectively) creates a ` struct`
230
+ with a field for each variable used in the expression, and call overloaded.
231
+
232
+ Do not use `@thunk` if this would be equal or more work than actually evaluating the expression itself. Examples being:
233
+ - The expression wrapping something in a `struct`, such as `Adjoint(x)` or `Diagonal(x)`
234
+ - The expression being a constant
235
+ - The expression being itself a `thunk`
236
+ - The expression being from another `rrule` or `frule` ( it would be `@thunk`ed if required by the defining rule already)
238
237
"""
239
238
struct Thunk{F} <: AbstractThunk
240
239
f:: F
0 commit comments