Skip to content

Commit 3d21f41

Browse files
authored
Rewind the allocator after the temporaries of blas_contract! and planarcontract! (#628)
* Rewind the allocator after the temporaries of blas_contract! and planarcontract! * Test the allocator rewind with an AMD buffer
1 parent 86d33b5 commit 3d21f41

8 files changed

Lines changed: 98 additions & 0 deletions

File tree

‎src/planar/planaroperations.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ function planarcontract!(
221221
A_in_layout = (oindA, cindA) == (codomainind(A), domainind(A))
222222
B_in_layout = (cindB, oindB) == (codomainind(B), domainind(B))
223223

224+
cp = TO.allocator_checkpoint!(allocator)
224225
if A_in_layout
225226
A′ = A
226227
else
@@ -254,6 +255,7 @@ function planarcontract!(
254255

255256
A_in_layout || TO.tensorfree!(A′, allocator)
256257
B_in_layout || TO.tensorfree!(B′, allocator)
258+
TO.allocator_reset!(allocator, cp)
257259
end
258260
return C
259261
end

‎src/tensors/braidingtensor.jl‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ function planarcontract!(
233233
# using a single `braid!` is wrong: it would resolve cyclic moves as crossings and
234234
# pick up spurious R-symbol factors.
235235
B_in_layout = (cindB == codB && oindB == domB)
236+
cp = TO.allocator_checkpoint!(allocator)
236237
if B_in_layout
237238
B′ = B
238239
else
@@ -255,6 +256,7 @@ function planarcontract!(
255256
)
256257

257258
B_in_layout || TO.tensorfree!(B′, allocator)
259+
TO.allocator_reset!(allocator, cp)
258260
return C
259261
end
260262
function planarcontract!(
@@ -292,6 +294,7 @@ function planarcontract!(
292294
# pick up spurious R-symbol factors.
293295

294296
A_in_layout = (oindA == codA && cindA == domA)
297+
cp = TO.allocator_checkpoint!(allocator)
295298
if A_in_layout
296299
A′ = A
297300
else
@@ -315,6 +318,7 @@ function planarcontract!(
315318
)
316319

317320
A_in_layout || TO.tensorfree!(A′, allocator)
321+
TO.allocator_reset!(allocator, cp)
318322
return C
319323
end
320324

‎src/tensors/tensoroperations.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ function blas_contract!(
422422
twistB = false
423423
end
424424

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

459460
copyA && TO.tensorfree!(Anew, allocator)
460461
copyB && TO.tensorfree!(Bnew, allocator)
462+
TO.allocator_reset!(allocator, cp)
461463

462464
return C
463465
end

‎test/amd/tensors.jl‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,21 @@ end
628628
@test (@tensor s[1; 2] * d[2; 1]) ≈ (@tensor scpu[1; 2] * dcpu[2; 1])
629629
end
630630
end
631+
632+
@timedtestset "Contraction outside `@tensor` rewinds an AMD buffer" begin
633+
using .TestSetup: VRepU₁, VfRepSU₂
634+
for V in (VRepU₁, VfRepSU₂)
635+
V1, V2, V3, V4 = V
636+
A = AMDGPU.randn(Float64, V1 ⊗ V2 ⊗ V3)
637+
B = AMDGPU.randn(Float64, V3' ⊗ V2' ⊗ V4)
638+
@tensor Cref[d; a] := A[a b c] * B[c b d]
639+
buffer = TO.AMDBufferAllocator()
640+
C = TO.tensorcontract(
641+
A, ((1,), (2, 3)), false, B, ((2, 1), (3,)), false, ((2,), (1,)), true,
642+
TO.DefaultBackend(), buffer
643+
)
644+
@test isempty(buffer)
645+
@test C ≈ Cref
646+
@test buffer.max_offset > 0
647+
end
648+
end

‎test/chainrules/tensoroperations.jl‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ for V in spacelist
126126
end
127127
end
128128

129+
@testset "tensorcontract! pullback rewinds the allocator" begin
130+
using .TestSetup: VRepU₁, VRepSU₂, VfRepSU₂
131+
for V in (VRepU₁, VRepSU₂, VfRepSU₂)
132+
V1, V2, V3, V4 = V
133+
A = randn(V1 ⊗ V2 ⊗ V3)
134+
B = randn(V3' ⊗ V2' ⊗ V4)
135+
C = randn(V4 ← V1')
136+
buffer = TensorOperations.BufferAllocator()
137+
_, pullback = rrule(
138+
tensorcontract!, C, A, ((1,), (2, 3)), false, B, ((2, 1), (3,)), false,
139+
((2,), (1,)), 2.0, 1.0, TensorOperations.DefaultBackend(), buffer
140+
)
141+
@test isempty(buffer)
142+
foreach(unthunk, pullback(randn(space(C))))
143+
@test isempty(buffer)
144+
@test buffer.max_offset > 0
145+
end
146+
end
147+
129148
# https://github.com/quantumkithub/TensorKit.jl/issues/209
130149
@testset "Issue #209" begin
131150
function f(T, D)

‎test/cuda/tensors.jl‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,3 +701,21 @@ end
701701
@test (@tensor s[1; 2] * d[2; 1]) ≈ (@tensor scpu[1; 2] * dcpu[2; 1])
702702
end
703703
end
704+
705+
@timedtestset "Contraction outside `@tensor` rewinds a CUDA buffer" begin
706+
using .TestSetup: VRepU₁, VfRepSU₂
707+
for V in (VRepU₁, VfRepSU₂)
708+
V1, V2, V3, V4 = V
709+
A = cuRAND.randn(Float64, V1 ⊗ V2 ⊗ V3)
710+
B = cuRAND.randn(Float64, V3' ⊗ V2' ⊗ V4)
711+
@tensor Cref[d; a] := A[a b c] * B[c b d]
712+
buffer = TO.CUDABufferAllocator()
713+
C = TO.tensorcontract(
714+
A, ((1,), (2, 3)), false, B, ((2, 1), (3,)), false, ((2,), (1,)), true,
715+
TO.DefaultBackend(), buffer
716+
)
717+
@test isempty(buffer)
718+
@test C ≈ Cref
719+
@test buffer.max_offset > 0
720+
end
721+
end

‎test/tensors/contractions.jl‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Test, TestExtras
22
using TensorKit
33
using TensorKit: type_repr
4+
using TensorOperations
45

56

67
spacelist = default_spacelist(fast_tests)
@@ -178,6 +179,29 @@ for V in spacelist
178179
TensorKit.empty_globalcaches!()
179180
end
180181

182+
@timedtestset "Contraction outside `@tensor` rewinds the allocator" begin
183+
# `tensorfree!` is a no-op for a `BufferAllocator`, so the permuted copies of a
184+
# contraction are only given back by resetting it; outside `@tensor` there is no
185+
# surrounding checkpoint to do so.
186+
using .TestSetup: VRepU₁, VRepSU₂, VfRepSU₂
187+
@testset for V in (VRepU₁, VRepSU₂, VfRepSU₂)
188+
V1, V2, V3, V4 = V
189+
A = randn(V1 ⊗ V2 ⊗ V3)
190+
B = randn(V3' ⊗ V2' ⊗ V4)
191+
@tensor Cref[d; a] := A[a b c] * B[c b d]
192+
buffer = TensorOperations.BufferAllocator()
193+
for _ in 1:2
194+
C = TensorOperations.tensorcontract(
195+
A, ((1,), (2, 3)), false, B, ((2, 1), (3,)), false, ((2,), (1,)), true,
196+
TensorOperations.DefaultBackend(), buffer
197+
)
198+
@test isempty(buffer)
199+
@test C ≈ Cref
200+
end
201+
@test buffer.max_offset > 0
202+
end
203+
end
204+
181205
@timedtestset "Deligne tensor product: test via conversion" begin
182206
using .TestSetup: Vtr, VRepℤ₂, VRepSU₂, VRepA4
183207
@testset for Vlist1 in (Vtr, VRepSU₂), Vlist2 in (VRepℤ₂, VRepA4)

‎test/tensors/planar.jl‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,17 @@ end
249249
@test isempty(buffer)
250250
@test C ≈ Cref
251251
@test C2 ≈ Cref
252+
253+
# a direct call has no surrounding checkpoint, so it has to rewind by itself
254+
t = rand(T, W ⊗ W ⊗ W ← W ⊗ W)
255+
u = rand(T, W ⊗ W ← W ⊗ W ⊗ W)
256+
@planar ρref[a; b] := t[a c d; e f] * u[e f; b c d]
257+
ρ = planarcontract!(
258+
zeros(T, W ← W), t, ((1,), (4, 5, 3, 2)), u, ((1, 2, 5, 4), (3,)),
259+
((1,), (2,)), true, false, TensorOperations.DefaultBackend(), buffer
260+
)
261+
@test isempty(buffer)
262+
@test ρ ≈ ρref
252263
end
253264
end
254265

0 commit comments

Comments
 (0)