-
Notifications
You must be signed in to change notification settings - Fork 33
/
BijectorsReverseDiffExt.jl
270 lines (247 loc) · 8.72 KB
/
BijectorsReverseDiffExt.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
module BijectorsReverseDiffExt
if isdefined(Base, :get_extension)
using ReverseDiff:
ReverseDiff,
@grad,
value,
track,
TrackedReal,
TrackedVector,
TrackedMatrix,
@grad_from_chainrules
using Bijectors:
ChainRulesCore,
Elementwise,
SimplexBijector,
maphcat,
simplex_link_jacobian,
simplex_invlink_jacobian,
simplex_logabsdetjac_gradient,
Inverse
import Bijectors:
_eps,
logabsdetjac,
_logabsdetjac_scale,
_simplex_bijector,
_simplex_inv_bijector,
replace_diag,
jacobian,
_inv_link_chol_lkj,
_link_chol_lkj,
_transform_ordered,
_transform_inverse_ordered,
find_alpha,
pd_from_lower,
lower_triangular,
upper_triangular
using Bijectors.LinearAlgebra
using Bijectors.Compat: eachcol
using Bijectors.Distributions: LocationScale
else
using ..ReverseDiff:
ReverseDiff,
@grad,
value,
track,
TrackedReal,
TrackedVector,
TrackedMatrix,
@grad_from_chainrules
using ..Bijectors:
ChainRulesCore,
Elementwise,
SimplexBijector,
maphcat,
simplex_link_jacobian,
simplex_invlink_jacobian,
simplex_logabsdetjac_gradient,
Inverse
import ..Bijectors:
_eps,
logabsdetjac,
_logabsdetjac_scale,
_simplex_bijector,
_simplex_inv_bijector,
replace_diag,
jacobian,
_inv_link_chol_lkj,
_link_chol_lkj,
_transform_ordered,
_transform_inverse_ordered,
find_alpha,
pd_from_lower,
lower_triangular,
upper_triangular
using ..Bijectors.LinearAlgebra
using ..Bijectors.Compat: eachcol
using ..Bijectors.Distributions: LocationScale
end
_eps(::Type{<:TrackedReal{T}}) where {T} = _eps(T)
function Base.minimum(d::LocationScale{<:TrackedReal})
m = minimum(d.ρ)
if isfinite(m)
return d.μ + d.σ * m
else
return m
end
end
function Base.maximum(d::LocationScale{<:TrackedReal})
m = maximum(d.ρ)
if isfinite(m)
return d.μ + d.σ * m
else
return m
end
end
function logabsdetjac(b::Elementwise{typeof(log)}, x::Union{TrackedVector,TrackedMatrix})
return track(logabsdetjac, b, x)
end
@grad function logabsdetjac(b::Elementwise{typeof(log)}, x::AbstractVector)
return -sum(log, value(x)), Δ -> (nothing, -Δ ./ value(x))
end
function _logabsdetjac_scale(a::TrackedReal, x::Real, ::Val{0})
return track(_logabsdetjac_scale, a, value(x), Val(0))
end
@grad function _logabsdetjac_scale(a::Real, x::Real, v::Val{0})
return _logabsdetjac_scale(value(a), value(x), Val(0)),
Δ -> (inv(value(a)) .* Δ, nothing, nothing)
end
# Need to treat `AbstractVector` and `AbstractMatrix` separately due to ambiguity errors
function _logabsdetjac_scale(a::TrackedReal, x::AbstractVector, ::Val{0})
return track(_logabsdetjac_scale, a, value(x), Val(0))
end
@grad function _logabsdetjac_scale(a::Real, x::AbstractVector, v::Val{0})
da = value(a)
J = fill(inv.(da), length(x))
return _logabsdetjac_scale(da, value(x), Val(0)),
Δ -> (transpose(J) * Δ, nothing, nothing)
end
function _logabsdetjac_scale(a::TrackedReal, x::AbstractMatrix, ::Val{0})
return track(_logabsdetjac_scale, a, value(x), Val(0))
end
@grad function _logabsdetjac_scale(a::Real, x::AbstractMatrix, v::Val{0})
da = value(a)
J = fill(size(x, 1) / da, size(x, 2))
return _logabsdetjac_scale(da, value(x), Val(0)),
Δ -> (transpose(J) * Δ, nothing, nothing)
end
# adjoints for 1-dim and 2-dim `Scale` using `AbstractVector`
function _logabsdetjac_scale(a::TrackedVector, x::AbstractVector, ::Val{1})
return track(_logabsdetjac_scale, a, value(x), Val(1))
end
@grad function _logabsdetjac_scale(a::TrackedVector, x::AbstractVector, v::Val{1})
# ∂ᵢ (∑ⱼ log|aⱼ|) = ∑ⱼ δᵢⱼ ∂ᵢ log|aⱼ|
# = ∂ᵢ log |aᵢ|
# = (1 / aᵢ) ∂ᵢ aᵢ
# = (1 / aᵢ)
da = value(a)
J = inv.(da)
return _logabsdetjac_scale(da, value(x), Val(1)), Δ -> (J .* Δ, nothing, nothing)
end
function _logabsdetjac_scale(a::TrackedVector, x::AbstractMatrix, ::Val{1})
return track(_logabsdetjac_scale, a, value(x), Val(1))
end
@grad function _logabsdetjac_scale(a::TrackedVector, x::AbstractMatrix, v::Val{1})
da = value(a)
Jᵀ = repeat(inv.(da), 1, size(x, 2))
return _logabsdetjac_scale(da, value(x), Val(1)), Δ -> (Jᵀ * Δ, nothing, nothing)
end
function _simplex_bijector(X::Union{TrackedVector,TrackedMatrix}, b::SimplexBijector)
return track(_simplex_bijector, X, b)
end
@grad function _simplex_bijector(Y::AbstractVector, b::SimplexBijector)
Yd = value(Y)
return _simplex_bijector(Yd, b), Δ -> (simplex_link_jacobian(Yd)' * Δ, nothing)
end
function _simplex_inv_bijector(X::Union{TrackedVector,TrackedMatrix}, b::SimplexBijector)
return track(_simplex_inv_bijector, X, b)
end
@grad function _simplex_inv_bijector(Y::AbstractVector, b::SimplexBijector)
Yd = value(Y)
return _simplex_inv_bijector(Yd, b), Δ -> (simplex_invlink_jacobian(Yd)' * Δ, nothing)
end
@grad function _simplex_inv_bijector(Y::AbstractMatrix, b::SimplexBijector)
Yd = value(Y)
return _simplex_inv_bijector(Yd, b),
Δ -> begin
maphcat(eachcol(Yd), eachcol(Δ)) do c1, c2
simplex_invlink_jacobian(c1)' * c2
end,
nothing
end
end
replace_diag(::typeof(log), X::TrackedMatrix) = track(replace_diag, log, X)
@grad function replace_diag(::typeof(log), X)
Xd = value(X)
f(i, j) = i == j ? log(Xd[i, j]) : Xd[i, j]
out = f.(1:size(Xd, 1), (1:size(Xd, 2))')
out, ∇ -> begin
g(i, j) = i == j ? ∇[i, j] / Xd[i, j] : ∇[i, j]
return (nothing, g.(1:size(Xd, 1), (1:size(Xd, 2))'))
end
end
replace_diag(::typeof(exp), X::TrackedMatrix) = track(replace_diag, exp, X)
@grad function replace_diag(::typeof(exp), X)
Xd = value(X)
f(i, j) = ifelse(i == j, exp(Xd[i, j]), Xd[i, j])
out = f.(1:size(Xd, 1), (1:size(Xd, 2))')
out, ∇ -> begin
g(i, j) = ifelse(i == j, ∇[i, j] * exp(Xd[i, j]), ∇[i, j])
return (nothing, g.(1:size(Xd, 1), (1:size(Xd, 2))'))
end
end
function logabsdetjac(b::SimplexBijector, x::Union{TrackedVector,TrackedMatrix})
return track(logabsdetjac, b, x)
end
@grad function logabsdetjac(b::SimplexBijector, x::AbstractVector)
xd = value(x)
return logabsdetjac(b, xd), Δ -> begin
(nothing, simplex_logabsdetjac_gradient(xd) * Δ)
end
end
pd_from_lower(X::TrackedMatrix) = track(pd_from_lower, X)
@grad function pd_from_lower(X::AbstractMatrix)
Xd = value(X)
return LowerTriangular(Xd) * LowerTriangular(Xd)',
Δ -> begin
Xl = LowerTriangular(Xd)
return (LowerTriangular(Δ' * Xl + Δ * Xl),)
end
end
@grad_from_chainrules pd_from_upper(X::TrackedMatrix)
lower_triangular(A::TrackedMatrix) = track(lower_triangular, A)
@grad function lower_triangular(A::AbstractMatrix)
Ad = value(A)
return lower_triangular(Ad), Δ -> (lower_triangular(Δ),)
end
upper_triangular(A::TrackedMatrix) = track(upper_triangular, A)
@grad function upper_triangular(A::AbstractMatrix)
Ad = value(A)
return upper_triangular(Ad), Δ -> (upper_triangular(Δ),)
end
function find_alpha(wt_y::T, wt_u_hat::T, b::T) where {T<:TrackedReal}
return track(find_alpha, wt_y, wt_u_hat, b)
end
@grad function find_alpha(wt_y::TrackedReal, wt_u_hat::TrackedReal, b::TrackedReal)
α = find_alpha(value(wt_y), value(wt_u_hat), value(b))
∂wt_y = inv(1 + wt_u_hat * sech(α + b)^2)
∂wt_u_hat = -tanh(α + b) * ∂wt_y
∂b = ∂wt_y - 1
find_alpha_pullback(Δ::Real) = (Δ * ∂wt_y, Δ * ∂wt_u_hat, Δ * ∂b)
return α, find_alpha_pullback
end
# `OrderedBijector`
@grad_from_chainrules _transform_ordered(y::Union{TrackedVector,TrackedMatrix})
@grad_from_chainrules _transform_inverse_ordered(x::Union{TrackedVector,TrackedMatrix})
@grad_from_chainrules update_triu_from_vec(vals::TrackedVector{<:Real}, k::Int, dim::Int)
@grad_from_chainrules _link_chol_lkj(x::TrackedMatrix)
@grad_from_chainrules _inv_link_chol_lkj(x::TrackedVector)
if VERSION <= v"1.8.0-DEV.1526"
# HACK: This dispatch does not wrap X in Hermitian before calling cholesky.
# cholesky does not work with AbstractMatrix in julia versions before the compared one,
# and it would error with Hermitian{ReverseDiff.TrackedArray}.
# See commit when the fix was introduced :
# https://github.com/JuliaLang/julia/commit/635449dabee81bba315ab066627a98f856141969
cholesky_factor(X::ReverseDiff.TrackedArray) = cholesky_factor(cholesky(X))
end
end