Skip to content

Commit c4d0f3d

Browse files
committed
Add Enzyme rules for factorizations
1 parent 3e1b0fb commit c4d0f3d

9 files changed

Lines changed: 253 additions & 2 deletions

File tree

‎.github/workflows/CI.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ jobs:
2222
with:
2323
fast: "${{ github.event.pull_request.draft == true }}"
2424
exclude: '["cuda", "amd"]'
25-
timeout-minutes: 120
25+
timeout-minutes: 150
2626
secrets:
2727
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Preferences = "1"
6565
Printf = "1"
6666
Random = "1"
6767
ScopedValues = "1.3.0"
68-
Strided = "2.6.1"
68+
Strided = "2.6.3"
6969
TensorKitSectors = "0.3.7"
7070
TensorOperations = "5.5.2, 5.6"
7171
TimerOutputs = "1"

‎ext/TensorKitEnzymeExt/TensorKitEnzymeExt.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ include("utility.jl")
1515
include("linalg.jl")
1616
include("indexmanipulations.jl")
1717
include("tensoroperations.jl")
18+
include("factorizations.jl")
1819

1920
end
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# need these due to Enzyme choking on blocks
2+
3+
for f in (:project_hermitian, :project_antihermitian)
4+
f! = Symbol(f, :!)
5+
@eval begin
6+
function EnzymeRules.augmented_primal(
7+
config::EnzymeRules.RevConfigWidth{1},
8+
func::Const{typeof($f!)},
9+
::Type{RT},
10+
A::Annotation{<:AbstractTensorMap},
11+
arg::Annotation{<:AbstractTensorMap},
12+
alg::Const,
13+
) where {RT}
14+
$f!(A.val, arg.val, alg.val)
15+
primal = EnzymeRules.needs_primal(config) ? arg.val : nothing
16+
shadow = EnzymeRules.needs_shadow(config) ? arg.dval : nothing
17+
cache = nothing
18+
return EnzymeRules.AugmentedReturn(primal, shadow, cache)
19+
end
20+
function EnzymeRules.reverse(
21+
config::EnzymeRules.RevConfigWidth{1},
22+
func::Const{typeof($f!)},
23+
::Type{RT},
24+
cache,
25+
A::Annotation{<:AbstractTensorMap},
26+
arg::Annotation{<:AbstractTensorMap},
27+
alg::Const,
28+
) where {RT}
29+
if !isa(A, Const) && !isa(arg, Const)
30+
$f!(arg.dval, arg.dval, alg.val)
31+
if A.dval !== arg.dval
32+
A.dval .+= arg.dval
33+
make_zero!(arg.dval)
34+
end
35+
end
36+
return (nothing, nothing, nothing)
37+
end
38+
function EnzymeRules.augmented_primal(
39+
config::EnzymeRules.RevConfigWidth{1},
40+
func::Const{typeof($f)},
41+
::Type{RT},
42+
A::Annotation{<:AbstractTensorMap},
43+
alg::Const,
44+
) where {RT}
45+
ret = $f(A.val, alg.val)
46+
dret = make_zero(ret)
47+
primal = EnzymeRules.needs_primal(config) ? ret : nothing
48+
shadow = EnzymeRules.needs_shadow(config) ? dret : nothing
49+
cache = dret
50+
return EnzymeRules.AugmentedReturn(primal, shadow, cache)
51+
end
52+
function EnzymeRules.reverse(
53+
config::EnzymeRules.RevConfigWidth{1},
54+
func::Const{typeof($f)},
55+
::Type{RT},
56+
cache,
57+
A::Annotation{<:AbstractTensorMap},
58+
alg::Const,
59+
) where {RT}
60+
dret = cache
61+
if !isa(A, Const)
62+
$f!(dret, dret, alg.val)
63+
add!(A.dval, dret)
64+
end
65+
make_zero!(dret)
66+
return (nothing, nothing)
67+
end
68+
end
69+
end

