forked from xLLM-AI/xllm
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
212 lines (180 loc) · 7.14 KB
/
Copy pathutils.py
File metadata and controls
212 lines (180 loc) · 7.14 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
import os
import sys
import platform
import subprocess
import sysconfig
import io
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 "a2"
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"):
try:
subprocess.run(["pre-commit", "install"], check=True, capture_output=True, text=True)
except (subprocess.CalledProcessError, FileNotFoundError):
print("❌ Run 'pre-commit install' failed. Please install pre-commit: pip install pre-commit")
exit(1)
def run_shell_command(command: str, cwd: Optional[str] = None, check: bool = True) -> bool:
try:
import shlex
subprocess.run(shlex.split(command), cwd=cwd, check=check, capture_output=True, text=True)
return True
except (subprocess.CalledProcessError, FileNotFoundError) as e:
print(f"❌ Run shell command '{command}' failed: {e.stderr.strip() if hasattr(e, 'stderr') else e}")
return False
def has_uncommitted_changes(repo_path: str) -> bool:
result = subprocess.run(
["git", "status", "--porcelain"],
cwd=repo_path,
capture_output=True,
text=True
)
return bool(result.stdout.strip())
def is_safe_directory_set(repo_path: str) -> bool:
try:
result = subprocess.run(
["git", "config", "--global", "--get-all", "safe.directory"],
capture_output=True,
text=True,
check=True
)
existing_paths = result.stdout.strip().split("\n")
return repo_path in existing_paths
except subprocess.CalledProcessError:
return False
def apply_patch_safely(patch_file_path: str, repo_path: str) -> bool:
print(f"🔍 Checking repo status: {repo_path}")
if not is_safe_directory_set(repo_path):
try:
subprocess.run(
["git", "config", "--global", "--add", "safe.directory", repo_path],
check=True
)
print(f"✅ Add safe.directory success: {repo_path}")
except subprocess.CalledProcessError as e:
print(f"❌ Add safe.directory fail: {e.stderr.strip()}")
print(f" Please manually set 'git config --global --add safe.directory {repo_path}'")
return False
if has_uncommitted_changes(repo_path):
print(f"⚠️ Uncommitted changes detected. Running `git reset --hard` for {repo_path}")
if not run_shell_command("git reset --hard", cwd=repo_path):
print("❌ Failed to reset changes!")
return False
print(f"🛠️ Apply patch: {patch_file_path}")
apply_success = run_shell_command(f"git apply --check {patch_file_path}", cwd=repo_path, check=False)
if apply_success:
if not run_shell_command(f"git apply {patch_file_path}", cwd=repo_path):
print(f"❌ Apply patch '{patch_file_path}' failed!")
apply_success = False
if apply_success:
print("🎉 Success apply patch!")
return True
else:
print("\n❌ Conflicts detected! Please resolve manually and retry:")
print(f" cd {repo_path} && git apply {patch_file_path}")
return False
def pre_build(device: str) -> None:
if os.path.exists("third_party/custom_patch"):
script_path = os.path.dirname(os.path.abspath(__file__))
mooncake_repo_path = os.path.join(script_path, "third_party/Mooncake")
if device in ("a2", "a3"):
if not apply_patch_safely("../custom_patch/Mooncake_npu.patch", mooncake_repo_path):
print("❌ Failed to apply Mooncake_npu.patch!")
exit(1)
else:
if not apply_patch_safely("../custom_patch/Mooncake.patch", mooncake_repo_path):
print("❌ Failed to apply Mooncake.patch!")
exit(1)
cpprestsdk_repo_path = os.path.join(script_path, "third_party/cpprestsdk")
if not apply_patch_safely("../custom_patch/cpprestsdk.patch", cpprestsdk_repo_path):
print("❌ Failed to apply cpprestsdk.patch!")
exit(1)
if not run_shell_command("sh third_party/dependencies.sh", cwd=script_path):
print("❌ Run shell command 'sh third_party/dependencies.sh' failed!")
exit(1)
# export CMAKE_PREFIX_PATH to environment for yalantinglibs or other dependencies
os.environ["CMAKE_PREFIX_PATH"] = os.path.join(os.path.expanduser("~"), ".local")
print(f"✅ Export CMAKE_PREFIX_PATH to environment: {os.environ['CMAKE_PREFIX_PATH']}")