Skip to content

Commit 4b3a1e2

Browse files
kshyattlkdvos
andauthored
Incremental fixes for MPSKit (#54)
* Incremental fixes for MPSKit * Use sources again * Update Project.toml Let's try being a little more flexible with compat... * Try to fix similar_tensormaptype * A few more specializations for truncation and diagonal * Apply suggestions from code review Co-authored-by: Lukas Devos <ldevos98@gmail.com> * Restore old storagetype --------- Co-authored-by: Lukas Devos <ldevos98@gmail.com>
1 parent 5a66126 commit 4b3a1e2

6 files changed

Lines changed: 73 additions & 7 deletions

File tree

‎ext/BlockTensorKitGPUArraysExt.jl‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,15 @@ function KernelAbstractions.get_backend(BA::BlockArrays.BlockArray{T, N, A}) whe
88
return KernelAbstractions.get_backend(first(BA.blocks))
99
end
1010

11+
function Base.copyto!(dest::BM, src::TA) where {T <: Number, TA <: AnyGPUMatrix{T}, BM <: BlockMatrix{T, Matrix{TA}}}
12+
# TODO -- should we use Threads here to parallelize these
13+
# transfers in streams if possible?
14+
for block_index in Iterators.product(blockaxes(dest)...)
15+
indices = getindex.(axes(dest), block_index)
16+
dest_view = @view dest[block_index...]
17+
dest_view .= src[indices...]
18+
end
19+
return dest
20+
end
21+
1122
end

‎src/linalg/factorizations.jl‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ for f! in (
2525
)
2626
@eval function MAK.$f!(t::AbstractBlockTensorMap, F, alg::AbstractAlgorithm)
2727
TensorKit.foreachblock(t, F...) do _, (tblock, Fblocks...)
28-
dense_block = similar_dense(tblock)
29-
Fblocks′ = MAK.$f!(copy_dense!(dense_block, tblock), alg)
28+
Fblocks′ = MAK.$f!(copy_dense!(similar_dense(tblock), tblock), alg)
3029
# deal with the case where the output is not in-place
3130
for (b′, b) in zip(Fblocks′, Fblocks)
32-
b === b′ || copy!(b, b′)
31+
b === b′ || copyto!(b, b′)
3332
end
3433
return nothing
3534
end
@@ -45,10 +44,9 @@ for f! in (
4544
)
4645
@eval function MAK.$f!(t::AbstractBlockTensorMap, N, alg::AbstractAlgorithm)
4746
TensorKit.foreachblock(t, N) do _, (tblock, Nblock)
48-
dense_block = similar_dense(tblock)
49-
Nblock′ = MAK.$f!(copy_dense!(dense_block, tblock), alg)
47+
Nblock′ = MAK.$f!(copy_dense!(similar_dense(tblock), tblock), alg)
5048
# deal with the case where the output is not the same as the input
51-
Nblock === Nblock′ || copy!(Nblock, Nblock′)
49+
Nblock === Nblock′ || copyto!(Nblock, Nblock′)
5250
return nothing
5351
end
5452
return N
@@ -190,3 +188,30 @@ for f! in (
190188
@eval MAK.$f!(::AbstractBlockTensorMap, x, ::DiagonalAlgorithm) =
191189
error("Blocktensors are incompatible with diagonal algorithm")
192190
end
191+
192+
function TensorKit.Factorizations.truncate_domain!(tdst::AbstractBlockTensorMap, tsrc::AbstractBlockTensorMap, inds)
193+
TensorKit.foreachblock(tdst, tsrc) do c, (dst_block, src_block)
194+
I = get(inds, c, nothing)
195+
dst_dense = copy_dense!(similar_dense(dst_block), dst_block)
196+
src_dense = copy_dense!(similar_dense(src_block), src_block)
197+
@assert !isnothing(I)
198+
@views dst_dense .= src_dense[:, I]
199+
# deal with the case where the output is not in-place
200+
dst_dense === dst_block || copyto!(dst_block, dst_dense)
201+
return nothing
202+
end
203+
return tdst
204+
end
205+
function TensorKit.Factorizations.truncate_codomain!(tdst::AbstractBlockTensorMap, tsrc::AbstractBlockTensorMap, inds)
206+
TensorKit.foreachblock(tdst, tsrc) do c, (dst_block, src_block)
207+
I = get(inds, c, nothing)
208+
dst_dense = copy_dense!(similar_dense(dst_block), dst_block)
209+
src_dense = copy_dense!(similar_dense(src_block), src_block)
210+
@assert !isnothing(I)
211+
@views dst_dense .= src_dense[I, :]
212+
# deal with the case where the output is not in-place
213+
dst_dense === dst_block || copyto!(dst_block, dst_dense)
214+
return nothing
215+
end
216+
return tdst
217+
end

‎src/linalg/linalg.jl‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,23 @@ function LinearAlgebra.isposdef!(t::AbstractBlockTensorMap)
239239
end
240240
return true
241241
end
242+
243+
function LinearAlgebra.lmul!(D::DiagonalTensorMap, t::AbstractBlockTensorMap)
244+
domain(D) == codomain(t) || throw(SpaceMismatch())
245+
TensorKit.foreachblock(t, D) do c, (tblock, Dblock)
246+
tblock′ = lmul!(Dblock, copy_dense!(similar_dense(tblock), tblock))
247+
tblock === tblock′ || copyto!(tblock, tblock′)
248+
return tblock
249+
end
250+
return t
251+
end
252+
253+
function LinearAlgebra.rmul!(t::AbstractBlockTensorMap, D::DiagonalTensorMap)
254+
codomain(D) == domain(t) || throw(SpaceMismatch())
255+
TensorKit.foreachblock(t, D) do c, (tblock, Dblock)
256+
tblock′ = rmul!(copy_dense!(similar_dense(tblock), tblock), Dblock)
257+
tblock === tblock′ || copyto!(tblock, tblock′)
258+
return tblock
259+
end
260+
return t
261+
end

‎src/tensors/abstractblocktensor/abstractarray.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ function similar_tensormaptype(
288288
) where {S}
289289
if eltype(t) === T && typeof(space(t)) === typeof(P)
290290
return T
291-
elseif isconcretetype(T)
291+
elseif isconcretetype(T) || T isa Union
292292
return tensormaptype(S, numout(P), numin(P), storagetype(T))
293293
else
294294
return AbstractTensorMap{scalartype(T), S, numout(P), numin(P)}

‎src/tensors/blocktensor.jl‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ end
161161
Base.eltype(::Type{<:BlockTensorMap{TT}}) where {TT} = TT
162162
Base.parent(t::BlockTensorMap) = t.data
163163

164+
# handle this separately because the storagetype of `AbstractTensorMap` is
165+
# *always* Vector no matter the actual data storage type
166+
TK.storagetype(t::BlockTensorMap{AbstractTensorMap{E, S, N₁, N₂}}) where {E, S, N₁, N₂} =
167+
foldl(TK.promote_storagetype, values(t.data))
168+
164169
function Base.copyto!(
165170
dest::BlockTensorMap, Rdest::CartesianIndices,
166171
src::BlockTensorMap, Rsrc::CartesianIndices,

‎src/tensors/sparseblocktensor.jl‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ VI.scalartype(::Type{<:SparseBlockTensorMap{TT}}) where {TT} = scalartype(TT)
168168
Base.parent(t::SparseBlockTensorMap) = SparseTensorArray(t.data, space(t))
169169
Base.eltype(::Type{<:SparseBlockTensorMap{TT}}) where {TT} = TT
170170

171+
# handle this separately because the storagetype of `AbstractTensorMap` is
172+
# *always* Vector no matter the actual data storage type
173+
TK.storagetype(t::SparseBlockTensorMap{AbstractTensorMap{E, S, N₁, N₂}}) where {E, S, N₁, N₂} =
174+
foldl(TK.promote_storagetype, nonzero_values(t))
175+
171176
issparse(::SparseBlockTensorMap) = true
172177
nonzero_keys(t::SparseBlockTensorMap) = keys(t.data)
173178
nonzero_values(t::SparseBlockTensorMap) = values(t.data)

0 commit comments

Comments
 (0)