‎ext/TensorKitEnzymeExt/utility.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ end
147147
@inline EnzymeRules.inactive(::typeof(TensorKit.insertleftunit), ::HomSpace, ::Any) = nothing
148148
@inline EnzymeRules.inactive(::typeof(TensorKit.insertrightunit), ::HomSpace, ::Any) = nothing
149149
@inline EnzymeRules.inactive(::typeof(TensorKit.removeunit), ::HomSpace, ::Any) = nothing
150+
@inline EnzymeRules.inactive(::typeof(TensorKit.infimum), ::Any, ::Any) = nothing
150151
@inline EnzymeRules.inactive(::typeof(TensorKit.sectorstructure), ::Any) = nothing
151152
@inline EnzymeRules.inactive(::typeof(TensorKit.degeneracystructure), ::Any) = nothing
152153
@inline EnzymeRules.inactive(::typeof(TensorKit.select), s::HomSpace, i::Index2Tuple) = nothing

‎src/factorizations/diagonal.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
_repack_diagonal(d::DiagonalTensorMap) = Diagonal(d.data)
44
_repack_diagonal(d::SectorVector) = Diagonal(parent(d))
55

6+
MAK.diagonal(t::SectorVector) = DiagonalTensorMap(t)
67
MAK.diagview(t::DiagonalTensorMap) = SectorVector(t.data, TensorKit.diagonalblockstructure(space(t)))
78

