Skip to content

MoE autotune cache: pick kernel configs for the operands' GPU - #14751

Merged
danielhanchen merged 2 commits into
mainfrom
dh/s3-mixed-gpu-operand-device
Oct 22, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/s3-mixed-gpu-operand-device

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

The grouped GEMM launches on X.device, but get_or_autotune_moe_kernels read the
capability, shared memory limit, cache key and disk entry of the current device.
On a host with two GPU models (for example H100 + L4, B200 + RTX PRO 6000) a model
split across both could get configs fitted to the bigger card. With current H100
and operands on an L4 (101,376 B limit):

Path main fwd / dX / dW (B) this PR
heuristic bf16 98,304 / 122,880 / 41,984 98,304 / 81,920 / 41,984
heuristic fp32 X, bf16 W 131,072 / 147,456 / 82,944 65,536 / 98,304 / 82,944
disk cache with an H100 entry reuses it, dX 122,880 tunes on the L4, dX 81,920

Changes

  • get_or_autotune_moe_kernels takes an optional device; the cache key, disk entry
    validation and save, shared memory refit, heuristic/default fallbacks and the
    tuning run all follow it. Tuning runs under torch.cuda.device(device).
  • device=None keeps the current-device behaviour. unsloth-zoo passes the operands'
    device only when this argument exists.

Single GPU: 450 mocked cells (T4, L4, A100, H100, B200, RTX 5090 x 5 dtype pairs x
5 shapes, with no device, cuda:0 and cuda) give byte-identical configs to main.

Tests: tests/test_moe_autotune_operand_device.py (41 tests, CPU with mocked
devices) fail on main and pass here; the existing MoE autotune, fallback,
cache and grouped GEMM tests are unchanged.

get_or_autotune_moe_kernels read the capability, shared memory limit and
disk cache entry of the current device, while the grouped GEMM launches on
X.device. On a host with two GPU models (H100 + L4) a model split across
both could get configs fitted to the larger card. Add an optional device
argument that the capability check, cache key, disk entry validation,
shared memory refit, fallback configs and tuning all follow. device=None
keeps the current-device behaviour, so single GPU configs are unchanged.
@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-22T17:53:37.249667Z 537a5a2 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6698a68bb7

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

force_autotune: bool = False,
seq_len: int = 8192,
weight_dtype: Optional[torch.dtype] = None,
device = None,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Wire the operand device into the production caller

With the supported unsloth_zoo==2026.10.7, forward_triton_grouped_gemm still invokes both get_or_autotune_moe_kernels calls without device (its compatibility helper only forwards weight_dtype). Consequently this new argument remains None, so capability lookup, shared-memory fitting, cache selection, and tuning still follow the current GPU rather than gemm_hidden_states.device; the stated H100/L4 failure therefore persists for normal MoE execution. The production caller needs to pass the operand device conditionally, and the dependency floor must require a zoo release containing that wiring.

Useful? React with 👍 / 👎.

unsloth_zoo releases up to 2026.10.7 ask get_or_autotune_moe_kernels for
configs without a device, so on a mixed host the tiles are fitted to the
current card. When the operands sit on a different GPU model, the
non-autotuned forward / dX / dW launches now shrink a copy of the tiles to
that card's shared memory (cached per card, dtype and tile). One GPU, a
same-model card or operands on the current card keep the tiles unchanged.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 537a5a2658

ℹ️ 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 08a5ae7 into main Oct 22, 2026
@danielhanchen
danielhanchen deleted the dh/s3-mixed-gpu-operand-device branch October 22, 2026 20:17
danielhanchen added a commit to danielhanchen/unsloth-zoo-staging that referenced this pull request Oct 22, 2026
On a host with two GPU models (H100 + A100, B200 + RTX PRO 6000), three
checks read
the current or home card instead of the card the operands live on.

1. Triton MoE kernel configs: forward_triton_grouped_gemm asked
get_or_autotune_moe_kernels for configs without a device, so they were
fitted to
the current card while the GEMM launches on X.device. It now passes the
operands' device when the installed unsloth accepts it
(unslothai/unsloth
   companion PR); older unsloth gets the same kwargs as before.
