Skip to content

MoE grouped GEMM: keep the legacy dW kernel when an explicit tile overflows per-expert shared memory - #14723

Merged
danielhanchen merged 4 commits into
mainfrom
moe-dw-smem-operand-device
Oct 22, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
moe-dw-smem-operand-device

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

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:

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

…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.
@danielhanchen danielhanchen changed the title MoE grouped GEMM: fit explicit dW tiles and config lookups to the operand's card MoE grouped GEMM: keep the legacy dW kernel when an explicit tile overflows per-expert shared memory Oct 22, 2026
[skip ci]
@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-22T14:51:28.183609Z f02daad 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. Delightful!

Reviewed commit: f02daaddcb

ℹ️ 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 e66f408 into main Oct 22, 2026
@danielhanchen
danielhanchen deleted the moe-dw-smem-operand-device branch October 22, 2026 15:01
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.
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