From cba5519b6ffdcecabb2aeeadaf9412b57aa2c68c Mon Sep 17 00:00:00 2001 From: pangyoki Date: Wed, 21 Apr 2021 12:47:46 +0000 Subject: [PATCH 1/3] let paddle.utils.install_check support CPU package with GPU device --- python/paddle/utils/install_check.py | 31 +++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/python/paddle/utils/install_check.py b/python/paddle/utils/install_check.py index 3b98680c89f25..8591f6d6790bf 100644 --- a/python/paddle/utils/install_check.py +++ b/python/paddle/utils/install_check.py @@ -74,6 +74,30 @@ def _is_cuda_available(): return False +def _run_dygraph_single(use_cuda): + """ + Testing the simple network in dygraph mode using one CPU/GPU. + + Args: + use_cuda (bool): Whether running with CUDA. + """ + paddle.disable_static() + weight_attr = paddle.ParamAttr( + name="weight", initializer=paddle.nn.initializer.Constant(value=0.5)) + bias_attr = paddle.ParamAttr( + name="bias", initializer=paddle.nn.initializer.Constant(value=1.0)) + linear = paddle.nn.Linear( + 2, 4, weight_attr=weight_attr, bias_attr=bias_attr) + input_np = _prepare_data(1) + input_tensor = paddle.to_tensor(input_np) + linear_out = linear(input_tensor) + out = paddle.tensor.sum(linear_out) + out.backward() + opt = paddle.optimizer.Adam( + learning_rate=0.001, parameters=linear.parameters()) + opt.step() + + def _run_static_single(use_cuda): """ Testing the simple network with executor running directly, using one CPU/GPU. @@ -152,7 +176,11 @@ def run_check(): print("Running verify PaddlePaddle program ... ") - use_cuda = _is_cuda_available() + if paddle.is_compiled_with_cuda(): + use_cuda = _is_cuda_available() + else: + use_cuda = False + if use_cuda: device_str = "GPU" device_list = paddle.static.cuda_places() @@ -162,6 +190,7 @@ def run_check(): device_count = len(device_list) _run_static_single(use_cuda) + _run_dygraph_single(use_cuda) print("PaddlePaddle works well on 1 {}.".format(device_str)) try: From 2cdf4c8ef969c15584d6cf20323d27e4d9839fbd Mon Sep 17 00:00:00 2001 From: pangyoki Date: Thu, 22 Apr 2021 03:17:15 +0000 Subject: [PATCH 2/3] use use_cuda in dygraph checking --- python/paddle/utils/install_check.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python/paddle/utils/install_check.py b/python/paddle/utils/install_check.py index 8591f6d6790bf..b39009985e735 100644 --- a/python/paddle/utils/install_check.py +++ b/python/paddle/utils/install_check.py @@ -82,6 +82,10 @@ def _run_dygraph_single(use_cuda): use_cuda (bool): Whether running with CUDA. """ paddle.disable_static() + if use_cuda: + paddle.set_device('gpu') + else: + paddle.set_device('cpu') weight_attr = paddle.ParamAttr( name="weight", initializer=paddle.nn.initializer.Constant(value=0.5)) bias_attr = paddle.ParamAttr( From ce94bff74d7a3298eb1d4c265c2e44f8328c28d4 Mon Sep 17 00:00:00 2001 From: pangyoki Date: Thu, 22 Apr 2021 06:47:26 +0000 Subject: [PATCH 3/3] add unittest for install_check --- .../tests/unittests/test_gpu_package_without_gpu_device.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_gpu_package_without_gpu_device.py b/python/paddle/fluid/tests/unittests/test_gpu_package_without_gpu_device.py index 2b51bec9cb0e7..e528e742a277a 100644 --- a/python/paddle/fluid/tests/unittests/test_gpu_package_without_gpu_device.py +++ b/python/paddle/fluid/tests/unittests/test_gpu_package_without_gpu_device.py @@ -34,6 +34,7 @@ def test_import_paddle(self): with open(test_file, 'w') as wb: cmd_test = """ import paddle +paddle.utils.run_check() x = paddle.rand([3,4]) assert x.place.is_gpu_place() is False, "There is no CUDA device, but Tensor's place is CUDAPlace" """ @@ -52,7 +53,7 @@ def test_import_paddle(self): assert 'CPU device will be used by default' in str( stderr ), "GPU version Paddle is installed. But CPU device can't be used when CUDA device is not set properly" - assert "Error" not in str( + assert "AssertionError" not in str( stderr ), "There is no CUDA device, but Tensor's place is CUDAPlace"