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 grouped GEMM: skip kernel-info logging work unless debug logging is on (#14787)

Daniel Han390b866546c2
diff --git a/tests/test_grouped_gemm_log_kernel_info.py b/tests/test_grouped_gemm_log_kernel_info.py
new file mode 100644
index 00000000000..f5f2e166804
--- /dev/null
+++ b/tests/test_grouped_gemm_log_kernel_info.py
@@ -0,0 +1,52 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""log_kernel_info runs after every grouped GEMM launch: with debug logging off it must not
+read or format the compiled kernel's metadata (that cost ~12 us per launch)."""
+
+import logging
+import sys
+from pathlib import Path
+
+import pytest
+
+ROOT = Path(__file__).resolve().parents[1]
+sys.path.insert(0, str(ROOT))
+
+torch = pytest.importorskip("torch")
+pytest.importorskip("triton")
+
+
[email protected](scope = "module")
+def interface():
+ try:
+ from unsloth.kernels.moe.grouped_gemm import interface
+ except Exception as exc:
+ if torch.cuda.is_available():
+ raise # a real import regression, not an unsupported host
+ pytest.skip(f"grouped_gemm needs a GPU build: {exc}")
+ return interface
+
+
+class _Untouchable:
+ def __getattr__(self, name):
+ raise AssertionError(f"read compiled_kernel.{name} with debug logging off")
+
+
+def test_no_metadata_read_without_debug_logging(interface):
+ level = interface.logger.level
+ interface.logger.setLevel(logging.WARNING)
+ try:
+ interface.log_kernel_info(_Untouchable(), best_config = _Untouchable())
+ finally:
+ interface.logger.setLevel(level)
+
+
+def test_debug_logging_still_reports(interface, monkeypatch, caplog):
+ class Kernel:
+ name, n_regs, n_spills, metadata = "k", 32, 0, "meta"
+
+ with caplog.at_level(logging.DEBUG, logger = interface.logger.name):
+ interface.log_kernel_info(Kernel(), best_config = "cfg")
+ assert "k: n_regs=32 n_spills=0 metadata=meta" in caplog.text
+ assert "k autotuned best_config: cfg" in caplog.text
diff --git a/unsloth/kernels/moe/grouped_gemm/interface.py b/unsloth/kernels/moe/grouped_gemm/interface.py
index 372304c0bbd..d7291a68168 100644
--- a/unsloth/kernels/moe/grouped_gemm/interface.py
+++ b/unsloth/kernels/moe/grouped_gemm/interface.py
@@ -217,6 +217,9 @@ def get_per_device_per_stream_alloc_fn(device):
def log_kernel_info(
compiled_kernel: triton.compiler.CompiledKernel, best_config: triton.Config = None
):
+ # Runs after every launch: formatting the metadata costs several us per call even when nothing logs.
+ if not logger.isEnabledFor(logging.DEBUG):
+ return
kernel_name = compiled_kernel.name
nregs = compiled_kernel.n_regs
nspills = compiled_kernel.n_spills