89
for f in (

‎src/factorizations/pullbacks.jl‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ for pullback! in (
1111
end
1212
return Δt
1313
end
14+
@eval function MAK.$pullback!(
15+
Δt::AbstractTensorMap, ::Nothing, F, ΔF; kwargs...
16+
)
17+
foreachblock(Δt) do c, (Δb,)
18+
Fc = block.(F, Ref(c))
19+
ΔFc = block.(ΔF, Ref(c))
20+
return MAK.$pullback!(Δb, nothing, Fc, ΔFc; kwargs...)
21+
end
22+
return Δt
23+
end
1424
end
1525
for pullback! in (:qr_null_pullback!, :lq_null_pullback!)
1626
@eval function MAK.$pullback!(
@@ -41,6 +51,33 @@ for pullback! in (:svd_pullback!, :eig_pullback!, :eigh_pullback!)
4151
end
4252
return Δt
4353
end
54+
@eval function MAK.$pullback!(
55+
Δt::AbstractTensorMap, ::Nothing, F, ΔF, inds; kwargs...
56+
)
57+
foreachblock(Δt) do c, (Δb,)
58+
haskey(inds, c) || return nothing
59+
ind = inds[c]
60+
Fc = block.(F, Ref(c))
61+
ΔFc = map(ΔFc -> isnothing(ΔFc) ? nothing : block(ΔFc, c), ΔF)
62+
return MAK.$pullback!(Δb, nothing, Fc, ΔFc, ind; kwargs...)
63+
end
64+
return Δt
65+
end
66+
@eval function MAK.$pullback!(
67+
Δt::AbstractTensorMap, t::AbstractTensorMap, F, ΔF, ::Colon; kwargs...
68+
)
69+
return MAK.$pullback!(Δt, t, F, ΔF, _notrunc_ind(t); kwargs...)
70+
end
71+
@eval function MAK.$pullback!(
72+
Δt::AbstractTensorMap, ::Nothing, F, ΔF; kwargs...
73+
)
74+
return MAK.$pullback!(Δt, nothing, F, ΔF, _notrunc_ind(Δt); kwargs...)
75+
end
76+
@eval function MAK.$pullback!(
77+
Δt::AbstractTensorMap, ::Nothing, F, ΔF, ::Colon; kwargs...
78+
)
79+
return MAK.$pullback!(Δt, nothing, F, ΔF, _notrunc_ind(Δt); kwargs...)
80+
end
4481
end
4582

4683
for pullback_trunc! in (:svd_trunc_pullback!, :eig_trunc_pullback!, :eigh_trunc_pullback!)
@@ -97,3 +134,7 @@ function MAK.remove_svd_gauge_dependence!(
97134
end
98135
return ΔU, ΔVᴴ
99136
end
137+
138+
MAK.has_equal_storage(A::AbstractTensorMap, B::AbstractTensorMap) = A === B
139+
MAK.has_equal_storage(A::AbstractTensorMap, B::SectorVector) = false
140+
MAK.has_equal_storage(A::SectorVector, B::AbstractTensorMap) = false

‎test/Project.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ cuTENSOR = "011b41b2-24ef-40a8-b3eb-fa098493e9e1"
3232

3333
[sources]
3434
TensorKit = {path = ".."}
35+
Enzyme = {url = "https://github.com/EnzymeAD/Enzyme.jl", rev = "ksh/nested-only-entry"}
3536

3637
[compat]
3738
Aqua = "0.6, 0.7, 0.8"
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
is_ci = get(ENV, "CI", "false") == "true"
14+
15+
spacelist = ad_spacelist(fast_tests)
16+
eltypes = (Float64, ComplexF64)
17+
18+
@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])'))
19+
atol = default_tol(T)
20+
rtol = default_tol(T)
21+
22+
@testset "SVD" begin
23+
if !is_ci
24+
S = svd_vals(t)
25+
EnzymeTestUtils.test_reverse(svd_vals, Duplicated, (t, Duplicated); atol, rtol)
26+
27+
USVᴴ = svd_full(t)
28+
ΔUSVᴴ = EnzymeTestUtils.rand_tangent(USVᴴ)
29+
remove_svd_gauge_dependence!(ΔUSVᴴ[1], ΔUSVᴴ[3], USVᴴ...)
30+
EnzymeTestUtils.test_reverse(svd_full, Duplicated, (t, Duplicated); output_tangent = ΔUSVᴴ, atol, rtol)
31+
32+
USVᴴ = svd_compact(t)
33+
ΔUSVᴴ = EnzymeTestUtils.rand_tangent(USVᴴ)
34+
remove_svd_gauge_dependence!(ΔUSVᴴ[1], ΔUSVᴴ[3], USVᴴ...)
35+
EnzymeTestUtils.test_reverse(svd_compact, Duplicated, (t, Duplicated); output_tangent = ΔUSVᴴ, atol, rtol)
36+
end
37+
38+
V_trunc = spacetype(t)(c => min(size(b)...) ÷ 2 for (c, b) in blocks(t))
39+
trunc = truncspace(V_trunc)
40+
alg = MatrixAlgebraKit.select_algorithm(svd_trunc_no_error, t, nothing; trunc)
41+
USVᴴtrunc = svd_trunc_no_error(t, alg)
42+
ΔUSVᴴtrunc = EnzymeTestUtils.rand_tangent(USVᴴtrunc)
43+
remove_svd_gauge_dependence!(ΔUSVᴴtrunc[1], ΔUSVᴴtrunc[3], USVᴴtrunc...)
44+
EnzymeTestUtils.test_reverse(svd_trunc_no_error, Duplicated, (t, Duplicated), (alg, Const); output_tangent = ΔUSVᴴtrunc, atol, rtol)
45+
end
46+
47+
@testset "LQ" begin
48+
EnzymeTestUtils.test_reverse(lq_compact, Duplicated, (t, Duplicated); atol, rtol)
49+
50+
if !is_ci
51+
# lq_full/lq_null requires being careful with gauges
52+
LQ = lq_full(t)
53+
ΔLQ = EnzymeTestUtils.rand_tangent(LQ)
54+
remove_lq_gauge_dependence!(ΔLQ..., t, LQ...)
55+
EnzymeTestUtils.test_reverse(lq_full, Duplicated, (t, Duplicated); output_tangent = ΔLQ, atol, rtol)
56+
57+
Nᴴ = lq_null(t)
58+
Q = lq_compact(t)[2]
59+
ΔNᴴ = EnzymeTestUtils.rand_tangent(Nᴴ)
60+
remove_lq_null_gauge_dependence!(ΔNᴴ, Q, Nᴴ)
61+
EnzymeTestUtils.test_reverse(lq_null, Duplicated, (t, Duplicated); output_tangent = ΔNᴴ, atol, rtol)
62+
end
63+
end
64+
65+
@testset "QR" begin
66+
EnzymeTestUtils.test_reverse(qr_compact, Duplicated, (t, Duplicated); atol, rtol)
67+
68+
if !is_ci
69+
# qr_full/qr_null requires being careful with gauges
70+
QR = qr_full(t)
71+
ΔQR = EnzymeTestUtils.rand_tangent(QR)
72+
remove_qr_gauge_dependence!(ΔQR..., t, QR...)
73+
EnzymeTestUtils.test_reverse(qr_full, Duplicated, (t, Duplicated); output_tangent = ΔQR, atol, rtol)
74+
75+
N = qr_null(t)
76+
Q = qr_compact(t)[1]
77+
ΔN = EnzymeTestUtils.rand_tangent(N)
78+
remove_qr_null_gauge_dependence!(ΔN, t, N)
79+
EnzymeTestUtils.test_reverse(qr_null, Duplicated, (t, Duplicated); atol, rtol, output_tangent = ΔN)
80+
end
81+
end
82+
end
83+
84+
@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]))
85+
atol = default_tol(T)
86+
rtol = default_tol(T)
87+
88+
@testset "EIG" begin
89+
if !is_ci
90+
DV = eig_full(t)
91+
ΔDV = EnzymeTestUtils.rand_tangent(DV)
92+
remove_eig_gauge_dependence!(ΔDV[2], DV...)
93+
EnzymeTestUtils.test_reverse(eig_full, Duplicated, (t, Duplicated); output_tangent = ΔDV, atol, rtol)
94+
95+
D = eig_vals(t)
96+
EnzymeTestUtils.test_reverse(eig_vals, Duplicated, (t, Duplicated); atol, rtol)
97+
end
98+
99+
V_trunc = spacetype(t)(c => min(size(b)...) ÷ 2 for (c, b) in blocks(t))
100+
trunc = truncspace(V_trunc)
101+
alg = MatrixAlgebraKit.select_algorithm(eig_trunc_no_error, t, nothing; trunc)
102+
DVtrunc = eig_trunc_no_error(t, alg)
103+
ΔDVtrunc = EnzymeTestUtils.rand_tangent(DVtrunc)
104+
remove_eig_gauge_dependence!(ΔDVtrunc[2], DVtrunc...)
105+
EnzymeTestUtils.test_reverse(eig_trunc_no_error, Duplicated, (t, Duplicated), (alg, Const); output_tangent = ΔDVtrunc, atol, rtol)
106+
end
107+
108+
@testset "EIGH" begin
109+
th = project_hermitian(t)
110+
if !is_ci
111+
DV = eigh_full(th)
112+
ΔDV = EnzymeTestUtils.rand_tangent(DV)
113+
remove_eigh_gauge_dependence!(ΔDV[2], DV...)
114+
proj_eigh_full(t) = eigh_full(project_hermitian(t))
115+
EnzymeTestUtils.test_reverse(proj_eigh_full, Duplicated, (th, Duplicated); output_tangent = ΔDV, atol, rtol)
116+
117+
D = eigh_vals(th)
118+
EnzymeTestUtils.test_reverse(eigh_vals ∘ project_hermitian, Duplicated, (th, Duplicated); atol, rtol)
119+
end
120+
121+
V_trunc = spacetype(th)(c => min(size(b)...) ÷ 2 for (c, b) in blocks(t))
122+
trunc = truncspace(V_trunc)
123+
alg = MatrixAlgebraKit.select_algorithm(eigh_trunc_no_error, th, nothing; trunc)
124+
DVtrunc = eigh_trunc_no_error(th, alg)
125+
ΔDVtrunc = EnzymeTestUtils.rand_tangent(DVtrunc)
126+
remove_eigh_gauge_dependence!(ΔDVtrunc[2], DVtrunc...)
127+
proj_eigh(t, alg) = eigh_trunc_no_error(project_hermitian(t), alg)
128+
EnzymeTestUtils.test_reverse(proj_eigh, Duplicated, (th, Duplicated), (alg, Const); output_tangent = ΔDVtrunc, atol, rtol)
129+
end
130+
131+
@testset "Projections" begin
132+
EnzymeTestUtils.test_reverse(project_hermitian, Duplicated, (t, Duplicated); atol, rtol)
133+
EnzymeTestUtils.test_reverse(project_antihermitian, Duplicated, (t, Duplicated); atol, rtol)
134+
EnzymeTestUtils.test_reverse(project_hermitian!, Duplicated, (t, Duplicated); atol, rtol)
135+
EnzymeTestUtils.test_reverse(project_antihermitian!, Duplicated, (t, Duplicated); atol, rtol)
136+
end
137+
end

0 commit comments

Comments
 (0)