Last Updated on October 6, 2022 by Jay
This tutorial will walk through how to run the Stable Diffusion AI software using an AMD GPU on the Windows 10 operating system.
AMD Radeon RX 580 with 8GB of video RAM.
CPU and RAM are kind of irrelevant, any modern computer should be fine.
0. Download & Install Python & Git
The source code Stable Diffusion model/software is written in Python, so we’ll need to install Python first. We’ll need to get Python version 3.7+ (64-bit) to run Stable Diffusion. You can download the Python installation file from either one of the following resources:
Official Python website: https://www.python.org/downloads/
Anaconda Distribution: https://www.anaconda.com/
Python installation is fairly straightforward so we won’t cover that here.
Git is also required, without Git the program will not install properly.
Download Git: https://git-scm.com/downloads
Installing Git is also straightforward, the installer wizard will ask a lot of questions, just go with default for everything.
1. Download Files
1.1 Download Code – Modified Diffusers Library
Download this copy of the diffusers library: https://github.com/harishanand95/diffusers/tree/dml
Note it should be the “dml” branch, not the main branch.
Once downloaded, unzip the content to a folder of your choice.
1.2 Download Onnx Nightly Build Wheel File
Download the wheel file from the below link. Wheel files are Python libraries that we can use pip to install.
Choose one that matches the Python version you have on your computer. For example, if you have Python 3.8, you’ll download the cp38. And if you have Python version 3.9, you’ll download cp39, etc.
2. Build Environment
Open up Command Prompt with admin privileges.
Then navigate to the folder where we store the modified diffusers library, which I saved here: C:\Users\jay\Desktop\PythonInOffice\stable_diffusion_amd\diffusers-dml
Our command prompt should be inside this folder now:
2.1. Create A Python Virtual Environment
When trying something new, we should always use a Python virtual environment. This helps prevent messing up our current Python environments. To create a virtual environment, type the following:
## create the virtual environment python -m venv amd_venv ## activate the virtual environment cd amd_venv/Scripts activate
2.2. Install The Modified Diffusers Library
Then pip install the current directory (i.e. the modified diffusers library) by using the -e . argument. This will treat the current directory as a Python library and install it.
(amd_venv) C:\Users\jay\Desktop\PythonInOffice\stable_diffusion_amd>pip install -e .
2.3. Install The Onnx Nightly Build Wheel File
The easiest way is to use the Command Prompt to navigate to the same folder that stores the wheel file. Then type pip install followed by the file name:
pip install ort_nightly_directml-1.13.0.dev20220908001-cp39-cp39-win_amd64.whl
2.4. Install Other Libraries
We need to install a few more other libraries using pip:
pip install transformers ftfy scipy
3. Connect to Hugging Face (For Downloading Stable Diffusion Model Weights)
When installing the diffusers library, another library called huggingface-hub was also installed. This hugginface-hub provides some utility programs that help facilitate downloading the Stable Diffusion models
Inside Command Prompt, type:
If this is your first time logging in, you’ll need to provide an access token from Hugging Face. Generate the token and copy it, paste it back to the Command Prompt. Then you should see login successful message.
4. Run save_onnx.py
Navigate to the examples\inference folder, there should be a file named save_onnx.py. This Python script will convert the Stable Diffusion model into onnx files. This step will take a few minutes depending on your CPU speed.
This concludes our Environment build for Stable Diffusion on an AMD GPU on Windows operating system.
5. Run Stable Diffusion using AMD GPU on Windows
Inside the same folder examples/inference we’ll find another file named “dml_onnx.py”. This is the script for running Stable Diffusion.
Inside this file, we can modify inputs such as prompt, image size, inference steps, etc.
I used all default settings with this prompt (copied from Reddit):
background dark, block houses, eastern Europe, city highly detailed oil painting, unreal 5 render,
rhads, bruce pennington, studio ghibli, tim hildebrandt, digital art, octane render, beautiful composition,
trending on artstation, award-winning photograph, masterpiece
And this is the image it generated:
This experiment was done using an AMD RX580 GPU with 8GB of VRAM. This GPU is supposed to be on par with Nvidia RTX 1070/1080. However, I noticed that Stable Diffusion runs significantly slower on AMD. I was getting around 7-8 seconds for each iteration, so for the default 50 inference steps, generating one 512×512 image would take roughly 6-7 minutes. A comparable image would take