-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsetup_conda_envs.py
More file actions
229 lines (215 loc) · 11.5 KB
/
setup_conda_envs.py
File metadata and controls
229 lines (215 loc) · 11.5 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
#doing this with python instead of cmd cus it stops running after 1 env setup????
import os, subprocess, sys
def is_linux():
return sys.platform.startswith("linux")
def is_windows():
return sys.platform.startswith("win")
def is_macos():
return sys.platform.startswith("darwin")
mainpath = os.path.dirname(__file__)
if is_windows():
username = os.environ.get('USERNAME')
if os.path.exists(os.path.join(mainpath, "miniconda")):
conda_path = os.path.join(mainpath, "miniconda", "condabin", "conda.bat")
elif os.path.exists(os.path.join("C:", os.sep, "ProgramData", "anaconda3")):
conda_path = os.path.join("C:", os.sep, "ProgramData", "anaconda3", "condabin", "conda.bat")
elif os.path.exists(os.path.join("C:", os.sep, "ProgramData", "miniconda3")):
conda_path = os.path.join("C:", os.sep, "ProgramData", "miniconda3", "condabin", "conda.bat")
elif os.path.exists(os.path.join("C:", os.sep, "Users", username, "anaconda3")):
conda_path = os.path.join("C:", os.sep, "Users", username, "anaconda3", "condabin", "conda.bat")
elif os.path.exists(os.path.join("C:", os.sep, "Users", username, "miniconda3")):
conda_path = os.path.join("C:", os.sep, "Users", username, "miniconda3", "condabin", "conda.bat")
else:
conda_path = "conda"
elif is_macos():
if os.path.exists(os.path.join("opt", "miniconda3")):
conda_path = os.path.join("opt", "miniconda3", "etc", "profile.d", "conda.sh")
elif os.path.exists(os.path.join("opt", "anaconda3")):
conda_path = os.path.join("opt", "anaconda3", "etc", "profile.d", "conda.sh")
else:
conda_path = "conda"
elif is_linux():
username = os.environ.get('USER')
if os.path.exists(os.path.join("home", username, "anaconda3")):
conda_path = os.path.join("home", username, "anaconda3", "etc", "profile.d", "conda.sh")
elif os.path.exists(os.path.join("home", username, "miniconda3")):
conda_path = os.path.join("home", username, "miniconda3", "etc", "profile.d", "conda.sh")
else:
conda_path = "conda"
###create envs###
def run_cmd(cmd):
try:
subprocess.run(cmd, check=True, shell=True)
except subprocess.CalledProcessError as e:
print(f"Error running command: {e}")
def run_cmdA(cmd):
if is_windows():
cmd = f'"{conda_path}" activate difftrainerA >nul && {cmd}'
elif is_linux() or is_macos():
cmd = f'eval "$(conda shell.bash hook)" && {conda_path} activate difftrainerA && {cmd}'
try:
subprocess.run(cmd, check=True, shell=True)
except subprocess.CalledProcessError as e:
print(f"Error running command: {e}")
def run_cmdB(cmd):
if is_windows():
cmd = f'"{conda_path}" activate difftrainerB >nul && {cmd}'
elif is_linux() or is_macos():
cmd = f'eval "$(conda shell.bash hook)" && {conda_path} activate difftrainerB && {cmd}'
try:
subprocess.run(cmd, check=True, shell=True)
except subprocess.CalledProcessError as e:
print(f"Error running command: {e}")
def run_cmdBase(cmd):
if is_windows():
cmd = cmd
elif is_linux() or is_macos():
cmd = f'eval "$(conda shell.bash hook)" && {cmd}'
try:
subprocess.run(cmd, check=True, shell=True)
except subprocess.CalledProcessError as e:
print(f"Error running command: {e}")
print("Creating environment for DiffSinger...")
try:
output = subprocess.check_output(f'"{conda_path}" env list', stderr=subprocess.STDOUT).decode()
lines = output.split("\n")
for line in lines:
if "difftrainerA" in line:
command = [conda_path, "remove", "-n", "difftrainerA", "--all", "--yes"]
yeet = " ".join(command)
run_cmdBase(yeet)
envtxt = os.path.join(mainpath, "assets", "environmentA.yml")
except:
print("Error removing old environment")
run_cmdBase(f'"{conda_path}" env create -f "{envtxt}"')
print("Creating environment for ONNX...")
try:
output = subprocess.check_output(f'"{conda_path}" env list', stderr=subprocess.STDOUT).decode()
lines = output.split("\n")
for line in lines:
if "difftrainerB" in line:
command = [conda_path, "remove", "-n", "difftrainerB", "--all", "--yes"]
yeet = " ".join(command)
run_cmdBase(yeet)
except:
print("Error removing old environment")
envtxt = os.path.join(mainpath, "assets", "environmentB.yml")
run_cmdBase(f'"{conda_path}" env create -f "{envtxt}"')
###setup envs###
print("Setting up training environment...")
try:
output = subprocess.check_output(["nvcc", "--version"], stderr=subprocess.STDOUT).decode()
lines = output.split("\n")
for line in lines:
if "release" in line.lower():
release = line.split(',')[-2]
version = release.split()[1]
print("CUDA version:", version)
if "11.8" <= version < "12.1":
torch = ["pip", "install", "torch==2.4.0+cu118", "torchvision==0.19.0+cu118", "torchaudio==2.4.0", "--extra-index-url", "https://download.pytorch.org/whl/cu118", "--no-warn-script-location"]
nottorch = ["pip", "install", "protobuf", "onnxruntime", "click", "--no-warn-script-location"]
command1 = " ".join(torch)
command2 = " ".join(nottorch)
run_cmdA(command1)
run_cmdA(command2)
break
elif "12.1" <= version < "12.4":
torch = ["pip", "install", "torch==2.4.0+cu121", "torchvision==0.19.0+cu121", "torchaudio==2.4.0", "--extra-index-url", "https://download.pytorch.org/whl/cu121", "--no-warn-script-location"]
nottorch = ["pip", "install", "protobuf", "onnxruntime", "click", "--no-warn-script-location"]
command1 = " ".join(torch)
command2 = " ".join(nottorch)
run_cmdA(command1)
run_cmdA(command2)
break
elif "12.4" <= version < "12.6":
torch = ["pip", "install", "torch==2.4.0+cu124", "torchvision==0.19.0+cu124", "torchaudio==2.4.0", "--extra-index-url", "https://download.pytorch.org/whl/cu124", "--no-warn-script-location"]
nottorch = ["pip", "install", "protobuf", "onnxruntime", "click", "--no-warn-script-location"]
command1 = " ".join(torch)
command2 = " ".join(nottorch)
run_cmdA(command1)
run_cmdA(command2)
break
elif "12.6" <= version < "12.8":
#print("Preferred Torch version not available for this CUDA version, installing latest")
torch = ["pip", "install", "torch==2.8.0+cu126", "torchvision==0.23.0+cu126", "torchaudio==2.8.0", "--extra-index-url", "https://download.pytorch.org/whl/cu126", "--no-warn-script-location"]
nottorch = ["pip", "install", "protobuf", "onnxruntime", "click", "--no-warn-script-location"]
command1 = " ".join(torch)
command2 = " ".join(nottorch)
run_cmdA(command1)
run_cmdA(command2)
break
elif version == "12.8":
#print("Preferred Torch version not available for this CUDA version, installing latest")
torch = ["pip", "install", "torch==2.8.0+cu128", "torchvision==0.23.0+cu128", "torchaudio==2.8.0", "--extra-index-url", "https://download.pytorch.org/whl/cu128", "--no-warn-script-location"]
nottorch = ["pip", "install", "protobuf", "onnxruntime", "click", "--no-warn-script-location"]
command1 = " ".join(torch)
command2 = " ".join(nottorch)
run_cmdA(command1)
run_cmdA(command2)
break
elif version == "12.9":
#print("Preferred Torch version not available for this CUDA version, installing latest")
torch = ["pip", "install", "torch==2.8.0+cu129", "torchvision==0.23.0+cu129", "torchaudio==2.8.0", "--extra-index-url", "https://download.pytorch.org/whl/cu129", "--no-warn-script-location"]
nottorch = ["pip", "install", "protobuf", "onnxruntime", "click", "--no-warn-script-location"]
command1 = " ".join(torch)
command2 = " ".join(nottorch)
run_cmdA(command1)
run_cmdA(command2)
break
elif version > "12.9":
print("CUDA version not supported, trying 12.9")
torch = ["pip", "install", "torch==2.8.0+cu129", "torchvision==0.23.0+cu129", "torchaudio==2.8.0", "--extra-index-url", "https://download.pytorch.org/whl/cu129", "--no-warn-script-location"]
nottorch = ["pip", "install", "protobuf", "onnxruntime", "click", "--no-warn-script-location"]
command1 = " ".join(torch)
command2 = " ".join(nottorch)
run_cmdA(command1)
run_cmdA(command2)
break
else:
print("Unsupported CUDA version detected! Installing generic Torch")
torch = ["pip", "install", "torch==2.4.0", "torchvision==0.19.0", "torchaudio==2.4.0", "--no-warn-script-location"]
nottorch = ["pip", "install", "protobuf", "onnxruntime", "click", "--no-warn-script-location"]
command1 = " ".join(torch)
command2 = " ".join(nottorch)
run_cmdA(command1)
run_cmdA(command2)
break
else:
print("CUDA version not found")
torch = ["pip", "install", "torch==2.4.0", "torchvision==0.19.0", "torchaudio==2.4.0", "--no-warn-script-location"]
nottorch = ["pip", "install", "protobuf", "onnxruntime", "click", "--no-warn-script-location"]
command1 = " ".join(torch)
command2 = " ".join(nottorch)
run_cmdA(command1)
run_cmdA(command2)
except (FileNotFoundError, subprocess.CalledProcessError):
print("CUDA is not available")
if is_macos():
print("Mac detected! Installing latest available for this CPU")
torch = ["pip", "install", "torch<=2.8.0", "torchvision<=0.23.0", "torchaudio<=2.8.0", "--no-warn-script-location"]
nottorch = ["pip", "install", "protobuf", "onnxruntime", "click", "--no-warn-script-location"]
command1 = " ".join(torch)
command2 = " ".join(nottorch)
run_cmdA(command1)
run_cmdA(command2)
else:
torch = ["pip", "install", "torch==2.4.0", "torchvision==0.19.0", "torchaudio==2.4.0", "--no-warn-script-location"]
nottorch = ["pip", "install", "protobuf", "onnxruntime", "click", "--no-warn-script-location"]
command1 = " ".join(torch)
command2 = " ".join(nottorch)
run_cmdA(command1)
run_cmdA(command2)
deac = [conda_path, "deactivate"]
deactivate = " ".join(deac)
run_cmdBase(deactivate)
print("Setting up ONNX environment...")
if is_macos():
torch = ["pip", "install", "torch==1.13.1", "torchvision==0.14.1", "torchaudio==0.13.1", "--no-warn-script-location"]
else:
torch = ["pip", "install", "torch==1.13.1+cpu", "torchvision==0.14.1+cpu", "torchaudio==0.13.1", "--extra-index-url", "https://download.pytorch.org/whl/cpu", "--no-warn-script-location"]
nottorch = ["pip", "install", "protobuf", "onnxruntime", "click", "--no-warn-script-location"]
command1 = " ".join(torch)
command2 = " ".join(nottorch)
run_cmdB(command1)
run_cmdB(command2)
run_cmdBase(deactivate)