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.

MoE: keep the fitted dW tile for float32 activations in the heuristic configs (#15928)

Daniel Hand011e08d1f79
diff --git a/tests/test_moe_fallback_tile_dims.py b/tests/test_moe_fallback_tile_dims.py
index 653c6204933..309c538b6c3 100644
--- a/tests/test_moe_fallback_tile_dims.py
+++ b/tests/test_moe_fallback_tile_dims.py
@@ -290,3 +290,26 @@ def test_wide_dw_tile_only_on_sm80_plus_nvidia(ac, monkeypatch, available, hip,
def test_t4_keeps_the_fitted_dw_tile(ac, no_smem_fit, monkeypatch):
monkeypatch.setattr(ac, "_wide_dw_device_ok", lambda device = None: False)
assert _tiles(ac._get_heuristic_configs(n_dim = 5760, k_dim = 2880)[2:]) == ((64, 64, 64, 8, 4),)
+
+
+ "dtype, wide", [(torch.bfloat16, True), (torch.float16, True), (torch.float32, False)]
+)
+def test_heuristic_dw_widens_only_16bit_activations_on_sm100(ac, monkeypatch, dtype, wide):
+ # float32 dW is 13-15% slower on the wide tile (B200, gpt-oss gate_up and N 1024 x K 960).
+ monkeypatch.setattr(ac, "_wide_dw_device_ok", lambda device = None: True)
+ monkeypatch.setattr(torch.cuda, "get_device_capability", lambda *a, **k: (10, 0))
+ for n_dim, k_dim in ((5760, 2880), (1024, 960)):
+ dw = ac._get_heuristic_configs(n_dim = n_dim, k_dim = k_dim, dtype = dtype)[2]
+ assert (dw.BLOCK_SIZE_N == 128) is wide
+
+
[email protected]("dtype", [torch.bfloat16, torch.float16, torch.float32])
+def test_heuristic_dw_widens_all_dtypes_on_sm89(ac, monkeypatch, dtype):
+ # L4 (sm89): float32 dW is 6-18% faster on the wide tile, so main's choice stays.
+ monkeypatch.setattr(ac, "_wide_dw_device_ok", lambda device = None: True)
+ monkeypatch.setattr(torch.cuda, "get_device_capability", lambda *a, **k: (8, 9))
+ for n_dim, k_dim in ((5760, 2880), (1024, 960)):
+ assert (
+ ac._get_heuristic_configs(n_dim = n_dim, k_dim = k_dim, dtype = dtype)[2].BLOCK_SIZE_N == 128
+ )
diff --git a/unsloth/kernels/moe/autotune_cache.py b/unsloth/kernels/moe/autotune_cache.py
index 4e112f8b745..145fb5f82f9 100644
--- a/unsloth/kernels/moe/autotune_cache.py
+++ b/unsloth/kernels/moe/autotune_cache.py
@@ -680,6 +680,18 @@ def _wide_dw_device_ok(device = None) -> bool:
return False
+# Compute capability majors where float32 dW was measured slower on the 128-wide tile: B200 (sm100) 13-15%.
+# L4 (sm89) is 6-18% faster on it, so other architectures keep the wide tile for float32 too.
+_FP32_NARROW_DW_MAJORS = (10,)
+
+
+def _fp32_narrow_dw_device(device = None) -> bool:
+ try:
+ return torch.cuda.get_device_capability(device)[0] in _FP32_NARROW_DW_MAJORS
+ except Exception:
+ return False
+
+
def _get_heuristic_configs(
n_dim: Optional[int] = None,
k_dim: Optional[int] = None,
@@ -742,6 +754,7 @@ def _get_heuristic_configs(
# output tile starves the kernel; a 128-wide N tile at 3 stages is faster at the same error. Other K, an N
# that 128 does not divide, or an N x K output too small to fill the card at half the programs (the legacy
# kernel was 5-23% slower at N 1024 x K <= 576) keep the fitted tile. The SMEM fit below still shrinks it.
+ # float32 activations keep it on sm100, where the wide tile is 13-15% slower (B200).
config_bwd_dw = configs[2]
if (
config_bwd_dw.BLOCK_SIZE_K == 64
@@ -750,6 +763,7 @@ def _get_heuristic_configs(
and n_dim % 128 == 0
and n_dim * k_dim >= _WIDE_DW_MIN_OUTPUT
and _wide_dw_device_ok(device)
+ and not (dtype == torch.float32 and _fp32_narrow_dw_device(device))
):
config_bwd_dw.BLOCK_SIZE_N = 128
config_bwd_dw.num_stages = 3