Repository navigation
Infer precompile workloads only for concrete types - #620
Conversation
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
left a comment
There was a problem hiding this comment.
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!
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@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. |
Problem
The precompile workloads loop over
eltypes(aVector{DataType}) andN 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/Float64contract workload, 28175 of 39015 inference frames (about 45% of the inference time) had non-concrete signatures. Tracing everyStrided.mul!frame up the inference tree, the first ancestor with a concrete signature wasprecompile_contractitself.Change
Base.inferencebarrier, so inference only ever sees concrete argument types.Nin the type domain (_prodspace(V, Val(N))).V^nwith a runtimenhas an uninferable result type, so with only the barrier the tensors in the workload were still inferred asTensorMap{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.Base.compilecache(TensorKit)wallTensorKitpkgimage (.so)using TensorKitin a fresh session (two runs)Inference per workload family (
U1Irrep,Float64, workload run in a session where the TensorKit workload is disabled; frame counts are deterministic):Cache coverage. Against both full builds, a fresh session calls a function that does an MPS-style transfer-matrix contraction with
@tensor(U1Irrep/ComplexF64andSU2Irrep/Float64) plus ansvd_compact. This was measured without any instrumentation, using--trace-compile-timingand@timed, two runs each: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