Skip to content

Transform the fusion tree pairs of a UniqueTreeTransformer on several tasks - #632

Open
leburgel wants to merge 1 commit into
mainfrom
lb/threaded_unique_transformers
Open

leburgel wants to merge 1 commit into
mainfrom
lb/threaded_unique_transformers

Conversation

@leburgel

@leburgel leburgel commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

GenericTreeTransformer already computes its recoupling blocks on get_num_manipulation_threads() tasks, but UniqueTreeTransformer transforms its fusion tree pairs in a serial loop. For abelian tensors with many sectors per leg that loop is most of the cost of building a transformer, so cold index manipulations do not speed up with the manipulation threads.

This splits the pairs into one equal range per task and transforms the ranges through taskforeach, on up to get_num_manipulation_threads() tasks as in the generic case. Every pair of an abelian transformer costs about the same, so equal ranges are balanced; every task gets at least 1024 pairs, so small transformers stay serial. Each task writes its own entries of data, the destination trees are only read, and the caches of the fusion tree manipulations are already safe to use from several tasks. With timers enabled the construction stays serial, as for GenericTreeTransformer. The transformer data is identical for any number of tasks.

Reproducer: builds the transformers of four braids of V ⊗ V ⊗ V ← V ⊗ V ⊗ V with V having one-dimensional FermionParity ⊠ U1Irrep sectors of charges -K:K, with the braid cache emptied first (cold) or kept (cached), and checks that every thread count gives the same data.

# Construction time of the tree transformers of abelian braids, with 1 and several manipulation threads.
# Run with `julia -t 8 reproducer.jl`; `K` sets the U1 charges -K:K of every leg.
using TensorKit, Printf
using TensorKit: MemoizationKit

K = parse(Int, get(ENV, "K", "3"))
V = Vect[FermionParity ⊠ U1Irrep](FermionParity(f) ⊠ U1Irrep(a) => 1 for f in 0:1 for a in -K:K)
W = V ⊗ V ⊗ V ← V ⊗ V ⊗ V
perms = [((1, 2, 4), (3, 5, 6)), ((6, 2, 4), (1, 5, 3)), ((2, 3, 5), (4, 6, 1)), ((1, 4, 5), (2, 3, 6))]
levels = (1, 2, 3, 4, 5, 6)

# the transformers are built without their cache; the fusion tree and subblock structures are cached
transformers() = [
    MemoizationKit.uncached(TensorKit.treebraider, Vector{Float64}, permute(W, p), W, p, false, levels)
        for p in perms
]

@printf("TensorKit %s, %d Julia threads, K = %d, %d fusion tree pairs\n",
    pkgversion(TensorKit), Threads.nthreads(), K, length(fusiontrees(W)))
reference = transformers() # compilation, and the structures of all spaces
println("threads  cold braids (s)  cached braids (s)")
for n in (1, 2, 4, 8)
    n > Threads.nthreads() && continue
    TensorKit.set_num_manipulation_threads(n)
    cold = minimum(1:3) do _
        MemoizationKit.empty_caches!(TensorKit.fusiontreebraid)
        return @elapsed transformers()
    end
    cached = minimum(_ -> @elapsed(transformers()), 1:3)
    @assert all(((t, r),) -> t.data == r.data, zip(transformers(), reference))
    @printf("%7d  %15.3f  %17.3f\n", n, cold, cached)
end

julia -t 8, 8 cores of one machine, OpenBLAS 1 thread, minimum of 3 runs, times in seconds:

K (pairs) threads main, cold main, cached this PR, cold this PR, cached
2 (56 032) 1 0.116 0.115 0.113 0.105
2 0.115 0.116 0.061 0.059
4 0.116 0.113 0.030 0.034
8 0.113 0.124 0.021 0.020
3 (298 592) 1 0.699 0.890 0.827 0.870
2 0.852 0.765 0.428 0.422
4 0.750 0.774 0.213 0.242
8 0.771 0.694 0.108 0.107

The machine was shared, so the serial times vary by about ±0.1 s between runs. These times were measured with fixed chunks of 1024 pairs handed out one at a time instead of one range per task; the two gave the same times within this noise when compared on one machine.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/tensors/treetransformers.jl 97.56% <100.00%> (+0.70%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/tensors/treetransformers.jl Outdated
data[isrc] = (convert(T, coeff), idst, isrc)
# the pairs are transformed in chunks, such that every task has enough work
nthreads = get_num_manipulation_threads()
chunks = Iterators.partition(eachindex(data), UNIQUE_TRANSFORM_CHUNKSIZE)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is looping over everything 1024 elements at a time, but for abelian symmetries it's probably a fair assumption that all of the work is more or less the same, so a static scheduler that just splits in nthreads equal chunks has even less overhead and should otherwise also be load-balanced.

(With of course a cutoff for not spawning tasks for too few entries).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched to the static split you suggested: the pairs are now split into one equal range per task, and the number of tasks is capped such that every task gets at least 1024 pairs, so small transformers stay serial.

There's no measurable performance difference with the chunks of 1024, but this is strictly simpler.

… tasks

The pairs are split into one equal range per task, on up to
get_num_manipulation_threads() tasks with taskforeach, as the fusion tree
blocks of a GenericTreeTransformer already are. Every task gets at least
1024 pairs, so small transformers stay serial.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leburgel
leburgel force-pushed the lb/threaded_unique_transformers branch from 658808c to ee29572 Compare November 23, 2026 08:10

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants