Safemotion Lib
Loading...
Searching...
No Matches
collect_env.py
Go to the documentation of this file.
1# encoding: utf-8
2"""
3@author: xingyu liao
4@contact: sherlockliao01@gmail.com
5"""
6
7# based on
8# https://github.com/facebookresearch/detectron2/blob/master/detectron2/utils/collect_env.py
9import importlib
10import os
11import re
12import subprocess
13import sys
14from collections import defaultdict
15
16import PIL
17import numpy as np
18import torch
19import torchvision
20from tabulate import tabulate
21
22__all__ = ["collect_env_info"]
23
24
26 try:
27 import torch.__config__
28
29 return torch.__config__.show()
30 except ImportError:
31 # compatible with older versions of pytorch
32 from torch.utils.collect_env import get_pretty_env_info
33
34 return get_pretty_env_info()
35
36
38 var_name = "FASTREID_ENV_MODULE"
39 return var_name, os.environ.get(var_name, "<not set>")
40
41
42def detect_compute_compatibility(CUDA_HOME, so_file):
43 try:
44 cuobjdump = os.path.join(CUDA_HOME, "bin", "cuobjdump")
45 if os.path.isfile(cuobjdump):
46 output = subprocess.check_output(
47 "'{}' --list-elf '{}'".format(cuobjdump, so_file), shell=True
48 )
49 output = output.decode("utf-8").strip().split("\n")
50 sm = []
51 for line in output:
52 line = re.findall(r"\.sm_[0-9]*\.", line)[0]
53 sm.append(line.strip("."))
54 sm = sorted(set(sm))
55 return ", ".join(sm)
56 else:
57 return so_file + "; cannot find cuobjdump"
58 except Exception:
59 # unhandled failure
60 return so_file
61
62
64 has_gpu = torch.cuda.is_available() # true for both CUDA & ROCM
65 torch_version = torch.__version__
66
67 # NOTE: the use of CUDA_HOME and ROCM_HOME requires the CUDA/ROCM build deps, though in
68 # theory detectron2 should be made runnable with only the corresponding runtimes
69 from torch.utils.cpp_extension import CUDA_HOME
70
71 has_rocm = False
72 if tuple(map(int, torch_version.split(".")[:2])) >= (1, 5):
73 from torch.utils.cpp_extension import ROCM_HOME
74
75 if (getattr(torch.version, "hip", None) is not None) and (ROCM_HOME is not None):
76 has_rocm = True
77 has_cuda = has_gpu and (not has_rocm)
78
79 data = []
80 data.append(("sys.platform", sys.platform))
81 data.append(("Python", sys.version.replace("\n", "")))
82 data.append(("numpy", np.__version__))
83
84 try:
85 import fastreid # noqa
86
87 data.append(
88 ("fastreid", fastreid.__version__ + " @" + os.path.dirname(fastreid.__file__))
89 )
90 except ImportError:
91 data.append(("fastreid", "failed to import"))
92
93 data.append(get_env_module())
94 data.append(("PyTorch", torch_version + " @" + os.path.dirname(torch.__file__)))
95 data.append(("PyTorch debug build", torch.version.debug))
96
97 data.append(("GPU available", has_gpu))
98 if has_gpu:
99 devices = defaultdict(list)
100 for k in range(torch.cuda.device_count()):
101 devices[torch.cuda.get_device_name(k)].append(str(k))
102 for name, devids in devices.items():
103 data.append(("GPU " + ",".join(devids), name))
104
105 if has_rocm:
106 data.append(("ROCM_HOME", str(ROCM_HOME)))
107 else:
108 data.append(("CUDA_HOME", str(CUDA_HOME)))
109
110 cuda_arch_list = os.environ.get("TORCH_CUDA_ARCH_LIST", None)
111 if cuda_arch_list:
112 data.append(("TORCH_CUDA_ARCH_LIST", cuda_arch_list))
113 data.append(("Pillow", PIL.__version__))
114
115 try:
116 data.append(
117 (
118 "torchvision",
119 str(torchvision.__version__) + " @" + os.path.dirname(torchvision.__file__),
120 )
121 )
122 if has_cuda:
123 try:
124 torchvision_C = importlib.util.find_spec("torchvision._C").origin
125 msg = detect_compute_compatibility(CUDA_HOME, torchvision_C)
126 data.append(("torchvision arch flags", msg))
127 except ImportError:
128 data.append(("torchvision._C", "failed to find"))
129 except AttributeError:
130 data.append(("torchvision", "unknown"))
131
132 try:
133 import fvcore
134
135 data.append(("fvcore", fvcore.__version__))
136 except ImportError:
137 pass
138
139 try:
140 import cv2
141
142 data.append(("cv2", cv2.__version__))
143 except ImportError:
144 pass
145 env_str = tabulate(data) + "\n"
146 env_str += collect_torch_env()
147 return env_str
148
149
150if __name__ == "__main__":
151 try:
152 import detectron2 # noqa
153 except ImportError:
154 print(collect_env_info())
155 else:
156 from fastreid.utils.collect_env import collect_env_info
157
158 print(collect_env_info())
detect_compute_compatibility(CUDA_HOME, so_file)