diff --git a/paddle/phi/backends/custom/custom_device.cc b/paddle/phi/backends/custom/custom_device.cc index 928101b09f23675ebdf17c110445a99fe32cabc0..3b438b562ce1d1037fd7bfc3417f7572c37cc694 100644 --- a/paddle/phi/backends/custom/custom_device.cc +++ b/paddle/phi/backends/custom/custom_device.cc @@ -53,11 +53,14 @@ class CustomDevice : public DeviceInterface { ~CustomDevice() override { Finalize(); } size_t GetDeviceCount() override { - size_t count; - if (pimpl_->get_device_count(&count) != C_SUCCESS) { - count = 0; + if (!device_init_flag_) { + if (pimpl_->get_device_count(&device_count_) != C_SUCCESS) { + device_count_ = 0; + } else { + device_init_flag_ = true; + } } - return count; + return device_count_; } std::vector GetDeviceList() override { @@ -882,6 +885,8 @@ class CustomDevice : public DeviceInterface { std::unique_ptr pimpl_; void* dso_handle_; std::unordered_map devices_pool; + bool device_init_flag_ = false; + size_t device_count_; }; bool ValidCustomCustomRuntimeParams(const CustomRuntimeParams* params) { diff --git a/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_plugin.py b/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_plugin.py index 79e3e506b906ed1cf12322f85ba5e21a823df086..f1cfa8ef7392a7ff404c150d1a75ff9024b2f911 100644 --- a/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_plugin.py +++ b/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_plugin.py @@ -140,6 +140,7 @@ class TestCustomCPUPlugin(unittest.TestCase): import paddle paddle.set_device('custom_cpu') + paddle.device.get_available_device() x_tensor = paddle.to_tensor(x, stop_gradient=False) y_tensor = paddle.to_tensor(y) z1_tensor = paddle.matmul(x_tensor, y_tensor)