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
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 `true`, which leads to increased performance.
54
-
However, in the case of closures or callable structs which contain differentiated data, `constant_function` should be set to `false` to ensure correctness (more details below).
50
+
AutoEnzyme(; mode=nothing)
55
51
56
52
# Fields
57
53
58
54
- `mode::M`: can be either
59
55
60
56
+ an object subtyping `EnzymeCore.Mode` (like `EnzymeCore.Forward` or `EnzymeCore.Reverse`) if a specific mode is required
61
57
+ `nothing` to choose the best mode automatically
62
-
63
-
# Notes
64
-
65
-
We now give several examples of functions.
66
-
For each one, we explain how `constant_function` should be set in order to compute the correct derivative with respect to the input `x`.
67
-
68
-
```julia
69
-
function f1(x)
70
-
return x[1]
71
-
end
72
-
```
73
-
74
-
The function `f1` is not a closure, it does not contain any data.
75
-
Thus `f1` can be differentiated with `AutoEnzyme(constant_function=true)` (although here setting `constant_function=false` would change neither correctness nor performance).
76
-
77
-
```julia
78
-
parameter = [0.0]
79
-
function f2(x)
80
-
return parameter[1] + x[1]
81
-
end
82
-
```
83
-
84
-
The function `f2` is a closure over `parameter`, but `parameter` is never modified based on the input `x`.
85
-
Thus, `f2` can be differentiated with `AutoEnzyme(constant_function=true)` (setting `constant_function=false` would not change correctness but would hinder performance).
86
-
87
-
```julia
88
-
cache = [0.0]
89
-
function f3(x)
90
-
cache[1] = x[1]
91
-
return cache[1] + x[1]
92
-
end
93
-
```
94
-
95
-
The function `f3` is a closure over `cache`, and `cache` is modified based on the input `x`.
96
-
That means `cache` cannot be treated as constant, since derivative values must be propagated through it.
97
-
Thus `f3` must be differentiated with `AutoEnzyme(constant_function=false)` (setting `constant_function=true` would make the result incorrect).
0 commit comments