Skip to content

Commit 36bdb3c

Browse files
Katharine Hyattkshyatt
authored andcommitted
Clean up duplicated stuff in AMD
1 parent 20a3152 commit 36bdb3c

1 file changed

Lines changed: 17 additions & 320 deletions

File tree

‎test/amd/tensors.jl‎

Lines changed: 17 additions & 320 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,18 @@ for V in spacelist
9797
@test domain(t2) == one(W)
9898
end
9999
end
100-
@timedtestset "Adapt SectorVector" begin
101-
for T in (Float32, ComplexF64)
102-
v = TensorKit.SectorVector{T}(undef, V1)
103-
rand!(parent(v))
104-
v_gpu = @constinferred adapt(ROCArray, v)
105-
@test v_gpu isa TensorKit.SectorVector{T, I, <:ROCArray{T}}
106-
@test v_gpu.structure == v.structure
107-
v_cpu = @constinferred adapt(Array, v_gpu)
108-
@test v_cpu isa TensorKit.SectorVector{T, I, Vector{T}}
109-
@test parent(v_cpu) == parent(v)
110-
t = AMDGPU.randn(T, V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)')
111-
s = svd_vals(t)
112-
s_cpu = adapt(Array, s)
113-
@test parent(s_cpu) == collect(parent(s))
114-
@test all(c -> block(s_cpu, c) == collect(block(s, c)), blocksectors(s))
100+
@timedtestset "Adapt" begin
101+
W = V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)'
102+
for T in (Int, Float32, ComplexF64)
103+
t = rand(T, W)
104+
t_gpu = @constinferred adapt(ROCArray, t)
105+
@test storagetype(t_gpu) <: ROCArray{T}
106+
@test scalartype(t_gpu) === scalartype(t)
107+
@test collect(t_gpu.data) == t.data
108+
109+
t_cpu = @constinferred adapt(Array, t_gpu)
110+
@test t_cpu == t
111+
@test storagetype(t_cpu) <: Array{T}
115112
end
116113
end
117114
@timedtestset "Tensor Dict conversion" begin
@@ -122,175 +119,6 @@ for V in spacelist
122119
@test convert(Dict, adapt(Array, t)) == d
123120
end
124121
end
125-
symmetricbraiding && @timedtestset "Basic linear algebra" begin
126-
W = V1 ⊗ V2 ⊗ V3 ← V4 ⊗ V5
127-
for T in (Float32, ComplexF64)
128-
t = @constinferred AMDGPU.rand(T, W)
129-
@test scalartype(t) == T
130-
@test space(t) == W
131-
@test space(t') == W'
132-
@test dim(t) == dim(space(t))
133-
@test codomain(t) == codomain(W)
134-
@test domain(t) == domain(W)
135-
# blocks for adjoint
136-
bs = @constinferred blocks(t')
137-
(c, b1), state = @constinferred Nothing iterate(bs)
138-
@test c == first(blocksectors(W'))
139-
next = @constinferred Nothing iterate(bs, state)
140-
b2 = @constinferred block(t', first(blocksectors(t')))
141-
@test b1 == b2
142-
@test eltype(bs) === Pair{typeof(c), typeof(b1)}
143-
@test typeof(b1) === TensorKit.blocktype(t')
144-
@test typeof(c) === sectortype(t)
145-
# linear algebra
146-
@test isa(@constinferred(norm(t)), real(T))
147-
@test norm(t)^2 ≈ dot(t, t)
148-
α = rand(T)
149-
@test norm(α * t) ≈ abs(α) * norm(t)
150-
@test norm(t + t, 2) ≈ 2 * norm(t, 2)
151-
@test norm(t + t, 1) ≈ 2 * norm(t, 1)
152-
@test norm(t + t, Inf) ≈ 2 * norm(t, Inf)
153-
p = 3 * rand(Float64)
154-
@test norm(t + t, p) ≈ 2 * norm(t, p)
155-
@test norm(t) ≈ norm(t')
156-
for p in (1, 2, 3 * rand(Float64), Inf)
157-
tn = @constinferred normalize(t, p)
158-
@test storagetype(tn) == storagetype(t)
159-
@test norm(tn, p) ≈ 1
160-
@test tn ≈ t / norm(t, p)
161-
@test normalize!(copy(t), p) ≈ tn
162-
@test normalize(t', p) ≈ tn'
163-
ta = copy(t)'
164-
@test normalize!(ta, p) === ta
165-
@test ta ≈ tn'
166-
end
167-
168-
t2 = @constinferred rand!(similar(t))
169-
β = rand(T)
170-
#@test @constinferred(dot(β * t2, α * t)) ≈ conj(β) * α * conj(dot(t, t2)) # broken for Irrep[CU₁]
171-
@test dot(β * t2, α * t) ≈ conj(β) * α * conj(dot(t, t2))
172-
@test dot(t2, t) ≈ conj(dot(t, t2))
173-
@test dot(t2, t) ≈ conj(dot(t2', t'))
174-
@test dot(t2, t) ≈ dot(t', t2')
175-
176-
i1 = @constinferred(isomorphism(ROCVector{T, AMDGPU.Mem.HIPBuffer}, V1 ⊗ V2, V2 ⊗ V1))
177-
i2 = @constinferred(isomorphism(ROCVector{T, AMDGPU.Mem.HIPBuffer}, V2 ⊗ V1, V1 ⊗ V2))
178-
@test i1 * i2 == @constinferred(id(ROCVector{T, AMDGPU.Mem.HIPBuffer}, V1 ⊗ V2))
179-
@test i2 * i1 == @constinferred(id(ROCVector{T, AMDGPU.Mem.HIPBuffer}, V2 ⊗ V1))
180-
w = @constinferred(isometry(ROCVector{T, AMDGPU.Mem.HIPBuffer}, V1 ⊗ (oneunit(V1) ⊕ oneunit(V1)), V1))
181-
@test dim(w) == 2 * dim(V1 ← V1)
182-
@test w' * w == id(ROCVector{T, AMDGPU.Mem.HIPBuffer}, V1)
183-
@test w * w' == (w * w')^2
184-
end
185-
end
186-
@timedtestset "Trivial space insertion and removal" begin
187-
W = V1 ⊗ V2 ← (V3 ⊗ V4 ⊗ V5)'
188-
for T in (Float32, ComplexF64)
189-
t = @constinferred AMDGPU.rand(T, W)
190-
t2 = @constinferred insertleftunit(t)
191-
@test t2 == @constinferred insertrightunit(t)
192-
@test numind(t2) == numind(t) + 1
193-
@test space(t2) == insertleftunit(space(t))
194-
@test scalartype(t2) === T
195-
@test t.data === t2.data
196-
@test @constinferred(removeunit(t2, $(numind(t2)))) == t
197-
t3 = @constinferred insertleftunit(t; copy = true)
198-
@test t3 == @constinferred insertrightunit(t; copy = true)
199-
@test t.data !== t3.data
200-
for (c, b) in blocks(t)
201-
@test b == block(t3, c)
202-
end
203-
@test @constinferred(removeunit(t3, $(numind(t3)))) == t
204-
t4 = @constinferred insertrightunit(t, 3; dual = true)
205-
@test numin(t4) == numin(t) + 1 && numout(t4) == numout(t)
206-
for (c, b) in blocks(t)
207-
@test b == block(t4, c)
208-
end
209-
@test @constinferred(removeunit(t4, 4)) == t
210-
t5 = @constinferred insertleftunit(t, 4; dual = true)
211-
@test numin(t5) == numin(t) + 1 && numout(t5) == numout(t)
212-
for (c, b) in blocks(t)
213-
@test b == block(t5, c)
214-
end
215-
@test @constinferred(removeunit(t5, 4)) == t
216-
end
217-
end
218-
if hasfusiontensor(I)
219-
@timedtestset "Basic linear algebra: test via CPU" begin
220-
W = V1 ⊗ V2 ⊗ V3 ← V4 ⊗ V5
221-
for T in (Float32, ComplexF64)
222-
t = AMDGPU.rand(T, W)
223-
t2 = @constinferred AMDGPU.rand!(similar(t))
224-
α = rand(T)
225-
@test norm(t, 2) ≈ norm(adapt(Array, t), 2)
226-
@test dot(t2, t) ≈ dot(adapt(Array, t2), adapt(Array, t))
227-
@test adapt(Array, α * t) ≈ α * adapt(Array, t)
228-
@test adapt(Array, t + t) ≈ 2 * adapt(Array, t)
229-
end
230-
end
231-
@timedtestset "Real and imaginary parts" begin
232-
W = V1 ⊗ V2
233-
for T in (Float64, ComplexF64, ComplexF32)
234-
t = @constinferred AMDGPU.randn(T, W, W)
235-
236-
tr = @constinferred real(t)
237-
@test scalartype(tr) <: Real
238-
@test real(adapt(Array, t)) == adapt(Array, tr)
239-
@test storagetype(tr) == ROCVector{real(T), AMDGPU.Mem.HIPBuffer}
240-
241-
ti = @constinferred imag(t)
242-
@test scalartype(ti) <: Real
243-
@test imag(adapt(Array, t)) == adapt(Array, ti)
244-
@test storagetype(ti) == ROCVector{real(T), AMDGPU.Mem.HIPBuffer}
245-
246-
tc = @inferred complex(t)
247-
@test scalartype(tc) <: Complex
248-
@test complex(adapt(Array, t)) == adapt(Array, tc)
249-
@test storagetype(tc) == ROCVector{complex(T), AMDGPU.Mem.HIPBuffer}
250-
251-
tc2 = @inferred complex(tr, ti)
252-
@test tc2 ≈ tc
253-
@test storagetype(tc2) == ROCVector{complex(T), AMDGPU.Mem.HIPBuffer}
254-
end
255-
end
256-
end
257-
@timedtestset "Tensor conversion" begin
258-
W = V1 ⊗ V2
259-
t = @constinferred AMDGPU.randn(W ← W)
260-
@test typeof(convert(TensorMap, t')) == typeof(t)
261-
tc = complex(t)
262-
@test convert(typeof(tc), t) == tc
263-
@test typeof(convert(typeof(tc), t)) == typeof(tc)
264-
@test typeof(convert(typeof(tc), t')) == typeof(tc)
265-
@test Base.promote_typeof(t, tc) == typeof(tc)
266-
@test Base.promote_typeof(tc, t) == typeof(tc + t)
267-
end
268-
#=@timedtestset "diag/diagm" begin
269-
W = V1 ⊗ V2 ⊗ V3 ← V4 ⊗ V5
270-
t = AMDGPU.randn(ComplexF64, W)
271-
d = LinearAlgebra.diag(t)
272-
# TODO find a way to use AMDGPU here
273-
D = LinearAlgebra.diagm(codomain(t), domain(t), d)
274-
@test LinearAlgebra.isdiag(D)
275-
@test LinearAlgebra.diag(D) == d
276-
end=#
277-
symmetricbraiding && @timedtestset "Permutations: test via CPU" begin
278-
W = V1 ⊗ V2 ⊗ V3 ⊗ V4 ⊗ V5
279-
t = AMDGPU.rand(ComplexF64, W)
280-
for k in 0:5
281-
for p in permutations(1:5)
282-
p1 = ntuple(n -> p[n], k)
283-
p2 = ntuple(n -> p[k + n], 5 - k)
284-
dt2 = permute(t, (p1, p2))
285-
ht2 = permute(adapt(Array, t), (p1, p2))
286-
@test ht2 ≈ adapt(Array, dt2)
287-
end
288-
289-
dt3 = repartition(t, k)
290-
ht3 = repartition(adapt(Array, t), k)
291-
@test ht3 ≈ adapt(Array, dt3)
292-
end
293-
end
294122
symmetricbraiding && @timedtestset "Full trace: test self-consistency" begin
295123
t = AMDGPU.rand(ComplexF64, V1 ⊗ V2' ⊗ V2 ⊗ V1')
296124
t2 = permute(t, ((1, 2), (4, 3)))
@@ -309,102 +137,19 @@ for V in spacelist
309137
@test ss ≈ s2
310138
@test ss ≈ s3
311139
end
312-
symmetricbraiding && @timedtestset "Partial trace: test self-consistency" begin
313-
t = AMDGPU.rand(ComplexF64, V1 ⊗ V2' ⊗ V3 ⊗ V2 ⊗ V1' ⊗ V3')
314-
@tensor t2[a, b] := t[c, d, b, d, c, a]
315-
@tensor t4[a, b, c, d] := t[d, e, b, e, c, a]
316-
@tensor t5[a, b] := t4[a, b, c, c]
317-
@test t2 ≈ t5
318-
end
319-
symmetricbraiding && @timedtestset "Trace: test via conversion" begin
320-
t = AMDGPU.rand(ComplexF64, V1 ⊗ V2' ⊗ V3 ⊗ V2 ⊗ V1' ⊗ V3')
321-
@tensor t2[a, b] := t[c, d, b, d, c, a]
322-
@tensor t3[a, b] := ad(t)[c, d, b, d, c, a]
323-
@test t3 ≈ ad(t2)
324-
end
325-
symmetricbraiding && @timedtestset "Trace and contraction" begin
326-
t1 = AMDGPU.rand(ComplexF64, V1 ⊗ V2 ⊗ V3)
327-
t2 = AMDGPU.rand(ComplexF64, V2' ⊗ V4 ⊗ V1')
328-
t3 = t1 ⊗ t2
329-
@tensor ta[a, b] := t1[x, y, a] * t2[y, b, x]
330-
@tensor tb[a, b] := t3[x, y, a, y, b, x]
331-
@test ta ≈ tb
332-
end
333-
if BraidingStyle(I) isa Bosonic && hasfusiontensor(I)
334-
@timedtestset "Tensor contraction: test via CPU" begin
335-
dA1 = AMDGPU.randn(ComplexF64, V1' * V2', V3')
336-
dA2 = AMDGPU.randn(ComplexF64, V3 * V4, V5)
337-
drhoL = AMDGPU.randn(ComplexF64, V1, V1)
338-
drhoR = AMDGPU.randn(ComplexF64, V5, V5)' # test adjoint tensor
339-
dH = AMDGPU.randn(ComplexF64, V2 * V4, V2 * V4)
340-
@tensor dHrA12[a, s1, s2, c] := drhoL[a, a'] * conj(dA1[a', t1, b]) *
341-
dA2[b, t2, c'] * drhoR[c', c] *
342-
dH[s1, s2, t1, t2]
343-
@tensor hHrA12[a, s1, s2, c] := adapt(Array, drhoL)[a, a'] * conj(adapt(Array, dA1)[a', t1, b]) *
344-
adapt(Array, dA2)[b, t2, c'] * adapt(Array, drhoR)[c', c] *
345-
adapt(Array, dH)[s1, s2, t1, t2]
346-
@test adapt(Array, dHrA12) ≈ hHrA12
347-
end
348-
end
349-
hasbraiding && @timedtestset "Index flipping: test flipping inverse" begin
350-
t = AMDGPU.rand(ComplexF64, V1 ⊗ V1' ← V1' ⊗ V1)
351-
for i in 1:4
352-
@test t ≈ flip(flip(t, i), i; inv = true)
353-
@test t ≈ flip(flip(t, i; inv = true), i)
354-
end
355-
end
356-
symmetricbraiding && @timedtestset "Index flipping: test via explicit flip" begin
357-
t = AMDGPU.rand(ComplexF64, V1 ⊗ V1' ← V1' ⊗ V1)
358-
F1 = adapt(ROCArray{ComplexF64}, unitary(flip(V1), V1))
359-
360-
@tensor tf[a, b; c, d] := F1[a, a'] * t[a', b; c, d]
361-
@test flip(t, 1) ≈ tf
362-
@tensor tf[a, b; c, d] := conj(F1[b, b']) * t[a, b'; c, d]
363-
@test twist!(flip(t, 2), 2) ≈ tf
364-
@tensor tf[a, b; c, d] := F1[c, c'] * t[a, b; c', d]
365-
@test flip(t, 3) ≈ tf
366-
@tensor tf[a, b; c, d] := conj(F1[d, d']) * t[a, b; c, d']
367-
@test twist!(flip(t, 4), 4) ≈ tf
368-
end
369-
symmetricbraiding && @timedtestset "Index flipping: test via contraction" begin
370-
t1 = AMDGPU.rand(ComplexF64, V1 ⊗ V2 ⊗ V3 ← V4)
371-
t2 = AMDGPU.rand(ComplexF64, V2' ⊗ V5 ← V4' ⊗ V1)
372-
@tensor ta[a, b] := t1[x, y, a, z] * t2[y, b, z, x]
373-
@tensor tb[a, b] := flip(t1, 1)[x, y, a, z] * flip(t2, 4)[y, b, z, x]
374-
@test ta ≈ tb
375-
@tensor tb[a, b] := flip(t1, (2, 4))[x, y, a, z] * flip(t2, (1, 3))[y, b, z, x]
376-
@test ta ≈ tb
377-
@tensor tb[a, b] := flip(t1, (1, 2, 4))[x, y, a, z] * flip(t2, (1, 3, 4))[y, b, z, x]
378-
@tensor tb[a, b] := flip(t1, (1, 3))[x, y, a, z] * flip(t2, (2, 4))[y, b, z, x]
379-
@test flip(ta, (1, 2)) ≈ tb
380-
end
381-
@timedtestset "Multiplication of isometries: test properties" begin
382-
W1 = V1 ⊗ V2 ⊗ V3
383-
W2 = (V4 ⊗ V5)'
384-
for T in (Float64, ComplexF64)
385-
t1 = randisometry(ROCMatrix{T, AMDGPU.Mem.HIPBuffer}, W1, W2)
386-
t2 = randisometry(ROCMatrix{T, AMDGPU.Mem.HIPBuffer}, W2 ← W2)
387-
@test isisometric(t1)
388-
@test isunitary(t2)
389-
P = t1 * t1'
390-
@test P * P ≈ P
391-
end
392-
end
393140
@timedtestset "Multiplication and inverse: test compatibility" begin
394141
W1 = V1 ⊗ V2 ⊗ V3
395142
W2 = (V4 ⊗ V5)'
396143
for T in (Float64, ComplexF64)
397144
t1 = AMDGPU.rand(T, W1, W1)
398145
t2 = AMDGPU.rand(T, W2, W2)
399146
t = AMDGPU.rand(T, W1, W2)
400-
#=@test t1 * (t1 \ t) ≈ t
147+
@test t1 * (t1 \ t) ≈ t
401148
@test (t / t2) * t2 ≈ t
402-
AMDGPU.@allowscalar begin
403-
@test t1 \ one(t1) ≈ inv(t1)
404-
@test one(t1) / t1 ≈ pinv(t1)
405-
tp = pinv(t) * t
406-
@test tp ≈ tp * tp
407-
end=#
149+
@test t1 \ one(t1) ≈ inv(t1)
150+
@test one(t1) / t1 ≈ pinv(t1)
151+
tp = pinv(t) * t
152+
@test tp ≈ tp * tp
408153
@test_throws SpaceMismatch inv(t)
409154
@test_throws SpaceMismatch t2 \ t
410155
@test_throws SpaceMismatch t / t1
@@ -504,58 +249,10 @@ for V in spacelist
504249
# end
505250
#
506251
# TODO
507-
@timedtestset "Tensor product: test via norm preservation" begin
508-
for T in (ComplexF64,)
509-
t1 = AMDGPU.rand(T, V1, V5')
510-
t2 = AMDGPU.rand(T, V2 ⊗ V3, V4')
511-
t = @constinferred (t1 ⊗ t2)
512-
@test norm(t) ≈ norm(t1) * norm(t2)
513-
end
514-
end
515-
symmetricbraiding && @timedtestset "Tensor product: test via conversion" begin
516-
for T in (Float32, ComplexF64)
517-
t1 = AMDGPU.rand(T, V1, V5')
518-
t2 = AMDGPU.rand(T, V2 ⊗ V3, V4')
519-
d1 = dim(codomain(t1))
520-
d2 = dim(codomain(t2))
521-
d3 = dim(domain(t1))
522-
d4 = dim(domain(t2))
523-
t = @constinferred (t1 ⊗ t2)
524-
At = ad(t)
525-
@test ad(t) ≈ ad(t1) ⊗ ad(t2)
526-
end
527-
end
528-
symmetricbraiding && @timedtestset "Tensor product: test via tensor contraction" begin
529-
for T in (Float32, ComplexF64)
530-
t1 = AMDGPU.rand(T, V2 ⊗ V3 ⊗ V1)
531-
t2 = AMDGPU.rand(T, V2 ⊗ V1 ⊗ V3)
532-
t = @constinferred (t1 ⊗ t2)
533-
@tensor t′[1, 2, 3, 4, 5, 6] := t1[1, 2, 3] * t2[4, 5, 6]
534-
# @test t ≈ t′ # TODO broken for symmetry: Irrep[ℤ₃]
535-
end
536-
end
537252
end
538253
TensorKit.empty_globalcaches!()
539254
end
540255

541-
@timedtestset "Deligne tensor product: test via conversion" begin
542-
using .TestSetup: Vtr, VRepℤ₂, VRepSU₂, VRepA4
543-
@testset for Vlist1 in (Vtr, VRepSU₂), Vlist2 in (VRepℤ₂, VRepA4)
544-
V1, V2, V3, V4, V5 = Vlist1
545-
W1, W2, W3, W4, W5 = Vlist2
546-
for T in (Float32, ComplexF64)
547-
t1 = AMDGPU.rand(T, V1 ⊗ V2, V3' ⊗ V4)
548-
t2 = AMDGPU.rand(T, W2, W1 ⊗ W1')
549-
t = @constinferred (t1 ⊠ t2)
550-
d1 = dim(codomain(t1))
551-
d2 = dim(codomain(t2))
552-
d3 = dim(domain(t1))
553-
d4 = dim(domain(t2))
554-
@test ad(t1) ⊠ ad(t2) ≈ ad(t1 ⊠ t2)
555-
end
556-
end
557-
end
558-
559256
@timedtestset "DiagonalTensorMap index manipulations with domain $V: test via CPU" for V in (
560257
(ℂ^4)',
561258
Vect[Z2Irrep](0 => 2, 1 => 3),

0 commit comments

Comments
 (0)