Skip to content

Commit efa177a

Browse files
lkdvosclaude
andcommitted
Copy subblocks once per block in TensorMap <-> block tensor conversion
`_copy_subblocks!` looped over destination fusion trees and, for each, over all source blocks calling `v[f₁, f₂]` (a structure cache lookup) even when the slice was empty, rebuilding `blockedrange` axes per tree. The reverse constructors `BlockTensorMap(t, space)` / `SparseBlockTensorMap(t, space)` indexed the block tensor per fusion tree, building a mortar over all blocks. Both directions now share one generic body (branching on `issparse`): loop over the blocks once, iterate each block's `subblocks` once, and copy to/from the matching slice of the full tensor's subblock, looked up through a single `subblocks` iterator. Per-leg block offsets are precomputed per uncoupled sector. Trivial sectors take the cheap `subblock` path to avoid a structure lookup per block. Sparse targets keep the previous semantics of storing every block with a nonempty space. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6d9efe0 commit efa177a

5 files changed

Lines changed: 68 additions & 24 deletions

File tree

‎src/tensors/abstractblocktensor/conversion.jl‎

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
11
# Conversion
22
# ----------
33

4-
function _copy_subblocks!(tdst, tsrc)
5-
N₁, N₂ = numout(tsrc), numin(tsrc)
6-
offsets = ntuple(i -> _sumspace_offsets(i <= N₁ ? codomain(tsrc)[i] : domain(tsrc)[i - N₁]), N₁ + N₂)
7-
# a single `SubblockIterator` holds the structure: `tdst[f₁, f₂]` would look it up in TensorKit's locked cache every time
8-
dstblocks = subblocks(tdst)
9-
for (k, v) in nonzero_pairs(tsrc), ((f₁, f₂), src) in subblocks(v)
10-
ranges = map(offsets, (f₁.uncoupled..., f₂.uncoupled...), Tuple(k)) do o, c, kᵢ
11-
return (o[c][kᵢ] + 1):o[c][kᵢ + 1]
4+
function _subblock_ranges(offsets, (f₁, f₂), I::CartesianIndex)
5+
return map(offsets, (f₁.uncoupled..., f₂.uncoupled...), Tuple(I)) do o, c, kᵢ
6+
return (o[c][kᵢ] + 1):o[c][kᵢ + 1]
7+
end
8+
end
9+
10+
function _subblock_pairs(t::AbstractTensorMap)
11+
sectortype(t) === Trivial || return subblocks(t)
12+
f = TK.trivial_fusiontree(t)
13+
return (f => subblock(t, f),)
14+
end
15+
function _subblock_getter(t::AbstractTensorMap)
16+
return sectortype(t) === Trivial ? Base.Fix1(subblock, t) : Base.Fix1(getindex, subblocks(t))
17+
end
18+
19+
function _copy_subblocks!(tdst::TensorMap, tsrc::AbstractBlockTensorMap)
20+
offsets = map(_sumspace_offsets, eachspace(tsrc).sumspaces)
21+
dst = _subblock_getter(tdst)
22+
for (I, v) in nonzero_pairs(tsrc), (f, src) in _subblock_pairs(v)
23+
copy!(view(dst(f), _subblock_ranges(offsets, f, I)...), src)
24+
end
25+
return tdst
26+
end
27+
28+
function _copy_subblocks!(tdst::AbstractBlockTensorMap, tsrc::AbstractTensorMap)
29+
offsets = map(_sumspace_offsets, eachspace(tdst).sumspaces)
30+
src = _subblock_getter(tsrc)
31+
for (I, V) in pairs(eachspace(tdst))
32+
v = similar(eltype(tdst), V)
33+
vblocks = _subblock_pairs(v)
34+
issparse(tdst) && all(isempty ∘ last, vblocks) && continue
35+
for (f, dst) in vblocks
36+
copy!(dst, view(src(f), _subblock_ranges(offsets, f, I)...))
1237
end
13-
copy!(dstblocks[(f₁, f₂)][ranges...], src)
38+
@inbounds tdst[I] = v
1439
end
1540
return tdst
1641
end

