Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jantic
GitHub Repository: jantic/deoldify
Path: blob/master/fastai/utils/collect_env.py
781 views
1
"Utility functions to help deal with user environment"
2
3
from ..imports.torch import *
4
from ..core import *
5
from ..script import *
6
from .pynvml_gate import *
7
import fastprogress, subprocess, platform
8
9
__all__ = ['show_install', 'check_perf']
10
11
def get_env(name):
12
"Return env var value if it's defined and not an empty string, or return Unknown"
13
res = os.environ.get(name,'')
14
return res if len(res) else "Unknown"
15
16
def show_install(show_nvidia_smi:bool=False):
17
"Print user's setup information"
18
19
import platform, fastai.version
20
21
rep = []
22
opt_mods = []
23
24
rep.append(["=== Software ===", None])
25
rep.append(["python", platform.python_version()])
26
rep.append(["fastai", fastai.__version__])
27
rep.append(["fastprogress", fastprogress.__version__])
28
rep.append(["torch", torch.__version__])
29
30
# nvidia-smi
31
cmd = "nvidia-smi"
32
have_nvidia_smi = False
33
try: result = subprocess.run(cmd.split(), shell=False, check=False, stdout=subprocess.PIPE)
34
except: pass
35
else:
36
if result.returncode == 0 and result.stdout: have_nvidia_smi = True
37
38
# XXX: if nvidia-smi is not available, another check could be:
39
# /proc/driver/nvidia/version on most systems, since it's the
40
# currently active version
41
42
if have_nvidia_smi:
43
smi = result.stdout.decode('utf-8')
44
# matching: "Driver Version: 396.44"
45
match = re.findall(r'Driver Version: +(\d+\.\d+)', smi)
46
if match: rep.append(["nvidia driver", match[0]])
47
48
available = "available" if torch.cuda.is_available() else "**Not available** "
49
rep.append(["torch cuda", f"{torch.version.cuda} / is {available}"])
50
51
# no point reporting on cudnn if cuda is not available, as it
52
# seems to be enabled at times even on cpu-only setups
53
if torch.cuda.is_available():
54
enabled = "enabled" if torch.backends.cudnn.enabled else "**Not enabled** "
55
rep.append(["torch cudnn", f"{torch.backends.cudnn.version()} / is {enabled}"])
56
57
rep.append(["\n=== Hardware ===", None])
58
59
# it's possible that torch might not see what nvidia-smi sees?
60
gpu_total_mem = []
61
nvidia_gpu_cnt = 0
62
if have_nvidia_smi:
63
try:
64
cmd = "nvidia-smi --query-gpu=memory.total --format=csv,nounits,noheader"
65
result = subprocess.run(cmd.split(), shell=False, check=False, stdout=subprocess.PIPE)
66
except:
67
print("have nvidia-smi, but failed to query it")
68
else:
69
if result.returncode == 0 and result.stdout:
70
output = result.stdout.decode('utf-8')
71
gpu_total_mem = [int(x) for x in output.strip().split('\n')]
72
nvidia_gpu_cnt = len(gpu_total_mem)
73
74
75
if nvidia_gpu_cnt: rep.append(["nvidia gpus", nvidia_gpu_cnt])
76
77
torch_gpu_cnt = torch.cuda.device_count()
78
if torch_gpu_cnt:
79
rep.append(["torch devices", torch_gpu_cnt])
80
# information for each gpu
81
for i in range(torch_gpu_cnt):
82
rep.append([f" - gpu{i}", (f"{gpu_total_mem[i]}MB | " if gpu_total_mem else "") + torch.cuda.get_device_name(i)])
83
else:
84
if nvidia_gpu_cnt:
85
rep.append([f"Have {nvidia_gpu_cnt} GPU(s), but torch can't use them (check nvidia driver)", None])
86
else:
87
rep.append([f"No GPUs available", None])
88
89
90
rep.append(["\n=== Environment ===", None])
91
92
rep.append(["platform", platform.platform()])
93
94
if platform.system() == 'Linux':
95
distro = try_import('distro')
96
if distro:
97
# full distro info
98
rep.append(["distro", ' '.join(distro.linux_distribution())])
99
else:
100
opt_mods.append('distro');
101
# partial distro info
102
rep.append(["distro", platform.uname().version])
103
104
rep.append(["conda env", get_env('CONDA_DEFAULT_ENV')])
105
rep.append(["python", sys.executable])
106
rep.append(["sys.path", "\n".join(sys.path)])
107
108
print("\n\n```text")
109
110
keylen = max([len(e[0]) for e in rep if e[1] is not None])
111
for e in rep:
112
print(f"{e[0]:{keylen}}", (f": {e[1]}" if e[1] is not None else ""))
113
114
if have_nvidia_smi:
115
if show_nvidia_smi: print(f"\n{smi}")
116
else:
117
if torch_gpu_cnt: print("no nvidia-smi is found")
118
else: print("no supported gpus found on this system")
119
120
print("```\n")
121
122
print("Please make sure to include opening/closing ``` when you paste into forums/github to make the reports appear formatted as code sections.\n")
123
124
if opt_mods:
125
print("Optional package(s) to enhance the diagnostics can be installed with:")
126
print(f"pip install {' '.join(opt_mods)}")
127
print("Once installed, re-run this utility to get the additional information")
128
129
def pypi_module_version_is_available(module, version):
130
"Check whether module==version is available on pypi"
131
# returns True/False (or None if failed to execute the check)
132
133
# using a hack that when passing "module==" w/ no version number to pip
134
# it "fails" and returns all the available versions in stderr
135
try:
136
cmd = f"pip install {module}=="
137
result = subprocess.run(cmd.split(), shell=False, check=False,
138
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
139
except Exception as e:
140
print(f"Error: {e}")
141
return None
142
else:
143
if result.returncode == 1 and result.stderr:
144
output = result.stderr.decode('utf-8')
145
return True if version in output else False
146
else:
147
print(f"Some error in {cmd}")
148
return None
149
150
def check_perf():
151
"Suggest how to improve the setup to speed things up"
152
153
from PIL import features, Image
154
from packaging import version
155
156
print("Running performance checks.")
157
158
# libjpeg_turbo check
159
print("\n*** libjpeg-turbo status")
160
if version.parse(Image.PILLOW_VERSION) >= version.parse("5.3.9"):
161
if features.check_feature('libjpeg_turbo'):
162
print("✔ libjpeg-turbo is on")
163
else:
164
print("✘ libjpeg-turbo is not on. It's recommended you install libjpeg-turbo to speed up JPEG decoding. See https://docs.fast.ai/performance.html#libjpeg-turbo")
165
else:
166
print(f"❓ libjpeg-turbo's status can't be derived - need Pillow(-SIMD)? >= 5.4.0 to tell, current version {Image.PILLOW_VERSION}")
167
# XXX: remove this check/note once Pillow and Pillow-SIMD 5.4.0 is available
168
pillow_ver_5_4_is_avail = pypi_module_version_is_available("Pillow", "5.4.0")
169
if pillow_ver_5_4_is_avail == False:
170
print("5.4.0 is not yet available, other than the dev version on github, which can be installed via pip from git+https://github.com/python-pillow/Pillow. See https://docs.fast.ai/performance.html#libjpeg-turbo")
171
172
# Pillow-SIMD check
173
print("\n*** Pillow-SIMD status")
174
if re.search(r'\.post\d+', Image.PILLOW_VERSION):
175
print(f"✔ Running Pillow-SIMD {Image.PILLOW_VERSION}")
176
else:
177
print(f"✘ Running Pillow {Image.PILLOW_VERSION}; It's recommended you install Pillow-SIMD to speed up image resizing and other operations. See https://docs.fast.ai/performance.html#pillow-simd")
178
179
# CUDA version check
180
# compatibility table: k: min nvidia ver is required for v: cuda ver
181
# note: windows nvidia driver version is slightly higher, see:
182
# https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html
183
# note: add new entries if pytorch starts supporting new cudaXX
184
nvidia2cuda = {
185
"410.00": "10.0",
186
"384.81": "9.0",
187
"367.48": "8.0",
188
}
189
print("\n*** CUDA status")
190
if torch.cuda.is_available():
191
pynvml = load_pynvml_env()
192
nvidia_ver = (pynvml.nvmlSystemGetDriverVersion().decode('utf-8') if platform.system() != "Darwin" else "Cannot be determined on OSX yet")
193
cuda_ver = torch.version.cuda
194
max_cuda = "8.0"
195
for k in sorted(nvidia2cuda.keys()):
196
if version.parse(nvidia_ver) > version.parse(k): max_cuda = nvidia2cuda[k]
197
if version.parse(str(max_cuda)) <= version.parse(cuda_ver):
198
print(f"✔ Running the latest CUDA {cuda_ver} with NVIDIA driver {nvidia_ver}")
199
else:
200
print(f"✘ You are running pytorch built against cuda {cuda_ver}, your NVIDIA driver {nvidia_ver} supports cuda10. See https://pytorch.org/get-started/locally/ to install pytorch built against the faster CUDA version.")
201
else:
202
print(f"❓ Running cpu-only torch version, CUDA check is not relevant")
203
204
print("\nRefer to https://docs.fast.ai/performance.html to make sense out of these checks and suggestions.")
205
206