Skip to content

Commit 4dcf03e

Browse files
committed
Add Enzyme forward and reverse rules and tests for VectorInterface
1 parent cb6a766 commit 4dcf03e

5 files changed

Lines changed: 117 additions & 6 deletions

File tree

‎Project.toml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ uuid = "07d1fe3e-3e46-537d-9eac-e9e13d0d4cec"
33
version = "0.17.0"
44
authors = ["Jutho Haegeman, Lukas Devos"]
55

6+
[workspace]
7+
projects = ["test", "docs"]
8+
69
[deps]
710
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
811
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
@@ -39,12 +42,9 @@ TensorKitFiniteDifferencesExt = "FiniteDifferences"
3942
TensorKitGPUArraysExt = "GPUArrays"
4043
TensorKitMooncakeExt = "Mooncake"
4144

42-
[workspace]
43-
projects = ["test", "docs"]
44-
4545
[compat]
46-
Adapt = "4"
4746
AMDGPU = "2"
47+
Adapt = "4"
4848
CUDA = "6"
4949
ChainRulesCore = "1"
5050
Dictionaries = "0.4"
@@ -62,7 +62,7 @@ Random = "1"
6262
ScopedValues = "1.3.0"
6363
Strided = "2.6.1"
6464
TensorKitSectors = "0.3.7"
65-
TensorOperations = "5.5.2"
65+
TensorOperations = "5.5.2, 5.6"
6666
TupleTools = "1.5"
67-
VectorInterface = "0.4.8, 0.5, 0.6"
67+
VectorInterface = "0.6"
6868
julia = "1.10"

