Repository navigation
MoE fallback kernel configs: fit the device shared memory for float32 operands - #14576
Conversation
…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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
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]
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…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).
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 withUNSLOTH_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 withOutOfResources. Float32 weights also overflow H100 / B200 (294,912 B).Changes
_estimate_smemmodels 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 compiledmetadata.sharedfor sm_75 to sm_120 on Triton 3.6 to 3.8._fit_config_to_smemsteps 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 whenweight_dtypediffers fromdtype.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).UNSLOTH_MOE_DISABLE_AUTOTUNE=1: before,OutOfResources: Required: 131072, Hardware limit: 101376. After, it trains (561 ms/step). The same holds withUNSLOTH_MOE_TRITON_FP32=1added.Not covered