Ray's Knowledge Base
Unlock

candle's cold Metal kernel compile takes about 10 s and is lost when another Metal process runs

PitfallVerified 29 Sep 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

The first GPU (Metal) run of a candle model takes about 10 s; later runs take about 0.3 s. When that first call runs under a short timeout (2 s), it is killed, and the GPU stays cold. The GPU can also go cold again after a warm-up said it was done: every search times out at 2 s (ranked_by: bm25 (laya: timeout after 2000 ms)) although a warm marker exists.

Cause#

candle compiles its Metal kernel source at runtime. macOS keeps the compiled kernels in one cache shared by every command-line tool ($(getconf DARWIN_USER_CACHE_DIR)com.apple.metal/), keyed by the kernel source, not by the binary: copies, renames, touch and new builds with the same kernels stay warm. A compile is not kept when:

  • the process is killed before it finishes (a search timeout);
  • another Metal process runs at the same time (3 of 3 with two overlapping cold warm-ups);
  • the compiling process was spawned by a Metal process and inherited its open cache files (see lesson 9a0af6431e). In these runs, the first in-process compile of a warm-up was never kept either; a fresh process compiling alone was.

Fix#

Warm up with no time limit, then check it in a fresh process: time one short GPU call in a new process, and trust the warm-up only when that call takes 1.5 s or less; otherwise compile once more and check again. Use the CPU engine until a warm-up is confirmed. After a GPU timeout, drop the confirmation and run one background warm-up that closes inherited file descriptors.

rkb 0.0.7 does all of this: rkb models warm (and rkb models fetch, rkb install) runs the checked warm-up, a GPU timeout starts rkb models warm --background, and rkb doctor times a real GPU call instead of trusting the marker (over 1.5 s is a warning; the probe itself compiles, so a second rkb doctor is fast). If searches still say laya: timeout after 2000 ms, run rkb models warm.

Evidence#

Laya reranker on an M4 Pro: first GPU run 9.61 s, second 0.27 s; after a warm-up step, rkb search used the GPU in 0.26 s. An early test (2026-09-27) found a copied binary slow (10.00 s); a later investigation (2026-09-29) found copies, renames and changed binary UUIDs all warm (0.2 s), so the cache is keyed by kernel source.

rkb 0.0.5, 2026-09-29: rkb install wrote the warm marker, yet every cold rkb search still timed out at 2 s. One search with a 60 s limit took 9.7 s; after it, searches took 0.28 s. A compile was lost 3 of 3 times with two overlapping cold warm-ups and with a warm-up during 8 GPU searches; runs alone kept it.

rkb 0.0.7 development, 2026-09-29, with a copy of rkb whose Metal source comments were changed (cold): every cold rkb models warm needed 2 compiles before a fresh process scored in 0.2 s, and then it held (probe 165 ms 20 s later). A search timed out at 2 s, the next used the CPU, the background warm-up ended after 15 to 20 s, and later searches said laya (gpu) in 0.4 s. rkb doctor on a cold copy: "a GPU call took 6.0 s (the kernels were cold; compiled now)", then ok at 0.2 s.

Also found by searching

  • ranked_by: bm25 (laya: timeout after 2000 ms) every search even after warmup
  • first candle GPU inference on Mac takes 10 seconds then later runs are fast, but sometimes slow again
  • how do I keep candle Metal kernels compiled and warm across processes on macOS
  • com.apple.metal shader cache DARWIN_USER_CACHE_DIR evicted, Metal pipeline compile cold start
  • rkb search ranked_by: bm25 (laya: timeout after 2000 ms) every time though warm marker exists
  • candle on Metal first run takes 10 seconds then 0.3 seconds, killed by timeout stays slow
  • how do I warm up candle Metal shader compile so the GPU reranker is fast on first search?
  • metal kernel cache com.apple.metal not kept, shader compile repeats, GPU reranking always cold after parallel process