Skip to content

Infer precompile workloads only for concrete types - #620

Merged
lkdvos merged 4 commits into
QuantumKitHub:mainfrom
hz-xiaxz:precompile-inference-barrier
Nov 13, 2026
Merged

lkdvos merged 4 commits into
QuantumKitHub:mainfrom
hz-xiaxz:precompile-inference-barrier

Conversation

@hz-xiaxz

@hz-xiaxz hz-xiaxz commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The precompile workloads loop over eltypes (a Vector{DataType}) and N in 1:ndims. Inside the loop body the element type and the arity are therefore unknown to inference, so the whole contraction / index-manipulation / factorization machinery is inferred twice: once for the abstract signatures (e.g. TensorMap{T, S, N₁, N₂} where {T, S, N₁, N₂}) when the workload function itself is compiled, and once more for the concrete types reached by dynamic dispatch at runtime. Only the concrete instances are ever used by real code, but the abstract ones are also serialized into the pkgimage.

For the U1Irrep/Float64 contract workload, 28175 of 39015 inference frames (about 45% of the inference time) had non-concrete signatures. Tracing every Strided.mul! frame up the inference tree, the first ancestor with a concrete signature was precompile_contract itself.

Change

  • The body for each element type (and arity) now runs behind Base.inferencebarrier, so inference only ever sees concrete argument types.
  • The product spaces are built with N in the type domain (_prodspace(V, Val(N))). V^n with a runtime n has an uninferable result type, so with only the barrier the tensors in the workload were still inferred as TensorMap{Float64, S, _A, _B}.

Only the workload code changes; the public precompile_* functions keep their signatures. No library code is touched, so runtime performance is unaffected.

Measurements

Full default build (5 sectors × Float64/ComplexF64, all three workloads). Julia 1.13.3, TensorOperations 5.8.3, single-threaded, AMD EPYC 7742 node on a shared cluster. One run each, and wall times on these nodes vary by up to ~20% between identical runs, so the build-time number is indicative only; the pkgimage size is deterministic.

main this PR
Base.compilecache(TensorKit) wall 881 s 845 s
peak RSS during build 6.92 GB 6.48 GB
TensorKit pkgimage (.so) 322.7 MB 263.8 MB (−18%)
using TensorKit in a fresh session (two runs) 4.8 / 4.6 s 3.1 / 3.0 s

Inference per workload family (U1Irrep, Float64, workload run in a session where the TensorKit workload is disabled; frame counts are deterministic):

workload frames (main) frames (this PR) abstract-signature frames
contract 39015 10271 28175 → 1491
indexmanipulations 20635 9573 11698 → 1681
factorizations 22264 11769 9583 → 1400

Cache coverage. Against both full builds, a fresh session calls a function that does an MPS-style transfer-matrix contraction with @tensor (U1Irrep/ComplexF64 and SU2Irrep/Float64) plus an svd_compact. This was measured without any instrumentation, using --trace-compile-timing and @timed, two runs each:

main this PR
first call 0.35 / 0.34 s 0.33 / 0.32 s
statements compiled at runtime (whole script) 164 160
total time of those compilations 2.76 s 2.66 s

So the concrete instances that real code needs are still cached.

Still investigating

About 1.5k abstract-signature frames remain per workload family. These come from type instabilities inside the library itself, not from the workloads, and we are still looking into them; we will follow up separately.

🤖 Generated with Claude Code

hz-xiaxz and others added 2 commits November 10, 2026 09:31
The precompile workloads loop over `eltypes` (a `Vector{DataType}`) and `1:ndims`, so
inside the loop body the element type and arity are unknown to inference. As a result the
entire contraction / index-manipulation / factorization machinery was also inferred, and
serialized into the pkgimage, for abstract signatures that no actual call uses.

Run the body for each element type and arity behind `Base.inferencebarrier`, and build the
product spaces with `N` in the type domain (`V^n` with a runtime `n` has an uninferable
result type), so that inference only sees concrete argument types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@lkdvos lkdvos left a comment

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.

Let me start by saying that I really appreciate the help on looking into this. Compilation and precompilation times have long been an issue that I just didn't have the bandwidth to do much on, so any help is greatly appreciated. I am unfortunately switching jobs myself so reviews might take a little longer, but I'm sure we will get there!

Comment thread src/precompile/precompile.jl Resolved
Comment thread src/precompile/indexmanipulations.jl Resolved
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hz-xiaxz

Copy link
Copy Markdown
Contributor Author

@lkdvos Thanks, and good luck with the new job!

A broader thought: I suspect much of the compile time here can't be fixed on the package side and needs improvements in the compiler itself, much like the upstream work did more for CairoMakie than anything Makie did on its own. I opened QuantumKitHub/MPSKit.jl#553 about submitting a TTFX snippet to perf.julialang.org. Do you think that example is suitable to submit, or is there a more representative workload? I'd rather submit one example that captures typical usage than several that split upstream's attention, and it would also mean fewer package-side workarounds to maintain.

@lkdvos
lkdvos merged commit 4815929 into QuantumKitHub:main Nov 13, 2026
29 of 33 checks passed
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