|
| 1 | +using Test, TestExtras |
| 2 | +using TensorKit |
| 3 | +using TensorOperations |
| 4 | +using MatrixAlgebraKit |
| 5 | +using MatrixAlgebraKit: remove_svd_gauge_dependence! |
| 6 | +using MatrixAlgebraKit: remove_eig_gauge_dependence! |
| 7 | +using MatrixAlgebraKit: remove_eigh_gauge_dependence! |
| 8 | +using MatrixAlgebraKit: remove_lq_gauge_dependence!, remove_lq_null_gauge_dependence! |
| 9 | +using MatrixAlgebraKit: remove_qr_gauge_dependence!, remove_qr_null_gauge_dependence! |
| 10 | +using Enzyme, EnzymeTestUtils |
| 11 | +using Random |
| 12 | + |
| 13 | +spacelist = ad_spacelist(fast_tests) |
| 14 | +eltypes = (Float64, ComplexF64) |
| 15 | + |
| 16 | +@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])')) |
| 17 | + atol = default_tol(T) |
| 18 | + rtol = default_tol(T) |
| 19 | + |
| 20 | + @testset "SVD" begin |
| 21 | + S = svd_vals(t) |
| 22 | + EnzymeTestUtils.test_reverse(svd_vals, Duplicated, (t, Duplicated); atol, rtol) |
| 23 | + |
| 24 | + USVᴴ = svd_full(t) |
| 25 | + ΔUSVᴴ = EnzymeTestUtils.rand_tangent(USVᴴ) |
| 26 | + remove_svd_gauge_dependence!(ΔUSVᴴ[1], ΔUSVᴴ[3], USVᴴ...) |
| 27 | + EnzymeTestUtils.test_reverse(svd_full, Duplicated, (t, Duplicated); output_tangent = ΔUSVᴴ, atol, rtol) |
| 28 | + |
| 29 | + USVᴴ = svd_compact(t) |
| 30 | + ΔUSVᴴ = EnzymeTestUtils.rand_tangent(USVᴴ) |
| 31 | + remove_svd_gauge_dependence!(ΔUSVᴴ[1], ΔUSVᴴ[3], USVᴴ...) |
| 32 | + EnzymeTestUtils.test_reverse(svd_compact, Duplicated, (t, Duplicated); output_tangent = ΔUSVᴴ, atol, rtol) |
| 33 | + |
| 34 | + V_trunc = spacetype(t)(c => min(size(b)...) ÷ 2 for (c, b) in blocks(t)) |
| 35 | + trunc = truncspace(V_trunc) |
| 36 | + alg = MatrixAlgebraKit.select_algorithm(svd_trunc_no_error, t, nothing; trunc) |
| 37 | + USVᴴtrunc = svd_trunc_no_error(t, alg) |
| 38 | + ΔUSVᴴtrunc = EnzymeTestUtils.rand_tangent(USVᴴtrunc) |
| 39 | + remove_svd_gauge_dependence!(ΔUSVᴴtrunc[1], ΔUSVᴴtrunc[3], USVᴴtrunc...) |
| 40 | + EnzymeTestUtils.test_reverse(svd_trunc_no_error, Duplicated, (t, Duplicated), (alg, Const); output_tangent = ΔUSVᴴtrunc, atol, rtol) |
| 41 | + end |
| 42 | + |
| 43 | + @testset "LQ" begin |
| 44 | + EnzymeTestUtils.test_reverse(lq_compact, Duplicated, (A, Duplicated); atol, rtol) |
| 45 | + |
| 46 | + # lq_full/lq_null requires being careful with gauges |
| 47 | + LQ = lq_full(A) |
| 48 | + ΔLQ = EnzymeTestUtils.rand_tangent(LQ) |
| 49 | + remove_lq_gauge_dependence!(ΔLQ..., A, LQ...) |
| 50 | + EnzymeTestUtils.test_reverse(lq_full, Duplicated, (A, Duplicated); output_tangent = ΔLQ, atol, rtol) |
| 51 | + |
| 52 | + Nᴴ = lq_null(A) |
| 53 | + Q = lq_compact(A)[2] |
| 54 | + ΔNᴴ = EnzymeTestUtils.rand_tangent(Nᴴ) |
| 55 | + remove_lq_null_gauge_dependence!(ΔNᴴ, Q, Nᴴ) |
| 56 | + EnzymeTestUtils.test_reverse(lq_null, Duplicated, (A, Duplicated); output_tangent = ΔNᴴ, atol, rtol) |
| 57 | + end |
| 58 | + |
| 59 | + @testset "QR" begin |
| 60 | + # qr_full/qr_null requires being careful with gauges |
| 61 | + QR = qr_full(A) |
| 62 | + ΔQR = EnzymeTestUtils.rand_tangent(QR) |
| 63 | + remove_qr_gauge_dependence!(ΔQR..., A, QR...) |
| 64 | + EnzymeTestUtils.test_reverse(qr_full, Duplicated, (A, Duplicated); output_tangent = ΔQR, atol, rtol) |
| 65 | + |
| 66 | + N = qr_null(A) |
| 67 | + Q = qr_compact(A)[1] |
| 68 | + ΔN = EnzymeTestUtils.rand_tangent(N) |
| 69 | + remove_qr_null_gauge_dependence!(ΔN, A, N) |
| 70 | + EnzymeTestUtils.test_reverse(qr_null, Duplicated, (A, Duplicated); atol, rtol, output_tangent = ΔN) |
| 71 | + end |
| 72 | +end |
| 73 | + |
| 74 | +@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])) |
| 75 | + atol = default_tol(T) |
| 76 | + rtol = default_tol(T) |
| 77 | + |
| 78 | + @testest "EIG" begin |
| 79 | + DV = eig_full(t) |
| 80 | + ΔDV = EnzymeTestUtils.rand_tangent(DV) |
| 81 | + remove_eig_gauge_dependence!(ΔDV[2], DV...) |
| 82 | + EnzymeTestUtils.test_reverse(eig_full, Duplicated, (t, Duplicated); output_tangent = ΔDV, atol, rtol) |
| 83 | + |
| 84 | + D = eig_vals(t) |
| 85 | + EnzymeTestUtils.test_reverse(eig_vals, Duplicated, (t, Duplicated); atol, rtol) |
| 86 | + |
| 87 | + V_trunc = spacetype(t)(c => min(size(b)...) ÷ 2 for (c, b) in blocks(t)) |
| 88 | + trunc = truncspace(V_trunc) |
| 89 | + alg = MatrixAlgebraKit.select_algorithm(eig_trunc_no_error, t, nothing; trunc) |
| 90 | + DVtrunc = eig_trunc_no_error(t, alg) |
| 91 | + ΔDVtrunc = EnzymeTestUtils.rand_tangent(DVtrunc) |
| 92 | + remove_eig_gauge_dependence!(ΔDVtrunc[2], DVtrunc...) |
| 93 | + EnzymeTestUtils.test_reverse(eig_trunc_no_error, Duplicated, (t, Duplicated), (alg, Const); output_tangent = ΔDVtrunc, atol, rtol) |
| 94 | + end |
| 95 | + |
| 96 | + @testest "EIGH" begin |
| 97 | + th = project_hermitian(t) |
| 98 | + DV = eigh_full(th) |
| 99 | + ΔDV = EnzymeTestUtils.rand_tangent(DV) |
| 100 | + remove_eigh_gauge_dependence!(ΔDV[2], DV...) |
| 101 | + proj_eigh_full(t) = eigh_full(project_hermitian(t)) |
| 102 | + EnzymeTestUtils.test_reverse(proj_eigh_full, Duplicated, (th, Duplicated); output_tangent = ΔDV, atol, rtol) |
| 103 | + |
| 104 | + D = eigh_vals(th) |
| 105 | + EnzymeTestUtils.test_reverse(eigh_vals ∘ project_hermitian, Duplicated, (th, Duplicated); atol, rtol) |
| 106 | + |
| 107 | + V_trunc = spacetype(th)(c => min(size(b)...) ÷ 2 for (c, b) in blocks(t)) |
| 108 | + trunc = truncspace(V_trunc) |
| 109 | + alg = MatrixAlgebraKit.select_algorithm(eigh_trunc_no_error, th, nothing; trunc) |
| 110 | + DVtrunc = eigh_trunc_no_error(th, alg) |
| 111 | + ΔDVtrunc = EnzymeTestUtils.rand_tangent(DVtrunc) |
| 112 | + remove_eigh_gauge_dependence!(ΔDVtrunc[2], DVtrunc...) |
| 113 | + proj_eigh(t, alg) = eigh_trunc_no_error(project_hermitian(t), alg) |
| 114 | + EnzymeTestUtils.test_reverse(proj_eigh, Duplicated, (th, Duplicated), (alg, Const); output_tangent = ΔDVtrunc, atol, rtol) |
| 115 | + end |
| 116 | + |
| 117 | + @testset "Projections" begin |
| 118 | + EnzymeTestUtils.test_reverse(project_hermitian, Duplicated, (t, Duplicated); atol, rtol) |
| 119 | + EnzymeTestUtils.test_reverse(project_antihermitian, Duplicated, (t, Duplicated); atol, rtol) |
| 120 | + EnzymeTestUtils.test_reverse(project_hermitian!, Duplicated, (t, Duplicated); atol, rtol) |
| 121 | + EnzymeTestUtils.test_reverse(project_antihermitian!, Duplicated, (t, Duplicated); atol, rtol) |
| 122 | + end |
| 123 | +end |
0 commit comments