‎src/tensors/blocktensor.jl‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,7 @@ end
112112
# AbstractTensorMap -> BlockTensorMap
113113
function BlockTensorMap(t::AbstractTensorMap, space::TensorMapSumSpace)
114114
TT = tensormaptype(spacetype(t), numout(t), numin(t), storagetype(t))
115-
tdst = BlockTensorMap{TT}(undef, space)
116-
@inbounds for (f₁, f₂) in fusiontrees(t)
117-
dst = tdst[f₁, f₂]
118-
src = t[f₁, f₂]
119-
for block_index in Iterators.product(blockaxes(dst)...)
120-
indices = getindex.(axes(dst), block_index)
121-
dst[block_index...] .= @view src[indices...]
122-
end
123-
end
124-
return tdst
115+
return _copy_subblocks!(BlockTensorMap{TT}(undef_blocks, space), t)
125116
end
126117

127118
# Convenience constructors

‎src/tensors/sparseblocktensor.jl‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,7 @@ end
8585
# AbstractTensorMap -> SparseBlockTensorMap
8686
function SparseBlockTensorMap(t::AbstractTensorMap, space::TensorMapSumSpace)
8787
TT = tensormaptype(spacetype(t), numout(t), numin(t), storagetype(t))
88-
tdst = SparseBlockTensorMap{TT}(undef, space)
89-
for (f₁, f₂) in fusiontrees(tdst)
90-
tdst[f₁, f₂] = t[f₁, f₂]
91-
end
92-
return tdst
88+
return _copy_subblocks!(SparseBlockTensorMap{TT}(undef_blocks, space), t)
9389
end
9490

9591
# Utility constructors

‎test/abstracttensor/blocktensor.jl‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ end
9999
@test t1 ≈ t1″
100100
end
101101

102+
@testset "TensorMap conversion: sectors and empty blocks" begin
103+
Vs = (
104+
Vect[U1Irrep](0 => 2, 1 => 1) ⊞ Vect[U1Irrep](-1 => 1, 2 => 1) ⊞ Vect[U1Irrep](1 => 2),
105+
Vect[SU2Irrep](0 => 1, 1 // 2 => 2) ⊞ Vect[SU2Irrep](1 => 1) ⊞ Vect[SU2Irrep](1 // 2 => 1, 3 // 2 => 1),
106+
ℂ^0 ⊞ ℂ^2 ⊞ ℂ^3,
107+
)
108+
for V in Vs, W in (V ⊗ V' ← V ⊗ V, V ⊗ V ⊗ V' ← one(V)), T in scalartypes
109+
t = randn(T, W)
110+
t′ = @constinferred TensorMap(t)
111+
@test norm(t) ≈ norm(t′)
112+
@test BlockTensorMap(t′, W) ≈ t
113+
@test SparseBlockTensorMap(t′, W) ≈ t
114+
end
115+
end
116+
102117
@testset "Adapt" begin
103118
W = V1 ⊗ V2 ⊗ V3 ← V4 ⊗ V5
104119
t1 = rand(Float32, W)

‎test/abstracttensor/sparseblocktensor.jl‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ end
9191
end
9292
end
9393

94+
@testset "TensorMap conversion: sectors and empty blocks" begin
95+
Vs = (
96+
Vect[U1Irrep](0 => 2, 1 => 1) ⊞ Vect[U1Irrep](-1 => 1, 2 => 1) ⊞ Vect[U1Irrep](1 => 2),
97+
Vect[SU2Irrep](0 => 1, 1 // 2 => 2) ⊞ Vect[SU2Irrep](1 => 1) ⊞ Vect[SU2Irrep](1 // 2 => 1, 3 // 2 => 1),
98+
ℂ^0 ⊞ ℂ^2 ⊞ ℂ^3,
99+
)
100+
for V in Vs, W in (V ⊗ V' ← V ⊗ V, V ⊗ V ⊗ V' ← one(V)), T in scalartypes
101+
t = sprand(T, W, 0.5)
102+
t′ = @constinferred TensorMap(t)
103+
@test norm(t) ≈ norm(t′)
104+
t″ = SparseBlockTensorMap(t′, W)
105+
@test t″ ≈ t
106+
@test all(I -> dim(eachspace(t)[I]) > 0, nonzero_keys(t″))
107+
@test TensorMap(BlockTensorMap(t)) ≈ t′
108+
end
109+
end
110+
94111
@testset "Adapt" begin
95112
W = V1 ⊗ V2 ⊗ V3 ← V4 ⊗ V5
96113
t1 = sprand(Float32, W, 0.5)

0 commit comments

Comments
 (0)