Skip to content

Commit e852e6a

Browse files
kshyattKatharine Hyatt
andauthored
Reverse/forward Enzyme rules, refactoring, and tests for planar ops (#474)
* Reverse Enzyme rules, refactoring, and tests for planar ops * Fix bad linalg rule * Remove unneeded files * Remove duplicated lines * Apply suggestions * Can we avoid marking FusionTreePair inactive --------- Co-authored-by: Katharine Hyatt <katharine.s.hyatt@gmail.com>
1 parent a6b6667 commit e852e6a

10 files changed

Lines changed: 139 additions & 109 deletions

File tree

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Adapt = "4"
4848
CUDA = "6"
4949
ChainRulesCore = "1"
5050
Dictionaries = "0.4"
51-
Enzyme = "0.13.157"
51+
Enzyme = "0.13.195"
5252
EnzymeTestUtils = "0.2.8"
5353
FiniteDifferences = "0.12"
5454
GPUArrays = "11.4.1"

‎ext/TensorKitEnzymeExt/linalg.jl‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ function EnzymeRules.augmented_primal(
1111
α::Annotation,
1212
β::Annotation,
1313
) where {RT}
14-
cacheC = !isa(β, Const) && copy(C.val)
15-
cacheA = !isa(B, Const) && EnzymeRules.overwritten(config)[3] ? copy(A.val) : nothing
16-
cacheB = !isa(A, Const) && EnzymeRules.overwritten(config)[4] ? copy(B.val) : nothing
14+
cacheC = !isa(β, Const) ? copy(C.val) : C.val
15+
cacheA = !isa(B, Const) && EnzymeRules.overwritten(config)[3] ? copy(A.val) : A.val
16+
cacheB = !isa(A, Const) && EnzymeRules.overwritten(config)[4] ? copy(B.val) : B.val
1717
AB = if !isa(α, Const)
1818
AB = A.val * B.val
1919
add!(C.val, AB, α.val, β.val)
@@ -39,16 +39,7 @@ function EnzymeRules.reverse(
3939
α::Annotation{<:Number},
4040
β::Annotation{<:Number},
4141
) where {RT}
42-
if RT <: Const
43-
Δα = isa(α, Const) ? nothing : zero(α.val)
44-
Δβ = isa(β, Const) ? nothing : zero(β.val)
45-
return (nothing, nothing, nothing, Δα, Δβ)
46-
end
47-
cacheC, cacheA, cacheB, AB = cache
48-
Cval = something(cacheC, C.val)
49-
Aval = something(cacheA, A.val)
50-
Bval = something(cacheB, B.val)
51-
42+
Cval, Aval, Bval, AB = cache
5243
!isa(A, Const) && !isa(C, Const) && TK.project_mul!(A.dval, C.dval, Bval', conj(α.val), One())
5344
!isa(B, Const) && !isa(C, Const) && TK.project_mul!(B.dval, Aval', C.dval, conj(α.val), One())
5445
Δαr = pullback_dα(α, C, AB)

‎ext/TensorKitEnzymeExt/utility.jl‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ pullback_dC!(ΔC, β::Number) = scale!(ΔC, conj(β))
1515
# Ignore derivatives
1616
# ------------------
1717

18+
@inline EnzymeRules.inactive_type(::Type{TensorKit.GenericFusion}) = true
19+
@inline EnzymeRules.inactive_type(::Type{TensorKit.UniqueFusion}) = true
20+
@inline EnzymeRules.inactive_type(::Type{TensorKit.MultipleFusion}) = true
21+
@inline EnzymeRules.inactive_type(::Type{TensorKit.SimpleFusion}) = true
22+
@inline EnzymeRules.inactive_type(::Type{TensorKit.MultiplicityFreeFusion}) = true
1823
@inline EnzymeRules.inactive_type(::Type{<:TensorKit.FusionTree}) = true
24+
@inline EnzymeRules.inactive_type(::Type{<:TensorKit.FusionTreeDict}) = true
25+
@inline EnzymeRules.inactive_type(::Type{<:TensorKit.FusionTreeBlock}) = true
1926
@inline EnzymeRules.inactive_type(::Type{<:TensorKit.GenericTreeTransformer}) = true
2027
@inline EnzymeRules.inactive_type(::Type{<:TensorKit.VectorSpace}) = true
28+
@inline EnzymeRules.inactive_type(::Type{<:TensorKit.LRU}) = true
2129

2230
function EnzymeRules.augmented_primal(
2331
config::EnzymeRules.RevConfigWidth{1},
@@ -34,7 +42,7 @@ function EnzymeRules.augmented_primal(
3442
else
3543
nothing
3644
end
37-
return EnzymeRules.AugmentedReturn(ret, dret, dret)
45+
return EnzymeRules.AugmentedReturn(ret, dret, nothing)
3846
end
3947

4048
function EnzymeRules.reverse(
@@ -129,6 +137,10 @@ function EnzymeRules.forward(
129137
end
130138
end
131139

140+
@inline EnzymeRules.inactive(::typeof(TensorKit.planar_trace), ::TensorKit.FusionTreePair, ::TensorKit.Index2Tuple, ::TensorKit.Index2Tuple) = nothing
141+
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontrees), ::Any) = nothing
142+
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontrees), ::Any, ::Any) = nothing
143+
@inline EnzymeRules.inactive(::typeof(TensorKit.fusiontrees), ::Any, ::Any, ::Any) = nothing
132144
@inline EnzymeRules.inactive(::typeof(TensorKit.fsbraid), ::Any) = nothing
133145
@inline EnzymeRules.inactive(::typeof(TensorKit.fsbraid), ::Any, ::Any) = nothing
134146
@inline EnzymeRules.inactive(::typeof(TensorKit.artin_braid), ::Any, ::Any) = nothing

‎ext/TensorKitMooncakeExt/TensorKitMooncakeExt.jl‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ include("linalg.jl")
1919
include("indexmanipulations.jl")
2020
include("vectorinterface.jl")
2121
include("tensoroperations.jl")
22-
include("planaroperations.jl")
2322
include("factorizations.jl")
2423

2524
end

‎ext/TensorKitMooncakeExt/planaroperations.jl‎

Lines changed: 0 additions & 92 deletions
This file was deleted.

‎ext/TensorKitMooncakeExt/utility.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Mooncake.tangent_type(::Type{<:HomSpace}) = Mooncake.NoTangent
2020

2121
@zero_derivative DefaultCtx Tuple{typeof(TensorOperations.tensorcontract_structure), AbstractTensorMap, Index2Tuple, Bool, AbstractTensorMap, Index2Tuple, Bool, Index2Tuple}
2222

23+
@zero_derivative DefaultCtx Tuple{typeof(TensorKit.planar_trace), TensorKit.FusionTreePair, Index2Tuple, Index2Tuple}
2324
@zero_derivative DefaultCtx Tuple{typeof(TensorKit.has_shared_permute), AbstractTensorMap, Index2Tuple}
2425
@zero_derivative DefaultCtx Tuple{typeof(TensorKit.select), HomSpace, Index2Tuple}
2526
@zero_derivative DefaultCtx Tuple{typeof(TensorKit.flip), HomSpace, Any}

‎src/TensorKit.jl‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ include("planar/planaroperations.jl")
285285
# ------------------------
286286
include("auxiliary/ad.jl")
287287
include("pullbacks/tensoroperations.jl")
288-
289288
include("pullbacks/indexmanipulations.jl")
290289

291290
end

‎test/Project.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ ParallelTestRunner = "2"
4343
Test = "1"
4444
TestExtras = "0.2,0.3"
4545
Zygote = "0.7"
46+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using Test, TestExtras
2+
using TensorKit
3+
using TensorOperations
4+
using VectorInterface: Zero, One
5+
using Enzyme, EnzymeTestUtils
6+
using Random
7+
8+
is_ci = get(ENV, "CI", "false") == "true"
9+
10+
spacelist = ad_spacelist(fast_tests)
11+
eltypes = (Float64, ComplexF64)
12+
13+
@timedtestset "Enzyme - PlanarOperations (planarcontract): $(TensorKit.type_repr(sectortype(eltype(V)))) ($T)" for V in spacelist, T in eltypes
14+
atol = default_tol(T)
15+
rtol = default_tol(T)
16+
V1, V2, V3, V4, V5 = V
17+
k1 = 3
18+
k2 = 2
19+
k3 = 3
20+
k′ = rand(0:(k1 + k2))
21+
pA = randcircshift(k′, k1 + k2 - k′, k1)
22+
ipA = _repartition(invperm(linearize(pA)), k′)
23+
k′ = rand(0:(k2 + k3))
24+
pB = randcircshift(k′, k2 + k3 - k′, k2)
25+
ipB = _repartition(invperm(linearize(pB)), k′)
26+
# TODO: primal value already is broken for this?
27+
# pAB = randcircshift(k1, k3)
28+
pAB = _repartition(tuple((1:(k1 + k3))...), k1)
29+
30+
α_ = randn(T)
31+
β_ = randn(T)
32+
33+
A = randn(T, permute(V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)', ipA))
34+
B = randn(T, permute((V4 ⊗ V5)' ← V1 ⊗ V2 ⊗ V3, ipB))
35+
C = randn!(
36+
TensorOperations.tensoralloc_contract(
37+
T, A, pA, false, B, pB, false, pAB, Val(false)
38+
)
39+
)
40+
αβs = !is_ci ? ((One(), Zero()), (α_, β_)) : ((α_, β_),)
41+
@testset for TC in (Duplicated,), TA in (Duplicated,), TB in (Duplicated,), (α, β) in αβs
42+
rTαs = if (α === Zero() || α === One())
43+
(Const,)
44+
elseif !is_ci
45+
(Active, Const)
46+
else
47+
(Active,)
48+
end
49+
rTβs = if (β === Zero() || β === One())
50+
(Const,)
51+
elseif !is_ci
52+
(Active, Const)
53+
else
54+
(Active,)
55+
end
56+
for Tα in rTαs, Tβ in rTβs
57+
EnzymeTestUtils.test_reverse(TensorKit.planarcontract!, TC, (C, TC), (A, TA), (pA, Const), (B, TB), (pB, Const), (pAB, Const), (α, Tα), (β, Tβ); atol, rtol, testset_name = "planarcontract! Tα = $Tα, Tβ = $Tβ")
58+
end
59+
fTαs = if (α === Zero() || α === One())
60+
(Const,)
61+
elseif !is_ci
62+
(Duplicated, Const)
63+
else
64+
(Duplicated,)
65+
end
66+
fTβs = if (β === Zero() || β === One())
67+
(Const,)
68+
elseif !is_ci
69+
(Duplicated, Const)
70+
else
71+
(Duplicated,)
72+
end
73+
# TODO broken internally in Enzyme?
74+
#for Tα in fTαs, Tβ in fTβs
75+
# EnzymeTestUtils.test_forward(TensorKit.planarcontract!, TC, (C, TC), (A, TA), (pA, Const), (B, TB), (pB, Const), (pAB, Const), (α, Tα), (β, Tβ); atol, rtol, testset_name = "planarcontract! Tα = $Tα, Tβ = $Tβ")
76+
#end
77+
end
78+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Test, TestExtras
2+
using TensorKit
3+
using TensorOperations
4+
using VectorInterface: Zero, One
5+
using Enzyme, EnzymeTestUtils
6+
using Random
7+
8+
spacelist = ad_spacelist(fast_tests)
9+
#eltypes = (Float64, ComplexF64)
10+
eltypes = (ComplexF64,)
11+
12+
@timedtestset "Enzyme - PlanarOperations (planartrace): $(TensorKit.type_repr(sectortype(eltype(V)))) ($T)" for V in spacelist, T in eltypes
13+
atol = default_tol(T)
14+
rtol = default_tol(T)
15+
for _ in 1:5
16+
k1 = rand(0:2)
17+
k2 = rand(0:1)
18+
V1 = map(v -> rand(Bool) ? v' : v, rand(V, k1))
19+
V2 = map(v -> rand(Bool) ? v' : v, rand(V, k2))
20+
V3 = prod(x -> x ⊗ x', V2[1:k2]; init = one(V[1]))
21+
V4 = prod(x -> x ⊗ x', V2[(k2 + 1):end]; init = one(V[1]))
22+
23+
k′ = rand(0:(k1 + 2k2))
24+
(_p, _q) = randcircshift(k′, k1 + 2k2 - k′, k1)
25+
p = _repartition(_p, rand(0:k1))
26+
q = (tuple(_q[1:2:end]...), tuple(_q[2:2:end]...))
27+
if !all(isempty, p) && !all(isempty, q)
28+
α = randn(T)
29+
β = randn(T)
30+
ip = _repartition(invperm(linearize((_p, _q))), k′)
31+
A = randn(T, permute(prod(V1) ⊗ V3 ← V4, ip))
32+
C = randn!(TensorOperations.tensoralloc_add(T, A, p, false, Val(false)))
33+
for Tα in (Const, Active), Tβ in (Const, Active)
34+
EnzymeTestUtils.test_reverse(TensorKit.planartrace!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (q, Const), (α, Tα), (β, Tβ), (TensorOperations.DefaultBackend(), Const), (TensorOperations.DefaultAllocator(), Const); atol, rtol, testset_name = "planartrace reverse Tα $Tα Tβ $Tβ")
35+
end
36+
for Tα in (Const, Duplicated), Tβ in (Const, Duplicated)
37+
EnzymeTestUtils.test_forward(TensorKit.planartrace!, Duplicated, (C, Duplicated), (A, Duplicated), (p, Const), (q, Const), (α, Tα), (β, Tβ), (TensorOperations.DefaultBackend(), Const), (TensorOperations.DefaultAllocator(), Const); atol, rtol, testset_name = "planartrace forward Tα $Tα Tβ $Tβ")
38+
end
39+
end
40+
end
41+
end

0 commit comments

Comments
 (0)