2. grouped_mm support probes: the warmup resolved only the home card. A
model with
stacked experts on a second GPU model first queried that card inside the
compiled forward, reached the compiler-disabled probe and broke the
graph
(torch._dynamo Unsupported under fullgraph=True). The loader entry point
now
runs the probes eagerly for every other GPU model that holds experts.
Cards
   without experts and cards of the home model add no probe.
3. float32 GQA SDPA: can_use_efficient_attention reads current-device
properties,
so the mem-efficient eligibility could come from the wrong card and skip
the
repeat-kv memory saving. It is now asked on the query's card, as the
flash check
   already does.

Single GPU hosts are unchanged: no device switch, no extra probe, and
the config
picker gets the current device.

Tests (CPU, mocked devices): tests/test_mixed_gpu_operand_device.py and
three new
cases in tests/test_moe_grouped_mm_probe_all_devices.py. The
mixed-device cases
fail on main and pass here; the single-device cases pass on both.

The Triton MoE caller passes the device only when the installed unsloth
accepts it (unslothai/unsloth#14751), so the two
can land in either order.
danielhanchen added a commit that referenced this pull request Oct 22, 2026
pre-commit.ci fails `ruff-format-with-kwargs` on every open PR (#14806,
#14805, #14803, #14790, #14785, #14781, #14780, #14777, #14776, #14775,
#14774, #14773, #14772, #14770, #14769, #14768, and others): "files were
modified by this hook". pre-commit.ci runs the hooks on all files, and
20 files on main (26 by the time this lands) are not in the formatter's
output, so every PR is reformatted outside its own diff. Where
pre-commit.ci can push, its autofix commit drags those unrelated files
into the PR (for example #14790's touches `tests/test_block_compile.py`,
`tests/test_bnb_integration_compile.py` and `unsloth/models/_utils.py`).

The drift came in with PRs merged today, among them #14702, #14751,
#14759, #14784, #14786, #14787, #14788, #14791 and the FP8 block decode,
GRPO routing replay, 4-bit decode, compiled blocks and MoE LoRA target
changes.

### Change

`python scripts/run_ruff_format.py` (the hook's own entry point,
`ruff==0.6.9` as pinned in `.pre-commit-config.yaml`) applied to those
20 files, plus 6 more from #14739, #14779 and #14806, which merged while
this was open. Formatting only.

### Verification

- Every changed file parses to an identical AST before and after.
- A second run of the hook over the same files changes nothing (fixed
point), and over all tracked Python files on this branch it changes
nothing.
- `ruff check` passes on the changed files;
`tests/test_formatter_fixed_point.py` passes (16).

Open PRs pick this up on their next pre-commit.ci run after merging
main.
danielhanchen added a commit that referenced this pull request Oct 23, 2026
main landed #14751, which already threads an optional device through
get_or_autotune_moe_kernels (capability, disk entry validation and save,
SMEM refit, heuristic/default fallbacks and tuning all take it explicitly).
That overlaps this branch's device handling, so the resolution keeps main's
design and layers on what is unique here.

unsloth/kernels/moe/autotune_cache.py:
- Kept main's explicit device plumbing (_device_kwargs, _cuda_index,
  _device_smem_per_block(device), _run_moe_autotuning(device=...)) and its
  docstring. Dropped this branch's wrapper that made the experts' card
  current and re-entered a device-less helper: main passes the device to
  every read, so the wrapper is redundant.
- Kept from this branch: the cache key carries the GPU model (name,
  capability, SM count via _device_model, cached per index and cleared by
  clear_cache), so two models sharing a capability tune separately; a
  pre-model-key disk entry is still loaded when every visible card is the
  same model (_all_cards_match).
- Kept this branch's intent that a CPU device keeps the current-device
  path: _operand_cuda_device maps a non-CUDA device to None (main would
  raise from get_device_capability(cpu)).

tests/test_moe_autotune_operand_device.py (add/add):
- Took main's file and harness; its mocked cards now also report name,
  major/minor and SM count, and the two tests that compute the saved key
  use the model-aware key the code now writes.
- Ported this branch's tests onto main's harness (tuning recorded via the
  device kwarg instead of the current device): each card tunes for itself,
  None and the current card share an entry, CPU/unindexed keep the current
  path, same-capability models tune separately, identical cards share one
  entry without repeated property reads, a legacy entry loads on a
  single-model host; plus a new check that a legacy entry is not trusted
  on a mixed-model host.

[skip ci]
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