-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathderivative.jl
More file actions
193 lines (161 loc) · 4.79 KB
/
derivative.jl
File metadata and controls
193 lines (161 loc) · 4.79 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
## Docstrings
"""
prepare_derivative(f, backend, x, [contexts...]) -> prep
prepare_derivative(f!, y, backend, x, [contexts...]) -> prep
$(docstring_prepare("derivative"; inplace=true))
"""
function prepare_derivative end
"""
prepare!_derivative(f, prep, backend, x, [contexts...]) -> new_prep
prepare!_derivative(f!, y, prep, backend, x, [contexts...]) -> new_prep
$(docstring_prepare!("derivative"))
"""
function prepare!_derivative end
"""
value_and_derivative(f, [prep,] backend, x, [contexts...]) -> (y, der)
value_and_derivative(f!, y, [prep,] backend, x, [contexts...]) -> (y, der)
Compute the value and the derivative of the function `f` at point `x`.
$(docstring_preparation_hint("derivative"))
"""
function value_and_derivative end
"""
value_and_derivative!(f, der, [prep,] backend, x, [contexts...]) -> (y, der)
value_and_derivative!(f!, y, der, [prep,] backend, x, [contexts...]) -> (y, der)
Compute the value and the derivative of the function `f` at point `x`, overwriting `der`.
$(docstring_preparation_hint("derivative"))
"""
function value_and_derivative! end
"""
derivative(f, [prep,] backend, x, [contexts...]) -> der
derivative(f!, y, [prep,] backend, x, [contexts...]) -> der
Compute the derivative of the function `f` at point `x`.
$(docstring_preparation_hint("derivative"))
"""
function derivative end
"""
derivative!(f, der, [prep,] backend, x, [contexts...]) -> der
derivative!(f!, y, der, [prep,] backend, x, [contexts...]) -> der
Compute the derivative of the function `f` at point `x`, overwriting `der`.
$(docstring_preparation_hint("derivative"))
"""
function derivative! end
## Preparation
struct PushforwardDerivativePrep{E<:PushforwardPrep} <: DerivativePrep
pushforward_prep::E
end
function prepare_derivative(
f::F, backend::AbstractADType, x, contexts::Vararg{Context,C}
) where {F,C}
pushforward_prep = prepare_pushforward(f, backend, x, (one(x),), contexts...)
return PushforwardDerivativePrep(pushforward_prep)
end
function prepare_derivative(
f!::F, y, backend::AbstractADType, x, contexts::Vararg{Context,C}
) where {F,C}
pushforward_prep = prepare_pushforward(f!, y, backend, x, (one(x),), contexts...)
return PushforwardDerivativePrep(pushforward_prep)
end
## One argument
function value_and_derivative(
f::F,
prep::PushforwardDerivativePrep,
backend::AbstractADType,
x,
contexts::Vararg{Context,C},
) where {F,C}
y, ty = value_and_pushforward(
f, prep.pushforward_prep, backend, x, (one(x),), contexts...
)
return y, only(ty)
end
function value_and_derivative!(
f::F,
der,
prep::PushforwardDerivativePrep,
backend::AbstractADType,
x,
contexts::Vararg{Context,C},
) where {F,C}
y, _ = value_and_pushforward!(
f, (der,), prep.pushforward_prep, backend, x, (one(x),), contexts...
)
return y, der
end
function derivative(
f::F,
prep::PushforwardDerivativePrep,
backend::AbstractADType,
x,
contexts::Vararg{Context,C},
) where {F,C}
ty = pushforward(f, prep.pushforward_prep, backend, x, (one(x),), contexts...)
return only(ty)
end
function derivative!(
f::F,
der,
prep::PushforwardDerivativePrep,
backend::AbstractADType,
x,
contexts::Vararg{Context,C},
) where {F,C}
pushforward!(f, (der,), prep.pushforward_prep, backend, x, (one(x),), contexts...)
return der
end
## Two arguments
function value_and_derivative(
f!::F,
y,
prep::PushforwardDerivativePrep,
backend::AbstractADType,
x,
contexts::Vararg{Context,C},
) where {F,C}
y, ty = value_and_pushforward(
f!, y, prep.pushforward_prep, backend, x, (one(x),), contexts...
)
return y, only(ty)
end
function value_and_derivative!(
f!::F,
y,
der,
prep::PushforwardDerivativePrep,
backend::AbstractADType,
x,
contexts::Vararg{Context,C},
) where {F,C}
y, _ = value_and_pushforward!(
f!, y, (der,), prep.pushforward_prep, backend, x, (one(x),), contexts...
)
return y, der
end
function derivative(
f!::F,
y,
prep::PushforwardDerivativePrep,
backend::AbstractADType,
x,
contexts::Vararg{Context,C},
) where {F,C}
ty = pushforward(f!, y, prep.pushforward_prep, backend, x, (one(x),), contexts...)
return only(ty)
end
function derivative!(
f!::F,
y,
der,
prep::PushforwardDerivativePrep,
backend::AbstractADType,
x,
contexts::Vararg{Context,C},
) where {F,C}
pushforward!(f!, y, (der,), prep.pushforward_prep, backend, x, (one(x),), contexts...)
return der
end
## Shuffled
function shuffled_derivative(
x, f::F, backend::AbstractADType, rewrap::Rewrap{C}, unannotated_contexts::Vararg{Any,C}
) where {F,C}
return derivative(f, backend, x, rewrap(unannotated_contexts...)...)
end