@@ -172,7 +172,11 @@ def _build_saved_state_dict(state_dict):
172
172
raise ValueError (
173
173
"The saved tensor is not initialized. If you used group sharded, please use save_group_sharded_model."
174
174
)
175
- if value .is_dense () and value .place .is_custom_place ():
175
+ if (
176
+ value .is_dense ()
177
+ and value .place .is_custom_place ()
178
+ and paddle .device .is_compiled_with_custom_device ("npu" )
179
+ ):
176
180
value = paddle ._C_ops .npu_identity (value , - 1 )
177
181
save_dict [key ] = np .array (value .cpu ())
178
182
name_table [key ] = value .name
@@ -213,7 +217,11 @@ def _load_state_dict_from_save_inference_model(model_path, config):
213
217
load_param_dict = {}
214
218
for var_name in persistable_var_dict :
215
219
tmp_var = persistable_var_dict [var_name ]
216
- if tmp_var .is_dense () and tmp_var .place .is_custom_place ():
220
+ if (
221
+ tmp_var .is_dense ()
222
+ and tmp_var .place .is_custom_place ()
223
+ and paddle .device .is_compiled_with_custom_device ("npu" )
224
+ ):
217
225
load_param_dict [var_name ] = np .array (
218
226
paddle ._C_ops .npu_identity (tmp_var , - 1 ).cpu ()
219
227
)
@@ -271,7 +279,11 @@ def _load_state_dict_from_save_params(model_path):
271
279
# 3. construct state_dict
272
280
load_param_dict = {}
273
281
for var in load_var_list :
274
- if var .is_dense () and var .place .is_custom_place ():
282
+ if (
283
+ var .is_dense ()
284
+ and var .place .is_custom_place ()
285
+ and paddle .device .is_compiled_with_custom_device ("npu" )
286
+ ):
275
287
var = paddle ._C_ops .npu_identity (var , - 1 )
276
288
load_param_dict [var .name ] = np .array (var .cpu ())
277
289
@@ -423,7 +435,11 @@ def _pickle_save(obj, f, protocol):
423
435
)
424
436
425
437
def reduce_varbase (self ):
426
- if self .is_dense () and self .place .is_custom_place ():
438
+ if (
439
+ self .is_dense ()
440
+ and self .place .is_custom_place ()
441
+ and paddle .device .is_compiled_with_custom_device ("npu" )
442
+ ):
427
443
data = np .array (paddle ._C_ops .npu_identity (self , - 1 ).cpu ())
428
444
else :
429
445
data = np .array (self .cpu ())
@@ -434,7 +450,10 @@ def reduce_varbase(self):
434
450
def reduce_DenseTensor (self ):
435
451
p = core .Place ()
436
452
p .set_place (paddle .CPUPlace ())
437
- if self ._place ().is_custom_place ():
453
+ if (
454
+ self ._place ().is_custom_place ()
455
+ and paddle .device .is_compiled_with_custom_device ("npu" )
456
+ ):
438
457
data = np .array (paddle ._C_ops .npu_identity (self , - 1 )._copy (p ))
439
458
else :
440
459
data = np .array (self ._copy (p ))
@@ -1239,7 +1258,12 @@ def load(path: str | BytesIO, **configs: Unpack[_LoadOptions]) -> Any:
1239
1258
if config .return_numpy :
1240
1259
p = core .Place ()
1241
1260
p .set_place (paddle .CPUPlace ())
1242
- if tensor ._place ().is_custom_place ():
1261
+ if (
1262
+ tensor ._place ().is_custom_place ()
1263
+ and paddle .device .is_compiled_with_custom_device (
1264
+ "npu"
1265
+ )
1266
+ ):
1243
1267
return np .array (
1244
1268
paddle ._C_ops .npu_identity (tensor , - 1 )._copy (p )
1245
1269
)
0 commit comments