Skip to content

Commit 6150cc8

Browse files
authored
fix Error of gpu version paddle when CUDA device is not set properly (#27819)
* fix gpu version paddle Error when have no CUDA device * optimize format and add new unittest * fix coverage problem * fix unittest format * change static mode to dygraph mode * use subprocess in unittest
1 parent 9209f34 commit 6150cc8

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

python/paddle/fluid/framework.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,17 @@ def _current_expected_place():
284284
global _global_expected_place_
285285
if _global_expected_place_ is None:
286286
if core.is_compiled_with_cuda():
287-
_global_expected_place_ = core.CUDAPlace(0)
287+
try:
288+
device_count = core.get_cuda_device_count()
289+
except Exception as e:
290+
device_count = 0
291+
if device_count > 0:
292+
_global_expected_place_ = core.CUDAPlace(0)
293+
else:
294+
warnings.warn(
295+
"You are using GPU version Paddle, but your CUDA device is not set properly. CPU device will be used by default."
296+
)
297+
_global_expected_place_ = core.CPUPlace()
288298
else:
289299
_global_expected_place_ = core.CPUPlace()
290300

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import print_function
16+
17+
import os
18+
import sys
19+
import subprocess
20+
import unittest
21+
import paddle
22+
import paddle.fluid as fluid
23+
from paddle.fluid import core
24+
25+
26+
class TestGPUPackagePaddle(unittest.TestCase):
27+
def test_import_paddle(self):
28+
if core.is_compiled_with_cuda():
29+
os.environ['CUDA_VISIBLE_DEVICES'] = ''
30+
test_file = 'test_no_gpu_run_rand.py'
31+
with open(test_file, 'w') as wb:
32+
cmd_test = """
33+
import paddle
34+
x = paddle.rand([3,4])
35+
assert x.place.is_gpu_place() is False, "There is no CUDA device, but Tensor's place is CUDAPlace"
36+
"""
37+
wb.write(cmd_test)
38+
39+
_python = sys.executable
40+
41+
ps_cmd = '{} {}'.format(_python, test_file)
42+
ps_proc = subprocess.Popen(
43+
ps_cmd.strip().split(" "),
44+
stdout=subprocess.PIPE,
45+
stderr=subprocess.PIPE)
46+
stdout, stderr = ps_proc.communicate()
47+
48+
assert 'CPU device will be used by default' in str(
49+
stderr
50+
), "GPU version Paddle is installed. But CPU device can't be used when CUDA device is not set properly"
51+
assert "Error" not in str(
52+
stderr
53+
), "There is no CUDA device, but Tensor's place is CUDAPlace"
54+
55+
56+
if __name__ == '__main__':
57+
unittest.main()

0 commit comments

Comments
 (0)