21
21
22
22
import paddle
23
23
24
+ from paddleformers .utils .log import logger
25
+
24
26
try :
25
27
from safetensors import safe_open
26
28
except :
50
52
51
53
def paddle_name_to_hf_names_ds_v2 (paddle_name : str ) -> List [str ]:
52
54
"""
53
- 将Paddle模型参数名称转换为Hugging Face格式的名称列表
55
+ Convert Paddle model parameter names to Hugging Face format name lists
54
56
55
- 参数 :
56
- paddle_name: Paddle格式的参数名称
57
+ Args :
58
+ paddle_name: Parameter name in Paddle format
57
59
58
- 返回 :
59
- Hugging Face格式的参数名称列表(可能拆分多个参数 )
60
+ Returns :
61
+ List of parameter names in Hugging Face format (may be split into multiple parameters )
60
62
"""
61
63
if paddle_name == "_layers.deepseek_v2.embed_tokens.weight" :
62
64
return ["model.embed_tokens.weight" ]
@@ -69,7 +71,6 @@ def paddle_name_to_hf_names_ds_v2(paddle_name: str) -> List[str]:
69
71
70
72
m = _LAYER_RE_v2 .match (paddle_name )
71
73
if not m :
72
- print ("not match here !!" , paddle_name )
73
74
return []
74
75
75
76
rest = m .group (2 ) or ""
@@ -125,13 +126,13 @@ def paddle_name_to_hf_names_ds_v2(paddle_name: str) -> List[str]:
125
126
126
127
def paddle_name_to_hf_names (paddle_name : str ) -> List [str ]:
127
128
"""
128
- 将Paddle模型参数名称转换为Hugging Face格式的名称列表
129
+ Convert Paddle model parameter names to Hugging Face format name lists
129
130
130
- 参数 :
131
- paddle_name: Paddle格式的参数名称
131
+ Args :
132
+ paddle_name: Parameter name in Paddle format
132
133
133
- 返回 :
134
- Hugging Face格式的参数名称列表(可能拆分多个参数)
134
+ Returns :
135
+ List of parameter names in Hugging Face format (may be split into multiple parameters)
135
136
"""
136
137
if paddle_name == "_layers.local_shared_layers.DeepseekV2_shared_weight.embed_tokens.weight" :
137
138
return ["model.embed_tokens.weight" ]
@@ -142,7 +143,6 @@ def paddle_name_to_hf_names(paddle_name: str) -> List[str]:
142
143
m = _LAYER_RE .match (paddle_name )
143
144
144
145
if not m :
145
- print ("not match here !!" , paddle_name )
146
146
return []
147
147
else :
148
148
rest = m .group (3 ) or ""
@@ -201,8 +201,8 @@ def paddle_name_to_hf_names(paddle_name: str) -> List[str]:
201
201
202
202
203
203
def _get_hf_prefix (segment_id : int , id_in_segment : int ) -> str :
204
- """生成Hugging Face格式的层级前缀 """
205
- # 特殊层级映射
204
+ """Generate hierarchical prefix in Hugging Face format """
205
+ # Special layer mappings
206
206
# special_cases = {(0, 0): "model", (60, 2): "model.layers.61", (60, 3): "model"}
207
207
# special_cases = {(0, 0): "model", (28, 2): "model.layers.61", (28, 3): "model"}
208
208
# special_cases = {(0, 0): "model", (28, 2): "model.layers.61", (4, 1): "model"}
@@ -212,7 +212,7 @@ def _get_hf_prefix(segment_id: int, id_in_segment: int) -> str:
212
212
if (segment_id , id_in_segment ) in special_cases :
213
213
return special_cases [(segment_id , id_in_segment )]
214
214
215
- # 通用层级计算
215
+ # General layer calculation
216
216
layer_idx = segment_id + id_in_segment - 1
217
217
return f"model.layers.{ layer_idx } "
218
218
@@ -265,39 +265,38 @@ def prepare_tensor(tensor, dst_shape, *, force_transpose=False):
265
265
axis = - 1 ,
266
266
)
267
267
if t .shape != dst_shape :
268
- print ("base shape" , tensor [0 ].shape , tensor [1 ].shape )
269
- print ("shape not match " , t .shape , dst_shape )
268
+ logger .warning (
269
+ f"Prepare_tensor: shape not match. base tensor shape: { tensor [0 ].shape } , { tensor [1 ].shape } , t.shape: { t .shape } , dst_shape: { dst_shape } "
270
+ )
270
271
sys .exit ()
271
272
return t
272
273
273
274
if force_transpose :
274
275
return tensor .T .contiguous ()
275
276
276
277
if tensor .shape == dst_shape :
277
- if len (tensor .shape ) != 1 :
278
- print ("attention same shape not transpose !!!!!!!!!!!!!!!!!!!!!!" )
279
278
return tensor
280
279
if len (tensor .shape ) == 2 and paddle .transpose (tensor , perm = [1 , 0 ]).contiguous ().shape == dst_shape :
281
280
return paddle .transpose (tensor , perm = [1 , 0 ]).contiguous ()
282
281
283
- print ( " shape not match here " )
282
+ logger . warning ( "Prepare_tensor: shape not match. " )
284
283
sys .exit ()
285
284
286
285
287
286
def load_huggingface_ckpt (model , huggingface_ckpt_path ):
288
287
ckpt_pre = huggingface_ckpt_path
289
288
290
- # 1. 加载参数-文件映射表
289
+ # 1. Load parameter file mapping table
291
290
weight_map_path = ckpt_pre + "/model.safetensors.index.json"
292
291
with open (weight_map_path , "r" ) as f :
293
292
weight_map = json .load (f )["weight_map" ]
294
293
295
- # 2. 创建反向索引:文件 -> 参数列表
294
+ # 2. Create inverse index: file -> parameter list
296
295
file_to_params = defaultdict (list )
297
296
for param_name , filename in weight_map .items ():
298
297
file_to_params [filename ].append (param_name )
299
298
300
- # 2. 收集模型需要的文件列表
299
+ # 3. Collect file list that model needs
301
300
required_files = set ()
302
301
file_to_pd_param_name = defaultdict (list )
303
302
pd_param_name_to_file = defaultdict (list )
@@ -309,7 +308,7 @@ def load_huggingface_ckpt(model, huggingface_ckpt_path):
309
308
file_to_pd_param_name [filename ].append (pd_name )
310
309
pd_param_name_to_file [pd_name ].append (filename )
311
310
else :
312
- print (f"Warning: { pd_name } -> { hf_name [0 ]} not found in weight map" )
311
+ logger . warning (f"Warning: { pd_name } -> { hf_name [0 ]} not found in weight map" )
313
312
import sys
314
313
315
314
sys .exit ()
@@ -322,15 +321,15 @@ def load_huggingface_ckpt(model, huggingface_ckpt_path):
322
321
if filename != pd_param_name_to_file [pd_name ][0 ]:
323
322
pd_param_name_to_file [pd_name ].append (filename )
324
323
else :
325
- print (f"Warning: { pd_name } -> { hf_name [1 ]} not found in weight map" )
324
+ logger . warning (f"Warning: { pd_name } -> { hf_name [1 ]} not found in weight map" )
326
325
327
- # 3. 按文件分组加载
326
+ # 4. Group file and load
328
327
check_list = []
329
- print ("Start load huggingface ckpt" )
328
+ logger . info ("Start load huggingface ckpt" )
330
329
for i , filename in enumerate (required_files ):
331
330
try :
332
331
with safe_open (ckpt_pre + filename , framework = "paddle" , device = "cpu" ) as f :
333
- # 加载该文件包含的所有参数
332
+ # Load all parameters in file
334
333
pd_params = file_to_pd_param_name [filename ]
335
334
for pd_param in pd_params :
336
335
if pd_param in check_list :
@@ -374,5 +373,5 @@ def load_huggingface_ckpt(model, huggingface_ckpt_path):
374
373
check_list .append (pd_param )
375
374
376
375
except Exception as e :
377
- print (f"Error loading { filename } : { str (e )} " )
376
+ logger . warning (f"Error loading { filename } : { str (e )} " )
378
377
raise
0 commit comments