-
Notifications
You must be signed in to change notification settings - Fork 7
Basic eig(h) forward rules #245
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
681cb3d
Basic eig(h) forward rules
kshyatt 62881ef
Support Enzyme too
0e55bf8
Update MatrixAlgebraKitEnzymeExt.jl
kshyatt 7858d38
Apply suggestions from code review
kshyatt 6e695de
A little more cleanup
kshyatt c3f92e8
Apply suggestions from code review
kshyatt 4b6727e
Naming consistency
bdd7dc3
Update src/pushforwards/eigh.jl
kshyatt 725a588
Don't multiply into nothing
cf50437
And eigh
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ using MatrixAlgebraKit: diagview, inv_safe, truncate | |
| using MatrixAlgebraKit: qr_pullback!, lq_pullback! | ||
| using MatrixAlgebraKit: qr_null_pullback!, lq_null_pullback! | ||
| using MatrixAlgebraKit: eig_pullback!, eigh_pullback!, eig_vals_pullback!, eigh_vals_pullback! | ||
| using MatrixAlgebraKit: eig_pushforward!, eigh_pushforward!, eig_vals_pushforward!, eigh_vals_pushforward! | ||
| using MatrixAlgebraKit: svd_pullback!, svd_vals_pullback! | ||
| using MatrixAlgebraKit: left_polar_pullback!, right_polar_pullback! | ||
| using MatrixAlgebraKit: left_polar_pushforward!, right_polar_pushforward! | ||
|
|
@@ -119,22 +120,28 @@ for (f, pb) in ( | |
| end | ||
|
|
||
| for (f, pf) in ( | ||
| (left_polar!, left_polar_pushforward!), | ||
| (right_polar!, right_polar_pushforward!), | ||
| (:right_polar!, :right_polar_pushforward!), | ||
| (:left_polar!, :left_polar_pushforward!), | ||
| (:eigh_full!, :eigh_pushforward!), | ||
| (:eig_full!, :eig_pushforward!), | ||
| ) | ||
| @eval begin | ||
| function EnzymeRules.forward( | ||
| config::EnzymeRules.FwdConfigWidth{1}, | ||
| func::Const{typeof($f)}, | ||
| ::Type{RT}, | ||
| A::Annotation, | ||
| arg::Annotation{TA}, | ||
| arg::Annotation, | ||
| alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm}, | ||
| ) where {RT, TA} | ||
| ) where {RT} | ||
| A_is_arg1 = !isa(A, Const) && A.val === arg.val[1] | ||
| A_is_arg2 = !isa(A, Const) && A.val === arg.val[2] | ||
| A_is_arg = A_is_arg1 || A_is_arg2 | ||
| $f(A.val, arg.val, alg.val) | ||
| if !isa(A, Const) && !isa(arg, Const) | ||
| $pf(A.dval, A.val, arg.val, arg.dval) | ||
| end | ||
| !A_is_arg && make_zero!(A.dval) | ||
| if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config) | ||
| return arg | ||
| elseif EnzymeRules.needs_primal(config) | ||
|
|
@@ -367,9 +374,9 @@ for (f, trunc_f, full_f, pb) in ( | |
| end | ||
| end | ||
|
|
||
| for (f!, f_full!, pb!) in ( | ||
| (eig_vals!, eig_full!, eig_vals_pullback!), | ||
| (eigh_vals!, eigh_full!, eigh_vals_pullback!), | ||
| for (f!, f_full!, pb!, pf!) in ( | ||
| (:eig_vals!, :eig_full!, :eig_vals_pullback!, :eig_vals_pushforward!), | ||
| (:eigh_vals!, :eigh_full!, :eigh_vals_pullback!, :eigh_vals_pushforward!), | ||
| ) | ||
| @eval begin | ||
| function EnzymeRules.augmented_primal( | ||
|
|
@@ -418,6 +425,34 @@ for (f!, f_full!, pb!) in ( | |
| !isa(D, Const) && !A_is_arg && make_zero!(D.dval) | ||
| return (nothing, nothing, nothing) | ||
| end | ||
| function EnzymeRules.forward( | ||
| config::EnzymeRules.FwdConfigWidth{1}, | ||
| func::Const{typeof($f!)}, | ||
| ::Type{RT}, | ||
| A::Annotation{TA}, | ||
| D::Annotation, | ||
| alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm}, | ||
| ) where {RT, TA} | ||
| A_is_arg = !isa(A, Const) && TA <: Diagonal && diagview(A.dval) === D.dval | ||
| DV = $f_full!(A.val, alg.val) | ||
| Dval, V = DV | ||
| if !isa(A, Const) && !isa(D, Const) | ||
| ΔD = A_is_arg ? make_zero(D.dval) : D.dval | ||
| $pf!(A.dval, A.val, (Diagonal(diagview(Dval)), V), ΔD) | ||
|
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. Is this |
||
| A_is_arg && (D.dval .= ΔD) | ||
| end | ||
| copyto!(D.val, diagview(Dval)) | ||
| !isa(A, Const) && !A_is_arg && make_zero!(A.dval) | ||
| if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config) | ||
| return D | ||
| elseif EnzymeRules.needs_primal(config) | ||
| return D.val | ||
| elseif EnzymeRules.needs_shadow(config) | ||
| return D.dval | ||
| else | ||
| return nothing | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| function eig_pushforward!( | ||
| ΔA, A, DV, ΔDV; | ||
| degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), | ||
| gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]) | ||
| ) | ||
| D, V = DV | ||
| ΔD, ΔV = ΔDV | ||
| ΔAV = isnothing(ΔV) ? ΔA * V : mul!(ΔV, ΔA, V) # reusing ΔV memory if possible | ||
| ∂K = V \ ΔAV | ||
| if !iszerotangent(ΔD) | ||
| diagview(ΔD) .= diagview(∂K) | ||
| end | ||
| if !iszerotangent(ΔV) | ||
| ∂K .*= inv_safe.(transpose(diagview(D)) .- diagview(D), degeneracy_atol) | ||
| mul!(ΔV, V, ∂K, 1, 0) | ||
| end | ||
| return ΔDV | ||
| end | ||
|
|
||
| function eig_vals_pushforward!(ΔA, A, DV, ΔD; kwargs...) | ||
| return eig_pushforward!(ΔA, A, DV, (Diagonal(ΔD), nothing); kwargs...) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| function eigh_pushforward!( | ||
| ΔA, A, DV, ΔDV; | ||
| degeneracy_atol::Real = default_pullback_rank_atol(DV[1]), | ||
| gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]) | ||
| ) | ||
| D, V = DV | ||
| ΔD, ΔV = ΔDV | ||
| ΔAV = isnothing(ΔV) ? ΔA * V : mul!(ΔV, ΔA, V) # reusing ΔV memory if possible | ||
| ∂K = V' * ΔAV | ||
| if !iszerotangent(ΔD) | ||
| diagview(ΔD) .= real.(diagview(∂K)) | ||
| end | ||
| if !iszerotangent(ΔV) | ||
| ∂K .*= inv_safe.(transpose(diagview(D)) .- diagview(D), degeneracy_atol) | ||
| ΔV = mul!(ΔV, V, ∂K) | ||
| end | ||
| return (ΔD, ΔV) | ||
| end | ||
|
|
||
| function eigh_vals_pushforward!(ΔA, A, DV, ΔD; kwargs...) | ||
| return eigh_pushforward!(ΔA, A, DV, (Diagonal(ΔD), nothing); kwargs...) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is there actually any of these functions that supports
A_is_arg? I think not, but if any of them does, I am wondering if thepushforwardrules take this possibility into account. In the case ofeig(h)for example,mul!(ΔV, ΔA, V)might be a bad idea if the possibility ofΔV === ΔAexists (but I don't think any of the algorithms actually support this). Or maybeDiagonalAlgorithm?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.
It can indeed happen in the
DiagonalAlgorithmcase, see e.g. https://github.com/QuantumKitHub/MatrixAlgebraKit.jl/blob/main/src/implementations/eig.jl#L68 or https://github.com/QuantumKitHub/MatrixAlgebraKit.jl/blob/main/src/implementations/lq.jl#L83