Skip to content

Commit 20a3152

Browse files
Katharine Hyattkshyatt
authored andcommitted
Trim down CUDA tensors tests
1 parent eabf00a commit 20a3152

2 files changed

Lines changed: 14 additions & 371 deletions

File tree

‎test/cuda/tensors.jl‎

Lines changed: 0 additions & 368 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,6 @@ for V in spacelist
116116
@test storagetype(t_cpu) <: Array{T}
117117
end
118118
end
119-
@timedtestset "Adapt SectorVector" begin
120-
for T in (Float32, ComplexF64)
121-
v = TensorKit.SectorVector{T}(undef, V1)
122-
rand!(parent(v))
123-
v_gpu = @constinferred adapt(CuArray, v)
124-
@test v_gpu isa TensorKit.SectorVector{T, I, <:CuArray{T}}
125-
@test v_gpu.structure == v.structure
126-
v_cpu = @constinferred adapt(Array, v_gpu)
127-
@test v_cpu isa TensorKit.SectorVector{T, I, Vector{T}}
128-
@test parent(v_cpu) == parent(v)
129-
t = CUDA.randn(T, V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)')
130-
s = svd_vals(t)
131-
s_cpu = adapt(Array, s)
132-
@test parent(s_cpu) == collect(parent(s))
133-
@test all(c -> block(s_cpu, c) == collect(block(s, c)), blocksectors(s))
134-
end
135-
end
136119
@timedtestset "Tensor Dict conversion" begin
137120
W = V1 ⊗ V2 ← (V3 ⊗ V4 ⊗ V5)'
138121
for T in (Int, Float32, ComplexF64)
@@ -141,225 +124,6 @@ for V in spacelist
141124
@test convert(Dict, adapt(Array, t)) == d
142125
end
143126
end
144-
symmetricbraiding && @timedtestset "Basic linear algebra" begin
145-
W = V1 ⊗ V2 ← (V3 ⊗ V4 ⊗ V5)'
146-
for T in (Float32, ComplexF64)
147-
t = @constinferred cuRAND.rand(T, W)
148-
@test scalartype(t) == T
149-
@test space(t) == W
150-
@test space(t') == W'
151-
@test dim(t) == dim(space(t))
152-
@test codomain(t) == codomain(W)
153-
@test domain(t) == domain(W)
154-
# blocks for adjoint
155-
bs = @constinferred blocks(t')
156-
(c, b1), state = @constinferred Nothing iterate(bs)
157-
@test c == first(blocksectors(W'))
158-
next = @constinferred Nothing iterate(bs, state)
159-
b2 = @constinferred block(t', first(blocksectors(t')))
160-
@test b1 == b2
161-
@test eltype(bs) === Pair{typeof(c), typeof(b1)}
162-
@test typeof(b1) === TensorKit.blocktype(t')
163-
@test typeof(c) === sectortype(t)
164-
# linear algebra
165-
@test isa(@constinferred(norm(t)), real(T))
166-
@test norm(t)^2 ≈ dot(t, t)
167-
α = rand(T)
168-
@test norm(α * t) ≈ abs(α) * norm(t)
169-
@test norm(t + t, 2) ≈ 2 * norm(t, 2)
170-
@test norm(t + t, 1) ≈ 2 * norm(t, 1)
171-
@test norm(t + t, Inf) ≈ 2 * norm(t, Inf)
172-
p = 3 * rand(Float64)
173-
@test norm(t + t, p) ≈ 2 * norm(t, p)
174-
@test norm(t) ≈ norm(t')
175-
for p in (1, 2, 3 * rand(Float64), Inf)
176-
tn = @constinferred normalize(t, p)
177-
@test storagetype(tn) == storagetype(t)
178-
@test norm(tn, p) ≈ 1
179-
@test tn ≈ t / norm(t, p)
180-
@test normalize!(copy(t), p) ≈ tn
181-
@test normalize(t', p) ≈ tn'
182-
ta = copy(t)'
183-
@test normalize!(ta, p) === ta
184-
@test ta ≈ tn'
185-
end
186-
187-
t2 = @constinferred rand!(similar(t))
188-
β = rand(T)
189-
#@test @constinferred(dot(β * t2, α * t)) ≈ conj(β) * α * conj(dot(t, t2)) # broken for Irrep[CU₁]
190-
@test dot(β * t2, α * t) ≈ conj(β) * α * conj(dot(t, t2))
191-
@test dot(t2, t) ≈ conj(dot(t, t2))
192-
@test dot(t2, t) ≈ conj(dot(t2', t'))
193-
@test dot(t2, t) ≈ dot(t', t2')
194-
195-
i1 = @constinferred(isomorphism(CuVector{T, CUDA.DeviceMemory}, V1 ⊗ V2, V2 ⊗ V1))
196-
i2 = @constinferred(isomorphism(CuVector{T, CUDA.DeviceMemory}, V2 ⊗ V1, V1 ⊗ V2))
197-
@test i1 * i2 == @constinferred(id(CuVector{T, CUDA.DeviceMemory}, V1 ⊗ V2))
198-
@test i2 * i1 == @constinferred(id(CuVector{T, CUDA.DeviceMemory}, V2 ⊗ V1))
199-
w = @constinferred(isometry(CuVector{T, CUDA.DeviceMemory}, V1 ⊗ (oneunit(V1) ⊕ oneunit(V1)), V1))
200-
@test dim(w) == 2 * dim(V1 ← V1)
201-
@test w' * w == id(CuVector{T, CUDA.DeviceMemory}, V1)
202-
@test w * w' == (w * w')^2
203-
end
204-
end
205-
@timedtestset "Trivial space insertion and removal" begin
206-
W = V1 ⊗ V2 ← (V3 ⊗ V4 ⊗ V5)'
207-
for T in (Float32, ComplexF64)
208-
t = @constinferred cuRAND.rand(T, W)
209-
t2 = @constinferred insertleftunit(t)
210-
@test t2 == @constinferred insertrightunit(t)
211-
@test numind(t2) == numind(t) + 1
212-
@test space(t2) == insertleftunit(space(t))
213-
@test scalartype(t2) === T
214-
@test t.data === t2.data
215-
@test @constinferred(removeunit(t2, $(numind(t2)))) == t
216-
t3 = @constinferred insertleftunit(t; copy = true)
217-
@test t3 == @constinferred insertrightunit(t; copy = true)
218-
@test t.data !== t3.data
219-
for (c, b) in blocks(t)
220-
@test b == block(t3, c)
221-
end
222-
@test @constinferred(removeunit(t3, $(numind(t3)))) == t
223-
t4 = @constinferred insertrightunit(t, 3; dual = true)
224-
@test_broken numin(t4) == numin(t) && numout(t4) == numout(t) + 1
225-
for (c, b) in blocks(t)
226-
@test b == block(t4, c)
227-
end
228-
@test @constinferred(removeunit(t4, 4)) == t
229-
t5 = @constinferred insertleftunit(t, 4; dual = true)
230-
@test numin(t5) == numin(t) + 1 && numout(t5) == numout(t)
231-
for (c, b) in blocks(t)
232-
@test b == block(t5, c)
233-
end
234-
@test @constinferred(removeunit(t5, 4)) == t
235-
end
236-
end
237-
if hasfusiontensor(I)
238-
@timedtestset "Basic linear algebra: test via CPU" begin
239-
W = V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)'
240-
for T in (Float32, ComplexF64)
241-
t = cuRAND.rand(T, W)
242-
t2 = @constinferred cuRAND.rand!(similar(t))
243-
α = rand(T)
244-
@test norm(t, 2) ≈ norm(adapt(Array, t), 2)
245-
@test dot(t2, t) ≈ dot(adapt(Array, t2), adapt(Array, t))
246-
@test adapt(Vector{T}, (α * t)) ≈ α * adapt(Vector{T}, t)
247-
@test adapt(Vector{T}, (t + t)) ≈ 2 * adapt(Vector{T}, t)
248-
end
249-
end
250-
@timedtestset "Real and imaginary parts" begin
251-
W = V1 ⊗ V2
252-
for T in (Float64, ComplexF64, ComplexF32)
253-
t = @constinferred cuRAND.randn(T, W, W)
254-
255-
tr = @constinferred real(t)
256-
@test scalartype(tr) <: Real
257-
@test real(adapt(Array, t)) == adapt(Array, tr)
258-
@test storagetype(tr) == CuVector{real(T), CUDA.DeviceMemory}
259-
260-
ti = @constinferred imag(t)
261-
@test scalartype(ti) <: Real
262-
@test imag(adapt(Array, t)) == adapt(Array, ti)
263-
@test storagetype(ti) == CuVector{real(T), CUDA.DeviceMemory}
264-
265-
tc = @inferred complex(t)
266-
@test scalartype(tc) <: Complex
267-
@test complex(adapt(Array, t)) == adapt(Array, tc)
268-
@test storagetype(tc) == CuVector{complex(T), CUDA.DeviceMemory}
269-
270-
tc2 = @inferred complex(tr, ti)
271-
@test tc2 ≈ tc
272-
@test storagetype(tc2) == CuVector{complex(T), CUDA.DeviceMemory}
273-
end
274-
end
275-
end
276-
@timedtestset "Tensor conversion" begin
277-
W = V1 ⊗ V2
278-
t = @constinferred cuRAND.randn(W ← W)
279-
@test typeof(convert(typeof(t), t')) == typeof(t)
280-
@test typeof(adapt(Array, t')) == typeof(adapt(Array, t)')
281-
tc = complex(t)
282-
@test convert(typeof(tc), t) == tc
283-
@test typeof(convert(typeof(tc), t)) == typeof(tc)
284-
@test typeof(convert(typeof(tc), t')) == typeof(tc)
285-
@test Base.promote_typeof(t, tc) == typeof(tc)
286-
@test Base.promote_typeof(tc, t) == typeof(tc + t)
287-
end
288-
#=@timedtestset "diag/diagm" begin
289-
W = V1 ⊗ V2 ⊗ V3 ← V4 ⊗ V5
290-
t = cuRAND.randn(ComplexF64, W)
291-
d = LinearAlgebra.diag(t)
292-
# TODO find a way to use CUDA here
293-
D = LinearAlgebra.diagm(codomain(t), domain(t), d)
294-
@test LinearAlgebra.isdiag(D)
295-
@test LinearAlgebra.diag(D) == d
296-
end=#
297-
symmetricbraiding && @timedtestset "Permutations: test via inner product invariance" begin
298-
W = V1 ⊗ V2 ⊗ V3 ⊗ V4 ⊗ V5
299-
t = cuRAND.rand(ComplexF64, W)
300-
ht = adapt(Vector{ComplexF64}, t)
301-
t′ = cuRAND.randn!(similar(t))
302-
ht′ = adapt(Vector{ComplexF64}, t′)
303-
dot_htt′ = dot(ht′, ht)
304-
dot_tt′ = dot(t′, t)
305-
@test dot_tt′ ≈ dot_htt′
306-
norm_t = norm(t)
307-
for k in 0:5
308-
for p in permutations(1:5)
309-
p1 = ntuple(n -> p[n], k)
310-
p2 = ntuple(n -> p[k + n], 5 - k)
311-
t2 = @constinferred permute(t, (p1, p2))
312-
t2′ = permute(t′, (p1, p2))
313-
@test norm(t2) ≈ norm_t
314-
@test dot(t2′, t2) ≈ dot_tt′
315-
@test dot(transpose(t2′), transpose(t2)) ≈ dot_tt′
316-
end
317-
t3 = @constinferred repartition(t, $k)
318-
t3 = repartition(t, k)
319-
@test norm(t3) ≈ norm(t)
320-
t3′ = @constinferred repartition!(similar(t3), t′)
321-
@test norm(t3′) ≈ norm(t′)
322-
@test dot(t′, t) ≈ dot(t3′, t3)
323-
end
324-
end
325-
symmetricbraiding && @timedtestset "Permutations: test via CPU" begin
326-
W = V1 ⊗ V2 ⊗ V3 ⊗ V4 ⊗ V5
327-
t = cuRAND.rand(ComplexF64, W)
328-
for k in 0:5
329-
for p in permutations(1:5)
330-
p1 = ntuple(n -> p[n], k)
331-
p2 = ntuple(n -> p[k + n], 5 - k)
332-
dt2 = permute(t, (p1, p2))
333-
ht2 = permute(adapt(Array, t), (p1, p2))
334-
@test ht2 ≈ adapt(Array, dt2)
335-
end
336-
dt3 = repartition(t, k)
337-
ht3 = repartition(adapt(Array, t), k)
338-
@test ht3 ≈ adapt(Array, dt3)
339-
end
340-
end
341-
hasbraiding && @timedtestset "Twists: test via CPU" begin
342-
t = cuRAND.rand(ComplexF64, V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)')
343-
ht = adapt(Array, t)
344-
for inds in (1, (2, 4), [1, 3, 5]), inv in (false, true)
345-
ref = twist(ht, inds; inv)
346-
@test adapt(Array, twist(t, inds; inv)) ≈ ref
347-
@test adapt(Array, twist!(copy(t), inds; inv)) ≈ ref
348-
# adjoint operands
349-
@test adapt(Array, twist(t', inds; inv)) ≈ twist(copy(ht'), inds; inv)
350-
t2 = copy(t)
351-
twist!(t2', inds; inv)
352-
@test adapt(Array, t2)' ≈ twist(copy(ht'), inds; inv)
353-
end
354-
@test twist(twist(t, 2), 2; inv = true) ≈ t
355-
twistmask = (true, false, true, false, true)
356-
if !TensorKit.has_shared_twist(t, twistmask)
357-
p = (codomainind(t), domainind(t))
358-
twister = TensorKit.treetwister(storagetype(t), space(t), space(t), p, false, twistmask, false)
359-
@test twister isa getglobal(GPUArraysExt, :DeviceUniqueTreeTransformer)
360-
@test twister === TensorKit.treetwister(storagetype(t), space(t), space(t), p, false, twistmask, false)
361-
end
362-
end
363127
symmetricbraiding && @timedtestset "Full trace: test self-consistency" begin
364128
t = cuRAND.rand(ComplexF64, V1 ⊗ V2' ⊗ V2 ⊗ V1')
365129
t2 = permute(t, ((1, 2), (4, 3)))
@@ -378,87 +142,6 @@ for V in spacelist
378142
@test ss ≈ s2
379143
@test ss ≈ s3
380144
end
381-
symmetricbraiding && @timedtestset "Partial trace: test self-consistency" begin
382-
t = cuRAND.rand(ComplexF64, V1 ⊗ V2' ⊗ V3 ⊗ V2 ⊗ V1' ⊗ V3')
383-
@tensor t2[a, b] := t[c, d, b, d, c, a]
384-
@tensor t4[a, b, c, d] := t[d, e, b, e, c, a]
385-
@tensor t5[a, b] := t4[a, b, c, c]
386-
@test t2 ≈ t5
387-
end
388-
symmetricbraiding && @timedtestset "Trace: test via conversion" begin
389-
t = cuRAND.rand(ComplexF64, V1 ⊗ V2' ⊗ V3 ⊗ V2 ⊗ V1' ⊗ V3')
390-
@tensor t2[a, b] := t[c, d, b, d, c, a]
391-
@tensor t3[a, b] := ad(t)[c, d, b, d, c, a]
392-
@test t3 ≈ ad(t2)
393-
end
394-
symmetricbraiding && @timedtestset "Trace and contraction" begin
395-
t1 = cuRAND.rand(ComplexF64, V1 ⊗ V2 ⊗ V3)
396-
t2 = cuRAND.rand(ComplexF64, V2' ⊗ V4 ⊗ V1')
397-
t3 = t1 ⊗ t2
398-
@tensor ta[a, b] := t1[x, y, a] * t2[y, b, x]
399-
@tensor tb[a, b] := t3[x, y, a, y, b, x]
400-
@test ta ≈ tb
401-
end
402-
if BraidingStyle(I) isa Bosonic && hasfusiontensor(I)
403-
@timedtestset "Tensor contraction: test via CPU" begin
404-
dA1 = cuRAND.randn(ComplexF64, V1' * V2', V3')
405-
dA2 = cuRAND.randn(ComplexF64, V3 * V4, V5)
406-
drhoL = cuRAND.randn(ComplexF64, V1, V1)
407-
drhoR = cuRAND.randn(ComplexF64, V5, V5)' # test adjoint tensor
408-
dH = cuRAND.randn(ComplexF64, V2 * V4, V2 * V4)
409-
@tensor dHrA12[a, s1, s2, c] := drhoL[a, a'] * conj(dA1[a', t1, b]) *
410-
dA2[b, t2, c'] * drhoR[c', c] *
411-
dH[s1, s2, t1, t2]
412-
@tensor hHrA12[a, s1, s2, c] := adapt(Array, drhoL)[a, a'] * conj(adapt(Array, dA1)[a', t1, b]) *
413-
adapt(Array, dA2)[b, t2, c'] * adapt(Array, drhoR)[c', c] *
414-
adapt(Array, dH)[s1, s2, t1, t2]
415-
@test adapt(Array, dHrA12) ≈ hHrA12
416-
end
417-
end
418-
hasbraiding && @timedtestset "Index flipping: test flipping inverse" begin
419-
t = cuRAND.rand(ComplexF64, V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)')
420-
for i in 1:5
421-
@test t ≈ flip(flip(t, i), i; inv = true)
422-
@test t ≈ flip(flip(t, i; inv = true), i)
423-
end
424-
end
425-
symmetricbraiding && @timedtestset "Index flipping: test via explicit flip" begin
426-
t = cuRAND.rand(ComplexF64, V1 ⊗ V1' ← V1' ⊗ V1)
427-
F1 = adapt(CuArray{ComplexF64}, unitary(flip(V1), V1))
428-
429-
@tensor tf[a, b; c, d] := F1[a, a'] * t[a', b; c, d]
430-
@test flip(t, 1) ≈ tf
431-
@tensor tf[a, b; c, d] := conj(F1[b, b']) * t[a, b'; c, d]
432-
@test twist!(flip(t, 2), 2) ≈ tf
433-
@tensor tf[a, b; c, d] := F1[c, c'] * t[a, b; c', d]
434-
@test flip(t, 3) ≈ tf
435-
@tensor tf[a, b; c, d] := conj(F1[d, d']) * t[a, b; c, d']
436-
@test twist!(flip(t, 4), 4) ≈ tf
437-
end
438-
symmetricbraiding && @timedtestset "Index flipping: test via contraction" begin
439-
t1 = cuRAND.rand(ComplexF64, V1 ⊗ V2 ⊗ V3 ← V4)
440-
t2 = cuRAND.rand(ComplexF64, V2' ⊗ V5 ← V4' ⊗ V1)
441-
@tensor ta[a, b] := t1[x, y, a, z] * t2[y, b, z, x]
442-
@tensor tb[a, b] := flip(t1, 1)[x, y, a, z] * flip(t2, 4)[y, b, z, x]
443-
@test ta ≈ tb
444-
@tensor tb[a, b] := flip(t1, (2, 4))[x, y, a, z] * flip(t2, (1, 3))[y, b, z, x]
445-
@test ta ≈ tb
446-
@tensor tb[a, b] := flip(t1, (1, 2, 4))[x, y, a, z] * flip(t2, (1, 3, 4))[y, b, z, x]
447-
@tensor tb[a, b] := flip(t1, (1, 3))[x, y, a, z] * flip(t2, (2, 4))[y, b, z, x]
448-
@test flip(ta, (1, 2)) ≈ tb
449-
end
450-
@timedtestset "Multiplication of isometries: test properties" begin
451-
W1 = V1 ⊗ V2 ⊗ V3
452-
W2 = (V4 ⊗ V5)'
453-
for T in (Float64, ComplexF64)
454-
t1 = randisometry(CuMatrix{T}, W1, W2)
455-
t2 = randisometry(CuMatrix{T}, W2 ← W2)
456-
@test isisometric(t1)
457-
@test isunitary(t2)
458-
P = t1 * t1'
459-
@test P * P ≈ P
460-
end
461-
end
462145
@timedtestset "Multiplication and inverse: test compatibility" begin
463146
W1 = V1 ⊗ V2 ⊗ V3
464147
W2 = (V4 ⊗ V5)'
@@ -574,61 +257,10 @@ for V in spacelist
574257
# end
575258
#
576259
# TODO
577-
@timedtestset "Tensor product: test via norm preservation" begin
578-
for T in (ComplexF64,) # Float32 case broken because of cuTENSOR
579-
t1 = cuRAND.rand(T, V1, V5')
580-
t2 = cuRAND.rand(T, V2 ⊗ V3, V4')
581-
t = @constinferred (t1 ⊗ t2)
582-
@test norm(t) ≈ norm(t1) * norm(t2)
583-
end
584-
end
585-
symmetricbraiding && @timedtestset "Tensor product: test via conversion" begin
586-
for T in (Float32, ComplexF64)
587-
t1 = cuRAND.rand(T, V1, V5')
588-
t2 = cuRAND.rand(T, V2 ⊗ V3, V4')
589-
d1 = dim(codomain(t1))
590-
d2 = dim(codomain(t2))
591-
d3 = dim(domain(t1))
592-
d4 = dim(domain(t2))
593-
t = @constinferred (t1 ⊗ t2)
594-
At = ad(t)
595-
@test ad(t) ≈ ad(t1) ⊗ ad(t2)
596-
end
597-
end
598-
symmetricbraiding && @timedtestset "Tensor product: test via tensor contraction" begin
599-
for T in (Float32, ComplexF64)
600-
t1 = cuRAND.rand(T, V1, V5')
601-
t2 = cuRAND.rand(T, V2 ⊗ V3, V4')
602-
t = @constinferred (t1 ⊗ t2)
603-
@tensor t′[1 2 3; 4 5] := t1[1; 4] * t2[2 3; 5]
604-
@test t ≈ t′
605-
end
606-
end
607260
end
608261
TensorKit.empty_globalcaches!()
609262
end
610263

611-
@timedtestset "Deligne tensor product: test via conversion" begin
612-
using .TestSetup: Vtr, VRepℤ₂, VRepSU₂, VRepA4
613-
@testset for Vlist1 in (Vtr, VRepSU₂), Vlist2 in (VRepℤ₂, VRepA4)
614-
V1, V2, V3, V4, V5 = Vlist1
615-
W1, W2, W3, W4, W5 = Vlist2
616-
for T in (Float32, ComplexF64)
617-
t1 = CUDA.rand(T, V2 ⊗ V3, (V4 ⊗ V5)')
618-
t2 = CUDA.rand(T, W2, (W3 ⊗ W4)')
619-
t = @constinferred (t1 ⊠ t2)
620-
d1 = dim(codomain(t1))
621-
d2 = dim(codomain(t2))
622-
d3 = dim(domain(t1))
623-
d4 = dim(domain(t2))
624-
At = convert(Array, adapt(Vector{T}, t))
625-
@test reshape(At, (d1, d2, d3, d4)) ≈
626-
reshape(convert(Array, adapt(Vector{T}, t1)), (d1, 1, d3, 1)) .*
627-
reshape(convert(Array, adapt(Vector{T}, t2)), (1, d2, 1, d4))
628-
end
629-
end
630-
end
631-
632264
@timedtestset "DiagonalTensorMap index manipulations with domain $V: test via CPU" for V in (
633265
(ℂ^4)',
634266
Vect[Z2Irrep](0 => 2, 1 => 3),

0 commit comments

Comments
 (0)