From 2240f7fd8bae5785743cbc2fa23c06c26cb5f8fd Mon Sep 17 00:00:00 2001 From: Zeng Jinle <32832641+sneaxiy@users.noreply.github.com> Date: Fri, 28 Jun 2019 13:36:51 +0800 Subject: [PATCH] refine error msg of CUDAPlace, test=release/1.5 (#18376) --- paddle/fluid/pybind/pybind.cc | 39 ++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index f6096fb8ca..d9e9df1e0b 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #include #include +#include #include #include #include // NOLINT // for call_once @@ -786,13 +787,41 @@ All parameter, weight, gradient are variables in Paddle. .def("__init__", [](platform::CUDAPlace &self, int dev_id) { #ifdef PADDLE_WITH_CUDA - PADDLE_ENFORCE( - dev_id >= 0 && dev_id < platform::GetCUDADeviceCount(), - "Invalid CUDAPlace(%d), must inside [0, %d)", dev_id, - platform::GetCUDADeviceCount()); + if (UNLIKELY(dev_id < 0)) { + LOG(ERROR) << string::Sprintf( + "Invalid CUDAPlace(%d), device id must be 0 or " + "positive integer", + dev_id); + std::exit(-1); + } + + if (UNLIKELY(dev_id >= platform::GetCUDADeviceCount())) { + if (platform::GetCUDADeviceCount() == 0) { + LOG(ERROR) << "Cannot use GPU because there is no GPU " + "detected on your " + "machine."; + std::exit(-1); + } else { + LOG(ERROR) << string::Sprintf( + "Invalid CUDAPlace(%d), must inside [0, %d), because GPU " + "number on your machine is %d", + dev_id, platform::GetCUDADeviceCount(), + platform::GetCUDADeviceCount()); + std::exit(-1); + } + } + new (&self) platform::CUDAPlace(dev_id); #else - PADDLE_THROW("Cannot use CUDAPlace in CPU only version"); + LOG(ERROR) << string::Sprintf( + "Cannot use GPU because you have installed CPU version " + "PaddlePaddle.\n" + "If you want to use GPU, please try to install GPU version " + "PaddlePaddle by: pip install paddlepaddle-gpu\n" + "If you only have CPU, please change CUDAPlace(%d) to be " + "CPUPlace().\n", + dev_id); + std::exit(-1); #endif }) .def("_type", &PlaceIndex) -- GitLab