From 924dff5e005abd779455d177c933e64c1ecd94e6 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Mon, 9 Nov 2026 15:00:09 +0100 Subject: [PATCH] Use JLArrays to test GPU functionality --- test/Project.toml | 2 + test/amd/planar.jl | 304 +++--------------- test/amd/tensors.jl | 431 +------------------------ test/cuda/planar.jl | 310 ++---------------- test/cuda/tensors.jl | 494 +--------------------------- test/jlarrays/planar.jl | 335 +++++++++++++++++++ test/jlarrays/tensors.jl | 672 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 1083 insertions(+), 1465 deletions(-) create mode 100644 test/jlarrays/planar.jl create mode 100644 test/jlarrays/tensors.jl diff --git a/test/Project.toml b/test/Project.toml index 80da0d8cc..cfec459f4 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -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" @@ -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" diff --git a/test/amd/planar.jl b/test/amd/planar.jl index a18395086..c963b44f5 100644 --- a/test/amd/planar.jl +++ b/test/amd/planar.jl @@ -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) @@ -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 diff --git a/test/amd/tensors.jl b/test/amd/tensors.jl index e3e2236dc..2b4347c89 100644 --- a/test/amd/tensors.jl +++ b/test/amd/tensors.jl @@ -37,14 +37,6 @@ for V in spacelist @test domain(t) == one(W) @test typeof(t) == TensorMap{Float64, spacetype(t), 5, 0, ROCVector{Float64, AMDGPU.Mem.HIPBuffer}} end - for f in (rand, randn) - t = @constinferred f(ROCVector{Float64, AMDGPU.Mem.HIPBuffer}, W) - @test scalartype(t) == Float64 - @test codomain(t) == W - @test space(t) == (W ← one(W)) - @test domain(t) == one(W) - @test typeof(t) == TensorMap{Float64, spacetype(t), 5, 0, ROCVector{Float64, AMDGPU.Mem.HIPBuffer}} - end for f! in (rocrand!, rocrandn!) t = @constinferred AMDGPU.zeros(W) f!(t) @@ -56,25 +48,8 @@ for V in spacelist end for T in (Int, Float32, Float64, ComplexF32, ComplexF64) t = @constinferred AMDGPU.zeros(T, W) - AMDGPU.@allowscalar begin - @test @constinferred(hash(t)) == hash(deepcopy(t)) - end @test scalartype(t) == T - @test norm(t) == 0 - @test codomain(t) == W - @test space(t) == (W ← one(W)) - @test domain(t) == one(W) @test typeof(t) == TensorMap{T, spacetype(t), 5, 0, ROCVector{T, AMDGPU.Mem.HIPBuffer}} - # blocks - bs = @constinferred blocks(t) - (c, b1), state = @constinferred Nothing iterate(bs) - @test c == first(blocksectors(W)) - next = @constinferred Nothing iterate(bs, state) - b2 = @constinferred block(t, first(blocksectors(t))) - @test b1 == b2 - @test eltype(bs) === Pair{typeof(c), typeof(b1)} - @test typeof(b1) === TensorKit.blocktype(t) - @test typeof(c) === sectortype(t) end end @timedtestset "Conversion to/from host" begin @@ -97,200 +72,6 @@ for V in spacelist @test domain(t2) == one(W) end end - @timedtestset "Adapt SectorVector" begin - for T in (Float32, ComplexF64) - v = TensorKit.SectorVector{T}(undef, V1) - rand!(parent(v)) - v_gpu = @constinferred adapt(ROCArray, v) - @test v_gpu isa TensorKit.SectorVector{T, I, <:ROCArray{T}} - @test v_gpu.structure == v.structure - v_cpu = @constinferred adapt(Array, v_gpu) - @test v_cpu isa TensorKit.SectorVector{T, I, Vector{T}} - @test parent(v_cpu) == parent(v) - t = AMDGPU.randn(T, V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)') - s = svd_vals(t) - s_cpu = adapt(Array, s) - @test parent(s_cpu) == collect(parent(s)) - @test all(c -> block(s_cpu, c) == collect(block(s, c)), blocksectors(s)) - end - end - @timedtestset "Tensor Dict conversion" begin - W = V1 ⊗ V2 ← (V3 ⊗ V4 ⊗ V5)' - for T in (Int, Float32, ComplexF64) - t = @constinferred AMDGPU.rand(T, W) - d = convert(Dict, t) - @test convert(Dict, adapt(Array, t)) == d - end - end - symmetricbraiding && @timedtestset "Basic linear algebra" begin - W = V1 ⊗ V2 ⊗ V3 ← V4 ⊗ V5 - for T in (Float32, ComplexF64) - t = @constinferred AMDGPU.rand(T, W) - @test scalartype(t) == T - @test space(t) == W - @test space(t') == W' - @test dim(t) == dim(space(t)) - @test codomain(t) == codomain(W) - @test domain(t) == domain(W) - # blocks for adjoint - bs = @constinferred blocks(t') - (c, b1), state = @constinferred Nothing iterate(bs) - @test c == first(blocksectors(W')) - next = @constinferred Nothing iterate(bs, state) - b2 = @constinferred block(t', first(blocksectors(t'))) - @test b1 == b2 - @test eltype(bs) === Pair{typeof(c), typeof(b1)} - @test typeof(b1) === TensorKit.blocktype(t') - @test typeof(c) === sectortype(t) - # linear algebra - @test isa(@constinferred(norm(t)), real(T)) - @test norm(t)^2 ≈ dot(t, t) - α = rand(T) - @test norm(α * t) ≈ abs(α) * norm(t) - @test norm(t + t, 2) ≈ 2 * norm(t, 2) - @test norm(t + t, 1) ≈ 2 * norm(t, 1) - @test norm(t + t, Inf) ≈ 2 * norm(t, Inf) - p = 3 * rand(Float64) - @test norm(t + t, p) ≈ 2 * norm(t, p) - @test norm(t) ≈ norm(t') - for p in (1, 2, 3 * rand(Float64), Inf) - tn = @constinferred normalize(t, p) - @test storagetype(tn) == storagetype(t) - @test norm(tn, p) ≈ 1 - @test tn ≈ t / norm(t, p) - @test normalize!(copy(t), p) ≈ tn - @test normalize(t', p) ≈ tn' - ta = copy(t)' - @test normalize!(ta, p) === ta - @test ta ≈ tn' - end - - t2 = @constinferred rand!(similar(t)) - β = rand(T) - #@test @constinferred(dot(β * t2, α * t)) ≈ conj(β) * α * conj(dot(t, t2)) # broken for Irrep[CU₁] - @test dot(β * t2, α * t) ≈ conj(β) * α * conj(dot(t, t2)) - @test dot(t2, t) ≈ conj(dot(t, t2)) - @test dot(t2, t) ≈ conj(dot(t2', t')) - @test dot(t2, t) ≈ dot(t', t2') - - i1 = @constinferred(isomorphism(ROCVector{T, AMDGPU.Mem.HIPBuffer}, V1 ⊗ V2, V2 ⊗ V1)) - i2 = @constinferred(isomorphism(ROCVector{T, AMDGPU.Mem.HIPBuffer}, V2 ⊗ V1, V1 ⊗ V2)) - @test i1 * i2 == @constinferred(id(ROCVector{T, AMDGPU.Mem.HIPBuffer}, V1 ⊗ V2)) - @test i2 * i1 == @constinferred(id(ROCVector{T, AMDGPU.Mem.HIPBuffer}, V2 ⊗ V1)) - w = @constinferred(isometry(ROCVector{T, AMDGPU.Mem.HIPBuffer}, V1 ⊗ (oneunit(V1) ⊕ oneunit(V1)), V1)) - @test dim(w) == 2 * dim(V1 ← V1) - @test w' * w == id(ROCVector{T, AMDGPU.Mem.HIPBuffer}, V1) - @test w * w' == (w * w')^2 - end - end - @timedtestset "Trivial space insertion and removal" begin - W = V1 ⊗ V2 ← (V3 ⊗ V4 ⊗ V5)' - for T in (Float32, ComplexF64) - t = @constinferred AMDGPU.rand(T, W) - t2 = @constinferred insertleftunit(t) - @test t2 == @constinferred insertrightunit(t) - @test numind(t2) == numind(t) + 1 - @test space(t2) == insertleftunit(space(t)) - @test scalartype(t2) === T - @test t.data === t2.data - @test @constinferred(removeunit(t2, $(numind(t2)))) == t - t3 = @constinferred insertleftunit(t; copy = true) - @test t3 == @constinferred insertrightunit(t; copy = true) - @test t.data !== t3.data - for (c, b) in blocks(t) - @test b == block(t3, c) - end - @test @constinferred(removeunit(t3, $(numind(t3)))) == t - t4 = @constinferred insertrightunit(t, 3; dual = true) - @test numin(t4) == numin(t) + 1 && numout(t4) == numout(t) - for (c, b) in blocks(t) - @test b == block(t4, c) - end - @test @constinferred(removeunit(t4, 4)) == t - t5 = @constinferred insertleftunit(t, 4; dual = true) - @test numin(t5) == numin(t) + 1 && numout(t5) == numout(t) - for (c, b) in blocks(t) - @test b == block(t5, c) - end - @test @constinferred(removeunit(t5, 4)) == t - end - end - if hasfusiontensor(I) - @timedtestset "Basic linear algebra: test via CPU" begin - W = V1 ⊗ V2 ⊗ V3 ← V4 ⊗ V5 - for T in (Float32, ComplexF64) - t = AMDGPU.rand(T, W) - t2 = @constinferred AMDGPU.rand!(similar(t)) - α = rand(T) - @test norm(t, 2) ≈ norm(adapt(Array, t), 2) - @test dot(t2, t) ≈ dot(adapt(Array, t2), adapt(Array, t)) - @test adapt(Array, α * t) ≈ α * adapt(Array, t) - @test adapt(Array, t + t) ≈ 2 * adapt(Array, t) - end - end - @timedtestset "Real and imaginary parts" begin - W = V1 ⊗ V2 - for T in (Float64, ComplexF64, ComplexF32) - t = @constinferred AMDGPU.randn(T, W, W) - - tr = @constinferred real(t) - @test scalartype(tr) <: Real - @test real(adapt(Array, t)) == adapt(Array, tr) - @test storagetype(tr) == ROCVector{real(T), AMDGPU.Mem.HIPBuffer} - - ti = @constinferred imag(t) - @test scalartype(ti) <: Real - @test imag(adapt(Array, t)) == adapt(Array, ti) - @test storagetype(ti) == ROCVector{real(T), AMDGPU.Mem.HIPBuffer} - - tc = @inferred complex(t) - @test scalartype(tc) <: Complex - @test complex(adapt(Array, t)) == adapt(Array, tc) - @test storagetype(tc) == ROCVector{complex(T), AMDGPU.Mem.HIPBuffer} - - tc2 = @inferred complex(tr, ti) - @test tc2 ≈ tc - @test storagetype(tc2) == ROCVector{complex(T), AMDGPU.Mem.HIPBuffer} - end - end - end - @timedtestset "Tensor conversion" begin - W = V1 ⊗ V2 - t = @constinferred AMDGPU.randn(W ← W) - @test typeof(convert(TensorMap, t')) == typeof(t) - tc = complex(t) - @test convert(typeof(tc), t) == tc - @test typeof(convert(typeof(tc), t)) == typeof(tc) - @test typeof(convert(typeof(tc), t')) == typeof(tc) - @test Base.promote_typeof(t, tc) == typeof(tc) - @test Base.promote_typeof(tc, t) == typeof(tc + t) - end - #=@timedtestset "diag/diagm" begin - W = V1 ⊗ V2 ⊗ V3 ← V4 ⊗ V5 - t = AMDGPU.randn(ComplexF64, W) - d = LinearAlgebra.diag(t) - # TODO find a way to use AMDGPU here - D = LinearAlgebra.diagm(codomain(t), domain(t), d) - @test LinearAlgebra.isdiag(D) - @test LinearAlgebra.diag(D) == d - end=# - symmetricbraiding && @timedtestset "Permutations: test via CPU" begin - W = V1 ⊗ V2 ⊗ V3 ⊗ V4 ⊗ V5 - t = AMDGPU.rand(ComplexF64, W) - for k in 0:5 - for p in permutations(1:5) - p1 = ntuple(n -> p[n], k) - p2 = ntuple(n -> p[k + n], 5 - k) - dt2 = permute(t, (p1, p2)) - ht2 = permute(adapt(Array, t), (p1, p2)) - @test ht2 ≈ adapt(Array, dt2) - end - - dt3 = repartition(t, k) - ht3 = repartition(adapt(Array, t), k) - @test ht3 ≈ adapt(Array, dt3) - end - end symmetricbraiding && @timedtestset "Full trace: test self-consistency" begin t = AMDGPU.rand(ComplexF64, V1 ⊗ V2' ⊗ V2 ⊗ V1') t2 = permute(t, ((1, 2), (4, 3))) @@ -309,87 +90,6 @@ for V in spacelist @test ss ≈ s2 @test ss ≈ s3 end - symmetricbraiding && @timedtestset "Partial trace: test self-consistency" begin - t = AMDGPU.rand(ComplexF64, V1 ⊗ V2' ⊗ V3 ⊗ V2 ⊗ V1' ⊗ V3') - @tensor t2[a, b] := t[c, d, b, d, c, a] - @tensor t4[a, b, c, d] := t[d, e, b, e, c, a] - @tensor t5[a, b] := t4[a, b, c, c] - @test t2 ≈ t5 - end - symmetricbraiding && @timedtestset "Trace: test via conversion" begin - t = AMDGPU.rand(ComplexF64, V1 ⊗ V2' ⊗ V3 ⊗ V2 ⊗ V1' ⊗ V3') - @tensor t2[a, b] := t[c, d, b, d, c, a] - @tensor t3[a, b] := ad(t)[c, d, b, d, c, a] - @test t3 ≈ ad(t2) - end - symmetricbraiding && @timedtestset "Trace and contraction" begin - t1 = AMDGPU.rand(ComplexF64, V1 ⊗ V2 ⊗ V3) - t2 = AMDGPU.rand(ComplexF64, V2' ⊗ V4 ⊗ V1') - t3 = t1 ⊗ t2 - @tensor ta[a, b] := t1[x, y, a] * t2[y, b, x] - @tensor tb[a, b] := t3[x, y, a, y, b, x] - @test ta ≈ tb - end - if BraidingStyle(I) isa Bosonic && hasfusiontensor(I) - @timedtestset "Tensor contraction: test via CPU" begin - dA1 = AMDGPU.randn(ComplexF64, V1' * V2', V3') - dA2 = AMDGPU.randn(ComplexF64, V3 * V4, V5) - drhoL = AMDGPU.randn(ComplexF64, V1, V1) - drhoR = AMDGPU.randn(ComplexF64, V5, V5)' # test adjoint tensor - dH = AMDGPU.randn(ComplexF64, V2 * V4, V2 * V4) - @tensor dHrA12[a, s1, s2, c] := drhoL[a, a'] * conj(dA1[a', t1, b]) * - dA2[b, t2, c'] * drhoR[c', c] * - dH[s1, s2, t1, t2] - @tensor hHrA12[a, s1, s2, c] := adapt(Array, drhoL)[a, a'] * conj(adapt(Array, dA1)[a', t1, b]) * - adapt(Array, dA2)[b, t2, c'] * adapt(Array, drhoR)[c', c] * - adapt(Array, dH)[s1, s2, t1, t2] - @test adapt(Array, dHrA12) ≈ hHrA12 - end - end - hasbraiding && @timedtestset "Index flipping: test flipping inverse" begin - t = AMDGPU.rand(ComplexF64, V1 ⊗ V1' ← V1' ⊗ V1) - for i in 1:4 - @test t ≈ flip(flip(t, i), i; inv = true) - @test t ≈ flip(flip(t, i; inv = true), i) - end - end - symmetricbraiding && @timedtestset "Index flipping: test via explicit flip" begin - t = AMDGPU.rand(ComplexF64, V1 ⊗ V1' ← V1' ⊗ V1) - F1 = adapt(ROCArray{ComplexF64}, unitary(flip(V1), V1)) - - @tensor tf[a, b; c, d] := F1[a, a'] * t[a', b; c, d] - @test flip(t, 1) ≈ tf - @tensor tf[a, b; c, d] := conj(F1[b, b']) * t[a, b'; c, d] - @test twist!(flip(t, 2), 2) ≈ tf - @tensor tf[a, b; c, d] := F1[c, c'] * t[a, b; c', d] - @test flip(t, 3) ≈ tf - @tensor tf[a, b; c, d] := conj(F1[d, d']) * t[a, b; c, d'] - @test twist!(flip(t, 4), 4) ≈ tf - end - symmetricbraiding && @timedtestset "Index flipping: test via contraction" begin - t1 = AMDGPU.rand(ComplexF64, V1 ⊗ V2 ⊗ V3 ← V4) - t2 = AMDGPU.rand(ComplexF64, V2' ⊗ V5 ← V4' ⊗ V1) - @tensor ta[a, b] := t1[x, y, a, z] * t2[y, b, z, x] - @tensor tb[a, b] := flip(t1, 1)[x, y, a, z] * flip(t2, 4)[y, b, z, x] - @test ta ≈ tb - @tensor tb[a, b] := flip(t1, (2, 4))[x, y, a, z] * flip(t2, (1, 3))[y, b, z, x] - @test ta ≈ tb - @tensor tb[a, b] := flip(t1, (1, 2, 4))[x, y, a, z] * flip(t2, (1, 3, 4))[y, b, z, x] - @tensor tb[a, b] := flip(t1, (1, 3))[x, y, a, z] * flip(t2, (2, 4))[y, b, z, x] - @test flip(ta, (1, 2)) ≈ tb - end - @timedtestset "Multiplication of isometries: test properties" begin - W1 = V1 ⊗ V2 ⊗ V3 - W2 = (V4 ⊗ V5)' - for T in (Float64, ComplexF64) - t1 = randisometry(ROCMatrix{T, AMDGPU.Mem.HIPBuffer}, W1, W2) - t2 = randisometry(ROCMatrix{T, AMDGPU.Mem.HIPBuffer}, W2 ← W2) - @test isisometric(t1) - @test isunitary(t2) - P = t1 * t1' - @test P * P ≈ P - end - end @timedtestset "Multiplication and inverse: test compatibility" begin W1 = V1 ⊗ V2 ⊗ V3 W2 = (V4 ⊗ V5)' @@ -397,14 +97,12 @@ for V in spacelist t1 = AMDGPU.rand(T, W1, W1) t2 = AMDGPU.rand(T, W2, W2) t = AMDGPU.rand(T, W1, W2) - #=@test t1 * (t1 \ t) ≈ t + @test t1 * (t1 \ t) ≈ t @test (t / t2) * t2 ≈ t - AMDGPU.@allowscalar begin - @test t1 \ one(t1) ≈ inv(t1) - @test one(t1) / t1 ≈ pinv(t1) - tp = pinv(t) * t - @test tp ≈ tp * tp - end=# + @test t1 \ one(t1) ≈ inv(t1) + @test one(t1) / t1 ≈ pinv(t1) + tp = pinv(t) * t + @test tp ≈ tp * tp @test_throws SpaceMismatch inv(t) @test_throws SpaceMismatch t2 \ t @test_throws SpaceMismatch t / t1 @@ -504,145 +202,34 @@ for V in spacelist # end # # TODO - @timedtestset "Tensor product: test via norm preservation" begin - for T in (ComplexF64,) - t1 = AMDGPU.rand(T, V1, V5') - t2 = AMDGPU.rand(T, V2 ⊗ V3, V4') - t = @constinferred (t1 ⊗ t2) - @test norm(t) ≈ norm(t1) * norm(t2) - end - end - symmetricbraiding && @timedtestset "Tensor product: test via conversion" begin - for T in (Float32, ComplexF64) - t1 = AMDGPU.rand(T, V1, V5') - t2 = AMDGPU.rand(T, V2 ⊗ V3, V4') - d1 = dim(codomain(t1)) - d2 = dim(codomain(t2)) - d3 = dim(domain(t1)) - d4 = dim(domain(t2)) - t = @constinferred (t1 ⊗ t2) - At = ad(t) - @test ad(t) ≈ ad(t1) ⊗ ad(t2) - end - end - symmetricbraiding && @timedtestset "Tensor product: test via tensor contraction" begin - for T in (Float32, ComplexF64) - t1 = AMDGPU.rand(T, V2 ⊗ V3 ⊗ V1) - t2 = AMDGPU.rand(T, V2 ⊗ V1 ⊗ V3) - t = @constinferred (t1 ⊗ t2) - @tensor t′[1, 2, 3, 4, 5, 6] := t1[1, 2, 3] * t2[4, 5, 6] - # @test t ≈ t′ # TODO broken for symmetry: Irrep[ℤ₃] - end - end end TensorKit.empty_globalcaches!() end -@timedtestset "Deligne tensor product: test via conversion" begin - using .TestSetup: Vtr, VRepℤ₂, VRepSU₂, VRepA4 - @testset for Vlist1 in (Vtr, VRepSU₂), Vlist2 in (VRepℤ₂, VRepA4) - V1, V2, V3, V4, V5 = Vlist1 - W1, W2, W3, W4, W5 = Vlist2 - for T in (Float32, ComplexF64) - t1 = AMDGPU.rand(T, V1 ⊗ V2, V3' ⊗ V4) - t2 = AMDGPU.rand(T, W2, W1 ⊗ W1') - t = @constinferred (t1 ⊠ t2) - d1 = dim(codomain(t1)) - d2 = dim(codomain(t2)) - d3 = dim(domain(t1)) - d4 = dim(domain(t2)) - @test ad(t1) ⊠ ad(t2) ≈ ad(t1 ⊠ t2) - end - end -end - -@timedtestset "DiagonalTensorMap index manipulations with domain $V: test via CPU" for V in ( +@timedtestset "DiagonalTensorMap with domain $V: test via CPU" for V in ( (ℂ^4)', Vect[Z2Irrep](0 => 2, 1 => 3), Vect[U1Irrep](0 => 2, 1 => 2, -1 => 1), Vect[FermionParity](0 => 2, 1 => 3), Vect[SU2Irrep](0 => 2, 1 => 1)', ) - function test_tensoradd(dst, d, p, conjd, α, β) - cpu = TO.tensoradd!(adapt(Array, dst), adapt(Array, d), p, conjd, α, β, TO.DefaultBackend(), TO.DefaultAllocator()) - gpu = TO.tensoradd!(copy(dst), d, p, conjd, α, β, TO.DefaultBackend(), TO.DefaultAllocator()) - @test gpu isa typeof(dst) - @test adapt(Array, gpu) ≈ cpu - return nothing - end diagonal(T, W) = DiagonalTensorMap(ROCVector{T}(rand(T, reduceddim(W))), W) - @testset "eltype $T" for T in (Float64, ComplexF64) + @testset "normalize, eltype $T" for T in (Float64, ComplexF64) d = diagonal(T, V) dcpu = adapt(Array, d) - scalars = ((TensorKit.One(), TensorKit.Zero()), (T(2), T(3))) - for p in (1, 2, 3 * rand(Float64), Inf) dn = @constinferred normalize(d, p) @test dn isa typeof(d) @test adapt(Array, dn) ≈ normalize(dcpu, p) @test adapt(Array, normalize!(copy(d), p)) ≈ normalize(dcpu, p) end - - for p in (((), (1, 2)), ((1, 2), ()), ((2, 1), ()), ((), (2, 1)), ((1,), (2,)), ((2,), (1,))), conjd in (false, true) - W = TO.tensoradd_structure(dcpu, p, conjd) - dst = adapt(ROCVector{T}, randn(T, W)) - for (α, β) in scalars - test_tensoradd(dst, d, p, conjd, α, β) - end - end - for p in (((1,), (2,)), ((2,), (1,))), conjd in (false, true) - W = domain(TO.tensoradd_structure(dcpu, p, conjd))[1] - for (α, β) in scalars - test_tensoradd(diagonal(T, W), d, p, conjd, α, β) - end - end - for p in (((), (1, 2)), ((1, 2), ()), ((2, 1), ()), ((2,), (1,))) - @test adapt(Array, permute(d, p)) ≈ permute(dcpu, p) - @test adapt(Array, permute(d', p)) ≈ permute(dcpu', p) - end - for k in (0, 2) - @test adapt(Array, repartition(d, k)) ≈ repartition(dcpu, k) - @test adapt(Array, repartition(d', k)) ≈ repartition(dcpu', k) - end - for levels in ((1, 2), (2, 1)) - @test adapt(Array, braid(d, ((1, 2), ()), levels)) ≈ braid(dcpu, ((1, 2), ()), levels) - end - for inds in ((1,), (2,), (1, 2)), inv in (false, true) - ref = twist(dcpu, inds; inv) - @test twist(d, inds; inv) isa typeof(d) - @test adapt(Array, twist(d, inds; inv)) ≈ ref - @test adapt(Array, twist(d', inds; inv)) ≈ twist(dcpu', inds; inv) - @test adapt(Array, twist!(copy(d), inds; inv)) ≈ ref - dst = adapt(ROCVector{T}, randn(T, space(dcpu))) - @test adapt(Array, twist!(copy(dst), d, inds, T(2), T(3); inv)) ≈ 3 * adapt(Array, dst) + 2 * ref - ddst = diagonal(T, V) - @test adapt(Array, twist!(copy(ddst), d, inds, T(2), T(3); inv)) ≈ 3 * adapt(Array, ddst) + 2 * ref - end - end - - @testset "real source, complex destination" begin - d = diagonal(Float64, V) - p = ((1,), (2,)) - W = domain(TO.tensoradd_structure(adapt(Array, d), p, true))[1] - test_tensoradd(diagonal(ComplexF64, W), d, p, true, TensorKit.One(), TensorKit.Zero()) - test_tensoradd(diagonal(ComplexF64, W), d, p, true, ComplexF64(2), ComplexF64(3)) end - @testset "contractions" begin + @testset "scalar contraction" begin d = diagonal(Float64, V) - t = adapt(ROCVector{ComplexF64}, randn(ComplexF64, V ⊗ V ← V)) s = adapt(ROCVector{Float64}, randn(Float64, V ← V)) - dcpu, tcpu, scpu = adapt(Array, d), adapt(Array, t), adapt(Array, s) - - @tensor gpu[-1 -2; -3] := t[-1 -2; 1] * d[1; -3] - @tensor cpu[-1 -2; -3] := tcpu[-1 -2; 1] * dcpu[1; -3] - @test adapt(Array, gpu) ≈ cpu - - @tensor gpu[-1 -2; -3] := d[-1; 1] * t[1 -2; -3] - @tensor cpu[-1 -2; -3] := dcpu[-1; 1] * tcpu[1 -2; -3] - @test adapt(Array, gpu) ≈ cpu - + dcpu, scpu = adapt(Array, d), adapt(Array, s) @test (@tensor s[1; 2] * d[2; 1]) ≈ (@tensor scpu[1; 2] * dcpu[2; 1]) end end diff --git a/test/cuda/planar.jl b/test/cuda/planar.jl index e92faef8e..19b8f6a12 100644 --- a/test/cuda/planar.jl +++ b/test/cuda/planar.jl @@ -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, CUDA, cuTENSOR using Random using TensorKit using TensorKit: type_repr -using TensorKit: PlanarTrivial, ℙ -using TensorKit: planaradd!, planartrace!, planarcontract! +using TensorKit: planartrace! const CUDAExt = Base.get_extension(TensorKit, :TensorKitCUDAExt) @assert !isnothing(CUDAExt) -const CuTensorMap = getglobal(CUDAExt, :CuTensorMap) Random.seed!(123456) @@ -23,54 +24,17 @@ 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]), CuVector{T, CUDA.DeviceMemory}}(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) == CuVector{scalartype(t1), CUDA.DeviceMemory} + @test scalartype(t1) == T + @test storagetype(t1) == CuVector{T, CUDA.DeviceMemory} t2 = @constinferred BraidingTensor{ComplexF64, spacetype(V[2]), CuVector{ComplexF64, CUDA.DeviceMemory}}(W) - @test scalartype(t2) == ComplexF64 @test storagetype(t2) == CuVector{ComplexF64, CUDA.DeviceMemory} - 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) == CuVector{ComplexF64, CUDA.DeviceMemory} t4 = braid(adapt(CuArray, 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 - - CUDA.@allowscalar begin - for (f1, f2) in fusiontrees(t1) - @test t1[f1, f2] ≈ t3[f1, f2] - end - end - - t5 = @inferred TensorMap(t2') - @test storagetype(t5) == CuVector{ComplexF64, CUDA.DeviceMemory} - t6 = braid(adapt(CuArray, id(scalartype(t2), domain(t2'))), ((2, 1), (3, 4)), (4, 3, 2, 1)) - CUDA.@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 + @test t3 ≈ t4 end end @@ -92,248 +56,20 @@ for V in spacelist end end -@testset "planar methods" verbose = true begin - @testset "planaradd" begin - A = CUDA.randn(ℂ^2 ⊗ ℂ^3 ← ℂ^6 ⊗ ℂ^5 ⊗ ℂ^4) - C = CUDA.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 = CUDA.randn(ℂ^2 ⊗ ℂ^3 ← ℂ^2 ⊗ ℂ^5 ⊗ ℂ^4) - C = CUDA.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 "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(CuArray, C), adapt(CuArray, A), p, q, α, β) - @test adapt(Vector{ComplexF64}, Cgpu) ≈ planartrace!(C, A, p, q, α, β) - end - end - - @testset "planarcontract" begin - A = CUDA.randn(ℂ^2 ⊗ ℂ^3 ← ℂ^2 ⊗ ℂ^5 ⊗ ℂ^4) - B = CUDA.randn(ℂ^2 ⊗ ℂ^4 ← ℂ^4 ⊗ ℂ^3) - C = CUDA.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) - end -end - -@testset "@planar" verbose = true begin - T = ComplexF64 - - @testset "contractcheck" begin - V = ℂ^2 - A = CUDA.rand(T, V ⊗ V ← V) - B = CUDA.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 = CUDA.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 = CUDA.rand(T, V ← V ⊗ V) - B = CUDA.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 = CUDA.randn(T, Vmps ⊗ P ← Vmps) - O = CUDA.randn(T, Vmpo ⊗ P ← P ⊗ Vmpo) - GL = CUDA.randn(T, Vmps ⊗ Vmpo' ← Vmps) - GR = CUDA.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 = CUDA.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 = CUDA.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 = CUDA.randn(T, Vmera ⊗ Vmera ← Vmera ⊗ Vmera) - w = CUDA.randn(T, Vmera ⊗ Vmera ← Vmera) - ρ = CUDA.randn(T, Vmera ⊗ Vmera ⊗ Vmera ← Vmera ⊗ Vmera ⊗ Vmera) - h = CUDA.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′ - end - end - - @testset "Issue 93" begin - T = Float64 - V1 = ℂ^2 - V2 = ℂ^3 - t1 = CUDA.randn(T, V1 ← V2) - t2 = CUDA.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 = CUDA.randn(T, V ← V) - B = CUDA.randn(T, V ← V') - C = CUDA.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(CuArray, C), adapt(CuArray, A), p, q, α, β) + @test adapt(Vector{ComplexF64}, Cgpu) ≈ planartrace!(C, A, p, q, α, β) end end diff --git a/test/cuda/tensors.jl b/test/cuda/tensors.jl index ec351d09f..e3b754322 100644 --- a/test/cuda/tensors.jl +++ b/test/cuda/tensors.jl @@ -34,22 +34,6 @@ for V in spacelist @test domain(t) == one(W) @test typeof(t) == TensorMap{Float64, spacetype(t), 5, 0, CuVector{Float64, CUDA.DeviceMemory}} end - for f in (Base.ones, Base.zeros) - t = @constinferred f(CuVector{Float64, CUDA.DeviceMemory}, W) - @test scalartype(t) == Float64 - @test codomain(t) == W - @test space(t) == (W ← one(W)) - @test domain(t) == one(W) - @test typeof(t) == TensorMap{Float64, spacetype(t), 5, 0, CuVector{Float64, CUDA.DeviceMemory}} - end - for f in (rand, randn) - t = @constinferred f(CuVector{Float64, CUDA.DeviceMemory}, W) - @test scalartype(t) == Float64 - @test codomain(t) == W - @test space(t) == (W ← one(W)) - @test domain(t) == one(W) - @test typeof(t) == TensorMap{Float64, spacetype(t), 5, 0, CuVector{Float64, CUDA.DeviceMemory}} - end for f! in (cuRAND.rand!, cuRAND.randn!) t = @constinferred CUDA.zeros(W) f!(t) @@ -61,25 +45,8 @@ for V in spacelist end for T in (Int, Float32, Float64, ComplexF32, ComplexF64) t = @constinferred CUDA.zeros(T, W) - CUDA.@allowscalar begin - @test @constinferred(hash(t)) == hash(deepcopy(t)) - end @test scalartype(t) == T - @test norm(t) == 0 - @test codomain(t) == W - @test space(t) == (W ← one(W)) - @test domain(t) == one(W) @test typeof(t) == TensorMap{T, spacetype(t), 5, 0, CuVector{T, CUDA.DeviceMemory}} - # blocks - bs = @constinferred blocks(t) - (c, b1), state = @constinferred Nothing iterate(bs) - @test c == first(blocksectors(W)) - next = @constinferred Nothing iterate(bs, state) - b2 = @constinferred block(t, first(blocksectors(t))) - @test b1 == b2 - @test eltype(bs) === Pair{typeof(c), typeof(b1)} - @test typeof(b1) === TensorKit.blocktype(t) - @test typeof(c) === sectortype(t) end end @timedtestset "Conversion to/from host" begin @@ -102,264 +69,6 @@ for V in spacelist @test domain(t2) == one(W) end end - @timedtestset "Adapt" begin - W = V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)' - for T in (Int, Float32, ComplexF64) - t = rand(T, W) - t_gpu = @constinferred adapt(CuArray, t) - @test storagetype(t_gpu) <: CuArray{T} - @test scalartype(t_gpu) === scalartype(t) - @test collect(t_gpu.data) == t.data - - t_cpu = @constinferred adapt(Array, t_gpu) - @test t_cpu == t - @test storagetype(t_cpu) <: Array{T} - end - end - @timedtestset "Adapt SectorVector" begin - for T in (Float32, ComplexF64) - v = TensorKit.SectorVector{T}(undef, V1) - rand!(parent(v)) - v_gpu = @constinferred adapt(CuArray, v) - @test v_gpu isa TensorKit.SectorVector{T, I, <:CuArray{T}} - @test v_gpu.structure == v.structure - v_cpu = @constinferred adapt(Array, v_gpu) - @test v_cpu isa TensorKit.SectorVector{T, I, Vector{T}} - @test parent(v_cpu) == parent(v) - t = CUDA.randn(T, V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)') - s = svd_vals(t) - s_cpu = adapt(Array, s) - @test parent(s_cpu) == collect(parent(s)) - @test all(c -> block(s_cpu, c) == collect(block(s, c)), blocksectors(s)) - end - end - @timedtestset "Tensor Dict conversion" begin - W = V1 ⊗ V2 ← (V3 ⊗ V4 ⊗ V5)' - for T in (Int, Float32, ComplexF64) - t = @constinferred cuRAND.rand(T, W) - d = convert(Dict, t) - @test convert(Dict, adapt(Array, t)) == d - end - end - symmetricbraiding && @timedtestset "Basic linear algebra" begin - W = V1 ⊗ V2 ← (V3 ⊗ V4 ⊗ V5)' - for T in (Float32, ComplexF64) - t = @constinferred cuRAND.rand(T, W) - @test scalartype(t) == T - @test space(t) == W - @test space(t') == W' - @test dim(t) == dim(space(t)) - @test codomain(t) == codomain(W) - @test domain(t) == domain(W) - # blocks for adjoint - bs = @constinferred blocks(t') - (c, b1), state = @constinferred Nothing iterate(bs) - @test c == first(blocksectors(W')) - next = @constinferred Nothing iterate(bs, state) - b2 = @constinferred block(t', first(blocksectors(t'))) - @test b1 == b2 - @test eltype(bs) === Pair{typeof(c), typeof(b1)} - @test typeof(b1) === TensorKit.blocktype(t') - @test typeof(c) === sectortype(t) - # linear algebra - @test isa(@constinferred(norm(t)), real(T)) - @test norm(t)^2 ≈ dot(t, t) - α = rand(T) - @test norm(α * t) ≈ abs(α) * norm(t) - @test norm(t + t, 2) ≈ 2 * norm(t, 2) - @test norm(t + t, 1) ≈ 2 * norm(t, 1) - @test norm(t + t, Inf) ≈ 2 * norm(t, Inf) - p = 3 * rand(Float64) - @test norm(t + t, p) ≈ 2 * norm(t, p) - @test norm(t) ≈ norm(t') - for p in (1, 2, 3 * rand(Float64), Inf) - tn = @constinferred normalize(t, p) - @test storagetype(tn) == storagetype(t) - @test norm(tn, p) ≈ 1 - @test tn ≈ t / norm(t, p) - @test normalize!(copy(t), p) ≈ tn - @test normalize(t', p) ≈ tn' - ta = copy(t)' - @test normalize!(ta, p) === ta - @test ta ≈ tn' - end - - t2 = @constinferred rand!(similar(t)) - β = rand(T) - #@test @constinferred(dot(β * t2, α * t)) ≈ conj(β) * α * conj(dot(t, t2)) # broken for Irrep[CU₁] - @test dot(β * t2, α * t) ≈ conj(β) * α * conj(dot(t, t2)) - @test dot(t2, t) ≈ conj(dot(t, t2)) - @test dot(t2, t) ≈ conj(dot(t2', t')) - @test dot(t2, t) ≈ dot(t', t2') - - i1 = @constinferred(isomorphism(CuVector{T, CUDA.DeviceMemory}, V1 ⊗ V2, V2 ⊗ V1)) - i2 = @constinferred(isomorphism(CuVector{T, CUDA.DeviceMemory}, V2 ⊗ V1, V1 ⊗ V2)) - @test i1 * i2 == @constinferred(id(CuVector{T, CUDA.DeviceMemory}, V1 ⊗ V2)) - @test i2 * i1 == @constinferred(id(CuVector{T, CUDA.DeviceMemory}, V2 ⊗ V1)) - w = @constinferred(isometry(CuVector{T, CUDA.DeviceMemory}, V1 ⊗ (oneunit(V1) ⊕ oneunit(V1)), V1)) - @test dim(w) == 2 * dim(V1 ← V1) - @test w' * w == id(CuVector{T, CUDA.DeviceMemory}, V1) - @test w * w' == (w * w')^2 - end - end - @timedtestset "Trivial space insertion and removal" begin - W = V1 ⊗ V2 ← (V3 ⊗ V4 ⊗ V5)' - for T in (Float32, ComplexF64) - t = @constinferred cuRAND.rand(T, W) - t2 = @constinferred insertleftunit(t) - @test t2 == @constinferred insertrightunit(t) - @test numind(t2) == numind(t) + 1 - @test space(t2) == insertleftunit(space(t)) - @test scalartype(t2) === T - @test t.data === t2.data - @test @constinferred(removeunit(t2, $(numind(t2)))) == t - t3 = @constinferred insertleftunit(t; copy = true) - @test t3 == @constinferred insertrightunit(t; copy = true) - @test t.data !== t3.data - for (c, b) in blocks(t) - @test b == block(t3, c) - end - @test @constinferred(removeunit(t3, $(numind(t3)))) == t - t4 = @constinferred insertrightunit(t, 3; dual = true) - @test_broken numin(t4) == numin(t) && numout(t4) == numout(t) + 1 - for (c, b) in blocks(t) - @test b == block(t4, c) - end - @test @constinferred(removeunit(t4, 4)) == t - t5 = @constinferred insertleftunit(t, 4; dual = true) - @test numin(t5) == numin(t) + 1 && numout(t5) == numout(t) - for (c, b) in blocks(t) - @test b == block(t5, c) - end - @test @constinferred(removeunit(t5, 4)) == t - end - end - if hasfusiontensor(I) - @timedtestset "Basic linear algebra: test via CPU" begin - W = V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)' - for T in (Float32, ComplexF64) - t = cuRAND.rand(T, W) - t2 = @constinferred cuRAND.rand!(similar(t)) - α = rand(T) - @test norm(t, 2) ≈ norm(adapt(Array, t), 2) - @test dot(t2, t) ≈ dot(adapt(Array, t2), adapt(Array, t)) - @test adapt(Vector{T}, (α * t)) ≈ α * adapt(Vector{T}, t) - @test adapt(Vector{T}, (t + t)) ≈ 2 * adapt(Vector{T}, t) - end - end - @timedtestset "Real and imaginary parts" begin - W = V1 ⊗ V2 - for T in (Float64, ComplexF64, ComplexF32) - t = @constinferred cuRAND.randn(T, W, W) - - tr = @constinferred real(t) - @test scalartype(tr) <: Real - @test real(adapt(Array, t)) == adapt(Array, tr) - @test storagetype(tr) == CuVector{real(T), CUDA.DeviceMemory} - - ti = @constinferred imag(t) - @test scalartype(ti) <: Real - @test imag(adapt(Array, t)) == adapt(Array, ti) - @test storagetype(ti) == CuVector{real(T), CUDA.DeviceMemory} - - tc = @inferred complex(t) - @test scalartype(tc) <: Complex - @test complex(adapt(Array, t)) == adapt(Array, tc) - @test storagetype(tc) == CuVector{complex(T), CUDA.DeviceMemory} - - tc2 = @inferred complex(tr, ti) - @test tc2 ≈ tc - @test storagetype(tc2) == CuVector{complex(T), CUDA.DeviceMemory} - end - end - end - @timedtestset "Tensor conversion" begin - W = V1 ⊗ V2 - t = @constinferred cuRAND.randn(W ← W) - @test typeof(convert(typeof(t), t')) == typeof(t) - @test typeof(adapt(Array, t')) == typeof(adapt(Array, t)') - tc = complex(t) - @test convert(typeof(tc), t) == tc - @test typeof(convert(typeof(tc), t)) == typeof(tc) - @test typeof(convert(typeof(tc), t')) == typeof(tc) - @test Base.promote_typeof(t, tc) == typeof(tc) - @test Base.promote_typeof(tc, t) == typeof(tc + t) - end - #=@timedtestset "diag/diagm" begin - W = V1 ⊗ V2 ⊗ V3 ← V4 ⊗ V5 - t = cuRAND.randn(ComplexF64, W) - d = LinearAlgebra.diag(t) - # TODO find a way to use CUDA here - D = LinearAlgebra.diagm(codomain(t), domain(t), d) - @test LinearAlgebra.isdiag(D) - @test LinearAlgebra.diag(D) == d - end=# - symmetricbraiding && @timedtestset "Permutations: test via inner product invariance" begin - W = V1 ⊗ V2 ⊗ V3 ⊗ V4 ⊗ V5 - t = cuRAND.rand(ComplexF64, W) - ht = adapt(Vector{ComplexF64}, t) - t′ = cuRAND.randn!(similar(t)) - ht′ = adapt(Vector{ComplexF64}, t′) - dot_htt′ = dot(ht′, ht) - dot_tt′ = dot(t′, t) - @test dot_tt′ ≈ dot_htt′ - norm_t = norm(t) - for k in 0:5 - for p in permutations(1:5) - p1 = ntuple(n -> p[n], k) - p2 = ntuple(n -> p[k + n], 5 - k) - t2 = @constinferred permute(t, (p1, p2)) - t2′ = permute(t′, (p1, p2)) - @test norm(t2) ≈ norm_t - @test dot(t2′, t2) ≈ dot_tt′ - @test dot(transpose(t2′), transpose(t2)) ≈ dot_tt′ - end - t3 = @constinferred repartition(t, $k) - t3 = repartition(t, k) - @test norm(t3) ≈ norm(t) - t3′ = @constinferred repartition!(similar(t3), t′) - @test norm(t3′) ≈ norm(t′) - @test dot(t′, t) ≈ dot(t3′, t3) - end - end - symmetricbraiding && @timedtestset "Permutations: test via CPU" begin - W = V1 ⊗ V2 ⊗ V3 ⊗ V4 ⊗ V5 - t = cuRAND.rand(ComplexF64, W) - for k in 0:5 - for p in permutations(1:5) - p1 = ntuple(n -> p[n], k) - p2 = ntuple(n -> p[k + n], 5 - k) - dt2 = permute(t, (p1, p2)) - ht2 = permute(adapt(Array, t), (p1, p2)) - @test ht2 ≈ adapt(Array, dt2) - end - dt3 = repartition(t, k) - ht3 = repartition(adapt(Array, t), k) - @test ht3 ≈ adapt(Array, dt3) - end - end - hasbraiding && @timedtestset "Twists: test via CPU" begin - t = cuRAND.rand(ComplexF64, V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)') - ht = adapt(Array, t) - for inds in (1, (2, 4), [1, 3, 5]), inv in (false, true) - ref = twist(ht, inds; inv) - @test adapt(Array, twist(t, inds; inv)) ≈ ref - @test adapt(Array, twist!(copy(t), inds; inv)) ≈ ref - # adjoint operands - @test adapt(Array, twist(t', inds; inv)) ≈ twist(copy(ht'), inds; inv) - t2 = copy(t) - twist!(t2', inds; inv) - @test adapt(Array, t2)' ≈ twist(copy(ht'), inds; inv) - end - @test twist(twist(t, 2), 2; inv = true) ≈ t - twistmask = (true, false, true, false, true) - if !TensorKit.has_shared_twist(t, twistmask) - p = (codomainind(t), domainind(t)) - twister = TensorKit.treetwister(storagetype(t), space(t), space(t), p, false, twistmask, false) - @test twister isa getglobal(GPUArraysExt, :DeviceUniqueTreeTransformer) - @test twister === TensorKit.treetwister(storagetype(t), space(t), space(t), p, false, twistmask, false) - end - end symmetricbraiding && @timedtestset "Full trace: test self-consistency" begin t = cuRAND.rand(ComplexF64, V1 ⊗ V2' ⊗ V2 ⊗ V1') t2 = permute(t, ((1, 2), (4, 3))) @@ -378,87 +87,6 @@ for V in spacelist @test ss ≈ s2 @test ss ≈ s3 end - symmetricbraiding && @timedtestset "Partial trace: test self-consistency" begin - t = cuRAND.rand(ComplexF64, V1 ⊗ V2' ⊗ V3 ⊗ V2 ⊗ V1' ⊗ V3') - @tensor t2[a, b] := t[c, d, b, d, c, a] - @tensor t4[a, b, c, d] := t[d, e, b, e, c, a] - @tensor t5[a, b] := t4[a, b, c, c] - @test t2 ≈ t5 - end - symmetricbraiding && @timedtestset "Trace: test via conversion" begin - t = cuRAND.rand(ComplexF64, V1 ⊗ V2' ⊗ V3 ⊗ V2 ⊗ V1' ⊗ V3') - @tensor t2[a, b] := t[c, d, b, d, c, a] - @tensor t3[a, b] := ad(t)[c, d, b, d, c, a] - @test t3 ≈ ad(t2) - end - symmetricbraiding && @timedtestset "Trace and contraction" begin - t1 = cuRAND.rand(ComplexF64, V1 ⊗ V2 ⊗ V3) - t2 = cuRAND.rand(ComplexF64, V2' ⊗ V4 ⊗ V1') - t3 = t1 ⊗ t2 - @tensor ta[a, b] := t1[x, y, a] * t2[y, b, x] - @tensor tb[a, b] := t3[x, y, a, y, b, x] - @test ta ≈ tb - end - if BraidingStyle(I) isa Bosonic && hasfusiontensor(I) - @timedtestset "Tensor contraction: test via CPU" begin - dA1 = cuRAND.randn(ComplexF64, V1' * V2', V3') - dA2 = cuRAND.randn(ComplexF64, V3 * V4, V5) - drhoL = cuRAND.randn(ComplexF64, V1, V1) - drhoR = cuRAND.randn(ComplexF64, V5, V5)' # test adjoint tensor - dH = cuRAND.randn(ComplexF64, V2 * V4, V2 * V4) - @tensor dHrA12[a, s1, s2, c] := drhoL[a, a'] * conj(dA1[a', t1, b]) * - dA2[b, t2, c'] * drhoR[c', c] * - dH[s1, s2, t1, t2] - @tensor hHrA12[a, s1, s2, c] := adapt(Array, drhoL)[a, a'] * conj(adapt(Array, dA1)[a', t1, b]) * - adapt(Array, dA2)[b, t2, c'] * adapt(Array, drhoR)[c', c] * - adapt(Array, dH)[s1, s2, t1, t2] - @test adapt(Array, dHrA12) ≈ hHrA12 - end - end - hasbraiding && @timedtestset "Index flipping: test flipping inverse" begin - t = cuRAND.rand(ComplexF64, V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)') - for i in 1:5 - @test t ≈ flip(flip(t, i), i; inv = true) - @test t ≈ flip(flip(t, i; inv = true), i) - end - end - symmetricbraiding && @timedtestset "Index flipping: test via explicit flip" begin - t = cuRAND.rand(ComplexF64, V1 ⊗ V1' ← V1' ⊗ V1) - F1 = adapt(CuArray{ComplexF64}, unitary(flip(V1), V1)) - - @tensor tf[a, b; c, d] := F1[a, a'] * t[a', b; c, d] - @test flip(t, 1) ≈ tf - @tensor tf[a, b; c, d] := conj(F1[b, b']) * t[a, b'; c, d] - @test twist!(flip(t, 2), 2) ≈ tf - @tensor tf[a, b; c, d] := F1[c, c'] * t[a, b; c', d] - @test flip(t, 3) ≈ tf - @tensor tf[a, b; c, d] := conj(F1[d, d']) * t[a, b; c, d'] - @test twist!(flip(t, 4), 4) ≈ tf - end - symmetricbraiding && @timedtestset "Index flipping: test via contraction" begin - t1 = cuRAND.rand(ComplexF64, V1 ⊗ V2 ⊗ V3 ← V4) - t2 = cuRAND.rand(ComplexF64, V2' ⊗ V5 ← V4' ⊗ V1) - @tensor ta[a, b] := t1[x, y, a, z] * t2[y, b, z, x] - @tensor tb[a, b] := flip(t1, 1)[x, y, a, z] * flip(t2, 4)[y, b, z, x] - @test ta ≈ tb - @tensor tb[a, b] := flip(t1, (2, 4))[x, y, a, z] * flip(t2, (1, 3))[y, b, z, x] - @test ta ≈ tb - @tensor tb[a, b] := flip(t1, (1, 2, 4))[x, y, a, z] * flip(t2, (1, 3, 4))[y, b, z, x] - @tensor tb[a, b] := flip(t1, (1, 3))[x, y, a, z] * flip(t2, (2, 4))[y, b, z, x] - @test flip(ta, (1, 2)) ≈ tb - end - @timedtestset "Multiplication of isometries: test properties" begin - W1 = V1 ⊗ V2 ⊗ V3 - W2 = (V4 ⊗ V5)' - for T in (Float64, ComplexF64) - t1 = randisometry(CuMatrix{T}, W1, W2) - t2 = randisometry(CuMatrix{T}, W2 ← W2) - @test isisometric(t1) - @test isunitary(t2) - P = t1 * t1' - @test P * P ≈ P - end - end @timedtestset "Multiplication and inverse: test compatibility" begin W1 = V1 ⊗ V2 ⊗ V3 W2 = (V4 ⊗ V5)' @@ -574,148 +202,34 @@ for V in spacelist # end # # TODO - @timedtestset "Tensor product: test via norm preservation" begin - for T in (ComplexF64,) # Float32 case broken because of cuTENSOR - t1 = cuRAND.rand(T, V1, V5') - t2 = cuRAND.rand(T, V2 ⊗ V3, V4') - t = @constinferred (t1 ⊗ t2) - @test norm(t) ≈ norm(t1) * norm(t2) - end - end - symmetricbraiding && @timedtestset "Tensor product: test via conversion" begin - for T in (Float32, ComplexF64) - t1 = cuRAND.rand(T, V1, V5') - t2 = cuRAND.rand(T, V2 ⊗ V3, V4') - d1 = dim(codomain(t1)) - d2 = dim(codomain(t2)) - d3 = dim(domain(t1)) - d4 = dim(domain(t2)) - t = @constinferred (t1 ⊗ t2) - At = ad(t) - @test ad(t) ≈ ad(t1) ⊗ ad(t2) - end - end - symmetricbraiding && @timedtestset "Tensor product: test via tensor contraction" begin - for T in (Float32, ComplexF64) - t1 = cuRAND.rand(T, V1, V5') - t2 = cuRAND.rand(T, V2 ⊗ V3, V4') - t = @constinferred (t1 ⊗ t2) - @tensor t′[1 2 3; 4 5] := t1[1; 4] * t2[2 3; 5] - @test t ≈ t′ - end - end end TensorKit.empty_globalcaches!() end -@timedtestset "Deligne tensor product: test via conversion" begin - using .TestSetup: Vtr, VRepℤ₂, VRepSU₂, VRepA4 - @testset for Vlist1 in (Vtr, VRepSU₂), Vlist2 in (VRepℤ₂, VRepA4) - V1, V2, V3, V4, V5 = Vlist1 - W1, W2, W3, W4, W5 = Vlist2 - for T in (Float32, ComplexF64) - t1 = CUDA.rand(T, V2 ⊗ V3, (V4 ⊗ V5)') - t2 = CUDA.rand(T, W2, (W3 ⊗ W4)') - t = @constinferred (t1 ⊠ t2) - d1 = dim(codomain(t1)) - d2 = dim(codomain(t2)) - d3 = dim(domain(t1)) - d4 = dim(domain(t2)) - At = convert(Array, adapt(Vector{T}, t)) - @test reshape(At, (d1, d2, d3, d4)) ≈ - reshape(convert(Array, adapt(Vector{T}, t1)), (d1, 1, d3, 1)) .* - reshape(convert(Array, adapt(Vector{T}, t2)), (1, d2, 1, d4)) - end - end -end - -@timedtestset "DiagonalTensorMap index manipulations with domain $V: test via CPU" for V in ( +@timedtestset "DiagonalTensorMap with domain $V: test via CPU" for V in ( (ℂ^4)', Vect[Z2Irrep](0 => 2, 1 => 3), Vect[U1Irrep](0 => 2, 1 => 2, -1 => 1), Vect[FermionParity](0 => 2, 1 => 3), Vect[SU2Irrep](0 => 2, 1 => 1)', ) - function test_tensoradd(dst, d, p, conjd, α, β) - cpu = TO.tensoradd!(adapt(Array, dst), adapt(Array, d), p, conjd, α, β, TO.DefaultBackend(), TO.DefaultAllocator()) - gpu = TO.tensoradd!(copy(dst), d, p, conjd, α, β, TO.DefaultBackend(), TO.DefaultAllocator()) - @test gpu isa typeof(dst) - @test adapt(Array, gpu) ≈ cpu - return nothing - end diagonal(T, W) = DiagonalTensorMap(CuVector{T}(rand(T, reduceddim(W))), W) - @testset "eltype $T" for T in (Float64, ComplexF64) + @testset "normalize, eltype $T" for T in (Float64, ComplexF64) d = diagonal(T, V) dcpu = adapt(Array, d) - scalars = ((TensorKit.One(), TensorKit.Zero()), (T(2), T(3))) - for p in (1, 2, 3 * rand(Float64), Inf) dn = @constinferred normalize(d, p) @test dn isa typeof(d) @test adapt(Array, dn) ≈ normalize(dcpu, p) @test adapt(Array, normalize!(copy(d), p)) ≈ normalize(dcpu, p) end - - for p in (((), (1, 2)), ((1, 2), ()), ((2, 1), ()), ((), (2, 1)), ((1,), (2,)), ((2,), (1,))), conjd in (false, true) - W = TO.tensoradd_structure(dcpu, p, conjd) - dst = adapt(CuVector{T}, randn(T, W)) - for (α, β) in scalars - test_tensoradd(dst, d, p, conjd, α, β) - end - end - for p in (((1,), (2,)), ((2,), (1,))), conjd in (false, true) - W = domain(TO.tensoradd_structure(dcpu, p, conjd))[1] - for (α, β) in scalars - test_tensoradd(diagonal(T, W), d, p, conjd, α, β) - end - end - for p in (((), (1, 2)), ((1, 2), ()), ((2, 1), ()), ((2,), (1,))) - @test adapt(Array, permute(d, p)) ≈ permute(dcpu, p) - @test adapt(Array, permute(d', p)) ≈ permute(dcpu', p) - end - for k in (0, 2) - @test adapt(Array, repartition(d, k)) ≈ repartition(dcpu, k) - @test adapt(Array, repartition(d', k)) ≈ repartition(dcpu', k) - end - for levels in ((1, 2), (2, 1)) - @test adapt(Array, braid(d, ((1, 2), ()), levels)) ≈ braid(dcpu, ((1, 2), ()), levels) - end - for inds in ((1,), (2,), (1, 2)), inv in (false, true) - ref = twist(dcpu, inds; inv) - @test twist(d, inds; inv) isa typeof(d) - @test adapt(Array, twist(d, inds; inv)) ≈ ref - @test adapt(Array, twist(d', inds; inv)) ≈ twist(dcpu', inds; inv) - @test adapt(Array, twist!(copy(d), inds; inv)) ≈ ref - dst = adapt(CuVector{T}, randn(T, space(dcpu))) - @test adapt(Array, twist!(copy(dst), d, inds, T(2), T(3); inv)) ≈ 3 * adapt(Array, dst) + 2 * ref - ddst = diagonal(T, V) - @test adapt(Array, twist!(copy(ddst), d, inds, T(2), T(3); inv)) ≈ 3 * adapt(Array, ddst) + 2 * ref - end end - @testset "real source, complex destination" begin + @testset "scalar contraction" begin d = diagonal(Float64, V) - p = ((1,), (2,)) - W = domain(TO.tensoradd_structure(adapt(Array, d), p, true))[1] - test_tensoradd(diagonal(ComplexF64, W), d, p, true, TensorKit.One(), TensorKit.Zero()) - test_tensoradd(diagonal(ComplexF64, W), d, p, true, ComplexF64(2), ComplexF64(3)) - end - - @testset "contractions" begin - d = diagonal(Float64, V) - t = adapt(CuVector{ComplexF64}, randn(ComplexF64, V ⊗ V ← V)) s = adapt(CuVector{Float64}, randn(Float64, V ← V)) - dcpu, tcpu, scpu = adapt(Array, d), adapt(Array, t), adapt(Array, s) - - @tensor gpu[-1 -2; -3] := t[-1 -2; 1] * d[1; -3] - @tensor cpu[-1 -2; -3] := tcpu[-1 -2; 1] * dcpu[1; -3] - @test adapt(Array, gpu) ≈ cpu - - @tensor gpu[-1 -2; -3] := d[-1; 1] * t[1 -2; -3] - @tensor cpu[-1 -2; -3] := dcpu[-1; 1] * tcpu[1 -2; -3] - @test adapt(Array, gpu) ≈ cpu - + dcpu, scpu = adapt(Array, d), adapt(Array, s) @test (@tensor s[1; 2] * d[2; 1]) ≈ (@tensor scpu[1; 2] * dcpu[2; 1]) end end diff --git a/test/jlarrays/planar.jl b/test/jlarrays/planar.jl new file mode 100644 index 000000000..7c32862ea --- /dev/null +++ b/test/jlarrays/planar.jl @@ -0,0 +1,335 @@ +using Test, TestExtras +using Adapt +using TensorOperations, JLArrays +using Random +using TensorKit +using TensorKit: type_repr +using TensorKit: PlanarTrivial, ℙ +using TensorKit: planaradd!, planartrace!, planarcontract! + +Random.seed!(123456) + +spacelist = default_spacelist(fast_tests) + +for V in spacelist + I = sectortype(first(V)) + Istr = type_repr(I) + BraidingStyle(I) isa NoBraiding && continue + @timedtestset "Braiding tensor + CUDA with symmetry: $Istr" verbose = true begin + W = V[1] ⊗ V[2] ← V[2] ⊗ V[1] + T = isreal(sectortype(W)) ? Float64 : ComplexF64 + t1 = @constinferred BraidingTensor{T, spacetype(V[2]), JLVector{T}}(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) == JLVector{scalartype(t1)} + t2 = @constinferred BraidingTensor{ComplexF64, spacetype(V[2]), JLVector{ComplexF64}}(W) + @test scalartype(t2) == ComplexF64 + @test storagetype(t2) == JLVector{ComplexF64} + 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 + + t3 = @inferred TensorMap(t2) + @test storagetype(t3) == JLVector{ComplexF64} + t4 = braid(adapt(JLArray, 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 + + JLArrays.@allowscalar begin + for (f1, f2) in fusiontrees(t1) + @test t1[f1, f2] ≈ t3[f1, f2] + end + end + + t5 = @inferred TensorMap(t2') + @test storagetype(t5) == JLVector{ComplexF64} + t6 = braid(adapt(JLArray, id(scalartype(t2), domain(t2'))), ((2, 1), (3, 4)), (4, 3, 2, 1)) + JLArrays.@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 + +for V in spacelist + I = sectortype(first(V)) + Istr = type_repr(I) + @timedtestset "planartrace + JLArrays 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(JLArray, C), adapt(JLArray, A), p, q, α, β) + @test adapt(Vector{ComplexF64}, Cgpu) ≈ planartrace!(C, A, p, q, α, β) + end + end +end + +@testset "planar methods" verbose = true begin + @testset "planaradd" begin + A = adapt(JLArray, randn(ℂ^2 ⊗ ℂ^3 ← ℂ^6 ⊗ ℂ^5 ⊗ ℂ^4)) + C = adapt(JLArray, 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 = adapt(JLArray, randn(ℂ^2 ⊗ ℂ^3 ← ℂ^2 ⊗ ℂ^5 ⊗ ℂ^4)) + C = adapt(JLArray, 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 "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(JLArray, C), adapt(JLArray, A), p, q, α, β) + @test adapt(Vector{ComplexF64}, Cgpu) ≈ planartrace!(C, A, p, q, α, β) + end + end + + @testset "planarcontract" begin + A = adapt(JLArray, randn(ℂ^2 ⊗ ℂ^3 ← ℂ^2 ⊗ ℂ^5 ⊗ ℂ^4)) + B = adapt(JLArray, randn(ℂ^2 ⊗ ℂ^4 ← ℂ^4 ⊗ ℂ^3)) + C = adapt(JLArray, 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) + end +end + +@testset "@planar" verbose = true begin + T = ComplexF64 + + @testset "contractcheck" begin + V = ℂ^2 + A = adapt(JLArray, rand(T, V ⊗ V ← V)) + B = adapt(JLArray, 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 = adapt(JLArray, 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 = adapt(JLArray, rand(T, V ← V ⊗ V)) + B = adapt(JLArray, 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 = adapt(JLArray, randn(T, Vmps ⊗ P ← Vmps)) + O = adapt(JLArray, randn(T, Vmpo ⊗ P ← P ⊗ Vmpo)) + GL = adapt(JLArray, randn(T, Vmps ⊗ Vmpo' ← Vmps)) + GR = adapt(JLArray, 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 = adapt(JLArray, 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 = adapt(JLArray, 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 = adapt(JLArray, randn(T, Vmera ⊗ Vmera ← Vmera ⊗ Vmera)) + w = adapt(JLArray, randn(T, Vmera ⊗ Vmera ← Vmera)) + ρ = adapt(JLArray, randn(T, Vmera ⊗ Vmera ⊗ Vmera ← Vmera ⊗ Vmera ⊗ Vmera)) + h = adapt(JLArray, 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′ + end + end + + @testset "Issue 93" begin + T = Float64 + V1 = ℂ^2 + V2 = ℂ^3 + t1 = adapt(JLArray, randn(T, V1 ← V2)) + t2 = adapt(JLArray, 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 = adapt(JLArray, randn(T, V ← V)) + B = adapt(JLArray, randn(T, V ← V')) + C = adapt(JLArray, 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 + end +end diff --git a/test/jlarrays/tensors.jl b/test/jlarrays/tensors.jl new file mode 100644 index 000000000..41dd0f083 --- /dev/null +++ b/test/jlarrays/tensors.jl @@ -0,0 +1,672 @@ +using Adapt, JLArrays +using Test, TestExtras +using Random +using TensorKit, Combinatorics +using TensorOperations: TensorOperations as TO +ad = adapt(Array) +const GPUArraysExt = Base.get_extension(TensorKit, :TensorKitGPUArraysExt) +@assert !isnothing(GPUArraysExt) + +Random.seed!(123456) + +spacelist = default_spacelist(fast_tests) + +for V in spacelist + I = sectortype(first(V)) + Istr = TensorKit.type_repr(I) + println("---------------------------------------") + println("JLArrays Tensors with symmetry: $Istr") + println("---------------------------------------") + hasbraiding = BraidingStyle(I) isa HasBraiding + symmetricbraiding = BraidingStyle(I) isa SymmetricBraiding + @timedtestset "Tensors with symmetry: $Istr" verbose = true begin + V1, V2, V3, V4, V5 = V + @timedtestset "Basic tensor properties" begin + W = V1 ⊗ V2 ⊗ V3 ⊗ V4 ⊗ V5 + for f in (Base.ones, Base.zeros) + t = @constinferred f(JLVector{Float64}, W) + @test scalartype(t) == Float64 + @test codomain(t) == W + @test space(t) == (W ← one(W)) + @test domain(t) == one(W) + @test typeof(t) == TensorMap{Float64, spacetype(t), 5, 0, JLVector{Float64}} + end + for f in (rand, randn) + t = @constinferred f(JLVector{Float64}, W) + @test scalartype(t) == Float64 + @test codomain(t) == W + @test space(t) == (W ← one(W)) + @test domain(t) == one(W) + @test typeof(t) == TensorMap{Float64, spacetype(t), 5, 0, JLVector{Float64}} + end + for T in (Int, Float32, Float64, ComplexF32, ComplexF64) + t = @constinferred adapt(JLArray, zeros(T, W)) + JLArrays.@allowscalar begin + @test @constinferred(hash(t)) == hash(deepcopy(t)) + end + @test scalartype(t) == T + @test norm(t) == 0 + @test codomain(t) == W + @test space(t) == (W ← one(W)) + @test domain(t) == one(W) + @test typeof(t) == TensorMap{T, spacetype(t), 5, 0, JLVector{T}} + # blocks + bs = @constinferred blocks(t) + (c, b1), state = @constinferred Nothing iterate(bs) + @test c == first(blocksectors(W)) + next = @constinferred Nothing iterate(bs, state) + b2 = @constinferred block(t, first(blocksectors(t))) + @test b1 == b2 + @test eltype(bs) === Pair{typeof(c), typeof(b1)} + @test typeof(b1) === TensorKit.blocktype(t) + @test typeof(c) === sectortype(t) + end + end + @timedtestset "Adapt" begin + W = V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)' + for T in (Int, Float32, ComplexF64) + t = rand(T, W) + t_gpu = @constinferred adapt(JLArray, t) + @test storagetype(t_gpu) <: JLArray{T} + @test scalartype(t_gpu) === scalartype(t) + @test collect(t_gpu.data) == t.data + + t_cpu = @constinferred adapt(Array, t_gpu) + @test t_cpu == t + @test storagetype(t_cpu) <: Array{T} + end + end + @timedtestset "Adapt SectorVector" begin + for T in (Float32, ComplexF64) + v = TensorKit.SectorVector{T}(undef, V1) + rand!(parent(v)) + v_gpu = @constinferred adapt(JLArray, v) + @test v_gpu isa TensorKit.SectorVector{T, I, <:JLArray{T}} + @test v_gpu.structure == v.structure + v_cpu = @constinferred adapt(Array, v_gpu) + @test v_cpu isa TensorKit.SectorVector{T, I, Vector{T}} + @test parent(v_cpu) == parent(v) + t = JLArrays.randn(T, V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)') + s = svd_vals(t) + s_cpu = adapt(Array, s) + @test parent(s_cpu) == collect(parent(s)) + @test all(c -> block(s_cpu, c) == collect(block(s, c)), blocksectors(s)) + end + end + @timedtestset "Tensor Dict conversion" begin + W = V1 ⊗ V2 ← (V3 ⊗ V4 ⊗ V5)' + for T in (Int, Float32, ComplexF64) + t = @constinferred adapt(JLArray, rand(T, W)) + d = convert(Dict, t) + @test convert(Dict, adapt(Array, t)) == d + end + end + symmetricbraiding && @timedtestset "Basic linear algebra" begin + W = V1 ⊗ V2 ← (V3 ⊗ V4 ⊗ V5)' + for T in (Float32, ComplexF64) + t = @constinferred adapt(JLArray, rand(T, W)) + @test scalartype(t) == T + @test space(t) == W + @test space(t') == W' + @test dim(t) == dim(space(t)) + @test codomain(t) == codomain(W) + @test domain(t) == domain(W) + # blocks for adjoint + bs = @constinferred blocks(t') + (c, b1), state = @constinferred Nothing iterate(bs) + @test c == first(blocksectors(W')) + next = @constinferred Nothing iterate(bs, state) + b2 = @constinferred block(t', first(blocksectors(t'))) + @test b1 == b2 + @test eltype(bs) === Pair{typeof(c), typeof(b1)} + @test typeof(b1) === TensorKit.blocktype(t') + @test typeof(c) === sectortype(t) + # linear algebra + @test isa(@constinferred(norm(t)), real(T)) + @test norm(t)^2 ≈ dot(t, t) + α = rand(T) + @test norm(α * t) ≈ abs(α) * norm(t) + @test norm(t + t, 2) ≈ 2 * norm(t, 2) + @test norm(t + t, 1) ≈ 2 * norm(t, 1) + @test norm(t + t, Inf) ≈ 2 * norm(t, Inf) + p = 3 * rand(Float64) + @test norm(t + t, p) ≈ 2 * norm(t, p) + @test norm(t) ≈ norm(t') + for p in (1, 2, 3 * rand(Float64), Inf) + tn = @constinferred normalize(t, p) + @test storagetype(tn) == storagetype(t) + @test norm(tn, p) ≈ 1 + @test tn ≈ t / norm(t, p) + @test normalize!(copy(t), p) ≈ tn + @test normalize(t', p) ≈ tn' + ta = copy(t)' + @test normalize!(ta, p) === ta + @test ta ≈ tn' + end + + t2 = @constinferred rand!(similar(t)) + β = rand(T) + #@test @constinferred(dot(β * t2, α * t)) ≈ conj(β) * α * conj(dot(t, t2)) # broken for Irrep[CU₁] + @test dot(β * t2, α * t) ≈ conj(β) * α * conj(dot(t, t2)) + @test dot(t2, t) ≈ conj(dot(t, t2)) + @test dot(t2, t) ≈ conj(dot(t2', t')) + @test dot(t2, t) ≈ dot(t', t2') + + i1 = @constinferred(isomorphism(JLVector{T}, V1 ⊗ V2, V2 ⊗ V1)) + i2 = @constinferred(isomorphism(JLVector{T}, V2 ⊗ V1, V1 ⊗ V2)) + @test i1 * i2 == @constinferred(id(JLVector{T}, V1 ⊗ V2)) + @test i2 * i1 == @constinferred(id(JLVector{T}, V2 ⊗ V1)) + w = @constinferred(isometry(JLVector{T}, V1 ⊗ (oneunit(V1) ⊕ oneunit(V1)), V1)) + @test dim(w) == 2 * dim(V1 ← V1) + @test w' * w == id(JLVector{T}, V1) + @test w * w' == (w * w')^2 + end + end + @timedtestset "Trivial space insertion and removal" begin + W = V1 ⊗ V2 ← (V3 ⊗ V4 ⊗ V5)' + for T in (Float32, ComplexF64) + t = @constinferred adapt(JLArray, rand(T, W)) + t2 = @constinferred insertleftunit(t) + @test t2 == @constinferred insertrightunit(t) + @test numind(t2) == numind(t) + 1 + @test space(t2) == insertleftunit(space(t)) + @test scalartype(t2) === T + @test t.data === t2.data + @test @constinferred(removeunit(t2, $(numind(t2)))) == t + t3 = @constinferred insertleftunit(t; copy = true) + @test t3 == @constinferred insertrightunit(t; copy = true) + @test t.data !== t3.data + for (c, b) in blocks(t) + @test b == block(t3, c) + end + @test @constinferred(removeunit(t3, $(numind(t3)))) == t + t4 = @constinferred insertrightunit(t, 3; dual = true) + @test_broken numin(t4) == numin(t) && numout(t4) == numout(t) + 1 + for (c, b) in blocks(t) + @test b == block(t4, c) + end + @test @constinferred(removeunit(t4, 4)) == t + t5 = @constinferred insertleftunit(t, 4; dual = true) + @test numin(t5) == numin(t) + 1 && numout(t5) == numout(t) + for (c, b) in blocks(t) + @test b == block(t5, c) + end + @test @constinferred(removeunit(t5, 4)) == t + end + end + if hasfusiontensor(I) + @timedtestset "Basic linear algebra: test via CPU" begin + W = V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)' + for T in (Float32, ComplexF64) + t = adapt(JLArray, rand(T, W)) + t2 = @constinferred adapt(JLArray, rand!(similar(t))) + α = rand(T) + @test norm(t, 2) ≈ norm(adapt(Array, t), 2) + @test dot(t2, t) ≈ dot(adapt(Array, t2), adapt(Array, t)) + @test adapt(Vector{T}, (α * t)) ≈ α * adapt(Vector{T}, t) + @test adapt(Vector{T}, (t + t)) ≈ 2 * adapt(Vector{T}, t) + end + end + @timedtestset "Real and imaginary parts" begin + W = V1 ⊗ V2 + for T in (Float64, ComplexF64, ComplexF32) + t = @constinferred adapt(JLArray, randn(T, W, W)) + + tr = @constinferred real(t) + @test scalartype(tr) <: Real + @test real(adapt(Array, t)) == adapt(Array, tr) + @test storagetype(tr) == JLVector{real(T)} + + ti = @constinferred imag(t) + @test scalartype(ti) <: Real + @test imag(adapt(Array, t)) == adapt(Array, ti) + @test storagetype(ti) == JLVector{real(T)} + + tc = @inferred complex(t) + @test scalartype(tc) <: Complex + @test complex(adapt(Array, t)) == adapt(Array, tc) + @test storagetype(tc) == JLVector{complex(T)} + + tc2 = @inferred complex(tr, ti) + @test tc2 ≈ tc + @test storagetype(tc2) == JLVector{complex(T)} + end + end + end + @timedtestset "Tensor conversion" begin + W = V1 ⊗ V2 + t = @constinferred adapt(JLArray, randn(W ← W)) + @test typeof(convert(typeof(t), t')) == typeof(t) + @test typeof(adapt(Array, t')) == typeof(adapt(Array, t)') + tc = complex(t) + @test convert(typeof(tc), t) == tc + @test typeof(convert(typeof(tc), t)) == typeof(tc) + @test typeof(convert(typeof(tc), t')) == typeof(tc) + @test Base.promote_typeof(t, tc) == typeof(tc) + @test Base.promote_typeof(tc, t) == typeof(tc + t) + end + #=@timedtestset "diag/diagm" begin + W = V1 ⊗ V2 ⊗ V3 ← V4 ⊗ V5 + t = adapt(JLArray, randn(ComplexF64, W)) + d = LinearAlgebra.diag(t) + D = LinearAlgebra.diagm(codomain(t), domain(t), d) + @test LinearAlgebra.isdiag(D) + @test LinearAlgebra.diag(D) == d + end=# + symmetricbraiding && @timedtestset "Permutations: test via inner product invariance" begin + W = V1 ⊗ V2 ⊗ V3 ⊗ V4 ⊗ V5 + t = adapt(JLArray, rand(ComplexF64, W)) + ht = adapt(Vector{ComplexF64}, t) + t′ = adapt(JLArray, randn!(similar(t))) + ht′ = adapt(Vector{ComplexF64}, t′) + dot_htt′ = dot(ht′, ht) + dot_tt′ = dot(t′, t) + @test dot_tt′ ≈ dot_htt′ + norm_t = norm(t) + for k in 0:5 + for p in permutations(1:5) + p1 = ntuple(n -> p[n], k) + p2 = ntuple(n -> p[k + n], 5 - k) + t2 = @constinferred permute(t, (p1, p2)) + t2′ = permute(t′, (p1, p2)) + @test norm(t2) ≈ norm_t + @test dot(t2′, t2) ≈ dot_tt′ + @test dot(transpose(t2′), transpose(t2)) ≈ dot_tt′ + end + t3 = @constinferred repartition(t, $k) + t3 = repartition(t, k) + @test norm(t3) ≈ norm(t) + t3′ = @constinferred repartition!(similar(t3), t′) + @test norm(t3′) ≈ norm(t′) + @test dot(t′, t) ≈ dot(t3′, t3) + end + end + symmetricbraiding && @timedtestset "Permutations: test via CPU" begin + W = V1 ⊗ V2 ⊗ V3 ⊗ V4 ⊗ V5 + t = adapt(JLArray, rand(ComplexF64, W)) + for k in 0:5 + for p in permutations(1:5) + p1 = ntuple(n -> p[n], k) + p2 = ntuple(n -> p[k + n], 5 - k) + dt2 = permute(t, (p1, p2)) + ht2 = permute(adapt(Array, t), (p1, p2)) + @test ht2 ≈ adapt(Array, dt2) + end + dt3 = repartition(t, k) + ht3 = repartition(adapt(Array, t), k) + @test ht3 ≈ adapt(Array, dt3) + end + end + hasbraiding && @timedtestset "Twists: test via CPU" begin + t = adapt(JLArray, rand(ComplexF64, V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)')) + ht = adapt(Array, t) + for inds in (1, (2, 4), [1, 3, 5]), inv in (false, true) + ref = twist(ht, inds; inv) + @test adapt(Array, twist(t, inds; inv)) ≈ ref + @test adapt(Array, twist!(copy(t), inds; inv)) ≈ ref + # adjoint operands + @test adapt(Array, twist(t', inds; inv)) ≈ twist(copy(ht'), inds; inv) + t2 = copy(t) + twist!(t2', inds; inv) + @test adapt(Array, t2)' ≈ twist(copy(ht'), inds; inv) + end + @test twist(twist(t, 2), 2; inv = true) ≈ t + twistmask = (true, false, true, false, true) + if !TensorKit.has_shared_twist(t, twistmask) + p = (codomainind(t), domainind(t)) + twister = TensorKit.treetwister(storagetype(t), space(t), space(t), p, false, twistmask, false) + @test twister isa getglobal(GPUArraysExt, :DeviceUniqueTreeTransformer) + @test twister === TensorKit.treetwister(storagetype(t), space(t), space(t), p, false, twistmask, false) + end + end + symmetricbraiding && @timedtestset "Full trace: test self-consistency" begin + t = adapt(JLArray, rand(ComplexF64, V1 ⊗ V2' ⊗ V2 ⊗ V1')) + t2 = permute(t, ((1, 2), (4, 3))) + s = @constinferred tr(t2) + @test conj(s) ≈ tr(t2') + #=if !isdual(V1) + t2 = twist!(t2, 1) + end + if isdual(V2) + t2 = twist!(t2, 2) + end + ss = tr(t2) + @tensor s2 = t[a, b, b, a] + @tensor t3[a, b] := t[a, c, c, b] + @tensor s3 = t3[a, a] + @test ss ≈ s2 + @test ss ≈ s3=# + end + symmetricbraiding && @timedtestset "Partial trace: test self-consistency" begin + t = adapt(JLArray, rand(ComplexF64, V1 ⊗ V2' ⊗ V3 ⊗ V2 ⊗ V1' ⊗ V3')) + @tensor t2[a, b] := t[c, d, b, d, c, a] + @tensor t4[a, b, c, d] := t[d, e, b, e, c, a] + @tensor t5[a, b] := t4[a, b, c, c] + @test t2 ≈ t5 + end + symmetricbraiding && @timedtestset "Trace: test via conversion" begin + t = adapt(JLArray, rand(ComplexF64, V1 ⊗ V2' ⊗ V3 ⊗ V2 ⊗ V1' ⊗ V3')) + @tensor t2[a, b] := t[c, d, b, d, c, a] + @tensor t3[a, b] := ad(t)[c, d, b, d, c, a] + @test t3 ≈ ad(t2) + end + symmetricbraiding && @timedtestset "Trace and contraction" begin + t1 = adapt(JLArray, rand(ComplexF64, V1 ⊗ V2 ⊗ V3)) + t2 = adapt(JLArray, rand(ComplexF64, V2' ⊗ V4 ⊗ V1')) + t3 = t1 ⊗ t2 + @tensor ta[a, b] := t1[x, y, a] * t2[y, b, x] + @tensor tb[a, b] := t3[x, y, a, y, b, x] + @test ta ≈ tb + end + if BraidingStyle(I) isa Bosonic && hasfusiontensor(I) + @timedtestset "Tensor contraction: test via CPU" begin + dA1 = adapt(JLArray, randn(ComplexF64, V1' * V2', V3')) + dA2 = adapt(JLArray, randn(ComplexF64, V3 * V4, V5)) + drhoL = adapt(JLArray, randn(ComplexF64, V1, V1)) + drhoR = adapt(JLArray, randn(ComplexF64, V5, V5)') # test adjoint tensor + dH = adapt(JLArray, randn(ComplexF64, V2 * V4, V2 * V4)) + @tensor dHrA12[a, s1, s2, c] := drhoL[a, a'] * conj(dA1[a', t1, b]) * + dA2[b, t2, c'] * drhoR[c', c] * + dH[s1, s2, t1, t2] + @tensor hHrA12[a, s1, s2, c] := adapt(Array, drhoL)[a, a'] * conj(adapt(Array, dA1)[a', t1, b]) * + adapt(Array, dA2)[b, t2, c'] * adapt(Array, drhoR)[c', c] * + adapt(Array, dH)[s1, s2, t1, t2] + @test adapt(Array, dHrA12) ≈ hHrA12 + end + end + hasbraiding && @timedtestset "Index flipping: test flipping inverse" begin + t = adapt(JLArray, rand(ComplexF64, V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)')) + for i in 1:5 + @test t ≈ flip(flip(t, i), i; inv = true) + @test t ≈ flip(flip(t, i; inv = true), i) + end + end + symmetricbraiding && @timedtestset "Index flipping: test via explicit flip" begin + t = adapt(JLArray, rand(ComplexF64, V1 ⊗ V1' ← V1' ⊗ V1)) + F1 = adapt(JLArray{ComplexF64}, unitary(flip(V1), V1)) + + @tensor tf[a, b; c, d] := F1[a, a'] * t[a', b; c, d] + @test flip(t, 1) ≈ tf + @tensor tf[a, b; c, d] := conj(F1[b, b']) * t[a, b'; c, d] + @test twist!(flip(t, 2), 2) ≈ tf + @tensor tf[a, b; c, d] := F1[c, c'] * t[a, b; c', d] + @test flip(t, 3) ≈ tf + @tensor tf[a, b; c, d] := conj(F1[d, d']) * t[a, b; c, d'] + @test twist!(flip(t, 4), 4) ≈ tf + end + symmetricbraiding && @timedtestset "Index flipping: test via contraction" begin + t1 = adapt(JLArray, rand(ComplexF64, V1 ⊗ V2 ⊗ V3 ← V4)) + t2 = adapt(JLArray, rand(ComplexF64, V2' ⊗ V5 ← V4' ⊗ V1)) + @tensor ta[a, b] := t1[x, y, a, z] * t2[y, b, z, x] + @tensor tb[a, b] := flip(t1, 1)[x, y, a, z] * flip(t2, 4)[y, b, z, x] + @test ta ≈ tb + @tensor tb[a, b] := flip(t1, (2, 4))[x, y, a, z] * flip(t2, (1, 3))[y, b, z, x] + @test ta ≈ tb + @tensor tb[a, b] := flip(t1, (1, 2, 4))[x, y, a, z] * flip(t2, (1, 3, 4))[y, b, z, x] + @tensor tb[a, b] := flip(t1, (1, 3))[x, y, a, z] * flip(t2, (2, 4))[y, b, z, x] + @test flip(ta, (1, 2)) ≈ tb + end + @timedtestset "Multiplication of isometries: test properties" begin + W1 = V1 ⊗ V2 ⊗ V3 + W2 = (V4 ⊗ V5)' + for T in (Float64, ComplexF64) + t1 = randisometry(JLMatrix{T}, W1, W2) + t2 = randisometry(JLMatrix{T}, W2 ← W2) + @test isisometric(t1) + @test isunitary(t2) + P = t1 * t1' + @test P * P ≈ P + end + end + @timedtestset "Multiplication and inverse: test compatibility" begin + W1 = V1 ⊗ V2 ⊗ V3 + W2 = (V4 ⊗ V5)' + for T in (Float64, ComplexF64) + t1 = adapt(JLArray, rand(T, W1, W1)) + t2 = adapt(JLArray, rand(T, W2, W2)) + t = adapt(JLArray, rand(T, W1, W2)) + #=@test t1 * (t1 \ t) ≈ t + @test (t / t2) * t2 ≈ t + @test t1 \ one(t1) ≈ inv(t1) + @test one(t1) / t1 ≈ pinv(t1)=# + @test_throws SpaceMismatch inv(t) + @test_throws SpaceMismatch t2 \ t + @test_throws SpaceMismatch t / t1 + #=tp = pinv(t) * t + @test tp ≈ tp * tp=# + end + end + @timedtestset "Multiplication and inverse: test via CPU" begin + W1 = V1 ⊗ V2 ⊗ V3 + W2 = (V4 ⊗ V5)' + for T in (Float32, Float64, ComplexF32, ComplexF64) + t1 = adapt(JLArray, rand(T, W1, W1)) + t2 = adapt(JLArray, rand(T, W2, W2)) + t = adapt(JLArray, rand(T, W1, W2)) + ht1 = adapt(Array, t1) + ht2 = adapt(Array, t2) + ht = adapt(Array, t) + @test adapt(Array, t1 * t) ≈ ht1 * ht + @test adapt(Array, t1' * t) ≈ ht1' * ht + @test adapt(Array, t2 * t') ≈ ht2 * ht' + @test adapt(Array, t2' * t') ≈ ht2' * ht' + + #@test adapt(Array, inv(t1)) ≈ inv(ht1) + #@test adapt(Array, pinv(t)) ≈ pinv(ht) + + if T == Float32 || T == ComplexF32 + continue + end + + #=@test adapt(Array, t1 \ t) ≈ ht1 \ ht + @test adapt(Array, t1' \ t) ≈ ht1' \ ht + @test adapt(Array, t2 \ t') ≈ ht2 \ ht' + @test adapt(Array, t2' \ t') ≈ ht2' \ ht' + + @test adapt(Array, t2 / t) ≈ ht2 / ht + @test adapt(Array, t2' / t) ≈ ht2' / ht + @test adapt(Array, t1 / t') ≈ ht1 / ht' + @test adapt(Array, t1' / t') ≈ ht1' / ht'=# + end + end + symmetricbraiding && @timedtestset "Tensor functions" begin + W = V1 ⊗ V2 + for T in (Float64, ComplexF64) + t = project_hermitian!(adapt(JLArray, randn(T, W, W))) + s = dim(W) + @test_broken (@constinferred sqrt(t))^2 ≈ t + @test_broken adapt(Array, sqrt(t)) ≈ sqrt(adapt(Array, t)) + + #expt = @constinferred exp(t) + #@test adapt(Array, expt) ≈ exp(adapt(Array, t)) + + # log doesn't work on JLArrays yet (scalar indexing) + #@test_broken exp(@constinferred log(project_hermitian!(expt))) ≈ expt + #@test_broken adapt(Array, log(project_hermitian!(expt))) ≈ log(adapt(Array, expt)) + + @test_broken (@constinferred cos(t))^2 + (@constinferred sin(t))^2 ≈ + id(storagetype(t), W) + @test_broken (@constinferred tan(t)) ≈ sin(t) / cos(t) + @test_broken (@constinferred cot(t)) ≈ cos(t) / sin(t) + @test_broken (@constinferred cosh(t))^2 - (@constinferred sinh(t))^2 ≈ + id(storagetype(t), W) + @test_broken (@constinferred tanh(t)) ≈ sinh(t) / cosh(t) + @test_broken (@constinferred coth(t)) ≈ cosh(t) / sinh(t) + + #=t1 = sin(t) + @test_broken sin(@constinferred asin(t1)) ≈ t1 + t2 = cos(t) + @test_broken cos(@constinferred acos(t2)) ≈ t2 + t3 = sinh(t) + @test_broken sinh(@constinferred asinh(t3)) ≈ t3 + t4 = cosh(t) + @test_broken cosh(@constinferred acosh(t4)) ≈ t4 + t5 = tan(t) + @test_broken tan(@constinferred atan(t5)) ≈ t5 + t6 = cot(t) + @test_broken cot(@constinferred acot(t6)) ≈ t6 + t7 = tanh(t) + @test_broken tanh(@constinferred atanh(t7)) ≈ t7 + t8 = coth(t) + @test_broken coth(@constinferred acoth(t8)) ≈ t8=# + end + end + # Sylvester not defined for JLArrays + # @timedtestset "Sylvester equation" begin + # for T in (Float32, ComplexF64) + # tA = adapt(JLArray, rand(T, V1 ⊗ V3, V1 ⊗ V3)) + # tB = adapt(JLArray, rand(T, V2 ⊗ V4, V2 ⊗ V4)) + # tA = 3 // 2 * leftorth(tA; alg=Polar())[1] + # tB = 1 // 5 * leftorth(tB; alg=Polar())[1] + # tC = adapt(JLArray, rand(T, V1 ⊗ V3, V2 ⊗ V4)) + # t = @constinferred sylvester(tA, tB, tC) + # @test codomain(t) == V1 ⊗ V3 + # @test domain(t) == V2 ⊗ V4 + # @test norm(tA * t + t * tB + tC) < + # (norm(tA) + norm(tB) + norm(tC)) * eps(real(T))^(2 / 3) + # if BraidingStyle(I) isa Bosonic && hasfusiontensor(I) + # matrix(x) = reshape(convert(Array, x), dim(codomain(x)), dim(domain(x))) + # @test matrix(t) ≈ sylvester(matrix(tA), matrix(tB), matrix(tC)) + # end + # end + # end + # + # TODO + @timedtestset "Tensor product: test via norm preservation" begin + for T in (ComplexF64,) # Float32 case broken because of cuTENSOR + t1 = adapt(JLArray, rand(T, V1, V5')) + t2 = adapt(JLArray, rand(T, V2 ⊗ V3, V4')) + t = @constinferred (t1 ⊗ t2) + @test norm(t) ≈ norm(t1) * norm(t2) + end + end + symmetricbraiding && @timedtestset "Tensor product: test via conversion" begin + for T in (Float32, ComplexF64) + t1 = adapt(JLArray, rand(T, V1, V5')) + t2 = adapt(JLArray, rand(T, V2 ⊗ V3, V4')) + d1 = dim(codomain(t1)) + d2 = dim(codomain(t2)) + d3 = dim(domain(t1)) + d4 = dim(domain(t2)) + t = @constinferred (t1 ⊗ t2) + At = ad(t) + @test ad(t) ≈ ad(t1) ⊗ ad(t2) + end + end + symmetricbraiding && @timedtestset "Tensor product: test via tensor contraction" begin + for T in (Float32, ComplexF64) + t1 = adapt(JLArray, rand(T, V1, V5')) + t2 = adapt(JLArray, rand(T, V2 ⊗ V3, V4')) + t = @constinferred (t1 ⊗ t2) + @tensor t′[1 2 3; 4 5] := t1[1; 4] * t2[2 3; 5] + @test t ≈ t′ + end + end + end + TensorKit.empty_globalcaches!() +end + +@timedtestset "Deligne tensor product: test via conversion" begin + using .TestSetup: Vtr, VRepℤ₂, VRepSU₂, VRepA4 + @testset for Vlist1 in (Vtr, VRepSU₂), Vlist2 in (VRepℤ₂, VRepA4) + V1, V2, V3, V4, V5 = Vlist1 + W1, W2, W3, W4, W5 = Vlist2 + for T in (Float32, ComplexF64) + t1 = adapt(JLArray, rand(T, V2 ⊗ V3, (V4 ⊗ V5)')) + t2 = adapt(JLArray, rand(T, W2, (W3 ⊗ W4)')) + t = @constinferred (t1 ⊠ t2) + d1 = dim(codomain(t1)) + d2 = dim(codomain(t2)) + d3 = dim(domain(t1)) + d4 = dim(domain(t2)) + At = convert(Array, adapt(Vector{T}, t)) + @test reshape(At, (d1, d2, d3, d4)) ≈ + reshape(convert(Array, adapt(Vector{T}, t1)), (d1, 1, d3, 1)) .* + reshape(convert(Array, adapt(Vector{T}, t2)), (1, d2, 1, d4)) + end + end +end + +@timedtestset "DiagonalTensorMap index manipulations with domain $V: test via CPU" for V in ( + (ℂ^4)', + Vect[Z2Irrep](0 => 2, 1 => 3), + Vect[U1Irrep](0 => 2, 1 => 2, -1 => 1), + Vect[FermionParity](0 => 2, 1 => 3), + Vect[SU2Irrep](0 => 2, 1 => 1)', + ) + function test_tensoradd(dst, d, p, conjd, α, β) + cpu = TO.tensoradd!(adapt(Array, dst), adapt(Array, d), p, conjd, α, β, TO.DefaultBackend(), TO.DefaultAllocator()) + gpu = TO.tensoradd!(copy(dst), d, p, conjd, α, β, TO.DefaultBackend(), TO.DefaultAllocator()) + @test gpu isa typeof(dst) + @test adapt(Array, gpu) ≈ cpu + return nothing + end + diagonal(T, W) = DiagonalTensorMap(JLVector{T}(rand(T, reduceddim(W))), W) + + @testset "eltype $T" for T in (Float64, ComplexF64) + d = diagonal(T, V) + dcpu = adapt(Array, d) + scalars = ((TensorKit.One(), TensorKit.Zero()), (T(2), T(3))) + + for p in (((), (1, 2)), ((1, 2), ()), ((2, 1), ()), ((), (2, 1)), ((1,), (2,)), ((2,), (1,))), conjd in (false, true) + W = TO.tensoradd_structure(dcpu, p, conjd) + dst = adapt(JLVector{T}, randn(T, W)) + for (α, β) in scalars + test_tensoradd(dst, d, p, conjd, α, β) + end + end + for p in (((1,), (2,)), ((2,), (1,))), conjd in (false, true) + W = domain(TO.tensoradd_structure(dcpu, p, conjd))[1] + for (α, β) in scalars + test_tensoradd(diagonal(T, W), d, p, conjd, α, β) + end + end + for p in (((), (1, 2)), ((1, 2), ()), ((2, 1), ()), ((2,), (1,))) + @test adapt(Array, permute(d, p)) ≈ permute(dcpu, p) + @test adapt(Array, permute(d', p)) ≈ permute(dcpu', p) + end + for k in (0, 2) + @test adapt(Array, repartition(d, k)) ≈ repartition(dcpu, k) + @test adapt(Array, repartition(d', k)) ≈ repartition(dcpu', k) + end + for levels in ((1, 2), (2, 1)) + @test adapt(Array, braid(d, ((1, 2), ()), levels)) ≈ braid(dcpu, ((1, 2), ()), levels) + end + for inds in ((1,), (2,), (1, 2)), inv in (false, true) + ref = twist(dcpu, inds; inv) + @test twist(d, inds; inv) isa typeof(d) + @test adapt(Array, twist(d, inds; inv)) ≈ ref + @test adapt(Array, twist(d', inds; inv)) ≈ twist(dcpu', inds; inv) + @test adapt(Array, twist!(copy(d), inds; inv)) ≈ ref + dst = adapt(JLVector{T}, randn(T, space(dcpu))) + @test adapt(Array, twist!(copy(dst), d, inds, T(2), T(3); inv)) ≈ 3 * adapt(Array, dst) + 2 * ref + ddst = diagonal(T, V) + @test adapt(Array, twist!(copy(ddst), d, inds, T(2), T(3); inv)) ≈ 3 * adapt(Array, ddst) + 2 * ref + end + end + + @testset "real source, complex destination" begin + d = diagonal(Float64, V) + p = ((1,), (2,)) + W = domain(TO.tensoradd_structure(adapt(Array, d), p, true))[1] + test_tensoradd(diagonal(ComplexF64, W), d, p, true, TensorKit.One(), TensorKit.Zero()) + test_tensoradd(diagonal(ComplexF64, W), d, p, true, ComplexF64(2), ComplexF64(3)) + end + + @testset "contractions" begin + d = diagonal(Float64, V) + t = adapt(JLVector{ComplexF64}, randn(ComplexF64, V ⊗ V ← V)) + s = adapt(JLVector{Float64}, randn(Float64, V ← V)) + dcpu, tcpu, scpu = adapt(Array, d), adapt(Array, t), adapt(Array, s) + + @tensor gpu[-1 -2; -3] := t[-1 -2; 1] * d[1; -3] + @tensor cpu[-1 -2; -3] := tcpu[-1 -2; 1] * dcpu[1; -3] + @test adapt(Array, gpu) ≈ cpu + + @tensor gpu[-1 -2; -3] := d[-1; 1] * t[1 -2; -3] + @tensor cpu[-1 -2; -3] := dcpu[-1; 1] * tcpu[1 -2; -3] + @test adapt(Array, gpu) ≈ cpu + + #@test (@tensor s[1; 2] * d[2; 1]) ≈ (@tensor scpu[1; 2] * dcpu[2; 1]) + end +end