1818from paddle import nn
1919from paddle .distributed import fleet
2020
21- from fastdeploy .model_executor .utils import set_weight_attrs
21+ from fastdeploy .model_executor .utils import default_weight_loader , set_weight_attrs
2222
2323from .utils import get_tensor
2424
@@ -53,44 +53,61 @@ def __init__(
5353 self .bias_key = prefix + ".bias"
5454 else :
5555 self .bias_key = None
56- self .use_ep = fd_config .parallel_config .use_ep
56+ self .fd_config = fd_config
57+ self .tp_group = fd_config .parallel_config .tp_group
5758 self .column_cut = True
59+ self .nranks = fd_config .parallel_config .tensor_parallel_size
5860
5961 ColumnParallelLinear = fleet .meta_parallel .ColumnParallelLinear
6062 RowParallelLinear = fleet .meta_parallel .RowParallelLinear
6163
62- if self .use_ep :
63- self .weight = self .create_parameter (
64- shape = [embedding_dim , num_embeddings ],
65- dtype = paddle .get_default_dtype (),
66- is_bias = False ,
64+ if self .column_cut :
65+ need_gather = True
66+ self .linear = ColumnParallelLinear (
67+ embedding_dim ,
68+ num_embeddings ,
69+ mp_group = self .tp_group ,
70+ weight_attr = None ,
71+ has_bias = True if self .bias_key is not None else False ,
72+ gather_output = need_gather ,
73+ fuse_matmul_bias = False , # False diff更小
6774 )
68- else :
69- if self .column_cut :
70- need_gather = True
71- self . linear = ColumnParallelLinear (
72- embedding_dim ,
73- num_embeddings ,
74- mp_group = fleet . get_hybrid_communicate_group (). get_model_parallel_group (),
75- weight_attr = None ,
76- has_bias = True if self . bias_key is not None else False ,
77- gather_output = need_gather ,
78- fuse_matmul_bias = False , # False diff更小
75+ set_weight_attrs (
76+ self .linear . weight ,
77+ {
78+ "weight_loader" : default_weight_loader ( self . fd_config ),
79+ "model_format" : self . fd_config . model_config . model_format ,
80+ } ,
81+ )
82+ if self . bias_key is not None :
83+ set_weight_attrs (
84+ self . linear . bias ,
85+ { "rl_need_attr" : { "rl_tp_degree" : fd_config . parallel_config . tensor_parallel_size }},
7986 )
87+ if self .nranks > 1 :
8088 set_weight_attrs (self .linear .weight , {"output_dim" : True })
81- if self .bias_key is not None :
82- set_weight_attrs (self .linear .bias , {"output_dim" : True })
83- else :
84- self .linear = RowParallelLinear (
85- embedding_dim ,
86- num_embeddings ,
87- mp_group = fleet .get_hybrid_communicate_group ().get_model_parallel_group (),
88- weight_attr = None ,
89- has_bias = True if self .bias_key is not None else False ,
90- input_is_parallel = False ,
91- fuse_matmul_bias = False , # False diff更小
92- )
93- set_weight_attrs (self .linear .weight , {"output_dim" : False })
89+ else :
90+ self .linear = RowParallelLinear (
91+ embedding_dim ,
92+ num_embeddings ,
93+ mp_group = self .tp_group ,
94+ weight_attr = None ,
95+ has_bias = True if self .bias_key is not None else False ,
96+ input_is_parallel = False ,
97+ fuse_matmul_bias = False , # False diff更小
98+ )
99+ set_weight_attrs (
100+ self .linear .weight ,
101+ {
102+ "weight_loader" : default_weight_loader (self .fd_config ),
103+ "model_format" : self .fd_config .model_config .model_format ,
104+ },
105+ )
106+ if self .nranks > 1 :
107+ set_weight_attrs (self .linear .weight , {"output_dim" : True })
108+ set_weight_attrs (
109+ self .linear .weight , {"rl_need_attr" : {"rl_tp_degree" : fd_config .parallel_config .tensor_parallel_size }}
110+ )
94111
95112 def load_state_dict (self , state_dict ):
96113 """
@@ -100,17 +117,14 @@ def load_state_dict(self, state_dict):
100117 state_dict (dict): A dictionary containing the checkpoint weights and biases.
101118 """
102119
103- if self .use_ep :
104- self .weight .set_value (get_tensor (state_dict .pop (self .weight_key )).astype (paddle .get_default_dtype ()))
105- else :
106- weight_tensor = get_tensor (state_dict .pop (self .weight_key )).astype (paddle .get_default_dtype ())
107- if self .linear .weight .shape != weight_tensor .shape :
108- weight_tensor = weight_tensor .transpose ([1 , 0 ])
109- self .linear .weight .set_value (weight_tensor )
120+ weight_tensor = get_tensor (state_dict .pop (self .weight_key )).astype (paddle .get_default_dtype ())
121+ if self .linear .weight .shape != weight_tensor .shape :
122+ weight_tensor = weight_tensor .transpose ([1 , 0 ])
123+ self .linear .weight .set_value (weight_tensor )
110124
111- if self .bias_key is not None :
112- bias = get_tensor (state_dict .pop (self .bias_key )).astype (paddle .get_default_dtype ())
113- self .linear .bias .set_value (bias )
125+ if self .bias_key is not None :
126+ bias = get_tensor (state_dict .pop (self .bias_key )).astype (paddle .get_default_dtype ())
127+ self .linear .bias .set_value (bias )
114128
115129 def forward (self , input ):
116130 """
@@ -123,8 +137,5 @@ def forward(self, input):
123137 Tensor: The output tensor after processing through the layer.
124138 """
125139 logits = input
126- if self .use_ep :
127- logits = paddle .matmul (logits , self .weight )
128- else :
129- logits = self .linear (logits )
140+ logits = self .linear (logits )
130141 return logits
0 commit comments