Skip to content

MoE heuristic dW config: 128-wide N tile for gpt-oss K = 2880 - #15505

Merged
danielhanchen merged 11 commits into
mainfrom
moe-dw-config-tune-v2
Oct 28, 2026
Merged

danielhanchen merged 11 commits into
mainfrom
moe-dw-config-tune-v2

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

With UNSLOTH_MOE_DISABLE_AUTOTUNE=1, the MoE grouped GEMM uses heuristic kernel configs. For gpt-oss (K = 2880), the dW tile is fitted down to 64 x 64 x 64 with 4 stages, because K only leaves BLOCK_SIZE_K 64. dW tiles N x K and reduces over tokens, so a 64 x 64 output tile leaves the kernel starved.

Change

_get_heuristic_configs (unsloth/kernels/moe/autotune_cache.py) handles this case: when the fitted dW config has BLOCK_SIZE_K == 64 and 128 divides N, it uses a 128-wide N tile at 3 stages, as long as the dW output is at least N x K = 768 x 1024 and the GPU is NVIDIA sm80 or newer.

  • T4 and ROCm keep the old tile: on T4 (sm75) the wide tile was 7-9x slower in every cell measured (fp16, torch 2.14.1, Kaggle; e.g. K 2880 x N 1024 x 4096 tokens, per-expert 51.6 ms -> 458 ms). ROCm is unmeasured.
  • Small outputs: below that size the tile is left as is. Halving the program count on a small output was slower for the legacy kernel (table below).
  • Unchanged: every other shape, and the autotuned path, which is the default.
  • Small cards: the shared-memory fit (_fit_configs_to_smem) still runs afterwards, so the tile shrinks to fit there as before.

Results

gpt-oss-20b gate_up dW: E 32, top-k 4, N 5760, K 2880, bf16, permute_x. B200, CUDA events, 3 interleaved reps per arm; the main arm ran twice per rep for the A/A spread.

dW kernel tokens main ms this PR ms speedup A/A spread
per-expert (default dW kernel) 512 0.824 0.615 1.34x 5.5%
per-expert 4096 1.894 1.372 1.38x 2.1%
legacy 512 2.086 1.498 1.39x 0.1%
legacy 4096 11.555 6.557 1.76x 0.2%

The max error against an fp64 reference is the same in both arms. The card was shared, so treat these timings as rough.

Shapes where BLOCK_SIZE_K is 64 (old tile vs 128-wide; B200, bf16, E 32, top-k 4; CUDA-graph replays, 7 interleaved A/A/B rounds, A/A spread under 1% except two cells at 2.4% and 4.6%; outputs bit-equal). Speedup of the 128-wide tile, for the per-expert / legacy kernel:

N x K 128 tokens 1024 tokens 4096 tokens
1024 x 192 1.20x / 0.77x 1.17x / 0.84x 1.21x / 0.88x
1024 x 576 1.30x / 0.83x 1.33x / 0.83x 1.29x / 0.95x
1024 x 960 1.50x / 1.31x 1.39x / 1.38x 1.27x / 1.70x
5760 x 192 1.36x / 1.37x 1.31x / 1.50x 1.26x / 1.76x
5760 x 2880 1.50x / 1.33x 1.38x / 1.46x 1.33x / 1.77x

The first two rows (N x K below 768 x 1024) keep the old tile; every other shape tested (K 192 to 2880, N 1024 and 5760) was faster on both kernels.

Other GPUs (CUDA-graph replays, interleaved A/A/B, outputs bit-equal in every cell, torch 2.14.1):

  • A100 40GB (Colab, bf16): above the floor the wide tile is 0.997-1.14x on the per-expert kernel and 1.05-1.65x on the legacy kernel. Two per-expert cells were under 1% slower, just outside a very tight A/A spread.
  • T4 (Kaggle, fp16): 0.10-0.16x in all 42 cells that finished, hence the sm80+ gate.
  • L4 (Colab, sm89, bf16): above the floor the default per-expert kernel is 1.03-1.20x in all 18 cells. The opt-in legacy kernel is 0.96-1.41x: one cell, K 960 x N 1024 x 128 tokens, was 3.8% slower. The tile tests pass (69 of 69).
  • Tests: the tile tests pass on T4 and A100 (63 of 63).

