Skip to content

Commit 8ba311f

Browse files
lkdvosclaude
andcommitted
Keep the blocks of temporaries in one!/isomorphism!/isometry!
Off-diagonal blocks of a temporary belong to its allocator, so they are kept (and zeroed) instead of dropped. Zero blocks for missing entries are allocated as `similarstoragetype`, since storage types such as `PtrArray` cannot be constructed from a size. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4638d71 commit 8ba311f

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

‎src/tensors/abstractblocktensor/abstracttensormap.jl‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ function _subblock(t::AbstractBlockTensorMap, entries, (f₁, f₂)::Tuple{Fusio
105105
V = W[I]
106106
sz = (dims(codomain(V), f₁.uncoupled)..., dims(domain(V), f₂.uncoupled)...)
107107
if prod(sz) == 0 || (issparse(t) && !haskey(entries, I))
108-
return sreshape(StridedView(zerovector!(similar(storagetype(t), prod(sz)))), sz)
108+
data = similar(TK.similarstoragetype(storagetype(t), scalartype(t)), prod(sz))
109+
return sreshape(StridedView(zerovector!(data)), sz)
109110
else
110111
return getsubblock(entries[I], (f₁, f₂))
111112
end
@@ -121,10 +122,11 @@ function _blockdims(P::ProductSumSpace{S, N}, c::Sector) where {S, N}
121122
)
122123
end
123124

125+
# allocated as `similarstoragetype`, as the storage type itself need not be constructible
124126
zeroblock(::Type{TT}, d₁::Int, d₂::Int) where {TT <: AbstractTensorMap} =
125-
zerovector!(similar(storagetype(TT), d₁, d₂))
127+
zerovector!(similar(TK.similarstoragetype(TT, scalartype(TT)), d₁, d₂))
126128
function zeroblock(::Type{TT}, d₁::Int, d₂::Int) where {TT <: TensorMap}
127-
data = zerovector!(similar(storagetype(TT), d₁ * d₂))
129+
data = zerovector!(similar(TK.similarstoragetype(TT, scalartype(TT)), d₁ * d₂))
128130
return reshape(view(data, 1:(d₁ * d₂)), (d₁, d₂))
129131
end
130132
zeroblock(::Type{<:AdjointTensorMap{<:Any, <:Any, <:Any, <:Any, TT}}, d₁::Int, d₂::Int) where {TT} =
@@ -136,7 +138,7 @@ TensorKit.hasblock(t::AbstractBlockTensorMap, c::Sector) = c in blocksectors(t)
136138

137139
Base.@assume_effects :foldable function _blockeltype(::Type{TT}) where {TT <: AbstractBlockTensorMap}
138140
T = scalartype(TT)
139-
B = TK.blocktype(eltype(TT))
141+
B = Union{TK.blocktype(eltype(TT)), Base.promote_op(zeroblock, Type{eltype(TT)}, Int, Int)}
140142
return B <: AbstractMatrix{T} ? B : AbstractMatrix{T} # safeguard against type-instability
141143
end
142144
function TensorKit.blocktype(::Type{TT}) where {TT <: AbstractBlockTensorMap}

‎src/tensors/sparseblocktensor.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ for f in (:(TK.one!), :(TK.isomorphism!), :(TK.isometry!))
217217
@eval function $f(t::SparseBlockTensorMap)
218218
diagonal = diagonalentries(t)
219219
invoke($f, Tuple{AbstractTensorMap}, storeentries!(t, in(diagonal)))
220-
filter!(in(diagonal) ∘ first, t.data)
220+
istemp(t) || filter!(in(diagonal) ∘ first, t.data)
221221
return t
222222
end
223223
end

‎test/linalg/tensoroperations.jl‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,18 @@ end
223223
return n
224224
end
225225

226+
for f! in (TensorKit.one!, TensorKit.isomorphism!, TensorKit.isometry!)
227+
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)
229+
TensorOperations.tensoradd!(
230+
T, X, ((1, 2), (3, 4)), false, 1, 0, TensorOperations.DefaultBackend(), al
231+
)
232+
n = norm(f!(T))
233+
TensorOperations.tensorfree!(T, al)
234+
return n
235+
end
236+
end
237+
226238
# permuted and BLAS-compatible destinations, without and with blocks outside of the product
227239
@tensor C1[x y; p q] := A[x q; w] * B[w p; y]
228240
@tensor C2[x q; p y] := A[x q; w] * B[w p; y]

0 commit comments

Comments
 (0)