-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdense.jl
349 lines (236 loc) · 10.8 KB
/
dense.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"""
AutoChainRules{RC}
Struct used to select an automatic differentiation backend based on [ChainRulesCore.jl](https://github.com/JuliaDiff/ChainRulesCore.jl) (see the list [here](https://juliadiff.org/ChainRulesCore.jl/stable/index.html#ChainRules-roll-out-status)).
Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
# Constructors
AutoChainRules(; ruleconfig)
# Fields
- `ruleconfig::RC`: a [`ChainRulesCore.RuleConfig`](https://juliadiff.org/ChainRulesCore.jl/stable/rule_author/superpowers/ruleconfig.html) object.
"""
Base.@kwdef struct AutoChainRules{RC} <: AbstractADType
ruleconfig::RC
end
mode(::AutoChainRules) = ForwardOrReverseMode() # specialized in the extension
function Base.show(io::IO, backend::AutoChainRules)
print(io, AutoChainRules, "(ruleconfig=", repr(backend.ruleconfig; context = io), ")")
end
"""
AutoDiffractor
Struct used to select the [Diffractor.jl](https://github.com/JuliaDiff/Diffractor.jl) backend for automatic differentiation.
Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
# Constructors
AutoDiffractor()
"""
struct AutoDiffractor <: AbstractADType end
mode(::AutoDiffractor) = ForwardOrReverseMode()
"""
AutoEnzyme{M,constant_function}
Struct used to select the [Enzyme.jl](https://github.com/EnzymeAD/Enzyme.jl) backend for automatic differentiation.
Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
# Constructors
AutoEnzyme(; mode=nothing, constant_function::Bool=false)
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.
For simple functions, this should usually be set to `false`, but in the case of closures or callable structs which contain differentiated data, it should be set to `true`.
# Fields
- `mode::M`: can be either
+ an object subtyping `EnzymeCore.Mode` (like `EnzymeCore.Forward` or `EnzymeCore.Reverse`) if a specific mode is required
+ `nothing` to choose the best mode automatically
"""
struct AutoEnzyme{M, constant_function} <: AbstractADType
mode::M
end
function AutoEnzyme(mode::M; constant_function::Bool = false) where {M}
return AutoEnzyme{M, constant_function}(mode)
end
function AutoEnzyme(; mode::M = nothing, constant_function::Bool = false) where {M}
return AutoEnzyme{M, constant_function}(mode)
end
mode(::AutoEnzyme) = ForwardOrReverseMode() # specialized in the extension
function Base.show(io::IO, backend::AutoEnzyme)
print(io, AutoEnzyme, "(")
!isnothing(backend.mode) && print(io, "mode=", repr(backend.mode; context = io))
print(io, ")")
end
"""
AutoFastDifferentiation
Struct used to select the [FastDifferentiation.jl](https://github.com/brianguenter/FastDifferentiation.jl) backend for automatic differentiation.
Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
# Constructors
AutoFastDifferentiation()
"""
struct AutoFastDifferentiation <: AbstractADType end
mode(::AutoFastDifferentiation) = SymbolicMode()
"""
AutoFiniteDiff{T1,T2,T3}
Struct used to select the [FiniteDiff.jl](https://github.com/JuliaDiff/FiniteDiff.jl) backend for automatic differentiation.
Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
# Constructors
AutoFiniteDiff(; fdtype=Val(:forward), fdjtype=fdtype, fdhtype=Val(:hcentral))
# Fields
- `fdtype::T1`: finite difference type
- `fdjtype::T2`: finite difference type for the Jacobian
- `fdhtype::T3`: finite difference type for the Hessian
"""
Base.@kwdef struct AutoFiniteDiff{T1, T2, T3} <: AbstractADType
fdtype::T1 = Val(:forward)
fdjtype::T2 = fdtype
fdhtype::T3 = Val(:hcentral)
end
mode(::AutoFiniteDiff) = ForwardMode()
function Base.show(io::IO, backend::AutoFiniteDiff)
print(io, AutoFiniteDiff, "(")
backend.fdtype != Val(:forward) &&
print(io, "fdtype=", repr(backend.fdtype; context = io), ", ")
backend.fdjtype != backend.fdtype &&
print(io, "fdjtype=", repr(backend.fdjtype; context = io), ", ")
backend.fdhtype != Val(:hcentral) &&
print(io, "fdhtype=", repr(backend.fdhtype; context = io))
print(io, ")")
end
"""
AutoFiniteDifferences{T}
Struct used to select the [FiniteDifferences.jl](https://github.com/JuliaDiff/FiniteDifferences.jl) backend for automatic differentiation.
Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
# Constructors
AutoFiniteDifferences(; fdm)
# Fields
- `fdm::T`: a [`FiniteDifferenceMethod`](https://juliadiff.org/FiniteDifferences.jl/stable/pages/api/#FiniteDifferences.FiniteDifferenceMethod)
"""
Base.@kwdef struct AutoFiniteDifferences{T} <: AbstractADType
fdm::T
end
mode(::AutoFiniteDifferences) = ForwardMode()
function Base.show(io::IO, backend::AutoFiniteDifferences)
print(io, AutoFiniteDifferences, "(fdm=", repr(backend.fdm; context = io), ")")
end
"""
AutoForwardDiff{chunksize,T}
Struct used to select the [ForwardDiff.jl](https://github.com/JuliaDiff/ForwardDiff.jl) backend for automatic differentiation.
Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
# Constructors
AutoForwardDiff(; chunksize=nothing, tag=nothing)
# Type parameters
- `chunksize`: the preferred [chunk size](https://juliadiff.org/ForwardDiff.jl/stable/user/advanced/#Configuring-Chunk-Size) to evaluate several derivatives at once
# Fields
- `tag::T`: a [custom tag](https://juliadiff.org/ForwardDiff.jl/release-0.10/user/advanced.html#Custom-tags-and-tag-checking-1) to handle nested differentiation calls (usually not necessary)
"""
struct AutoForwardDiff{chunksize, T} <: AbstractADType
tag::T
end
function AutoForwardDiff(; chunksize = nothing, tag = nothing)
AutoForwardDiff{chunksize, typeof(tag)}(tag)
end
mode(::AutoForwardDiff) = ForwardMode()
function Base.show(io::IO, backend::AutoForwardDiff{chunksize}) where {chunksize}
print(io, AutoForwardDiff, "(")
chunksize !== nothing && print(io, "chunksize=", repr(chunksize; context = io),
(backend.tag !== nothing ? ", " : ""))
backend.tag !== nothing && print(io, "tag=", repr(backend.tag; context = io))
print(io, ")")
end
"""
AutoPolyesterForwardDiff{chunksize,T}
Struct used to select the [PolyesterForwardDiff.jl](https://github.com/JuliaDiff/PolyesterForwardDiff.jl) backend for automatic differentiation.
Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
# Constructors
AutoPolyesterForwardDiff(; chunksize=nothing, tag=nothing)
# Type parameters
- `chunksize`: the preferred [chunk size](https://juliadiff.org/ForwardDiff.jl/stable/user/advanced/#Configuring-Chunk-Size) to evaluate several derivatives at once
# Fields
- `tag::T`: a [custom tag](https://juliadiff.org/ForwardDiff.jl/release-0.10/user/advanced.html#Custom-tags-and-tag-checking-1) to handle nested differentiation calls (usually not necessary)
"""
struct AutoPolyesterForwardDiff{chunksize, T} <: AbstractADType
tag::T
end
function AutoPolyesterForwardDiff(; chunksize = nothing, tag = nothing)
AutoPolyesterForwardDiff{chunksize, typeof(tag)}(tag)
end
mode(::AutoPolyesterForwardDiff) = ForwardMode()
function Base.show(io::IO, backend::AutoPolyesterForwardDiff{chunksize}) where {chunksize}
print(io, AutoPolyesterForwardDiff, "(")
chunksize !== nothing && print(io, "chunksize=", repr(chunksize; context = io),
(backend.tag !== nothing ? ", " : ""))
backend.tag !== nothing && print(io, "tag=", repr(backend.tag; context = io))
print(io, ")")
end
"""
AutoReverseDiff{compile}
Struct used to select the [ReverseDiff.jl](https://github.com/JuliaDiff/ReverseDiff.jl) backend for automatic differentiation.
Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
# Constructors
AutoReverseDiff(; compile::Union{Val, Bool} = Val(false))
# Fields
- `compile::Union{Val, Bool}`: whether to [compile the tape](https://juliadiff.org/ReverseDiff.jl/api/#ReverseDiff.compile) prior to differentiation (the boolean version is also the type parameter)
"""
struct AutoReverseDiff{C} <: AbstractADType
compile::Bool # this field is left for legacy reasons
function AutoReverseDiff(; compile::Union{Val, Bool} = Val(false))
_compile = _unwrap_val(compile)
return new{_compile}(_compile)
end
end
function Base.getproperty(ad::AutoReverseDiff, s::Symbol)
if s === :compile
Base.depwarn(
"`ad.compile` where `ad` is `AutoReverseDiff` has been deprecated and will be removed in v2. Instead it is available as a compile-time constant as `AutoReverseDiff{true}` or `AutoReverseDiff{false}`.",
:getproperty)
end
return getfield(ad, s)
end
mode(::AutoReverseDiff) = ReverseMode()
function Base.show(io::IO, ::AutoReverseDiff{compile}) where {compile}
print(io, AutoReverseDiff, "(")
compile && print(io, "compile=true")
print(io, ")")
end
"""
AutoSymbolics
Struct used to select the [Symbolics.jl](https://github.com/JuliaSymbolics/Symbolics.jl) backend for automatic differentiation.
Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
# Constructors
AutoSymbolics()
"""
struct AutoSymbolics <: AbstractADType end
mode(::AutoSymbolics) = SymbolicMode()
"""
AutoTapir
Struct used to select the [Tapir.jl](https://github.com/withbayes/Tapir.jl) backend for automatic differentiation.
Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
# Constructors
AutoTapir(; safe_mode=true)
# Fields
- `safe_mode::Bool`: whether to run additional checks to catch errors early. While this is
on by default to ensure that users are aware of this option, you should generally turn
it off for actual use, as it has substantial performance implications.
If you encounter a problem with using Tapir (it fails to differentiate a function, or
something truly nasty like a segfault occurs), then you should try switching `safe_mode`
on and look at what happens. Often errors are caught earlier and the error messages are
more useful.
"""
Base.@kwdef struct AutoTapir <: AbstractADType
safe_mode::Bool = true
end
mode(::AutoTapir) = ReverseMode()
function Base.show(io::IO, backend::AutoTapir)
print(io, AutoTapir, "(")
!(backend.safe_mode) && print(io, "safe_mode=false")
print(io, ")")
end
"""
AutoTracker
Struct used to select the [Tracker.jl](https://github.com/FluxML/Tracker.jl) backend for automatic differentiation.
Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
# Constructors
AutoTracker()
"""
struct AutoTracker <: AbstractADType end
mode(::AutoTracker) = ReverseMode()
"""
AutoZygote
Struct used to select the [Zygote.jl](https://github.com/FluxML/Zygote.jl) backend for automatic differentiation.
Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
# Constructors
AutoZygote()
"""
struct AutoZygote <: AbstractADType end
mode(::AutoZygote) = ReverseMode()