-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathprep.jl
More file actions
229 lines (196 loc) · 5.72 KB
/
prep.jl
File metadata and controls
229 lines (196 loc) · 5.72 KB
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
"""
Prep
Abstract supertype for all preparation results (outputs of `prepare_operator` functions).
!!! warning
The public API does not make any guarantees about the type parameters or field layout of `Prep`, the only guarantee is that this type exists.
"""
abstract type Prep{SIG} end
"""
$(docstring_preptype("PushforwardPrep", "pushforward"))
"""
abstract type PushforwardPrep{SIG} <: Prep{SIG} end
struct NoPushforwardPrep{SIG} <: PushforwardPrep{SIG}
_sig::Val{SIG}
end
"""
$(docstring_preptype("PullbackPrep", "pullback"))
"""
abstract type PullbackPrep{SIG} <: Prep{SIG} end
struct NoPullbackPrep{SIG} <: PullbackPrep{SIG}
_sig::Val{SIG}
end
"""
$(docstring_preptype("DerivativePrep", "derivative"))
"""
abstract type DerivativePrep{SIG} <: Prep{SIG} end
struct NoDerivativePrep{SIG} <: DerivativePrep{SIG}
_sig::Val{SIG}
end
"""
$(docstring_preptype("GradientPrep", "gradient"))
"""
abstract type GradientPrep{SIG} <: Prep{SIG} end
struct NoGradientPrep{SIG} <: GradientPrep{SIG}
_sig::Val{SIG}
end
"""
$(docstring_preptype("JacobianPrep", "jacobian"))
"""
abstract type JacobianPrep{SIG} <: Prep{SIG} end
struct NoJacobianPrep{SIG} <: JacobianPrep{SIG}
_sig::Val{SIG}
end
# assume the existence of a `sparsity` field
abstract type SparseJacobianPrep{SIG} <: JacobianPrep{SIG} end
"""
$(docstring_preptype("HVPPrep", "hvp"))
"""
abstract type HVPPrep{SIG} <: Prep{SIG} end
struct NoHVPPrep{SIG} <: HVPPrep{SIG}
_sig::Val{SIG}
end
"""
$(docstring_preptype("HessianPrep", "hessian"))
"""
abstract type HessianPrep{SIG} <: Prep{SIG} end
struct NoHessianPrep{SIG} <: HessianPrep{SIG}
_sig::Val{SIG}
end
# assume the existence of a `sparsity` field
abstract type SparseHessianPrep{SIG} <: HessianPrep{SIG} end
"""
$(docstring_preptype("SecondDerivativePrep", "second_derivative"))
"""
abstract type SecondDerivativePrep{SIG} <: Prep{SIG} end
struct NoSecondDerivativePrep{SIG} <: SecondDerivativePrep{SIG}
_sig::Val{SIG}
end
## Checks
is_strict(::Prep{Nothing}) = Val(false)
is_strict(::Prep) = Val(true)
struct PreparationMismatchError{SIG, EXEC_SIG} <: Exception
format::Vector{Symbol}
end
function PreparationMismatchError(
::Type{SIG}, ::Type{EXEC_SIG}; format
) where {SIG, EXEC_SIG}
return PreparationMismatchError{SIG, EXEC_SIG}(format)
end
function Base.showerror(
io::IO, e::PreparationMismatchError{SIG, EXEC_SIG}
) where {SIG <: Tuple, EXEC_SIG <: Tuple}
println(
io,
"PreparationMismatchError (inconsistent types between preparation and execution):",
)
for (s, pt, et) in zip(e.format, SIG.types, EXEC_SIG.types)
if pt == et
println(io, " - $s: ✅")
else
println(io, " - $s: ❌\n - prep: $pt\n - exec: $et")
end
end
println(
io,
"If you are confident that this check is superfluous, you can disable it by running preparation with the keyword argument `strict=Val(false)` inside DifferentiationInterface.",
)
return nothing
end
function signature(
f, backend::AbstractADType, x, contexts::Vararg{Context, C}; strict::Val{S}
) where {C, S}
if S
return Val(typeof((f, backend, x, contexts)))
else
return Val(Nothing)
end
end
function signature(
f!, y, backend::AbstractADType, x, contexts::Vararg{Context, C}; strict::Val{S}
) where {C, S}
if S
return Val(typeof((f!, y, backend, x, contexts)))
else
return Val(Nothing)
end
end
function signature(
f, backend::AbstractADType, x, t::NTuple, contexts::Vararg{Context, C}; strict::Val{S}
) where {C, S}
if S
return Val(typeof((f, backend, x, t, contexts)))
else
return Val(Nothing)
end
end
function signature(
f!,
y,
backend::AbstractADType,
x,
t::NTuple,
contexts::Vararg{Context, C};
strict::Val{S},
) where {C, S}
if S
return Val(typeof((f!, y, backend, x, t, contexts)))
else
return Val(Nothing)
end
end
function check_prep(
f, ::Prep{SIG}, backend::AbstractADType, x, contexts::Vararg{Context, C}
) where {SIG, C}
return if SIG !== Nothing
EXEC_SIG = typeof((f, backend, x, contexts))
if SIG != EXEC_SIG
throw(
PreparationMismatchError(
SIG, EXEC_SIG; format = [:f, :backend, :x, :contexts]
),
)
end
end
end
function check_prep(
f!, y, ::Prep{SIG}, backend::AbstractADType, x, contexts::Vararg{Context, C}
) where {SIG, C}
return if SIG !== Nothing
EXEC_SIG = typeof((f!, y, backend, x, contexts))
if SIG != EXEC_SIG
throw(
PreparationMismatchError(
SIG, EXEC_SIG; format = [:f!, :y, :backend, :x, :contexts]
),
)
end
end
end
function check_prep(
f, ::Prep{SIG}, backend::AbstractADType, x, t::NTuple, contexts::Vararg{Context, C}
) where {SIG, C}
return if SIG !== Nothing
EXEC_SIG = typeof((f, backend, x, t, contexts))
if SIG != EXEC_SIG
throw(
PreparationMismatchError(
SIG, EXEC_SIG; format = [:f, :backend, :x, :t, :contexts]
),
)
end
end
end
function check_prep(
f!, y, ::Prep{SIG}, backend::AbstractADType, x, t::NTuple, contexts::Vararg{Context, C}
) where {SIG, C}
return if SIG !== Nothing
EXEC_SIG = typeof((f!, y, backend, x, t, contexts))
if SIG != EXEC_SIG
throw(
PreparationMismatchError(
SIG, EXEC_SIG; format = [:f!, :y, :backend, :x, :t, :contexts]
),
)
end
end
end