Skip to content
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
12 changes: 11 additions & 1 deletion python/paddle/fluid/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,17 @@ def _current_expected_place():
global _global_expected_place_
if _global_expected_place_ is None:
if core.is_compiled_with_cuda():
_global_expected_place_ = core.CUDAPlace(0)
try:
device_count = core.get_cuda_device_count()
except Exception as e:
device_count = 0
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to do something if an Exception happens instead of just ignore it, we can raise warnings at least. It can be refined in the next PR.

if device_count > 0:
_global_expected_place_ = core.CUDAPlace(0)
else:
warnings.warn(
"You are using GPU version Paddle, but your CUDA device is not set properly. CPU device will be used by default."
)
_global_expected_place_ = core.CPUPlace()
else:
_global_expected_place_ = core.CPUPlace()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function

import os
import sys
import subprocess
import unittest
import paddle
import paddle.fluid as fluid
from paddle.fluid import core


class TestGPUPackagePaddle(unittest.TestCase):
def test_import_paddle(self):
if core.is_compiled_with_cuda():
os.environ['CUDA_VISIBLE_DEVICES'] = ''
test_file = 'test_no_gpu_run_rand.py'
with open(test_file, 'w') as wb:
cmd_test = """
import paddle
x = paddle.rand([3,4])
assert x.place.is_gpu_place() is False, "There is no CUDA device, but Tensor's place is CUDAPlace"
"""
wb.write(cmd_test)

_python = sys.executable

ps_cmd = '{} {}'.format(_python, test_file)
ps_proc = subprocess.Popen(
ps_cmd.strip().split(" "),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = ps_proc.communicate()

assert 'CPU device will be used by default' in str(
Copy link
Contributor

Choose a reason for hiding this comment

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

Not important.
Since unittest is used, I recommend using unittest's API, like self.assertXXX instead of python's builtin assert.

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(
stderr
), "There is no CUDA device, but Tensor's place is CUDAPlace"


if __name__ == '__main__':
unittest.main()