Skip to content

Commit 813e2c2

Browse files
committed
Working braiding transpose and more tests
1 parent 3c5477c commit 813e2c2

3 files changed

Lines changed: 63 additions & 52 deletions

File tree

Project.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
name = "TensorKit"
22
uuid = "07d1fe3e-3e46-537d-9eac-e9e13d0d4cec"
3-
authors = ["Jutho Haegeman, Lukas Devos"]
43
version = "0.16.3"
4+
authors = ["Jutho Haegeman, Lukas Devos"]
55

66
[deps]
7+
GPUArrays = "0c68f7d7-f131-5f86-a1c3-88cf8149b2d7"
78
LRUCache = "8ac3fa9e-de4c-5943-b1dc-09c6b5f20637"
89
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
910
MatrixAlgebraKit = "6c742aac-3347-4629-af66-fc926824e5e4"
@@ -53,7 +54,7 @@ Printf = "1"
5354
Random = "1"
5455
SafeTestsets = "0.1"
5556
ScopedValues = "1.3.0"
56-
Strided = "2.3.4"
57+
Strided = "2.3.5"
5758
TensorKitSectors = "0.3.5"
5859
TensorOperations = "5.1"
5960
Test = "1"
@@ -87,3 +88,6 @@ cuTENSOR = "011b41b2-24ef-40a8-b3eb-fa098493e9e1"
8788

8889
[targets]
8990
test = ["ArgParse", "Adapt", "Aqua", "AllocCheck", "Combinatorics", "CUDA", "cuTENSOR", "GPUArrays", "LinearAlgebra", "SafeTestsets", "TensorOperations", "Test", "TestExtras", "ChainRulesCore", "ChainRulesTestUtils", "FiniteDifferences", "Zygote", "Mooncake", "JET"]
91+
92+
[sources]
93+
Strided = {url = "https://github.com/QuantumKitHub/Strided.jl", rev = "ksh/copyto"}

