Skip to content
Draft
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
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ EnzymeTestUtils = "12d8515a-0907-448a-8884-5fe00fdf1c5a"
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
GPUArrays = "0c68f7d7-f131-5f86-a1c3-88cf8149b2d7"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
JLArrays = "27aeb0d3-9eb9-45fb-866b-73c2ecf80fcb"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MatrixAlgebraKit = "6c742aac-3347-4629-af66-fc926824e5e4"
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
Expand All @@ -40,6 +41,7 @@ ChainRulesTestUtils = "1"
Combinatorics = "1"
cuTENSOR = "6"
JET = "0.9, 0.12, 0.13"
JLArrays = "0.3, 0.4, 0.5"
ParallelTestRunner = "2"
Test = "1"
TestExtras = "0.2,0.3"
Expand Down
304 changes: 38 additions & 266 deletions test/amd/planar.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Most planar functionality is tested in a vendor-independent way in `jlarrays/planar.jl`.
# The tests here only check vendor-specific storage types and run the device kernels from
# `TensorKitGPUArraysExt` on actual hardware.
using Test, TestExtras
using Adapt
using TensorOperations, AMDGPU
using Random
using TensorKit
using TensorKit: type_repr
using TensorKit: PlanarTrivial, ℙ
using TensorKit: planaradd!, planartrace!, planarcontract!
using TensorKit: planartrace!

const AMDGPUExt = Base.get_extension(TensorKit, :TensorKitAMDGPUExt)
@assert !isnothing(AMDGPUExt)
const ROCTensorMap = getglobal(AMDGPUExt, :ROCTensorMap)

Random.seed!(123456)

Expand All @@ -23,281 +24,52 @@ for V in spacelist
W = V[1] ⊗ V[2] ← V[2] ⊗ V[1]
T = isreal(sectortype(W)) ? Float64 : ComplexF64
t1 = @constinferred BraidingTensor{T, spacetype(V[2]), ROCVector{T, AMDGPU.Mem.HIPBuffer}}(W)
@test space(t1) == W
@test codomain(t1) == codomain(W)
@test domain(t1) == domain(W)
@test scalartype(t1) == (isreal(sectortype(W)) ? Float64 : ComplexF64)
@test storagetype(t1) == ROCVector{scalartype(t1), AMDGPU.Mem.HIPBuffer}
@test scalartype(t1) == T
@test storagetype(t1) == ROCVector{T, AMDGPU.Mem.HIPBuffer}
t2 = @constinferred BraidingTensor{ComplexF64, spacetype(V[2]), ROCVector{ComplexF64, AMDGPU.Mem.HIPBuffer}}(W)
@test scalartype(t2) == ComplexF64
@test storagetype(t2) == ROCVector{ComplexF64, AMDGPU.Mem.HIPBuffer}
t3 = @testinferred adapt(storagetype(t2), t1)
@test storagetype(t3) == storagetype(t2)
t4 = @testinferred adapt(scalartype(t2), t1)
@test storagetype(t3) == storagetype(t2)
@test t3 ≈ t2

W2 = reverse(codomain(W)) ← domain(W)
@test_throws SpaceMismatch BraidingTensor(W2)

@test adjoint(t1) isa BraidingTensor
@test complex(t1) isa BraidingTensor
@test scalartype(complex(t1)) <: Complex

# materializing runs the braiding subblock fill kernel, braiding the identity runs the permute kernels
t3 = @inferred TensorMap(t2)
@test storagetype(t3) == ROCVector{ComplexF64, AMDGPU.Mem.HIPBuffer}
t4 = braid(adapt(ROCArray, id(scalartype(t2), domain(t2))), ((2, 1), (3, 4)), (1, 2, 3, 4))
@test t1 ≈ t4
for (c, b) in blocks(t1)
@test block(t1, c) ≈ b ≈ block(t3, c)
end

AMDGPU.@allowscalar begin
for (f1, f2) in fusiontrees(t1)
@test t1[f1, f2] ≈ t3[f1, f2]
end
end

