Skip to content

MoE dW: fall back to the legacy kernel when a per-expert tile overflows and no smaller tile is known - #15238

Merged
danielhanchen merged 2 commits into
mainfrom
moe-dw-legacy-fallback-on-oor
Oct 25, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
moe-dw-legacy-fallback-on-oor

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #14550 (per-expert MoE dW kernel) for ROCm.

Problem

The per-expert dW kernel can overflow shared memory (LDS) on ROCm, and nothing catches it:

  • Overflow: the heuristic tile used with UNSLOTH_MOE_DISABLE_AUTOTUNE=1 (64x64x256, 8 warps, 4 stages, bf16) needs about 80 KiB of LDS in the per-expert kernel. gfx1151 has 64 KiB. The legacy kernel needs 32 KiB for the same tile.
  • Retry cannot help: on an OutOfResources launch, the launcher retries once with a tile fitted to the card. On HIP, _card_smem returns None, so there is no shared memory model, the "fitted" tile equals the failed one, and the launch re-raises.

The result is a backward-pass crash in MoE training with the unsloth_triton backend on gfx1151:

OutOfResources: out of resource: shared memory, Required: 81920, Hardware limit: 65536

NVIDIA is not affected, because _per_expert_dW_stages and the retry both use the card's real shared memory limit there.

Change

Only in _launch_grouped_gemm_dW (interface.py), and only when a per-expert launch raises OutOfResources and no smaller tile is known:

  • that launch runs the legacy dW kernel with the same tile, the legacy grid and the requested stage count;
  • dW is zeroed first unless accumulating, since the legacy kernel skips empty experts;
  • the result is recorded in _PER_EXPERT_DW_FITS, so later calls with that tile go straight to legacy without a failed launch.

The NVIDIA shrink-and-retry path is unchanged, and a legacy launch that overflows still raises as before.

Tests

New tests/test_moe_dw_rocm_lds_fallback.py simulates the ROCm condition on any CUDA GPU:

  • _card_smem returns None, and the per-expert launch raises OutOfResources(81920, 65536);
  • the test checks that the result equals the legacy kernel bit for bit, for a fresh dW and an accumulated one, with an empty expert in the routing.
main this PR
new test + test_moe_dw_per_expert.py + test_moe_dw_accumulate.py (B200, torch 2.14.1) 2 failed (the OutOfResources above), 181 passed 183 passed

gfx1151 (Radeon 8060S, ROCm 7.13, torch 2.11), base vs this PR on the same runner

The heuristic tile runs once with permute_x (gate_up style) and once unpermuted (down_proj style):

case main this PR
permute_x=True OutOfResources: shared memory, Required: 73728, Hardware limit: 65536 runs, equal to the legacy kernel bit for bit
permute_x=False OutOfResources: shared memory, Required: 106496, Hardware limit: 65536 runs, equal to the legacy kernel bit for bit

The dW test files give 108 passed, 1 failed on main and 110 passed, 1 failed with this PR (the new file adds 2). The one failure is the same test on both: test_moe_dw_accumulate.py::test_existing_gradient_survives_in_microbatch_cancellation. It is a separate, pre-existing gfx1151 accumulate precision issue that this PR does not touch; I will follow up on it separately.

Not covered

  • Windows ROCm was not run.

…ws and no smaller tile is known

#14550 made the per-expert dW kernel the default. Its OutOfResources retry only shrinks the
tile where a shared memory model exists (NVIDIA); on ROCm _card_smem is None, so the fitted
tile equals the failed one and the launch re-raises. The heuristic tile that
UNSLOTH_MOE_DISABLE_AUTOTUNE=1 (or an explicit 4-stage tile) picks, 64x64x256 at 4 stages,
needs ~81,920 B of LDS per-expert on gfx1151 (64 KiB; offline Triton compile), while the
legacy kernel needs 32,768 B, so MoE training there failed where it ran before.

When no smaller tile is known, run the legacy kernel for this launch (zeroed dW unless
accumulating, its own grid and stage count) and record the tile, so later calls skip the
per-expert attempt. NVIDIA tiles the fit can shrink keep the existing path.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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-25T09:52:43.879529Z 5685033 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. What shall we delve into next?

Reviewed commit: 5685033efd

ℹ️ 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 2b3a1b3 into main Oct 25, 2026
3 of 29 checks passed
@danielhanchen
danielhanchen deleted the moe-dw-legacy-fallback-on-oor branch October 25, 2026 10:28
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