forked from LykosAI/StabilityMatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComfyZluda.cs
More file actions
305 lines (262 loc) · 11.1 KB
/
ComfyZluda.cs
File metadata and controls
305 lines (262 loc) · 11.1 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
using System.Diagnostics;
using System.Text.RegularExpressions;
using Injectio.Attributes;
using StabilityMatrix.Core.Exceptions;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Helper;
using StabilityMatrix.Core.Helper.Cache;
using StabilityMatrix.Core.Helper.HardwareInfo;
using StabilityMatrix.Core.Models.FileInterfaces;
using StabilityMatrix.Core.Models.Progress;
using StabilityMatrix.Core.Processes;
using StabilityMatrix.Core.Python;
using StabilityMatrix.Core.Services;
namespace StabilityMatrix.Core.Models.Packages;
[RegisterSingleton<BasePackage, ComfyZluda>(Duplicate = DuplicateStrategy.Append)]
public class ComfyZluda(
IGithubApiCache githubApi,
ISettingsManager settingsManager,
IDownloadService downloadService,
IPrerequisiteHelper prerequisiteHelper,
IPyInstallationManager pyInstallationManager
) : ComfyUI(githubApi, settingsManager, downloadService, prerequisiteHelper, pyInstallationManager)
{
private const string ZludaPatchDownloadUrl =
"https://github.com/lshqqytiger/ZLUDA/releases/download/rel.5e717459179dc272b7d7d23391f0fad66c7459cf/ZLUDA-nightly-windows-rocm6-amd64.zip";
private const string HipSdkExtensionDownloadUrl = "https://cdn.lykos.ai/HIP-SDK-extension.7z";
private const string VenvDirectoryName = "venv";
private Process? zludaProcess;
public override string Name => "ComfyUI-Zluda";
public override string DisplayName => "ComfyUI-Zluda";
public override string Author => "patientx";
public override string LicenseUrl => "https://github.com/patientx/ComfyUI-Zluda/blob/master/LICENSE";
public override string Blurb =>
"Windows-only version of ComfyUI which uses ZLUDA to get better performance with AMD GPUs.";
public override string Disclaimer =>
"Prerequisite install may require admin privileges and a reboot. "
+ "Visual Studio Build Tools for C++ Desktop Development will be installed automatically. "
+ "AMD GPUs under the RX 6800 may require additional manual setup. ";
public override string LaunchCommand => Path.Combine("zluda", "zluda.exe");
public override List<LaunchOptionDefinition> LaunchOptions
{
get
{
var options = base.LaunchOptions;
// Update Cross Attention Method default
var crossAttentionIndex = options.FindIndex(o => o.Name == "Cross Attention Method");
if (crossAttentionIndex != -1)
{
options[crossAttentionIndex] = options[crossAttentionIndex] with
{
InitialValue = "--use-quad-cross-attention",
};
}
// Add new options before Extras (which is usually last)
var extrasIndex = options.FindIndex(o => o.Name == "Extra Launch Arguments");
var insertIndex = extrasIndex != -1 ? extrasIndex : options.Count;
options.Insert(
insertIndex,
new LaunchOptionDefinition
{
Name = "Disable Async Offload",
Type = LaunchOptionType.Bool,
InitialValue = true,
Options = ["--disable-async-offload"],
}
);
options.Insert(
insertIndex + 1,
new LaunchOptionDefinition
{
Name = "Disable Pinned Memory",
Type = LaunchOptionType.Bool,
InitialValue = true,
Options = ["--disable-pinned-memory"],
}
);
options.Insert(
insertIndex + 2,
new LaunchOptionDefinition
{
Name = "Disable Smart Memory",
Type = LaunchOptionType.Bool,
InitialValue = false,
Options = ["--disable-smart-memory"],
}
);
options.Insert(
insertIndex + 3,
new LaunchOptionDefinition
{
Name = "Disable Model/Node Caching",
Type = LaunchOptionType.Bool,
InitialValue = false,
Options = ["--cache-none"],
}
);
return options;
}
}
public override IEnumerable<TorchIndex> AvailableTorchIndices => [TorchIndex.Zluda];
public override TorchIndex GetRecommendedTorchVersion() => TorchIndex.Zluda;
public override PyVersion RecommendedPythonVersion => Python.PyInstallationManager.Python_3_11_13;
public override bool IsCompatible => HardwareHelper.PreferDirectMLOrZluda();
public override bool ShouldIgnoreReleases => true;
public override IEnumerable<PackagePrerequisite> Prerequisites =>
base.Prerequisites.Concat([PackagePrerequisite.HipSdk, PackagePrerequisite.VcBuildTools]);
public override bool InstallRequiresAdmin => true;
public override string AdminRequiredReason =>
"HIP SDK and Visual Studio Build Tools installation, as well as (if applicable) ROCmLibs patching, require admin privileges for accessing files in the Program Files directory. This may take several minutes to complete.";
public override async Task InstallPackage(
string installLocation,
InstalledPackage installedPackage,
InstallPackageOptions options,
IProgress<ProgressReport>? progress = null,
Action<ProcessOutput>? onConsoleOutput = null,
CancellationToken cancellationToken = default
)
{
if (!PrerequisiteHelper.IsHipSdkInstalled) // for updates
{
progress?.Report(new ProgressReport(-1, "Installing HIP SDK 6.4", isIndeterminate: true));
await PrerequisiteHelper
.InstallPackageRequirements(this, options.PythonOptions.PythonVersion, progress)
.ConfigureAwait(false);
}
if (options.IsUpdate)
{
return;
}
progress?.Report(new ProgressReport(-1, "Setting up venv", isIndeterminate: true));
await using var venvRunner = await SetupVenvPure(
installLocation,
pythonVersion: options.PythonOptions.PythonVersion
)
.ConfigureAwait(false);
var installNBatPath = new FilePath(installLocation, "install-n.bat");
var newInstallBatPath = new FilePath(installLocation, "install-sm.bat");
var installNText = await installNBatPath.ReadAllTextAsync(cancellationToken).ConfigureAwait(false);
var installNLines = installNText.Split(Environment.NewLine);
var cutoffIndex = Array.FindIndex(installNLines, line => line.Contains("Installation is completed"));
IEnumerable<string> filtered = installNLines;
if (cutoffIndex >= 0)
{
filtered = installNLines.Take(cutoffIndex);
}
newInstallBatPath.Create();
await newInstallBatPath
.WriteAllTextAsync(string.Join(Environment.NewLine, filtered), cancellationToken)
.ConfigureAwait(false);
var installProcess = ProcessRunner.StartAnsiProcess(
newInstallBatPath,
[],
installLocation,
onConsoleOutput,
GetEnvVars(true)
);
await installProcess.WaitForExitAsync(cancellationToken).ConfigureAwait(false);
progress?.Report(new ProgressReport(1, "Installed Successfully", isIndeterminate: false));
}
public override async Task RunPackage(
string installLocation,
InstalledPackage installedPackage,
RunPackageOptions options,
Action<ProcessOutput>? onConsoleOutput = null,
CancellationToken cancellationToken = default
)
{
if (!PrerequisiteHelper.IsHipSdkInstalled)
{
throw new MissingPrerequisiteException(
"HIP SDK",
"Your package has not yet been upgraded to use HIP SDK 6.4. To continue, please update this package or select \"Change Version\" from the 3-dots menu to have it upgraded automatically for you"
);
}
await SetupVenv(installLocation, pythonVersion: PyVersion.Parse(installedPackage.PythonVersion))
.ConfigureAwait(false);
var zludaPath = Path.Combine(installLocation, LaunchCommand);
ProcessArgs args = ["--", VenvRunner.PythonPath.ToString(), "main.py", .. options.Arguments];
zludaProcess = ProcessRunner.StartAnsiProcess(
zludaPath,
args,
installLocation,
HandleConsoleOutput,
GetEnvVars(false)
);
return;
void HandleConsoleOutput(ProcessOutput s)
{
onConsoleOutput?.Invoke(s);
if (!s.Text.Contains("To see the GUI go to", StringComparison.OrdinalIgnoreCase))
return;
var regex = new Regex(@"(https?:\/\/)([^:\s]+):(\d+)");
var match = regex.Match(s.Text);
if (match.Success)
{
WebUrl = match.Value;
}
OnStartupComplete(WebUrl);
}
}
public override async Task WaitForShutdown()
{
if (zludaProcess is { HasExited: false })
{
zludaProcess.Kill(true);
try
{
await zludaProcess
.WaitForExitAsync(new CancellationTokenSource(5000).Token)
.ConfigureAwait(false);
}
catch (OperationCanceledException e)
{
Console.WriteLine(e);
}
}
zludaProcess = null;
GC.SuppressFinalize(this);
}
private Dictionary<string, string> GetEnvVars(bool isInstall)
{
var portableGitBin = new DirectoryPath(PrerequisiteHelper.GitBinPath);
var hipPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
"AMD",
"ROCm",
"6.4"
);
var hipBinPath = Path.Combine(hipPath, "bin");
var envVars = new Dictionary<string, string>
{
["ZLUDA_COMGR_LOG_LEVEL"] = "1",
["HIP_PATH"] = hipPath,
["HIP_PATH_64"] = hipPath,
["GIT"] = portableGitBin.JoinFile("git.exe"),
};
if (isInstall)
{
envVars["VIRTUAL_ENV"] = VenvDirectoryName;
}
if (envVars.TryGetValue("PATH", out var pathValue))
{
envVars["PATH"] = Compat.GetEnvPathWithExtensions(hipBinPath, portableGitBin, pathValue);
}
else
{
envVars["PATH"] = Compat.GetEnvPathWithExtensions(hipBinPath, portableGitBin);
}
if (isInstall)
return envVars;
envVars["FLASH_ATTENTION_TRITON_AMD_ENABLE"] = "TRUE";
envVars["MIOPEN_FIND_MODE"] = "2";
envVars["MIOPEN_LOG_LEVEL"] = "3";
var gfxArch = PrerequisiteHelper.GetGfxArchFromAmdGpuName();
if (!string.IsNullOrWhiteSpace(gfxArch))
{
envVars["TRITON_OVERRIDE_ARCH"] = gfxArch;
}
envVars.Update(settingsManager.Settings.EnvironmentVariables);
return envVars;
}
}