Repository navigation
MoE autotune cache: version disk entries and refit loaded configs - #14617
Merged
Merged
Conversation
Disk cache hits were returned without the dims / shared memory fitters, and the key had no schema or Triton identity, so entries written by the pre-#14578 code (dormant then) now load unchanged and can exceed shared memory on the per-expert dW kernel or break K % BLOCK_SIZE_K. Add a schema version and the Triton version to the key and the stored JSON (unversioned or mismatched entries are misses), and run _fit_configs_to_dims / _fit_configs_to_smem on reconstructed configs before caching them.
[skip ci]
Member
Author
|
@codex review |
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. |
|
Codex Review: Didn't find any major issues. Breezy! 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". |
This was referenced Oct 22, 2026
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.
What broke
#14578 made disk cache hits in
unsloth/kernels/moe/autotune_cache.pylive:get_or_autotune_moe_kernelsrebuilds the cachedKernelConfig*objects and returns them straight away. Two things were missing on that path:Noneand deleted it, so the file was dormant) now loads unchanged._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 #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 andBLOCK_SIZE_K=128also loads unchanged and failstl.static_assert(K % BLOCK_SIZE_K == 0).Change
_CACHE_SCHEMA_VERSION = 2plustriton.__version__go into the cache key and into the saved JSON.load_cached_configtreats an entry without them, or with different values, as a miss (returnsNonewithout deleting; the next save overwrites it). Legacy files sit under the old key and are never opened._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_kernelsstays@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):BLOCK_SIZE_Klarger than K from disk is fitted to divide K and Ntests/test_moe_autotune_cache_hit.pynow 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