diff --git a/python/sglang/srt/layers/quantization/modelopt_quant.py b/python/sglang/srt/layers/quantization/modelopt_quant.py index 9c2786bd22..5108d85e3c 100755 --- a/python/sglang/srt/layers/quantization/modelopt_quant.py +++ b/python/sglang/srt/layers/quantization/modelopt_quant.py @@ -2847,14 +2847,22 @@ class ModelOptNvFp4FusedMoEMethod(FusedMoEMethodBase): else (runner_config.gemm1_clamp_limit or runner_config.swiglu_limit) ) if gemm1_clamp_limit is not None: - # The kernel clamps both raw halves with one limit, which is - # exact for both only when their dequantization scales match. - if not torch.equal(layer.g1_alphas, layer.g1_alphas_up): - logger.warning_once( - "NVFP4 MoE gate/up global scales differ; the TRT-LLM SwiGLU " - "clamp limit is exact for the gate half only. " - "Accuracy may be affected." + # The kernel clamps both raw halves with one limit, so move both onto + # the larger alpha by rescaling the smaller half's E4M3 block scales. + untied = layer.g1_alphas != layer.g1_alphas_up + if untied.any(): + shared_alphas = torch.maximum(layer.g1_alphas, layer.g1_alphas_up) + # Block-scale rows are [up, gate] in the TRT-LLM weight layout. + ratios = torch.stack([layer.g1_alphas_up, layer.g1_alphas], dim=1) + ratios = torch.where( + untied[:, None], ratios / shared_alphas[:, None], 1.0 ) + block_scales = layer.w13_weight_scale.data.unflatten(1, (2, -1)) + block_scales.copy_( + block_scales.float().mul_(ratios[:, :, None, None]) + ) + copy_or_rebind_param(layer, "g1_alphas", shared_alphas) + copy_or_rebind_param(layer, "g1_alphas_up", shared_alphas) copy_or_rebind_param( layer, "gemm1_clamp_limit", diff --git a/test/registered/unit/layers/quantization/test_modelopt_nvfp4_moe_scales.py b/test/registered/unit/layers/quantization/test_modelopt_nvfp4_moe_scales.py index 242f733d3d..8c0177b479 100644 --- a/test/registered/unit/layers/quantization/test_modelopt_nvfp4_moe_scales.py +++ b/test/registered/unit/layers/quantization/test_modelopt_nvfp4_moe_scales.py @@ -205,7 +205,8 @@ class TestNvfp4MoeScaleLoading(CustomTestCase): torch.testing.assert_close(up_alpha, gate_alpha) def test_trtllm_post_load_activation_parameters(self): - """Beta adds one in dequantized up units; SiTU keeps its linear scale.""" + """Beta adds one in dequantized up units and the shared clamp limit is L in + both halves' units without changing their weights; SiTU keeps its scale.""" for activation in ("silu", "situ"): with ( self.subTest(activation=activation), @@ -218,6 +219,10 @@ class TestNvfp4MoeScaleLoading(CustomTestCase): ): layer = _nvfp4_loader_layer() layer.w13_weight_scale_2.data.copy_(torch.tensor([[0.25, 1.0]])) + # TRT-LLM block-scale rows are [up, gate]. + layer.w13_weight_scale.data.copy_( + torch.tensor([2.0, 1.0]).repeat_interleave(64)[None, :, None] + ) config = layer.moe_runner_config config.activation = activation config.gemm1_alpha = 1.702 if activation == "silu" else 4.0 @@ -231,13 +236,21 @@ class TestNvfp4MoeScaleLoading(CustomTestCase): torch.testing.assert_close( layer.gemm1_alpha, torch.tensor([config.gemm1_alpha]) ) + up_blocks, gate_blocks = layer.w13_weight_scale[0].float().chunk(2) + torch.testing.assert_close( + up_blocks * layer.g1_alphas_up, torch.full((64, 4), 2.0 * 0.5) + ) + torch.testing.assert_close( + gate_blocks * layer.g1_alphas, torch.full((64, 4), 1.0 * 0.125) + ) if activation == "silu": torch.testing.assert_close( layer.gemm1_beta * layer.g1_alphas_up, torch.ones(1) ) - torch.testing.assert_close( - layer.gemm1_clamp_limit * layer.g1_alphas, torch.tensor([7.0]) - ) + for alphas in (layer.g1_alphas, layer.g1_alphas_up): + torch.testing.assert_close( + layer.gemm1_clamp_limit * alphas, torch.tensor([7.0]) + ) else: self.assertFalse(hasattr(layer, "gemm1_clamp_limit")) torch.testing.assert_close(layer.gemm1_beta, torch.tensor([25.0]))