-
Notifications
You must be signed in to change notification settings - Fork 65
Add Enzyme rules for factorizations #464
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
Draft
kshyatt
wants to merge
4
commits into
main
Choose a base branch
from
ksh/enz_fact
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,69 @@ | ||
| # need these due to Enzyme choking on blocks | ||
|
|
||
| for f in (:project_hermitian, :project_antihermitian) | ||
| f! = Symbol(f, :!) | ||
| @eval begin | ||
| function EnzymeRules.augmented_primal( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof($f!)}, | ||
| ::Type{RT}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| arg::Annotation{<:AbstractTensorMap}, | ||
| alg::Const, | ||
| ) where {RT} | ||
| $f!(A.val, arg.val, alg.val) | ||
| primal = EnzymeRules.needs_primal(config) ? arg.val : nothing | ||
| shadow = EnzymeRules.needs_shadow(config) ? arg.dval : nothing | ||
| cache = nothing | ||
| return EnzymeRules.AugmentedReturn(primal, shadow, cache) | ||
| end | ||
| function EnzymeRules.reverse( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof($f!)}, | ||
| ::Type{RT}, | ||
| cache, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| arg::Annotation{<:AbstractTensorMap}, | ||
| alg::Const, | ||
| ) where {RT} | ||
| if !isa(A, Const) && !isa(arg, Const) | ||
| $f!(arg.dval, arg.dval, alg.val) | ||
| if A.dval !== arg.dval | ||
| A.dval .+= arg.dval | ||
| make_zero!(arg.dval) | ||
| end | ||
| end | ||
| return (nothing, nothing, nothing) | ||
| end | ||
| function EnzymeRules.augmented_primal( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof($f)}, | ||
| ::Type{RT}, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| alg::Const, | ||
| ) where {RT} | ||
| ret = $f(A.val, alg.val) | ||
| dret = make_zero(ret) | ||
| primal = EnzymeRules.needs_primal(config) ? ret : nothing | ||
| shadow = EnzymeRules.needs_shadow(config) ? dret : nothing | ||
| cache = dret | ||
| return EnzymeRules.AugmentedReturn(primal, shadow, cache) | ||
| end | ||
| function EnzymeRules.reverse( | ||
| config::EnzymeRules.RevConfigWidth{1}, | ||
| func::Const{typeof($f)}, | ||
| ::Type{RT}, | ||
| cache, | ||
| A::Annotation{<:AbstractTensorMap}, | ||
| alg::Const, | ||
| ) where {RT} | ||
| dret = cache | ||
| if !isa(A, Const) | ||
| $f!(dret, dret, alg.val) | ||
| add!(A.dval, dret) | ||
| end | ||
| make_zero!(dret) | ||
| return (nothing, nothing) | ||
| 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
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,153 @@ | ||
| using Test, TestExtras | ||
| using TensorKit | ||
| using TensorOperations | ||
| using MatrixAlgebraKit | ||
| using MatrixAlgebraKit: remove_svd_gauge_dependence! | ||
| using MatrixAlgebraKit: remove_eig_gauge_dependence! | ||
| using MatrixAlgebraKit: remove_eigh_gauge_dependence! | ||
| using MatrixAlgebraKit: remove_lq_gauge_dependence!, remove_lq_null_gauge_dependence! | ||
| using MatrixAlgebraKit: remove_qr_gauge_dependence!, remove_qr_null_gauge_dependence! | ||
| using Enzyme, EnzymeTestUtils | ||
| using Random | ||
|
|
||
| is_ci = get(ENV, "CI", "false") == "true" | ||
|
|
||
| spacelist = ad_spacelist(fast_tests) | ||
| eltypes = (Float64, ComplexF64) | ||
|
|
||
| @timedtestset "Enzyme - Factorizations: $(TensorKit.type_repr(sectortype(eltype(V)))) ($T)" for V in spacelist, T in eltypes, t in (randn(T, V[1] ⊗ V[2] ← V[1] ⊗ V[2]), randn(T, V[1] ⊗ V[2] ← (V[3] ⊗ V[4] ⊗ V[5])')) | ||
| atol = default_tol(T) | ||
| rtol = default_tol(T) | ||
|
|
||
| @testset "SVD" begin | ||
| if !is_ci | ||
| S = svd_vals(t) | ||
| EnzymeTestUtils.test_reverse(svd_vals, Duplicated, (t, Duplicated); atol, rtol) | ||
| EnzymeTestUtils.test_forward(svd_vals, Duplicated, (t, Duplicated); atol, rtol) | ||
|
|
||
| USVᴴ = svd_full(t) | ||
| ΔUSVᴴ = EnzymeTestUtils.rand_tangent(USVᴴ) | ||
| remove_svd_gauge_dependence!(ΔUSVᴴ[1], ΔUSVᴴ[3], USVᴴ...) | ||
| EnzymeTestUtils.test_reverse(svd_full, Duplicated, (t, Duplicated); output_tangent = ΔUSVᴴ, atol, rtol) | ||
| EnzymeTestUtils.test_forward(svd_full, Duplicated, (t, Duplicated); atol, rtol) | ||
|
|
||
| USVᴴ = svd_compact(t) | ||
| ΔUSVᴴ = EnzymeTestUtils.rand_tangent(USVᴴ) | ||
| remove_svd_gauge_dependence!(ΔUSVᴴ[1], ΔUSVᴴ[3], USVᴴ...) | ||
| EnzymeTestUtils.test_reverse(svd_compact, Duplicated, (t, Duplicated); output_tangent = ΔUSVᴴ, atol, rtol) | ||
| end | ||
|
|
||
| V_trunc = spacetype(t)(c => min(size(b)...) ÷ 2 for (c, b) in blocks(t)) | ||
| trunc = truncspace(V_trunc) | ||
| alg = MatrixAlgebraKit.select_algorithm(svd_trunc_no_error, t, nothing; trunc) | ||
| USVᴴtrunc = svd_trunc_no_error(t, alg) | ||
| ΔUSVᴴtrunc = EnzymeTestUtils.rand_tangent(USVᴴtrunc) | ||
| remove_svd_gauge_dependence!(ΔUSVᴴtrunc[1], ΔUSVᴴtrunc[3], USVᴴtrunc...) | ||
| EnzymeTestUtils.test_reverse(svd_trunc_no_error, Duplicated, (t, Duplicated), (alg, Const); output_tangent = ΔUSVᴴtrunc, atol, rtol) | ||
| end | ||
|
|
||
| @testset "LQ" begin | ||
| EnzymeTestUtils.test_reverse(lq_compact, Duplicated, (t, Duplicated); atol, rtol) | ||
| EnzymeTestUtils.test_forward(lq_compact, Duplicated, (t, Duplicated); atol, rtol) | ||
|
|
||
| if !is_ci | ||
| # lq_full/lq_null requires being careful with gauges | ||
| LQ = lq_full(t) | ||
| ΔLQ = EnzymeTestUtils.rand_tangent(LQ) | ||
| remove_lq_gauge_dependence!(ΔLQ..., t, LQ...) | ||
| EnzymeTestUtils.test_reverse(lq_full, Duplicated, (t, Duplicated); output_tangent = ΔLQ, atol, rtol) | ||
| EnzymeTestUtils.test_forward(lq_full, Duplicated, (t, Duplicated); atol, rtol) | ||
|
|
||
| Nᴴ = lq_null(t) | ||
| Q = lq_compact(t)[2] | ||
| ΔNᴴ = EnzymeTestUtils.rand_tangent(Nᴴ) | ||
| remove_lq_null_gauge_dependence!(ΔNᴴ, Q, Nᴴ) | ||
| EnzymeTestUtils.test_reverse(lq_null, Duplicated, (t, Duplicated); output_tangent = ΔNᴴ, atol, rtol) | ||
| EnzymeTestUtils.test_forward(lq_null, Duplicated, (t, Duplicated); atol, rtol) | ||
| end | ||
| end | ||
|
|
||
| @testset "QR" begin | ||
| EnzymeTestUtils.test_reverse(qr_compact, Duplicated, (t, Duplicated); atol, rtol) | ||
| EnzymeTestUtils.test_forward(qr_compact, Duplicated, (t, Duplicated); atol, rtol) | ||
|
|
||
| if !is_ci | ||
| # qr_full/qr_null requires being careful with gauges | ||
| QR = qr_full(t) | ||
| ΔQR = EnzymeTestUtils.rand_tangent(QR) | ||
| remove_qr_gauge_dependence!(ΔQR..., t, QR...) | ||
| EnzymeTestUtils.test_reverse(qr_full, Duplicated, (t, Duplicated); output_tangent = ΔQR, atol, rtol) | ||
| EnzymeTestUtils.test_forward(qr_full, Duplicated, (t, Duplicated); atol, rtol) | ||
|
|
||
| N = qr_null(t) | ||
| Q = qr_compact(t)[1] | ||
| ΔN = EnzymeTestUtils.rand_tangent(N) | ||
| remove_qr_null_gauge_dependence!(ΔN, t, N) | ||
| EnzymeTestUtils.test_reverse(qr_null, Duplicated, (t, Duplicated); atol, rtol, output_tangent = ΔN) | ||
| EnzymeTestUtils.test_forward(qr_null, Duplicated, (t, Duplicated); atol, rtol) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| @timedtestset "Enzyme - Factorizations (EIGH/EIG): $(TensorKit.type_repr(sectortype(eltype(V)))) ($T)" for V in spacelist, T in eltypes, t in (randn(T, V[1] ← V[1]), rand(T, V[1] ⊗ V[2] ← V[1] ⊗ V[2])) | ||
| atol = default_tol(T) | ||
| rtol = default_tol(T) | ||
|
|
||
| @testset "EIG" begin | ||
| if !is_ci | ||
| DV = eig_full(t) | ||
| ΔDV = EnzymeTestUtils.rand_tangent(DV) | ||
| remove_eig_gauge_dependence!(ΔDV[2], DV...) | ||
| EnzymeTestUtils.test_reverse(eig_full, Duplicated, (t, Duplicated); output_tangent = ΔDV, atol, rtol) | ||
| EnzymeTestUtils.test_forward(eig_full, Duplicated, (t, Duplicated); atol, rtol) | ||
|
|
||
| D = eig_vals(t) | ||
| EnzymeTestUtils.test_reverse(eig_vals, Duplicated, (t, Duplicated); atol, rtol) | ||
| EnzymeTestUtils.test_forward(eig_vals, Duplicated, (t, Duplicated); atol, rtol) | ||
| end | ||
|
|
||
| V_trunc = spacetype(t)(c => min(size(b)...) ÷ 2 for (c, b) in blocks(t)) | ||
| trunc = truncspace(V_trunc) | ||
| alg = MatrixAlgebraKit.select_algorithm(eig_trunc_no_error, t, nothing; trunc) | ||
| DVtrunc = eig_trunc_no_error(t, alg) | ||
| ΔDVtrunc = EnzymeTestUtils.rand_tangent(DVtrunc) | ||
| remove_eig_gauge_dependence!(ΔDVtrunc[2], DVtrunc...) | ||
| EnzymeTestUtils.test_reverse(eig_trunc_no_error, Duplicated, (t, Duplicated), (alg, Const); output_tangent = ΔDVtrunc, atol, rtol) | ||
| end | ||
|
|
||
| @testset "EIGH" begin | ||
| th = project_hermitian(t) | ||
| if !is_ci | ||
| DV = eigh_full(th) | ||
| ΔDV = EnzymeTestUtils.rand_tangent(DV) | ||
| remove_eigh_gauge_dependence!(ΔDV[2], DV...) | ||
| proj_eigh_full(t) = eigh_full(project_hermitian(t)) | ||
| EnzymeTestUtils.test_reverse(proj_eigh_full, Duplicated, (th, Duplicated); output_tangent = ΔDV, atol, rtol) | ||
| EnzymeTestUtils.test_forward(proj_eigh_full, Duplicated, (th, Duplicated); atol, rtol) | ||
|
|
||
| D = eigh_vals(th) | ||
| EnzymeTestUtils.test_reverse(eigh_vals ∘ project_hermitian, Duplicated, (th, Duplicated); atol, rtol) | ||
| EnzymeTestUtils.test_forward(eigh_vals ∘ project_hermitian, Duplicated, (th, Duplicated); atol, rtol) | ||
| end | ||
|
|
||
| V_trunc = spacetype(th)(c => min(size(b)...) ÷ 2 for (c, b) in blocks(t)) | ||
| trunc = truncspace(V_trunc) | ||
| alg = MatrixAlgebraKit.select_algorithm(eigh_trunc_no_error, th, nothing; trunc) | ||
| DVtrunc = eigh_trunc_no_error(th, alg) | ||
| ΔDVtrunc = EnzymeTestUtils.rand_tangent(DVtrunc) | ||
| remove_eigh_gauge_dependence!(ΔDVtrunc[2], DVtrunc...) | ||
| proj_eigh(t, alg) = eigh_trunc_no_error(project_hermitian(t), alg) | ||
| EnzymeTestUtils.test_reverse(proj_eigh, Duplicated, (th, Duplicated), (alg, Const); output_tangent = ΔDVtrunc, atol, rtol) | ||
| end | ||
|
|
||
| @testset "Projections" begin | ||
| EnzymeTestUtils.test_reverse(project_hermitian, Duplicated, (t, Duplicated); atol, rtol) | ||
| EnzymeTestUtils.test_reverse(project_antihermitian, Duplicated, (t, Duplicated); atol, rtol) | ||
| EnzymeTestUtils.test_reverse(project_hermitian!, Duplicated, (t, Duplicated); atol, rtol) | ||
| EnzymeTestUtils.test_reverse(project_antihermitian!, Duplicated, (t, Duplicated); atol, rtol) | ||
| EnzymeTestUtils.test_forward(project_hermitian, Duplicated, (t, Duplicated); atol, rtol) | ||
| EnzymeTestUtils.test_forward(project_antihermitian, Duplicated, (t, Duplicated); atol, rtol) | ||
| EnzymeTestUtils.test_forward(project_hermitian!, Duplicated, (t, Duplicated); atol, rtol) | ||
| EnzymeTestUtils.test_forward(project_antihermitian!, Duplicated, (t, Duplicated); atol, rtol) | ||
| end | ||
| end |
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.
Out of curiosity, who is generating the
nothings here?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.
nvm, I see, do we also have this in the other parts of the code?
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.
What do you mean by other parts? It happens for pbs where the value of t doesn't contribute, it's a pattern inherited from MAK.
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.
thanks, that was exactly the answer I was after :) I guess there we don't have explicit dispatch for them, and use
iszerotangentin the function bodies. Is that worth it to do here as well? Would it be as simple asnothing_or_block(x, c) = isnothing(x) ? x : block(x, c)?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.
yeah we could do that, i'm always biased towards making another method but being less verbose is good