Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 47 additions & 35 deletions src/precompile/contract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,59 @@ See also [`precompile_indexmanipulations`](@ref TensorKit.precompile_indexmanipu
"""
function precompile_contract(::Type{S}; eltypes = PRECOMPILE_ELTYPES, ndims = PRECOMPILE_NDIMS) where {S <: IndexSpace}
V = unitspace(S)
backend = TO.DefaultBackend()
allocator = TO.DefaultAllocator()
symmetric_braiding = BraidingStyle(sectortype(S)) isa SymmetricBraiding
for T in eltypes, N in 1:ndims
Base.inferencebarrier(_precompile_contract)(V, T, Val(N), symmetric_braiding)
end
for T in eltypes
α, β = rand(T), rand(T)

# contraction + permutation for each requested arity (leg count); `Val(N)`/`ntuple`
# keep the index tuples concrete so the machinery specializes per arity
for N in 1:ndims
W = V^(N - 1) # N-1 legs
# contraction of two arity-N tensors over their N-1 shared legs -> arity-2 result
A = randn(T, V ← W)
B = randn(T, W ← V)
pA = ((1,), ntuple(i -> i + 1, Val(N - 1)))
pB = (ntuple(identity, Val(N - 1)), (N,))
pAB = ((1,), (2,))
C = TO.tensoralloc_contract(T, A, pA, false, B, pB, false, pAB, Val(false))
Base.inferencebarrier(_precompile_contract_conj_trace)(V, T, symmetric_braiding)
end
return nothing
end

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

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

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

# partial trace (the two traced legs are mutually dual)
At = randn(T, V ⊗ V' ← V)
Ct = TO.tensoralloc_add(T, At, ((3,), ()), false, Val(false))
planartrace!(Ct, At, ((3,), ()), ((1,), (2,)), α, β, backend, allocator)
symmetric_braiding && TO.tensortrace!(
Ct, At, ((3,), ()), ((1,), (2,)), false, α, β, backend, allocator
)
if symmetric_braiding
TO.tensorcontract!(C, A, pA, false, B, pB, false, pAB, α, β, backend, allocator)
TO.tensorcontract!(C, A, pA, false, B, pB, false, pAB, One(), Zero(), backend, allocator)
end
return nothing
end

function _precompile_contract_conj_trace(V, ::Type{T}, symmetric_braiding) where {T}
backend = TO.DefaultBackend()
allocator = TO.DefaultAllocator()
α, β = rand(T), rand(T)

# the conjugated-operand branch (`conjA=true`) is a distinct runtime path (adjoint
# handling) that `conj(A) * B` networks hit; `@plansor` picks the planar or non-planar
# implementation depending on the braiding style, so this compiles for any sectortype
A2 = randn(T, V ← V)
B2 = randn(T, V ← V)
@plansor Cc[a; c] := conj(A2[b; a]) * B2[b; c]

# partial trace (the two traced legs are mutually dual)
At = randn(T, V ⊗ V' ← V)
Ct = TO.tensoralloc_add(T, At, ((3,), ()), false, Val(false))
planartrace!(Ct, At, ((3,), ()), ((1,), (2,)), α, β, backend, allocator)
symmetric_braiding && TO.tensortrace!(
Ct, At, ((3,), ()), ((1,), (2,)), false, α, β, backend, allocator
)
return nothing
end
123 changes: 64 additions & 59 deletions src/precompile/factorizations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,71 +19,76 @@ See also [`precompile_contract`](@ref TensorKit.precompile_contract), [`precompi
function precompile_factorizations(::Type{S}; eltypes = PRECOMPILE_ELTYPES) where {S <: IndexSpace}
V = unitspace(S)
for T in eltypes
# Three representative shapes: a square tensor for the bulk of the decompositions, a
# hermitian one for the `eigh` path, and rectangular ones so the null-space kernels
# operate on non-empty blocks.
W = V^2 # V ⊗ V
t = randn(T, W ← W) # square
tr = randn(T, W ← V) # tall (codomain larger) -> non-empty left null space
tw = randn(T, V ← W) # wide (domain larger) -> non-empty right null space
Base.inferencebarrier(_precompile_factorizations)(V, T)
end
return nothing
end

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

# exponential
exponential(th)
# projectors
th = project_hermitian(t) # hermitian (square, Euclidean inner product)
project_antihermitian(t)
project_isometric(t)

# Singular value decomposition
svd_full(t)
svd_compact(t)
svd_vals(t)
svd_trunc(t; trunc = truncrank(1))
svd_trunc(t; trunc = trunctol(; atol = 1.0e-10))
svd_trunc(t; trunc = truncerror(; atol = 1.0e-10))
svd_trunc(t; trunc = notrunc())
svd_trunc(t; trunc = truncspace(space(t, 1)))
svd_trunc(t; trunc = truncrank(1) | trunctol(; atol = 1.0e-10))
svd_trunc(t; trunc = truncrank(1) & trunctol(; atol = 1.0e-10))
# exponential
exponential(th)

# QR / LQ decompositions (null-space variants on the appropriately shaped tensors)
qr_full(t)
qr_compact(t)
qr_null(tr)
lq_full(t)
lq_compact(t)
lq_null(tw)
# Singular value decomposition
svd_full(t)
svd_compact(t)
svd_vals(t)
svd_trunc(t; trunc = truncrank(1))
svd_trunc(t; trunc = trunctol(; atol = 1.0e-10))
svd_trunc(t; trunc = truncerror(; atol = 1.0e-10))
svd_trunc(t; trunc = notrunc())
svd_trunc(t; trunc = truncspace(space(t, 1)))
svd_trunc(t; trunc = truncrank(1) | trunctol(; atol = 1.0e-10))
svd_trunc(t; trunc = truncrank(1) & trunctol(; atol = 1.0e-10))

# Eigenvalue decompositions (hermitian variants require a hermitian input)
eig_full(t)
eig_vals(t)
eig_trunc(t; trunc = truncrank(1))
eig_trunc(t; trunc = trunctol(; atol = 1.0e-10))
eig_trunc(t; trunc = truncerror(; atol = 1.0e-10))
eig_trunc(t; trunc = notrunc())
eig_trunc(t; trunc = truncspace(space(t, 1)))
eig_trunc(t; trunc = truncrank(1) | trunctol(; atol = 1.0e-10))
eig_trunc(t; trunc = truncrank(1) & trunctol(; atol = 1.0e-10))
eigh_full(th)
eigh_vals(th)
eigh_trunc(th; trunc = truncrank(1))
eigh_trunc(th; trunc = trunctol(; atol = 1.0e-10))
eigh_trunc(th; trunc = truncerror(; atol = 1.0e-10))
eigh_trunc(th; trunc = notrunc())
eigh_trunc(th; trunc = truncspace(space(th, 1)))
eigh_trunc(th; trunc = truncrank(1) | trunctol(; atol = 1.0e-10))
eigh_trunc(th; trunc = truncrank(1) & trunctol(; atol = 1.0e-10))
# QR / LQ decompositions (null-space variants on the appropriately shaped tensors)
qr_full(t)
qr_compact(t)
qr_null(tr)
lq_full(t)
lq_compact(t)
lq_null(tw)

# Orthogonal / null-space helpers
left_orth(t)
right_orth(t)
left_null(tr)
right_null(tw)
# Eigenvalue decompositions (hermitian variants require a hermitian input)
eig_full(t)
eig_vals(t)
eig_trunc(t; trunc = truncrank(1))
eig_trunc(t; trunc = trunctol(; atol = 1.0e-10))
eig_trunc(t; trunc = truncerror(; atol = 1.0e-10))
eig_trunc(t; trunc = notrunc())
eig_trunc(t; trunc = truncspace(space(t, 1)))
eig_trunc(t; trunc = truncrank(1) | trunctol(; atol = 1.0e-10))
eig_trunc(t; trunc = truncrank(1) & trunctol(; atol = 1.0e-10))
eigh_full(th)
eigh_vals(th)
eigh_trunc(th; trunc = truncrank(1))
eigh_trunc(th; trunc = trunctol(; atol = 1.0e-10))
eigh_trunc(th; trunc = truncerror(; atol = 1.0e-10))
eigh_trunc(th; trunc = notrunc())
eigh_trunc(th; trunc = truncspace(space(th, 1)))
eigh_trunc(th; trunc = truncrank(1) | trunctol(; atol = 1.0e-10))
eigh_trunc(th; trunc = truncrank(1) & trunctol(; atol = 1.0e-10))

# Polar decompositions
left_polar(t)
right_polar(t)
end
# Orthogonal / null-space helpers
left_orth(t)
right_orth(t)
left_null(tr)
right_null(tw)

# Polar decompositions
left_polar(t)
right_polar(t)
return nothing
end
66 changes: 34 additions & 32 deletions src/precompile/indexmanipulations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,40 @@ function precompile_indexmanipulations(::Type{S}; eltypes = PRECOMPILE_ELTYPES,
V = unitspace(S)
symmetric_braiding = BraidingStyle(sectortype(S)) isa SymmetricBraiding
has_braiding = !(BraidingStyle(sectortype(S)) isa NoBraiding)
for T in eltypes
# `Val(N)`/`ntuple` keep the index tuples concrete so the machinery specializes per arity
for N in 1:ndims
N₁ = cld(N, 2) # codomain legs
# a tensor split non-trivially into codomain/domain (N₁ | N-N₁)
t = randn(T, V^N₁ ← V^(N - N₁))

# a non-trivial reordering of all N legs (reverse of `1:N`), split back into (N₁ | N-N₁)
perm = ntuple(i -> N - i + 1, Val(N))
p1 = ntuple(i -> perm[i], Val(N₁))
p2 = ntuple(i -> perm[i + N₁], Val(N - N₁))

# `permute` and `braid` funnel through `add_transform!` with different transformers
if symmetric_braiding
permute(t, (p1, p2))
permute(t', (p1, p2))
elseif has_braiding
braid(t, (p1, p2), ntuple(identity, Val(N))) # `levels` is a tuple over the source indices
braid(t', (p1, p2), ntuple(identity, Val(N))) # `levels` is a tuple over the source indices
end

# canonical transpose `(reverse(domain), reverse(codomain))` is always a valid cyclic transpose
tp1 = ntuple(i -> N - i + 1, Val(N - N₁))
tp2 = ntuple(i -> N₁ - i + 1, Val(N₁))
transpose(t, (tp1, tp2))

# repartition to a different codomain size, forcing the repartition/transpose path
repartition(t, N₁ < N ? N₁ + 1 : N₁ - 1)

twist(t, 1)
twist(t', 1)
end
for T in eltypes, N in 1:ndims
Base.inferencebarrier(_precompile_indexmanipulations)(V, T, Val(N), symmetric_braiding, has_braiding)
end
return nothing
end

function _precompile_indexmanipulations(V, ::Type{T}, ::Val{N}, symmetric_braiding, has_braiding) where {T, N}
N₁ = cld(N, 2) # codomain legs
# a tensor split non-trivially into codomain/domain (N₁ | N-N₁)
t = randn(T, _prodspace(V, Val(N₁)) ← _prodspace(V, Val(N - N₁)))

# a non-trivial reordering of all N legs (reverse of `1:N`), split back into (N₁ | N-N₁)
perm = ntuple(i -> N - i + 1, Val(N))
p1 = ntuple(i -> perm[i], Val(N₁))
p2 = ntuple(i -> perm[i + N₁], Val(N - N₁))

# `permute` and `braid` funnel through `add_transform!` with different transformers
if symmetric_braiding
permute(t, (p1, p2))
permute(t', (p1, p2))
elseif has_braiding
braid(t, (p1, p2), ntuple(identity, Val(N))) # `levels` is a tuple over the source indices
braid(t', (p1, p2), ntuple(identity, Val(N))) # `levels` is a tuple over the source indices
end

# canonical transpose `(reverse(domain), reverse(codomain))` is always a valid cyclic transpose
tp1 = ntuple(i -> N - i + 1, Val(N - N₁))
tp2 = ntuple(i -> N₁ - i + 1, Val(N₁))
transpose(t, (tp1, tp2))

# repartition to a different codomain size, forcing the repartition/transpose path
repartition(t, N₁ < N ? N₁ + 1 : N₁ - 1)

twist(t, 1)
twist(t', 1)
return nothing
end
4 changes: 4 additions & 0 deletions src/precompile/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ const PRECOMPILE_FACTORIZATIONS = @load_preference("precompile_factorizations",

# Workload
# --------
# `V^N` with `N` known in the type domain: unlike `V^n` for an `Int`, the resulting space type
# (and therefore the type of tensors built from it) can be inferred
_prodspace(V, ::Val{N}) where {N} = TensorKit.ProductSpace{typeof(V), N}(ntuple(Returns(V), Val(N)))
Comment thread
lkdvos marked this conversation as resolved.
Resolved

include("indexmanipulations.jl")
include("contract.jl")
include("factorizations.jl")
Expand Down
Loading