Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 66 additions & 45 deletions test/enzyme-linalg/mul.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Test, TestExtras
using TensorKit
using TensorOperations
using VectorInterface
using Enzyme, EnzymeTestUtils
using Random
Expand All @@ -9,61 +10,81 @@ eltypes = (Float64, ComplexF64)

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

@timedtestset verbose = true "Enzyme - LinearAlgebra (mul):" begin
rTs(::Number, is_ci::Bool) = is_ci ? (Active,) : (Active, Const)
rTs(::Zero, is_ci::Bool) = (Const,)
rTs(::One, is_ci::Bool) = (Const,)
fTs(::Number, is_ci::Bool) = is_ci ? (Duplicated,) : (Duplicated, Const)
fTs(::Zero, is_ci::Bool) = (Const,)
fTs(::One, is_ci::Bool) = (Const,)

@timedtestset verbose = true "Enzyme - LinearAlgebra (mul) and planarcontract!:" begin
@timedtestset "$(TensorKit.type_repr(sectortype(eltype(V)))) ($T)" for V in spacelist, T in eltypes
atol = default_tol(T)
rtol = default_tol(T)

# see https://github.com/QuantumKitHub/TensorKit.jl/issues/457
@static if VERSION < v"1.11.0-rc"
C = randn(T, V[1] ⊗ V[2] ← (V[4] ⊗ V[5])')
else
C = randn(T, V[1] ⊗ V[2] ← (V[3] ⊗ V[4] ⊗ V[5])')
end
A = randn(T, codomain(C) ← V[5]' ⊗ V[4]')
B = randn(T, domain(A) ← domain(C))
zero_αβs = ((Zero(), Zero()), (randn(T), Zero()), (Zero(), randn(T)))
αβs = !is_ci ? vcat(zero_αβs..., (randn(T), randn(T))) : ((randn(T), randn(T)),)
for TC in (Duplicated,), TA in (Duplicated,), TB in (Duplicated,)
for (α, β) in αβs
rTαs = if α === Zero()
(Const,)
elseif !is_ci
(Active, Const)
else
(Active,)
@timedtestset "mul" begin
C = randn(T, V[1] ⊗ V[2] ← (V[3] ⊗ V[4] ⊗ V[5])')
A = randn(T, codomain(C) ← V[5]' ⊗ V[4]')
B = randn(T, domain(A) ← domain(C))
for TC in (Duplicated,), TA in (Duplicated,), TB in (Duplicated,)
for (α, β) in αβs
rTαs = rTs(α, is_ci)
rTβs = rTs(β, is_ci)
fTαs = fTs(α, is_ci)
fTβs = fTs(β, is_ci)
for Tα in rTαs, Tβ in rTβs
EnzymeTestUtils.test_reverse(mul!, TC, (C, TC), (A, TA), (B, TB), (α, Tα), (β, Tβ); atol, rtol, testset_name = "mul! reverse Tα $Tα, Tβ $Tβ")
end
for Tα in fTαs, Tβ in fTβs
EnzymeTestUtils.test_forward(mul!, TC, (C, TC), (A, TA), (B, TB), (α, Tα), (β, Tβ); atol, rtol, testset_name = "mul! forward Tα $Tα, Tβ $Tβ")
end
end
rTβs = if β === Zero()
(Const,)
elseif !is_ci
(Active, Const)
else
(Active,)
if !is_ci
EnzymeTestUtils.test_reverse(mul!, TC, (C, TC), (A, TA), (B, TB); atol, rtol, testset_name = "mul! reverse no α no β")
EnzymeTestUtils.test_forward(mul!, TC, (C, TC), (A, TA), (B, TB); atol, rtol, testset_name = "mul! forward no α no β")
end
end
end
@timedtestset "planarcontract!" begin
V1, V2, V3, V4, V5 = V
k1 = 3
k2 = 2
k3 = 3
k′ = rand(0:(k1 + k2))
pA = randcircshift(k′, k1 + k2 - k′, k1)
ipA = _repartition(invperm(linearize(pA)), k′)
k′ = rand(0:(k2 + k3))
pB = randcircshift(k′, k2 + k3 - k′, k2)
ipB = _repartition(invperm(linearize(pB)), k′)
# TODO: primal value already is broken for this?
# pAB = randcircshift(k1, k3)
pAB = _repartition(tuple((1:(k1 + k3))...), k1)

α_ = randn(T)
β_ = randn(T)

A = randn(T, permute(V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)', ipA))
B = randn(T, permute((V4 ⊗ V5)' ← V1 ⊗ V2 ⊗ V3, ipB))
C = randn!(
TensorOperations.tensoralloc_contract(
T, A, pA, false, B, pB, false, pAB, Val(false)
)
)
αβs = !is_ci ? ((One(), Zero()), (α_, β_)) : ((α_, β_),)
@testset for TC in (Duplicated,), TA in (Duplicated,), TB in (Duplicated,), (α, β) in αβs
rTαs = rTs(α, is_ci)
rTβs = rTs(β, is_ci)
fTαs = fTs(α, is_ci)
fTβs = fTs(β, is_ci)
for Tα in rTαs, Tβ in rTβs
EnzymeTestUtils.test_reverse(mul!, TC, (C, TC), (A, TA), (B, TB), (α, Tα), (β, Tβ); atol, rtol, testset_name = "mul! reverse Tα $Tα, Tβ $Tβ")
end
fTαs = if α === Zero()
(Const,)
elseif !is_ci
(Duplicated, Const)
else
(Duplicated,)
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β")
end
fTβs = if β === Zero()
(Const,)
elseif !is_ci
(Duplicated, Const)
else
(Duplicated,)
end
for Tα in fTαs, Tβ in fTβs
EnzymeTestUtils.test_forward(mul!, TC, (C, TC), (A, TA), (B, TB), (α, Tα), (β, Tβ); atol, rtol, testset_name = "mul! forward Tα $Tα, Tβ $Tβ")
end
end
if !is_ci
EnzymeTestUtils.test_reverse(mul!, TC, (C, TC), (A, TA), (B, TB); atol, rtol, testset_name = "mul! reverse no α no β")
EnzymeTestUtils.test_forward(mul!, TC, (C, TC), (A, TA), (B, TB); atol, rtol, testset_name = "mul! forward no α no β")
# TODO broken internally in Enzyme?
#for Tα in fTαs, Tβ in fTβs
# 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β")
#end
end
end
end
Expand Down
78 changes: 0 additions & 78 deletions test/enzyme-planaroperations/planarcontract.jl

This file was deleted.

41 changes: 0 additions & 41 deletions test/enzyme-planaroperations/planartrace.jl

This file was deleted.

28 changes: 28 additions & 0 deletions test/enzyme-vi-to/trace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,33 @@ TAs = is_ci ? (Duplicated,) : (Const, Duplicated)
end
end
end
@timedtestset "planartrace!" begin
for _ in 1:5
k1 = rand(0:2)
k2 = rand(0:1)
V1 = map(v -> rand(Bool) ? v' : v, rand(V, k1))
V2 = map(v -> rand(Bool) ? v' : v, rand(V, k2))
V3 = prod(x -> x ⊗ x', V2[1:k2]; init = one(V[1]))
V4 = prod(x -> x ⊗ x', V2[(k2 + 1):end]; init = one(V[1]))

k′ = rand(0:(k1 + 2k2))
(_p, _q) = randcircshift(k′, k1 + 2k2 - k′, k1)
p = _repartition(_p, rand(0:k1))
q = (tuple(_q[1:2:end]...), tuple(_q[2:2:end]...))
if !all(isempty, p) && !all(isempty, q)
α = randn(T)
β = randn(T)
ip = _repartition(invperm(linearize((_p, _q))), k′)
A = randn(T, permute(prod(V1) ⊗ V3 ← V4, ip))
C = randn!(TensorOperations.tensoralloc_add(T, A, p, false, Val(false)))
for Tα in (Const, Active), Tβ in (Const, Active)
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β")
end
for Tα in (Const, Duplicated), Tβ in (Const, Duplicated)
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β")
end
end
end
end
end
end
Loading