t5 = @inferred TensorMap(t2')
@test storagetype(t5) == ROCVector{ComplexF64, AMDGPU.Mem.HIPBuffer}
t6 = braid(adapt(ROCArray, id(scalartype(t2), domain(t2'))), ((2, 1), (3, 4)), (4, 3, 2, 1))
AMDGPU.@allowscalar begin
@test t5 ≈ t6
for (c, b) in blocks(t1')
@test block(t1', c) ≈ b ≈ block(t5, c)
end
for (f1, f2) in fusiontrees(t1')
# needed here for broadcasting the - in isapprox
@test t1'[f1, f2] ≈ t5[f1, f2]
end
end
end
end

@testset "planar methods" verbose = true begin
@testset "planaradd" begin
A = AMDGPU.randn(ℂ^2 ⊗ ℂ^3 ← ℂ^6 ⊗ ℂ^5 ⊗ ℂ^4)
C = AMDGPU.randn((ℂ^5)' ⊗ (ℂ^6)' ← ℂ^4 ⊗ (ℂ^3)' ⊗ (ℂ^2)')
A′ = force_planar(A)
C′ = force_planar(C)
p = ((4, 3), (5, 2, 1))

@test force_planar(tensoradd!(C, A, p, false, true, true)) ≈
planaradd!(C′, A′, p, true, true)
end

@testset "planartrace" begin
A = AMDGPU.randn(ℂ^2 ⊗ ℂ^3 ← ℂ^2 ⊗ ℂ^5 ⊗ ℂ^4)
C = AMDGPU.randn((ℂ^5)' ⊗ ℂ^3 ← ℂ^4)
A′ = force_planar(A)
C′ = force_planar(C)
p = ((4, 2), (5,))
q = ((1,), (3,))

@test force_planar(tensortrace!(C, A, p, q, false, true, true)) ≈
planartrace!(C′, A′, p, q, true, true)
end

@testset "planarcontract" begin
A = AMDGPU.randn(ℂ^2 ⊗ ℂ^3 ← ℂ^2 ⊗ ℂ^5 ⊗ ℂ^4)
B = AMDGPU.randn(ℂ^2 ⊗ ℂ^4 ← ℂ^4 ⊗ ℂ^3)
C = AMDGPU.randn((ℂ^5)' ⊗ (ℂ^2)' ⊗ ℂ^2 ← (ℂ^2)' ⊗ ℂ^4)

A′ = force_planar(A)
B′ = force_planar(B)
C′ = force_planar(C)

pA = ((1, 3, 4), (5, 2))
pB = ((2, 4), (1, 3))
pAB = ((3, 2, 1), (4, 5))

@test force_planar(tensorcontract!(C, A, pA, false, B, pB, false, pAB, true, true)) ≈
planarcontract!(C′, A′, pA, B′, pB, pAB, true, true)
@test t3 ≈ t4
end
end

@testset "@planar" verbose = true begin
T = ComplexF64

@testset "contractcheck" begin
V = ℂ^2
A = AMDGPU.rand(T, V ⊗ V ← V)
B = AMDGPU.rand(T, V ⊗ V ← V')
@tensor C1[i j; k l] := A[i j; m] * B[k l; m]
@tensor contractcheck = true C2[i j; k l] := A[i j; m] * B[k l; m]
@test C1 ≈ C2
B2 = AMDGPU.rand(T, V ⊗ V ← V) # wrong duality for third space
@test_throws SpaceMismatch("incompatible spaces for m: $V ≠ $(V')") begin
@tensor contractcheck = true C3[i j; k l] := A[i j; m] * B2[k l; m]
end

A = AMDGPU.rand(T, V ← V ⊗ V)
B = AMDGPU.rand(T, V ⊗ V ← V)
@planar C1[i; j] := A[i; k l] * τ[k l; m n] * B[m n; j]
@planar contractcheck = true C2[i; j] := A[i; k l] * τ[k l; m n] * B[m n; j]
@test C1 ≈ C2
@test_throws SpaceMismatch("incompatible spaces for m: $V ≠ $(V')") begin
@planar contractcheck = true C3[i; j] := A[i; k l] * τ[k l; m n] * B[n j; m]
end
end

@testset "MPS networks" begin
P = ℂ^2
Vmps = ℂ^12
Vmpo = ℂ^4

# ∂AC
# -------
x = AMDGPU.randn(T, Vmps ⊗ P ← Vmps)
O = AMDGPU.randn(T, Vmpo ⊗ P ← P ⊗ Vmpo)
GL = AMDGPU.randn(T, Vmps ⊗ Vmpo' ← Vmps)
GR = AMDGPU.randn(T, Vmps ⊗ Vmpo ← Vmps)

x′ = force_planar(x)
O′ = force_planar(O)
GL′ = force_planar(GL)
GR′ = force_planar(GR)

for alloc in
(TensorOperations.DefaultAllocator(),)
@tensor allocator = alloc y[-1 -2; -3] := GL[-1 2; 1] * x[1 3; 4] *
O[2 -2; 3 5] * GR[4 5; -3]
@planar allocator = alloc y′[-1 -2; -3] := GL′[-1 2; 1] * x′[1 3; 4] *
O′[2 -2; 3 5] * GR′[4 5; -3]
@test force_planar(y) ≈ y′
end

# ∂AC2
# -------
x2 = AMDGPU.randn(T, Vmps ⊗ P ← Vmps ⊗ P')
x2′ = force_planar(x2)
@tensor contractcheck = true y2[-1 -2; -3 -4] := GL[-1 7; 6] * x2[6 5; 1 3] *
O[7 -2; 5 4] * O[4 -4; 3 2] *
GR[1 2; -3]
@planar y2′[-1 -2; -3 -4] := GL′[-1 7; 6] * x2′[6 5; 1 3] * O′[7 -2; 5 4] *
O′[4 -4; 3 2] * GR′[1 2; -3]
@test force_planar(y2) ≈ y2′

# transfer matrix
# ----------------
v = AMDGPU.randn(T, Vmps ← Vmps)
v′ = force_planar(v)
@tensor ρ[-1; -2] := x[-1 2; 1] * conj(x[-2 2; 3]) * v[1; 3]
@planar ρ′[-1; -2] := x′[-1 2; 1] * conj(x′[-2 2; 3]) * v′[1; 3]
@test force_planar(ρ) ≈ ρ′

@tensor ρ2[-1 -2; -3] := GL[1 -2; 3] * x[3 2; -3] * conj(x[1 2; -1])
@plansor ρ3[-1 -2; -3] := GL[1 2; 4] * x[4 5; -3] * τ[2 3; 5 -2] * conj(x[1 3; -1])
@planar ρ2′[-1 -2; -3] := GL′[1 2; 4] * x′[4 5; -3] * τ[2 3; 5 -2] *
conj(x′[1 3; -1])
@test force_planar(ρ2) ≈ ρ2′
@test ρ2 ≈ ρ3

# Periodic boundary conditions
# ----------------------------
f1 = isomorphism(storagetype(O), fuse(Vmpo^3), Vmpo ⊗ Vmpo' ⊗ Vmpo)
f2 = isomorphism(storagetype(O), fuse(Vmpo^3), Vmpo ⊗ Vmpo' ⊗ Vmpo)
f1′ = force_planar(f1)
f2′ = force_planar(f2)
@tensor O_periodic1[-1 -2; -3 -4] := O[1 -2; -3 2] * f1[-1; 1 3 4] *
conj(f2[-4; 2 3 4])
@plansor O_periodic2[-1 -2; -3 -4] := O[1 2; -3 6] * f1[-1; 1 3 5] *
conj(f2[-4; 6 7 8]) * τ[2 3; 7 4] *
τ[4 5; 8 -2]
@planar O_periodic′[-1 -2; -3 -4] := O′[1 2; -3 6] * f1′[-1; 1 3 5] *
conj(f2′[-4; 6 7 8]) * τ[2 3; 7 4] *
τ[4 5; 8 -2]
@test O_periodic1 ≈ O_periodic2
@test force_planar(O_periodic1) ≈ O_periodic′
end

@testset "MERA networks" begin
Vmera = ℂ^2

u = AMDGPU.randn(T, Vmera ⊗ Vmera ← Vmera ⊗ Vmera)
w = AMDGPU.randn(T, Vmera ⊗ Vmera ← Vmera)
ρ = AMDGPU.randn(T, Vmera ⊗ Vmera ⊗ Vmera ← Vmera ⊗ Vmera ⊗ Vmera)
h = AMDGPU.randn(T, Vmera ⊗ Vmera ⊗ Vmera ← Vmera ⊗ Vmera ⊗ Vmera)

u′ = force_planar(u)
w′ = force_planar(w)
ρ′ = force_planar(ρ)
h′ = force_planar(h)

for alloc in
(TensorOperations.DefaultAllocator(),)
@tensor allocator = alloc begin
C = (
(
(
(
(
((h[9 3 4; 5 1 2] * u[1 2; 7 12]) * conj(u[3 4; 11 13])) *
(u[8 5; 15 6] * w[6 7; 19])
) *
(conj(u[8 9; 17 10]) * conj(w[10 11; 22]))
) *
((w[12 14; 20] * conj(w[13 14; 23])) * ρ[18 19 20; 21 22 23])
) *
w[16 15; 18]
) * conj(w[16 17; 21])
)
end
@planar allocator = alloc begin
C′ = (
(
(
(
(
((h′[9 3 4; 5 1 2] * u′[1 2; 7 12]) * conj(u′[3 4; 11 13])) *
(u′[8 5; 15 6] * w′[6 7; 19])
) *
(conj(u′[8 9; 17 10]) * conj(w′[10 11; 22]))
) *
((w′[12 14; 20] * conj(w′[13 14; 23])) * ρ′[18 19 20; 21 22 23])
) *
w′[16 15; 18]
) * conj(w′[16 17; 21])
)
end
@test C ≈ C′
for V in spacelist
I = sectortype(first(V))
Istr = type_repr(I)
@timedtestset "planartrace + AMDGPU with symmetry: $Istr" begin
# (space, p, q) for a trace of neighbouring legs and a nested trace
cases = (
(V[1] ⊗ V[2] ⊗ V[3] ← V[4] ⊗ V[3], ((1, 2), (4,)), ((3,), (5,))),
(V[1] ⊗ V[2] ⊗ V[3] ⊗ V[4] ← V[3] ⊗ V[4], ((1,), (2,)), ((3, 4), (5, 6))),
)
for (W, p, q) in cases, (α, β) in ((0.7, 0.3), (1.0, 0.0))
A = randn(ComplexF64, W)
C = randn(ComplexF64, TensorKit.select(W, p))
Cgpu = planartrace!(adapt(ROCArray, C), adapt(ROCArray, A), p, q, α, β)
@test adapt(Vector{ComplexF64}, Cgpu) ≈ planartrace!(C, A, p, q, α, β)
end
end
end

@testset "Issue 93" begin
T = Float64
V1 = ℂ^2
V2 = ℂ^3
t1 = AMDGPU.randn(T, V1 ← V2)
t2 = AMDGPU.randn(T, V2 ← V1)

tr1 = @planar opt = true t1[a; b] * t2[b; a] / 2
tr2 = @planar opt = true t1[d; a] * t2[b; c] * 1 / 2 * τ[c b; a d]
tr3 = @planar opt = true t1[d; a] * t2[b; c] * τ[a c; d b] / 2
tr4 = @planar opt = true t1[f; a] * 1 / 2 * t2[c; d] * τ[d b; c e] * τ[e b; a f]
tr5 = @planar opt = true t1[f; a] * t2[c; d] / 2 * τ[d b; c e] * τ[a e; f b]
tr6 = @planar opt = true t1[f; a] * t2[c; d] * τ[c d; e b] / 2 * τ[e b; a f]
tr7 = @planar opt = true t1[f; a] * t2[c; d] * (τ[c d; e b] * τ[a e; f b] / 2)

@test tr1 ≈ tr2 ≈ tr3 ≈ tr4 ≈ tr5 ≈ tr6 ≈ tr7

tr1 = @plansor opt = true t1[a; b] * t2[b; a] / 2
tr2 = @plansor opt = true t1[d; a] * t2[b; c] * 1 / 2 * τ[c b; a d]
tr3 = @plansor opt = true t1[d; a] * t2[b; c] * τ[a c; d b] / 2
tr4 = @plansor opt = true t1[f; a] * 1 / 2 * t2[c; d] * τ[d b; c e] * τ[e b; a f]
tr5 = @plansor opt = true t1[f; a] * t2[c; d] / 2 * τ[d b; c e] * τ[a e; f b]
tr6 = @plansor opt = true t1[f; a] * t2[c; d] * τ[c d; e b] / 2 * τ[e b; a f]
tr7 = @plansor opt = true t1[f; a] * t2[c; d] * (τ[c d; e b] * τ[a e; f b] / 2)

@test tr1 ≈ tr2 ≈ tr3 ≈ tr4 ≈ tr5 ≈ tr6 ≈ tr7
end
@testset "Issue 262" begin
V = ℂ^2
A = AMDGPU.randn(T, V ← V)
B = AMDGPU.randn(T, V ← V')
C = AMDGPU.randn(T, V' ← V)
@planar D1[i; j] := A[i; j] + B[i; k] * C[k; j]
@planar D2[i; j] := B[i; k] * C[k; j] + A[i; j]
@test D1 ≈ D2
@testset "planartrace with large traces: $I" for (I, V, Vr) in (
(FermionParity, Vect[FermionParity](0 => 40, 1 => 40), Vect[FermionParity](0 => 2, 1 => 3)),
(FibonacciAnyon, Vect[FibonacciAnyon](:I => 30, :τ => 30), Vect[FibonacciAnyon](:I => 2, :τ => 1)),
)
# (space, p, q) for a full reduction and for a trace with few remaining entries, which are
# both split into several chunks of traced entries
cases = (
(V ⊗ V ← V ⊗ V, ((), ()), ((1, 2), (3, 4))),
(Vr ⊗ V ⊗ V ← V ⊗ V, ((1,), ()), ((2, 3), (4, 5))),
)
for (W, p, q) in cases, (α, β) in ((0.7, 0.3), (1.0, 0.0))
A = randn(ComplexF64, W)
C = randn(ComplexF64, TensorKit.select(W, p))
Cgpu = planartrace!(adapt(ROCArray, C), adapt(ROCArray, A), p, q, α, β)
@test adapt(Vector{ComplexF64}, Cgpu) ≈ planartrace!(C, A, p, q, α, β)
end
end
Loading
Loading