Tests

  • tests/test_moe_fallback_tile_dims.py gains the gpt-oss K = 2880 case and the small-output cases (N 1024 x K 192 / 576 keep the old tile, N 1024 x K 960 widens). Run against main's code, it fails ((64, 64, 64, 8, 4) != (64, 128, 64, 8, 3)); here, 61 passed. The MoE dW GPU suites (tile dims, ROCm LDS fallback, dW accumulate, per-expert dW) give 227 passed on B200.
  • Unchanged on main and here:
    • fallback SMEM fit, operand device and cache hit / validate;
    • mixed TMA device and autotune under compile;
    • a wider grouped GEMM / dW suite (10 files, 345 tests, including per-expert dW, dW accumulate and fallback SMEM).

Not covered

  • H100 (sm90) and A10 / RTX 30 (sm86) are enabled by the sm80+ gate but not timed (Colab gave no H100); A100 (sm80), L4 (sm89) and B200 (sm100) all gain.
  • Timings on L4, A100 and T4 for the per-expert kernel; the small-card fit is checked through the mocked SMEM tests.
  • An end-to-end gpt-oss training step with UNSLOTH_MOE_DISABLE_AUTOTUNE=1; the numbers above are kernel-level.

With UNSLOTH_MOE_DISABLE_AUTOTUNE=1 the heuristic dW tile for gpt-oss (K = 2880)
fit down to 64 x 64 x 64. 64 x 128 x 64 at 3 stages, used when 128 divides N,
makes gpt-oss-20b gate_up dW 1.34-1.38x faster on the per-expert kernel and
1.39-1.76x on the legacy one (B200, 512 / 4096 tokens), with the same error
against fp64. Every other shape keeps its config, and the shared memory fit
still shrinks the tile on small cards.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 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-27T11:25:32.376253Z ad7024f 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 84a1d00e63

