-
Notifications
You must be signed in to change notification settings - Fork 89
/
dense.jl
191 lines (160 loc) · 4.66 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
using LinearAlgebra: AbstractTriangular
# Matrix wrapper types that we know are square and are thus potentially invertible. For
# these we can use simpler definitions for `/` and `\`.
const SquareMatrix{T} = Union{Diagonal{T},AbstractTriangular{T}}
#####
##### `dot`
#####
function frule((_, Δx, Δy), ::typeof(dot), x, y)
return dot(x, y), sum(Δx .* y) + sum(x .* Δy)
end
function rrule(::typeof(dot), x, y)
function dot_pullback(ΔΩ)
return (NO_FIELDS, @thunk(ΔΩ .* y), @thunk(x .* ΔΩ))
end
return dot(x, y), dot_pullback
end
#####
##### `inv`
#####
function frule((_, Δx), ::typeof(inv), x::AbstractArray)
Ω = inv(x)
return Ω, -Ω * Δx * Ω
end
function rrule(::typeof(inv), x::AbstractArray)
Ω = inv(x)
function inv_pullback(ΔΩ)
return NO_FIELDS, -Ω' * ΔΩ * Ω'
end
return Ω, inv_pullback
end
#####
##### `det`
#####
function frule((_, ẋ), ::typeof(det), x::Union{Number, AbstractMatrix})
Ω = det(x)
# TODO Performance optimization: probably there is an efficent
# way to compute this trace without during the full compution within
return Ω, Ω * tr(inv(x) * ẋ)
end
function rrule(::typeof(det), x::Union{Number, AbstractMatrix})
Ω = det(x)
function det_pullback(ΔΩ)
return NO_FIELDS, Ω * ΔΩ * transpose(inv(x))
end
return Ω, det_pullback
end
#####
##### `logdet`
#####
function frule((_, Δx), ::typeof(logdet), x::Union{Number, AbstractMatrix})
Ω = logdet(x)
return Ω, tr(inv(x) * Δx)
end
function rrule(::typeof(logdet), x::Union{Number, AbstractMatrix})
Ω = logdet(x)
function logdet_pullback(ΔΩ)
return (NO_FIELDS, ΔΩ * transpose(inv(x)))
end
return Ω, logdet_pullback
end
#####
##### `trace`
#####
function frule((_, Δx), ::typeof(tr), x)
return tr(x), tr(Δx)
end
function rrule(::typeof(tr), x)
# This should really be a FillArray
# see https://github.com/JuliaDiff/ChainRules.jl/issues/46
function tr_pullback(ΔΩ)
return (NO_FIELDS, @thunk Diagonal(fill(ΔΩ, size(x, 1))))
end
return tr(x), tr_pullback
end
#####
##### `*`
#####
function rrule(::typeof(*), A::AbstractMatrix{<:Real}, B::AbstractMatrix{<:Real})
function times_pullback(Ȳ)
return (NO_FIELDS, @thunk(Ȳ * B'), @thunk(A' * Ȳ))
end
return A * B, times_pullback
end
#####
##### `/`
#####
function rrule(::typeof(/), A::AbstractMatrix{<:Real}, B::T) where T<:SquareMatrix{<:Real}
Y = A / B
function slash_pullback(Ȳ)
S = T.name.wrapper
∂A = @thunk Ȳ / B'
∂B = @thunk S(-Y' * (Ȳ / B'))
return (NO_FIELDS, ∂A, ∂B)
end
return Y, slash_pullback
end
function rrule(::typeof(/), A::AbstractVecOrMat{<:Real}, B::AbstractVecOrMat{<:Real})
Aᵀ, dA_pb = rrule(adjoint, A)
Bᵀ, dB_pb = rrule(adjoint, B)
Cᵀ, dS_pb = rrule(\, Bᵀ, Aᵀ)
C, dC_pb = rrule(adjoint, Cᵀ)
function slash_pullback(Ȳ)
# Optimization note: dAᵀ, dBᵀ, dC are calculated no matter which partial you want
_, dC = dC_pb(Ȳ)
_, dBᵀ, dAᵀ = dS_pb(unthunk(dC))
∂A = last(dA_pb(unthunk(dAᵀ)))
∂B = last(dA_pb(unthunk(dBᵀ)))
(NO_FIELDS, ∂A, ∂B)
end
return C, slash_pullback
end
#####
##### `\`
#####
function rrule(::typeof(\), A::T, B::AbstractVecOrMat{<:Real}) where T<:SquareMatrix{<:Real}
Y = A \ B
function backslash_pullback(Ȳ)
S = T.name.wrapper
∂A = @thunk S(-(A' \ Ȳ) * Y')
∂B = @thunk A' \ Ȳ
return NO_FIELDS, ∂A, ∂B
end
return Y, backslash_pullback
end
function rrule(::typeof(\), A::AbstractVecOrMat{<:Real}, B::AbstractVecOrMat{<:Real})
Y = A \ B
function backslash_pullback(Ȳ)
∂A = @thunk begin
B̄ = A' \ Ȳ
Ā = -B̄ * Y'
_add!(Ā, (B - A * Y) * B̄' / A')
_add!(Ā, A' \ Y * (Ȳ' - B̄'A))
Ā
end
∂B = @thunk A' \ Ȳ
return NO_FIELDS, ∂A, ∂B
end
return Y, backslash_pullback
end
#####
##### `norm`
#####
function rrule(::typeof(norm), A::AbstractArray{<:Real}, p::Real=2)
y = norm(A, p)
function norm_pullback(ȳ)
u = y^(1-p)
∂A = @thunk ȳ .* u .* abs.(A).^p ./ A
∂p = @thunk ȳ * (u * sum(a->abs(a)^p * log(abs(a)), A) - y * log(y)) / p
(NO_FIELDS, ∂A, ∂p)
end
return y, norm_pullback
end
function rrule(::typeof(norm), x::Real, p::Real=2)
function norm_pullback(ȳ)
∂x = @thunk ȳ * sign(x)
∂p = @thunk zero(x) # TODO: should this be Zero()?
(NO_FIELDS, ∂x, ∂p)
end
return norm(x, p), norm_pullback
end