Repository navigation
MoE grouped GEMM: keep the legacy dW kernel when an explicit tile overflows per-expert shared memory - #14723
Merged
Conversation
…rflows per-expert shared memory The per-expert dW kernel (#14550) double buffers both operand tiles, so a caller-supplied tile the legacy kernel launched can now raise OutOfResources: 128x256x256 at 4 stages compiles to 131,584 B on sm_80/86/89/120 (legacy 69,632 B) and 262,656 B on sm_90 (legacy 131,072 B), over the 101,376 B / 232,448 B limits. Heuristic, default and refit disk configs are already fitted (#14576, #14617), but explicit tiles and configs fitted against another card (fitting reads the current device, not the operand's) are not. Before launching a non-autotuned per-expert dW, estimate its shared memory on the operand's card with the same _estimate_smem the fitting uses (cached per card and tile) and fall back to the legacy kernel when it does not fit. The two kernels are bitwise equal, so results do not change.
…card get_or_autotune_moe_kernels keyed the cache by the current device's capability and fitted heuristic, default and disk configs to the current device's shared memory. On a mixed host the experts' tensors need not be on the current device (accelerate moves inputs without switching it), so a bf16 dW tile refit for an A100 (128x256x256, 3 stages, 132,096 B on sm_89) was kept for an L4 (101,376 B). Add an optional device argument (the operand's): the lookup runs with that card current, so the cache key, the shared memory fit and any autotuning use it. Omitting it keeps the current-device behaviour.
[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. Delightful! 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". |
danielhanchen
added a commit
to Imagineer99/unsloth
that referenced
this pull request
Oct 24, 2026
…flow the card (unslothai#15070) The per-expert MoE dW kernel (the default since unslothai#14550) can raise `OutOfResources` on cards with less shared memory than an H100. unslothai#14723 added a fallback to the legacy kernel for oversized tiles, but its shared memory estimate is wrong in one case: when neither operand is gathered (`permute_x = False` and `permute_y = False`). ### Cause Shared memory was checked with offline `triton.compile` runs (Triton 3.6 and 3.8, sm75 / 80 / 89 / 90), reading `metadata.shared`. - **Gathered launches:** the legacy kernel, and any launch where one operand is gathered, never pipeline. Their shared memory does not depend on `num_stages`, which is what `_estimate_smem("dW")` models. - **Unpermuted per-expert launches:** the per-expert kernel's single M loop is software pipelined when both loads are contiguous. - sm80-class cards (sm80 / 86 / 89 / 120): it needs `(stages - 1) * X tile + one dY tile`. - sm90+: it needs `stages * (X tile + dY tile)`. - **Measured against the estimate:** | Card | Tile (M, N, K, stages) | gathered | not gathered | estimate on main | limit | |---|---|---|---|---|---| | L4 (sm89) | heuristic 64x64x256x4 | 41216 | 106496 | 41984 | 101376 | | A100 (sm80) | explicit 128x256x256x4 | 131584 | 262144 | 132096 | 166912 | - **Effect:** neither the smem refit nor the unslothai#14723 fallback fired. With no permute, `grouped_gemm` / `grouped_gemm_dW` on L4 raised `OutOfResources(106496, 101376)` for the heuristic tile. - **T4 (sm75):** the estimate is right there, but explicit tiles sized for bigger cards overflow the legacy kernel as well (131072 B against 65536 B), and nothing refit them. ### Change - **New estimate input:** `_estimate_smem` takes `pipelined_dW` and adds the measured per-stage cost for unpermuted per-expert launches. - **Stage drop:** those launches lower `num_stages` until the tile fits. The tile stays the same, so the result is bit-identical. - **Retry on a too-big tile:** if a non-autotuned dW launch still raises `OutOfResources`, it retries once with a tile fitted to that card. Triton raises before the kernel runs, so `dW` is untouched. A tile that already launches is never changed. - **Unchanged:** permuted launches, which are what the MoE training paths use (one operand is always gathered), keep main's estimate and launch parameters. ROCm keeps its current behaviour. - **Test fix:** `test_moe_fallback_tile_dims.py` expected the heuristic dX tile to keep 4 stages on L4. That tile needs 122880 B there, over the 101376 B limit, so the 3 stages the smem fit picks is correct. The test now turns the smem fit off, since the smem tests already cover it. ### Tests Colab, failing test IDs, main vs this PR, on torch 2.10.0 and 2.14.1 (same IDs on both versions): | GPU | main | this PR | |---|---|---| | T4 | `test_explicit_tile_launches_on_this_gpu`, `test_per_expert_fp32_activations_bf16_weight[False]` / `[True]` fail | pass | | L4 | 7 `test_moe_fallback_tile_dims` cases fail (stale stage count) | pass | | A100 | 0 failures | 0 failures, 9 new tests pass | - **New test failing on main:** `test_unpermuted_heuristic_tile_launches_on_this_gpu`. On L4 it raises `OutOfResources` (106496 > 101376). - **Failures on both sides** (same IDs, not touched here): - L4: two `test_fp8_ragged_block_dequant` cases and `test_fp8_dequant_save_policy[False]`; - T4: two fullgraph compile tests, which fail because Inductor on sm75 skips bf16 graphs (handled separately). **L4 timing.** bf16, heuristic dW config, no permute, median over 3 alternating runs: | case | main | this PR (3 stages) | legacy kernel | |---|---|---|---| | gate_up T=4096, torch 2.10.0 | OutOfResources | 7.07 ms | 10.57 ms | | gate_up T=4096, torch 2.14.1 | OutOfResources | 7.08 ms | 10.48 ms | | gate_up T=512, torch 2.14.1 | OutOfResources | 3.59 ms | 7.11 ms | | down T=4096, torch 2.14.1 | OutOfResources | 3.60 ms | 5.23 ms | - **Permuted launches:** they take the identical path with identical launch parameters. - **A100:** stage counts are unchanged, and timings are within 0.1 to 0.4% of main. ### Not covered - **Other GPUs:** no H100, B200 or sm120 runs. The sm90 numbers come from offline compiles only. - **A100 explicit unpermuted tile:** the 128x256x256 case now runs at 2 stages (131072 B by offline compile) but was not launched on a real A100.
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.
Follow-up to #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: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
OutOfResourcesconfigs. 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 readstorch.cuda.current_device(), not the operand's device.Change: before a non-autotuned per-expert dW launch,
_launch_grouped_gemm_dWestimates shared memory on the operand's card, cached per card and tile. It uses the same_estimate_smemthe 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
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
X.deviceindependently of it.