Skip to content

Commit 81ac203

Browse files
committed
feat: preferences + docs
1 parent 5805367 commit 81ac203

File tree

5 files changed

+82
-17
lines changed

5 files changed

+82
-17
lines changed

docs/src/.vitepress/config.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export default defineConfig({
8989
{text: "Local build", link: "/tutorials/local-build"},
9090
{text: "Control Flow", link: "/tutorials/control-flow"},
9191
{text: "Sharding", link: "/tutorials/sharding"},
92+
{text: "Persistent Compilation Cache", link: "/tutorials/persistent_compile_cache"},
9293
],
9394
},
9495
{
@@ -160,6 +161,7 @@ export default defineConfig({
160161
{ text: "Local build", link: "/tutorials/local-build" },
161162
{ text: "Control Flow", link: "/tutorials/control-flow" },
162163
{ text: "Sharding", link: "/tutorials/sharding" },
164+
{ text: "Persistent Compilation Cache", link: "/tutorials/persistent_compile_cache" },
163165
],
164166
}
165167
],

docs/src/tutorials/index.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Tutorials
22

3-
- [Profiling](@ref profiling).
4-
- [Multi-Host Environments](@ref distributed).
5-
- [Local build of ReactantExtra](@ref local-build).
6-
- [Control flow](@ref control-flow).
3+
- [Profiling](@ref profiling).
4+
- [Multi-Host Environments](@ref distributed).
5+
- [Local build of ReactantExtra](@ref local-build).
6+
- [Control flow](@ref control-flow).
7+
- [Sharding](@ref sharding).
8+
- [Persistent Compilation Cache](@ref persistent_compile_cache).
79

810
We are currently working on adding more tutorials to Reactant!! Please check back soon!
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [Persistent Compilation Cache](@id persistent_compile_cache)
2+
3+
Reactant.jl supports a persistent compilation cache that caches compiled and autotuned
4+
kernels on disk. We use [XLA's persisted autotuning](https://openxla.org/xla/persisted_autotuning)
5+
for this purpose. By default, the autotuning cache is enabled.
6+
7+
## Preferences
8+
9+
- `persistent_cache_enabled`: Whether to enable the persistent compilation cache. Defaults
10+
to `false`.
11+
- `persistent_cache_directory`: The base directory to use for the persistent compilation
12+
cache. Note that it is recommended to not set this preference, as Reactant will create
13+
a unique directory corresponding to XLA and Reactant_jll's version. If the user sets
14+
this preference, it is the user's responsibility to ensure that the directory exists
15+
and is writable and needs to be segregated based on XLA and Reactant_jll's version.
16+
Defaults to `""`.
17+
- `persistent_kernel_cache_enabled`: Whether to enable the kernel cache. Defaults to `false`.
18+
- `persistent_autotune_cache_enabled`: Whether to enable the autotuning cache. Defaults to
19+
`true`.
20+
21+
## Clearing the cache
22+
23+
To clear the cache, you can reuse `Scratch.jl`'s `clear_scclear_scratchspaces!` function:
24+
25+
```julia
26+
using Scratch, Reactant
27+
Scratch.clear_scratchspaces!(Reactant)
28+
```

src/PersistentCompileCache.jl

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,45 @@
11
module PersistentCompileCache
22

3+
using ..Reactant: Reactant
4+
5+
using Preferences: load_preference
36
using Scratch: @get_scratch!
47
using Reactant_jll: Reactant_jll
58

69
const CACHE_DIR = Ref{Union{Nothing,String}}(nothing)
710
const KERNEL_CACHE_ENABLED = Ref(false)
811
const AUTOTUNE_CACHE_ENABLED = Ref(false)
912

10-
# TODO: preference to disable persistent caching
11-
# TODO: preference to control what is being cached.
12-
# TODO: preference for cache dir
13-
# TODO: clear cache function
14-
1513
function __init__()
16-
# We version our cache directory based on Reactant_jll version (technically we need to
17-
# version according to XLA, but this is a good enough proxy)
18-
version = pkgversion(Reactant_jll)
19-
CACHE_DIR[] = @get_scratch!(
20-
"xla_persistent_cache_$(version.major)_$(version.minor)_$(version.patch)"
21-
)
22-
KERNEL_CACHE_ENABLED[] = true
23-
AUTOTUNE_CACHE_ENABLED[] = true
14+
persistent_cache_enabled = load_preference(Reactant, "persistent_cache_enabled", true)
15+
persistent_cache_directory = load_preference(Reactant, "persistent_cache_directory", "")
16+
17+
if persistent_cache_enabled
18+
if isempty(persistent_cache_directory)
19+
# We version our cache directory based on Reactant_jll version (technically we
20+
# need to version according to XLA, but this is a good enough proxy)
21+
version = pkgversion(Reactant_jll)
22+
CACHE_DIR[] = @get_scratch!(
23+
"xla_persistent_cache_$(version.major)_$(version.minor)_$(version.patch)"
24+
)
25+
else
26+
CACHE_DIR[] = persistent_cache_directory
27+
end
28+
@debug "Persistent compilation cache enabled. Using base directory: $(CACHE_DIR[])"
29+
30+
KERNEL_CACHE_ENABLED[] = load_preference(
31+
Reactant, "persistent_kernel_cache_enabled", false
32+
)
33+
@debug "Kernel cache enabled: $(KERNEL_CACHE_ENABLED[])"
34+
35+
AUTOTUNE_CACHE_ENABLED[] = load_preference(
36+
Reactant, "persistent_autotune_cache_enabled", true
37+
)
38+
@debug "Autotune cache enabled: $(AUTOTUNE_CACHE_ENABLED[])"
39+
else
40+
@debug "Persistent compilation cache disabled..."
41+
end
42+
2443
return nothing
2544
end
2645

test/basic.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,3 +1510,17 @@ end
15101510
@test @jit(nested_mapreduce_hcat(x_ra, y_ra)) nested_mapreduce_hcat(x, y)
15111511
end
15121512
end
1513+
1514+
@testset "compilation cache" begin
1515+
if Reactant.PersistentCompileCache.autotune_cache_enabled() &&
1516+
contains(string(Reactant.devices()[1]), "CUDA")
1517+
A = Reactant.to_rarray(rand(Float32, 2, 5))
1518+
B = Reactant.to_rarray(rand(Float32, 5, 1000))
1519+
@jit A * B # This should populate the cache dir
1520+
1521+
@test any(
1522+
endswith(".textproto"),
1523+
readdir(Reactant.PersistentCompileCache.get_autotune_cache_directory()),
1524+
)
1525+
end
1526+
end

0 commit comments

Comments
 (0)