csukuangfj commited on
Commit
4b19472
1 Parent(s): 9d5ca47

update k2 doc

Browse files
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__
__init__.py ADDED
File without changes
update-k2-doc-cpu-linux.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+
3
+ import os
4
+ import re
5
+ from collections import defaultdict
6
+ from pathlib import Path
7
+ from typing import Dict, List, Tuple
8
+
9
+ from update_readme import generate_url, get_all_files
10
+
11
+
12
+ class Wheel:
13
+ def __init__(self, full_name: str, url: str):
14
+ """
15
+ Args:
16
+ full_name:
17
+ Example: k2-1.23.4.dev20230223+cpu.torch1.10.0-cp36-cp36m-linux_x86_64.whl
18
+ """
19
+ self.full_name = full_name
20
+ # pattern = r"k2-(\d)\.(\d+)(\.(\d+))?\.dev(\d{8})+cpu\.torch(\d\.\d+)"
21
+ pattern = (
22
+ r"k2-(\d)\.(\d)+((\.)(\d))?\.dev(\d{8})\+cpu\.torch(\d\.\d+\.\d)-cp(\d+)"
23
+ )
24
+ m = re.search(pattern, full_name)
25
+
26
+ self.k2_major = int(m.group(1))
27
+ self.k2_minor = int(m.group(2))
28
+ self.k2_patch = int(m.group(5))
29
+ self.k2_date = int(m.group(6))
30
+ self.torch_version = m.group(7)
31
+ self.py_version = int(m.group(8))
32
+ self.url = url
33
+
34
+ def __str__(self):
35
+ return self.url
36
+
37
+ def __repr__(self):
38
+ return self.url
39
+
40
+
41
+ def generate_index(filename: str, torch_versions) -> str:
42
+ b = []
43
+ for i in torch_versions:
44
+ b.append(f" ./{i}.rst")
45
+ b = "\n".join(b)
46
+
47
+ s = f"""\
48
+ Pre-compiled CPU wheels (Linux)
49
+ ===============================
50
+
51
+ This page describes pre-compiled ``CPU`` wheels for `k2`_ on Linux.
52
+
53
+ .. toctree::
54
+ :maxdepth: 2
55
+
56
+ {b}
57
+ """
58
+ with open(filename, "w") as f:
59
+ f.write(s)
60
+
61
+
62
+ def sort_by_wheel(x: Wheel):
63
+ return x.k2_major, x.k2_minor, x.k2_patch, x.k2_date, x.py_version
64
+
65
+
66
+ def sort_by_torch(x):
67
+ major, minor, patch = x.split(".")
68
+ return int(major), int(minor), int(patch)
69
+
70
+
71
+ def get_all_torch_versions(wheels: List[Wheel]) -> List[str]:
72
+ ans = set()
73
+ for w in wheels:
74
+ ans.add(w.torch_version)
75
+
76
+ # sort torch version from high to low
77
+ ans = list(ans)
78
+ ans.sort(reverse=True, key=sort_by_torch)
79
+ return ans
80
+
81
+
82
+ def get_doc_dir():
83
+ k2_dir = os.getenv("K2_DIR")
84
+ if k2_dir is None:
85
+ raise ValueError("Please set the environment variable k2_dir")
86
+
87
+ cpu_dir = Path(k2_dir) / "docs/source/installation/pre-compiled-cpu-wheels"
88
+
89
+ if not Path(cpu_dir).is_dir():
90
+ raise ValueError(f"{cpu_dir} does not exist")
91
+
92
+ print(f"k2 doc cpu_dir: {cpu_dir}")
93
+ return cpu_dir
94
+
95
+
96
+ def remove_all_files(d: str):
97
+ files = get_all_files(d, "*.rst")
98
+ for f in files:
99
+ print(f"removing {f}")
100
+ os.remove(f)
101
+
102
+
103
+ def get_all_cpu_wheels():
104
+ cpu = get_all_files("cpu", suffix="*.whl")
105
+ cpu_wheels = generate_url(cpu)
106
+ return cpu_wheels
107
+
108
+
109
+ def generate_file(d: str, torch_version: str, wheels: List[Wheel]) -> str:
110
+ s = f"troch {torch_version}\n"
111
+ s += "=" * len(f"torch {torch_version}")
112
+ s += "\n" * 3
113
+ wheels = filter(lambda w: w.torch_version == torch_version, wheels)
114
+ wheels = list(wheels)
115
+ wheels.sort(reverse=True, key=sort_by_wheel)
116
+ for w in wheels:
117
+ s += f"- `{w.full_name} <{w.url}>`_\n"
118
+
119
+ with open(f"{d}/{torch_version}.rst", "w") as f:
120
+ f.write(s)
121
+
122
+
123
+ def main():
124
+ d = get_doc_dir()
125
+ remove_all_files(d)
126
+
127
+ urls = get_all_cpu_wheels()
128
+
129
+ wheels = []
130
+ for url in urls:
131
+ full_name = url.rsplit("/", maxsplit=1)[1]
132
+ wheels.append(Wheel(full_name, url))
133
+ torch_versions = get_all_torch_versions(wheels)
134
+
135
+ content = []
136
+ for t in torch_versions:
137
+ s = generate_file(d, t, wheels)
138
+
139
+ generate_index(f"{d}/index.rst", torch_versions)
140
+
141
+
142
+ if __name__ == "__main__":
143
+ main()
update-k2-doc-cuda-linux.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+
3
+ import os
4
+ import re
5
+ from collections import defaultdict
6
+ from pathlib import Path
7
+ from typing import Dict, List, Tuple
8
+
9
+ from update_readme import generate_url, get_all_files
10
+
11
+
12
+ class Wheel:
13
+ def __init__(self, full_name: str, url: str):
14
+ """
15
+ Args:
16
+ full_name:
17
+ Example: k2-1.23.4.dev20230224+cuda10.1.torch1.6.0-cp36-cp36m-linux_x86_64.whl
18
+ """
19
+ self.full_name = full_name
20
+ pattern = r"k2-(\d)\.(\d)+((\.)(\d))?\.dev(\d{8})\+cuda(\d+)\.(\d+)\.torch(\d\.\d+\.\d)-cp(\d+)"
21
+ m = re.search(pattern, full_name)
22
+
23
+ self.k2_major = int(m.group(1))
24
+ self.k2_minor = int(m.group(2))
25
+ self.k2_patch = int(m.group(5))
26
+ self.k2_date = int(m.group(6))
27
+ self.cuda_major_version = int(m.group(7))
28
+ self.cuda_minor_version = int(m.group(8))
29
+ self.torch_version = m.group(9)
30
+ self.py_version = int(m.group(10))
31
+ self.url = url
32
+
33
+ def __str__(self):
34
+ return self.url
35
+
36
+ def __repr__(self):
37
+ return self.url
38
+
39
+
40
+ def generate_index(filename: str, torch_versions) -> str:
41
+ b = []
42
+ for i in torch_versions:
43
+ b.append(f" ./{i}.rst")
44
+ b = "\n".join(b)
45
+
46
+ s = f"""\
47
+ Pre-compiled CUDA wheels (Linux)
48
+ ================================
49
+
50
+ This page describes pre-compiled ``CUDA`` wheels for `k2`_ on Linux.
51
+
52
+ .. toctree::
53
+ :maxdepth: 2
54
+
55
+ {b}
56
+ """
57
+ with open(filename, "w") as f:
58
+ f.write(s)
59
+
60
+
61
+ def sort_by_wheel(x: Wheel):
62
+ return (
63
+ x.k2_major,
64
+ x.k2_minor,
65
+ x.k2_patch,
66
+ x.k2_date,
67
+ x.cuda_major_version,
68
+ x.cuda_minor_version,
69
+ x.py_version,
70
+ )
71
+
72
+
73
+ def sort_by_torch(x):
74
+ major, minor, patch = x.split(".")
75
+ return int(major), int(minor), int(patch)
76
+
77
+
78
+ def get_all_torch_versions(wheels: List[Wheel]) -> List[str]:
79
+ ans = set()
80
+ for w in wheels:
81
+ ans.add(w.torch_version)
82
+
83
+ # sort torch version from high to low
84
+ ans = list(ans)
85
+ ans.sort(reverse=True, key=sort_by_torch)
86
+ return ans
87
+
88
+
89
+ def get_doc_dir():
90
+ k2_dir = os.getenv("K2_DIR")
91
+ if k2_dir is None:
92
+ raise ValueError("Please set the environment variable k2_dir")
93
+
94
+ cuda = Path(k2_dir) / "docs/source/installation/pre-compiled-cuda-wheels"
95
+
96
+ if not Path(cuda).is_dir():
97
+ raise ValueError(f"{cuda} does not exist")
98
+
99
+ print(f"k2 doc cuda: {cuda}")
100
+ return cuda
101
+
102
+
103
+ def remove_all_files(d: str):
104
+ files = get_all_files(d, "*.rst")
105
+ for f in files:
106
+ print(f"removing {f}")
107
+ os.remove(f)
108
+
109
+
110
+ def get_all_cuda_wheels():
111
+ cuda = get_all_files("cuda", suffix="*.whl")
112
+ cuda_wheels = generate_url(cuda)
113
+ return cuda_wheels
114
+
115
+
116
+ def generate_file(d: str, torch_version: str, wheels: List[Wheel]) -> str:
117
+ s = f"troch {torch_version}\n"
118
+ s += "=" * len(f"torch {torch_version}")
119
+ s += "\n" * 3
120
+ wheels = filter(lambda w: w.torch_version == torch_version, wheels)
121
+ wheels = list(wheels)
122
+ wheels.sort(reverse=True, key=sort_by_wheel)
123
+ for w in wheels:
124
+ s += f"- `{w.full_name} <{w.url}>`_\n"
125
+
126
+ with open(f"{d}/{torch_version}.rst", "w") as f:
127
+ f.write(s)
128
+
129
+
130
+ def main():
131
+ d = get_doc_dir()
132
+ remove_all_files(d)
133
+
134
+ urls = get_all_cuda_wheels()
135
+
136
+ wheels = []
137
+ for url in urls:
138
+ full_name = url.rsplit("/", maxsplit=1)[1]
139
+ wheels.append(Wheel(full_name, url))
140
+ torch_versions = get_all_torch_versions(wheels)
141
+
142
+ content = []
143
+ for t in torch_versions:
144
+ s = generate_file(d, t, wheels)
145
+
146
+ generate_index(f"{d}/index.rst", torch_versions)
147
+
148
+
149
+ if __name__ == "__main__":
150
+ main()
update-readme.py → update_readme.py RENAMED
@@ -14,8 +14,8 @@ def generate_url(files: List[str]) -> List[str]:
14
  return ans
