From d65f41dba1792559736a0a3e8174a4389320de90 Mon Sep 17 00:00:00 2001 From: pangyoki Date: Wed, 27 Oct 2021 19:56:32 +0800 Subject: [PATCH] add paddle.version.cuda and paddle.version.cudnn API (#36556) * add paddle.version.cuda and paddle.version.cudnn API * fix little bug * fix bug * add doc string * fix mkdir error * fix windows path * fix new paddle/version path * fix unittest * fix format --- .../unittests/test_cuda_cudnn_version.py | 27 +++++++ python/setup.py.in | 73 ++++++++++++++++++- 2 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 python/paddle/fluid/tests/unittests/test_cuda_cudnn_version.py diff --git a/python/paddle/fluid/tests/unittests/test_cuda_cudnn_version.py b/python/paddle/fluid/tests/unittests/test_cuda_cudnn_version.py new file mode 100644 index 00000000000..d8229247a81 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_cuda_cudnn_version.py @@ -0,0 +1,27 @@ +# 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 unittest +import paddle + + +class TestCPUVersion(unittest.TestCase): + def test_cuda_cudnn_version_in_cpu_package(self): + if not paddle.is_compiled_with_cuda(): + self.assertEqual(paddle.version.cuda(), 'False') + self.assertEqual(paddle.version.cudnn(), 'False') + + +if __name__ == '__main__': + unittest.main() diff --git a/python/setup.py.in b/python/setup.py.in index b246225cbab..03b0555c965 100644 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -54,6 +54,25 @@ def get_minor(): def get_patch(): return str(_get_version_detail(2)) +def get_cuda_version(): + if '@WITH_GPU@' == 'ON': + return '@CUDA_VERSION@' + else: + return 'False' + +def get_cudnn_version(): + if '@WITH_GPU@' == 'ON': + temp_cudnn_version = '' + if '@CUDNN_MAJOR_VERSION@': + temp_cudnn_version += '@CUDNN_MAJOR_VERSION@' + if '@CUDNN_MINOR_VERSION@': + temp_cudnn_version += '.@CUDNN_MINOR_VERSION@' + if '@CUDNN_PATCHLEVEL_VERSION@': + temp_cudnn_version += '.@CUDNN_PATCHLEVEL_VERSION@' + return temp_cudnn_version + else: + return 'False' + def is_taged(): try: cmd = ['git', 'describe', '--exact-match', '--tags', 'HEAD', '2>/dev/null'] @@ -67,7 +86,7 @@ def is_taged(): else: return False -def write_version_py(filename='paddle/version.py'): +def write_version_py(filename='paddle/version/__init__.py'): cnt = '''# THIS FILE IS GENERATED FROM PADDLEPADDLE SETUP.PY # full_version = '%(major)d.%(minor)d.%(patch)s' @@ -75,10 +94,14 @@ major = '%(major)d' minor = '%(minor)d' patch = '%(patch)s' rc = '%(rc)d' +cuda_version = '%(cuda)s' +cudnn_version = '%(cudnn)s' istaged = %(istaged)s commit = '%(commit)s' with_mkl = '%(with_mkl)s' +__all__ = ['cuda', 'cudnn'] + def show(): if istaged: print('full_version:', full_version) @@ -91,8 +114,51 @@ def show(): def mkl(): return with_mkl + +def cuda(): + """Get cuda version of paddle package. + + Returns: + string: Return the version information of cuda. If paddle package is CPU version, it will return False. + + Examples: + .. code-block:: python + + import paddle + + paddle.version.cuda() + # '10.2' + + """ + return cuda_version + +def cudnn(): + """Get cudnn version of paddle package. + + Returns: + string: Return the version information of cudnn. If paddle package is CPU version, it will return False. + + Examples: + .. code-block:: python + + import paddle + + paddle.version.cudnn() + # '7.6.5' + + """ + return cudnn_version ''' commit = git_commit() + + dirname = os.path.dirname(filename) + + try: + os.makedirs(dirname) + except OSError as e: + if e.errno != errno.EEXIST: + raise + with open(filename, 'w') as f: f.write(cnt % { 'major': get_major(), @@ -100,11 +166,13 @@ def mkl(): 'patch': get_patch(), 'rc': RC, 'version': '${PADDLE_VERSION}', + 'cuda': get_cuda_version(), + 'cudnn': get_cudnn_version(), 'commit': commit, 'istaged': is_taged(), 'with_mkl': '@WITH_MKL@'}) -write_version_py(filename='@PADDLE_BINARY_DIR@/python/paddle/version.py') +write_version_py(filename='@PADDLE_BINARY_DIR@/python/paddle/version/__init__.py') def write_cuda_env_config_py(filename='paddle/cuda_env.py'): cnt = "" @@ -251,6 +319,7 @@ packages=['paddle', 'paddle.autograd', 'paddle.device', 'paddle.device.cuda', + 'paddle.version', ] with open('@PADDLE_SOURCE_DIR@/python/requirements.txt') as f: -- GitLab