11using Test, TestExtras
22using TensorKit
3+ using TensorOperations
34using VectorInterface
45using Enzyme, EnzymeTestUtils
56using Random
@@ -9,61 +10,81 @@ eltypes = (Float64, ComplexF64)
910
1011is_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
0 commit comments