forked from InnopolisUni/innofw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpckg_util.py
55 lines (45 loc) · 1.81 KB
/
pckg_util.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
import os
import subprocess
import sys
import logging
import importlib
import importlib.metadata
def install_and_import(package, version="", params="", link=""):
try:
if importlib.metadata.version(package)!=version:
raise ImportError
importlib.import_module(package)
except ImportError:
import pip
installation_str = package
installation_cmd_list = ["install"]
if version:
installation_str += "==" + version
installation_cmd_list.append(installation_str)
if params:
installation_cmd_list.append(params)
if link:
installation_cmd_list.append(link)
subprocess.check_call([sys.executable, "-m", "pip", *installation_cmd_list])
finally:
globals()[package] = importlib.import_module(package)
def execute_bash_command(cmd):
tenv = os.environ.copy()
tenv['LC_ALL'] = "C"
bash_command = cmd
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE, env=tenv)
return process.communicate()[0]
def check_gpu_and_torch_compatibility():
try:
import platform
if platform.system() == 'Windows':
install_and_import("torch", "1.12.1+cu116", "-f", "https://download.pytorch.org/whl/torch_stable.html")
else:
bash_command = "nvidia-smi --query-gpu=name --format=csv"
output = execute_bash_command(bash_command).decode()
if "NVIDIA A100" in output:
install_and_import("torch", "1.11.0+cu113", "-f", "https://download.pytorch.org/whl/torch_stable.html")
install_and_import("torchvision", "0.12.0+cu113", "-f",
"https://download.pytorch.org/whl/torch_stable.html")
except OSError:
logging.info("GPU device is not available")