Repository navigation
Warm restarts: hand the compile cache key tag to Inductor's compile workers (no autotune re-benchmark every run) - #18111
Merged
Merged
Conversation
Unsloth adds its kernel hashes to torch.compiler.config.cache_key_tag in this process only. Inductor's compile workers are separate interpreters that read the tag from TORCH_COMPILE_CACHE_KEY_TAG, so they keyed each kernel's autotune result under the empty tag, while warm runs look it up here under the full tag. Every warm run therefore missed and re-benchmarked those kernels (290 benchmarks, 12.5 s of step 1 for Gemma-4-E2B on L4) and saved the result under the worker key again. Mirror the tag into the environment whenever it changes and once more at the end of import, before the worker pool starts. A new tag, unsloth/autotune-tag-v2, changes the FX graph keys once so cached entries whose save hook still carries the worker key are rebuilt.
Member
Author
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
[skip ci]
danielhanchen
added a commit
that referenced
this pull request
Nov 17, 2026
…oth.kernels loaded on CPU since #18111) (#18117) Two CPU-backend tests fail on main in the CPU `rest` shard since #18111 (seen on the staging runs of #18075 and #18076): ``` tests/test_cpu_backend_init.py::test_no_accelerator_loads_the_cpu_backend tests/test_cpu_backend_tiled_mlp.py::test_cpu_backend_loader_tiles_like_the_gpu_loaders E AssertionError: assert ['unsloth.kernels', ...] == [] ``` ## Cause #18111 added, at the end of `unsloth/__init__.py`: ```python from .kernels.triton_launch import sync_compile_cache_tag_to_workers as _sync_tag ``` It runs for every backend. On a machine with no accelerator, `import unsloth` now loads `unsloth.kernels` (and everything its package `__init__` pulls in, 87 modules) even though the CPU backend deliberately never loads GPU kernels; the tests above guard exactly that. The sync itself is right, but it only matters where kernels add tags to `torch.compiler.config.cache_key_tag`, i.e. the GPU backend, which has already imported `unsloth.kernels` by then. On the CPU backend there is no tag to hand to compile workers. ## Fix Run the sync only when the GPU backend was selected (`_IS_CPU_BACKEND` is False). The MLX branch never defines `_IS_CPU_BACKEND`, so it is skipped there too. ## Verification - `tests/test_cpu_backend_init.py`, `tests/test_cpu_backend_tiled_mlp.py`: 2 failed on main, pass here. - `tests/test_compile_cache_tag_workers.py` (#18111's own tests): 6 passed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Warm restarts (second and later runs on the same machine, same model and versions) re-benchmark Inductor's Triton kernels on every run, even though their autotune results are on disk. On Gemma-4-E2B on an L4 that is 290 benchmarks and 12.5 s of step 1, every warm run.
Cause
Unsloth adds its kernel hashes (and the zoo's rand4x workaround) to
torch.compiler.config.cache_key_tag, but only in the training process. Inductor's compile workers are separate interpreters that read the tag only fromTORCH_COMPILE_CACHE_KEY_TAGat start-up, so they run with an empty tag.The autotune key is
sha256(f"{kernel file}:{cache_key_tag}")plustorch_key():AutotuneCachekeyed under the empty tag. That object is pickled into the FX graph cache entry as the kernel's save hook.recheck_autotune_cachelooks it up in this process under the full tag. It misses.I confirmed this from the cache files on a warm run. For one kernel, the
.best_configwritten after benchmarking matched the key for tag'', and the key looked up matched the full Unsloth tag. Kernels compiled in the main process were not affected, which is why most kernels still hit.Fix
tag_compile_cachenow also copies the tag intoTORCH_COMPILE_CACHE_KEY_TAG, andimport unslothcopies the final tag (zoo tags included) once more before the worker pool can start. Workers then key autotune results the same way as the training process.unsloth/autotune-tag-v2, changes the FX graph keys once. Cached entries whose save hook still carries the empty-tag key are rebuilt instead of re-benchmarking forever. The cost is one cold compile after upgrading, the same as any release that touches a tagged kernel file.Nothing changes when torch has no
cache_key_tag, or when the tag is empty.Timing (Colab L4, one VM, back to back)
Each arm has its own cache: one cold run, then 3 warm runs. Setup: 30 steps, batch 2 x 4 accumulation, max length 512, alpaca, torch 2.14.1, transformers 5.18.0, trl 1.15.0.
Warm-run benchmarks fall from 290 to 0 (Gemma) and from 70 to 0 (Llama);
CachingAutotuner.benchmark_all_configsfalls from 12.5 s and 3.0 s to 0. The repeated-main spread is 0.4 s (Gemma) and 0.5 s (Llama), so both changes are well outside it.Correctness
Tests
tests/test_compile_cache_tag_workers.py(CPU) checks:import unslothsyncs after the kernel imports;All 6 fail on main and pass here.
tests/test_compile_config_threads.py,tests/test_import_gc_threshold.pyand the cache-tag test intests/test_nf4_triton_dequant.pystill pass.Not covered
cache_key_tagexisting.import unslothtoday, before the first compile.