Repository navigation
Expand file tree
/
Copy pathpullbacks.jl
More file actions
113 lines (99 loc) · 4.04 KB
/
Copy pathpullbacks.jl
File metadata and controls
113 lines (99 loc) · 4.04 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
"""
iszerotangent(x)
Return true if `x` is of a type that the different AD engines use to communicate
a (co)tangent that is identically zero. By overloading this method, and writing
pullback definitions in term of it, we will be able to hook into different AD
ecosystems
"""
function iszerotangent end
iszerotangent(::Any) = false
iszerotangent(::Nothing) = true
# Solve the Sylvester equation `P * X + X * P = C` for a Hermitian positive semidefinite `P`
# with eigendecomposition `P = U * Diagonal(d) * U'`.
function _sylvester_psd(U, d, C, tol)
Y = U' * C * U
dd = d .+ transpose(d)
Δ = maximum(abs, (abs.(dd ./ 2) .< tol) .* Y; init = zero(real(eltype(Y))))
Y .*= inv_safe.(dd, tol)
return U * Y * U', Δ
end
# Remove from `X` its block within the null space of the Hermitian positive semidefinite
# `P = U * Diagonal(d) * U'`, i.e. the eigenvectors with eigenvalues smaller than `tol`.
function _remove_null_block!(X, U, d, tol)
isnull = abs.(d) .< tol
any(isnull) || return X
Y = U' * X * U
Y .*= isnull .& transpose(isnull)
return mul!(X, U, Y * U', -1, 1)
end
"""
select_indices(r::AbstractRange, ind)
Compute `r[ind]` without iterating over `ind`, so that this also works for an `ind` that
lives on a device.
"""
select_indices(r::AbstractRange, ind) = r[ind]
select_indices(r::AbstractRange, ind::AbstractRange{<:Integer}) = r[ind]
function select_indices(r::AbstractRange, ind::AbstractVector{<:Integer})
checkbounds(r, ind)
return first(r) .+ step(r) .* (ind .- 1)
end
# `Bool <: Integer`, so logical masks need to
# be converted *before* the arithmetic above
function select_indices(r::AbstractRange, ind::AbstractVector{Bool})
checkbounds(r, ind)
return select_indices(r, findall(ind))
end
"""
is_leading_index(ind, p::Int)
Check whether `ind` selects the first `p` values in order, i.e. whether `ind == 1:p`, without
iterating over `ind`, so that this also works for an `ind` that lives on a device.
"""
is_leading_index(ind::AbstractRange, p::Int) = ind == 1:p
is_leading_index(ind::AbstractVector, p::Int) = length(ind) == p && all(ind .== 1:p)
"""
accelerative_smith_iteration!(X, Xₙ, G, w, atol, maxiter)
Solve `X = B + G * X * Diagonal(w)` by summing the Neumann series
`X = Σₖ Gᵏ * B * Diagonal(w)ᵏ` by doubling (Smith's method), i.e. by repeatedly adding
`G^(2ʲ) * X * Diagonal(w)^(2ʲ)` to `X` until the norm of that increment drops below `atol`,
for at most `maxiter` steps.
On entry, `X` contains `B`, and it is overwritten with the result. `Xₙ` is used as a buffer,
and `G` and `w` are overwritten. `w` is normalized such that `maximum(abs, w) == 1`, so that
squaring it can only shrink it; `G` is scaled by the inverse factor to compensate.
Reference: https://doi.org/10.1016/j.aml.2009.01.012.
"""
function accelerative_smith_iteration!(X, Xₙ, G, w, atol, maxiter)
Gₙ = similar(G)
wmax = maximum(abs, w)
w ./= wmax
G .*= wmax
for k in 1:maxiter
Xₙ = rmul!(mul!(Xₙ, G, X), Diagonal(w))
if maximum(abs, Xₙ) < atol
break
end
X .+= Xₙ
if k == maxiter
@warn "Sylvester iteration did not converge after $k iterations, final norm of X: $(maximum(abs, X))"
break
end
w .= w .^ 2
Gₙ = mul!(Gₙ, G, G)
G, Gₙ = Gₙ, G
end
return X
end
"""
antihermitian_columns!(X, ind)
Given the columns `X = M[:, ind]` of a square matrix `M` that is nonzero only in its columns
`ind`, overwrite `X` with the same columns of the antihermitian part `(M - M') / 2` and return it.
Here `M'` is nonzero only in the rows `ind`, so it only contributes to the square block
`X[ind, :] = M[ind, ind]` on the diagonal of `M`.
"""
function antihermitian_columns!(X, ind)
# NOTE: all columns in order (e.g. from `ind = Colon()` in the pullback): the original in-place projection
is_leading_index(ind, size(X, 1)) && return project_antihermitian!(X)
Xdiag = project_antihermitian!(X[ind, :]) # the diagonal block M[ind, ind]
X ./= 2
X[ind, :] .= Xdiag
return X
end