Skip to content

Commit 2240f7f

Browse files
authored
refine error msg of CUDAPlace, test=release/1.5 (#18376)
1 parent 924e53b commit 2240f7f

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

paddle/fluid/pybind/pybind.cc

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License. */
1414
#include <Python.h>
1515
#include <algorithm>
16+
#include <cstdlib>
1617
#include <map>
1718
#include <memory>
1819
#include <mutex> // NOLINT // for call_once
@@ -786,13 +787,41 @@ All parameter, weight, gradient are variables in Paddle.
786787
.def("__init__",
787788
[](platform::CUDAPlace &self, int dev_id) {
788789
#ifdef PADDLE_WITH_CUDA
789-
PADDLE_ENFORCE(
790-
dev_id >= 0 && dev_id < platform::GetCUDADeviceCount(),
791-
"Invalid CUDAPlace(%d), must inside [0, %d)", dev_id,
792-
platform::GetCUDADeviceCount());
790+
if (UNLIKELY(dev_id < 0)) {
791+
LOG(ERROR) << string::Sprintf(
792+
"Invalid CUDAPlace(%d), device id must be 0 or "
793+
"positive integer",
794+
dev_id);
795+
std::exit(-1);
796+
}
797+
798+
if (UNLIKELY(dev_id >= platform::GetCUDADeviceCount())) {
799+
if (platform::GetCUDADeviceCount() == 0) {
800+
LOG(ERROR) << "Cannot use GPU because there is no GPU "
801+
"detected on your "
802+
"machine.";
803+
std::exit(-1);
804+
} else {
805+
LOG(ERROR) << string::Sprintf(
806+
"Invalid CUDAPlace(%d), must inside [0, %d), because GPU "
807+
"number on your machine is %d",
808+
dev_id, platform::GetCUDADeviceCount(),
809+
platform::GetCUDADeviceCount());
810+
std::exit(-1);
811+
}
812+
}
813+
793814
new (&self) platform::CUDAPlace(dev_id);
794815
#else
795-
PADDLE_THROW("Cannot use CUDAPlace in CPU only version");
816+
LOG(ERROR) << string::Sprintf(
817+
"Cannot use GPU because you have installed CPU version "
818+
"PaddlePaddle.\n"
819+
"If you want to use GPU, please try to install GPU version "
820+
"PaddlePaddle by: pip install paddlepaddle-gpu\n"
821+
"If you only have CPU, please change CUDAPlace(%d) to be "
822+
"CPUPlace().\n",
823+
dev_id);
824+
std::exit(-1);
796825
#endif
797826
})
798827
.def("_type", &PlaceIndex<platform::CUDAPlace>)

0 commit comments

Comments
 (0)