Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let paddle.utils.install_check support CPU package with GPU device #32428

Merged
merged 3 commits into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
"""
Expand All @@ -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"

Expand Down
35 changes: 34 additions & 1 deletion python/paddle/utils/install_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ def _is_cuda_available():
return False


def _run_dygraph_single(use_cuda):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use_cuda is not used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"""
Testing the simple network in dygraph mode using one CPU/GPU.

Args:
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(
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.
Expand Down Expand Up @@ -152,7 +180,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()
Expand All @@ -162,6 +194,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:
Expand Down