-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpytorch_compute_capabilities.py
238 lines (177 loc) · 6.4 KB
/
pytorch_compute_capabilities.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
236
237
238
import fnmatch
import glob
import json
import multiprocessing.pool
import os
import shutil
import subprocess
import tarfile
import urllib.parse
import urllib.request
from typing import List, Mapping, Optional
from natsort import natsort_keygen
import packaging.version
import pandas as pd
import parse
import tqdm
BASE_URL = "https://conda.anaconda.org/pytorch/linux-64/"
def strip_extension(fn: str, extensions=[".tar.bz2", ".tar.gz"]):
for ext in extensions:
if fn.endswith(ext):
return fn[: -len(ext)]
raise ValueError(f"Unexpected extension for filename: {fn}")
def download_file(src, dst, force=False):
if not force and os.path.isfile(dst):
return dst
src_url = urllib.parse.urljoin(BASE_URL, src)
bar = tqdm.tqdm(
desc=f"Downloading {src}...",
unit="B",
unit_scale=True,
unit_divisor=1024,
)
def _update_bar(blocks_transferred, block_size, total_size):
bar.total = total_size
bar.update(block_size)
urllib.request.urlretrieve(src_url, dst, _update_bar)
bar.close()
return dst
def get_lib_fns(raw_pkg_archive_fn) -> List[str]:
pkg_name = strip_extension(raw_pkg_archive_fn)
pkg_cache_dir = os.path.join("cache", pkg_name)
os.makedirs(pkg_cache_dir, exist_ok=True)
lib_fns = glob.glob(os.path.join(pkg_cache_dir, "*.so"))
if lib_fns:
return lib_fns
# Else download and extract
cache_pkg_archive_fn = os.path.join("cache", raw_pkg_archive_fn)
try:
download_file(raw_pkg_archive_fn, cache_pkg_archive_fn)
except Exception as exc:
tqdm.tqdm.write(str(exc))
os.remove(cache_pkg_archive_fn)
raise
try:
tqdm.tqdm.write(f"Reading archive {cache_pkg_archive_fn}...")
with tarfile.open(cache_pkg_archive_fn, "r:*") as tf:
match = False
for m in tf:
libname = os.path.basename(m.name)
if fnmatch.fnmatch(libname, "*.so"):
tqdm.tqdm.write(f"Extracting {cache_pkg_archive_fn}/{libname}...")
with open(os.path.join(pkg_cache_dir, libname), "wb") as df:
shutil.copyfileobj(tf.extractfile(m), df)
match = True
if not match:
tqdm.tqdm.write(f"{cache_pkg_archive_fn}/*.so not found")
with open(os.path.join(pkg_cache_dir, "filelist.txt"), "w") as f:
f.write("\n".join(tf.getnames()))
except (tarfile.TarError, EOFError) as exc:
tqdm.tqdm.write(str(exc))
os.remove(cache_pkg_archive_fn)
return []
else:
return glob.glob(os.path.join(pkg_cache_dir, "*.so"))
def get_cached_summary(pkg_archive_fn: str) -> Optional[Mapping[str, str]]:
pkg_name = strip_extension(pkg_archive_fn)
cache_dir = os.path.join("cache", pkg_name)
summary_fn = os.path.join(cache_dir, "summary.json")
try:
with open(summary_fn) as f:
return json.load(f)
except FileNotFoundError:
return None
def get_summary(pkg_archive_fn) -> Mapping[str, str]:
summary = get_cached_summary(pkg_archive_fn)
if summary is not None:
return summary
pkg_name = strip_extension(pkg_archive_fn)
architectures = set()
lib_fns = get_lib_fns(pkg_archive_fn)
for lib_fn in lib_fns:
tqdm.tqdm.write(f"Reading lib {lib_fn}...")
try:
output = subprocess.check_output(
f'cuobjdump "{lib_fn}"', shell=True
).decode("utf-8")
lib_archs = set(m["arch"] for m in parse.findall("arch = {arch}\n", output))
if not lib_archs:
os.remove(lib_fn)
architectures.update(lib_archs)
except subprocess.CalledProcessError:
os.remove(lib_fn)
cache_dir = os.path.join("cache", pkg_name)
summary_fn = os.path.join(cache_dir, "summary.json")
summary = {"package": pkg_name, "architectures": ", ".join(sorted(architectures))}
# Cleanup package archive
if architectures:
with open(summary_fn, "w") as f:
json.dump(summary, f)
try:
os.remove(os.path.join("cache", pkg_archive_fn))
except FileNotFoundError:
pass
for fn in lib_fns:
try:
os.remove(fn)
except FileNotFoundError:
pass
return summary
def parse_version(text):
return packaging.version.parse(text)
parse_version.pattern = r"\d+(\.\d+)*"
PYTHON_MIN_VER = packaging.version.parse("3.7")
PYTHON_MAX_VER = packaging.version.parse("4")
def main():
print("Loading ")
cached_repodata_fn = download_file(
"repodata.json", os.path.join("cache", "repodata.json"), force=True
)
with open(cached_repodata_fn) as f:
repodata = json.load(f)
pkg_archive_fns = []
for pkg_archive_fn, p in repodata["packages"].items():
if p["name"] != "pytorch":
continue
python_ver = parse.search(
"py{python_ver:ver}", p["build"], extra_types=dict(ver=parse_version)
)
if python_ver is None:
continue
if (PYTHON_MAX_VER < python_ver["python_ver"]) or (
python_ver["python_ver"] < PYTHON_MIN_VER
):
continue
print(pkg_archive_fn, python_ver["python_ver"])
if "cuda" not in p["build"]:
continue
if "py" not in p["build"]:
continue
pkg_archive_fns.append(pkg_archive_fn)
print("Processing packages...")
print()
# Load cached summaries
table = []
remaining_pkg_archive_fns = []
for pkg_archive_fn in pkg_archive_fns:
summary = get_cached_summary(pkg_archive_fn)
if summary is not None:
table.append(summary)
else:
remaining_pkg_archive_fns.append(pkg_archive_fn)
with multiprocessing.pool.ThreadPool(4) as p:
table.extend(
tqdm.tqdm(
p.imap_unordered(get_summary, remaining_pkg_archive_fns),
desc="Processing packages...",
total=len(remaining_pkg_archive_fns),
)
)
table = pd.DataFrame(table)
table = table.sort_values("package", key=natsort_keygen(), ascending=False)
with open("table.md", "w") as f:
table.to_markdown(f, tablefmt="github", index=False)
table.to_csv("table.csv", index=False)
print("Done.")
if __name__ == "__main__":
main()