Skip to content

Commit 57e9012

Browse files
committed
Merge branch 'worktree-agent-aceca53c0bddcaa75' into optimizatio
2 parents ee4ff0a + 1e24f74 commit 57e9012

5 files changed

Lines changed: 76 additions & 31 deletions

File tree

‎src/tensors/abstractblocktensor/conversion.jl‎

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,50 @@
11
# Conversion
22
# ----------
33

4-
function _copy_subblocks!(tdst, tsrc)
5-
S = spacetype(tsrc)
6-
N₁, N₂ = numout(tsrc), numin(tsrc)
7-
for ((f₁, f₂), arr) in subblocks(tdst)
8-
blockax = ntuple(N₁ + N₂) do i
9-
return if i <= N₁
10-
blockedrange(map(Base.Fix2(dim, f₁.uncoupled[i]), space(tsrc, i)))
11-
else
12-
blockedrange(map(Base.Fix2(dim, f₂.uncoupled[i - N₁]), space(tsrc, i)'))
13-
end
14-
end
4+
function _subblock_offsets(W::TensorMapSumSpace)
5+
return map(eachspace(W).sumspaces) do V
6+
sectortype(V) === Trivial && return Returns([0; cumsum(dims(V))])
7+
offsets = Dict(s => [0; cumsum(map(Base.Fix2(dim, s), V.spaces))] for s in sectors(V))
8+
return Base.Fix1(getindex, offsets)
9+
end
10+
end
11+
12+
function _subblock_ranges(offsets, (f₁, f₂), I::CartesianIndex)
13+
return map(offsets, (f₁.uncoupled..., f₂.uncoupled...), Tuple(I)) do o, s, j
14+
r = o(s)
15+
return (r[j] + 1):r[j + 1]
16+
end
17+
end
18+
19+
function _subblock_pairs(t::AbstractTensorMap)
20+
sectortype(t) === Trivial || return subblocks(t)
21+
f = TK.trivial_fusiontree(t)
22+
return (f => subblock(t, f),)
23+
end
24+
function _subblock_getter(t::AbstractTensorMap)
25+
return sectortype(t) === Trivial ? Base.Fix1(subblock, t) : Base.Fix1(getindex, subblocks(t))
26+
end
27+
28+
function _copy_subblocks!(tdst::TensorMap, tsrc::AbstractBlockTensorMap)
29+
offsets = _subblock_offsets(space(tsrc))
30+
dst = _subblock_getter(tdst)
31+
for (I, v) in nonzero_pairs(tsrc), (f, src) in _subblock_pairs(v)
32+
copy!(view(dst(f), _subblock_ranges(offsets, f, I)...), src)
33+
end
34+
return tdst
35+
end
1536

16-
for (k, v) in nonzero_pairs(tsrc)
17-
indices = getindex.(blockax, Block.(Tuple(k)))
18-
arr_slice = arr[indices...]
19-
# need to check for empty since fusion tree pair might not be present
20-
isempty(arr_slice) || copy!(arr_slice, v[f₁, f₂])
37+
function _copy_subblocks!(tdst::AbstractBlockTensorMap, tsrc::AbstractTensorMap)
38+
offsets = _subblock_offsets(space(tdst))
39+
src = _subblock_getter(tsrc)
40+
for (I, V) in pairs(eachspace(tdst))
41+
v = similar(eltype(tdst), V)
42+
vblocks = _subblock_pairs(v)
43+
issparse(tdst) && all(isempty ∘ last, vblocks) && continue
44+
for (f, dst) in vblocks
45+
copy!(dst, view(src(f), _subblock_ranges(offsets, f, I)...))
2146
end
47+
@inbounds tdst[I] = v
2248
end
2349
return tdst
2450
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)