Repository navigation
MoE grouped GEMM autotune: key, tune and fit tiles on the experts' own GPU - #14719
Conversation
|
@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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6604ea6302
ℹ️ 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".
| device = _operand_cuda_index(device) | ||
| if device is not None and device != torch.cuda.current_device(): | ||
| with torch.cuda.device(device): |
There was a problem hiding this comment.
Include the GPU model in the device-specific cache key
On mixed-model hosts where two cards expose the same compute capability, switching the current device here still produces the same _get_cache_key, so the second layer immediately reuses the first card's _kernel_config_cache entry instead of tuning and SMEM-fitting on its own card. This also bypasses the per-model distinction already implemented by _autotuner_for in grouped_gemm/interface.py, which keys tuning caches by device name, capability, and SM count. Include equivalent model/resource information in this outer cache key so distinct models with the same capability cannot share tiles.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch, fixed in 52f1ac828a. The cache key now also carries the GPU model (name, capability, SM count), the same identity _autotuner_for uses, read once per card and cached so repeated calls do no property query. Two different cards with the same capability now tune separately, while identical cards still share one entry. Disk entries written under the old capability-only key are still read when every visible card is the same model, so single-GPU and homogeneous hosts keep their existing caches; on mixed hosts they are ignored and retuned. Added spoofed tests for two sm89 cards of different models, two identical sm89 cards, and the old disk entry fallback.
There was a problem hiding this comment.
Correction: after rebasing onto the pre-commit.ci format commit the fix landed as 0e7f334 (same change).
…rflows per-expert shared memory (unslothai#14723) Follow-up to unslothai#14550: keep the legacy dW kernel when an explicit tile overflows per-expert shared memory. The per-expert dW kernel double buffers both operand tiles, so a tile the legacy kernel launched can now raise `OutOfResources`. CPU cross-compile (Triton 3.8, 16 B aligned pointers), bf16, tile 128x256x256, 4 stages: | Card | Legacy | Per-expert | Limit | |---|---:|---:|---:| | A100 (sm_80) | 69,632 | 131,584 | 166,912 | | A10 / L4 (sm_86 / sm_89) | 69,632 | 131,584 | 101,376 | | H100 (sm_90) | 131,072 | 262,656 | 232,448 | | RTX 5090 (sm_120) | 69,632 | 131,584 | 101,376 | The automatic config sources are already fitted. A sweep of 2,220 fitted configs found 0 over the limit. It covered heuristic, default, and every autotune candidate tile refit as a stale disk entry, on sm_80/86/89/90/120, bf16 and fp32, two GEMM shapes. The autotuner also skips `OutOfResources` configs. Two cases are not covered: a caller-supplied tile (`grouped_gemm(kernel_config_bwd_dW=...)` or `_grouped_gemm_dW_impl`), and a config fitted against another card. Fitting reads `torch.cuda.current_device()`, not the operand's device. Change: before a non-autotuned per-expert dW launch, `_launch_grouped_gemm_dW` estimates shared memory on the operand's card, cached per card and tile. It uses the same `_estimate_smem` the fitting uses. If the tile does not fit, it falls back to the legacy kernel. The two kernels are bitwise equal, so results do not change. ROCm and an unreadable device keep the per-expert kernel. ## Tests - GPU kernel suite on B200: main 164 passed / 8 failed (all 8 are the new tests), this branch 172 passed. - New `tests/test_moe_dw_explicit_tile_smem.py`. CPU routing per card (A100, A10, L4, H100, B200, RTX 5090) plus a GPU test: the explicit tile launches, is bitwise equal to legacy, and the accumulate path matches. Base: 7 failed. Fix: 7 passed plus the GPU case. Related MoE kernel tests, CPU, torch 2.14: base 233 passed / 7 failed (all new), fix 240 passed, 43 skipped on both. ## Not covered - Fitting configs to the operand's card instead of the current one (the other half of the mixed-host gap) is unslothai#14719; this PR checks explicit tiles on `X.device` independently of it. - unslothai#14550's reported 3 to 4% A100 slowdown was not re-timed: fp32 activations, heuristic tile, Mixtral gate_up, T = 4096. No A100 on this host. It is a single kernel-time cell on a non-default fp32 path, and it is not routed here.
Two cards with the same compute capability but a different model (name or SM count) produced the same key, so the second reused the first card's tuning. Add the (name, capability, SM count) identity grouped_gemm uses, read once per card. Disk entries written before this key still load when every visible card is the same model. [skip ci]
|
Codex Review: Didn't find any major issues. Keep it up! 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". |
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]
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3fcc7ec8fd
ℹ️ 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".
| if cached_data is None and device_model is not None and _all_cards_match(device_model): | ||
| # Entries written before the key carried the GPU model; on a single-model host they are this card's. | ||
| cached_data = load_cached_config(_get_cache_key(*key_args), **dev_kw) |
There was a problem hiding this comment.
Reject model-less cache entries without provenance
When ~/.cache is shared between hosts or survives a GPU replacement, a legacy entry may have been tuned on a different GPU model with the same compute capability. _all_cards_match only describes the cards visible now, so it cannot prove which model wrote that entry; loading it here skips tuning for the current model, and because the result is only placed in memory under the new key, every fresh process can continue reusing the stale legacy file indefinitely. Reject model-less entries or require persisted model provenance before migrating them.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, the visible cards cannot say which GPU model wrote a model-less entry, and since the migrated configs only went into memory, every new process kept reading the old file. I removed the legacy fallback (and _all_cards_match) entirely: an entry under the pre-model key is never reused now. The card retunes once and saves under its own model-keyed file, which later processes load directly. When the device properties are unreadable the key is still the model-less one, so that path and the explicit device handling from #14751 are unchanged.
Tests: test_a_pre_model_key_entry_is_retuned_once_and_saved_under_the_model_key replaces the single-model-host test (it fails on the previous head) and checks that a fresh process loads the new file without retuning. The cache hit / validate fixtures now pin the GPU model so their keys match what the lookup reads, independent of the host's card.
The visible cards cannot prove which GPU model wrote a pre-model-key entry, and the migrated configs only lived in memory, so every process kept rereading the stale file. Retune once and persist under the model key. [skip ci]
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. 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". |
…dable current device keeps the model-less key [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! 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". |
Summary
On a
device_mapsplit 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 #14578 / #14617 that choice is also reused from disk by later processes.get_or_autotune_moe_kernelsnow takes an optionaldevice. 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_smemread 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) passesgemm_hidden_states.devicewhen the installed unsloth accepts the parameter, so either repo can land first.What does not change
_unsloth_moe_configs) still makes this one lookup per layer, not per step; no device property is read per launch.autotuning.py,_TRITON_HAS_TMA = False), so the process-wide sm90 check from MoE grouped GEMM: require sm90 on every visible card for TMA and launch on the operands' device #14358 has no effect on the configs used today.Tests
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=Noneand the current card share one cached entry; CPU / unindexed devices keep the current path. All 3 fail on main and pass here.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.