Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/planar/planaroperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ function planarcontract!(
A_in_layout = (oindA, cindA) == (codomainind(A), domainind(A))
B_in_layout = (cindB, oindB) == (codomainind(B), domainind(B))

cp = TO.allocator_checkpoint!(allocator)
if A_in_layout
A′ = A
else
Expand Down Expand Up @@ -254,6 +255,7 @@ function planarcontract!(

A_in_layout || TO.tensorfree!(A′, allocator)
B_in_layout || TO.tensorfree!(B′, allocator)
TO.allocator_reset!(allocator, cp)
end
return C
end
Expand Down
4 changes: 4 additions & 0 deletions src/tensors/braidingtensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ function planarcontract!(
# using a single `braid!` is wrong: it would resolve cyclic moves as crossings and
# pick up spurious R-symbol factors.
B_in_layout = (cindB == codB && oindB == domB)
cp = TO.allocator_checkpoint!(allocator)
if B_in_layout
B′ = B
else
Expand All @@ -255,6 +256,7 @@ function planarcontract!(
)

B_in_layout || TO.tensorfree!(B′, allocator)
TO.allocator_reset!(allocator, cp)
return C
end
function planarcontract!(
Expand Down Expand Up @@ -292,6 +294,7 @@ function planarcontract!(
# pick up spurious R-symbol factors.

A_in_layout = (oindA == codA && cindA == domA)
cp = TO.allocator_checkpoint!(allocator)
if A_in_layout
A′ = A
else
Expand All @@ -315,6 +318,7 @@ function planarcontract!(
)

A_in_layout || TO.tensorfree!(A′, allocator)
TO.allocator_reset!(allocator, cp)
return C
end

Expand Down
2 changes: 2 additions & 0 deletions src/tensors/tensoroperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ function blas_contract!(
twistB = false
end

cp = TO.allocator_checkpoint!(allocator)
# Bring A in the correct form for BLAS contraction
if copyA
Anew = @timeit_debug GLOBAL_TIMER "alloc: buffers" TO.tensoralloc_add(TC, A, pA, false, Val(true), allocator)
Expand Down Expand Up @@ -458,6 +459,7 @@ function blas_contract!(

copyA && TO.tensorfree!(Anew, allocator)
copyB && TO.tensorfree!(Bnew, allocator)
TO.allocator_reset!(allocator, cp)

return C
end
Expand Down
18 changes: 18 additions & 0 deletions test/amd/tensors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,21 @@ end
@test (@tensor s[1; 2] * d[2; 1]) ≈ (@tensor scpu[1; 2] * dcpu[2; 1])
end
end

@timedtestset "Contraction outside `@tensor` rewinds an AMD buffer" begin
using .TestSetup: VRepU₁, VfRepSU₂
for V in (VRepU₁, VfRepSU₂)
V1, V2, V3, V4 = V
A = AMDGPU.randn(Float64, V1 ⊗ V2 ⊗ V3)
B = AMDGPU.randn(Float64, V3' ⊗ V2' ⊗ V4)
@tensor Cref[d; a] := A[a b c] * B[c b d]
buffer = TO.AMDBufferAllocator()
C = TO.tensorcontract(
A, ((1,), (2, 3)), false, B, ((2, 1), (3,)), false, ((2,), (1,)), true,
TO.DefaultBackend(), buffer
)
@test isempty(buffer)
@test C ≈ Cref
@test buffer.max_offset > 0
end
end
19 changes: 19 additions & 0 deletions test/chainrules/tensoroperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ for V in spacelist
end
end

@testset "tensorcontract! pullback rewinds the allocator" begin
using .TestSetup: VRepU₁, VRepSU₂, VfRepSU₂
for V in (VRepU₁, VRepSU₂, VfRepSU₂)
V1, V2, V3, V4 = V
A = randn(V1 ⊗ V2 ⊗ V3)
B = randn(V3' ⊗ V2' ⊗ V4)
C = randn(V4 ← V1')
buffer = TensorOperations.BufferAllocator()
_, pullback = rrule(
tensorcontract!, C, A, ((1,), (2, 3)), false, B, ((2, 1), (3,)), false,
((2,), (1,)), 2.0, 1.0, TensorOperations.DefaultBackend(), buffer
)
@test isempty(buffer)
foreach(unthunk, pullback(randn(space(C))))
@test isempty(buffer)
@test buffer.max_offset > 0
end
end

# https://github.com/quantumkithub/TensorKit.jl/issues/209
@testset "Issue #209" begin
function f(T, D)
Expand Down
18 changes: 18 additions & 0 deletions test/cuda/tensors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -701,3 +701,21 @@ end
@test (@tensor s[1; 2] * d[2; 1]) ≈ (@tensor scpu[1; 2] * dcpu[2; 1])
end
end

@timedtestset "Contraction outside `@tensor` rewinds a CUDA buffer" begin
Comment thread
leburgel marked this conversation as resolved.
Resolved
using .TestSetup: VRepU₁, VfRepSU₂
for V in (VRepU₁, VfRepSU₂)
V1, V2, V3, V4 = V
A = cuRAND.randn(Float64, V1 ⊗ V2 ⊗ V3)
B = cuRAND.randn(Float64, V3' ⊗ V2' ⊗ V4)
@tensor Cref[d; a] := A[a b c] * B[c b d]
buffer = TO.CUDABufferAllocator()
C = TO.tensorcontract(
A, ((1,), (2, 3)), false, B, ((2, 1), (3,)), false, ((2,), (1,)), true,
TO.DefaultBackend(), buffer
)
@test isempty(buffer)
@test C ≈ Cref
@test buffer.max_offset > 0
end
end
24 changes: 24 additions & 0 deletions test/tensors/contractions.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Test, TestExtras
using TensorKit
using TensorKit: type_repr
using TensorOperations


spacelist = default_spacelist(fast_tests)
Expand Down Expand Up @@ -178,6 +179,29 @@ for V in spacelist
TensorKit.empty_globalcaches!()
end

@timedtestset "Contraction outside `@tensor` rewinds the allocator" begin
# `tensorfree!` is a no-op for a `BufferAllocator`, so the permuted copies of a
# contraction are only given back by resetting it; outside `@tensor` there is no
# surrounding checkpoint to do so.
using .TestSetup: VRepU₁, VRepSU₂, VfRepSU₂
@testset for V in (VRepU₁, VRepSU₂, VfRepSU₂)
V1, V2, V3, V4 = V
A = randn(V1 ⊗ V2 ⊗ V3)
B = randn(V3' ⊗ V2' ⊗ V4)
@tensor Cref[d; a] := A[a b c] * B[c b d]
buffer = TensorOperations.BufferAllocator()
for _ in 1:2
C = TensorOperations.tensorcontract(
A, ((1,), (2, 3)), false, B, ((2, 1), (3,)), false, ((2,), (1,)), true,
TensorOperations.DefaultBackend(), buffer
)
@test isempty(buffer)
@test C ≈ Cref
end
@test buffer.max_offset > 0
end
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)
Expand Down
11 changes: 11 additions & 0 deletions test/tensors/planar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,17 @@ end
@test isempty(buffer)
@test C ≈ Cref
@test C2 ≈ Cref

# a direct call has no surrounding checkpoint, so it has to rewind by itself
t = rand(T, W ⊗ W ⊗ W ← W ⊗ W)
u = rand(T, W ⊗ W ← W ⊗ W ⊗ W)
@planar ρref[a; b] := t[a c d; e f] * u[e f; b c d]
ρ = planarcontract!(
zeros(T, W ← W), t, ((1,), (4, 5, 3, 2)), u, ((1, 2, 5, 4), (3,)),
((1,), (2,)), true, false, TensorOperations.DefaultBackend(), buffer
)
@test isempty(buffer)
@test ρ ≈ ρref
end
end

Expand Down
Loading