Skip to content

MoE autotune cache: version disk entries and refit loaded configs - #14617

Merged
danielhanchen merged 2 commits into
mainfrom
moe-autotune-cache-validate
Oct 22, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
moe-autotune-cache-validate

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

What broke

#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-MoE autotune cache: compare device capability as a tuple so saved configs are reused #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 #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.

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

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T07:18:41.725464Z f1e2e89 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. Breezy!

Reviewed commit: f1e2e89942

ℹ️ 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".

@danielhanchen
danielhanchen merged commit 465263e into main Oct 22, 2026
@danielhanchen
danielhanchen deleted the moe-autotune-cache-validate branch October 22, 2026 07:19
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>
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