From f3e9336dcbd695e635736aab7963f4c43903d488 Mon Sep 17 00:00:00 2001 From: fzyzcjy <5236035+fzyzcjy@users.noreply.github.com> Date: Mon, 17 Nov 2025 15:55:26 +0800 Subject: [PATCH] Support weight update for blackwell DeepGEMM (#13324) --- python/sglang/srt/models/deepseek_v2.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/python/sglang/srt/models/deepseek_v2.py b/python/sglang/srt/models/deepseek_v2.py index 2749d0865..3193a1a07 100644 --- a/python/sglang/srt/models/deepseek_v2.py +++ b/python/sglang/srt/models/deepseek_v2.py @@ -105,6 +105,7 @@ from sglang.srt.layers.quantization.fp8_utils import ( block_quant_dequant, block_quant_to_tensor_quant, channel_quant_to_tensor_quant, + inverse_transform_scale_ue8m0, normalize_e4m3fn_to_e4m3fnuz, quant_weight_ue8m0, requant_weight_ue8m0_inplace, @@ -3270,6 +3271,8 @@ class DeepseekV2ForCausalLM(nn.Module): } ) self.capture_aux_hidden_states = False + self._executed_weight_requant_ue8m0 = False + self.nsa_enable_prefill_cp = is_nsa_enable_prefill_cp() if self.nsa_enable_prefill_cp: self.cp_rank = get_attention_tp_rank() @@ -3443,6 +3446,18 @@ class DeepseekV2ForCausalLM(nn.Module): weight = w weight_scale = self_attn.kv_b_proj.weight_scale_inv + if ( + should_deepgemm_weight_requant_ue8m0( + weight_block_size=getattr( + self.quant_config, "weight_block_size", None + ) + ) + and self._executed_weight_requant_ue8m0 + ): + weight_scale = inverse_transform_scale_ue8m0( + weight_scale, mn=weight.shape[-2] + ) + if ( _is_cuda and weight_block_size[0] == 128 @@ -3553,9 +3568,14 @@ class DeepseekV2ForCausalLM(nn.Module): self_attn.w_vc = bind_or_assign(self_attn.w_vc, w_vc.contiguous()) self_attn.use_deep_gemm_bmm = True - if not ENABLE_FLASHINFER_FP8_GEMM and should_deepgemm_weight_requant_ue8m0( - weight_block_size=getattr(self.quant_config, "weight_block_size", None) + if ( + not ENABLE_FLASHINFER_FP8_GEMM + and should_deepgemm_weight_requant_ue8m0( + weight_block_size=getattr(self.quant_config, "weight_block_size", None) + ) + and not self._executed_weight_requant_ue8m0 ): + self._executed_weight_requant_ue8m0 = True self._weight_requant_ue8m0(is_nextn) # TODO can move weight_requant_ue8m0 and transform_scale_ue8m0 into Fp8LinearMethod.process_weights_after_loading