Skip to content

Commit 091d3b6

Browse files
committed
More details
1 parent 995986a commit 091d3b6

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/dense.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,41 @@ Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
5050
AutoEnzyme(; mode=nothing, constant_function::Bool=false)
5151
5252
The `constant_function` keyword argument (and type parameter) determines whether the function object itself should be considered constant or not during differentiation with Enzyme.jl.
53-
For simple functions, `constant_function` should usually be set to `false`, but in the case of closures or callable structs which contain differentiated data that can be treated as constant, `constant_function` should be set to `true` for increased performance.
53+
For simple functions, `constant_function` should usually be set to `false`, but in the case of closures or callable structs which contain differentiated data that can be treated as constant, `constant_function` should be set to `true` for increased performance (more details below).
5454
5555
# Fields
5656
5757
- `mode::M`: can be either
5858
5959
+ an object subtyping `EnzymeCore.Mode` (like `EnzymeCore.Forward` or `EnzymeCore.Reverse`) if a specific mode is required
6060
+ `nothing` to choose the best mode automatically
61+
62+
# Notes
63+
64+
If `constant_function = true` but the enclosed data is not truly constant, then Enzyme.jl will not compute the correct derivative values.
65+
An example of such a function is:
66+
67+
```julia
68+
cache = [0.0]
69+
function f(x)
70+
cache[1] = x[1]^2
71+
cache[1] + x[1]
72+
end
73+
```
74+
75+
In this case, the enclosed cache is a function of the differentiated input, and thus its values are non-constant with respect to the input.
76+
Thus, in order to compute the correct derivative of the output, the derivative must propagate through the `cache` value, and said `cache` must not be treated as constant.
77+
78+
Conversely, the following function can treat `parameter` as a constant, because `parameter` is never modified based on the input `x`:
79+
80+
```julia
81+
parameter = [0.0]
82+
function f(x)
83+
parameter[1] + x[1]
84+
end
85+
```
86+
87+
In this case, `constant_function = true` would allow the chosen differentiation system to perform extra memory and compute optimizations, under the assumption that `parameter` is kept constant.
6188
"""
6289
struct AutoEnzyme{M, constant_function} <: AbstractADType
6390
mode::M

0 commit comments

Comments
 (0)