ℹ️ 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 added a commit that referenced this pull request Oct 27, 2026
…reaches (#15515)

`tests/test_optimized_precision_conflicts.py` fails 19 of 19 on main
(`Repo tests (CPU, rest)`, e.g. job 122034866725 on #15505):

```
NameError: name '_agree_fast_inference_across_ranks' is not defined
```

The fixture compiles `FastLanguageModel.from_pretrained` out of
`loader.py` together with a fixed list of three module helpers. #15390
added a call from `from_pretrained` to a new module helper,
`_agree_fast_inference_across_ranks`, which was not on that list, so
every case now stops on the NameError before reaching the precision
checks this file is about. The product code is fine (the helper returns
early when `fast_inference` was not requested); the harness list was
stale.

Instead of adding one more name, the fixture now lifts every `loader.py`
top-level function (and literal constant such as `_VLLM_ENGINE_KWARGS`)
that `from_pretrained` reaches by name, transitively, skipping anything
the fixture already stubs in its namespace. A future helper is picked up
automatically, and the stubs keep precedence.

Verification (local, CPU):
- main: 19 failed; this branch: 19 passed.
- Sabotage: making `_agree_fast_inference_across_ranks` raise fails all
19, so the real helper runs on every case.
- Stub precedence: redefining a stubbed name
(`_raise_if_modeling_ignores_config`) in `loader.py` to raise still
passes 19, so the lift never overrides a stub.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 89066ed284

ℹ️ 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 added a commit that referenced this pull request Oct 27, 2026
… device (#15516)

`tests/python/test_accelerator_skip_guards.py::test_no_skip_guard_reads_a_spoofable_accelerator_probe`
fails on main (`Repo tests (CPU, studio)`, e.g. job 122034866848 on
#15505):

```
these skip guards read a probe tests/_zoo_aggressive_cuda_spoof.py patches to True process-wide ...
    test_mxfp4_packed_low_memory.py:428: torch.cuda.is_available()
```

#15376 added `test_chunked_dx_stays_inside_its_own_budget` with
`@pytest.mark.skipif(not torch.cuda.is_available(), ...)`. That probe is
the one the CUDA spoof patches to True, so the guard can un-skip on a
CPU-only runner when the test shares a session with
`tests/version_compat` or `tests/vllm_compat`. The rest of the file
already gates on `has_real_cuda()` through its module `pytestmark`; this
switches the new test's guard to the same probe. The guard test is right
and the kernel change is unaffected; only the new test's skip condition
was wrong.

Verification (local):
- `tests/python/test_accelerator_skip_guards.py`: main 1 failed, 23
passed; this branch 24 passed.
- `tests/test_mxfp4_packed_low_memory.py` on a CPU-only box: all 46
skip, including the new test.
- On a real GPU `has_real_cuda()` is True, so the test still runs where
it is meant to.
The legacy dW kernel was 5-23% slower with the 128-wide tile at N 1024 x K <= 576
(half the programs on a small output). Only widen at N x K >= 768 * 1024.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 3cb5adb927

ℹ️ 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

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: ad7024fd0a

ℹ️ 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 self-assigned this Oct 27, 2026
@danielhanchen
danielhanchen merged commit 3a360b3 into main Oct 28, 2026
3 of 34 checks passed
@danielhanchen
danielhanchen deleted the moe-dw-config-tune-v2 branch October 28, 2026 15:58
danielhanchen added a commit that referenced this pull request Nov 1, 2026
… configs (#15928)

Follow-up to #15505.

#15505 widens the heuristic backward dW tile to 64 x 128 x 64 at 3
stages when K leaves `BLOCK_SIZE_K` 64 (gpt-oss gate_up). The condition
does not look at the activation dtype. On a B200 the wide tile is slower
for float32 activations, though it is faster for bf16:

| dW N x K, float32 X / dY / dW | tokens | before #15505 (ms) | main
(ms) | change |
|---|---|---|---|---|
| 1024 x 960 | 128 | 0.0906 | 0.1026 | 13.2% slower |
| 1024 x 960 | 4096 | 0.6449 | 0.7412 | 14.9% slower |
| 5760 x 2880 (gpt-oss gate_up) | 128 | 1.4518 | 1.6368 | 12.7% slower |
| 5760 x 2880 (gpt-oss gate_up) | 4096 | 10.709 | 12.309 | 14.9% slower
|

These are B200 numbers from a recheck reviewer: torch 2.14.1, Triton
3.8, interleaved, A/A spread under 0.14%, and bitwise equal outputs. The
same runs reproduced the bf16 gain of 1.35x - 1.50x.

float32 reaches this path with float32 expert weights,
`UNSLOTH_FORCE_FLOAT32=1`, or `UNSLOTH_MOE_TRITON_FP32=1`. These
heuristic configs are used under `UNSLOTH_MOE_DISABLE_AUTOTUNE=1`. The
autotuned default and the autotune-failure fallback are unchanged.

## Change

Widening now skips float32 activations on compute capability 10.x (B200
/ sm100), the only architecture where the wide tile was measured slower
for float32. bf16 / fp16, callers that pass no dtype, and float32 on
every other architecture keep the wide tile.

## Tests

- New `test_heuristic_dw_widens_only_16bit_activations[bf16, fp16,
fp32]` checks gpt-oss gate_up and N 1024 x K 960. The fp32 case fails on
main and passes here.
- `tests/test_moe*.py`: 337 passed, 46 skipped (CUDA only), CPU torch
2.14.1.



## Other architectures

On L4 (sm89) and RTX PRO 6000 Blackwell (sm120) the wide tile is faster
for float32 too, so the float32 exclusion is limited to sm100. fp32 dW
kernel time with the 64-wide tile vs main's 128-wide tile (CUDA graphs,
interleaved A / A / B over 9 rounds, medians, torch 2.11.0+cu130, Triton
3.6.0; outputs bitwise equal):

| cell (fp32), dW kernel | L4: 64-wide vs main | RTX PRO 6000: 64-wide
vs main |
|---|---|---|
| 1024 x 960, T 128, legacy | -5.3% | +9.5% |
| 1024 x 960, T 2048, legacy | +7.4% | +22.5% |
| 1024 x 960, T 2048, auto | +17.9% | +21.5% |
| 1024 x 960, T 8192, legacy | +17.2% | +39.4% |
| 5760 x 2880, T 2048, legacy | +22.5% | +32.5% |
| 5760 x 2880, T 4096, legacy | +26.2% | +43.4% |

With this change both cards pick main's tile for float32 (fix vs main
within the A / A band in every cell).
`test_heuristic_dw_widens_all_dtypes_on_sm89[bf16, fp16, fp32]` pins it;
the fp32 case fails on the previous head. B200 itself was not
re-measured here, and A100 / H100 were not measured.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant