Skip to content

Commit c19f94c

Browse files
kshyattKatharine Hyatt
andauthored
Combining the mul/planarcontract tests (#500)
* Try combining the mul/planarcontract tests * Include TO * Dumb typo * Combine planartrace with trace_permute * Update test/Project.toml Try the new Enzyme branch * Get rid of unnecessary source in test/Project.toml --------- Co-authored-by: Katharine Hyatt <katharine.s.hyatt@gmail.com>
1 parent 3e1b0fb commit c19f94c

4 files changed

Lines changed: 94 additions & 164 deletions

File tree

‎test/enzyme-linalg/mul.jl‎

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Test, TestExtras
22
using TensorKit
3+
using TensorOperations
34
using VectorInterface
45
using Enzyme, EnzymeTestUtils
56
using Random
@@ -9,61 +10,81 @@ eltypes = (Float64, ComplexF64)
910

1011
is_ci = get(ENV, "CI", "false") == "true"
1112

12-
@timedtestset verbose = true "Enzyme - LinearAlgebra (mul):" begin
13+
rTs(::Number, is_ci::Bool) = is_ci ? (Active,) : (Active, Const)
14+
rTs(::Zero, is_ci::Bool) = (Const,)
15+
rTs(::One, is_ci::Bool) = (Const,)
16+
fTs(::Number, is_ci::Bool) = is_ci ? (Duplicated,) : (Duplicated, Const)
17+
fTs(::Zero, is_ci::Bool) = (Const,)
18+
fTs(::One, is_ci::Bool) = (Const,)
19+
20+
@timedtestset verbose = true "Enzyme - LinearAlgebra (mul) and planarcontract!:" begin
1321
@timedtestset "$(TensorKit.type_repr(sectortype(eltype(V)))) ($T)" for V in spacelist, T in eltypes
1422
atol = default_tol(T)
1523
rtol = default_tol(T)
1624

17-
# see https://github.com/QuantumKitHub/TensorKit.jl/issues/457
18-
@static if VERSION < v"1.11.0-rc"
19-
C = randn(T, V[1] ⊗ V[2] ← (V[4] ⊗ V[5])')
20-
else
21-
C = randn(T, V[1] ⊗ V[2] ← (V[3] ⊗ V[4] ⊗ V[5])')
22-
end
23-
A = randn(T, codomain(C) ← V[5]' ⊗ V[4]')
24-
B = randn(T, domain(A) ← domain(C))
2525
zero_αβs = ((Zero(), Zero()), (randn(T), Zero()), (Zero(), randn(T)))
2626
αβs = !is_ci ? vcat(zero_αβs..., (randn(T), randn(T))) : ((randn(T), randn(T)),)
27-
for TC in (Duplicated,), TA in (Duplicated,), TB in (Duplicated,)
28-
for (α, β) in αβs
29-
rTαs = if α === Zero()
30-
(Const,)
31-
elseif !is_ci
32-
(Active, Const)
33-
else
34-
(Active,)
27+
@timedtestset "mul" begin
28+
C = randn(T, V[1] ⊗ V[2] ← (V[3] ⊗ V[4] ⊗ V[5])')
29+
A = randn(T, codomain(C) ← V[5]' ⊗ V[4]')
30+
B = randn(T, domain(A) ← domain(C))
31+
for TC in (Duplicated,), TA in (Duplicated,), TB in (Duplicated,)
32+
for (α, β) in αβs
33+
rTαs = rTs(α, is_ci)
34+
rTβs = rTs(β, is_ci)
35+
fTαs = fTs(α, is_ci)
36+
fTβs = fTs(β, is_ci)
37+
for Tα in rTαs, Tβ in rTβs
38+
EnzymeTestUtils.test_reverse(mul!, TC, (C, TC), (A, TA), (B, TB), (α, Tα), (β, Tβ); atol, rtol, testset_name = "mul! reverse Tα $Tα, Tβ $Tβ")
39+
end
40+
for Tα in fTαs, Tβ in fTβs
41+
EnzymeTestUtils.test_forward(mul!, TC, (C, TC), (A, TA), (B, TB), (α, Tα), (β, Tβ); atol, rtol, testset_name = "mul! forward Tα $Tα, Tβ $Tβ")
42+
end
3543
end
36-
rTβs = if β === Zero()
37-
(Const,)
38-
elseif !is_ci
39-
(Active, Const)
40-
else
41-
(Active,)
44+
if !is_ci
45+
EnzymeTestUtils.test_reverse(mul!, TC, (C, TC), (A, TA), (B, TB); atol, rtol, testset_name = "mul! reverse no α no β")
46+
EnzymeTestUtils.test_forward(mul!, TC, (C, TC), (A, TA), (B, TB); atol, rtol, testset_name = "mul! forward no α no β")
4247
end
48+
end
49+
end
50+
@timedtestset "planarcontract!" begin
51+
V1, V2, V3, V4, V5 = V
52+
k1 = 3
53+
k2 = 2
54+
k3 = 3
55+
k′ = rand(0:(k1 + k2))
56+
pA = randcircshift(k′, k1 + k2 - k′, k1)
57+
ipA = _repartition(invperm(linearize(pA)), k′)
58+
k′ = rand(0:(k2 + k3))
59+
pB = randcircshift(k′, k2 + k3 - k′, k2)
60+
ipB = _repartition(invperm(linearize(pB)), k′)
61+
# TODO: primal value already is broken for this?
62+
# pAB = randcircshift(k1, k3)
63+
pAB = _repartition(tuple((1:(k1 + k3))...), k1)
64+
65+
α_ = randn(T)
66+
β_ = randn(T)
67+
68+
A = randn(T, permute(V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)', ipA))
69+
B = randn(T, permute((V4 ⊗ V5)' ← V1 ⊗ V2 ⊗ V3, ipB))
70+
C = randn!(
71+
TensorOperations.tensoralloc_contract(
72+
T, A, pA, false, B, pB, false, pAB, Val(false)
73+
)
74+
)
75+
αβs = !is_ci ? ((One(), Zero()), (α_, β_)) : ((α_, β_),)
76+
@testset for TC in (Duplicated,), TA in (Duplicated,), TB in (Duplicated,), (α, β) in αβs
77+
rTαs = rTs(α, is_ci)
78+
rTβs = rTs(β, is_ci)
79+
fTαs = fTs(α, is_ci)
80+
fTβs = fTs(β, is_ci)
4381
for Tα in rTαs, Tβ in rTβs
44-
EnzymeTestUtils.test_reverse(mul!, TC, (C, TC), (A, TA), (B, TB), (α, Tα), (β, Tβ); atol, rtol, testset_name = "mul! reverse Tα $Tα, Tβ $Tβ")
45-
end
46-
fTαs = if α === Zero()
47-
(Const,)
48-
elseif !is_ci
49-
(Duplicated, Const)
50-
else
51-
(Duplicated,)
82+
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β")
5283
end
53-
fTβs = if β === Zero()
54-
(Const,)
55-
elseif !is_ci
56-
(Duplicated, Const)
57-
else
58-
(Duplicated,)
59-
end
60-
for Tα in fTαs, Tβ in fTβs
61-
EnzymeTestUtils.test_forward(mul!, TC, (C, TC), (A, TA), (B, TB), (α, Tα), (β, Tβ); atol, rtol, testset_name = "mul! forward Tα $Tα, Tβ $Tβ")
62-
end
63-
end
64-
if !is_ci
65-
EnzymeTestUtils.test_reverse(mul!, TC, (C, TC), (A, TA), (B, TB); atol, rtol, testset_name = "mul! reverse no α no β")
66-
EnzymeTestUtils.test_forward(mul!, TC, (C, TC), (A, TA), (B, TB); atol, rtol, testset_name = "mul! forward no α no β")
84+
# TODO broken internally in Enzyme?
85+
#for Tα in fTαs, Tβ in fTβs
86+
# 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β")
87+
#end
6788
end
6889
end
6990
end

‎test/enzyme-planaroperations/planarcontract.jl‎

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

‎test/enzyme-planaroperations/planartrace.jl‎

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

‎test/enzyme-vi-to/trace.jl‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,33 @@ TAs = is_ci ? (Duplicated,) : (Const, Duplicated)
5656
end
5757
end
5858
end
59+
@timedtestset "planartrace!" begin
60+
for _ in 1:5
61+
k1 = rand(0:2)
62+
k2 = rand(0:1)
63+
V1 = map(v -> rand(Bool) ? v' : v, rand(V, k1))
64+
V2 = map(v -> rand(Bool) ? v' : v, rand(V, k2))
65+
V3 = prod(x -> x ⊗ x', V2[1:k2]; init = one(V[1]))
66+
V4 = prod(x -> x ⊗ x', V2[(k2 + 1):end]; init = one(V[1]))
67+
68+
k′ = rand(0:(k1 + 2k2))
69+
(_p, _q) = randcircshift(k′, k1 + 2k2 - k′, k1)
70+
p = _repartition(_p, rand(0:k1))
71+
q = (tuple(_q[1:2:end]...), tuple(_q[2:2:end]...))
72+
if !all(isempty, p) && !all(isempty, q)
73+
α = randn(T)
74+
β = randn(T)
75+
ip = _repartition(invperm(linearize((_p, _q))), k′)
76+
A = randn(T, permute(prod(V1) ⊗ V3 ← V4, ip))
77+
C = randn!(TensorOperations.tensoralloc_add(T, A, p, false, Val(false)))
78+
for Tα in (Const, Active), Tβ in (Const, Active)
79+
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β")
80+
end
81+
for Tα in (Const, Duplicated), Tβ in (Const, Duplicated)
82+
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β")
83+
end
84+
end
85+
end
86+
end
5987
end
6088
end

0 commit comments

Comments
 (0)