Recently I’ve been playing around with Stable Diffusion to catch up with the recent AI trends. Naturally, I have been using AUTOMATIC1111’s Stable Diffusion Webui which provides an easy and quick starting point to start working with Stable Diffusion.

While playing with it, I also read and researched different techniques on getting better results and faster results. While looking into speeding things up, I read that updating torch and xformers provided significant boosts to generation times, especially on low to mid-tier Graphics Cards. While searching the topic, I encountered many conflicting information, many of which did not work. I will outline the method I used below.

Requirements

  • Modern Nvidia Graphics card (eg. Geforce RTX 3080)
  • Windows 10/11. I would think it will work on older Windows, but that is not confirmed.

Step 0: Install AUTOMATIC1111’s Stable Diffusion WebUI

Most people reading this should be able to skip this step:

First, make sure to install the AUTOMATIC1111’s Stable Diffusion WebUI, and make sure that you are able to use it. You will also want to enable --xformers if it is not enabled already. There are many guides for that so I will skip the instructions here.

Step 1: Install the latest CUDA Toolkit and Graphics Driver

If you are on a Nvidia card, Torch will need CUDA to work. You can install the latest CUDA Toolkit Here.

You will also want to update your graphics driver to the latest. CUDA has Graphics driver compatibility, and if you install the latest CUDA but are on an older graphics driver, it may not work. If you are particular about what driver you run, you can see the compatibility chart in the release notes. At the time of writing, the latest CUDA Toolkit is 12.4. You can see the compatibility chart here.

Once you verify that CUDA is installed, we can move on to the python portion.

Step 2: Entering venv

the Stable Diffusion WebUI works under Python’s venv. Venv is a virtual environment that can be created to install dependencies and requirements on your computer without affecting your normal environment. We will need to start venv so that we can make changes to the WebUI’s dependencies. To enter venv:

  1. Visit the folder where the WebUI is installed with Explorer. (eg. C:\stable-diffusion-ai ).
  2. Run cmd or right-click and Open in Terminal (if you have Windows Terminal installed)
  3. Type or copy: venv\Scripts\Activate and hit enter. Now we should be in venv.

NOTE: If you do not have a venv folder in your install, it is likely that you have never ran the webui. Please go back to Step 0.

Now we are in venv. This is optional, but I recommend we update pip before we move on to upgrading torch and xformers. We can update pip with the following command:

python -m pip install --upgrade pip

Step 3: Upgrading torch and xformers

Finally, we can upgrade everything. The following will install all of the dependencies we want to update in one call:

pip install --upgrade torch torchvision xformers --index-url "https://download.pytorch.org/whl/cu121"

Do not forget the last part: --index-url "https://download.pytorch.org/whl/cu121"

If you do not enter that part, torch will be installed using the default repository and not the one specific for Windows. Leaving that part out, torch will be updated, however you may end up with the following common error when running the webui: Torch is not able to use GPU; add --skip-torch-cuda-test to COMMANDLINE_ARGS variable to disable this check

At the time of writing, the latest explicitly supported CUDA version is 12.1, thus the 121 at the end of the url. If you would like to check to see if there is a newer URL, you can use the Install PyTorch section on pytorch.org

Wait for everything to download, and that should be it! Latest torch and xformers should be installed.

Step 4: Leaving venv and verifying torch and xformers updates

This is optional, but you may want to make sure you are out of venv. To exit:

  1. Visit the folder where the WebUI is installed with Explorer.
  2. Run cmd or right-click and Open in Terminal
  3. Type or copy: venv\Scripts\Deactivate and hit enter. Now we should be out of venv.

To verify that everything is updated, run the webui normally and visit the webui. At the bottom of the page, you should see all of the dependencies. If this is updated, we are good to go!