3 from __future__
import absolute_import, division, print_function, unicode_literals
10 from collections
import namedtuple
14 TORCH_AVAILABLE =
True 15 except (ImportError, NameError, AttributeError):
16 TORCH_AVAILABLE =
False 18 PY3 = sys.version_info >= (3, 0)
21 SystemEnv = namedtuple(
'SystemEnv', [
24 'cuda_compiled_version',
30 'cuda_runtime_version',
31 'nvidia_driver_version',
41 """Returns (return-code, stdout, stderr)""" 42 p = subprocess.Popen(command, stdout=subprocess.PIPE,
43 stderr=subprocess.PIPE, shell=
True)
44 output, err = p.communicate()
47 output = output.decode(
"utf-8")
48 err = err.decode(
"utf-8")
49 return rc, output.strip(), err.strip()
52 def run_and_read_all(run_lambda, command):
53 """Runs command using run_lambda; reads and returns entire output if rc is 0""" 54 rc, out, _ = run_lambda(command)
60 def run_and_parse_first_match(run_lambda, command, regex):
61 """Runs command using run_lambda, returns the first regex match if it exists""" 62 rc, out, _ = run_lambda(command)
65 match = re.search(regex, out)
71 def get_conda_packages(run_lambda):
72 if get_platform() ==
'win32':
73 grep_cmd =
r'findstr /R "torch soumith mkl magma"' 75 grep_cmd =
r'grep "torch\|soumith\|mkl\|magma"' 76 conda = os.environ.get(
'CONDA_EXE',
'conda')
77 out = run_and_read_all(run_lambda, conda +
' list | ' + grep_cmd)
81 comment_regex = re.compile(
r'^#.*\n')
82 return re.sub(comment_regex,
'', out)
85 def get_gcc_version(run_lambda):
86 return run_and_parse_first_match(run_lambda,
'gcc --version',
r'gcc (.*)')
89 def get_cmake_version(run_lambda):
90 return run_and_parse_first_match(run_lambda,
'cmake --version',
r'cmake (.*)')
93 def get_nvidia_driver_version(run_lambda):
94 if get_platform() ==
'darwin':
95 cmd =
'kextstat | grep -i cuda' 96 return run_and_parse_first_match(run_lambda, cmd,
97 r'com[.]nvidia[.]CUDA [(](.*?)[)]')
98 smi = get_nvidia_smi()
99 return run_and_parse_first_match(run_lambda, smi,
r'Driver Version: (.*?) ')
102 def get_gpu_info(run_lambda):
103 if get_platform() ==
'darwin':
107 smi = get_nvidia_smi()
108 uuid_regex = re.compile(
r' \(UUID: .+?\)')
109 rc, out, _ = run_lambda(smi +
' -L')
113 return re.sub(uuid_regex,
'', out)
116 def get_running_cuda_version(run_lambda):
117 return run_and_parse_first_match(run_lambda,
'nvcc --version',
r'V(.*)$')
120 def get_cudnn_version(run_lambda):
121 """This will return a list of libcudnn.so; it's hard to tell which one is being used""" 122 if get_platform() ==
'win32':
123 cudnn_cmd =
'where /R "%CUDA_PATH%\\bin" cudnn*.dll' 124 elif get_platform() ==
'darwin':
129 cudnn_cmd =
'ls /usr/local/cuda/lib/libcudnn*' 131 cudnn_cmd =
'ldconfig -p | grep libcudnn | rev | cut -d" " -f1 | rev' 132 rc, out, _ = run_lambda(cudnn_cmd)
134 if len(out) == 0
or (rc != 1
and rc != 0):
135 l = os.environ.get(
'CUDNN_LIBRARY')
136 if l
is not None and os.path.isfile(l):
137 return os.path.realpath(l)
140 for fn
in out.split(
'\n'):
141 fn = os.path.realpath(fn)
142 if os.path.isfile(fn):
147 files = list(sorted(files))
150 result =
'\n'.join(files)
151 return 'Probably one of the following:\n{}'.format(result)
154 def get_nvidia_smi():
157 if get_platform() ==
'win32':
158 smi =
'"C:\\Program Files\\NVIDIA Corporation\\NVSMI\\%s"' % smi
163 if sys.platform.startswith(
'linux'):
165 elif sys.platform.startswith(
'win32'):
167 elif sys.platform.startswith(
'cygwin'):
169 elif sys.platform.startswith(
'darwin'):
175 def get_mac_version(run_lambda):
176 return run_and_parse_first_match(run_lambda,
'sw_vers -productVersion',
r'(.*)')
179 def get_windows_version(run_lambda):
180 return run_and_read_all(run_lambda,
'wmic os get Caption | findstr /v Caption')
183 def get_lsb_version(run_lambda):
184 return run_and_parse_first_match(run_lambda,
'lsb_release -a',
r'Description:\t(.*)')
187 def check_release_file(run_lambda):
188 return run_and_parse_first_match(run_lambda,
'cat /etc/*-release',
189 r'PRETTY_NAME="(.*)"')
192 def get_os(run_lambda):
193 platform = get_platform()
195 if platform ==
'win32' or platform ==
'cygwin':
196 return get_windows_version(run_lambda)
198 if platform ==
'darwin':
199 version = get_mac_version(run_lambda)
202 return 'Mac OSX {}'.format(version)
204 if platform ==
'linux':
206 desc = get_lsb_version(run_lambda)
211 desc = check_release_file(run_lambda)
221 def get_pip_packages(run_lambda):
223 def run_with_pip(pip):
224 if get_platform() ==
'win32':
225 grep_cmd =
r'findstr /R "numpy torch"' 227 grep_cmd =
r'grep "torch\|numpy"' 228 return run_and_read_all(run_lambda, pip +
' list --format=freeze | ' + grep_cmd)
231 return 'pip', run_with_pip(
'pip')
234 out2 = run_with_pip(
'pip')
235 out3 = run_with_pip(
'pip3')
237 num_pips = len([x
for x
in [out2, out3]
if x
is not None])
253 pip_version, pip_list_output = get_pip_packages(run_lambda)
256 version_str = torch.__version__
257 debug_mode_str = torch.version.debug
259 cuda_version_str = torch.version.cuda
261 version_str = debug_mode_str = cuda_available_str = cuda_version_str =
'N/A' 264 torch_version=version_str,
265 is_debug_build=debug_mode_str,
266 python_version=
'{}.{}'.format(sys.version_info[0], sys.version_info[1]),
267 is_cuda_available=cuda_available_str,
268 cuda_compiled_version=cuda_version_str,
269 cuda_runtime_version=get_running_cuda_version(run_lambda),
270 nvidia_gpu_models=get_gpu_info(run_lambda),
271 nvidia_driver_version=get_nvidia_driver_version(run_lambda),
272 cudnn_version=get_cudnn_version(run_lambda),
273 pip_version=pip_version,
274 pip_packages=pip_list_output,
275 conda_packages=get_conda_packages(run_lambda),
276 os=get_os(run_lambda),
277 gcc_version=get_gcc_version(run_lambda),
278 cmake_version=get_cmake_version(run_lambda),
282 PyTorch version: {torch_version} 283 Is debug build: {is_debug_build} 284 CUDA used to build PyTorch: {cuda_compiled_version} 287 GCC version: {gcc_version} 288 CMake version: {cmake_version} 290 Python version: {python_version} 291 Is CUDA available: {is_cuda_available} 292 CUDA runtime version: {cuda_runtime_version} 293 GPU models and configuration: {nvidia_gpu_models} 294 Nvidia driver version: {nvidia_driver_version} 295 cuDNN version: {cudnn_version} 297 Versions of relevant libraries: 303 def pretty_str(envinfo):
304 def replace_nones(dct, replacement='Could not collect'):
305 for key
in dct.keys():
306 if dct[key]
is not None:
308 dct[key] = replacement
311 def replace_bools(dct, true='Yes', false='No'):
312 for key
in dct.keys():
315 elif dct[key]
is False:
319 def prepend(text, tag='[prepend]'):
320 lines = text.split(
'\n')
321 updated_lines = [tag + line
for line
in lines]
322 return '\n'.join(updated_lines)
324 def replace_if_empty(text, replacement='No relevant packages'):
325 if text
is not None and len(text) == 0:
329 def maybe_start_on_next_line(string):
331 if string
is not None and len(string.split(
'\n')) > 1:
332 return '\n{}\n'.format(string)
335 mutable_dict = envinfo._asdict()
338 mutable_dict[
'nvidia_gpu_models'] = \
339 maybe_start_on_next_line(envinfo.nvidia_gpu_models)
342 dynamic_cuda_fields = [
343 'cuda_runtime_version',
345 'nvidia_driver_version',
347 all_cuda_fields = dynamic_cuda_fields + [
'cudnn_version']
348 all_dynamic_cuda_fields_missing = all(
349 mutable_dict[field]
is None for field
in dynamic_cuda_fields)
351 for field
in all_cuda_fields:
352 mutable_dict[field] =
'No CUDA' 353 if envinfo.cuda_compiled_version
is None:
354 mutable_dict[
'cuda_compiled_version'] =
'None' 357 mutable_dict = replace_bools(mutable_dict)
360 mutable_dict = replace_nones(mutable_dict)
363 mutable_dict[
'pip_packages'] = replace_if_empty(mutable_dict[
'pip_packages'])
364 mutable_dict[
'conda_packages'] = replace_if_empty(mutable_dict[
'conda_packages'])
368 if mutable_dict[
'pip_packages']:
369 mutable_dict[
'pip_packages'] = prepend(mutable_dict[
'pip_packages'],
370 '[{}] '.format(envinfo.pip_version))
371 if mutable_dict[
'conda_packages']:
372 mutable_dict[
'conda_packages'] = prepend(mutable_dict[
'conda_packages'],
374 return env_info_fmt.format(**mutable_dict)
377 def get_pretty_env_info():
378 return pretty_str(get_env_info())
382 print(
"Collecting environment information...")
383 output = get_pretty_env_info()
387 if __name__ ==
'__main__':
def get_device_name(device=None)