Skip to content

Instantly share code, notes, and snippets.

@reid3333
Last active April 23, 2023 12:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reid3333/541fab0eb29d4c9558de748ef91a8238 to your computer and use it in GitHub Desktop.
Save reid3333/541fab0eb29d4c9558de748ef91a8238 to your computer and use it in GitHub Desktop.
stable-diffusion-webui for Windows + AMD GPU + DirectML (2023/4/23 ver)

CAUTION!

If the VRAM allocated to the AMD iGPU is small, such as 512MB ("Dedicated GPU Memory" in Task Manager), the following procedure may cause a BSOD.
Please increase the VRAM to a larger value, such as 2GB, from the BIOS in advance.

Install Instruction

  1. Install Latest GPU Driver
  2. Install Python
  3. git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
  4. Open cmd and go to stable-diffusion-webui folder
> cd path\to\stable-diffusion-webui
  1. Create venv
> python -m venv venv
> venv\Scripts\activate.bat
> python -m pip install -U pip
  1. Install PyTorch for CPU (not ROCm) and torch_directml
    (DirectML does not currently support PyTorch 2.0. microsoft/DirectML#415)
> pip install torch==1.13.1 torchvision==0.14.1 torch_directml
  1. Add --skip-torch-cuda-test --precision full --no-half to COMMANDLINE_ARGS= in webui-user.bat
    (DirectML does not currently support automatic mixing precision. microsoft/DirectML#192)

  2. Run webui-user.bat for download external repositories

  3. Modify modules/devices.py for use DirectML

diff --git a/modules/devices.py b/modules/devices.py
--- a/modules/devices.py	(revision 2217331cd1245d0bdda786a5dcaf4f7b843bd7e4)
+++ b/modules/devices.py	(date 1675333882247)
@@ -42,7 +42,11 @@
     if has_mps():
         return "mps"
 
-    return "cpu"
+    try:
+        import torch_directml
+        return torch_directml.device()
+    except ImportError as e:
+        return "cpu"
 
 
 def get_optimal_device():
  1. Modify repositories\k-diffusion\k_diffusion\external.py for workaround DirectML bugs (microsoft/DirectML#368)

This bug has been fixed in torch-directml 0.1.13.1.dev230413, so this step is not necessary.

diff --git a/k_diffusion/external.py b/k_diffusion/external.py
index 79b51ce..c7ba36f 100644
--- a/k_diffusion/external.py
+++ b/k_diffusion/external.py
@@ -69,7 +69,8 @@ class DiscreteSchedule(nn.Module):
         dists = log_sigma - self.log_sigmas[:, None]
         if quantize:
             return dists.abs().argmin(dim=0).view(sigma.shape)
-        low_idx = dists.ge(0).cumsum(dim=0).argmax(dim=0).clamp(max=self.log_sigmas.shape[0] - 2)
+        # low_idx = dists.ge(0).cumsum(dim=0).argmax(dim=0).clamp(max=self.log_sigmas.shape[0] - 2)
+        low_idx = dists.ge(0).to(torch.int32).cumsum(dim=0).argmax(dim=0).clamp(max=self.log_sigmas.shape[0] - 2)
         high_idx = low_idx + 1
         low, high = self.log_sigmas[low_idx], self.log_sigmas[high_idx]
         w = (low - log_sigma) / (low - high)
  1. Stop webui (Ctrl+C)
  2. Run webui-user.bat
  3. Enjoy!

Note: Not all features are confirmed to work. Even if they do, they may work without using the GPU.

Note 2: The following samplers are currently not working: DPM++ SDE, DPM fast, DPM adaptive, DPM++ SDE Karras, DDIM, PLMS

Note 3: Compared to the Linux + ROCm environment, performance is poor (about 1/3 to 1/4) and memory usage is high. Use options to reduce memory usage like --medvram or --lowvram if you crash due to lack of memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment