|
| 1 | +using Injectio.Attributes; |
| 2 | +using StabilityMatrix.Core.Helper; |
| 3 | +using StabilityMatrix.Core.Helper.Cache; |
| 4 | +using StabilityMatrix.Core.Helper.HardwareInfo; |
| 5 | +using StabilityMatrix.Core.Models.FileInterfaces; |
| 6 | +using StabilityMatrix.Core.Models.Progress; |
| 7 | +using StabilityMatrix.Core.Processes; |
| 8 | +using StabilityMatrix.Core.Python; |
| 9 | +using StabilityMatrix.Core.Services; |
| 10 | + |
| 11 | +namespace StabilityMatrix.Core.Models.Packages; |
| 12 | + |
| 13 | +[RegisterSingleton<BasePackage, ForgeClassic>(Duplicate = DuplicateStrategy.Append)] |
| 14 | +public class ForgeClassic( |
| 15 | + IGithubApiCache githubApi, |
| 16 | + ISettingsManager settingsManager, |
| 17 | + IDownloadService downloadService, |
| 18 | + IPrerequisiteHelper prerequisiteHelper |
| 19 | +) : SDWebForge(githubApi, settingsManager, downloadService, prerequisiteHelper) |
| 20 | +{ |
| 21 | + public override string Name => "forge-classic"; |
| 22 | + public override string Author => "Haoming02"; |
| 23 | + public override string RepositoryName => "sd-webui-forge-classic"; |
| 24 | + public override string DisplayName { get; set; } = "Stable Diffusion WebUI Forge - Classic"; |
| 25 | + public override string MainBranch => "classic"; |
| 26 | + |
| 27 | + public override string Blurb => |
| 28 | + "This fork is focused exclusively on SD1 and SDXL checkpoints, having various optimizations implemented, with the main goal of being the lightest WebUI without any bloatwares."; |
| 29 | + public override string LicenseUrl => |
| 30 | + "https://github.com/Haoming02/sd-webui-forge-classic/blob/classic/LICENSE"; |
| 31 | + public override Uri PreviewImageUri => |
| 32 | + new("https://github.com/Haoming02/sd-webui-forge-classic/raw/classic/html/ui.webp"); |
| 33 | + public override PackageDifficulty InstallerSortOrder => PackageDifficulty.Recommended; |
| 34 | + public override IEnumerable<TorchIndex> AvailableTorchIndices => [TorchIndex.Cuda]; |
| 35 | + public override bool IsCompatible => HardwareHelper.HasNvidiaGpu(); |
| 36 | + public override List<LaunchOptionDefinition> LaunchOptions => |
| 37 | + [ |
| 38 | + new() |
| 39 | + { |
| 40 | + Name = "Host", |
| 41 | + Type = LaunchOptionType.String, |
| 42 | + DefaultValue = "localhost", |
| 43 | + Options = ["--server-name"] |
| 44 | + }, |
| 45 | + new() |
| 46 | + { |
| 47 | + Name = "Port", |
| 48 | + Type = LaunchOptionType.String, |
| 49 | + DefaultValue = "7860", |
| 50 | + Options = ["--port"] |
| 51 | + }, |
| 52 | + new() |
| 53 | + { |
| 54 | + Name = "Share", |
| 55 | + Type = LaunchOptionType.Bool, |
| 56 | + Description = "Set whether to share on Gradio", |
| 57 | + Options = { "--share" } |
| 58 | + }, |
| 59 | + new() |
| 60 | + { |
| 61 | + Name = "Xformers", |
| 62 | + Type = LaunchOptionType.Bool, |
| 63 | + Description = "Set whether to use xformers", |
| 64 | + Options = { "--xformers" } |
| 65 | + }, |
| 66 | + new() |
| 67 | + { |
| 68 | + Name = "Use SageAttention", |
| 69 | + Type = LaunchOptionType.Bool, |
| 70 | + Description = "Set whether to use sage attention", |
| 71 | + Options = { "--sage" } |
| 72 | + }, |
| 73 | + new() |
| 74 | + { |
| 75 | + Name = "Pin Shared Memory", |
| 76 | + Type = LaunchOptionType.Bool, |
| 77 | + Options = { "--pin-shared-memory" }, |
| 78 | + InitialValue = SettingsManager.Settings.PreferredGpu?.IsAmpereOrNewerGpu() ?? false |
| 79 | + }, |
| 80 | + new() |
| 81 | + { |
| 82 | + Name = "CUDA Malloc", |
| 83 | + Type = LaunchOptionType.Bool, |
| 84 | + Options = { "--cuda-malloc" }, |
| 85 | + InitialValue = SettingsManager.Settings.PreferredGpu?.IsAmpereOrNewerGpu() ?? false |
| 86 | + }, |
| 87 | + new() |
| 88 | + { |
| 89 | + Name = "CUDA Stream", |
| 90 | + Type = LaunchOptionType.Bool, |
| 91 | + Options = { "--cuda-stream" }, |
| 92 | + InitialValue = SettingsManager.Settings.PreferredGpu?.IsAmpereOrNewerGpu() ?? false |
| 93 | + }, |
| 94 | + new() |
| 95 | + { |
| 96 | + Name = "Auto Launch", |
| 97 | + Type = LaunchOptionType.Bool, |
| 98 | + Description = "Set whether to auto launch the webui", |
| 99 | + Options = { "--auto-launch" } |
| 100 | + }, |
| 101 | + new() |
| 102 | + { |
| 103 | + Name = "Skip Python Version Check", |
| 104 | + Type = LaunchOptionType.Bool, |
| 105 | + Description = "Set whether to skip python version check", |
| 106 | + Options = { "--skip-python-version-check" }, |
| 107 | + InitialValue = true |
| 108 | + }, |
| 109 | + LaunchOptionDefinition.Extras, |
| 110 | + ]; |
| 111 | + |
| 112 | + public override async Task InstallPackage( |
| 113 | + string installLocation, |
| 114 | + InstalledPackage installedPackage, |
| 115 | + InstallPackageOptions options, |
| 116 | + IProgress<ProgressReport>? progress = null, |
| 117 | + Action<ProcessOutput>? onConsoleOutput = null, |
| 118 | + CancellationToken cancellationToken = default |
| 119 | + ) |
| 120 | + { |
| 121 | + progress?.Report(new ProgressReport(-1f, "Setting up venv", isIndeterminate: true)); |
| 122 | + |
| 123 | + await using var venvRunner = await SetupVenvPure(installLocation).ConfigureAwait(false); |
| 124 | + |
| 125 | + await venvRunner.PipInstall("--upgrade pip wheel", onConsoleOutput).ConfigureAwait(false); |
| 126 | + |
| 127 | + progress?.Report(new ProgressReport(-1f, "Installing requirements...", isIndeterminate: true)); |
| 128 | + |
| 129 | + var requirements = new FilePath(installLocation, "requirements.txt"); |
| 130 | + var requirementsContent = await requirements |
| 131 | + .ReadAllTextAsync(cancellationToken) |
| 132 | + .ConfigureAwait(false); |
| 133 | + |
| 134 | + var pipArgs = new PipInstallArgs(); |
| 135 | + |
| 136 | + var isBlackwell = |
| 137 | + SettingsManager.Settings.PreferredGpu?.IsBlackwellGpu() ?? HardwareHelper.HasBlackwellGpu(); |
| 138 | + var torchVersion = options.PythonOptions.TorchIndex ?? GetRecommendedTorchVersion(); |
| 139 | + |
| 140 | + if (isBlackwell && torchVersion is TorchIndex.Cuda) |
| 141 | + { |
| 142 | + pipArgs = pipArgs |
| 143 | + .AddArg("--pre") |
| 144 | + .WithTorch() |
| 145 | + .WithTorchVision() |
| 146 | + .WithTorchAudio() |
| 147 | + .WithTorchExtraIndex("nightly/cu128") |
| 148 | + .AddArg("--upgrade"); |
| 149 | + |
| 150 | + if (installedPackage.PipOverrides != null) |
| 151 | + { |
| 152 | + pipArgs = pipArgs.WithUserOverrides(installedPackage.PipOverrides); |
| 153 | + } |
| 154 | + progress?.Report( |
| 155 | + new ProgressReport(-1f, "Installing Torch for your shiny new GPU...", isIndeterminate: true) |
| 156 | + ); |
| 157 | + await venvRunner.PipInstall(pipArgs, onConsoleOutput).ConfigureAwait(false); |
| 158 | + } |
| 159 | + else |
| 160 | + { |
| 161 | + pipArgs = pipArgs.WithTorch().WithTorchVision().WithTorchExtraIndex("cu126"); |
| 162 | + } |
| 163 | + |
| 164 | + if (isBlackwell && torchVersion is TorchIndex.Cuda) |
| 165 | + { |
| 166 | + pipArgs = new PipInstallArgs(); |
| 167 | + } |
| 168 | + |
| 169 | + pipArgs = pipArgs.WithParsedFromRequirementsTxt(requirementsContent, excludePattern: "torch"); |
| 170 | + |
| 171 | + if (installedPackage.PipOverrides != null) |
| 172 | + { |
| 173 | + pipArgs = pipArgs.WithUserOverrides(installedPackage.PipOverrides); |
| 174 | + } |
| 175 | + |
| 176 | + await venvRunner.PipInstall(pipArgs, onConsoleOutput).ConfigureAwait(false); |
| 177 | + progress?.Report(new ProgressReport(1f, "Install complete", isIndeterminate: false)); |
| 178 | + } |
| 179 | +} |
0 commit comments