Repository navigation
Transform small blocks by a plain loop in the CPU add_transform! #626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | |||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -769,6 +769,76 @@ function _add_transform_block!( | ||||||||||||||||||||||||||||||||||||
| return nothing | |||||||||||||||||||||||||||||||||||||
| end | |||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||
| # blocks with at most this many entries to compute are transformed by plain loops | |||||||||||||||||||||||||||||||||||||
| const SMALL_BLOCK_LENGTH = 1024 | |||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||
| # On the CPU, the call overhead of `tensoradd!` and `mul!` dominates for small blocks of a `TensorMap`, | |||||||||||||||||||||||||||||||||||||
| # which are therefore transformed by a plain loop over the flat data for every term of `U`. | |||||||||||||||||||||||||||||||||||||
| function _add_transform_block!( | |||||||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||||||
| return @invoke _add_transform_block!( | |||||||||||||||||||||||||||||||||||||
| dst::TransformSubblocks, src::TransformSubblocks, p, conjsrc, U, inds_dst, inds_src, buffer, | |||||||||||||||||||||||||||||||||||||
| α, β, backend, allocator | |||||||||||||||||||||||||||||||||||||
| ) | |||||||||||||||||||||||||||||||||||||
| end | |||||||||||||||||||||||||||||||||||||
| op = conjsrc ? conj : identity | |||||||||||||||||||||||||||||||||||||
| @inbounds for (i, idst) in enumerate(inds_dst), (j, isrc) in enumerate(inds_src) | |||||||||||||||||||||||||||||||||||||
| j == 1 || !iszero(U[i, j]) || continue # the first term also applies `β` | |||||||||||||||||||||||||||||||||||||
| _add_strided!( | |||||||||||||||||||||||||||||||||||||
| dst.data, src.data, dst.structure[idst], src.structure[isrc], linearize(p), | |||||||||||||||||||||||||||||||||||||
| op, α * U[i, j], j == 1 ? β : One() | |||||||||||||||||||||||||||||||||||||
| ) | |||||||||||||||||||||||||||||||||||||
| end | |||||||||||||||||||||||||||||||||||||
| return nothing | |||||||||||||||||||||||||||||||||||||
| 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} | |||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this compare to actually forming the 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)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 I also tried out QuantumKitHub/Strided.jl#99, cherry-picked onto main, plus
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 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 |
|||||||||||||||||||||||||||||||||||||
| strides_src′ = TupleTools.getindices(strides_src, p) | |||||||||||||||||||||||||||||||||||||
| # loop over dimensions of size 1 outermost, so that the innermost loop is a nontrivial one | |||||||||||||||||||||||||||||||||||||
| sz, strides_dst, strides_src′ = _permutedims3(_singletons_last(sz), sz, strides_dst, strides_src′) | |||||||||||||||||||||||||||||||||||||
| # fuse dimensions along which both arrays are contiguous, and move the size-1 dimensions this leaves outward | |||||||||||||||||||||||||||||||||||||
| sz = _fusedims(sz, strides_dst, strides_src′) | |||||||||||||||||||||||||||||||||||||
| sz, strides_dst, strides_src′ = _permutedims3(_singletons_last(sz), sz, strides_dst, strides_src′) | |||||||||||||||||||||||||||||||||||||
| if iszero(β) | |||||||||||||||||||||||||||||||||||||
| _stridedloop(sz, strides_dst, strides_src′, offset_dst + 1, offset_src + 1) do i, j | |||||||||||||||||||||||||||||||||||||
| @inbounds a[i] = α * op(b[j]) | |||||||||||||||||||||||||||||||||||||
| end | |||||||||||||||||||||||||||||||||||||
| else | |||||||||||||||||||||||||||||||||||||
| _stridedloop(sz, strides_dst, strides_src′, offset_dst + 1, offset_src + 1) do i, j | |||||||||||||||||||||||||||||||||||||
| @inbounds a[i] = α * op(b[j]) + β * a[i] | |||||||||||||||||||||||||||||||||||||
| end | |||||||||||||||||||||||||||||||||||||
| end | |||||||||||||||||||||||||||||||||||||
| return nothing | |||||||||||||||||||||||||||||||||||||
| end | |||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||
| _permutedims3(q, ts...) = map(t -> TupleTools.getindices(t, q), ts) | |||||||||||||||||||||||||||||||||||||
| _singletons_last(sz::Dims{N}) where {N} = TupleTools.sortperm(ntuple(k -> k + N * (sz[k] == 1), Val(N))) | |||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||
| # merges dimension `k + 1` into `k` wherever both arrays are contiguous across them, leaving size 1 at `k + 1` | |||||||||||||||||||||||||||||||||||||
| function _fusedims(sz::Dims{N}, s₁::Dims{N}, s₂::Dims{N}) where {N} | |||||||||||||||||||||||||||||||||||||
| for k in (N - 1):-1:1 | |||||||||||||||||||||||||||||||||||||
| if s₁[k + 1] == sz[k] * s₁[k] && s₂[k + 1] == sz[k] * s₂[k] | |||||||||||||||||||||||||||||||||||||
| sz = Base.setindex(Base.setindex(sz, sz[k] * sz[k + 1], k), 1, k + 1) | |||||||||||||||||||||||||||||||||||||
| end | |||||||||||||||||||||||||||||||||||||
| end | |||||||||||||||||||||||||||||||||||||
| return sz | |||||||||||||||||||||||||||||||||||||
| end | |||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||
| # 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 | |||||||||||||||||||||||||||||||||||||
|
Comment on lines
+831
to
+840
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually quite a cool code pattern, I wonder if it could be used to replace some of the 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 😄
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes this looks very cool indeed. |
|||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||
| # computes `buffer_dst = buffer_src * transpose(U)`, where both are column-major views into `buffer` | |||||||||||||||||||||||||||||||||||||
| _recouple!(buffer, buffer_dst, buffer_src, U) = | |||||||||||||||||||||||||||||||||||||
| (mul!(buffer_dst, buffer_src, transpose(StridedView(U))); nothing) | |||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
Ubeing 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 🤷