Skip to content

Commit

Permalink
fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pangyoki committed Mar 30, 2021
1 parent 9e86ed7 commit fa651ac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
8 changes: 6 additions & 2 deletions paddle/fluid/pybind/tensor_py.h
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,12 @@ inline py::array TensorToPyArray(const framework::Tensor &tensor,

size_t copy_bytes = sizeof_dtype * numel;
auto p = BOOST_GET_CONST(platform::NPUPlace, tensor.place());
paddle::memory::Copy(platform::CPUPlace(), py_arr.mutable_data(), p,
tensor_buf_ptr, copy_bytes);
platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
auto &ctx = *pool.Get(tensor.place());
paddle::memory::Copy(
platform::CPUPlace(), py_arr.mutable_data(), p, tensor_buf_ptr,
copy_bytes,
reinterpret_cast<const platform::NPUDeviceContext &>(ctx).stream());
return py_arr;
#else
PADDLE_THROW(platform::errors::PermissionDenied(
Expand Down
12 changes: 10 additions & 2 deletions python/paddle/fluid/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1973,6 +1973,10 @@ def set_var(var, ndarray):
p = paddle.fluid.core.Place()
p.set_place(t._place())
place = paddle.fluid.XPUPlace(p.xpu_device_id())
elif p.is_npu_place():
p = paddle.fluid.core.Place()
p.set_place(t._place())
place = paddle.fluid.NPUPlace(p.npu_device_id())
else:
p = paddle.fluid.core.Place()
p.set_place(t._place())
Expand Down Expand Up @@ -2115,8 +2119,8 @@ def _load_vars_with_try_catch(exe,
error_str = "Failed to load model/variables `%s`, please make sure " \
"model/variables file is saved with the following APIs: " \
"save_params, save_persistables, save_vars."
filenames = [var.name for var in vars
] if filename is None else filename
filenames = [var.name for var in
vars] if filename is None else filename
if raise_error:
raise RuntimeError(error_str % filenames)
else:
Expand Down Expand Up @@ -2256,6 +2260,10 @@ def set_program_state(program, state_dict):
p = paddle.fluid.core.Place()
p.set_place(ten_place)
py_place = paddle.fluid.XPUPlace(p.xpu_device_id())
elif ten_place.is_npu_place():
p = paddle.fluid.core.Place()
p.set_place(ten_place)
py_place = paddle.fluid.NPUPlace(p.npu_device_id())

ten.set(new_para_np, py_place)

Expand Down
45 changes: 19 additions & 26 deletions python/paddle/fluid/tests/unittests/npu/test_save_load_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ def forward(self, input, label, init_hidden, init_cell):
return loss, last_hidden, last_cell


"""
@unittest.skipIf(not paddle.is_compiled_with_npu(),
"core is not compiled with NPU")
class TestSaveLoadBase(unittest.TestCase):
def test_ptb_rnn_cpu_float32(self):
seed = 90
Expand All @@ -239,8 +240,6 @@ def test_ptb_rnn_cpu_float32(self):
num_steps=num_steps,
init_scale=init_scale)

# place = fluid.CPUPlace() if not core.is_compiled_with_cuda(
# ) else fluid.CUDAPlace(0)
place = fluid.CPUPlace() if not core.is_compiled_with_npu(
) else paddle.NPUPlace(0)
exe = fluid.Executor(place)
Expand Down Expand Up @@ -320,6 +319,8 @@ def test_ptb_rnn_cpu_float32(self):
self.assertTrue(np.array_equal(new_t, base_t))


@unittest.skipIf(not paddle.is_compiled_with_npu(),
"core is not compiled with NPU")
class TestSaveLoadPartial(unittest.TestCase):
def test_ptb_rnn_cpu_float32(self):
seed = 90
Expand All @@ -342,8 +343,6 @@ def test_ptb_rnn_cpu_float32(self):
num_steps=num_steps,
init_scale=init_scale)

# place = fluid.CPUPlace() if not core.is_compiled_with_cuda(
# ) else fluid.CUDAPlace(0)
place = fluid.CPUPlace() if not core.is_compiled_with_npu(
) else paddle.NPUPlace(0)
exe = fluid.Executor(place)
Expand Down Expand Up @@ -432,6 +431,8 @@ def test_ptb_rnn_cpu_float32(self):
fluid.load(test_program, "./test_1.pdmodel", None)


@unittest.skipIf(not paddle.is_compiled_with_npu(),
"core is not compiled with NPU")
class TestSaveLoadSetStateDict(unittest.TestCase):
def test_ptb_rnn_cpu_float32(self):
seed = 90
Expand All @@ -454,8 +455,6 @@ def test_ptb_rnn_cpu_float32(self):
num_steps=num_steps,
init_scale=init_scale)

# place = fluid.CPUPlace() if not core.is_compiled_with_cuda(
# ) else fluid.CUDAPlace(0)
place = fluid.CPUPlace() if not core.is_compiled_with_npu(
) else paddle.NPUPlace(0)
exe = fluid.Executor(place)
Expand Down Expand Up @@ -535,6 +534,8 @@ def test_ptb_rnn_cpu_float32(self):
self.assertTrue(np.array_equal(new_t, base_t))


@unittest.skipIf(not paddle.is_compiled_with_npu(),
"core is not compiled with NPU")
class TestProgramStatePartial(unittest.TestCase):
def test_ptb_rnn_cpu_float32(self):
seed = 90
Expand All @@ -557,8 +558,6 @@ def test_ptb_rnn_cpu_float32(self):
num_steps=num_steps,
init_scale=init_scale)

# place = fluid.CPUPlace() if not core.is_compiled_with_cuda(
# ) else fluid.CUDAPlace(0)
place = fluid.CPUPlace() if not core.is_compiled_with_npu(
) else paddle.NPUPlace(0)
exe = fluid.Executor(place)
Expand Down Expand Up @@ -719,15 +718,15 @@ def test_ptb_rnn_cpu_float32(self):
self.assertTrue(np.array_equal(new_t, base_t))


@unittest.skipIf(not paddle.is_compiled_with_npu(),
"core is not compiled with NPU")
class TestVariableInit(unittest.TestCase):
def test_variable_init(self):

x = fluid.data(name="x", shape=[10, 10], dtype='float32')
y = fluid.layers.fc(x, 10)
z = fluid.layers.fc(y, 10)

# place = fluid.CPUPlace() if not core.is_compiled_with_cuda(
# ) else fluid.CUDAPlace(0)
place = fluid.CPUPlace() if not core.is_compiled_with_npu(
) else paddle.NPUPlace(0)
exe = fluid.Executor(place)
Expand All @@ -748,8 +747,6 @@ def set_var(var, ndarray):
program = fluid.default_main_program()
new_scope = fluid.core.Scope()

# place = fluid.CPUPlace() if not core.is_compiled_with_cuda(
# ) else fluid.CUDAPlace(0)
place = fluid.CPUPlace() if not core.is_compiled_with_npu(
) else paddle.NPUPlace(0)
exe = fluid.Executor(place)
Expand Down Expand Up @@ -800,7 +797,6 @@ def set_var(var, ndarray):
base_t = base_map[var.name]

self.assertTrue(np.array_equal(new_t, base_t))
"""


@unittest.skipIf(not paddle.is_compiled_with_npu(),
Expand Down Expand Up @@ -834,8 +830,6 @@ def test_load_from_old_interface(self):
num_steps=num_steps,
init_scale=init_scale)

# place = fluid.CPUPlace() if not core.is_compiled_with_cuda(
# ) else fluid.CUDAPlace(0)
place = fluid.CPUPlace() if not core.is_compiled_with_npu(
) else paddle.NPUPlace(0)
exe = fluid.Executor(place)
Expand Down Expand Up @@ -952,8 +946,6 @@ def test_load_from_old_interface_var_list(self):
num_steps=num_steps,
init_scale=init_scale)

# place = fluid.CPUPlace() if not core.is_compiled_with_cuda(
# ) else fluid.CUDAPlace(0)
place = fluid.CPUPlace() if not core.is_compiled_with_npu(
) else paddle.NPUPlace(0)
exe = fluid.Executor(place)
Expand Down Expand Up @@ -1045,7 +1037,8 @@ def test_load_from_old_interface_var_list(self):
self.assertTrue(np.sum(np.abs(new_t)) == 0)


"""
@unittest.skipIf(not paddle.is_compiled_with_npu(),
"core is not compiled with NPU")
class TestLoadFromOldInterfaceSingleFile(unittest.TestCase):
def test_load_from_old_interface(self):
seed = 90
Expand All @@ -1068,8 +1061,6 @@ def test_load_from_old_interface(self):
num_steps=num_steps,
init_scale=init_scale)

# place = fluid.CPUPlace() if not core.is_compiled_with_cuda(
# ) else fluid.CUDAPlace(0)
place = fluid.CPUPlace() if not core.is_compiled_with_npu(
) else paddle.NPUPlace(0)
exe = fluid.Executor(place)
Expand Down Expand Up @@ -1192,6 +1183,8 @@ def test_load_from_old_interface(self):
all_var_list + [temp_var])


@unittest.skipIf(not paddle.is_compiled_with_npu(),
"core is not compiled with NPU")
class TestProgramStateOldSave(unittest.TestCase):
def test_ptb_rnn_cpu_float32(self):
seed = 90
Expand All @@ -1214,8 +1207,6 @@ def test_ptb_rnn_cpu_float32(self):
num_steps=num_steps,
init_scale=init_scale)

# place = fluid.CPUPlace() if not core.is_compiled_with_cuda(
# ) else fluid.CUDAPlace(0)
place = fluid.CPUPlace() if not core.is_compiled_with_npu(
) else paddle.NPUPlace(0)
exe = fluid.Executor(place)
Expand Down Expand Up @@ -1338,6 +1329,8 @@ def check_in_static(self, main_program, base_map):
self.assertTrue(np.array_equal(new_t, base_t))


@unittest.skipIf(not paddle.is_compiled_with_npu(),
"core is not compiled with NPU")
class TestStaticSaveLoadLargeParameters(unittest.TestCase):
def test_large_parameters_static_save(self):
# enable static mode
Expand Down Expand Up @@ -1371,6 +1364,8 @@ def test_large_parameters_static_save(self):
np.sum(np.abs(result_z[0] - result_load[0])) < 1e-15)


@unittest.skipIf(not paddle.is_compiled_with_npu(),
"core is not compiled with NPU")
class TestProgramStateOldSaveSingleModel(unittest.TestCase):
def test_ptb_rnn_cpu_float32(self):
seed = 90
Expand All @@ -1393,8 +1388,6 @@ def test_ptb_rnn_cpu_float32(self):
num_steps=num_steps,
init_scale=init_scale)

# place = fluid.CPUPlace() if not core.is_compiled_with_cuda(
# ) else fluid.CUDAPlace(0)
place = fluid.CPUPlace() if not core.is_compiled_with_npu(
) else paddle.NPUPlace(0)
exe = fluid.Executor(place)
Expand Down Expand Up @@ -1502,7 +1495,7 @@ def test_ptb_rnn_cpu_float32(self):
main_program.global_block().create_var(
name="fake_var_name", persistable=True)
])
"""


if __name__ == '__main__':
paddle.enable_static()
Expand Down

0 comments on commit fa651ac

Please sign in to comment.