From 1f637a65b933be9582aa28144b0a3c96cc52573a Mon Sep 17 00:00:00 2001 From: McZyWu Date: Mon, 20 Jul 2026 11:05:44 +0800 Subject: [PATCH] [NPU] bugfix for W4A8MoE bias 3D dimension mismatch problem (#31707) --- .../hardware_backend/npu/quantization/moe_methods.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/python/sglang/srt/hardware_backend/npu/quantization/moe_methods.py b/python/sglang/srt/hardware_backend/npu/quantization/moe_methods.py index f9b33e473..e36e1ba36 100644 --- a/python/sglang/srt/hardware_backend/npu/quantization/moe_methods.py +++ b/python/sglang/srt/hardware_backend/npu/quantization/moe_methods.py @@ -419,6 +419,7 @@ class NPUW4A8Int8MoEMethod(_NPUMoEMethodBase): if scale_second is not None: delattr(layer, f"{weight_prefix}_weight_scale_second") delattr(layer, f"{weight_prefix}_weight_offset_second") + self._update_bias(layer, weight_prefix) else: # With clip: simple squeeze + unsqueeze processed_scale = scale.data.squeeze(-1).unsqueeze(1).contiguous() @@ -451,6 +452,16 @@ class NPUW4A8Int8MoEMethod(_NPUMoEMethodBase): if weight_prefix == "w13": self._set_dispatcher_output_dtype(layer, "int8") + @staticmethod + def _update_bias( + layer: torch.nn.Module, + weight_prefix: str, + ) -> None: + scale_bias_name = f"{weight_prefix}_scale_bias" + if hasattr(layer, scale_bias_name): + scale_bias = getattr(layer, scale_bias_name) + scale_bias.data = scale_bias.data.transpose(1, 2).contiguous().sum(dim=1) + def _process_scale( self, weight: torch.Tensor,