Skip to content

MoE fallback kernel configs: fit the device shared memory for float32 operands - #14576

Merged
danielhanchen merged 3 commits into
mainfrom
dh/s2-w20b-moe-fallback
Oct 21, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
dh/s2-w20b-moe-fallback

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

The heuristic (UNSLOTH_MOE_DISABLE_AUTOTUNE=1) and default (tuning failed) grouped GEMM configs were sized for 16-bit operands. Triton keeps each tile in shared memory in the dtype it was loaded in, so a float32 X / dY (gate_up in full finetuning, or every GEMM with UNSLOTH_MOE_TRITON_FP32=1) doubles it. On an L4 (101,376 B per block) the heuristic gate_up forward needs 131,072 B and the default dX 163,840 B, and training stops with OutOfResources. Float32 weights also overflow H100 / B200 (294,912 B).

Changes

  • _estimate_smem models each kernel's shared memory from tile sizes, stages and operand element sizes, per architecture family (sm_7x, sm_8x / sm_12x, sm_9x / sm_10x). It was checked against compiled metadata.shared for sm_75 to sm_120 on Triton 3.6 to 3.8.
  • _fit_config_to_smem steps the forward, dX and dW configs down until they fit the current device. The order is: stages to 3, the loop tile to 32, stages to 2, the largest tile to 16, then one stage. It replaces the one-entry step-down table, and gives the same result for that entry.
  • get_or_autotune_moe_kernels(..., weight_dtype=None): the fallbacks are sized for the activation and weight dtypes, and gate_up is tuned on the weight dtype it actually reads (it was tuned on float32 W under float32 activations). The cache key changes only when weight_dtype differs from dtype.
  • 16-bit configs are unchanged on every GPU class (T4, L4, RTX 4090, A10, A100, H100, B200, sm_120).

Testing

  • tests/test_moe_fallback_smem.py: picks for T4 / L4 / 4090 / A10 / A100 / H100 / B200 / sm_120 x bf16 / fp16 / fp32 activations x weight dtypes fit the device. 16-bit picks are identical to before. A sample is recompiled for the real targets on CPU and compared with the estimate. It passes on torch 2.10 / 2.13 / 2.14 (Triton 3.6 / 3.7 / 3.8).
  • L4, tiny Qwen3-MoE full finetuning with UNSLOTH_MOE_DISABLE_AUTOTUNE=1: before, OutOfResources: Required: 131072, Hardware limit: 101376. After, it trains (561 ms/step). The same holds with UNSLOTH_MOE_TRITON_FP32=1 added.
  • L4 LoRA bf16 step time, 3 interleaved repeats: 278.9 ms (spread 4.7) before, 279.8 ms (spread 4.9) after.
  • B200: grouped GEMM forward / dX / dW with the fallback configs against an fp64 reference. float32 weights crashed before and run now. Errors are unchanged where both run.

Not covered

  • Runtime on T4, A100, H100, RTX 4090, A10 and sm_120 (cross-compiled shared memory only); Triton 3.2 to 3.5; speed of the stepped-down fallback configs; ROCm, Windows, multi-GPU.

…rand dtypes

The heuristic (UNSLOTH_MOE_DISABLE_AUTOTUNE=1) and default (tuning failed) grouped GEMM
configs assumed 16-bit operands. With float32 X / dY the default dX needs 163840 B of shared
memory and raised OutOfResources on an L4 (101376 B); float32 weights overflow H100 / B200 too.

Estimate each kernel's shared memory from the tile sizes, stages and operand element sizes
(checked against compiled metadata.shared for sm_75 to sm_120 on Triton 3.6 to 3.8) and step
the forward, dX and dW configs down until they fit the current device. 16-bit configs are
unchanged on every GPU class. get_or_autotune_moe_kernels takes weight_dtype so gate_up is
tuned and sized with the bf16 experts it reads under float32 activations.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 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-21T22:31:32.464679Z 52b169f Manual request
🔒 Security Review ⚠️ Failed 2026-10-21T22:26:26.916690Z
We couldn’t complete this review. Comment @codex security review to try again.
a257e00 PR opened
ℹ️ 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

The account paying for this security review has reached its Codex usage limits. The payer can check the Codex usage dashboard. For personal accounts, using credits requires enabling “Use credits for security reviews” in Code review settings. If you do not manage the paying account, contact this repository's admins.

[skip ci]
@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: 52b169f4d5

ℹ️ 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 0391814 into main Oct 21, 2026
@danielhanchen
danielhanchen deleted the dh/s2-w20b-moe-fallback branch October 21, 2026 23:50
danielhanchen added a commit to unslothai/unsloth-zoo that referenced this pull request Oct 21, 2026
…ner (#1941)

### Summary
With float32 activations and bf16 experts, `forward_triton_grouped_gemm`
asked unsloth to tune (and size the fallback configs for) gate_up as if
the weights were float32. It now passes `weight_dtype` when it differs
from the activations and the installed unsloth accepts it. Older unsloth
is called exactly as before.

Depends on unslothai/unsloth#14576, which adds `weight_dtype` to
`get_or_autotune_moe_kernels`; without it this is a no-op.

### Testing
On L4 with `UNSLOTH_MOE_TRITON_FP32=1`, gate_up is now tuned on bf16 W
(different configs from before). Step time is unchanged within one run.
LoRA bf16 is unaffected (weights and activations share a dtype, so
nothing is passed).
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