Skip to content

Commit 05b8630

Browse files
authored
Cherry-pick-36556: add paddle.version.cuda and paddle.version.cudnn API (#36556) (#36795)
* 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
1 parent 7647d40 commit 05b8630

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) 2021 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+
import unittest
16+
import paddle
17+
18+
19+
class TestCPUVersion(unittest.TestCase):
20+
def test_cuda_cudnn_version_in_cpu_package(self):
21+
if not paddle.is_compiled_with_cuda():
22+
self.assertEqual(paddle.version.cuda(), 'False')
23+
self.assertEqual(paddle.version.cudnn(), 'False')
24+
25+
26+
if __name__ == '__main__':
27+
unittest.main()

python/setup.py.in

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ def get_minor():
5454
def get_patch():
5555
return str(_get_version_detail(2))
5656

57+
def get_cuda_version():
58+
if '@WITH_GPU@' == 'ON':
59+
return '@CUDA_VERSION@'
60+
else:
61+
return 'False'
62+
63+
def get_cudnn_version():
64+
if '@WITH_GPU@' == 'ON':
65+
temp_cudnn_version = ''
66+
if '@CUDNN_MAJOR_VERSION@':
67+
temp_cudnn_version += '@CUDNN_MAJOR_VERSION@'
68+
if '@CUDNN_MINOR_VERSION@':
69+
temp_cudnn_version += '.@CUDNN_MINOR_VERSION@'
70+
if '@CUDNN_PATCHLEVEL_VERSION@':
71+
temp_cudnn_version += '.@CUDNN_PATCHLEVEL_VERSION@'
72+
return temp_cudnn_version
73+
else:
74+
return 'False'
75+
5776
def is_taged():
5877
try:
5978
cmd = ['git', 'describe', '--exact-match', '--tags', 'HEAD', '2>/dev/null']
@@ -67,18 +86,22 @@ def is_taged():
6786
else:
6887
return False
6988

70-
def write_version_py(filename='paddle/version.py'):
89+
def write_version_py(filename='paddle/version/__init__.py'):
7190
cnt = '''# THIS FILE IS GENERATED FROM PADDLEPADDLE SETUP.PY
7291
#
7392
full_version = '%(major)d.%(minor)d.%(patch)s'
7493
major = '%(major)d'
7594
minor = '%(minor)d'
7695
patch = '%(patch)s'
7796
rc = '%(rc)d'
97+
cuda_version = '%(cuda)s'
98+
cudnn_version = '%(cudnn)s'
7899
istaged = %(istaged)s
79100
commit = '%(commit)s'
80101
with_mkl = '%(with_mkl)s'
81102

103+
__all__ = ['cuda', 'cudnn']
104+
82105
def show():
83106
if istaged:
84107
print('full_version:', full_version)
@@ -91,20 +114,65 @@ def show():
91114

92115
def mkl():
93116
return with_mkl
117+
118+
def cuda():
119+
"""Get cuda version of paddle package.
120+
121+
Returns:
122+
string: Return the version information of cuda. If paddle package is CPU version, it will return False.
123+
124+
Examples:
125+
.. code-block:: python
126+
127+
import paddle
128+
129+
paddle.version.cuda()
130+
# '10.2'
131+
132+
"""
133+
return cuda_version
134+
135+
def cudnn():
136+
"""Get cudnn version of paddle package.
137+
138+
Returns:
139+
string: Return the version information of cudnn. If paddle package is CPU version, it will return False.
140+
141+
Examples:
142+
.. code-block:: python
143+
144+
import paddle
145+
146+
paddle.version.cudnn()
147+
# '7.6.5'
148+
149+
"""
150+
return cudnn_version
94151
'''
95152
commit = git_commit()
153+
154+
dirname = os.path.dirname(filename)
155+
156+
try:
157+
os.makedirs(dirname)
158+
except OSError as e:
159+
if e.errno != errno.EEXIST:
160+
raise
161+
96162
with open(filename, 'w') as f:
97163
f.write(cnt % {
98164
'major': get_major(),
99165
'minor': get_minor(),
100166
'patch': get_patch(),
101167
'rc': RC,
102168
'version': '${PADDLE_VERSION}',
169+
'cuda': get_cuda_version(),
170+
'cudnn': get_cudnn_version(),
103171
'commit': commit,
104172
'istaged': is_taged(),
105173
'with_mkl': '@WITH_MKL@'})
106174

107-
write_version_py(filename='@PADDLE_BINARY_DIR@/python/paddle/version.py')
175+
write_version_py(filename='@PADDLE_BINARY_DIR@/python/paddle/version/__init__.py')
108176

109177
def write_cuda_env_config_py(filename='paddle/cuda_env.py'):
110178
cnt = ""
@@ -251,6 +319,7 @@ packages=['paddle',
251319
'paddle.autograd',
252320
'paddle.device',
253321
'paddle.device.cuda',
322+
'paddle.version',
254323
]
255324

256325
with open('@PADDLE_SOURCE_DIR@/python/requirements.txt') as f:

0 commit comments

Comments
 (0)