‎test/enzyme-vectorinterface/add.jl‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Test, TestExtras
2+
using TensorKit, Enzyme, EnzymeTestUtils
3+
using TensorOperations
4+
using Random
5+
6+
#spacelist = ad_spacelist(fast_tests)
7+
spacelist = [ad_spacelist(fast_tests)[1]]
8+
eltypes = (Float64, ComplexF64)
9+
10+
@testset "Enzyme - VectorInterface (add!) $(TensorKit.type_repr(sectortype(eltype(V)))) ($T)" for V in spacelist, T in eltypes
11+
atol = default_tol(T)
12+
rtol = default_tol(T)
13+
14+
C = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
15+
A = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
16+
α = randn(T)
17+
β = randn(T)
18+
19+
for TC in (Duplicated,), TA in (Duplicated,)
20+
C = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
21+
A = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
22+
EnzymeTestUtils.test_reverse(add!, TC, (C, TC), (A, TA); atol, rtol, testset_name = "add! reverse TC $TC TA $TA no α no β")
23+
EnzymeTestUtils.test_forward(add!, TC, (C, TC), (A, TA); atol, rtol, testset_name = "add! forward TC $TC TA $TA no α no β")
24+
for Tα in (Active, Const)
25+
C = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
26+
A = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
27+
EnzymeTestUtils.test_reverse(add!, TC, (C, TC), (A, TA), (α, Tα); atol, rtol, testset_name = "add! reverse TC $TC TA $TA Tα $Tα no β")
28+
for Tβ in (Active, Const)
29+
C = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
30+
A = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
31+
EnzymeTestUtils.test_reverse(add!, TC, (C, TC), (A, TA), (α, Tα), (β, Tβ); atol, rtol, testset_name = "add! reverse TC $TC TA $TA Tα $Tα Tβ $Tβ")
32+
end
33+
end
34+
for Tα in (Duplicated, Const)
35+
C = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
36+
A = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
37+
EnzymeTestUtils.test_forward(add!, TC, (C, TC), (A, TA), (α, Tα); atol, rtol, testset_name = "add! forward TC $TC TA $TA Tα $Tα no β")
38+
for Tβ in (Duplicated, Const)
39+
C = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
40+
A = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
41+
EnzymeTestUtils.test_forward(add!, TC, (C, TC), (A, TA), (α, Tα), (β, Tβ); atol, rtol, testset_name = "add! forward TC $TC TA $TA Tα $Tα Tβ $Tβ")
42+
end
43+
end
44+
end
45+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Test, TestExtras
2+
using TensorKit
3+
using TensorOperations
4+
using Enzyme, EnzymeTestUtils
5+
using Random, FiniteDifferences
6+
7+
spacelist = ad_spacelist(fast_tests)
8+
eltypes = (Float64, ComplexF64)
9+
10+
@testset "Enzyme - VectorInterface" begin
11+
@timedtestset "$(TensorKit.type_repr(sectortype(eltype(V)))) ($T)" for V in spacelist, T in eltypes
12+
@testset for TC in (Duplicated,), TA in (Duplicated,), f in (identity, adjoint)
13+
atol = default_tol(T)
14+
rtol = default_tol(T)
15+
C = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
16+
A = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
17+
for RT in (Active, Const)
18+
EnzymeTestUtils.test_reverse(inner, RT, (f(C), TC), (f(A), TA); atol, rtol)
19+
end
20+
for RT in (Duplicated, Const)
21+
EnzymeTestUtils.test_forward(inner, RT, (f(C), TC), (f(A), TA); atol, rtol)
22+
end
23+
end
24+
end
25+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Test, TestExtras
2+
using TensorKit
3+
using TensorOperations
4+
using Enzyme, EnzymeTestUtils
5+
using Random
6+
7+
spacelist = ad_spacelist(fast_tests)
8+
eltypes = (Float64, ComplexF64)
9+
10+
@testset "Enzyme - VectorInterface (scale!)" begin
11+
@timedtestset "$(TensorKit.type_repr(sectortype(eltype(V)))) ($T)" for V in spacelist, T in eltypes
12+
atol = default_tol(T)
13+
rtol = default_tol(T)
14+
α = randn(T)
15+
@testset for TC in (Duplicated,)
16+
for Tα in (Active, Const)
17+
C = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
18+
EnzymeTestUtils.test_reverse(scale!, TC, (C, TC), (α, Tα); atol, rtol)
19+
C = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
20+
EnzymeTestUtils.test_reverse(scale!, TC, (C', TC), (α, Tα); atol, rtol)
21+
@testset for TA in (Duplicated,), (fc, fa) in ((identity, identity), (adjoint, adjoint))
22+
C = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
23+
A = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
24+
EnzymeTestUtils.test_reverse(scale!, TC, (fc(C), TC), (fa(A), TA), (α, Tα); atol, rtol)
25+
end
26+
end
27+
for Tα in (Duplicated, Const)
28+
C = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
29+
EnzymeTestUtils.test_forward(scale!, TC, (C, TC), (α, Tα); atol, rtol)
30+
C = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
31+
EnzymeTestUtils.test_forward(scale!, TC, (C', TC), (α, Tα); atol, rtol)
32+
@testset for TA in (Duplicated,), (fc, fa) in ((identity, identity), (adjoint, adjoint))
33+
C = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
34+
A = randn(T, V[1] ⊗ V[2] ← V[3] ⊗ V[4] ⊗ V[5])
35+
EnzymeTestUtils.test_forward(scale!, TC, (fc(C), TC), (fa(A), TA), (α, Tα); atol, rtol)
36+
end
37+
end
38+
end
39+
end
40+
end

‎test/runtests.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ end
2323
if (Sys.isapple() && get(ENV, "CI", "false") == "true") || !isempty(VERSION.prerelease)
2424
filter!(!startswith("chainrules") ∘ first, testsuite)
2525
filter!(!startswith("mooncake") ∘ first, testsuite)
26+
filter!(!startswith("enzyme") ∘ first, testsuite)
2627
end
2728

2829
args = parse_args(ARGS; custom = ["fast"])

0 commit comments

Comments
 (0)