From c0df52be2d967d00ead51a232a2af1cee206dc86 Mon Sep 17 00:00:00 2001 From: Tuan Nguyen Gia Date: Wed, 7 Oct 2026 06:20:40 +0300 Subject: [PATCH] [AMD] Fix Quark W4A4 MXFP4 MoE clamped-SwiGLU on the AITER path (#34540) Co-authored-by: Cursor --- .../quark/schemes/quark_w4a4_mxfp4_moe.py | 7 +- .../quantization/test_quark_w4a4_mxfp4_moe.py | 138 ++++++++++++++++++ 2 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 test/registered/unit/layers/quantization/test_quark_w4a4_mxfp4_moe.py diff --git a/python/sglang/srt/layers/quantization/quark/schemes/quark_w4a4_mxfp4_moe.py b/python/sglang/srt/layers/quantization/quark/schemes/quark_w4a4_mxfp4_moe.py index b2dfbe7845..e3efb0833f 100644 --- a/python/sglang/srt/layers/quantization/quark/schemes/quark_w4a4_mxfp4_moe.py +++ b/python/sglang/srt/layers/quantization/quark/schemes/quark_w4a4_mxfp4_moe.py @@ -980,6 +980,7 @@ class QuarkW4A4MXFp4MoE(QuarkMoEScheme): from sglang.srt.layers.moe.moe_runner.aiter import ( AiterMoeQuantInfo, AiterQuantType, + aiter_swiglu_oai_limit, ) if hasattr(torch, "float4_e2m1fn_x2"): @@ -1015,7 +1016,11 @@ class QuarkW4A4MXFp4MoE(QuarkMoEScheme): expert_mask=layer.dispatcher.expert_mask_gpu, hidden_pad=getattr(layer, "hidden_pad", 0), intermediate_pad=getattr(layer, "intermediate_pad", 0), - swiglu_limit=self.moe_runner_config.swiglu_limit or 0.0, + swiglu_limit=( + aiter_swiglu_oai_limit(self.moe_runner_config) + or self.moe_runner_config.swiglu_limit + or 0.0 + ), fused_moe_kwargs=_fused_moe_kwargs, ) return self.runner.run(dispatch_output, quant_info) diff --git a/test/registered/unit/layers/quantization/test_quark_w4a4_mxfp4_moe.py b/test/registered/unit/layers/quantization/test_quark_w4a4_mxfp4_moe.py new file mode 100644 index 0000000000..38bb525f75 --- /dev/null +++ b/test/registered/unit/layers/quantization/test_quark_w4a4_mxfp4_moe.py @@ -0,0 +1,138 @@ +"""Unit tests for the Quark W4A4 MXFP4 MoE clamped-SwiGLU plumbing. + +CPU-only, no model loading and no aiter/ROCm dependency: they exercise the +pure-Python control flow in ``QuarkW4A4MXFp4MoE.create_moe_runner`` and +``apply_weights`` that decides whether the AITER MoE runner is driven on the +clamped-SwiGLU path (``activation="swiglu"`` + ``swiglu_limit``) or the plain +SiLU path. See the regression these guard against: clamped-SwiGLU MXFP4 +checkpoints (e.g. MiniMax-M3) declare ``activation="silu"`` + a +``gemm1_clamp_limit``, and without this plumbing the experts silently run plain +SiLU and emit garbage. +""" + +import sys +from types import SimpleNamespace + +import pytest +import torch + +import sglang.srt.layers.moe.utils as moe_utils +import sglang.srt.layers.quantization.quark.schemes.quark_w4a4_mxfp4_moe as quark_moe +from sglang.srt.layers.moe import MoeRunnerBackend, MoeRunnerConfig +from sglang.srt.layers.quantization.quark.schemes.quark_w4a4_mxfp4_moe import ( + QuarkW4A4MXFp4MoE, +) +from sglang.test.ci.ci_register import register_cpu_ci + +register_cpu_ci(est_time=5, suite="base-a-test-cpu") + + +def _make_scheme() -> QuarkW4A4MXFp4MoE: + # is_checkpoint_mxfp4_serialized=True skips the gfx95 hardware check. + return QuarkW4A4MXFp4MoE( + weight_config={"qscheme": "per_group"}, + input_config={"qscheme": "per_group", "is_dynamic": True}, + ) + + +def _force_aiter_runner(monkeypatch): + """Make ``create_moe_runner`` take the AITER branch and capture its config.""" + captured = {} + + def fake_moe_runner(backend, config): + captured["backend"] = backend + captured["config"] = config + return object() + + monkeypatch.setattr(quark_moe, "MoeRunner", fake_moe_runner) + monkeypatch.setattr( + moe_utils, "get_moe_runner_backend", lambda: MoeRunnerBackend.AITER + ) + return captured + + +def _fake_layer(): + # dtype uint8 so we never hit the float4_e2m1fn_x2 view branch (also guarded + # by delattr in the apply_weights tests below). + return SimpleNamespace( + w13_weight=torch.zeros(1, dtype=torch.uint8), + w2_weight=torch.zeros(1, dtype=torch.uint8), + w13_weight_scale=torch.zeros(1, dtype=torch.uint8), + w2_weight_scale=torch.zeros(1, dtype=torch.uint8), + dispatcher=SimpleNamespace(expert_mask_gpu=torch.zeros(1)), + ) + + +def _capturing_runner(store): + class _Runner: + def run(self, dispatch_output, quant_info): + store["quant_info"] = quant_info + return "ran" + + return _Runner() + + +def test_create_moe_runner_keeps_silu_when_clamped(monkeypatch): + captured = _force_aiter_runner(monkeypatch) + scheme = _make_scheme() + + cfg = MoeRunnerConfig( + activation="silu", + gemm1_alpha=1.702, + gemm1_beta=1.0, + gemm1_clamp_limit=7.0, + ) + scheme.create_moe_runner(layer=None, moe_runner_config=cfg) + + # The runner selects SwiGLU from this combo. The scheme must not rewrite it. + assert captured["config"].activation == "silu" + assert captured["config"] is cfg + + +def test_create_moe_runner_keeps_silu_without_clamp(monkeypatch): + captured = _force_aiter_runner(monkeypatch) + scheme = _make_scheme() + + cfg = MoeRunnerConfig(activation="silu") # no clamp + scheme.create_moe_runner(layer=None, moe_runner_config=cfg) + + assert captured["config"].activation == "silu" + assert captured["config"] is cfg + + +def test_apply_weights_forwards_swiglu_limit_when_clamped(monkeypatch): + monkeypatch.delattr(torch, "float4_e2m1fn_x2", raising=False) + scheme = _make_scheme() + monkeypatch.setattr( + "sglang.srt.layers.moe.moe_runner.aiter.is_gfx95_supported", + lambda: True, + ) + scheme.moe_runner_config = MoeRunnerConfig( + activation="silu", + gemm1_alpha=1.702, + gemm1_beta=1.0, + gemm1_clamp_limit=7.0, + ) + store = {} + scheme.runner = _capturing_runner(store) + + result = scheme.apply_weights(_fake_layer(), dispatch_output=object()) + + assert result == "ran" + assert store["quant_info"].swiglu_limit == 7.0 + + +def test_apply_weights_defaults_swiglu_limit_zero_without_clamp(monkeypatch): + monkeypatch.delattr(torch, "float4_e2m1fn_x2", raising=False) + scheme = _make_scheme() + scheme.moe_runner_config = MoeRunnerConfig(activation="silu") # no clamp + store = {} + scheme.runner = _capturing_runner(store) + + scheme.apply_weights(_fake_layer(), dispatch_output=object()) + + assert store["quant_info"].swiglu_limit == 0.0 + + +if __name__ == "__main__": + sys.exit(pytest.main([__file__, "-v"]))