Repository navigation
MoE autotune cache: compare device capability as a tuple so saved configs are reused - #14578
Merged
Merged
Conversation
…figs are reused JSON stores the (major, minor) tuple as a list, so load_cached_config saw a capability change on every lookup, deleted the entry and autotuning re-ran in every process.
Codex Review SummaryThis 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. |
|
The account paying for this security review has reached its Codex usage limits. The payer can check the Codex usage dashboard. For personal accounts, using credits requires enabling “Use credits for security reviews” in Code review settings. If you do not manage the paying account, contact this repository's admins. |
Lyxot
pushed a commit
to NilayYadav/unsloth
that referenced
this pull request
Oct 22, 2026
…slothai#14617) ## What broke unslothai#14578 made disk cache hits in `unsloth/kernels/moe/autotune_cache.py` live: `get_or_autotune_moe_kernels` rebuilds the cached `KernelConfig*` objects and returns them straight away. Two things were missing on that path: 1. The cache key has no schema or Triton identity, so a file written by the pre-unslothai#14578 code (whose loader always returned `None` and deleted it, so the file was dormant) now loads unchanged. 2. The loaded configs never go through `_fit_configs_to_dims` / `_fit_configs_to_smem`, which every other config source (heuristic, default) already uses. Concrete failure: bf16, E=128, hidden 2048, intermediate 768, top-k 8, a cached dW tile (BM, BN, BK, warps, stages) = (128, 256, 256, 8, 4). That tile was valid for the legacy dW kernel (69,632 B shared memory on sm86), but the per-expert dW kernel from unslothai#14550 needs 132,096 B on sm86 (limit 101,376 B) and 263,168 B on sm90 (limit 232,448 B), so the launch raises `OutOfResources`. A cached entry with N=128, K=64 and `BLOCK_SIZE_K=128` also loads unchanged and fails `tl.static_assert(K % BLOCK_SIZE_K == 0)`. ## Change - `_CACHE_SCHEMA_VERSION = 2` plus `triton.__version__` go into the cache key and into the saved JSON. `load_cached_config` treats an entry without them, or with different values, as a miss (returns `None` without deleting; the next save overwrites it). Legacy files sit under the old key and are never opened. - On a disk hit, the reconstructed configs run through `_fit_configs_to_dims(configs, 2 * intermediate_dim, hidden_dim)` and `_fit_configs_to_smem(configs, dtype, weight_dtype)` before they go into the in-memory cache. The example dW tile becomes (64, 256, 256, 8, 3), 66,048 B compiled on sm86. The warm path is still a single dict lookup: fitting runs once per key per process, on the first disk load. `get_or_autotune_moe_kernels` stays `@torch.compiler.disable`, so compile behaviour does not change. A one-time re-tune per model shape happens after upgrading, since old entries are misses. ## Validation New `tests/test_moe_autotune_cache_validate.py` (CPU, CUDA capability and shared memory mocked): - a legacy unversioned file is ignored and autotuning runs once; the legacy-key file is left on disk - an oversized dW tile from disk is fitted to (64, 256, 256, 8, 3) with zero autotune calls - a `BLOCK_SIZE_K` larger than K from disk is fitted to divide K and N - a valid current entry hits unchanged with zero autotune calls, and the warm call returns the same tuple without reading disk `tests/test_moe_autotune_cache_hit.py` now pins the mocked shared memory so its tuned tiles stay as saved. Flip on main: 3 of the 4 new tests fail (legacy file reused, dW tile unchanged, BLOCK_SIZE_K unchanged); the valid-hit guard passes on both. With the fix, the new tests plus the existing MoE cache / fallback / compile tests (`test_moe_autotune_cache_hit.py`, `test_moe_fallback_smem.py`, `test_moe_fallback_tile_dims.py`, `test_grouped_gemm_mixed_tma_device.py`, `test_moe_autotune_under_compile.py`) give 230 passed on torch 2.10 and torch 2.14 venvs. ## Not covered - No GPU launch of a refitted disk config was run in this change; the shared memory numbers come from the existing estimator and a CPU cross-compile. - Device selection is unchanged: lookup and tuning still use the current CUDA device, and unsloth_zoo passes no operand device. On a mixed-GPU host where the current device differs from the operand device, capability and shared memory can be read from the wrong card. Fixing that needs a device argument threaded from zoo, left for a follow-up.
danielhanchen
added a commit
to danielhanchen/unsloth-staging-2
that referenced
this pull request
Oct 25, 2026
…n GPU (unslothai#14719) ## Summary On a `device_map` split across different GPU models, the MoE grouped GEMM autotune lookup read the current CUDA device for everything: the cache key, the tuning run (`device = "cuda"`), the disk-cache capability check and the shared-memory fit. A layer whose experts live on another card therefore got tiles keyed, tuned and SMEM-fitted for the current card, and since unslothai#14578 / unslothai#14617 that choice is also reused from disk by later processes. `get_or_autotune_moe_kernels` now takes an optional `device`. When it names a CUDA card other than the current one, the lookup runs with that card current (`torch.cuda.device`), so the key carries the experts' capability, tuning benchmarks run there, and `_device_smem_per_block` / `_fit_configs_to_smem` read that card. `device=None`, the current card, CPU and unindexed devices take exactly today's path. The zoo side (unslothai/unsloth-zoo, branch `moe-autotune-operand-device`) passes `gemm_hidden_states.device` when the installed unsloth accepts the parameter, so either repo can land first. ## What does not change - Single-GPU and same-model multi-GPU hosts: same key, same tuning, same tiles. - The per-module config cache in zoo (`_unsloth_moe_configs`) still makes this one lookup per layer, not per step; no device property is read per launch. - TMA: left as is. The shipped autotuner keeps TMA off (`autotuning.py`, `_TRITON_HAS_TMA = False`), so the process-wide sm90 check from unslothai#14358 has no effect on the configs used today. ## Tests - New `tests/test_moe_autotune_operand_device.py` (spoofed sm90 + sm80 host): a layer on card 1 tunes and keys for sm80 and leaves the current device untouched; `device=None` and the current card share one cached entry; CPU / unindexed devices keep the current path. All 3 fail on main and pass here. - Existing autotune and fallback suites (`test_moe_autotune_cache_hit`, `_cache_validate`, `test_moe_fallback_smem`, `test_moe_fallback_tile_dims`): 211 passed with the new file. ## Not covered No mixed-model multi-GPU host was available; the device switching is proven with a spoofed CUDA runtime, not a real H100 + L4 split. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.
The MoE grouped GEMM autotune disk cache never hits.
load_cached_config(unsloth/kernels/moe/autotune_cache.py) compares the stored device capability, which JSON keeps as a list ([9, 0]), against the tuple fromtorch.cuda.get_device_capability()((9, 0)). Every lookup therefore counts as a device change, deletes the cache file and re-tunes. Each eager process pays the full autotune again.Change
Validation
New
tests/test_moe_autotune_cache_hit.py(CPU):The same 66 MoE autotune / grouped GEMM tests pass here on torch 2.10 and 2.14; 2 of them fail on main.
On Colab A100, the first step after the one-time tune goes from about 220 to 244 s to about 25 s once the cache is written.
Note: inside Unsloth's compiled blocks autotuning does not run at all (the configs are requested while tracing, on fake tensors), so compiled training uses the default tiles on main and here alike. This PR only affects processes that autotune eagerly.