From b2eb4c28c3fe4978ff76602c649586312aa8c29f Mon Sep 17 00:00:00 2001 From: linjieccc <623543001@qq.com> Date: Fri, 6 Aug 2021 09:57:55 +0000 Subject: [PATCH 1/5] Add cuda device count api --- python/paddle/device/cuda/__init__.py | 23 ++++++++++++++++++ .../tests/unittests/test_cuda_device_count.py | 24 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 python/paddle/fluid/tests/unittests/test_cuda_device_count.py diff --git a/python/paddle/device/cuda/__init__.py b/python/paddle/device/cuda/__init__.py index 8b5879fe9a4ad3..5ca8b559f3eb31 100644 --- a/python/paddle/device/cuda/__init__.py +++ b/python/paddle/device/cuda/__init__.py @@ -22,6 +22,7 @@ 'Event', 'current_stream', 'synchronize', + 'device_count', ] @@ -94,3 +95,25 @@ def synchronize(device=None): raise ValueError("device type must be int or paddle.CUDAPlace") return core._device_synchronize(device_id) + + +def device_count(): + ''' + Return the number of GPUs available. + + Returns: + the number of GPUs available. + + Examples: + .. code-block:: python + + import paddle + + paddle.device.cuda.device_count() + + ''' + + if hasattr(core, 'get_cuda_device_count'): + return core.get_cuda_device_count() + else: + return 0 diff --git a/python/paddle/fluid/tests/unittests/test_cuda_device_count.py b/python/paddle/fluid/tests/unittests/test_cuda_device_count.py new file mode 100644 index 00000000000000..22bd5f782a23b3 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_cuda_device_count.py @@ -0,0 +1,24 @@ +# Copyright (c) 2021 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. + +import paddle +import unittest + +class TestDeviceCount(unittest.TestCase): + def test_device_count(self): + s = paddle.device.cuda.device_count() + self.assertIsNotNone(s) + +if __name__ == "__main__": + unittest.main() From d26cb4715b9a74caa9b8f62db1c37403be95cee7 Mon Sep 17 00:00:00 2001 From: linjieccc <623543001@qq.com> Date: Wed, 11 Aug 2021 11:27:58 +0000 Subject: [PATCH 2/5] update coda format --- python/paddle/fluid/tests/unittests/test_cuda_device_count.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_cuda_device_count.py b/python/paddle/fluid/tests/unittests/test_cuda_device_count.py index 22bd5f782a23b3..f4114c9d451b39 100644 --- a/python/paddle/fluid/tests/unittests/test_cuda_device_count.py +++ b/python/paddle/fluid/tests/unittests/test_cuda_device_count.py @@ -15,10 +15,12 @@ import paddle import unittest + class TestDeviceCount(unittest.TestCase): def test_device_count(self): s = paddle.device.cuda.device_count() self.assertIsNotNone(s) + if __name__ == "__main__": unittest.main() From 56d0cb6a98c1633004fda2f9a98394b42c458abb Mon Sep 17 00:00:00 2001 From: linjieccc <623543001@qq.com> Date: Wed, 18 Aug 2021 17:47:07 +0800 Subject: [PATCH 3/5] fix unittest error --- python/paddle/device/cuda/__init__.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/python/paddle/device/cuda/__init__.py b/python/paddle/device/cuda/__init__.py index 5ca8b559f3eb31..94c2c545426780 100644 --- a/python/paddle/device/cuda/__init__.py +++ b/python/paddle/device/cuda/__init__.py @@ -113,7 +113,6 @@ def device_count(): ''' - if hasattr(core, 'get_cuda_device_count'): - return core.get_cuda_device_count() - else: - return 0 + num_gpus = core.get_cuda_device_count() if hasattr(core, 'get_cuda_device_count') else 0 + + return num_gpus From ace1aeab8f874d5abd7fde9894d07aae41cfa912 Mon Sep 17 00:00:00 2001 From: linjieccc <623543001@qq.com> Date: Wed, 18 Aug 2021 21:07:54 +0800 Subject: [PATCH 4/5] update code format --- python/paddle/device/cuda/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/paddle/device/cuda/__init__.py b/python/paddle/device/cuda/__init__.py index 94c2c545426780..55ba842d302850 100644 --- a/python/paddle/device/cuda/__init__.py +++ b/python/paddle/device/cuda/__init__.py @@ -113,6 +113,7 @@ def device_count(): ''' - num_gpus = core.get_cuda_device_count() if hasattr(core, 'get_cuda_device_count') else 0 + num_gpus = core.get_cuda_device_count() if hasattr( + core, 'get_cuda_device_count') else 0 return num_gpus From 398422f54eb1b2bb068326016fe5f22362a2a482 Mon Sep 17 00:00:00 2001 From: linjieccc <623543001@qq.com> Date: Fri, 20 Aug 2021 11:24:10 +0800 Subject: [PATCH 5/5] update comment --- python/paddle/device/cuda/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/device/cuda/__init__.py b/python/paddle/device/cuda/__init__.py index 55ba842d302850..834cda71fdc5f1 100644 --- a/python/paddle/device/cuda/__init__.py +++ b/python/paddle/device/cuda/__init__.py @@ -102,7 +102,7 @@ def device_count(): Return the number of GPUs available. Returns: - the number of GPUs available. + int: the number of GPUs available. Examples: .. code-block:: python