Skip to content

Commit 205efa8

Browse files
author
root
committed
fix paddle.save bug on cumstom_device backends
1 parent fa0f36f commit 205efa8

File tree

1 file changed

+30
-6
lines changed
  • python/paddle/framework

1 file changed

+30
-6
lines changed

python/paddle/framework/io.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,11 @@ def _build_saved_state_dict(state_dict):
172172
raise ValueError(
173173
"The saved tensor is not initialized. If you used group sharded, please use save_group_sharded_model."
174174
)
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+
):
176180
value = paddle._C_ops.npu_identity(value, -1)
177181
save_dict[key] = np.array(value.cpu())
178182
name_table[key] = value.name
@@ -213,7 +217,11 @@ def _load_state_dict_from_save_inference_model(model_path, config):
213217
load_param_dict = {}
214218
for var_name in persistable_var_dict:
215219
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+
):
217225
load_param_dict[var_name] = np.array(
218226
paddle._C_ops.npu_identity(tmp_var, -1).cpu()
219227
)
@@ -271,7 +279,11 @@ def _load_state_dict_from_save_params(model_path):
271279
# 3. construct state_dict
272280
load_param_dict = {}
273281
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+
):
275287
var = paddle._C_ops.npu_identity(var, -1)
276288
load_param_dict[var.name] = np.array(var.cpu())
277289

@@ -423,7 +435,11 @@ def _pickle_save(obj, f, protocol):
423435
)
424436

425437
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+
):
427443
data = np.array(paddle._C_ops.npu_identity(self, -1).cpu())
428444
else:
429445
data = np.array(self.cpu())
@@ -434,7 +450,10 @@ def reduce_varbase(self):
434450
def reduce_DenseTensor(self):
435451
p = core.Place()
436452
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+
):
438457
data = np.array(paddle._C_ops.npu_identity(self, -1)._copy(p))
439458
else:
440459
data = np.array(self._copy(p))
@@ -1239,7 +1258,12 @@ def load(path: str | BytesIO, **configs: Unpack[_LoadOptions]) -> Any:
12391258
if config.return_numpy:
12401259
p = core.Place()
12411260
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+
):
12431267
return np.array(
12441268
paddle._C_ops.npu_identity(tensor, -1)._copy(p)
12451269
)

0 commit comments

Comments
 (0)