-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstaller.py
More file actions
218 lines (184 loc) · 7 KB
/
installer.py
File metadata and controls
218 lines (184 loc) · 7 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
import json
import os
import shutil
import subprocess
import sys
from pathlib import Path
PLATFORM = "windows" if sys.platform == "win32" else "linux" if sys.platform == "linux" else ""
def check_version_and_platform() -> bool:
version = sys.version_info
return False if version.major != 3 and version.minor < 10 else PLATFORM != ""
def check_git_install() -> None:
try:
subprocess.check_call(
"git --version",
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
shell=PLATFORM == "linux",
)
except FileNotFoundError:
print("ERROR: git is not installed, please install git")
return False
return True
# windows only
def set_execution_policy() -> None:
try:
subprocess.check_call(str(Path("installables/change_execution_policy.bat")))
except subprocess.SubprocessError:
try:
subprocess.check_call(str(Path("installables/change_execution_policy_backup.bat")))
except subprocess.SubprocessError as e:
print(f"Failed to change the execution policy with error:\n {e}")
return False
return True
def setup_accelerate(platform: str) -> None:
if platform == "windows":
path = Path(f"{os.environ['USERPROFILE']}")
else:
path = Path.home()
path = path.joinpath(".cache/huggingface/accelerate/default_config.yaml")
if path.exists():
print("Default accelerate config already exists, skipping.")
return
if not path.parent.exists():
path.parent.mkdir(parents=True)
with open("default_config.yaml", "w") as f:
f.write("command_file: null\n")
f.write("commands: null\n")
f.write("compute_environment: LOCAL_MACHINE\n")
f.write("deepspeed_config: {}\n")
f.write("distributed_type: 'NO'\n")
f.write("downcase_fp16: 'NO'\n")
f.write("dynamo_backend: 'NO'\n")
f.write("fsdp_config: {}\n")
f.write("gpu_ids: '0'\n")
f.write("machine_rank: 0\n")
f.write("main_process_ip: null\n")
f.write("main_process_port: null\n")
f.write("main_training_function: main\n")
f.write("megatron_lm_config: {}\n")
f.write("mixed_precision: bf16\n")
f.write("num_machines: 1\n")
f.write("num_processes: 1\n")
f.write("rdzv_backend: static\n")
f.write("same_network: true\n")
f.write("tpu_name: null\n")
f.write("tpu_zone: null\n")
f.write("use_cpu: false")
shutil.move("default_config.yaml", str(path.resolve()))
def check_50_series_gpu():
try:
result = subprocess.run(
["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"],
capture_output=True,
text=True,
check=True,
)
print(result.stdout)
gpu_name = result.stdout.strip()
if "RTX 50" in gpu_name:
return True
elif "NVIDIA" in gpu_name:
return False
else:
return False
except subprocess.CalledProcessError:
print("No NVIDIA GPU detected or nvidia-smi not found.")
return False
except FileNotFoundError:
print("nvidia-smi command not found. Ensure NVIDIA drivers are installed.")
return False
def setup_venv(venv_pip):
if check_50_series_gpu():
subprocess.check_call(
f"{venv_pip} install -U --pre torch torchvision --index-url https://download.pytorch.org/whl/nightly/cu128",
shell=PLATFORM == "linux",
)
print("50 series GPU doesn't have xformers support!")
else:
subprocess.check_call(
f"{venv_pip} install -U torch==2.6.0 torchvision==0.21.0 --index-url https://download.pytorch.org/whl/cu124",
shell=PLATFORM == "linux",
)
subprocess.check_call(
f"{venv_pip} install -U xformers==0.0.29.post3 --index-url https://download.pytorch.org/whl/cu124",
shell=PLATFORM == "linux",
)
if PLATFORM == "windows":
subprocess.check_call("venv\\Scripts\\python.exe ..\\fix_torch.py")
subprocess.check_call(f"{venv_pip} install -U -r requirements.txt", shell=PLATFORM == "linux")
subprocess.check_call(f"{venv_pip} install -U ../custom_scheduler/.", shell=PLATFORM == "linux")
subprocess.check_call(f"{venv_pip} install -U -r ../requirements.txt", shell=PLATFORM == "linux")
subprocess.check_call(f"{venv_pip} install -U ../lycoris/.", shell=PLATFORM == "linux")
# colab only
def setup_colab(venv_pip):
setup_venv(venv_pip)
setup_accelerate("linux")
def ask_yes_no(question: str) -> bool:
reply = None
while reply not in ("y", "n"):
reply = input(f"{question} (y/n): ")
return reply == "y"
def setup_config(colab: bool = False, local: bool = False) -> None:
if colab:
config = {
"remote": True,
"remote_mode": "cloudflared",
"kill_tunnel_on_train_start": True,
"kill_server_on_train_end": True,
"colab": True,
"port": 8000,
}
with open("config.json", "w") as f:
f.write(json.dumps(config, indent=2))
return
is_remote = False if local else ask_yes_no("are you using this remotely?")
remote_mode = "none"
if is_remote:
remote_mode = "ngrok" if ask_yes_no("do you want to use ngrok?") else "cloudflared"
ngrok_token = ""
if remote_mode == "ngrok":
ngrok_token = input(
"copy paste your token from your ngrok dashboard (https://dashboard.ngrok.com/get-started/your-authtoken) (requires account): "
)
with open("config.json", "w") as f:
f.write(
json.dumps(
{
"remote": is_remote,
"remote_mode": remote_mode,
"ngrok_token": ngrok_token,
"port": 8000,
},
indent=2,
)
)
def main():
if not check_version_and_platform() or not check_git_install():
quit()
subprocess.check_call("git submodule init", shell=PLATFORM == "linux")
subprocess.check_call("git submodule update", shell=PLATFORM == "linux")
if PLATFORM == "windows":
print("setting execution policy to unrestricted")
if not set_execution_policy():
quit()
setup_config(
len(sys.argv) > 1 and sys.argv[1] == "colab",
len(sys.argv) > 1 and sys.argv[1] == "local",
)
os.chdir("sd_scripts")
if PLATFORM == "windows":
pip = Path("venv/Scripts/pip.exe")
else:
pip = Path("venv/bin/pip")
print("creating venv and installing requirements")
subprocess.check_call(f"{sys.executable} -m venv venv", shell=PLATFORM == "linux")
if len(sys.argv) > 1 and sys.argv[1] == "colab":
setup_colab(pip)
print("completed installing")
quit()
setup_venv(pip)
setup_accelerate(PLATFORM)
print("Completed installing, you can run the server via the run.bat or run.sh files")
if __name__ == "__main__":
main()