-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytorch_compute_capabilities.py
177 lines (128 loc) · 4.91 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
# ref: https://github.com/moi90/pytorch_compute_capabilities
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
from natsort import natsort_keygen
import pandas as pd
import parse
import tqdm
# if use mirror
# BASE_URL = "https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/linux-64/"
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(pkg_archive_fn, force=False):
if not force and os.path.isfile(pkg_archive_fn):
return
file_url = urllib.parse.urljoin(BASE_URL, pkg_archive_fn)
bar = tqdm.tqdm(desc=f"Downloading {pkg_archive_fn}...")
def _update_bar(blocks_transferred, block_size, total_size):
bar.total = total_size // 1024
bar.update(block_size // 1024)
urllib.request.urlretrieve(file_url, pkg_archive_fn, _update_bar)
bar.close()
def get_lib_fns(pkg_archive_fn) -> List[str]:
pkg_name = strip_extension(pkg_archive_fn)
os.makedirs(pkg_name, exist_ok=True)
lib_fns = glob.glob(os.path.join(pkg_name, "*.so"))
if lib_fns:
return lib_fns
# Else download and extract
download_file(pkg_archive_fn)
try:
tqdm.tqdm.write(f"Reading archive {pkg_archive_fn}...")
with tarfile.open(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 {pkg_archive_fn}/{libname}...")
with open(os.path.join(pkg_name, libname), "wb") as df:
shutil.copyfileobj(tf.extractfile(m), df)
match = True
if not match:
tqdm.tqdm.write(f"{pkg_archive_fn}/*.so not found")
with open(os.path.join(pkg_name, "filelist.txt"), "w") as f:
f.write("\n".join(tf.getnames()))
except (tarfile.TarError, EOFError) as exc:
tqdm.tqdm.write(str(exc))
os.remove(pkg_archive_fn)
return []
else:
return glob.glob(os.path.join(pkg_name, "*.so"))
def get_summary(pkg_archive_fn) -> Mapping[str, str]:
pkg_name = strip_extension(pkg_archive_fn)
summary_fn = os.path.join(pkg_name, "summary.json")
try:
with open(summary_fn) as f:
return json.load(f)
except FileNotFoundError:
pass
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)
pkg_name = strip_extension(pkg_archive_fn)
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(pkg_archive_fn)
except FileNotFoundError:
pass
for fn in lib_fns:
try:
os.remove(fn)
except FileNotFoundError:
pass
return summary
def main():
if not os.path.exists('./repodata.json'):
download_file("repodata.json", force=True)
with open("repodata.json") as f:
repodata = json.load(f)
pkg_archive_fns = []
for pkg_archive_fn, p in repodata["packages"].items():
if p["name"] != "pytorch":
continue
if "cuda" not in p["build"]:
continue
if "py3.6" not in p["build"] and "py3.7" not in p["build"] and "py3.8" not in p["build"] and "py3.9" not in p["build"] and "py3.10" not in p["build"] and "py3.11" not in p["build"] and "py3.12" not in p["build"]:
continue
pkg_archive_fns.append(pkg_archive_fn)
print("Processing packages...")
print()
with multiprocessing.pool.ThreadPool(4) as p:
table = list(p.imap_unordered(get_summary, 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)
with open("table.csv", "w") as f:
table.to_csv(f, index=False)
print("Done.")
if __name__ == "__main__":
main()