Skip to content

Commit a0d9ce4

Browse files
lkdvosclaude
andcommitted
Mark temporaries with a flag instead of storing their allocator
Blocks of temporaries are obtained from the allocator of the operation, and kernels without allocator throw instead of allocating them elsewhere. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8ba311f commit a0d9ce4

4 files changed

Lines changed: 23 additions & 27 deletions

File tree

‎src/tensors/sparseblocktensor.jl‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@
22
struct SparseBlockTensorMap{TT<:AbstractTensorMap{E,S,N₁,N₂}} <: AbstractBlockTensorMap{E,S,N₁,N₂}
33
44
Sparse `SparseBlockTensorMap` type that stores tensors of type `TT` in a sparse dictionary.
5-
Temporaries of TensorOperations keep their `allocator` (otherwise `nothing`), from which their
6-
blocks are obtained and to which they are released.
5+
Temporaries of TensorOperations are marked by `istemp`, as their blocks are obtained from and
6+
released to the allocator of the operation.
77
"""
88
struct SparseBlockTensorMap{TT <: AbstractTensorMap, E, S, N₁, N₂, N} <:
99
AbstractBlockTensorMap{E, S, N₁, N₂}
1010
data::Dict{CartesianIndex{N}, TT}
1111
space::TensorMapSumSpace{S, N₁, N₂}
12-
allocator::Any
12+
istemp::Bool
1313

1414
# uninitialized constructor
1515
function SparseBlockTensorMap{TT, E, S, N₁, N₂, N}(
16-
::UndefBlocksInitializer, space::TensorMapSumSpace{S, N₁, N₂}; allocator = nothing
16+
::UndefBlocksInitializer, space::TensorMapSumSpace{S, N₁, N₂}; istemp::Bool = false
1717
) where {E, S, N₁, N₂, N, TT <: AbstractTensorMap{E, S, N₁, N₂}}
1818
@assert N₁ + N₂ == N "SparseBlockTensorMap: data has wrong number of dimensions"
1919
data = Dict{CartesianIndex{N}, TT}()
20-
return new{TT, E, S, N₁, N₂, N}(data, space, allocator)
20+
return new{TT, E, S, N₁, N₂, N}(data, space, istemp)
2121
end
2222

2323
# constructor from data
2424
function SparseBlockTensorMap{TT, E, S, N₁, N₂, N}(
2525
data::Dict{CartesianIndex{N}, TT}, space::TensorMapSumSpace{S, N₁, N₂}
2626
) where {E, S, N₁, N₂, N, TT <: AbstractTensorMap{E, S, N₁, N₂}}
2727
@assert N₁ + N₂ == N "SparseBlockTensorMap: data has wrong number of dimensions"
28-
return new{TT, E, S, N₁, N₂, N}(data, space, nothing)
28+
return new{TT, E, S, N₁, N₂, N}(data, space, false)
2929
end
3030
end
3131

@@ -226,15 +226,17 @@ for f in (:(Random.rand!), :(Random.randn!), :(Random.randexp!), :(TK.randisomet
226226
invoke($f, Tuple{Random.AbstractRNG, AbstractTensorMap}, rng, storeentries!(t))
227227
end
228228

229-
# blocks of temporaries are obtained from (and released to) their own allocator, those of
230-
# other destinations from the allocator of the operation, if any
229+
# blocks of temporaries are obtained from (and released to) the allocator of the operation,
230+
# so kernels without allocator cannot add them
231231
istemp(t::AbstractBlockTensorMap) = false
232-
istemp(t::SparseBlockTensorMap) = !isnothing(t.allocator)
232+
istemp(t::SparseBlockTensorMap) = t.istemp
233233

234-
function newblock(t::AbstractBlockTensorMap, I::CartesianIndex, allocator = TO.DefaultAllocator())
234+
function newblock(t::AbstractBlockTensorMap, I::CartesianIndex, allocator = nothing)
235235
V = eachspace(t)[I]
236-
istemp(t) && return TO.tensoralloc(eltype(t), V, Val(true), t.allocator)
237-
return TO.tensoralloc(eltype(t), V, Val(false), allocator)
236+
istemp(t) || return TO.tensoralloc(eltype(t), V, Val(false), something(allocator, TO.DefaultAllocator()))
237+
isnothing(allocator) &&
238+
throw(ArgumentError("cannot add block $I to a temporary without its allocator"))
239+
return TO.tensoralloc(eltype(t), V, Val(true), allocator)
238240
end
239241

240242
@propagate_inbounds function getindex!(t::SparseBlockTensorMap, I::CartesianIndex{N}) where {N}

‎src/tensors/tensoroperations.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ function TO.tensoralloc(
116116
return C
117117
end
118118

119-
# sparse tensors start out empty, and temporaries remember their allocator
119+
# sparse tensors start out empty
120120
function TO.tensoralloc(
121121
::Type{BT}, structure::TensorMapSumSpace, istemp::Val, allocator = TO.DefaultAllocator()
122122
) where {BT <: SparseBlockTensorMap}
123123
TT = allocated_blocktype(eltype(BT), structure, istemp, allocator)
124124
return SparseBlockTensorMap{TT}(
125-
undef_blocks, structure; allocator = istemp === Val(true) ? allocator : nothing
125+
undef_blocks, structure; istemp = istemp === Val(true)
126126
)
127127
end
128128

‎src/tensors/vectorinterface.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ end
5353
# blockwise updates
5454
# -----------------
5555
@inline function _addblock!(
56-
f, t::AbstractBlockTensorMap, I::CartesianIndex, β::Number = One(), allocator = TO.DefaultAllocator()
56+
f, t::AbstractBlockTensorMap, I::CartesianIndex, β::Number = One(), allocator = nothing
5757
)
5858
t[I] = if haskey(t, I)
5959
f(t[I], isone(β) ? One() : β)

‎test/linalg/tensoroperations.jl‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,22 +212,16 @@ end
212212
check_allocator() do al
213213
return @planar allocator = al R[x; q] := A[x z; w] * B[w z; y] * D[y; q]
214214
end
215-
check_allocator() do al # kernels without allocator argument fill an empty temporary
216-
T = TensorOperations.tensoralloc(typeof(D), a ← a, Val(true), al)
217-
mul!(T, D, D')
218-
TensorOperations.tensortrace!(
219-
T, X, ((1,), (3,)), ((2,), (4,)), false, 1, 1, TensorOperations.DefaultBackend(), al
220-
)
221-
n = norm(T)
222-
TensorOperations.tensorfree!(T, al)
223-
return n
224-
end
215+
Y = sprand(Float64, a ⊗ b ← a ⊗ b, 1.0)
216+
T = TensorOperations.tensoralloc(typeof(Y), a ⊗ b ← a ⊗ b, Val(true))
217+
@test_throws ArgumentError TensorKit.one!(T) # blocks of temporaries require an allocator
218+
@test_throws ArgumentError mul!(T, Y, Y')
225219

226220
for f! in (TensorKit.one!, TensorKit.isomorphism!, TensorKit.isometry!)
227221
check_allocator() do al # blocks of a temporary off the diagonal are kept
228-
T = TensorOperations.tensoralloc_add(Float64, X, ((1, 2), (3, 4)), false, Val(true), al)
222+
T = TensorOperations.tensoralloc_add(Float64, Y, ((1, 2), (3, 4)), false, Val(true), al)
229223
TensorOperations.tensoradd!(
230-
T, X, ((1, 2), (3, 4)), false, 1, 0, TensorOperations.DefaultBackend(), al
224+
T, Y, ((1, 2), (3, 4)), false, 1, 0, TensorOperations.DefaultBackend(), al
231225
)
232226
n = norm(f!(T))
233227
TensorOperations.tensorfree!(T, al)

0 commit comments

Comments
 (0)