Skip to content

Commit 4815929

Browse files
authored
Infer precompile workloads only for concrete types (#620)
1 parent 3f69cc9 commit 4815929

4 files changed

Lines changed: 149 additions & 126 deletions

File tree

‎src/precompile/contract.jl‎

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,59 @@ See also [`precompile_indexmanipulations`](@ref TensorKit.precompile_indexmanipu
1818
"""
1919
function precompile_contract(::Type{S}; eltypes = PRECOMPILE_ELTYPES, ndims = PRECOMPILE_NDIMS) where {S <: IndexSpace}
2020
V = unitspace(S)
21-
backend = TO.DefaultBackend()
22-
allocator = TO.DefaultAllocator()
2321
symmetric_braiding = BraidingStyle(sectortype(S)) isa SymmetricBraiding
22+
for T in eltypes, N in 1:ndims
23+
Base.inferencebarrier(_precompile_contract)(V, T, Val(N), symmetric_braiding)
24+
end
2425
for T in eltypes
25-
α, β = rand(T), rand(T)
26-
27-
# contraction + permutation for each requested arity (leg count); `Val(N)`/`ntuple`
28-
# keep the index tuples concrete so the machinery specializes per arity
29-
for N in 1:ndims
30-
W = V^(N - 1) # N-1 legs
31-
# contraction of two arity-N tensors over their N-1 shared legs -> arity-2 result
32-
A = randn(T, V ← W)
33-
B = randn(T, W ← V)
34-
pA = ((1,), ntuple(i -> i + 1, Val(N - 1)))
35-
pB = (ntuple(identity, Val(N - 1)), (N,))
36-
pAB = ((1,), (2,))
37-
C = TO.tensoralloc_contract(T, A, pA, false, B, pB, false, pAB, Val(false))
26+
Base.inferencebarrier(_precompile_contract_conj_trace)(V, T, symmetric_braiding)
27+
end
28+
return nothing
29+
end
3830

39-
planarcontract!(C, A, pA, B, pB, pAB, α, β, backend, allocator)
40-
planarcontract!(C, A, pA, B, pB, pAB, One(), Zero(), backend, allocator)
31+
# contraction + permutation for a single arity (leg count) `N`
32+
function _precompile_contract(V, ::Type{T}, ::Val{N}, symmetric_braiding) where {T, N}
33+
backend = TO.DefaultBackend()
34+
allocator = TO.DefaultAllocator()
35+
α, β = rand(T), rand(T)
4136

42-
if symmetric_braiding
43-
TO.tensorcontract!(C, A, pA, false, B, pB, false, pAB, α, β, backend, allocator)
44-
TO.tensorcontract!(C, A, pA, false, B, pB, false, pAB, One(), Zero(), backend, allocator)
45-
end
46-
end
37+
W = _prodspace(V, Val(N - 1)) # N-1 legs
38+
# contraction of two arity-N tensors over their N-1 shared legs -> arity-2 result
39+
A = randn(T, V ← W)
40+
B = randn(T, W ← V)
41+
pA = ((1,), ntuple(i -> i + 1, Val(N - 1)))
42+
pB = (ntuple(identity, Val(N - 1)), (N,))
43+
pAB = ((1,), (2,))
44+
C = TO.tensoralloc_contract(T, A, pA, false, B, pB, false, pAB, Val(false))
4745

48-
# the conjugated-operand branch (`conjA=true`) is a distinct runtime path (adjoint
49-
# handling) that `conj(A) * B` networks hit; `@plansor` picks the planar or non-planar
50-
# implementation depending on the braiding style, so this compiles for any sectortype
51-
A2 = randn(T, V ← V)
52-
B2 = randn(T, V ← V)
53-
@plansor Cc[a; c] := conj(A2[b; a]) * B2[b; c]
46+
planarcontract!(C, A, pA, B, pB, pAB, α, β, backend, allocator)
47+
planarcontract!(C, A, pA, B, pB, pAB, One(), Zero(), backend, allocator)
5448

55-
# partial trace (the two traced legs are mutually dual)
56-
At = randn(T, V ⊗ V' ← V)
57-
Ct = TO.tensoralloc_add(T, At, ((3,), ()), false, Val(false))
58-
planartrace!(Ct, At, ((3,), ()), ((1,), (2,)), α, β, backend, allocator)
59-
symmetric_braiding && TO.tensortrace!(
60-
Ct, At, ((3,), ()), ((1,), (2,)), false, α, β, backend, allocator
61-
)
49+
if symmetric_braiding
50+
TO.tensorcontract!(C, A, pA, false, B, pB, false, pAB, α, β, backend, allocator)
51+
TO.tensorcontract!(C, A, pA, false, B, pB, false, pAB, One(), Zero(), backend, allocator)
6252
end
6353
return nothing
6454
end
55+
56+
function _precompile_contract_conj_trace(V, ::Type{T}, symmetric_braiding) where {T}
57+
backend = TO.DefaultBackend()
58+
allocator = TO.DefaultAllocator()
59+
α, β = rand(T), rand(T)
60+
61+
# the conjugated-operand branch (`conjA=true`) is a distinct runtime path (adjoint
62+
# handling) that `conj(A) * B` networks hit; `@plansor` picks the planar or non-planar
63+
# implementation depending on the braiding style, so this compiles for any sectortype
64+
A2 = randn(T, V ← V)
65+
B2 = randn(T, V ← V)
66+
@plansor Cc[a; c] := conj(A2[b; a]) * B2[b; c]
67+
68+
# partial trace (the two traced legs are mutually dual)
69+
At = randn(T, V ⊗ V' ← V)
70+
Ct = TO.tensoralloc_add(T, At, ((3,), ()), false, Val(false))
71+
planartrace!(Ct, At, ((3,), ()), ((1,), (2,)), α, β, backend, allocator)
72+
symmetric_braiding && TO.tensortrace!(
73+
Ct, At, ((3,), ()), ((1,), (2,)), false, α, β, backend, allocator
74+
)
75+
return nothing
76+
end

‎src/precompile/factorizations.jl‎

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,71 +19,76 @@ See also [`precompile_contract`](@ref TensorKit.precompile_contract), [`precompi
1919
function precompile_factorizations(::Type{S}; eltypes = PRECOMPILE_ELTYPES) where {S <: IndexSpace}
2020
V = unitspace(S)
2121
for T in eltypes
22-
# Three representative shapes: a square tensor for the bulk of the decompositions, a
23-
# hermitian one for the `eigh` path, and rectangular ones so the null-space kernels
24-
# operate on non-empty blocks.
25-
W = V^2 # V ⊗ V
26-
t = randn(T, W ← W) # square
27-
tr = randn(T, W ← V) # tall (codomain larger) -> non-empty left null space
28-
tw = randn(T, V ← W) # wide (domain larger) -> non-empty right null space
22+
Base.inferencebarrier(_precompile_factorizations)(V, T)
23+
end
24+
return nothing
25+
end
2926

30-
# projectors
31-
th = project_hermitian(t) # hermitian (square, Euclidean inner product)
32-
project_antihermitian(t)
33-
project_isometric(t)
27+
function _precompile_factorizations(V, ::Type{T}) where {T}
28+
# Three representative shapes: a square tensor for the bulk of the decompositions, a
29+
# hermitian one for the `eigh` path, and rectangular ones so the null-space kernels
30+
# operate on non-empty blocks.
31+
W = V^2 # V ⊗ V
32+
t = randn(T, W ← W) # square
33+
tr = randn(T, W ← V) # tall (codomain larger) -> non-empty left null space
34+
tw = randn(T, V ← W) # wide (domain larger) -> non-empty right null space
3435

35-
# exponential
36-
exponential(th)
36+
# projectors
37+
th = project_hermitian(t) # hermitian (square, Euclidean inner product)
38+
project_antihermitian(t)
39+
project_isometric(t)
3740

38-
# Singular value decomposition
39-
svd_full(t)
40-
svd_compact(t)
41-
svd_vals(t)
42-
svd_trunc(t; trunc = truncrank(1))
43-
svd_trunc(t; trunc = trunctol(; atol = 1.0e-10))
44-
svd_trunc(t; trunc = truncerror(; atol = 1.0e-10))
45-
svd_trunc(t; trunc = notrunc())
46-
svd_trunc(t; trunc = truncspace(space(t, 1)))
47-
svd_trunc(t; trunc = truncrank(1) | trunctol(; atol = 1.0e-10))
48-
svd_trunc(t; trunc = truncrank(1) & trunctol(; atol = 1.0e-10))
41+
# exponential
42+
exponential(th)
4943

50-
# QR / LQ decompositions (null-space variants on the appropriately shaped tensors)
51-
qr_full(t)
52-
qr_compact(t)
53-
qr_null(tr)
54-
lq_full(t)
55-
lq_compact(t)
56-
lq_null(tw)
44+
# Singular value decomposition
45+
svd_full(t)
46+
svd_compact(t)
47+
svd_vals(t)
48+
svd_trunc(t; trunc = truncrank(1))
49+
svd_trunc(t; trunc = trunctol(; atol = 1.0e-10))
50+
svd_trunc(t; trunc = truncerror(; atol = 1.0e-10))
51+
svd_trunc(t; trunc = notrunc())
52+
svd_trunc(t; trunc = truncspace(space(t, 1)))
53+
svd_trunc(t; trunc = truncrank(1) | trunctol(; atol = 1.0e-10))
54+
svd_trunc(t; trunc = truncrank(1) & trunctol(; atol = 1.0e-10))
5755

58-
# Eigenvalue decompositions (hermitian variants require a hermitian input)
59-
eig_full(t)
60-
eig_vals(t)
61-
eig_trunc(t; trunc = truncrank(1))
62-
eig_trunc(t; trunc = trunctol(; atol = 1.0e-10))
63-
eig_trunc(t; trunc = truncerror(; atol = 1.0e-10))
64-
eig_trunc(t; trunc = notrunc())
65-
eig_trunc(t; trunc = truncspace(space(t, 1)))
66-
eig_trunc(t; trunc = truncrank(1) | trunctol(; atol = 1.0e-10))
67-
eig_trunc(t; trunc = truncrank(1) & trunctol(; atol = 1.0e-10))
68-
eigh_full(th)
69-
eigh_vals(th)
70-
eigh_trunc(th; trunc = truncrank(1))
71-
eigh_trunc(th; trunc = trunctol(; atol = 1.0e-10))
72-
eigh_trunc(th; trunc = truncerror(; atol = 1.0e-10))
73-
eigh_trunc(th; trunc = notrunc())
74-
eigh_trunc(th; trunc = truncspace(space(th, 1)))
75-
eigh_trunc(th; trunc = truncrank(1) | trunctol(; atol = 1.0e-10))
76-
eigh_trunc(th; trunc = truncrank(1) & trunctol(; atol = 1.0e-10))
56+
# QR / LQ decompositions (null-space variants on the appropriately shaped tensors)
57+
qr_full(t)
58+
qr_compact(t)
59+
qr_null(tr)
60+
lq_full(t)
61+
lq_compact(t)
62+
lq_null(tw)
7763

78-
# Orthogonal / null-space helpers
79-
left_orth(t)
80-
right_orth(t)
81-
left_null(tr)
82-
right_null(tw)
64+
# Eigenvalue decompositions (hermitian variants require a hermitian input)
65+
eig_full(t)
66+
eig_vals(t)
67+
eig_trunc(t; trunc = truncrank(1))
68+
eig_trunc(t; trunc = trunctol(; atol = 1.0e-10))
69+
eig_trunc(t; trunc = truncerror(; atol = 1.0e-10))
70+
eig_trunc(t; trunc = notrunc())
71+
eig_trunc(t; trunc = truncspace(space(t, 1)))
72+
eig_trunc(t; trunc = truncrank(1) | trunctol(; atol = 1.0e-10))
73+
eig_trunc(t; trunc = truncrank(1) & trunctol(; atol = 1.0e-10))
74+
eigh_full(th)
75+
eigh_vals(th)
76+
eigh_trunc(th; trunc = truncrank(1))
77+
eigh_trunc(th; trunc = trunctol(; atol = 1.0e-10))
78+
eigh_trunc(th; trunc = truncerror(; atol = 1.0e-10))
79+
eigh_trunc(th; trunc = notrunc())
80+
eigh_trunc(th; trunc = truncspace(space(th, 1)))
81+
eigh_trunc(th; trunc = truncrank(1) | trunctol(; atol = 1.0e-10))
82+
eigh_trunc(th; trunc = truncrank(1) & trunctol(; atol = 1.0e-10))
8383

84-
# Polar decompositions
85-
left_polar(t)
86-
right_polar(t)
87-
end
84+
# Orthogonal / null-space helpers
85+
left_orth(t)
86+
right_orth(t)
87+
left_null(tr)
88+
right_null(tw)
89+
90+
# Polar decompositions
91+
left_polar(t)
92+
right_polar(t)
8893
return nothing
8994
end

‎src/precompile/indexmanipulations.jl‎

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,40 @@ function precompile_indexmanipulations(::Type{S}; eltypes = PRECOMPILE_ELTYPES,
1919
V = unitspace(S)
2020
symmetric_braiding = BraidingStyle(sectortype(S)) isa SymmetricBraiding
2121
has_braiding = !(BraidingStyle(sectortype(S)) isa NoBraiding)
22-
for T in eltypes
23-
# `Val(N)`/`ntuple` keep the index tuples concrete so the machinery specializes per arity
24-
for N in 1:ndims
25-
N₁ = cld(N, 2) # codomain legs
26-
# a tensor split non-trivially into codomain/domain (N₁ | N-N₁)
27-
t = randn(T, V^N₁ ← V^(N - N₁))
28-
29-
# a non-trivial reordering of all N legs (reverse of `1:N`), split back into (N₁ | N-N₁)
30-
perm = ntuple(i -> N - i + 1, Val(N))
31-
p1 = ntuple(i -> perm[i], Val(N₁))
32-
p2 = ntuple(i -> perm[i + N₁], Val(N - N₁))
33-
34-
# `permute` and `braid` funnel through `add_transform!` with different transformers
35-
if symmetric_braiding
36-
permute(t, (p1, p2))
37-
permute(t', (p1, p2))
38-
elseif has_braiding
39-
braid(t, (p1, p2), ntuple(identity, Val(N))) # `levels` is a tuple over the source indices
40-
braid(t', (p1, p2), ntuple(identity, Val(N))) # `levels` is a tuple over the source indices
41-
end
42-
43-
# canonical transpose `(reverse(domain), reverse(codomain))` is always a valid cyclic transpose
44-
tp1 = ntuple(i -> N - i + 1, Val(N - N₁))
45-
tp2 = ntuple(i -> N₁ - i + 1, Val(N₁))
46-
transpose(t, (tp1, tp2))
47-
48-
# repartition to a different codomain size, forcing the repartition/transpose path
49-
repartition(t, N₁ < N ? N₁ + 1 : N₁ - 1)
50-
51-
twist(t, 1)
52-
twist(t', 1)
53-
end
22+
for T in eltypes, N in 1:ndims
23+
Base.inferencebarrier(_precompile_indexmanipulations)(V, T, Val(N), symmetric_braiding, has_braiding)
5424
end
5525
return nothing
5626
end
27+
28+
function _precompile_indexmanipulations(V, ::Type{T}, ::Val{N}, symmetric_braiding, has_braiding) where {T, N}
29+
N₁ = cld(N, 2) # codomain legs
30+
# a tensor split non-trivially into codomain/domain (N₁ | N-N₁)
31+
t = randn(T, _prodspace(V, Val(N₁)) ← _prodspace(V, Val(N - N₁)))
32+
33+
# a non-trivial reordering of all N legs (reverse of `1:N`), split back into (N₁ | N-N₁)
34+
perm = ntuple(i -> N - i + 1, Val(N))
35+
p1 = ntuple(i -> perm[i], Val(N₁))
36+
p2 = ntuple(i -> perm[i + N₁], Val(N - N₁))
37+
38+
# `permute` and `braid` funnel through `add_transform!` with different transformers
39+
if symmetric_braiding
40+
permute(t, (p1, p2))
41+
permute(t', (p1, p2))
42+
elseif has_braiding
43+
braid(t, (p1, p2), ntuple(identity, Val(N))) # `levels` is a tuple over the source indices
44+
braid(t', (p1, p2), ntuple(identity, Val(N))) # `levels` is a tuple over the source indices
45+
end
46+
47+
# canonical transpose `(reverse(domain), reverse(codomain))` is always a valid cyclic transpose
48+
tp1 = ntuple(i -> N - i + 1, Val(N - N₁))
49+
tp2 = ntuple(i -> N₁ - i + 1, Val(N₁))
50+
transpose(t, (tp1, tp2))
51+
52+
# repartition to a different codomain size, forcing the repartition/transpose path
53+
repartition(t, N₁ < N ? N₁ + 1 : N₁ - 1)
54+
55+
twist(t, 1)
56+
twist(t', 1)
57+
return nothing
58+
end

‎src/precompile/precompile.jl‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ const PRECOMPILE_FACTORIZATIONS = @load_preference("precompile_factorizations",
5252

5353
# Workload
5454
# --------
55+
# `V^N` with `N` known in the type domain: unlike `V^n` for an `Int`, the resulting space type
56+
# (and therefore the type of tensors built from it) can be inferred
57+
_prodspace(V, ::Val{N}) where {N} = TensorKit.ProductSpace{typeof(V), N}(ntuple(Returns(V), Val(N)))
58+
5559
include("indexmanipulations.jl")
5660
include("contract.jl")
5761
include("factorizations.jl")

0 commit comments

Comments
 (0)