Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Format the MoE per-expert dW shared memory files (#15074)

Daniel Han2cc82d2358dd
diff --git a/tests/test_moe_dw_explicit_tile_smem.py b/tests/test_moe_dw_explicit_tile_smem.py
index 4c8b2cf6132..bbadae5a624 100644
--- a/tests/test_moe_dw_explicit_tile_smem.py
+++ b/tests/test_moe_dw_explicit_tile_smem.py
@@ -154,7 +154,6 @@ PIPELINED_SHARED = {
@pytest.mark.parametrize("capability", list(PIPELINED_SHARED))
def test_pipelined_estimate_covers_compiled_shared_memory(gg, capability):
from unsloth.kernels.moe.autotune_cache import _estimate_smem
-
for (M, N, K, stages), shared in PIPELINED_SHARED[capability].items():
tile = types.SimpleNamespace(
BLOCK_SIZE_M = M, BLOCK_SIZE_N = N, BLOCK_SIZE_K = K, num_stages = stages
diff --git a/unsloth/kernels/moe/autotune_cache.py b/unsloth/kernels/moe/autotune_cache.py
index b33337d7bda..f992b7f5de3 100644
--- a/unsloth/kernels/moe/autotune_cache.py
+++ b/unsloth/kernels/moe/autotune_cache.py
@@ -572,8 +572,7 @@ def _fit_config_to_smem(
# Halving a power-of-two tile keeps it dividing N / K.
def fits():
return (
- _estimate_smem(kind, config, act_bytes, weight_bytes, capability, pipelined_dW)
- <= smem
+ _estimate_smem(kind, config, act_bytes, weight_bytes, capability, pipelined_dW) <= smem
)
before = (config.BLOCK_SIZE_M, config.BLOCK_SIZE_N, config.BLOCK_SIZE_K, config.num_stages)
diff --git a/unsloth/kernels/moe/grouped_gemm/interface.py b/unsloth/kernels/moe/grouped_gemm/interface.py
index cd638e9bbab..f6539b9a509 100644
--- a/unsloth/kernels/moe/grouped_gemm/interface.py
+++ b/unsloth/kernels/moe/grouped_gemm/interface.py
@@ -587,7 +587,13 @@ def _card_smem(index):
def _per_expert_dW_stages(
- device, act_bytes, BLOCK_SIZE_M, BLOCK_SIZE_N, BLOCK_SIZE_K, num_stages, pipelined = False
+ device,
+ act_bytes,
+ BLOCK_SIZE_M,
+ BLOCK_SIZE_N,
+ BLOCK_SIZE_K,
+ num_stages,
+ pipelined = False,
):
"""Largest num_stages <= the requested one at which the per-expert dW kernel fits ``device`` shared memory, or
None (use the legacy kernel). Unpermuted operands (``pipelined``) multi-buffer X per stage, so 4 stages of the
@@ -634,7 +640,13 @@ def _per_expert_dW_stages(
def _per_expert_dW_fits(
- device, act_bytes, BLOCK_SIZE_M, BLOCK_SIZE_N, BLOCK_SIZE_K, num_stages, pipelined = False
+ device,
+ act_bytes,
+ BLOCK_SIZE_M,
+ BLOCK_SIZE_N,
+ BLOCK_SIZE_K,
+ num_stages,
+ pipelined = False,
):
"""False when this tile overflows ``device`` shared memory in the per-expert dW kernel at every stage count.
@@ -647,7 +659,9 @@ def _per_expert_dW_fits(
)
-def _fit_dW_tiles_to_card(device, act_bytes, pipelined, BLOCK_SIZE_M, BLOCK_SIZE_N, BLOCK_SIZE_K, num_stages):
+def _fit_dW_tiles_to_card(
+ device, act_bytes, pipelined, BLOCK_SIZE_M, BLOCK_SIZE_N, BLOCK_SIZE_K, num_stages
+):
"""Shrunk copy of a dW tile that raised OutOfResources on ``device`` (an explicit tile sized for a bigger card,
e.g. 128x256x256 on a 64 KiB T4, where the legacy kernel overflows too). Only called after a launch failed, so a
tile the estimate over-counts but that launches is never touched."""