Repository navigation
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
Open
kshyatt
wants to merge
7
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.
+338
−10
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cdd8236
Add Enzyme rules for factorizations
kshyatt 2d1d9e4
Get rid of unneeded projector rules
8ce9b20
Update src/factorizations/pullbacks.jl
kshyatt ea21098
Get rid of some Nothing specializations
135bcde
Update src/factorizations/pushforwards.jl
kshyatt 3e90a17
loosen type signatures
a5b48d7
Remove stray b
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
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,57 @@ | ||
| for pushforward! in ( | ||
| :qr_pushforward!, :lq_pushforward!, :left_polar_pushforward!, :right_polar_pushforward!, | ||
| ) | ||
| @eval function MAK.$pushforward!( | ||
| Δt::AbstractTensorMap, t, F, ΔF; kwargs... | ||
| ) | ||
| foreachblock(Δt) do c, (Δb,) | ||
| b = nothing_or_block(t, c) | ||
| Fc = block.(F, Ref(c)) | ||
| ΔFc = block.(ΔF, Ref(c)) | ||
| return MAK.$pushforward!(Δb, b, Fc, ΔFc; kwargs...) | ||
| end | ||
| return Δt | ||
| end | ||
|
kshyatt marked this conversation as resolved.
|
||
| end | ||
| for pushforward! in (:qr_null_pushforward!, :lq_null_pushforward!) | ||
| @eval function MAK.$pushforward!( | ||
| Δt::AbstractTensorMap, t::AbstractTensorMap, F, ΔF; kwargs... | ||
| ) | ||
| foreachblock(Δt, t) do c, (Δb, b) | ||
| Fc = block(F, c) | ||
| ΔFc = block(ΔF, c) | ||
| return MAK.$pushforward!(Δb, b, Fc, ΔFc; kwargs...) | ||
| end | ||
| return Δt | ||
| end | ||
| end | ||
|
|
||
| for pushforward! in (:eig_vals_pushforward!, :eigh_vals_pushforward!) | ||
| @eval function MAK.$pushforward!( | ||
| Δt::AbstractTensorMap, t, DV::Tuple{Diagonal, <:AbstractTensorMap}, ΔD, inds; | ||
| kwargs... | ||
| ) | ||
| return MAK.$pushforward!(Δt, t, (MAK.diagonal(parent(DV[1])), DV[2]), ΔD, inds; kwargs...) | ||
| end | ||
| end | ||
| function MAK.svd_vals_pushforward!( | ||
| Δt::AbstractTensorMap, t, USVᴴ::Tuple{<:AbstractTensorMap, Diagonal, <:AbstractTensorMap}, ΔS, ind; | ||
| kwargs... | ||
| ) | ||
| return MAK.svd_vals_pushforward!(Δt, t, (USVᴴ[1], MAK.diagonal(parent(USVᴴ[2])), USVᴴ[3]), ΔS, ind; kwargs...) | ||
| end | ||
|
kshyatt marked this conversation as resolved.
|
||
|
|
||
| for pushforward! in (:svd_pushforward!, :eig_pushforward!, :eigh_pushforward!) | ||
| @eval function MAK.$pushforward!( | ||
| Δt::AbstractTensorMap, t, F, ΔF; | ||
| kwargs... | ||
| ) | ||
| foreachblock(Δt, t) do c, (Δb, b) | ||
| Fc = nothing_or_block.(F, Ref(c)) | ||
| ΔFc = nothing_or_block.(ΔF, Ref(c)) | ||
| MAK.$pushforward!(Δb, b, Fc, ΔFc; kwargs...) | ||
| return nothing | ||
| end | ||
| return Δt | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| using Test, TestExtras | ||
| using TensorKit | ||
| using TensorOperations | ||
| using MatrixAlgebraKit | ||
| using MatrixAlgebraKit: remove_eig_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 (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) | ||
|
|
||
| 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 |
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,39 @@ | ||
| using Test, TestExtras | ||
| using TensorKit | ||
| using TensorOperations | ||
| using MatrixAlgebraKit | ||
| using MatrixAlgebraKit: remove_eigh_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 (EIGH): $(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) | ||
| 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 |
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,36 @@ | ||
| using Test, TestExtras | ||
| using TensorKit | ||
| using TensorOperations | ||
| using MatrixAlgebraKit | ||
| using MatrixAlgebraKit: remove_lq_gauge_dependence!, remove_lq_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 (LQ): $(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) | ||
|
|
||
| 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 |
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,24 @@ | ||
| using Test, TestExtras | ||
| using TensorKit | ||
| using TensorOperations | ||
| using MatrixAlgebraKit | ||
| using Enzyme, EnzymeTestUtils | ||
| using Random | ||
|
|
||
| is_ci = get(ENV, "CI", "false") == "true" | ||
|
|
||
| spacelist = ad_spacelist(fast_tests) | ||
| eltypes = (Float64, ComplexF64) | ||
|
|
||
| @timedtestset "Enzyme - Factorizations (Projections): $(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) | ||
| 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 |
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,36 @@ | ||
| using Test, TestExtras | ||
| using TensorKit | ||
| using TensorOperations | ||
| using MatrixAlgebraKit | ||
| 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 (QR): $(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) | ||
|
|
||
| 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 |
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.
Uh oh!
There was an error while loading. Please reload this page.