Repository navigation
Commit f3f660c
Specialise
* specialise dim to storage type
* oplus and ominus
* infimum and supremum
* fuse
* truncate_space
* restore binary search for sectordicts
* refactor sorted merge procedure
* speed up fuse slightly with sortperm
* import thing
* splat with type annotation above Val
* actually don't splat, but construct directly where previously a vector was made
* make slightly more readable maybe perhaps
* truncate_space always has non-dual entry spaces
* overkill iszero check in dim
* assert truncate_space spaces being non-dual
* introduce `sectorstoragetype`
* specialise truncation code to sectorstoragetype
* put `_sortedmerge` in `Base.mergewith` and use where possible
* introduce and use `blockdims`
* introduce the ntuple cutoff
* apply code suggestions
* slight refactor of sortmerge implementation
* introduce `FullVectorDict` and use it
* remove some reminder comments
* avoid `NTuple{N,Int}(::Vector)` conversion cliff at N == 32
`NTuple{N,Int}(v)` falls off Julia's unrolled fast path at exactly N = 32:
9.7 ns at N = 31 versus 825 ns at N = 32. That is precisely
`_ntuple_storage_threshold`, so the largest sector sets that tuple storage is
meant to cover were by far the slowest to build.
`ntuple(i -> @inbounds(v[i]), Val(N))` stays on the unrolled path and is faster
at every N (3.1 ns at N = 32), so the threshold can stay at 32.
Measured end-to-end, before -> after:
Vect[ZNIrrep{32}] constructor 1067 ns -> 137 ns
fuse, Z32, full 1329 ns -> 342 ns, 864 B/4 allocs -> 0
fuse, Z32, sparse 1088 ns -> 134 ns, 864 B/4 allocs -> 0
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* build tuple storage in a `MutableNTuple` with a bitmask
The scatter-write paths that build `NTuple{N,Int}` storage used two heap
buffers: a `zeros(Int, N)` accumulator and a `falses(N)` flag vector, both only
there because `Base.setindex` on a tuple in a loop is slow.
`TupleTools.MutableNTuple` replaces the accumulator: the optimizer elides the
mutable box in these call patterns, so nothing escapes to the heap. Tracking
which sectors have already been set needs one bit per sector, and tuple storage
is only used for `N <= _ntuple_storage_threshold`, so a `UInt64` mask does that
with no allocation at all.
`Base.setindex` was measured as the third option and ruled out: 2-3x behind at
N = 16 and 11x behind on the `fuse` accumulation pattern at N = 31.
Constructor, before -> after:
N = 4 121 ns, 240 B, 6 allocs -> 80 ns, 48 B, 1 alloc
N = 16 115 ns, 432 B, 6 allocs -> 86 ns, 144 B, 1 alloc
N = 32 137 ns, 560 B, 6 allocs -> 102 ns, 272 B, 1 alloc
`fuse` deliberately keeps its plain `Vector` accumulator: it does not escape
either, and `MutableNTuple` is ~9% slower there at N = 16 because the pointer
round-trip blocks register promotion of the accumulator.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* specialise `flip` to tuple storage
The generic `flip` rebuilds the space through `GradedSpace{I,NTuple{N,Int}}(dims)`,
paying a `findindex` per sector plus the whole constructor. For tuple storage the
result is just a fixed permutation of the dims with `isdual` negated, and that
permutation depends only on `I`, so `Base.@assume_effects :foldable` lets it fold
away entirely.
flip, Z32 1083 ns, 1280 B, 9 allocs -> 5.2 ns, 0 B, 0 allocs
flip, Z4 x Z8 1950 ns, 1280 B, 9 allocs -> 4.9 ns, 0 B, 0 allocs
Verified to agree with the generic method on both `isdual` branches for Z4, Z8,
Z32, a product sector, and empty spaces.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* make `ominus` a single pass
`⊖` validated with `V ≿ W` and only then subtracted the dimensions, so every
call traversed the sectors twice. `isepimorphic` also `collect`s
`blocksectors(W)` and does a `dim` lookup per sector, which is what made `⊖`
allocate on tuple storage where the subtraction itself is allocation-free.
Both methods now subtract and validate in one pass: `map` over the two tuples,
or one walk over the two sorted key vectors reusing `_mergestore!` so zero
results are dropped.
This also closes a latent trap. `_keepunmatched(-)` fell through to the `true`
default, so `mergewith(-, ...)` gave a sector present only in `W` a *positive*
dimension in the result; only the `≿` pre-check kept that unreachable. `⊖` was
the sole `mergewith(-, ...)` caller, and there are none left.
Rep[Z4] (tuple, N=4) 18.8 ns, 64 B, 2 allocs -> 2.1 ns, 0 B, 0 allocs
ZNIrrep{8} (tuple, N=8) 42.8 ns, 112 B, 3 allocs -> 2.4 ns, 0 B, 0 allocs
Rep[U1] (dict, 5 sect.) 76.3 ns, 384 B, 6 allocs -> 31.1 ns, 224 B, 5 allocs
Rep[SU2] (dict, 6 sect.) 59.3 ns, 432 B, 6 allocs -> 27.9 ns, 256 B, 5 allocs
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* replace `_builddensemap` by `sectormap` in dicts.jl
`_builddensemap(f, D, I, pairsiter, V)` took five arguments, two of which
(`sectorstoragetype(I)` and `I`) were derivable and were spelled out at all
fourteen call sites. It is really a `map` over `c => v` pairs into whichever
dense sector map matches the storage choice for `GradedSpace{I}`, so it belongs
next to the dict types rather than in the truncation code.
- `sectormaptype(I, V)` folds the two-step storage dispatch into one function,
keeping `_densemaptype` as the internal dispatch on the storage type.
- `sectormap(f, [V,] pairsiter)` derives `I` from `eltype(pairsiter)` and, when
`V` is omitted, the value type from `f` via `promote_op` - which is what four
call sites were computing by hand. Only the intersection/union sites still
pass `V` explicitly, to pin it to `Any`.
- The separate keys-only builder is gone; callers that need just one half of
the pair take a dummy argument. `pairs` normalises the intersection/union
inputs, whose components may be `SectorVector` masks rather than dicts.
`pairs(::SectorVector)` materialises a `SectorDict` that `sectormap` immediately
re-iterates, but making it lazy would return a `Base.Generator` with
`eltype == Any` and break the sector-type deduction at every call site, so it is
left alone.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* document why `fuse` accumulates into a `Dict`
Inserting straight into the sorted result was measured as the alternative. It
wins below a few hundred output sectors but is O(k^2) in them, and product
sectors reach that regime quickly: U1 x U1 with 196 input sectors fuses to 729
output sectors at 1.03 ms against 0.57 ms for the `Dict`, and the gap grows.
The `Dict` has no such cliff, so it stays.
Also drops a stray blank line left before `_dualpermutation`.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* address review: drop `FullVectorDict` and the `sectormap` paradigm
`FullVectorDict`/`sectormap` existed only to give the truncation code a dense
"sector => index" map for tuple-backed spaces. Their call sites all go through
`pairs(::SectorVector)`, which materialises a `SectorDict` anyway, so the dense
map bought little for a lot of `Sector`-specific machinery in `auxiliary/`.
Every truncation index map is a plain `SectorDict` again; instead
`pairs(::SectorVector)` is built directly from the already-sorted structure
rather than by repeated sorted insertion. The storage-specialised
`truncate_space` methods are kept, since they never used the dense map.
Also from the review:
- `⊖` for dict storage reuses `_sortedmerge`, whose `_keepunmatched` trait is
generalised into per-side `_unmatched1`/`_unmatched2` hooks
- `_ntuple_storage_threshold` -> `_NTUPLE_STORAGE_THRESHOLD`, lowered to 8
- `ZNSpace{N}` deprecated in favour of `Vect[ZNIrrep{N}]`, which the alias can
no longer track once `N` exceeds the threshold
- drop the over-strong `I == Trivial` assert in `truncate_space`, and name the
index variables `ind`/`inds` instead of shadowing the sector type `I`
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix `pairs(::SectorVector)` for non-`SubArray` views
`valtype(::SectorVector)` claims a `SubArray`, but `view` of a GPU array is
itself a `CuArray`/`ROCArray`, so pinning the element type to `valtype` broke
every GPU factorization. Take the element type from the views instead.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* review: storage-variant aliases, shared merge handlers, lazy `pairs`
- add `TupleGradedSpace{I,N}` / `DictGradedSpace{I}` aliases for the two
variants `sectorstoragetype` selects between, and use them for dispatch
- `SubtractDims` doubles as the unmatched handler for `⊖`, and is used for the
tuple variant as well, so both paths share one callable
- `_sortedmerge` takes the unmatched handlers as arguments; `mergewith` selects
them inline
- `pairs(::SectorVector)` is lazy, like `blocks(::AbstractTensorMap)`: every
consumer only iterates, and lookups go through the vector itself
- document `sectorstoragetype`, whose docstring interpolates the threshold
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* Apply batched suggestions from code review [skip ci]
Co-authored-by: Jutho <Jutho@users.noreply.github.com>
* review: drop StaticLength, simplify truncate_space, avoid re-sorting inds
Address Jutho's final review round on #511:
- replace `StaticLength(N)` with plain `N` (constant propagation makes
the old TupleTools artefact unnecessary now that N is already a type
parameter), and drop the now-dead `TupleTools: StaticLength` imports.
- rewrite `truncate_space(::TupleGradedSpace, inds)` as a pure `ntuple`
closure instead of mutating a `MutableNTuple`, guarding zero-dimension
sectors before indexing into `inds` (which only holds keys for sectors
with nonzero dimension).
- `truncate_space(::DictGradedSpace, inds)` no longer collects and sorts:
`inds` (whether a `SectorDict` or a `SectorVector`, depending on the
truncation strategy) already iterates in sorted order by sector.
- drop the now-unused `MutableNTuple`/`findindex` imports in the
factorizations submodule.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* test: fix pairs(::SectorVector) comparison against Dict
`pairs(::SectorVector)` is intentionally lazy (a `Base.Generator`), so
comparing it directly to a `Dict` with `==` silently evaluates to
`false` rather than erroring, since no `==` is defined between those
unrelated iterator types. Collect it before comparing.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
---------
Co-authored-by: lkdvos <ldevos98@gmail.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Jutho <Jutho@users.noreply.github.com>GradedSpace functions based on storage type (#511)1 parent c19f94c commit f3f660c
11 files changed
Lines changed: 348 additions & 145 deletions
File tree
- docs/src
- lib
- man
- src
- auxiliary
- factorizations
- spaces
- tensors
- test/tensors
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| 36 | + | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
40 | 46 | | |
41 | 47 | | |
42 | 48 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
47 | 52 | | |
48 | 53 | | |
49 | 54 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
| 34 | + | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
| 47 | + | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | | - | |
111 | 110 | | |
112 | 111 | | |
113 | 112 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | 92 | | |
102 | 93 | | |
103 | 94 | | |
104 | 95 | | |
105 | 96 | | |
106 | | - | |
| 97 | + | |
107 | 98 | | |
108 | 99 | | |
109 | 100 | | |
| |||
118 | 109 | | |
119 | 110 | | |
120 | 111 | | |
121 | | - | |
| 112 | + | |
122 | 113 | | |
123 | 114 | | |
124 | 115 | | |
125 | 116 | | |
126 | 117 | | |
127 | 118 | | |
128 | 119 | | |
129 | | - | |
| 120 | + | |
130 | 121 | | |
131 | 122 | | |
132 | 123 | | |
| |||
138 | 129 | | |
139 | 130 | | |
140 | 131 | | |
141 | | - | |
| 132 | + | |
142 | 133 | | |
143 | 134 | | |
144 | 135 | | |
| |||
153 | 144 | | |
154 | 145 | | |
155 | 146 | | |
156 | | - | |
| 147 | + | |
157 | 148 | | |
158 | 149 | | |
159 | 150 | | |
| |||
163 | 154 | | |
164 | 155 | | |
165 | 156 | | |
166 | | - | |
| 157 | + | |
167 | 158 | | |
168 | 159 | | |
169 | 160 | | |
| |||
186 | 177 | | |
187 | 178 | | |
188 | 179 | | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
189 | 246 | | |
190 | 247 | | |
191 | 248 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
36 | | - | |
| 35 | + | |
| 36 | + | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| |||
0 commit comments