This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsetup.py
235 lines (203 loc) · 7.2 KB
/
setup.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
import os
from distutils.errors import (
DistutilsPlatformError,
)
from setuptools import setup, find_packages
from setuptools_rust import Binding, RustExtension
import sys
import platform
import shutil
import sys
import tempfile
import urllib.request
from tqdm import tqdm
_nccl_records = []
library_records = {}
class DownloadProgressBar(tqdm):
def update_to(self, b=1, bsize=1, tsize=None):
if tsize is not None:
self.total = tsize
self.update(b * bsize - self.n)
def download_url(url, output_path):
with DownloadProgressBar(unit='B', unit_scale=True,
miniters=1, desc=url.split('/')[-1]) as t:
urllib.request.urlretrieve(
url, filename=output_path, reporthook=t.update_to)
def _make_nccl_url(public_version, filename):
# https://developer.download.nvidia.com/compute/redist/nccl/v2.8/nccl_2.8.4-1+cuda11.2_x86_64.txz
return (
"https://developer.download.nvidia.com/compute/redist/nccl/"
+ "v{}/{}".format(public_version, filename)
)
def _make_nccl_record(cuda_version, full_version, public_version, filename_linux):
return {
"cuda": cuda_version,
"nccl": full_version,
"assets": {
"Linux": {
"url": _make_nccl_url(public_version, filename_linux),
"filename": "libnccl.so.{}".format(full_version),
},
},
}
_nccl_records.append(
_make_nccl_record("11.4", "2.10.3", "2.10",
"nccl_2.10.3-1+cuda11.4_x86_64.txz")
)
_nccl_records.append(
_make_nccl_record("11.3", "2.10.3", "2.10",
"nccl_2.10.3-1+cuda11.0_x86_64.txz")
)
_nccl_records.append(
_make_nccl_record("11.2", "2.10.3", "2.10",
"nccl_2.10.3-1+cuda11.0_x86_64.txz")
)
_nccl_records.append(
_make_nccl_record("11.1", "2.10.3", "2.10",
"nccl_2.10.3-1+cuda11.0_x86_64.txz")
)
_nccl_records.append(
_make_nccl_record("11.0", "2.10.3", "2.10",
"nccl_2.10.3-1+cuda11.0_x86_64.txz")
)
_nccl_records.append(
_make_nccl_record("10.2", "2.10.3", "2.10",
"nccl_2.10.3-1+cuda10.2_x86_64.txz")
)
_nccl_records.append(
_make_nccl_record("10.1", "2.10.3", "2.10",
"nccl_2.10.3-1+cuda10.2_x86_64.txz")
)
library_records["nccl"] = _nccl_records
def install_baguanet(url, destination):
with tempfile.TemporaryDirectory() as tmpdir:
filename = os.path.join(tmpdir, os.path.basename(url))
print("Downloading {}...".format(url))
download_url(url, filename)
outdir = os.path.join(tmpdir, "extract")
shutil.unpack_archive(filename, outdir)
lib_dir = os.path.join(outdir, 'build')
for filename in os.listdir(lib_dir):
shutil.move(os.path.join(lib_dir, filename), destination)
def install_lib(cuda, prefix, library):
record = None
lib_records = library_records
for record in lib_records[library]:
if record["cuda"] == cuda:
break
else:
raise RuntimeError(
"""
The CUDA version({}) specified is not supported.
Should be one of {}.""".format(
cuda, str([x["cuda"] for x in lib_records[library]])
)
)
if prefix is None:
prefix = os.path.expanduser("~/.bagua_core/cuda_lib")
destination = calculate_destination(prefix, cuda, library, record[library])
if os.path.exists(destination):
print("The destination directory {} already exists.".format(destination))
shutil.rmtree(destination)
target_platform = platform.system()
asset = record["assets"].get(target_platform, None)
if asset is None:
raise RuntimeError(
"""
The current platform ({}) is not supported.""".format(
target_platform
)
)
print(
"Installing {} {} for CUDA {} to: {}".format(
library, record[library], record["cuda"], destination
)
)
url = asset["url"]
print("Downloading {}...".format(url))
with tempfile.TemporaryDirectory() as tmpdir:
filename = os.path.join(tmpdir, os.path.basename(url))
download_url(url, filename)
print("Extracting...")
outdir = os.path.join(tmpdir, "extract")
shutil.unpack_archive(filename, outdir)
print("Installing...")
if library == "nccl":
subdir = os.listdir(outdir)
assert len(subdir) == 1
shutil.move(os.path.join(outdir, subdir[0]), destination)
# Install bagua-net
dst_dir = os.path.join(destination, 'bagua-net')
os.mkdir(dst_dir)
install_baguanet(
"https://github.com/BaguaSys/bagua-net/releases/download/v0.1.1/bagua-net_refs.tags.v0.1.1_x86_64.tar.gz",
dst_dir)
else:
assert False
print("Cleaning up...")
print("Done!")
def calculate_destination(prefix, cuda, lib, lib_ver):
"""Calculates the installation directory."""
return os.path.join(prefix, ".data")
def check_torch_version():
try:
import torch
except ImportError:
print("import torch failed, is it installed?")
version = torch.__version__
if version is None:
raise DistutilsPlatformError(
"Unable to determine PyTorch version from the version string '%s'"
% torch.__version__
)
return version
def install_dependency_library():
nvcc_version = (
os.popen(
"nvcc --version | grep release | sed 's/.*release //' | sed 's/,.*//'"
)
.read()
.strip()
)
print("nvcc_version: ", nvcc_version)
install_lib(nvcc_version, os.path.join(cwd, "python/bagua_core"), "nccl")
if __name__ == "__main__":
import colorama
colorama.init(autoreset=True)
cwd = os.path.dirname(os.path.abspath(__file__))
if int(os.getenv("BAGUA_NO_INSTALL_DEPS", 0)) == 0 and \
len(sys.argv) > 1 and sys.argv[1] in ["install", "bdist_wheel"]:
print(
colorama.Fore.BLACK
+ colorama.Back.CYAN
+ "Bagua is automatically installing some system dependencies like NCCL, to disable set env variable BAGUA_NO_INSTALL_DEPS=1",
)
install_dependency_library()
setup(
name="bagua-core",
use_scm_version={"local_scheme": "no-local-version"},
setup_requires=["setuptools_scm"],
url="https://github.com/BaguaSys/bagua-core",
python_requires=">=3.6",
description="Core communication lib for Bagua.",
package_dir={"": "python/"},
packages=find_packages("python/"),
package_data={"": [".data/lib/libnccl.so",
".data/bagua-net/libbagua_net.so",
".data/bagua-net/libnccl-net.so"]},
rust_extensions=[
RustExtension(
"bagua_core.bagua_core",
path="bagua-core-py/Cargo.toml",
binding=Binding.PyO3,
native=True,
)
],
author="Kuaishou AI Platform & DS3 Lab",
author_email="admin@mail.xrlian.com",
install_requires=[
"setuptools_rust",
"colorama",
],
zip_safe=False,
)