@@ -9,61 +9,81 @@ eltypes = (Float64, ComplexF64)
99
1010is_ci = get (ENV , " CI" , " false" ) == " true"
1111
12- @timedtestset verbose = true " Enzyme - LinearAlgebra (mul):" begin
12+ rTs (:: Number , is_ci:: Bool ) = is_ci ? (Active,) : (Active, Const)
13+ rTs (:: Zero , is_ci:: Bool ) = (Const,)
14+ rTs (:: One , is_ci:: Bool ) = (Const,)
15+ fTs (:: Number , is_ci:: Bool ) = is_ci ? (Duplicated,) : (Duplicated, Const)
16+ fTs (:: Zero , is_ci:: Bool ) = (Const,)
17+ fTs (:: One , is_ci:: Bool ) = (Const,)
18+
19+ @timedtestset verbose = true " Enzyme - LinearAlgebra (mul) and planarcontract!:" begin
1320 @timedtestset " $(TensorKit. type_repr (sectortype (eltype (V)))) ($T )" for V in spacelist, T in eltypes
1421 atol = default_tol (T)
1522 rtol = default_tol (T)
1623
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))
2524 zero_αβs = ((Zero (), Zero ()), (randn (T), Zero ()), (Zero (), randn (T)))
2625 αβ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,)
26+ @timedtestset " mul" begin
27+ C = randn (T, V[1 ] ⊗ V[2 ] ← (V[3 ] ⊗ V[4 ] ⊗ V[5 ])' )
28+ A = randn (T, codomain (C) ← V[5 ]' ⊗ V[4 ]' )
29+ B = randn (T, domain (A) ← domain (C))
30+ for TC in (Duplicated,), TA in (Duplicated,), TB in (Duplicated,)
31+ for (α, β) in αβs
32+ rTαs = rTs (α)
33+ rTβs = rTs (β)
34+ fTαs = fTs (α)
35+ fTβs = fTs (β)
36+ for Tα in rTαs, Tβ in rTβs
37+ EnzymeTestUtils. test_reverse (mul!, TC, (C, TC), (A, TA), (B, TB), (α, Tα), (β, Tβ); atol, rtol, testset_name = " mul! reverse Tα $Tα , Tβ $Tβ " )
38+ end
39+ for Tα in fTαs, Tβ in fTβs
40+ EnzymeTestUtils. test_forward (mul!, TC, (C, TC), (A, TA), (B, TB), (α, Tα), (β, Tβ); atol, rtol, testset_name = " mul! forward Tα $Tα , Tβ $Tβ " )
41+ end
3542 end
36- rTβs = if β === Zero ()
37- (Const,)
38- elseif ! is_ci
39- (Active, Const)
40- else
41- (Active,)
43+ if ! is_ci
44+ EnzymeTestUtils. test_reverse (mul!, TC, (C, TC), (A, TA), (B, TB); atol, rtol, testset_name = " mul! reverse no α no β" )
45+ EnzymeTestUtils. test_forward (mul!, TC, (C, TC), (A, TA), (B, TB); atol, rtol, testset_name = " mul! forward no α no β" )
4246 end
47+ end
48+ end
49+ @timedtestset " planarcontract!" begin
50+ V1, V2, V3, V4, V5 = V
51+ k1 = 3
52+ k2 = 2
53+ k3 = 3
54+ k′ = rand (0 : (k1 + k2))
55+ pA = randcircshift (k′, k1 + k2 - k′, k1)
56+ ipA = _repartition (invperm (linearize (pA)), k′)
57+ k′ = rand (0 : (k2 + k3))
58+ pB = randcircshift (k′, k2 + k3 - k′, k2)
59+ ipB = _repartition (invperm (linearize (pB)), k′)
60+ # TODO : primal value already is broken for this?
61+ # pAB = randcircshift(k1, k3)
62+ pAB = _repartition (tuple ((1 : (k1 + k3)). .. ), k1)
63+
64+ α_ = randn (T)
65+ β_ = randn (T)
66+
67+ A = randn (T, permute (V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)' , ipA))
68+ B = randn (T, permute ((V4 ⊗ V5)' ← V1 ⊗ V2 ⊗ V3, ipB))
69+ C = randn! (
70+ TensorOperations. tensoralloc_contract (
71+ T, A, pA, false , B, pB, false , pAB, Val (false )
72+ )
73+ )
74+ αβs = ! is_ci ? ((One (), Zero ()), (α_, β_)) : ((α_, β_),)
75+ @testset for TC in (Duplicated,), TA in (Duplicated,), TB in (Duplicated,), (α, β) in αβs
76+ rTαs = rTs (α)
77+ rTβs = rTs (β)
78+ fTαs = fTs (α)
79+ fTβs = fTs (β)
4380 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,)
81+ 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β " )
5282 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 β" )
83+ # TODO broken internally in Enzyme?
84+ # for Tα in fTαs, Tβ in fTβs
85+ # 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β")
86+ # end
6787 end
6888 end
6989 end
0 commit comments