Skip to content

Commit 9cf39b1

Browse files
lkdvosclaude
andcommitted
Allocate sparse blocks through the allocator in tensoralloc_add/tensoralloc_contract
Sparse block tensors were allocated empty and filled lazily on the heap, while `tensorfree!` released every block through the allocator. Allocate exactly the blocks that will be written instead, and pre-insert the product blocks into a sparse destination so that temporaries derived from it are complete. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 294d3aa commit 9cf39b1

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

‎src/tensors/tensoroperations.jl‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,79 @@ function TO.tensoralloc(
105105
return C
106106
end
107107

108+
# sparse results allocate exactly the blocks that will be written, so `tensorfree!` is consistent
109+
const BlockOrAdjoint = Union{AbstractBlockTensorMap, AdjointBlockTensorMap}
110+
111+
_tensoralloc(ttype, structure, keys, istemp::Val, allocator) =
112+
TO.tensoralloc(ttype, structure, istemp, allocator)
113+
function _tensoralloc(
114+
ttype::Type{<:SparseBlockTensorMap}, structure, keys, istemp::Val, allocator
115+
)
116+
C = ttype(undef_blocks, structure)
117+
Vs = eachspace(C)
118+
for I in keys
119+
C[I] = TO.tensoralloc(eltype(C), Vs[I], istemp, allocator)
120+
end
121+
return C
122+
end
123+
124+
function contract_keys(A, pA::Index2Tuple, B, pB::Index2Tuple, pAB::Index2Tuple)
125+
OB = NTuple{length(pB[2]), Int}
126+
openB = Dict{NTuple{length(pB[1]), Int}, Vector{OB}}()
127+
for IB in nonzero_keys(B)
128+
push!(get!(Vector{OB}, openB, TT.getindices(IB.I, pB[1])), TT.getindices(IB.I, pB[2]))
129+
end
130+
p = TO.linearize(pAB)
131+
keys = Set{CartesianIndex{length(p)}}()
132+
for IA in nonzero_keys(A)
133+
oA = TT.getindices(IA.I, pA[1])
134+
for oB in get(openB, TT.getindices(IA.I, pA[2]), ())
135+
push!(keys, CartesianIndex(TT.getindices((oA..., oB...), p)))
136+
end
137+
end
138+
return keys
139+
end
140+
141+
function TO.tensoralloc_add(
142+
TC, A::BlockOrAdjoint, pA::Index2Tuple, conjA::Bool,
143+
istemp::Val = Val(false), allocator = TO.DefaultAllocator()
144+
)
145+
ttype = TO.tensoradd_type(TC, A, pA, conjA)
146+
structure = TO.tensoradd_structure(A, pA, conjA)
147+
p = TO.linearize(pA)
148+
keys = (CartesianIndex(TT.getindices(I.I, p)) for I in nonzero_keys(A))
149+
return _tensoralloc(ttype, structure, keys, istemp, allocator)
150+
end
151+
152+
function TO.tensoralloc_contract(
153+
TC, A::BlockOrAdjoint, pA::Index2Tuple, conjA::Bool,
154+
B::BlockOrAdjoint, pB::Index2Tuple, conjB::Bool,
155+
pAB::Index2Tuple, istemp::Val = Val(false), allocator = TO.DefaultAllocator()
156+
)
157+
ttype = TO.tensorcontract_type(TC, A, pA, conjA, B, pB, conjB, pAB)
158+
structure = TO.tensorcontract_structure(A, pA, conjA, B, pB, conjB, pAB)
159+
keys = contract_keys(A, pA, B, pB, pAB)
160+
return _tensoralloc(ttype, structure, keys, istemp, allocator)
161+
end
162+
163+
# temporaries derived from `C` must already contain every block the contraction produces
164+
function TO.tensorcontract!(
165+
C::SparseBlockTensorMap,
166+
A::BlockOrAdjoint, pA::Index2Tuple, conjA::Bool,
167+
B::BlockOrAdjoint, pB::Index2Tuple, conjB::Bool,
168+
pAB::Index2Tuple, α::Number, β::Number, backend, allocator
169+
)
170+
for I in contract_keys(A, pA, B, pB, pAB)
171+
getindex!(C, I)
172+
end
173+
return @invoke TO.tensorcontract!(
174+
C::AbstractTensorMap,
175+
A::AbstractTensorMap, pA::Index2Tuple, conjA::Bool,
176+
B::AbstractTensorMap, pB::Index2Tuple, conjB::Bool,
177+
pAB::Index2Tuple, α::Number, β::Number, backend::Any, allocator::Any
178+
)
179+
end
180+
108181
# tensorfree!
109182
# -----------
110183
function TO.tensorfree!(t::BlockTensorMap, allocator = TO.DefaultAllocator())

‎test/linalg/tensoroperations.jl‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,55 @@ end
150150
@test convert(TensorMap, F1) ≈ F2
151151
end
152152
end
153+
154+
##
155+
156+
struct TrackingAllocator
157+
live::Set{UInt}
158+
foreign::Base.RefValue{Int}
159+
end
160+
TrackingAllocator() = TrackingAllocator(Set{UInt}(), Ref(0))
161+
function TensorOperations.tensoralloc(
162+
::Type{A}, structure, ::Val, allocator::TrackingAllocator
163+
) where {A <: AbstractArray}
164+
x = fill!(TensorOperations.tensoralloc(A, structure, Val(false)), NaN)
165+
push!(allocator.live, objectid(x))
166+
return x
167+
end
168+
function TensorOperations.tensorfree!(x::AbstractArray, allocator::TrackingAllocator)
169+
if objectid(x) in allocator.live
170+
delete!(allocator.live, objectid(x))
171+
else
172+
allocator.foreign[] += 1
173+
end
174+
return nothing
175+
end
176+
177+
@testset "allocator only frees what it allocated ($(sectortype(a)))" for (a, b, c) in (
178+
(SumSpace(ℂ^2, ℂ^1), SumSpace(ℂ^1, ℂ^2, ℂ^1), SumSpace(ℂ^2, ℂ^3)),
179+
(
180+
SumSpace(Vect[FermionParity](0 => 1, 1 => 1), Vect[FermionParity](0 => 2)),
181+
SumSpace(Vect[FermionParity](1 => 2), Vect[FermionParity](0 => 1, 1 => 1)),
182+
SumSpace(Vect[FermionParity](0 => 1), Vect[FermionParity](1 => 1)),
183+
),
184+
)
185+
A = sprand(Float64, a ⊗ b ← c, 0.4)
186+
B = sprand(Float64, c ⊗ b' ← a, 0.4)
187+
D = sprand(Float64, a ← b, 0.4)
188+
189+
allocator = TrackingAllocator()
190+
@tensor allocator = allocator E[x; q] := A[x z; w] * B[w z; y] * D[y; q]
191+
@tensor Eref[x; q] := A[x z; w] * B[w z; y] * D[y; q]
192+
@test E ≈ Eref
193+
@test allocator.foreign[] == 0
194+
195+
@tensor C[x y; p q] := A[x q; w] * B[w p; y]
196+
C = spzeros(Float64, space(C))
197+
C[1, 1, 1, 1] = randn(Float64, space(C[1, 1, 1, 1]))
198+
Cref = copy(C)
199+
allocator = TrackingAllocator()
200+
@tensor allocator = allocator C[x y; p q] += A[x q; w] * B[w p; y]
201+
@tensor Cref[x y; p q] += A[x q; w] * B[w p; y]
202+
@test C ≈ Cref
203+
@test allocator.foreign[] == 0
204+
end

0 commit comments

Comments
 (0)