-
Notifications
You must be signed in to change notification settings - Fork 74k
/
find_rocm_config.py
446 lines (357 loc) · 14.8 KB
/
find_rocm_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# Copyright 2020 The TensorFlow 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.
# ==============================================================================
"""Prints ROCm library and header directories and versions found on the system.
The script searches for ROCm library and header files on the system, inspects
them to determine their version and prints the configuration to stdout.
The path to inspect is specified through an environment variable (ROCM_PATH).
If no valid configuration is found, the script prints to stderr and
returns an error code.
The script takes the directory specified by the ROCM_PATH environment variable.
The script looks for headers and library files in a hard-coded set of
subdirectories from base path of the specified directory. If ROCM_PATH is not
specified, then "/opt/rocm" is used as it default value
"""
import io
import os
import re
import sys
class ConfigError(Exception):
pass
def _get_default_rocm_path():
return "/opt/rocm"
def _get_rocm_install_path():
"""Determines and returns the ROCm installation path."""
rocm_install_path = _get_default_rocm_path()
if "ROCM_PATH" in os.environ:
rocm_install_path = os.environ["ROCM_PATH"]
# rocm_install_path = os.path.realpath(rocm_install_path)
return rocm_install_path
def _get_composite_version_number(major, minor, patch):
return 10000 * major + 100 * minor + patch
def _get_header_version(path, name):
"""Returns preprocessor defines in C header file."""
for line in io.open(path, "r", encoding="utf-8"):
match = re.match(r"#define %s +(\d+)" % name, line)
if match:
value = match.group(1)
return int(value)
raise ConfigError('#define "{}" is either\n'.format(name) +
" not present in file {} OR\n".format(path) +
" its value is not an integer literal")
def _find_rocm_config(rocm_install_path):
def rocm_version_numbers(path):
possible_version_files = [
"include/rocm-core/rocm_version.h", # ROCm 5.2
"include/rocm_version.h", # ROCm 5.1 and prior
]
version_file = None
for f in possible_version_files:
version_file_path = os.path.join(path, f)
if os.path.exists(version_file_path):
version_file = version_file_path
break
if not version_file:
raise ConfigError(
"ROCm version file not found in {}".format(possible_version_files))
major = _get_header_version(version_file, "ROCM_VERSION_MAJOR")
minor = _get_header_version(version_file, "ROCM_VERSION_MINOR")
patch = _get_header_version(version_file, "ROCM_VERSION_PATCH")
return major, minor, patch
major, minor, patch = rocm_version_numbers(rocm_install_path)
rocm_config = {
"rocm_version_number": _get_composite_version_number(major, minor, patch)
}
return rocm_config
def _find_hipruntime_config(rocm_install_path):
def hipruntime_version_number(path):
possible_version_files = [
"include/hip/hip_version.h", # ROCm 5.2
"hip/include/hip/hip_version.h", # ROCm 5.1 and prior
]
version_file = None
for f in possible_version_files:
version_file_path = os.path.join(path, f)
if os.path.exists(version_file_path):
version_file = version_file_path
break
if not version_file:
raise ConfigError("HIP Runtime version file not found in {}".format(
possible_version_files))
# This header file has an explicit #define for HIP_VERSION, whose value
# is (HIP_VERSION_MAJOR * 100 + HIP_VERSION_MINOR)
# Retreive the major + minor and re-calculate here, since we do not
# want get into the business of parsing arith exprs
major = _get_header_version(version_file, "HIP_VERSION_MAJOR")
minor = _get_header_version(version_file, "HIP_VERSION_MINOR")
return 100 * major + minor
hipruntime_config = {
"hipruntime_version_number": hipruntime_version_number(rocm_install_path)
}
return hipruntime_config
def _find_miopen_config(rocm_install_path):
def miopen_version_numbers(path):
possible_version_files = [
"include/miopen/version.h", # ROCm 5.2 and prior
"miopen/include/miopen/version.h", # ROCm 5.1 and prior
]
version_file = None
for f in possible_version_files:
version_file_path = os.path.join(path, f)
if os.path.exists(version_file_path):
version_file = version_file_path
break
if not version_file:
raise ConfigError(
'MIOpen version file "{}" not found'.format(version_file))
major = _get_header_version(version_file, "MIOPEN_VERSION_MAJOR")
minor = _get_header_version(version_file, "MIOPEN_VERSION_MINOR")
patch = _get_header_version(version_file, "MIOPEN_VERSION_PATCH")
return major, minor, patch
major, minor, patch = miopen_version_numbers(rocm_install_path)
miopen_config = {
"miopen_version_number":
_get_composite_version_number(major, minor, patch)
}
return miopen_config
def _find_rocblas_config(rocm_install_path):
def rocblas_version_numbers(path):
possible_version_files = [
"include/rocblas/internal/rocblas-version.h", # ROCm 5.2
"rocblas/include/internal/rocblas-version.h", # ROCm 5.1 and prior
]
version_file = None
for f in possible_version_files:
version_file_path = os.path.join(path, f)
if os.path.exists(version_file_path):
version_file = version_file_path
break
if not version_file:
raise ConfigError(
"rocblas version file not found in {}".format(
possible_version_files))
major = _get_header_version(version_file, "ROCBLAS_VERSION_MAJOR")
minor = _get_header_version(version_file, "ROCBLAS_VERSION_MINOR")
patch = _get_header_version(version_file, "ROCBLAS_VERSION_PATCH")
return major, minor, patch
major, minor, patch = rocblas_version_numbers(rocm_install_path)
rocblas_config = {
"rocblas_version_number":
_get_composite_version_number(major, minor, patch)
}
return rocblas_config
def _find_rocrand_config(rocm_install_path):
def rocrand_version_number(path):
possible_version_files = [
"include/rocrand/rocrand_version.h", # ROCm 5.1
"rocrand/include/rocrand_version.h", # ROCm 5.0 and prior
]
version_file = None
for f in possible_version_files:
version_file_path = os.path.join(path, f)
if os.path.exists(version_file_path):
version_file = version_file_path
break
if not version_file:
raise ConfigError(
"rocrand version file not found in {}".format(possible_version_files))
version_number = _get_header_version(version_file, "ROCRAND_VERSION")
return version_number
rocrand_config = {
"rocrand_version_number": rocrand_version_number(rocm_install_path)
}
return rocrand_config
def _find_rocfft_config(rocm_install_path):
def rocfft_version_numbers(path):
possible_version_files = [
"include/rocfft/rocfft-version.h", # ROCm 5.2
"rocfft/include/rocfft-version.h", # ROCm 5.1 and prior
]
version_file = None
for f in possible_version_files:
version_file_path = os.path.join(path, f)
if os.path.exists(version_file_path):
version_file = version_file_path
break
if not version_file:
raise ConfigError(
"rocfft version file not found in {}".format(possible_version_files))
major = _get_header_version(version_file, "rocfft_version_major")
minor = _get_header_version(version_file, "rocfft_version_minor")
patch = _get_header_version(version_file, "rocfft_version_patch")
return major, minor, patch
major, minor, patch = rocfft_version_numbers(rocm_install_path)
rocfft_config = {
"rocfft_version_number":
_get_composite_version_number(major, minor, patch)
}
return rocfft_config
def _find_hipfft_config(rocm_install_path):
def hipfft_version_numbers(path):
possible_version_files = [
"include/hipfft/hipfft-version.h", # ROCm 5.2
"hipfft/include/hipfft-version.h", # ROCm 5.1 and prior
]
version_file = None
for f in possible_version_files:
version_file_path = os.path.join(path, f)
if os.path.exists(version_file_path):
version_file = version_file_path
break
if not version_file:
raise ConfigError(
"hipfft version file not found in {}".format(possible_version_files))
major = _get_header_version(version_file, "hipfftVersionMajor")
minor = _get_header_version(version_file, "hipfftVersionMinor")
patch = _get_header_version(version_file, "hipfftVersionPatch")
return major, minor, patch
major, minor, patch = hipfft_version_numbers(rocm_install_path)
hipfft_config = {
"hipfft_version_number":
_get_composite_version_number(major, minor, patch)
}
return hipfft_config
def _find_roctracer_config(rocm_install_path):
def roctracer_version_numbers(path):
possible_version_files = [
"include/roctracer/roctracer.h", # ROCm 5.2
"roctracer/include/roctracer.h", # ROCm 5.1 and prior
]
version_file = None
for f in possible_version_files:
version_file_path = os.path.join(path, f)
if os.path.exists(version_file_path):
version_file = version_file_path
break
if not version_file:
raise ConfigError("roctracer version file not found in {}".format(
possible_version_files))
major = _get_header_version(version_file, "ROCTRACER_VERSION_MAJOR")
minor = _get_header_version(version_file, "ROCTRACER_VERSION_MINOR")
# roctracer header does not have a patch version number
patch = 0
return major, minor, patch
major, minor, patch = roctracer_version_numbers(rocm_install_path)
roctracer_config = {
"roctracer_version_number":
_get_composite_version_number(major, minor, patch)
}
return roctracer_config
def _find_hipsparse_config(rocm_install_path):
def hipsparse_version_numbers(path):
possible_version_files = [
"include/hipsparse/hipsparse-version.h", # ROCm 5.2
"hipsparse/include/hipsparse-version.h", # ROCm 5.1 and prior
]
version_file = None
for f in possible_version_files:
version_file_path = os.path.join(path, f)
if os.path.exists(version_file_path):
version_file = version_file_path
break
if not version_file:
raise ConfigError("hipsparse version file not found in {}".format(
possible_version_files))
major = _get_header_version(version_file, "hipsparseVersionMajor")
minor = _get_header_version(version_file, "hipsparseVersionMinor")
patch = _get_header_version(version_file, "hipsparseVersionPatch")
return major, minor, patch
major, minor, patch = hipsparse_version_numbers(rocm_install_path)
hipsparse_config = {
"hipsparse_version_number":
_get_composite_version_number(major, minor, patch)
}
return hipsparse_config
def _find_hipsolver_config(rocm_install_path):
def hipsolver_version_numbers(path):
possible_version_files = [
"include/hipsolver/internal/hipsolver-version.h", # ROCm 5.2
"hipsolver/include/internal/hipsolver-version.h", # ROCm 5.1
"hipsolver/include/hipsolver-version.h", # ROCm 5.0 and prior
]
version_file = None
for f in possible_version_files:
version_file_path = os.path.join(path, f)
if os.path.exists(version_file_path):
version_file = version_file_path
break
if not version_file:
raise ConfigError("hipsolver version file not found in {}".format(
possible_version_files))
major = _get_header_version(version_file, "hipsolverVersionMajor")
minor = _get_header_version(version_file, "hipsolverVersionMinor")
patch = _get_header_version(version_file, "hipsolverVersionPatch")
return major, minor, patch
major, minor, patch = hipsolver_version_numbers(rocm_install_path)
hipsolver_config = {
"hipsolver_version_number":
_get_composite_version_number(major, minor, patch)
}
return hipsolver_config
def _find_rocsolver_config(rocm_install_path):
def rocsolver_version_numbers(path):
possible_version_files = [
"include/rocsolver/rocsolver-version.h", # ROCm 5.2
"rocsolver/include/rocsolver-version.h", # ROCm 5.1 and prior
]
version_file = None
for f in possible_version_files:
version_file_path = os.path.join(path, f)
if os.path.exists(version_file_path):
version_file = version_file_path
break
if not version_file:
raise ConfigError("rocsolver version file not found in {}".format(
possible_version_files))
major = _get_header_version(version_file, "ROCSOLVER_VERSION_MAJOR")
minor = _get_header_version(version_file, "ROCSOLVER_VERSION_MINOR")
patch = _get_header_version(version_file, "ROCSOLVER_VERSION_PATCH")
return major, minor, patch
major, minor, patch = rocsolver_version_numbers(rocm_install_path)
rocsolver_config = {
"rocsolver_version_number":
_get_composite_version_number(major, minor, patch)
}
return rocsolver_config
def find_rocm_config():
"""Returns a dictionary of ROCm components config info."""
rocm_install_path = _get_rocm_install_path()
if not os.path.exists(rocm_install_path):
raise ConfigError(
'Specified ROCM_PATH "{}" does not exist'.format(rocm_install_path))
result = {}
result["rocm_toolkit_path"] = rocm_install_path
result.update(_find_rocm_config(rocm_install_path))
result.update(_find_hipruntime_config(rocm_install_path))
result.update(_find_miopen_config(rocm_install_path))
result.update(_find_rocblas_config(rocm_install_path))
result.update(_find_rocrand_config(rocm_install_path))
result.update(_find_rocfft_config(rocm_install_path))
if result["rocm_version_number"] >= 40100:
result.update(_find_hipfft_config(rocm_install_path))
result.update(_find_roctracer_config(rocm_install_path))
result.update(_find_hipsparse_config(rocm_install_path))
if result["rocm_version_number"] >= 40500:
result.update(_find_hipsolver_config(rocm_install_path))
result.update(_find_rocsolver_config(rocm_install_path))
return result
def main():
try:
for key, value in sorted(find_rocm_config().items()):
print("%s: %s" % (key, value))
except ConfigError as e:
sys.stderr.write("\nERROR: {}\n\n".format(str(e)))
sys.exit(1)
if __name__ == "__main__":
main()