Repository navigation
Conversation
On the CPU, `permute`, `braid`, `transpose` and `twist` call `tensoradd!` once per subblock (and `mul!` once per multi-tree block), whose call overhead dominates for tensors with many small subblocks. Blocks of a `TensorMap` with at most 1024 entries to compute are now transformed term by term with a plain loop over the flat data, addressed directly through the cached subblock structure. Nothing is added to the tree transformers. `taskforeach` skips the atomic counter for a single worker. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
| end | ||
|
|
||
| # computes `A = β * A + α * permutedims(op.(B), p)` for the strided arrays `A` and `B` within `a` and `b` | ||
| function _add_strided!(a, b, (sz, strides_dst, offset_dst), (_, strides_src, offset_src), p, op::F, α, β) where {F} |
There was a problem hiding this comment.
How does this compare to actually forming the StridedView and doing a broadcasting expression on that?
It might actually make sense to fix this "higher up", ie just make Strided.jl bypass all its optimizations/blocking/... when the number of entries is below 1024. The main question is a bit which part of the tensoradd! stack actually adds all the overhead, and whether or not we can get rid of it.
This is mostly what I was trying to do before, but definitely did not go into a lot of detail on that. (As a side comment, can you double-check you are comparing to the latest versions of Strided-TensorOperations, since some of the improvements did actually make it in)
There was a problem hiding this comment.
I checked, and this was all tested using the latest versions of everything at the time: Strided 2.6.7 (so with #87–#92 and #97), StridedViews 0.5.5, TensorOperations 5.8.4. StridedViews had a new release since, but that shouldn't affect the timings here.
For a 4-entry, 6-index ComplexF64 block, one tensoradd! call costs about 220 ns on my workstation, against about 20 ns for the loop. Forming the StridedView is ~1 ns, and broadcasting on it costs the same as tensoradd!, so it is not the TensorOperations wrapper (under 10% of the profile). About 60% of the samples are in _mapreduce_order! → order_and_fuse_dims (_fusedims, indexorder, the cost computation), and ~11% in the kernel itself.
I also tried out QuantumKitHub/Strided.jl#99, cherry-picked onto main, plus stridedtensoradd! calling the new permutedims!, so that TensorKit main picks it up unchanged. On top of that I added a plain loop nest for at most 32 elements that skips the sorting and fusing. permute! in ms on a shared, loaded machine (not the workstation, so only the ratios mean something; minimum over 8 interleaved runs):
| subblocks / largest | main | main + Strided#99 | main + Strided#99 + loop | this PR |
|---|---|---|---|---|
| 9632 / 16 (fℤ₂ ⊠ U1) | 5.65 | 3.48 | 1.62 | 0.92 |
| 40736 / 4 (fℤ₂ ⊠ U1) | 26.9 | 18.7 | 12.0 | 7.15 |
| 301 / 256 (U1) | 0.32 | 0.21 | 0.23 | 0.14 |
| 977 / 4 (fℤ₂ ⊠ SU2) | 1.14 | 0.65 | 0.59 | 0.18 |
| 977 / 256 (fℤ₂ ⊠ SU2) | 2.49 | 2.25 | 2.35 | 2.69 |
| 301 / 16384 (U1) | 17.5 | 17.2 | 18.1 | 17.4 |
So for Abelian symmetries with tiny blocks a Strided-side fix recovers most of it (75–85% of the time this PR saves), and it helps everything else that calls tensoradd! on small arrays. It doesn't cover the non-Abelian case, where the remaining time is the per-block recoupling and not the permutation.
One way forward would be to keep this PR for the small blocks, get the small path into Strided, and then lower the 1024 cutoff here to whatever the Strided path does not already handle well. Above ~256 entries the Strided kernel is already as fast as the loop, and faster for β ≠ 0 on complex data, so the cutoff here can probably come down quite a bit.
| # calls `f(i, j)` for the positions of all corresponding entries of two strided arrays of size `sz` | ||
| @inline _stridedloop(f, ::Tuple{}, ::Tuple{}, ::Tuple{}, i::Int, j::Int) = f(i, j) | ||
| @inline function _stridedloop(f, sz::Tuple, strides₁::Tuple, strides₂::Tuple, i::Int, j::Int) | ||
| for _ in 1:last(sz) | ||
| _stridedloop(f, Base.front(sz), Base.front(strides₁), Base.front(strides₂), i, j) | ||
| i += last(strides₁) | ||
| j += last(strides₂) | ||
| end | ||
| return nothing | ||
| end |
There was a problem hiding this comment.
This is actually quite a cool code pattern, I wonder if it could be used to replace some of the @generated functions in the Strided kernels. In particular, something I always wondered about is whether this could be used to reduce the compilation time a bit, for example by only inlining the first N layers and then actually using a function call. (This is definitely something Claude can play around with rather quickly/efficiently 😉)
Ultimately, I do think it reads a lot nicer than the actual generated functions so that would also be a bonus.
@Jutho tagging you here explicitly, since this might be of interest to you 😄
There was a problem hiding this comment.
Yes this looks very cool indeed.
As an aside, I hope to finally have a proper chunk of time for PR reviews the next few days.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
| dst::StridedSubblocks{<:CPUStorage}, src::StridedSubblocks{<:CPUStorage}, p, conjsrc::Bool, | ||
| U, inds_dst, inds_src, buffer, α, β, backend, allocator | ||
| ) | ||
| if length(U) * prod(first(@inbounds src.structure[first(inds_src)])) > SMALL_BLOCK_LENGTH |
There was a problem hiding this comment.
This is actually quite a good point, and somewhat independent of the strided kernel:
I think currently the recoupling step that uses the buffer only ever checks the size of U being 1x1, while arguably things like 2x2 still don't benefit from the BLAS call: every input has to be copied twice anyways, so the gain from the BLAS intermediate accessing memory more efficiently is completely not there. Ultimately, I think to get optimal performance it would be nice to get a custom kernel for the recoupling as a whole, which does something like QuasiStrided.jl but then for "lists of inputs" rather than just regular strided inputs. If someone has some tokens to burn and/or compute to do this it might still speed up things by quite a lot, also for the larger cases, but 🤷
…in _add_strided! Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Alternative to #597 that gets most of the speedup without caching anything in the tree transformers.
On the CPU,
permute,braid,transposeandtwistcalltensoradd!once per fusion-tree subblock, which costs about 0.3 µs per call independent of the subblock size. For tensors with many small subblocks, such as fermionic U(1)-symmetric PEPS tensors, this dominates:permute!becomes up to 100× slower than copying the data.#597 removes that overhead by collecting the small subblocks into groups with cached index lists. This PR instead adds a single method of
_add_transform_block!for theStridedSubblocksof CPUTensorMaps: blocks with at most 1024 entries to compute (length(U) * prod(sz)) are transformed term by term with a plain loop over the flat data, addressed through the(size, strides, offset)structure that the transformer already holds. Larger blocks take the same path as before. There is no new type, no new field in the transformers and no extra cached data, and the threaded and GPU paths are untouched.taskforeachalso skips its atomic counter for a single worker, which was a measurable part of the per-subblock cost.permute!into a preallocated destination, one thread, best of 30, in ms (p = ((1, 4, 2), (5, 3, 6))onVχ ⊗ VD ⊗ VD' ← Vχ ⊗ VD ⊗ VD'), the same cases as in #597, all three versions measured on the same machine:copy!Vχcharges ±2, deg. 4;VD±1, deg. 1Vχcharges ±8, deg. 2;VD±1, deg. 1Vχcharges ±2, deg. 16;VD±1, deg. 1Vχcharges ±2, deg. 32;VD±1, deg. 2Vχj ≤ 1, deg. 2;VDj ≤ 1/2, deg. 1Vχj ≤ 1, deg. 16;VDj ≤ 1/2, deg. 2Ranges are over two runs. Compared to #597:
mul!and unpack with onetensoradd!per subblock.The existing permutation tests only have small subblocks, so I added a testset with blocks on both sides of the threshold, which also covers nonzero
βand an adjoint source.Benchmark script
🤖 Generated with Claude Code