@@ -201,9 +201,6 @@ def forward(self, hidden_states: paddle.Tensor, forward_meta: ForwardMeta):
201201
202202
203203class DeepseekV3MLAAttention (nn .Layer ):
204- """
205- DeepseekV3MLAAttention
206- """
207204
208205 def __init__ (self , fd_config : FDConfig , layer_id : int , prefix : str = "" ) -> None :
209206 super ().__init__ ()
@@ -230,7 +227,12 @@ def __init__(self, fd_config: FDConfig, layer_id: int, prefix: str = "") -> None
230227 self .kv_lora_rank = fd_config .model_config .kv_lora_rank
231228
232229 self .attn_softmax_scale = self .qk_head_dim ** - 0.5
233- self .rope_theta = fd_config .model_config .rope_theta
230+
231+ if fd_config .model_config .model_type == "glm_moe_dsa" :
232+ self .rope_theta = fd_config .model_config .rope_parameters ["rope_theta" ]
233+ else :
234+ self .rope_theta = fd_config .model_config .rope_theta
235+
234236 self .rms_norm_eps = fd_config .model_config .rms_norm_eps
235237
236238 assert self .q_lora_rank is not None , "self.q_lora_rank is None, Please Check your config."
@@ -316,6 +318,10 @@ def __init__(self, fd_config: FDConfig, layer_id: int, prefix: str = "") -> None
316318 ]
317319 if key in self .rope_scaling
318320 }
321+
322+ # used for dsa attention.
323+ self .rope_scaling_kwargs = rope_scaling_kwargs
324+
319325 self .rope_scaling_factor = self .rope_scaling ["factor" ]
320326 self .rope_scaling_original_max_position_embeddings = self .rope_scaling ["original_max_position_embeddings" ]
321327 self .rotary_emb = DeepseekScalingRotaryEmbedding (
@@ -823,143 +829,28 @@ def forward(
823829 return indexer_top_k
824830
825831
826- class DeepseekV32DSAAttention (nn .Layer ):
827- """
828- DeepseekV32DSAAttention
829- """
832+ class DeepseekV32DSAAttention (DeepseekV3MLAAttention ):
830833
831834 def __init__ (self , fd_config : FDConfig , layer_id : int , prefix : str = "" ) -> None :
832- super ().__init__ ()
833-
834- self .tp_size = fd_config .parallel_config .tensor_parallel_size
835- self .hidden_size = fd_config .model_config .hidden_size
836- self .num_attention_heads = fd_config .model_config .num_attention_heads
837- self .num_attention_heads_tp = self .num_attention_heads // self .tp_size
838-
839- # MLA
840- self .qk_nope_head_dim = fd_config .model_config .qk_nope_head_dim
841- self .qk_rope_head_dim = fd_config .model_config .qk_rope_head_dim
842- self .qk_head_dim = self .qk_nope_head_dim + self .qk_rope_head_dim
843- self .v_head_dim = fd_config .model_config .v_head_dim
844- self .q_lora_rank = fd_config .model_config .q_lora_rank
845- self .kv_lora_rank = fd_config .model_config .kv_lora_rank
835+ super ().__init__ (fd_config , layer_id , prefix )
846836
847837 # Indexer
848838 self .index_head_dim = fd_config .model_config .index_head_dim
849839 self .index_n_heads = fd_config .model_config .index_n_heads
850840 self .index_topk = fd_config .model_config .index_topk
851841
852- self .attn_softmax_scale = self .qk_head_dim ** - 0.5
853- self .rope_theta = fd_config .model_config .rope_theta
854- if fd_config .model_config .model_type == "glm_moe_dsa" :
855- self .rope_theta = fd_config .model_config .rope_parameters ["rope_theta" ]
856-
857- self .rms_norm_eps = fd_config .model_config .rms_norm_eps
858-
859- assert self .q_lora_rank is not None , "self.q_lora_rank is None, Please Check your config."
860-
861- self .qkv_a_proj_with_mqa = MergedReplicatedLinear (
862- fd_config = fd_config ,
863- prefix = f"{ prefix } .qkv_a_proj_with_mqa" ,
864- input_size = self .hidden_size ,
865- output_sizes = [self .q_lora_rank , self .kv_lora_rank + self .qk_rope_head_dim ],
866- with_bias = False ,
867- )
868-
869- self .q_a_layernorm = RMSNorm (
870- fd_config ,
871- hidden_size = self .q_lora_rank ,
872- eps = self .rms_norm_eps ,
873- prefix = f"{ prefix } .q_a_layernorm" ,
874- )
875-
876- self .q_b_proj = ColumnParallelLinear (
877- fd_config = fd_config ,
878- prefix = f"{ prefix } .q_b_proj" ,
879- input_size = self .q_lora_rank ,
880- output_size = self .num_attention_heads * self .qk_head_dim ,
881- with_bias = False ,
882- )
883-
884- self .kv_a_layernorm = RMSNorm (
885- fd_config ,
886- hidden_size = self .kv_lora_rank ,
887- eps = self .rms_norm_eps ,
888- prefix = f"{ prefix } .kv_a_layernorm" ,
889- )
890-
891- self .kv_b_proj = ColumnParallelLinear (
892- fd_config = fd_config ,
893- prefix = f"{ prefix } .kv_b_proj" ,
894- input_size = self .kv_lora_rank ,
895- output_size = self .num_attention_heads * (self .qk_nope_head_dim + self .v_head_dim ),
896- with_bias = False ,
897- )
898-
899- self .o_proj = RowParallelLinear (
900- fd_config ,
901- prefix = f"{ prefix } .o_proj" ,
902- input_size = self .num_attention_heads * self .v_head_dim ,
903- output_size = self .hidden_size ,
904- with_bias = False ,
905- layer_id = layer_id ,
906- )
907-
908- self .kv_b_proj_bmm = KVBatchLinear (
909- fd_config = fd_config ,
910- kv_b_proj = self .kv_b_proj ,
911- prefix = f"{ prefix } .kv_b_proj" ,
912- kv_lora_rank = self .kv_lora_rank ,
913- num_attention_heads = self .num_attention_heads ,
914- qk_nope_head_dim = self .qk_nope_head_dim ,
915- v_head_dim = self .v_head_dim ,
916- )
917- self .rope_scaling = getattr (fd_config .model_config , "rope_scaling" , None )
918842 if self .rope_scaling and "factor" in self .rope_scaling :
919- mscale_all_dim = self .rope_scaling .get ("mscale_all_dim" , False )
920- scaling_factor = self .rope_scaling ["factor" ]
921- mscale = self .yarn_get_mscale (scaling_factor , float (mscale_all_dim ))
922- self .attn_softmax_scale = self .attn_softmax_scale * mscale * mscale
923-
924- rope_scaling_kwargs = {
925- key : self .rope_scaling [key ]
926- for key in [
927- "beta_fast" ,
928- "beta_slow" ,
929- "mscale" ,
930- "mscale_all_dim" ,
931- ]
932- if key in self .rope_scaling
933- }
934- self .rope_scaling_factor = self .rope_scaling ["factor" ]
935- self .rope_scaling_original_max_position_embeddings = self .rope_scaling ["original_max_position_embeddings" ]
936- self .rotary_emb = DeepseekScalingRotaryEmbedding (
937- self .qk_rope_head_dim ,
938- max_position_embeddings = self .rope_scaling_original_max_position_embeddings ,
939- base = self .rope_theta ,
940- scaling_factor = self .rope_scaling_factor ,
941- ** rope_scaling_kwargs ,
942- )
943843 self .indexer_rotary_emb = DeepseekScalingRotaryEmbedding (
944844 self .qk_rope_head_dim ,
945845 max_position_embeddings = self .rope_scaling_original_max_position_embeddings ,
946846 base = self .rope_theta ,
947847 scaling_factor = self .rope_scaling_factor ,
948- ** rope_scaling_kwargs ,
848+ ** self . rope_scaling_kwargs ,
949849 )
950850 else :
951- # Default rope without scaling
952- # The current `max_model_len` can cover the maximum context length.
953- max_position_embeddings = getattr (fd_config .model_config , "max_model_len" , 8192 )
954- self .rotary_emb = DeepseekScalingRotaryEmbedding (
955- self .qk_rope_head_dim ,
956- max_position_embeddings = max_position_embeddings ,
957- base = self .rope_theta ,
958- scaling_factor = 1.0 ,
959- )
960851 self .indexer_rotary_emb = DeepseekScalingRotaryEmbedding (
961852 self .qk_rope_head_dim ,
962- max_position_embeddings = max_position_embeddings ,
853+ max_position_embeddings = getattr ( fd_config . model_config , "max_model_len" , 8192 ) ,
963854 base = self .rope_theta ,
964855 scaling_factor = 1.0 ,
965856 )
@@ -976,15 +867,6 @@ def __init__(self, fd_config: FDConfig, layer_id: int, prefix: str = "") -> None
976867 use_neox_rotary_style = False ,
977868 )
978869
979- self .prefix = prefix
980-
981- @staticmethod
982- def yarn_get_mscale (scale = 1 , mscale = 1 ):
983- """ """
984- if scale <= 1 :
985- return 1.0
986- return 0.1 * mscale * math .log (scale ) + 1.0
987-
988870 def forward (
989871 self ,
990872 forward_meta : ForwardMeta ,
@@ -1041,9 +923,6 @@ def forward(
1041923
1042924
1043925class DeepSeekV3DecoderLayer (nn .Layer ):
1044- """
1045- DeepSeekV3DecoderLayer
1046- """
1047926
1048927 def __init__ (
1049928 self ,
@@ -1121,9 +1000,6 @@ def forward(
11211000
11221001@support_graph_optimization
11231002class DeepSeekV3Model (nn .Layer ):
1124- """
1125- DeepSeekV3Model
1126- """
11271003
11281004 def __init__ (
11291005 self ,
@@ -1191,9 +1067,6 @@ def forward(
11911067 primary_use = ModelCategory .TEXT_GENERATION ,
11921068)
11931069class DeepseekV3ForCausalLM (ModelForCasualLM ):
1194- """
1195- DeepseekV3ForCausalLM
1196- """
11971070
11981071 def __init__ (self , fd_config : FDConfig ):
11991072 """
@@ -1342,9 +1215,6 @@ def clear_graph_opt_backend(self):
13421215
13431216
13441217class DeepSeekV3PretrainedModel (PretrainedModel ):
1345- """
1346- DeepSeekV3PretrainedModel
1347- """
13481218
13491219 config_class = FDConfig
13501220
0 commit comments