Skip to content

Commit 204c99f

Browse files
lkdvosclaude
andauthored
fix: restore inferrable return type for block tensor slicing (#73)
`_slice_getindex` chose between returning a single block and a sliced block tensor with a runtime `inds isa NTuple{M, Int}` check, and `_slice_getindex_single` reached that check with an index tuple of element type `Union{Int, UnitRange{Int}}`, so the check could not be resolved statically. `blocktensor[2:(end - 1)]` on a tensor with a single nontrivial dimension therefore inferred as `Union{BlockTensorMap{...}, TensorMap{...}}` rather than `BlockTensorMap{...}`, and a logical mask inferred as `Any`. Make the choice by dispatch instead, and fill the trivial dimensions with `1:1` so that `Int` stays out of the index tuple. A mask is normalized up front, since it may consume several dimensions and `Base.to_indices` cannot infer one mixed with the fillers. Runtime behaviour is unchanged: slicing was verified to give identical types and values to v0.3.16 across every index form (`Int`, ranges, step ranges, `Colon`, index vectors, logical masks; single-index and full-rank) on dense and sparse tensors of one and three dimensions. Downstream this widened MPSKit's Jordan-form derivative operators to their abstract type, which in turn made `excitations` with `ChepigaAnsatz`/`ChepigaAnsatz2` uninferrable. The new tests cover the single-index path on a multi-index tensor, which had none: the existing single-index tests use a genuinely one-dimensional tensor and so take the `M == numind(t)` fast path. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 230cec7 commit 204c99f

3 files changed

Lines changed: 56 additions & 4 deletions

File tree

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BlockTensorKit"
22
uuid = "5f87ffc2-9cf1-4a46-8172-465d160bd8cd"
3-
version = "0.3.16"
3+
version = "0.3.17"
44
authors = ["Lukas Devos <ldevos98@gmail.com> and contributors"]
55

66
[deps]

‎src/tensors/abstractblocktensor/abstractarray.jl‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,33 @@ end
100100
t::AbstractBlockTensorMap, indices::Vararg{Any, M}
101101
) where {M}
102102
M == numind(t) || return _slice_getindex_single(t, indices...)
103-
inds = Base.to_indices(t, indices)
104-
inds isa NTuple{M, Int} && return parent(t)[inds...]
103+
return _slice_getindex_full(t, Base.to_indices(t, indices))
104+
end
105+
106+
# single block vs. sliced block tensor is chosen by dispatch, so the return type is inferrable
107+
@propagate_inbounds _slice_getindex_full(
108+
t::AbstractBlockTensorMap, inds::NTuple{M, Int}
109+
) where {M} = parent(t)[inds...]
110+
@propagate_inbounds function _slice_getindex_full(t::AbstractBlockTensorMap, inds::Tuple)
105111
tdst = similar(t, space(eachspace(t)[inds...]))
106112
length(tdst) == 0 && return tdst
107113
return _copyslice!(tdst, t, inds)
108114
end
109115

116+
# a mask may consume several dimensions, which `to_indices` cannot infer through the fillers below
117+
@propagate_inbounds _slice_getindex_single(
118+
t::AbstractBlockTensorMap, index::AbstractArray{Bool}
119+
) = _slice_getindex_single(t, Base.to_index(t, index))
120+
110121
# a single index is only supported when it selects a single nontrivial dimension
111122
@noinline function _slice_getindex_single(
112123
t::AbstractBlockTensorMap, indices::Vararg{Any, M}
113124
) where {M}
114125
space(eachspace(t)[indices...]) # errors as before if unsupported
115126
d = something(findfirst(>(1), size(t)), 1)
116-
return _slice_getindex(t, ntuple(i -> i == d ? only(indices) : 1, numind(t))...)
127+
# `1:1` rather than `1` keeps `Int` out of the tuple, so the dispatch above stays static
128+
inds = ntuple(i -> i == d ? only(indices) : (1:1), numind(t))
129+
return _slice_getindex_full(t, Base.to_indices(t, inds))
117130
end
118131

119132
# TODO: check if this fallback is fair

‎test/abstracttensor/indexing.jl‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,45 @@ for (label, t1) in (("dense", rand(V ← one(V))), ("sparse", sprand(V ← one(V
9292
end
9393
end
9494

95+
# single-index slicing of a multi-index block tensor takes a different path than the case above
96+
W = SumSpace(ℂ^2)
97+
for (label, ts) in (("dense", rand(W ⊗ V ← W)), ("sparse", sprand(W ⊗ V ← W, 0.8)))
98+
@testset "$label single-index slicing" begin
99+
@test size(ts) == (1, 3, 1)
100+
mid = 2:(length(ts) - 1)
101+
# a single index must stay as inferrable as the equivalent full-rank slice
102+
@test @inferred(ts[mid]) == ts[:, mid, :]
103+
@test space(ts[mid]) == space(ts[:, mid, :])
104+
@test @inferred(ts[1]) isa TensorMap
105+
@test @inferred(ts[:]) == ts
106+
@test size(@inferred(ts[[1, 3]])) == (1, 2, 1)
107+
@test ts[[1, 3]][1, 2, 1] == ts[1, 3, 1]
108+
mask = [true, false, true]
109+
@test @inferred(ts[mask]) == ts[:, mask, :]
110+
end
111+
end
112+
113+
# a widened return type propagates into everything built from a slice, so keep every form concrete
114+
infers_concretely(f, args...) =
115+
isconcretetype(Core.Compiler.return_type(f, Tuple{map(typeof, args)...}))
116+
117+
@testset "indexing inference ($label)" for (label, t) in (
118+
("dense", rand(W ⊗ V ← W)), ("sparse", sprand(W ⊗ V ← W, 0.8)),
119+
("dense square", rand(V ⊗ V ← V)), ("sparse square", sprand(V ⊗ V ← V, 0.5)),
120+
("dense vector", rand(V ← one(V))), ("sparse vector", sprand(V ← one(V), 0.8)),
121+
)
122+
for ind in (1, 2:3, 1:2:3, :, [1, 3], [true, false, true])
123+
@test infers_concretely(getindex, t, ind)
124+
end
125+
ndims(t) == 3 || continue
126+
for inds in (
127+
(1, 2, 1), (:, :, :), (1, 2:3, 1), (1, [1, 3], 1), (1, [true, false, true], 1),
128+
(1:1, 1:2:3, 1:1), ([1, 1, 3], :, :),
129+
)
130+
@test infers_concretely(getindex, t, inds...)
131+
end
132+
end
133+
95134
# the parent array has its own slicing implementation
96135
@testset "parent array slicing" begin
97136
st = sprand(V ⊗ V ⊗ V, 0.5)

0 commit comments

Comments
 (0)