From c6253d27f8f9f6c87cf7b9576c67f667fc656141 Mon Sep 17 00:00:00 2001 From: leburgel Date: Thu, 19 Nov 2026 08:47:28 +0100 Subject: [PATCH 1/2] Rewind the allocator after the temporaries of blas_contract! and planarcontract! --- src/planar/planaroperations.jl | 2 ++ src/tensors/braidingtensor.jl | 4 ++++ src/tensors/tensoroperations.jl | 2 ++ test/chainrules/tensoroperations.jl | 19 +++++++++++++++++++ test/cuda/tensors.jl | 18 ++++++++++++++++++ test/tensors/contractions.jl | 24 ++++++++++++++++++++++++ test/tensors/planar.jl | 11 +++++++++++ 7 files changed, 80 insertions(+) diff --git a/src/planar/planaroperations.jl b/src/planar/planaroperations.jl index 5cae4ea8f..263f9453b 100644 --- a/src/planar/planaroperations.jl +++ b/src/planar/planaroperations.jl @@ -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 @@ -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 diff --git a/src/tensors/braidingtensor.jl b/src/tensors/braidingtensor.jl index ea72f713b..b36e01bb0 100644 --- a/src/tensors/braidingtensor.jl +++ b/src/tensors/braidingtensor.jl @@ -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 @@ -255,6 +256,7 @@ function planarcontract!( ) B_in_layout || TO.tensorfree!(B′, allocator) + TO.allocator_reset!(allocator, cp) return C end function planarcontract!( @@ -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 @@ -315,6 +318,7 @@ function planarcontract!( ) A_in_layout || TO.tensorfree!(A′, allocator) + TO.allocator_reset!(allocator, cp) return C end diff --git a/src/tensors/tensoroperations.jl b/src/tensors/tensoroperations.jl index e11a1edf0..c0f530be9 100644 --- a/src/tensors/tensoroperations.jl +++ b/src/tensors/tensoroperations.jl @@ -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) @@ -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 diff --git a/test/chainrules/tensoroperations.jl b/test/chainrules/tensoroperations.jl index c3fd557b8..8347cd289 100644 --- a/test/chainrules/tensoroperations.jl +++ b/test/chainrules/tensoroperations.jl @@ -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) diff --git a/test/cuda/tensors.jl b/test/cuda/tensors.jl index f4c178549..b626fe128 100644 --- a/test/cuda/tensors.jl +++ b/test/cuda/tensors.jl @@ -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 + 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 diff --git a/test/tensors/contractions.jl b/test/tensors/contractions.jl index 1132809dc..d7a330410 100644 --- a/test/tensors/contractions.jl +++ b/test/tensors/contractions.jl @@ -1,6 +1,7 @@ using Test, TestExtras using TensorKit using TensorKit: type_repr +using TensorOperations spacelist = default_spacelist(fast_tests) @@ -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) diff --git a/test/tensors/planar.jl b/test/tensors/planar.jl index dcf7502e8..a7be20415 100644 --- a/test/tensors/planar.jl +++ b/test/tensors/planar.jl @@ -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 From 62a79b3a8e330969c4e22e5a2c0e90a35104f0d4 Mon Sep 17 00:00:00 2001 From: leburgel Date: Thu, 19 Nov 2026 11:54:56 +0100 Subject: [PATCH 2/2] Test the allocator rewind with an AMD buffer --- test/amd/tensors.jl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/amd/tensors.jl b/test/amd/tensors.jl index 453933d72..50e7541cf 100644 --- a/test/amd/tensors.jl +++ b/test/amd/tensors.jl @@ -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