Skip to content

Commit 7ee83b5

Browse files
committed
Move index manipulations up and fix typos
1 parent 70498e0 commit 7ee83b5

9 files changed

Lines changed: 55 additions & 59 deletions

File tree

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

test/enzyme-planaroperations/planarcontract.jl

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,44 @@ eltypes = (Float64, ComplexF64)
1111
@timedtestset "Enzyme - PlanarOperations (planarcontract): $(TensorKit.type_repr(sectortype(eltype(V)))) ($T)" for V in spacelist, T in eltypes
1212
atol = default_tol(T)
1313
rtol = default_tol(T)
14-
for _ in 1:5
15-
d = 0
16-
local V1, V2, V3, k1, k2, k3
17-
# retry a couple times to make sure there are at least some nonzero elements
18-
for _ in 1:10
19-
k1 = rand(0:3)
20-
k2 = rand(0:2)
21-
k3 = rand(0:2)
22-
V1 = prod(v -> rand(Bool) ? v' : v, rand(V, k1); init = one(V[1]))
23-
V2 = prod(v -> rand(Bool) ? v' : v, rand(V, k2); init = one(V[1]))
24-
V3 = prod(v -> rand(Bool) ? v' : v, rand(V, k3); init = one(V[1]))
25-
d = min(dim(V1 V2), dim(V1' V2), dim(V2 V3), dim(V2' V3))
26-
d > 1 && break
27-
end
28-
k′ = rand(0:(k1 + k2))
29-
pA = randcircshift(k′, k1 + k2 - k′, k1)
30-
ipA = _repartition(invperm(linearize(pA)), k′)
31-
k′ = rand(0:(k2 + k3))
32-
pB = randcircshift(k′, k2 + k3 - k′, k2)
33-
ipB = _repartition(invperm(linearize(pB)), k′)
34-
# TODO: primal value already is broken for this?
35-
# pAB = randcircshift(k1, k3)
36-
pAB = _repartition(tuple((1:(k1 + k3))...), k1)
14+
d = 0
15+
local V1, V2, V3, k1, k2, k3
16+
# retry a couple times to make sure there are at least some nonzero elements
17+
for _ in 1:10
18+
k1 = rand(0:3)
19+
k2 = rand(0:2)
20+
k3 = rand(0:2)
21+
V1 = prod(v -> rand(Bool) ? v' : v, rand(V, k1); init = one(V[1]))
22+
V2 = prod(v -> rand(Bool) ? v' : v, rand(V, k2); init = one(V[1]))
23+
V3 = prod(v -> rand(Bool) ? v' : v, rand(V, k3); init = one(V[1]))
24+
d = min(dim(V1 V2), dim(V1' V2), dim(V2 V3), dim(V2' V3))
25+
d > 1 && break
26+
end
27+
k′ = rand(0:(k1 + k2))
28+
pA = randcircshift(k′, k1 + k2 - k′, k1)
29+
ipA = _repartition(invperm(linearize(pA)), k′)
30+
k′ = rand(0:(k2 + k3))
31+
pB = randcircshift(k′, k2 + k3 - k′, k2)
32+
ipB = _repartition(invperm(linearize(pB)), k′)
33+
# TODO: primal value already is broken for this?
34+
# pAB = randcircshift(k1, k3)
35+
pAB = _repartition(tuple((1:(k1 + k3))...), k1)
3736

38-
α = randn(T)
39-
β = randn(T)
37+
α = randn(T)
38+
β = randn(T)
4039

41-
A = randn(T, permute(V1 V2, ipA))
42-
B = randn(T, permute(V2 V3, ipB))
43-
C = randn!(
44-
TensorOperations.tensoralloc_contract(
45-
T, A, pA, false, B, pB, false, pAB, Val(false)
46-
)
40+
A = randn(T, permute(V1 V2, ipA))
41+
B = randn(T, permute(V2 V3, ipB))
42+
C = randn!(
43+
TensorOperations.tensoralloc_contract(
44+
T, A, pA, false, B, pB, false, pAB, Val(false)
4745
)
48-
for TC in (Duplicated,), TA in (Duplicated,), TB in (Duplicated,)
49-
EnzymeTestUtils.test_reverse(TensorKit.planarcontract!, TC, (C, TC), (A, TA), (pA, Const), (B, TB), (pB, Const), (pAB, Const), (One(), Const), (Zero(), Const); atol, rtol)
50-
EnzymeTestUtils.test_reverse(TensorKit.planarcontract!, TC, (C, TC), (A, TA), (pA, Const), (B, TB), (pB, Const), (pAB, Const), (α, Const), (β, Const); atol, rtol)
51-
EnzymeTestUtils.test_reverse(TensorKit.planarcontract!, TC, (C, TC), (A, TA), (pA, Const), (B, TB), (pB, Const), (pAB, Const), (α, Const), (β, Active); atol, rtol)
52-
EnzymeTestUtils.test_reverse(TensorKit.planarcontract!, TC, (C, TC), (A, TA), (pA, Const), (B, TB), (pB, Const), (pAB, Const), (α, Active), (β, Const); atol, rtol)
53-
EnzymeTestUtils.test_reverse(TensorKit.planarcontract!, TC, (C, TC), (A, TA), (pA, Const), (B, TB), (pB, Const), (pAB, Const), (α, Active), (β, Active); atol, rtol)
54-
end
46+
)
47+
for TC in (Duplicated,), TA in (Duplicated,), TB in (Duplicated,)
48+
EnzymeTestUtils.test_reverse(TensorKit.planarcontract!, TC, (C, TC), (A, TA), (pA, Const), (B, TB), (pB, Const), (pAB, Const), (One(), Const), (Zero(), Const); atol, rtol)
49+
EnzymeTestUtils.test_reverse(TensorKit.planarcontract!, TC, (C, TC), (A, TA), (pA, Const), (B, TB), (pB, Const), (pAB, Const), (α, Const), (β, Const); atol, rtol)
50+
EnzymeTestUtils.test_reverse(TensorKit.planarcontract!, TC, (C, TC), (A, TA), (pA, Const), (B, TB), (pB, Const), (pAB, Const), (α, Const), (β, Active); atol, rtol)
51+
EnzymeTestUtils.test_reverse(TensorKit.planarcontract!, TC, (C, TC), (A, TA), (pA, Const), (B, TB), (pB, Const), (pAB, Const), (α, Active), (β, Const); atol, rtol)
52+
EnzymeTestUtils.test_reverse(TensorKit.planarcontract!, TC, (C, TC), (A, TA), (pA, Const), (B, TB), (pB, Const), (pAB, Const), (α, Active), (β, Active); atol, rtol)
5553
end
5654
end

test/enzyme-planaroperations/planartrace.jl

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,25 @@ eltypes = (Float64, ComplexF64)
1111
@timedtestset "Enzyme - PlanarOperations (planartrace): $(TensorKit.type_repr(sectortype(eltype(V)))) ($T)" for V in spacelist, T in eltypes
1212
atol = default_tol(T)
1313
rtol = default_tol(T)
14-
for _ in 1:5
15-
k1 = rand(0:2)
16-
k2 = rand(0:1)
17-
V1 = map(v -> rand(Bool) ? v' : v, rand(V, k1))
18-
V2 = map(v -> rand(Bool) ? v' : v, rand(V, k2))
19-
V3 = prod(x -> x x', V2[1:k2]; init = one(V[1]))
20-
V4 = prod(x -> x x', V2[(k2 + 1):end]; init = one(V[1]))
14+
k1 = rand(0:2)
15+
k2 = rand(0:1)
16+
V1 = map(v -> rand(Bool) ? v' : v, rand(V, k1))
17+
V2 = map(v -> rand(Bool) ? v' : v, rand(V, k2))
18+
V3 = prod(x -> x x', V2[1:k2]; init = one(V[1]))
19+
V4 = prod(x -> x x', V2[(k2 + 1):end]; init = one(V[1]))
2120

22-
k′ = rand(0:(k1 + 2k2))
23-
(_p, _q) = randcircshift(k′, k1 + 2k2 - k′, k1)
24-
p = _repartition(_p, rand(0:k1))
25-
q = (tuple(_q[1:2:end]...), tuple(_q[2:2:end]...))
26-
ip = _repartition(invperm(linearize((_p, _q))), k′)
27-
A = randn(T, permute(prod(V1) V3 V4, ip))
21+
k′ = rand(0:(k1 + 2k2))
22+
(_p, _q) = randcircshift(k′, k1 + 2k2 - k′, k1)
23+
p = _repartition(_p, rand(0:k1))
24+
q = (tuple(_q[1:2:end]...), tuple(_q[2:2:end]...))
25+
ip = _repartition(invperm(linearize((_p, _q))), k′)
26+
A = randn(T, permute(prod(V1) V3 V4, ip))
2827

29-
α = randn(T)
30-
β = randn(T)
31-
C = randn!(TensorOperations.tensoralloc_add(T, A, p, false, Val(false)))
32-
EnyzmeTestUtils.test_reverse(TensorKit.planartrace!, Active, (C, Duplicated), (A, Duplicated), (p, Const), (q, Const), (α, Const), (β, Const), (TensorOperations.DefaultBackend(), Const), (TensorOperations.DefaultAllocator(), Const); atol, rtol)
33-
EnyzmeTestUtils.test_reverse(TensorKit.planartrace!, Active, (C, Duplicated), (A, Duplicated), (p, Const), (q, Const), (α, Active), (β, Const), (TensorOperations.DefaultBackend(), Const), (TensorOperations.DefaultAllocator(), Const); atol, rtol)
34-
EnyzmeTestUtils.test_reverse(TensorKit.planartrace!, Active, (C, Duplicated), (A, Duplicated), (p, Const), (q, Const), (α, Const), (β, Active), (TensorOperations.DefaultBackend(), Const), (TensorOperations.DefaultAllocator(), Const); atol, rtol)
35-
EnyzmeTestUtils.test_reverse(TensorKit.planartrace!, Active, (C, Duplicated), (A, Duplicated), (p, Const), (q, Const), (α, Active), (β, Active), (TensorOperations.DefaultBackend(), Const), (TensorOperations.DefaultAllocator(), Const); atol, rtol)
36-
end
28+
α = randn(T)
29+
β = randn(T)
30+
C = randn!(TensorOperations.tensoralloc_add(T, A, p, false, Val(false)))
31+
EnzymeTestUtils.test_reverse(TensorKit.planartrace!, Active, (C, Duplicated), (A, Duplicated), (p, Const), (q, Const), (α, Const), (β, Const), (TensorOperations.DefaultBackend(), Const), (TensorOperations.DefaultAllocator(), Const); atol, rtol)
32+
EnzymeTestUtils.test_reverse(TensorKit.planartrace!, Active, (C, Duplicated), (A, Duplicated), (p, Const), (q, Const), (α, Active), (β, Const), (TensorOperations.DefaultBackend(), Const), (TensorOperations.DefaultAllocator(), Const); atol, rtol)
33+
EnzymeTestUtils.test_reverse(TensorKit.planartrace!, Active, (C, Duplicated), (A, Duplicated), (p, Const), (q, Const), (α, Const), (β, Active), (TensorOperations.DefaultBackend(), Const), (TensorOperations.DefaultAllocator(), Const); atol, rtol)
34+
EnzymeTestUtils.test_reverse(TensorKit.planartrace!, Active, (C, Duplicated), (A, Duplicated), (p, Const), (q, Const), (α, Active), (β, Active), (TensorOperations.DefaultBackend(), Const), (TensorOperations.DefaultAllocator(), Const); atol, rtol)
3735
end

0 commit comments

Comments
 (0)