ext/TensorKitCUDAExt/cutensormap.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,12 @@ function TensorKit._add_general_kernel_nonthreaded!(
177177
end
178178
return nothing
179179
end
180+
181+
function TensorKit.allocate_buffers(
182+
tdst::CuTensorMap, tsrc::CuTensorMap, transformer::TensorKit.GenericTreeTransformer
183+
)
184+
sz = TensorKit.buffersize(transformer)
185+
# force zeros to ensure the buffers are empty
186+
# otherwise memory re-use can fill them with garbage data
187+
return CUDA.zeros(eltype(tdst.data), sz), CUDA.zeros(eltype(tsrc.data), sz)
188+
end

test/cuda/tensors.jl

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -290,28 +290,29 @@ for V in spacelist
290290
@timedtestset "Permutations: test via inner product invariance" begin
291291
W = V1 V2 V3 V4 V5
292292
t = CUDA.rand(ComplexF64, W)
293+
ht = adapt(Vector{ComplexF64}, t)
293294
t′ = CUDA.randn!(similar(t))
295+
ht′ = adapt(Vector{ComplexF64}, t′)
296+
dot_htt′ = dot(ht′, ht)
297+
dot_tt′ = dot(t′, t)
298+
@test dot_tt′ dot_htt′
299+
norm_t = norm(t)
294300
for k in 0:5
295301
for p in permutations(1:5)
296302
p1 = ntuple(n -> p[n], k)
297303
p2 = ntuple(n -> p[k + n], 5 - k)
298-
CUDA.@allowscalar begin
299-
t2 = @constinferred permute(t, (p1, p2))
300-
t2 = permute(t, (p1, p2))
301-
@test norm(t2) norm(t)
302-
t2′ = permute(t′, (p1, p2))
303-
@test dot(t2′, t2) dot(t′, t) dot(transpose(t2′), transpose(t2))
304-
end
305-
end
306-
307-
CUDA.@allowscalar begin
308-
t3 = @constinferred repartition(t, $k)
309-
t3 = repartition(t, k)
310-
@test norm(t3) norm(t)
311-
t3′ = @constinferred repartition!(similar(t3), t′)
312-
@test norm(t3′) norm(t′)
313-
@test dot(t′, t) dot(t3′, t3)
304+
t2 = @constinferred permute(t, (p1, p2))
305+
t2′ = permute(t′, (p1, p2))
306+
@test norm(t2) norm_t
307+
@test dot(t2′, t2) dot_tt′
308+
@test dot(transpose(t2′), transpose(t2)) dot_tt′
314309
end
310+
t3 = @constinferred repartition(t, $k)
311+
t3 = repartition(t, k)
312+
t3′ = @constinferred repartition!(similar(t3), t′)
313+
@test norm(t3) norm(t)
314+
@test norm(t3′) norm(t′)
315+
@test dot(t′, t) dot(t3′, t3)
315316
end
316317
end
317318
if BraidingStyle(I) isa SymmetricBraiding
@@ -322,34 +323,35 @@ for V in spacelist
322323
for p in permutations(1:5)
323324
p1 = ntuple(n -> p[n], k)
324325
p2 = ntuple(n -> p[k + n], 5 - k)
325-
dt2 = CUDA.@allowscalar permute(t, (p1, p2))
326-
ht2 = permute(TensorKit.to_cpu(t), (p1, p2))
327-
@test ht2 == adapt(Vector{ComplexF64}, dt2)
326+
ht2 = permute(adapt(Vector{ComplexF64}, t), (p1, p2))
327+
dt2 = permute(t, (p1, p2))
328+
@test ht2 adapt(Vector{ComplexF64}, dt2)
329+
ht3 = transpose(adapt(Vector{ComplexF64}, dt2))
330+
dt3 = transpose(dt2)
331+
hht3 = adapt(Vector{ComplexF64}, dt3)
332+
@test ht3 hht3
328333
end
329-
330-
dt3 = CUDA.@allowscalar repartition(t, k)
331-
ht3 = repartition(adapt(Vector{ComplexF64}, t), k)
332-
@test ht3 == adapt(Vector{ComplexF64}, dt3)
334+
dt4 = repartition(t, k)
335+
ht4 = repartition(adapt(Vector{ComplexF64}, t), k)
336+
@test ht4 == adapt(Vector{ComplexF64}, dt4)
333337
end
334338
end
335339
end
336340
@timedtestset "Full trace: test self-consistency" begin
337341
t = CUDA.rand(ComplexF64, V1 V2' V2 V1')
338-
CUDA.@allowscalar begin
339-
t2 = permute(t, ((1, 2), (4, 3)))
340-
s = @constinferred tr(t2)
341-
@test conj(s) tr(t2')
342-
if !isdual(V1)
343-
t2 = twist!(t2, 1)
344-
end
345-
if isdual(V2)
346-
t2 = twist!(t2, 2)
347-
end
348-
ss = tr(t2)
349-
@tensor s2 = t[a, b, b, a]
350-
@tensor t3[a, b] := t[a, c, c, b]
351-
@tensor s3 = t3[a, a]
342+
t2 = permute(t, ((1, 2), (4, 3)))
343+
s = @constinferred tr(t2)
344+
@test conj(s) tr(t2')
345+
if !isdual(V1)
346+
t2 = twist!(t2, 1)
352347
end
348+
if isdual(V2)
349+
t2 = twist!(t2, 2)
350+
end
351+
ss = tr(t2)
352+
@tensor s2 = t[a, b, b, a]
353+
@tensor t3[a, b] := t[a, c, c, b]
354+
@tensor s3 = t3[a, a]
353355
@test ss s2
354356
@test ss s3
355357
end
@@ -363,24 +365,20 @@ for V in spacelist
363365
if BraidingStyle(I) isa Bosonic && hasfusiontensor(I)
364366
@timedtestset "Trace: test via conversion" begin
365367
t = CUDA.rand(ComplexF64, V1 V2' V3 V2 V1' V3')
366-
CUDA.@allowscalar begin
367-
@tensor t2[a, b] := t[c, d, b, d, c, a]
368-
@tensor t3[a, b] := ad(t)[c, d, b, d, c, a]
369-
end
368+
@tensor t2[a, b] := t[c, d, b, d, c, a]
369+
@tensor t3[a, b] := ad(t)[c, d, b, d, c, a]
370370
@test t3 ad(t2)
371371
end
372372
end
373373
@timedtestset "Trace and contraction" begin
374374
t1 = CUDA.rand(ComplexF64, V1 V2 V3)
375375
t2 = CUDA.rand(ComplexF64, V2' V4 V1')
376-
CUDA.@allowscalar begin
377-
t3 = t1 t2
378-
@tensor ta[a, b] := t1[x, y, a] * t2[y, b, x]
379-
@tensor tb[a, b] := t3[x, y, a, y, b, x]
380-
end
376+
t3 = t1 t2
377+
@tensor ta[a, b] := t1[x, y, a] * t2[y, b, x]
378+
@tensor tb[a, b] := t3[x, y, a, y, b, x]
381379
@test ta tb
382380
end
383-
#=if BraidingStyle(I) isa Bosonic && hasfusiontensor(I)
381+
if BraidingStyle(I) isa Bosonic && hasfusiontensor(I)
384382
@timedtestset "Tensor contraction: test via CPU" begin
385383
dA1 = CUDA.randn(ComplexF64, V1' * V2', V3')
386384
dA2 = CUDA.randn(ComplexF64, V3 * V4, V5)
@@ -395,7 +393,7 @@ for V in spacelist
395393
TensorKit.to_cpu(dH)[s1, s2, t1, t2]
396394
@test TensorKit.to_cpu(dHrA12) hHrA12
397395
end
398-
end=# # doesn't yet work because of AdjointTensor
396+
end
399397
@timedtestset "Index flipping: test flipping inverse" begin
400398
t = CUDA.rand(ComplexF64, V1 V1' V1' V1)
401399
for i in 1:4
@@ -405,7 +403,7 @@ for V in spacelist
405403
end
406404
end
407405
end
408-
#=@timedtestset "Index flipping: test via explicit flip" begin
406+
@timedtestset "Index flipping: test via explicit flip" begin
409407
t = CUDA.rand(ComplexF64, V1 V1' V1' V1)
410408
F1 = unitary(flip(V1), V1)
411409

@@ -433,7 +431,7 @@ for V in spacelist
433431
@tensor tb[a, b] := flip(t1, (1, 3))[x, y, a, z] * flip(t2, (2, 4))[y, b, z, x]
434432
@test flip(ta, (1, 2)) tb
435433
end
436-
end=# # TODO
434+
end
437435
@timedtestset "Multiplication of isometries: test properties" begin
438436
W2 = V4 V5
439437
W1 = W2 (oneunit(V1) oneunit(V1))

0 commit comments

Comments
 (0)