Repository navigation
Make the polar AD rules a lot more GPU friendly #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
031f933
3c00bac
1f2a47b
1d26580
3ad172b
9bda87c
16af1de
c4c09a8
6423814
89be025
5198536
4bb4c8a
1cab52b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,8 +11,25 @@ function iszerotangent end | |||||||||||||||||||||||||||||||||||||||||||||
| iszerotangent(::Any) = false | ||||||||||||||||||||||||||||||||||||||||||||||
| iszerotangent(::Nothing) = true | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| # fallback | ||||||||||||||||||||||||||||||||||||||||||||||
| _sylvester(A, B, C) = LinearAlgebra.sylvester(A, B, C) | ||||||||||||||||||||||||||||||||||||||||||||||
| # 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) | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+27
to
+31
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
An alternative would also be to directly only keep the complement, which is possible with one less allocation, if we are happy with temporarily destroying
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||
| select_indices(r::AbstractRange, ind) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -62,8 +62,8 @@ function left_polar!(A::AbstractMatrix, WP, alg::PolarViaSVD) | |||||
| W, P = WP | ||||||
| W = mul!(W, U, Vᴴ) | ||||||
| if !isempty(P) | ||||||
| S .= sqrt.(S) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How did this ever work before?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I choose not to question it |
||||||
| SsqrtVᴴ = lmul!(S, Vᴴ) | ||||||
| Ssqrt = sqrt.(diagview(S)) | ||||||
| SsqrtVᴴ = Vᴴ .= Ssqrt .* Vᴴ | ||||||
| P = _mul_herm!(P, SsqrtVᴴ') | ||||||
| end | ||||||
| return (W, P) | ||||||
|
|
@@ -74,8 +74,8 @@ function right_polar!(A::AbstractMatrix, PWᴴ, alg::PolarViaSVD) | |||||
| P, Wᴴ = PWᴴ | ||||||
| Wᴴ = mul!(Wᴴ, U, Vᴴ) | ||||||
| if !isempty(P) | ||||||
| S .= sqrt.(S) | ||||||
| USsqrt = rmul!(U, S) | ||||||
| Ssqrt = reshape(sqrt.(diagview(S)), 1, :) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if that would also work:
Suggested change
|
||||||
| USsqrt = U .= U .* Ssqrt | ||||||
| P = _mul_herm!(P, USsqrt) | ||||||
| end | ||||||
| return (P, Wᴴ) | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.