15
 
16
 
17
- def get_all_files(d: str) -> List[str]:
18
- return sorted(Path(d).glob("*.whl"))
19
 
20
 
21
  def to_markdown(urls: List[str]) -> List[str]:
@@ -32,12 +32,12 @@ def to_file(filename: str, md: List[str]):
32
 
33
 
34
  def main():
35
- cpu = get_all_files("cpu")
36
  cpu_wheels = generate_url(cpu)
37
  cpu_wheels = to_markdown(cpu_wheels)
38
  to_file("cpu.md", cpu_wheels)
39
 
40
- cuda = get_all_files("cuda")
41
  cuda_wheels = generate_url(cuda)
42
  cuda_wheels = to_markdown(cuda_wheels)
43
  to_file("cuda.md", cuda_wheels)
 
14
  return ans
15
 
16
 
17
+ def get_all_files(d: str, suffix: str) -> List[str]:
18
+ return sorted(Path(d).glob(suffix))
19
 
20
 
21
  def to_markdown(urls: List[str]) -> List[str]:
 
32
 
33
 
34
  def main():
35
+ cpu = get_all_files("cpu", suffix="*.whl")
36
  cpu_wheels = generate_url(cpu)
37
  cpu_wheels = to_markdown(cpu_wheels)
38
  to_file("cpu.md", cpu_wheels)
39
 
40
+ cuda = get_all_files("cuda", suffix="*.whl")
41
  cuda_wheels = generate_url(cuda)
42
  cuda_wheels = to_markdown(cuda_wheels)
43
  to_file("cuda.md", cuda_wheels)