-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsvd.jl
More file actions
251 lines (228 loc) · 11.1 KB
/
svd.jl
File metadata and controls
251 lines (228 loc) · 11.1 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
"""
svd_pullback!(
ΔA, A, USVᴴ, ΔUSVᴴ, [ind];
rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3])
)
Adds the pullback from the SVD of `A` to `ΔA` given the output `USVᴴ` of `svd_compact` or
`svd_full` and the cotangent `ΔUSVᴴ` of `svd_compact`, `svd_full` or `svd_trunc`.
In particular, it is assumed that `A ≈ U * S * Vᴴ`, or thus, that no singular values with
magnitude less than `rank_atol` are missing from `S`. For the cotangents, an arbitrary
number of singular vectors or singular values can be missing, i.e. for a matrix `A` with
size `(m, n)`, `ΔU` and `ΔVᴴ` can have sizes `(m, pU)` and `(pV, n)` respectively, whereas
`diagview(ΔS)` can have length `pS`. In those cases, additionally `ind` is required to
specify which singular vectors and values are present in `ΔU`, `ΔS` and `ΔVᴴ`.
A warning will be printed if the cotangents are not gauge-invariant, i.e. if the
anti-hermitian part of `U' * ΔU + Vᴴ * ΔVᴴ'`, restricted to rows `i` and columns `j` for
which `abs(S[i] - S[j]) < degeneracy_atol`, is not small compared to `gauge_atol`.
"""
function svd_pullback!(
ΔA::AbstractMatrix, A, USVᴴ, ΔUSVᴴ, ind = Colon();
rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3])
)
# Extract the SVD components
U, Smat, Vᴴ = USVᴴ
m, n = size(U, 1), size(Vᴴ, 2)
(m, n) == size(ΔA) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of U*S*Vᴴ ($m, $n)"))
minmn = min(m, n)
S = diagview(Smat)
length(S) == minmn || throw(DimensionMismatch("length of S ($(length(S))) does not matrix minimum dimension of U, Vᴴ ($minmn)"))
r = searchsortedlast(S, rank_atol; rev = true) # rank
Ur = view(U, :, 1:r)
Vᴴr = view(Vᴴ, 1:r, :)
Sr = view(S, 1:r)
# Extract and check the cotangents
ΔU, ΔSmat, ΔVᴴ = ΔUSVᴴ
UΔU = fill!(similar(U, (r, r)), 0)
VΔV = fill!(similar(Vᴴ, (r, r)), 0)
if !iszerotangent(ΔU)
m == size(ΔU, 1) || throw(DimensionMismatch("first dimension of ΔU ($(size(ΔU, 1))) does not match first dimension of U ($m)"))
pU = size(ΔU, 2)
pU > r && throw(DimensionMismatch("second dimension of ΔU ($(size(ΔU, 2))) does not match rank of S ($r)"))
indU = axes(U, 2)[ind]
length(indU) == pU || throw(DimensionMismatch("length of selected U columns ($(length(indU))) does not match second dimension of ΔU ($(size(ΔU, 2)))"))
UΔUp = view(UΔU, :, indU)
mul!(UΔUp, Ur', ΔU)
# ΔU -= Ur * UΔUp but one less allocation without overwriting ΔU
ΔU = mul!(copy(ΔU), Ur, UΔUp, -1, 1)
end
if !iszerotangent(ΔVᴴ)
n == size(ΔVᴴ, 2) || throw(DimensionMismatch("second dimension of ΔVᴴ ($(size(ΔVᴴ, 2))) does not match second dimension of Vᴴ ($n)"))
pV = size(ΔVᴴ, 1)
pV > r && throw(DimensionMismatch("first dimension of ΔVᴴ ($(size(ΔVᴴ, 1))) does not match rank of S ($r)"))
indV = axes(Vᴴ, 1)[ind]
length(indV) == pV || throw(DimensionMismatch("length of selected Vᴴ rows ($(length(indV))) does not match first dimension of ΔVᴴ ($(size(ΔVᴴ, 1)))"))
VΔVp = view(VΔV, :, indV)
mul!(VΔVp, Vᴴr, ΔVᴴ')
# ΔVᴴ -= VΔVp' * Vᴴr but one less allocation without overwriting ΔVᴴ
ΔVᴴ = mul!(copy(ΔVᴴ), VΔVp', Vᴴr, -1, 1)
end
# Project onto antihermitian part; hermitian part outside of Grassmann tangent space
aUΔU = project_antihermitian!(UΔU)
aVΔV = project_antihermitian!(VΔV)
# check whether cotangents arise from gauge-invariance objective function
mask = abs.(Sr' .- Sr) .< degeneracy_atol
Δgauge = norm(view(aUΔU, mask) + view(aVΔV, mask), Inf)
Δgauge ≤ gauge_atol ||
@warn "`svd` cotangents sensitive to gauge choice: (|Δgauge| = $Δgauge)"
UdΔAV = (aUΔU .+ aVΔV) .* inv_safe.(Sr' .- Sr, degeneracy_atol) .+
(aUΔU .- aVΔV) .* inv_safe.(Sr' .+ Sr, degeneracy_atol)
if !iszerotangent(ΔSmat)
ΔS = diagview(ΔSmat)
pS = length(ΔS)
indS = axes(S, 1)[ind]
length(indS) == pS || throw(DimensionMismatch("length of selected S diagonals ($(length(indS))) does not match length of ΔS diagonal ($(length(ΔS)))"))
view(diagview(UdΔAV), indS) .+= real.(ΔS)
end
ΔA = mul!(ΔA, Ur, UdΔAV * Vᴴr, 1, 1) # add the contribution to ΔA
# Add the remaining contributions
if m > r && !iszerotangent(ΔU) # remaining ΔU is already orthogonal to Ur
Sp = view(S, indU)
Vᴴp = view(Vᴴ, indU, :)
ΔA = mul!(ΔA, ΔU ./ Sp', Vᴴp, 1, 1)
end
if n > r && !iszerotangent(ΔVᴴ) # remaining ΔV is already orthogonal to Vᴴr
Sp = view(S, indV)
Up = view(U, :, indV)
ΔA = mul!(ΔA, Up, Sp .\ ΔVᴴ, 1, 1)
end
return ΔA
end
function svd_pullback!(
ΔA::Diagonal, A, USVᴴ, ΔUSVᴴ, ind = Colon();
rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3])
)
ΔA_full = zero!(similar(ΔA, size(ΔA)))
ΔA_full = svd_pullback!(ΔA_full, A, USVᴴ, ΔUSVᴴ, ind; rank_atol, degeneracy_atol, gauge_atol)
diagview(ΔA) .+= diagview(ΔA_full)
return ΔA
end
"""
svd_trunc_pullback!(
ΔA, A, USVᴴ, ΔUSVᴴ;
rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3])
)
Adds the pullback from the truncated SVD of `A` to `ΔA`, given the output `USVᴴ` and the
cotangent `ΔUSVᴴ` of `svd_trunc`.
In particular, it is assumed that `A * Vᴴ' ≈ U * S` and `U' * A = S * Vᴴ`, with `U` and `Vᴴ`
rectangular matrices of left and right singular vectors, and `S` diagonal. For the
cotangents, it is assumed that if `ΔU` and `ΔVᴴ` are not zero, then they have the same size
as `U` and `Vᴴ` (respectively), and if `ΔS` is not zero, then it is a diagonal matrix of the
same size as `S`. For this method to work correctly, it is also assumed that the remaining
singular values (not included in `S`) are (sufficiently) separated from those in `S`.
A warning will be printed if the cotangents are not gauge-invariant, i.e. if the
anti-hermitian part of `U' * ΔU + Vᴴ * ΔVᴴ'`, restricted to rows `i` and columns `j` for
which `abs(S[i] - S[j]) < degeneracy_atol`, is not small compared to `gauge_atol`.
"""
function svd_trunc_pullback!(
ΔA::AbstractMatrix, A, USVᴴ, ΔUSVᴴ;
rank_atol::Real = 0,
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3])
)
# Extract the SVD components
U, Smat, Vᴴ = USVᴴ
m, n = size(U, 1), size(Vᴴ, 2)
(m, n) == size(ΔA) || throw(DimensionMismatch())
p = size(U, 2)
p == size(Vᴴ, 1) || throw(DimensionMismatch())
S = diagview(Smat)
p == length(S) || throw(DimensionMismatch())
# Extract and check the cotangents
ΔU, ΔSmat, ΔVᴴ = ΔUSVᴴ
UΔU = fill!(similar(U, (p, p)), 0)
VΔV = fill!(similar(Vᴴ, (p, p)), 0)
if !iszerotangent(ΔU)
(m, p) == size(ΔU) || throw(DimensionMismatch())
mul!(UΔU, U', ΔU)
end
if !iszerotangent(ΔVᴴ)
(p, n) == size(ΔVᴴ) || throw(DimensionMismatch())
mul!(VΔV, Vᴴ, ΔVᴴ')
# ΔVᴴ -= VΔVp' * Vᴴr but one less allocation without overwriting ΔVᴴ
ΔVᴴ = mul!(copy(ΔVᴴ), VΔV', Vᴴ, -1, 1)
end
# Project onto antihermitian part; hermitian part outside of Grassmann tangent space
aUΔU = project_antihermitian!(UΔU)
aVΔV = project_antihermitian!(VΔV)
# check whether cotangents arise from gauge-invariance objective function
mask = abs.(S' .- S) .< degeneracy_atol
Δgauge = norm(view(aUΔU, mask) + view(aVΔV, mask), Inf)
Δgauge ≤ gauge_atol ||
@warn "`svd` cotangents sensitive to gauge choice: (|Δgauge| = $Δgauge)"
UdΔAV = (aUΔU .+ aVΔV) .* inv_safe.(S' .- S, degeneracy_atol) .+
(aUΔU .- aVΔV) .* inv_safe.(S' .+ S, degeneracy_atol)
if !iszerotangent(ΔSmat)
ΔS = diagview(ΔSmat)
p == length(ΔS) || throw(DimensionMismatch())
diagview(UdΔAV) .+= real.(ΔS)
end
ΔA = mul!(ΔA, U, UdΔAV * Vᴴ, 1, 1) # add the contribution to ΔA
# add contribution from orthogonal complement
Ũ = qr_null(U)
Ṽᴴ = lq_null(Vᴴ)
m̃ = m - p
ñ = n - p
à = Ũ' * A * Ṽᴴ'
ÃÃ = similar(A, (m̃ + ñ, m̃ + ñ))
fill!(ÃÃ, 0)
view(ÃÃ, (1:m̃), m̃ .+ (1:ñ)) .= Ã
view(ÃÃ, m̃ .+ (1:ñ), 1:m̃) .= Ã'
rhs = similar(Ũ, (m̃ + ñ, p))
if !iszerotangent(ΔU)
mul!(view(rhs, 1:m̃, :), Ũ', ΔU)
else
fill!(view(rhs, 1:m̃, :), 0)
end
if !iszerotangent(ΔVᴴ)
mul!(view(rhs, m̃ .+ (1:ñ), :), Ṽᴴ, ΔVᴴ')
else
fill!(view(rhs, m̃ .+ (1:ñ), :), 0)
end
XY = sylvester(ÃÃ, -Smat, rhs)
X = view(XY, 1:m̃, :)
Y = view(XY, m̃ .+ (1:ñ), :)
ΔA = mul!(ΔA, Ũ, X * Vᴴ, 1, 1)
ΔA = mul!(ΔA, U, Y' * Ṽᴴ, 1, 1)
return ΔA
end
function svd_trunc_pullback!(
ΔA::Diagonal, A, USVᴴ, ΔUSVᴴ;
rank_atol::Real = 0,
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3])
)
ΔA_full = zero!(similar(ΔA, size(ΔA)))
ΔA_full = svd_trunc_pullback!(ΔA_full, A, USVᴴ, ΔUSVᴴ; rank_atol, degeneracy_atol, gauge_atol)
diagview(ΔA) .+= diagview(ΔA_full)
return ΔA
end
"""
svd_vals_pullback!(
ΔA, A, USVᴴ, ΔS, [ind];
rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2])
)
Adds the pullback from the singular values of `A` to `ΔA`, given the output
`USVᴴ` of `svd_compact`, and the cotangent `ΔS` of `svd_vals`.
In particular, it is assumed that `A ≈ U * S * Vᴴ`, or thus, that no singular values with
magnitude less than `rank_atol` are missing from `S`. For the cotangents, an arbitrary
number of singular vectors or singular values can be missing, i.e. for a matrix `A` with
size `(m, n)`, `diagview(ΔS)` can have length `pS`. In those cases, additionally `ind` is required to
specify which singular vectors and values are present in `ΔS`.
"""
function svd_vals_pullback!(
ΔA, A, USVᴴ, ΔS, ind = Colon();
rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2])
)
ΔUSVᴴ = (nothing, diagonal(ΔS), nothing)
return svd_pullback!(ΔA, A, USVᴴ, ΔUSVᴴ, ind; rank_atol, degeneracy_atol)
end