Skip to content

Commit 4d0d7f7

Browse files
authored
Make precompilation opt-in (#213)
1 parent 71191a6 commit 4d0d7f7

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

docs/src/man/precompilation.md

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

1818
## Defaults
1919

20-
By default, precompilation is enabled for "tensors" of type `Array{T,N}`, where `T` and `N` range over the following values:
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:
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-
## Custom settings
27+
To enable precompilation with these default settings, you can *locally* change the `"precompile_workload"` key in the preferences.
28+
29+
```julia
30+
using TensorOperations, Preferences
31+
set_preferences!(TensorOperations, "precompile_workload" => true; force=true)
32+
```
2833

29-
The default precompilation settings can be tweaked to allow for more or less expansive coverage. This is achieved
30-
through a combination of `PrecompileTools`- and `Preferences`-based functionality.
34+
## Custom settings
3135

32-
To disable precompilation altogether, for example during development or when you prefer to have small binaries,
33-
you can *locally* change the `"precompile_workload"` key in the preferences.
36+
The default precompilation settings can be tweaked to allow for more or less expansive coverage.
37+
This is achieved through a combination of `PrecompileTools`- and `Preferences`-based functionality.
3438

3539
```julia
3640
using TensorOperations, Preferences
37-
set_preferences!(TensorOperations, "precompile_workload" => false; force=true)
41+
set_preferences!(TensorOperations, "setting" => value; force=true)
3842
```
3943

40-
Alternatively, you can keep precompilation enabled, change the settings above through the same machinery, via:
44+
Here **setting** and **value** can take on the following:
4145

4246
* `"precomple_eltypes"`: a `Vector{String}` that evaluate to the desired values of `T<:Number`
4347
* `"precompile_add_ndims"`: an `Int` to specify the maximum `N` for `tensoradd!`

src/TensorOperations.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ const costcache = LRU{Any,Any}(; maxsize=10^5)
7575
using PackageExtensionCompat
7676
function __init__()
7777
@require_extensions
78+
79+
@info """
80+
TensorOperations can optionally be instructed to precompile several functions, which can be used to reduce the time to first execution (TTFX).
81+
This is disabled by default as this can take a while on some machines, and is only relevant for contraction-heavy workloads.
82+
83+
To enable or disable precompilation, you can use the following script:
84+
85+
```julia
86+
using TensorOperations, Preferences
87+
set_preferences!(TensorOperations, "precompile_workload" => true; force=true)
88+
```
89+
90+
This will create a `LocalPreferences.toml` file next to your current `Project.toml` file to store this setting in a persistent way.
91+
""" maxlog = 1
7892
end
7993

8094
include("precompile.jl")

src/precompile.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using PrecompileTools: PrecompileTools
2-
using Preferences: @load_preference
2+
using Preferences: @load_preference, load_preference
33

44
# Validate preferences input
55
# --------------------------
@@ -47,11 +47,24 @@ const PRECOMPILE_TRACE_NDIMS = validate_trace_ndims(@load_preference("precompile
4747
const PRECOMPILE_CONTRACT_NDIMS = validate_contract_ndims(@load_preference("precompile_contract_ndims",
4848
[4, 2]))
4949

50+
# Copy from PrecompileTools.workload_enabled but default to false
51+
function workload_enabled(mod::Module=@__MODULE__)
52+
try
53+
if load_preference(PrecompileTools, "precompile_workloads", true)
54+
return load_preference(mod, "precompile_workload", false)
55+
else
56+
return false
57+
end
58+
catch
59+
false
60+
end
61+
end
62+
5063
# Using explicit precompile statements here instead of @compile_workload:
5164
# Actually running the precompilation through PrecompileTools leads to longer compile times
5265
# Keeping the workload_enabled functionality to have the option of disabling precompilation
5366
# in a compatible manner with the rest of the ecosystem
54-
if PrecompileTools.workload_enabled(@__MODULE__)
67+
if workload_enabled()
5568
# tensoradd!
5669
# ----------
5770
for T in PRECOMPILE_ELTYPES

0 commit comments

Comments
 (0)