Skip to content

Warm restarts: hand the compile cache key tag to Inductor's compile workers (no autotune re-benchmark every run) - #18111

Merged
danielhanchen merged 2 commits into
mainfrom
warm-restart-autotune-tag
Nov 17, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
warm-restart-autotune-tag

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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 from TORCH_COMPILE_CACHE_KEY_TAG at start-up, so they run with an empty tag.

The autotune key is sha256(f"{kernel file}:{cache_key_tag}") plus torch_key():

  1. A kernel compiled in a worker gets an AutotuneCache keyed under the empty tag. That object is pickled into the FX graph cache entry as the kernel's save hook.
  2. On a warm run the kernel is loaded from the FX graph cache, and recheck_autotune_cache looks it up in this process under the full tag. It misses.
  3. The kernel is re-benchmarked, and the result is saved through the pickled hook under the empty-tag key again. The next warm run misses the same way.

I confirmed this from the cache files on a warm run. For one kernel, the .best_config written 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_cache now also copies the tag into TORCH_COMPILE_CACHE_KEY_TAG, and import unsloth copies 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.
  • A new tag, 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.

Model Warm time to first step, main Warm time to first step, this PR Change 30 steps end to end, main This PR
Gemma-4-E2B-it, 4-bit LoRA 133.5 / 133.4 / 133.1 (median 133.4) 119.5 / 120.4 / 120.3 (median 120.3) -13.1 s (-9.8%) 169.4 156.6
Llama-3.2-1B-Instruct, 4-bit LoRA 54.9 / 55.4 / 55.0 (median 55.0) 52.1 / 53.0 / 52.2 (median 52.2) -2.8 s (-5.1%) 69.7 66.9

Warm-run benchmarks fall from 290 to 0 (Gemma) and from 70 to 0 (Llama); CachingAutotuner.benchmark_all_configs falls 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

  • Step-1 loss: identical in every run except one main warm run (7.9162 vs 7.9132). That run re-benchmarked and picked a different config.
  • Gemma loss curves: with the fix the warm runs reuse one config, and the run-to-run loss spread shrinks. Mean |dloss| over 30 steps is 0.0397 main vs main, 0.0104 fix vs fix and 0.0232 main vs fix.
  • Llama loss curves: main vs fix stays within main vs main (mean |dloss| 0.0004 for both).
  • B200 (correctness only): after the cold run, every multi-config kernel hits the autotune cache on warm runs and nothing is benchmarked. On main, tiny Gemma-4 re-benchmarks the same 2 kernels on every warm run.

Tests

tests/test_compile_cache_tag_workers.py (CPU) checks:

  • the tag reaches the environment, and tagging twice adds nothing;
  • a tag set elsewhere is forwarded, and an empty tag leaves the environment alone;
  • import unsloth syncs after the kernel imports;
  • a fresh interpreter (as a compile worker is) computes the same autotune key as the parent, and a different one without the environment copy.

All 6 fail on main and pass here. tests/test_compile_config_threads.py, tests/test_import_gc_threshold.py and the cache-tag test in tests/test_nf4_triton_dequant.py still pass.

Not covered

  • T4, A100, G4 and AMD were not timed.
  • torch older than 2.14.1 was not run. The change is guarded on cache_key_tag existing.
  • Kernels from a module imported after the worker pool has started would still get a stale worker tag. Every Unsloth tag is set during import unsloth today, before the first compile.

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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T07:51:06.157083Z 4673869 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 46738697a8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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
danielhanchen merged commit 8b0fed5 into main Nov 17, 2026
@danielhanchen
danielhanchen deleted the warm-restart-autotune-tag branch November 17, 2026 08:23
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant