Skip to content

MoE per-expert dW: count pipelined shared memory, fit tiles that overflow the card - #15070

Merged
danielhanchen merged 1 commit into
mainfrom
moe-dw-smem-estimate
Oct 24, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
moe-dw-smem-estimate

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

The per-expert MoE dW kernel (the default since #14550) can raise OutOfResources on cards with less shared memory than an H100. #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

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.

…flow the card

The per-expert dW kernel software pipelines its M loop when the operands are
not permuted, so the heuristic 64x64x256 tile at 4 stages needs 106,496 B on
sm_80-class cards (L4 limit 101,376) while the estimate assumed one buffer.
Unpermuted launches now drop stages until the tile fits. A launch that still
raises OutOfResources (an explicit tile sized for a bigger card, e.g. on a
64 KiB T4, where the legacy kernel overflows too) retries once with a tile
fitted to the card. The dims tests no longer depend on the card's smem fit.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 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-24T03:49:54.623442Z e339d1b 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. Hooray!

Reviewed commit: e339d1b9b1

ℹ️ 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 5f3d662 into main Oct 24, 2026
6 of 30 checks passed
@danielhanchen
danielhanchen deleted the moe-dw-smem-estimate branch October 24, 2026 03:56
danielhanchen added a commit that referenced this pull request Oct 24, 2026
#15070 landed three files that the repo's pre-commit `ruff format` hook
(ruff 0.6.9 plus the kwarg spacing pass in `scripts/run_ruff_format.py`)
rewrites:

- `tests/test_moe_dw_explicit_tile_smem.py`: blank line after a
function-local import
- `unsloth/kernels/moe/autotune_cache.py`: a comparison wrapped onto two
lines that fits on one
- `unsloth/kernels/moe/grouped_gemm/interface.py`: two signatures over
the line length

pre-commit.ci checks the files a PR touches, so any later PR editing one
of these goes red on lines it did not write. This is the hook's own
output and nothing else. `tests/test_moe_dw_explicit_tile_smem.py` still
passes (15 passed, 2 skipped on a CPU-only run).
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