-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathutils.py
More file actions
382 lines (318 loc) · 11.9 KB
/
utils.py
File metadata and controls
382 lines (318 loc) · 11.9 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import os
import sys
import platform
import subprocess
import sysconfig
import io
import shlex
from typing import Optional
# get cpu architecture
def get_cpu_arch() -> str:
arch = platform.machine()
if "x86" in arch or "amd64" in arch:
return "x86"
elif "arm" in arch or "aarch64" in arch:
return "arm"
else:
raise ValueError(f"❌ Unsupported architecture: {arch}")
# get device type
def get_device_type() -> str:
import torch
if torch.cuda.is_available():
try:
import ixformer
return "ilu"
except ImportError:
return "cuda"
try:
import torch_musa
if torch.musa.is_available():
return "musa"
except ImportError:
pass
try:
import torch_mlu
if torch.mlu.is_available():
return "mlu"
except ImportError:
pass
try:
import torch_npu
if torch.npu.is_available():
return "npu"
except ImportError:
pass
print("❌ Unsupported device type, please check what device you are using.")
exit(1)
def get_base_dir() -> str:
return os.path.abspath(os.path.dirname(__file__))
def _join_path(*paths: str) -> str:
return os.path.join(get_base_dir(), *paths)
# return the python version as a string like "310" or "311" etc
def get_python_version() -> str:
return sysconfig.get_python_version().replace(".", "")
def get_torch_version(device: str) -> Optional[str]:
try:
import torch
if device == "cuda":
return torch.__version__
return torch.__version__.split('+')[0]
except ImportError:
return None
def get_version() -> str:
# first read from environment variable
version: Optional[str] = os.getenv("XLLM_VERSION")
if not version:
# then read from version file
with open(_join_path("version.txt"), "r") as f:
version = f.read().strip()
# strip the leading 'v' if present
if version and version.startswith("v"):
version = version[1:]
if not version:
raise RuntimeError("❌ Unable to find version string.")
version_suffix = os.getenv("XLLM_VERSION_SUFFIX")
if version_suffix:
version += version_suffix
return version
def read_readme() -> str:
p = _join_path("README.md")
if os.path.isfile(p):
return io.open(p, "r", encoding="utf-8").read()
else:
return ""
def get_cmake_dir() -> str:
plat_name = sysconfig.get_platform()
python_version = get_python_version()
dir_name = f"cmake.{plat_name}-{sys.implementation.name}-{python_version}"
cmake_dir = os.path.join(get_base_dir(), "build", dir_name)
os.makedirs(cmake_dir, exist_ok=True)
return cmake_dir
def check_and_install_pre_commit() -> None:
# check if .git is a directory
if not os.path.isdir(".git"):
return
if not os.path.exists(".git/hooks/pre-commit"):
ok, _, _ = _run_command(["pre-commit", "install"], check=True)
if not ok:
print("❌ Run 'pre-commit install' failed. Please install pre-commit: pip install pre-commit")
exit(1)
def _run_command(
args: list[str],
cwd: Optional[str] = None,
check: bool = True,
input_text: Optional[str] = None,
passthrough_output: bool = False,
) -> tuple[bool, str, str]:
try:
if passthrough_output:
result = subprocess.run(
args,
cwd=cwd,
check=False,
input=input_text,
text=True,
)
else:
result = subprocess.run(
args,
cwd=cwd,
check=False,
input=input_text,
capture_output=True,
text=True,
)
except OSError as e:
return False, "", str(e)
if passthrough_output:
if check and result.returncode != 0:
return False, "", f"exit code {result.returncode}"
return result.returncode == 0, "", ""
if check and result.returncode != 0:
return False, result.stdout.strip(), (result.stderr or result.stdout).strip()
return result.returncode == 0, result.stdout.strip(), (result.stderr or "").strip()
def _print_manual_check_commands(commands: list[str]) -> None:
print("🔎 You can run these commands to inspect manually:")
for cmd in commands:
print(f" {cmd}")
def _run_shell_command(
command: str,
cwd: Optional[str] = None,
check: bool = True,
passthrough_output: bool = False,
) -> bool:
ok, _, err = _run_command(
shlex.split(command),
cwd=cwd,
check=check,
passthrough_output=passthrough_output,
)
if not ok:
print(f"❌ Run shell command '{command}' failed: {err}")
return False
return True
def _run_git_command(repo_root: str, args: list[str]) -> tuple[bool, str]:
ok, output, err = _run_command(["git"] + args, cwd=repo_root, check=True)
if not ok and "No such file or directory" in err:
print(f"❌ Failed to run git command in {repo_root}: git {' '.join(args)}")
print(f" {err}")
return False, ""
if not ok:
print(f"❌ Git command failed in {repo_root}: git {' '.join(args)}")
if err:
print(f" {err}")
return False, ""
return True, output
def _collect_submodule_init_issues(repo_root: str) -> dict[str, str]:
ok, output = _run_git_command(repo_root, ["submodule", "status"])
if not ok:
print("❌ Failed to inspect submodule status.")
_print_manual_check_commands([
f"cd {repo_root}",
"git submodule status",
"git submodule update --init",
])
exit(1)
issues: dict[str, str] = {}
for line in output.splitlines():
if not line:
continue
state = line[0]
content = line[1:].strip()
parts = content.split()
if len(parts) < 2:
continue
path = parts[1]
commit = parts[0]
if state == "-":
issues[path] = f"uninitialized (expected commit starts with {commit})"
elif state == "+":
issues[path] = f"commit mismatch (checked-out commit starts with {commit})"
elif state == "U":
issues[path] = "merge conflict"
return issues
def _is_dependency_installed(required_files: list[str]) -> bool:
normalized_files = [
os.path.abspath(os.path.expanduser(file_path))
for file_path in required_files
]
return all(os.path.isfile(file_path) for file_path in normalized_files)
def _get_required_dependency_files() -> dict[str, list[str]]:
install_prefix = "/usr/local/yalantinglibs"
return {
"yalantinglibs": [
os.path.join(
install_prefix,
"lib",
"cmake",
"yalantinglibs",
"config.cmake",
),
],
}
def _collect_missing_dependencies(
dependency_files: dict[str, list[str]],
) -> dict[str, list[str]]:
missing: dict[str, list[str]] = {}
for name, required_files in dependency_files.items():
normalized_files = [
os.path.abspath(os.path.expanduser(file_path))
for file_path in required_files
]
if not _is_dependency_installed(normalized_files):
missing[name] = normalized_files
return missing
def _export_cmake_prefix_paths(prefix_paths: list[str]) -> None:
existing = os.environ.get("CMAKE_PREFIX_PATH", "")
merged_paths: list[str] = []
for path in existing.split(os.pathsep):
if not path:
continue
normalized_path = os.path.abspath(os.path.expanduser(path))
if normalized_path and normalized_path not in merged_paths:
merged_paths.append(normalized_path)
for path in prefix_paths:
if not path:
continue
normalized_path = os.path.abspath(os.path.expanduser(path))
if normalized_path and normalized_path not in merged_paths:
merged_paths.append(normalized_path)
if not merged_paths:
return
os.environ["CMAKE_PREFIX_PATH"] = os.pathsep.join(merged_paths)
print(f"✅ Export CMAKE_PREFIX_PATH to environment: {os.environ['CMAKE_PREFIX_PATH']}")
def _run_dependencies_script_or_exit(script_path: str) -> None:
if not _run_shell_command(
"sh third_party/dependencies.sh",
cwd=script_path,
passthrough_output=True,
):
print("❌ Run shell command 'sh third_party/dependencies.sh' failed!")
_print_manual_check_commands([
f"cd {script_path}",
"sh third_party/dependencies.sh",
])
exit(1)
def _validate_submodules_or_exit(repo_root: str) -> None:
issues = _collect_submodule_init_issues(repo_root)
if issues:
print("❌ Submodule commit check failed. Repositories not correctly initialized:")
for path in sorted(issues):
print(f" - {path}: {issues[path]}")
print("\nPlease align submodules and try again:")
print(" git submodule update --init [-f|--force]")
exit(1)
def _ensure_prebuild_dependencies_installed(script_path: str) -> None:
dependency_files = _get_required_dependency_files()
missing_dependencies = _collect_missing_dependencies(dependency_files)
if missing_dependencies:
missing_names = ", ".join(sorted(missing_dependencies))
print(f"ℹ️ Missing third-party dependencies: {missing_names}. Running dependencies.sh ...")
_run_dependencies_script_or_exit(script_path)
missing_dependencies = _collect_missing_dependencies(dependency_files)
if missing_dependencies:
print("❌ Some third-party dependencies are still missing after running dependencies.sh:")
manual_commands = [f"cd {script_path}", "sh third_party/dependencies.sh"]
for name in sorted(missing_dependencies):
print(f" - {name}")
for file_path in missing_dependencies[name]:
print(f" missing file: {file_path}")
manual_commands.append(f"test -f {file_path}")
_print_manual_check_commands(manual_commands)
exit(1)
_export_cmake_prefix_paths(["/usr/local/yalantinglibs"])
def _get_cmake_cache_path() -> str:
plat_name = sysconfig.get_platform()
dir_name = f"cmake.{plat_name}-{sys.implementation.name}-{get_python_version()}"
return os.path.join(get_base_dir(), "build", dir_name, "CMakeCache.txt")
def _get_xllm_ops_marker_path() -> str:
ascend_home = os.getenv("ASCEND_HOME_PATH", "/usr/local/Ascend/ascend-toolkit/latest")
opp_root = os.path.join(ascend_home, "opp")
return os.path.join(opp_root, "vendors", "xllm", ".xllm_ops_git_head")
def _clear_xllm_ops_cache_git_head(cache_path: str) -> bool:
if not os.path.isfile(cache_path):
return False
cache_prefix = "XLLM_OPS_GIT_HEAD_CACHED:"
with open(cache_path, "r", encoding="utf-8") as cache_file:
old_lines = cache_file.readlines()
new_lines = [line for line in old_lines if not line.startswith(cache_prefix)]
if new_lines == old_lines:
return False
temp_file_path = f"{cache_path}.tmp"
with open(temp_file_path, "w", encoding="utf-8") as cache_file:
cache_file.writelines(new_lines)
os.replace(temp_file_path, cache_path)
return True
def _ensure_xllm_ops_rebuild_on_missing_marker() -> None:
marker_path = _get_xllm_ops_marker_path()
if os.path.isfile(marker_path):
return
cmake_cache_path = _get_cmake_cache_path()
if _clear_xllm_ops_cache_git_head(cmake_cache_path):
print("✅ Cleared XLLM_OPS_GIT_HEAD_CACHED from CMake cache to trigger xllm_ops rebuild.")
return
def pre_build() -> None:
script_path = os.path.dirname(os.path.abspath(__file__))
_validate_submodules_or_exit(script_path)
_ensure_prebuild_dependencies_installed(script_path)
_ensure_xllm_ops_rebuild_on_missing_marker()