Skip to content

Commit 8fd0f8f

Browse files
authored
Re-enable precompilation (#286)
1 parent 94e79f0 commit 8fd0f8f

10 files changed

Lines changed: 424 additions & 216 deletions

File tree

‎docs/src/man/precompilation.md‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@ faster precompile times for fast TTFX for a wider range of inputs.
1717

1818
## Defaults
1919

20-
By default, precompilation is disabled, but can be enabled for "tensors" of type `Array{T,N}`, where `T` and `N` range over the following values:
20+
By default, precompilation is enabled for "tensors" of type `Array{T,N}`, where `T` and `N` range over the following values:
2121

2222
* `T` is either `Float64` or `ComplexF64`
2323
* `tensoradd!` is precompiled up to `N = 5`
2424
* `tensortrace!` is precompiled up to `4` free output indices and `2` pairs of traced indices
2525
* `tensorcontract!` is precompiled up to `3` free output indices on both inputs, and `2` contracted indices
2626

27-
To enable precompilation with these default settings, you can *locally* change the `"precompile_workload"` key in the preferences.
27+
To disable precompilation altogether, for example during development or when you prefer to have small binaries,
28+
you can *locally* change the `"precompile_workload"` key in the preferences.
2829

2930
```julia
3031
using TensorOperations, Preferences
31-
set_preferences!(TensorOperations, "precompile_workload" => true; force=true)
32+
set_preferences!(TensorOperations, "precompile_workload" => false; force=true)
3233
```
3334

3435
## Custom settings
@@ -43,12 +44,31 @@ set_preferences!(TensorOperations, "setting" => value; force=true)
4344

4445
Here **setting** and **value** can take on the following:
4546

46-
* `"precomple_eltypes"`: a `Vector{String}` that evaluate to the desired values of `T<:Number`
47+
* `"precompile_eltypes"`: a `Vector{String}` that evaluate to the desired values of `T<:Number`
4748
* `"precompile_add_ndims"`: an `Int` to specify the maximum `N` for `tensoradd!`
4849
* `"precompile_trace_ndims"`: a `Vector{Int}` of length 2 to specify the maximal number of free and traced indices for `tensortrace!`.
4950
* `"precompile_contract_ndims"`: a `Vector{Int}` of length 2 to specify the maximal number of free and contracted indices for `tensorcontract!`.
5051

51-
!!! note "Backends"
52+
## Reuse in downstream packages
5253

53-
Currently, there is no support for precompiling methods that do not use the default backend. If this is a
54-
feature you would find useful, feel free to contact us or open an issue.
54+
The default workload is factored into three reusable functions, one per operation family, which
55+
a downstream package can call from its own `PrecompileTools.@compile_workload` to precompile for a
56+
different backend, allocator, or array type:
57+
58+
* `TensorOperations.precompile_tensoradd(T, N, backend, allocator)`
59+
* `TensorOperations.precompile_tensortrace(T, (N1, N2), backend, allocator)`
60+
* `TensorOperations.precompile_tensorcontract(T, (N1, N2, N3), backend, allocator)`
61+
62+
Each takes a single scalar type `T`, a single index-rank specification, and (optionally) a
63+
`backend` and `allocator` selecting the implementation to precompile for. The tensors are built
64+
by `TensorOperations.precompile_maketensor(T, N)`, which returns a `Array{T,N}`. For example, to
65+
precompile the `tensoradd!` path for a custom backend and allocator:
66+
67+
```julia
68+
using PrecompileTools, TensorOperations
69+
@compile_workload begin
70+
for T in (Float64, ComplexF64), N in 0:3
71+
TensorOperations.precompile_tensoradd(T, N, MyBackend(), MyAllocator())
72+
end
73+
end
74+
```

‎src/implementation/abstractarray.jl‎

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,23 @@ _stridedordiag(A::Diagonal) = A
6363
6464
Check that `C` has `numind(pC)` indices and that `pC` constitutes a valid permutation.
6565
"""
66-
function argcheck_index2tuple(C::AbstractArray, pC::Index2Tuple)
67-
return ndims(C) == numind(pC) && isperm(linearize(pC)) ||
66+
argcheck_index2tuple(C::AbstractArray, pC::Index2Tuple) = argcheck_indextuple(C, linearize(pC))
67+
function argcheck_indextuple(C::AbstractArray, pC::IndexTuple)
68+
ndims(C) == numind(pC) && isperm(pC) ||
6869
throw(IndexError(lazy"invalid permutation of length $(ndims(C)): $pC"))
70+
return nothing
6971
end
7072

7173
"""
7274
argcheck_tensoradd(C::AbstractArray, A::AbstractArray, pA::Index2Tuple)
7375
7476
Check that `C` and `A` have `numind(pA)` indices and that `pA` constitutes a valid permutation.
7577
"""
76-
function argcheck_tensoradd(C::AbstractArray, A::AbstractArray, pA::Index2Tuple)
78+
argcheck_tensoradd(C::AbstractArray, A::AbstractArray, pA::Index2Tuple) =
79+
argcheck_tensoradd(C, A, linearize(pA))
80+
function argcheck_tensoradd(C::AbstractArray, A::AbstractArray, pA::IndexTuple)
7781
ndims(C) == ndims(A) || throw(IndexError("non-matching number of dimensions"))
78-
argcheck_index2tuple(A, pA)
82+
argcheck_indextuple(A, pA)
7983
return nothing
8084
end
8185

@@ -85,14 +89,16 @@ end
8589
Check that the partial trace of `A` over indices `q` and with permutation of the remaining
8690
indices `p` is compatible with output `C`.
8791
"""
92+
argcheck_tensortrace(C::AbstractArray, A::AbstractArray, p::Index2Tuple, q::Index2Tuple) =
93+
argcheck_tensortrace(C, A, linearize(p), q)
8894
function argcheck_tensortrace(
89-
C::AbstractArray, A::AbstractArray, p::Index2Tuple, q::Index2Tuple
95+
C::AbstractArray, A::AbstractArray, p::IndexTuple, q::Index2Tuple
9096
)
9197
ndims(C) == numind(p) ||
9298
throw(IndexError(lazy"invalid selection of length $(ndims(C)): $p"))
9399
2 * numin(q) == 2 * numout(q) == ndims(A) - ndims(C) ||
94100
throw(IndexError("invalid number of trace dimensions"))
95-
argcheck_index2tuple(A, ((p[1]..., q[1]...), (p[2]..., q[2]...)))
101+
argcheck_indextuple(A, (p..., q[1]..., q[2]...))
96102
return nothing
97103
end
98104

@@ -108,7 +114,15 @@ function argcheck_tensorcontract(
108114
B::AbstractArray, pB::Index2Tuple,
109115
pAB::Index2Tuple
110116
)
111-
argcheck_index2tuple(C, pAB)
117+
return argcheck_tensorcontract(C, A, pA, B, pB, linearize(pAB))
118+
end
119+
function argcheck_tensorcontract(
120+
C::AbstractArray,
121+
A::AbstractArray, pA::Index2Tuple,
122+
B::AbstractArray, pB::Index2Tuple,
123+
pAB::IndexTuple
124+
)
125+
argcheck_indextuple(C, pAB)
112126
argcheck_index2tuple(A, pA)
113127
argcheck_index2tuple(B, pB)
114128
numout(pA) + numin(pB) == ndims(C) ||
@@ -123,27 +137,28 @@ end
123137
124138
Check that `C` and `A` have compatible sizes for the addition specified by `pA`.
125139
"""
126-
function dimcheck_tensoradd(C::AbstractArray, A::AbstractArray, pA::Index2Tuple)
140+
dimcheck_tensoradd(C::AbstractArray, A::AbstractArray, pA::Index2Tuple) = dimcheck_tensoradd(C, A, linearize(pA))
141+
function dimcheck_tensoradd(C::AbstractArray, A::AbstractArray, pA::IndexTuple)
127142
szA, szC = size(A), size(C)
128-
TupleTools.getindices(szA, linearize(pA)) == szC ||
143+
TupleTools.getindices(szA, pA) == szC ||
129144
throw(DimensionMismatch("non-matching sizes in uncontracted dimensions"))
130145
return nothing
131146
end
132147

133148
"""
134-
dimcheck_tensorcontract(C::AbstractArray, A::AbstractArray,
135-
p::Index2Tuple, q::Index2Tuple)
149+
dimcheck_tensorcontract(C::AbstractArray, A::AbstractArray, p::Index2Tuple, q::Index2Tuple)
136150
137-
Check that `C` and `A` have compatible sizes for the trace and addition specified by `p` and
138-
`q`.
151+
Check that `C` and `A` have compatible sizes for the trace and addition specified by `p` and `q`.
139152
"""
153+
dimcheck_tensortrace(C::AbstractArray, A::AbstractArray, p::Index2Tuple, q::Index2Tuple) =
154+
dimcheck_tensortrace(C, A, linearize(p), q)
140155
function dimcheck_tensortrace(
141-
C::AbstractArray, A::AbstractArray, p::Index2Tuple, q::Index2Tuple
156+
C::AbstractArray, A::AbstractArray, p::IndexTuple, q::Index2Tuple
142157
)
143158
szA, szC = size(A), size(C)
144159
TupleTools.getindices(szA, q[1]) == TupleTools.getindices(szA, q[2]) ||
145160
throw(DimensionMismatch("non-matching sizes in traced dimensions"))
146-
TupleTools.getindices(szA, linearize(p)) == szC ||
161+
TupleTools.getindices(szA, p) == szC ||
147162
throw(DimensionMismatch("non-matching sizes in uncontracted dimensions"))
148163
return nothing
149164
end
@@ -163,11 +178,19 @@ function dimcheck_tensorcontract(
163178
B::AbstractArray, pB::Index2Tuple,
164179
pAB::Index2Tuple
165180
)
181+
return dimcheck_tensorcontract(C, A, pA, B, pB, linearize(pAB))
182+
end
183+
function dimcheck_tensorcontract(
184+
C::AbstractArray,
185+
A::AbstractArray, pA::Index2Tuple,
186+
B::AbstractArray, pB::Index2Tuple,
187+
pAB::IndexTuple
188+
)
166189
szA, szB, szC = size(A), size(B), size(C)
167190
TupleTools.getindices(szA, pA[2]) == TupleTools.getindices(szB, pB[1]) ||
168191
throw(DimensionMismatch("non-matching sizes in contracted dimensions"))
169192
szAB = (TupleTools.getindices(szA, pA[1])..., TupleTools.getindices(szB, pB[2])...)
170-
TupleTools.getindices(szAB, linearize(pAB)) == szC ||
193+
TupleTools.getindices(szAB, pAB) == szC ||
171194
throw(DimensionMismatch("non-matching sizes in uncontracted dimensions"))
172195
return nothing
173196
end

‎src/implementation/blascontract.jl‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
function blas_contract!(C, A, pA, B, pB, pAB, α, β, backend, allocator)
77
rpA = reverse(pA)
88
rpB = reverse(pB)
9-
indCinoBA = let N₁ = numout(pA), N₂ = numin(pB)
10-
map(n -> ifelse(n > N₁, n - N₁, n + N₂), linearize(pAB))
9+
# note: `pAB` is only ever consumed through `linearize`/`invperm` from here on, so it is
10+
# canonicalized to an `IndexTuple` and the reversed permutation does not need to be
11+
# repartitioned the way `pAB` is
12+
pAB = linearize(pAB)
13+
rpAB = let N₁ = numout(pA), N₂ = numin(pB)
14+
map(n -> ifelse(n > N₁, n - N₁, n + N₂), pAB)
1115
end
12-
tpAB = trivialpermutation(pAB)
13-
rpAB = (
14-
TupleTools.getindices(indCinoBA, tpAB[1]),
15-
TupleTools.getindices(indCinoBA, tpAB[2]),
16-
)
1716
cp = allocator_checkpoint!(allocator)
1817
if contract_memcost(C, A, pA, B, pB, pAB) <= contract_memcost(C, B, rpB, A, rpA, rpAB)
1918
C = _blas_contract!(C, A, pA, B, pB, pAB, α, β, backend, allocator)
@@ -28,15 +27,15 @@ function blas_contract!(
2827
C::StridedView{T, 2},
2928
A::StridedView{T, 2}, pA::Index2Tuple{1, 1},
3029
B::StridedView{T, 2}, pB::Index2Tuple{1, 1},
31-
pAB::Index2Tuple{1, 1},
30+
pAB::IndexTuple{2},
3231
α::Number, β::Number,
3332
backend, allocator
3433
) where {T}
3534
A′ = pA == ((1,), (2,)) ? A : transpose(A)
3635
B′ = pB == ((1,), (2,)) ? B : transpose(B)
37-
if pAB == ((1,), (2,))
36+
if pAB == (1, 2)
3837
mul!(C, A′, B′, α, β)
39-
elseif pAB == ((2,), (1,))
38+
elseif pAB == (2, 1)
4039
mul!(C, transpose(B′), transpose(A′), α, β)
4140
end
4241
return C
@@ -49,18 +48,23 @@ function _blas_contract!(C, A, pA, B, pB, pAB, α, β, backend, allocator)
4948
A_, pA, flagA = makeblascontractable(A, pA, TC, backend, allocator)
5049
B_, pB, flagB = makeblascontractable(B, pB, TC, backend, allocator)
5150

51+
# the partition of `ipAB` is required by `isblasdestination` and `tensoralloc_add`, but
52+
# the actual contraction only needs the linearized permutation
5253
ipAB = oindABinC(pAB, pA, pB)
54+
ipAB′ = linearize(ipAB)
5355
flagC = isblasdestination(C, ipAB)
5456
if flagC
5557
C_ = C
56-
_unsafe_blas_contract!(C_, A_, pA, B_, pB, ipAB, α, β)
58+
_unsafe_blas_contract!(C_, A_, pA, B_, pB, ipAB′, α, β)
5759
else
5860
C_ = SV(tensoralloc_add(TC, C, ipAB, false, Val(true), allocator))
5961
_unsafe_blas_contract!(
60-
C_, A_, pA, B_, pB, trivialpermutation(ipAB),
62+
C_, A_, pA, B_, pB, trivialpermutation(ipAB′),
6163
one(TC), zero(TC)
6264
)
63-
tensoradd!(C, C_, pAB, false, α, β, backend, allocator)
65+
# `C` already exists, so only the permutation of `pAB` matters here and it can be
66+
# handed over in the canonical `{N,0}` partition
67+
tensoradd!(C, C_, (pAB, ()), false, α, β, backend, allocator)
6468
tensorfree!(C_.parent, allocator)
6569
end
6670
flagA || tensorfree!(A_.parent, allocator)
@@ -74,7 +78,7 @@ function _unsafe_blas_contract!(
7478
C::StridedView{T},
7579
A::StridedView{T}, pA,
7680
B::StridedView{T}, pB,
77-
pAB, α, β
81+
pAB::IndexTuple, α, β
7882
) where {T <: BlasFloat}
7983
sizeA = size(A)
8084
sizeB = size(B)
@@ -84,7 +88,7 @@ function _unsafe_blas_contract!(
8488
osizeB = TupleTools.getindices(sizeB, pB[2])
8589

8690
mul!(
87-
sreshape(permutedims(C, linearize(pAB)), (prod(osizeA), prod(osizeB))),
91+
sreshape(permutedims(C, pAB), (prod(osizeA), prod(osizeB))),
8892
sreshape(permutedims(A, linearize(pA)), (prod(osizeA), prod(csizeA))),
8993
sreshape(permutedims(B, linearize(pB)), (prod(csizeB), prod(osizeB))),
9094
α, β

0 commit comments

Comments
 (0)