You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 8aa4d68
Browse filesBrowse the repository at this point in the historyBrowse files
perf: invert index maps when slicing and assigning block tensors (#72)
* fix: use cartesian keys in `nonzero_pairs`
`pairs(A::AbstractVector)` uses linear indices, so `nonzero_pairs` yielded `Int`
keys for the parent of a one-dimensional dense `BlockTensorMap`, inconsistent
with `nonzero_keys`. Consumers comparing against `CartesianIndex` then silently
matched nothing, which made `t[:]` and `t[1:2]` return uninitialized blocks.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* perf: invert index maps when slicing block tensors
Slicing `getindex` scanned the whole source index grid once per stored block,
making it `O(nnz * prod(length.(indices)))` and materializing the slice region
as an `Array{CartesianIndex}`. Invert the index maps per dimension instead, so
each stored block finds its destinations in `O(1)`:
n nnz before after speedup
16 16 0.0045 ms 0.0015 ms 3x
64 64 0.1452 ms 0.0036 ms 40x
256 256 8.9455 ms 0.0107 ms 836x
512 512 71.1836 ms 0.0188 ms 3786x
(banded sparse block tensor, sliced to every other channel; the dense
`BlockTensorMap` variant goes from 7.94 ms to 0.087 ms at n = 64.)
`SparseTensorArray` had a second copy of the same logic for slicing the parent
array; both now share `_copyslice!`.
Along the way this fixes four bugs in the old index handling:
- Repeated indices dropped blocks instead of duplicating them, and for a dense
destination the dropped slot was returned as uninitialized memory. They now
duplicate, as they do for `AbstractArray`.
- Single-index slicing of one-dimensional block tensors (`t[:]`, `t[1:2]`)
returned uninitialized blocks.
- `Integer` indices other than `Int` threw: `t[UInt(1), 1:2, 1]` a
`BoundsError`, `t[Int8(1), Int8(1), Int8(1)]` a `MethodError`.
- Logical masks threw `CanonicalIndexError` on the parent array path,
`parent(t)[[true, false, true], :, :]`.
The `Vararg{Strided.SliceIndex}` methods are kept: they disambiguate against
TensorKit's own `getindex`/`setindex!` for `AbstractTensorMap`, so removing
them turns `t[:, :, :]` into a `MethodError`.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* perf: make sparse slice assignment scale with nnz
The three `SparseTensorArray` `copyto!` methods all walked the assigned region
rather than the stored blocks, so `t[inds...] = v` and `cat` cost
`O(prod(length.(inds)))` regardless of how little was stored — the mirror image
of the `getindex` problem in #67. Reuse the inverted index maps instead:
shape region nnz before after speedup
2d n=64 3_844 64 0.150 ms 0.015 ms 10x
2d n=256 64_516 256 2.267 ms 0.036 ms 63x
2d n=512 260_100 512 13.752 ms 0.051 ms 269x
4d n=24 234_256 24 9.459 ms 0.009 ms 1040x
4d n=48 4_477_456 48 140.997 ms 0.013 ms 10600x
narrow 4 512 0.005 ms 0.004 ms 1x
`copyto!(dst, view(parent(t), inds...))` improves likewise, 14.6 ms to 0.041 ms
for a 260_100-block region.
The narrow row matters: a small assignment into a large sparse array must not
become proportional to the destination's stored blocks. `_deletemissing!`
therefore sweeps whichever of the two is smaller, making it
`O(nnz(src) + min(region, nnz(dst)))` — never worse than before.
`copyto!(t::SparseTensorArray, ::SubArray)` needs no sweep at all: the
destination spans exactly the viewed region, so everything not copied is
dropped.
Also fix the `nonzero_*` accessors for `SparseTensorArray`, which fell through
to the `AbstractArray` fallbacks and reported every index rather than the stored
ones. `nonzero_keys` and `nonzero_length` were always wrong this way;
`nonzero_pairs` became wrong when it started going through
`pairs(IndexCartesian(), A)`, which bypasses `Base.pairs(::SparseTensorArray)`.
The visible effects were that slicing the parent array densified its result and
that `copyto!` over regions stored explicitly-zero blocks.
Verified semantics-preserving: the stored-key sets after slice assignment,
`copyto!` from a view, region-to-region `copyto!` and `cat` are identical to the
previous implementation once the accessors are fixed.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: docs/src/blocktensors.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,6 +71,7 @@ s[1] += 2 * s[1]
71
71
Slicing operations are also supported, and the `AbstractBlockTensorMap` can be sliced in the same way as an `AbstractArray{AbstractTensorMap}`.
72
72
There is however one elementary difference: as the slices still contain tensors with the same amount of legs, there can be no reduction in the number of dimensions.
73
73
In particular, in contrast to `AbstractArray`, scalar dimensions are not discarded, and as a result, linear index slicing is not allowed.
74
+
Repeated indices duplicate the selected tensors, as they would for an `AbstractArray`.
0 commit comments