Local Troubleshooting

**Guides for Known Solutions to Bugs**

## Speeding Up Stable Diffusion

Stable diffusion is a complex program that despite its internal mathematical simplicity still requests a very high VRAM and RAM usage. Due to this, most computers will struggle when generating images. In order to speed up your generations, you will be able to find below some general tips and tricks for usage:

### Speed up your generations through Samplers:

Samplers are the filtering mechanism that transforms noise into your desired image. They operate by solving differential spatial equations which is not important. What is important for your generating times is understanding there are two kinds of samplers: Ancestral samplers and DPM++ samplers. If what matters to you is the replication of images, you are stuck with DPM++ samplers. But if this is not the case ancestral samplers will provide high-quality generations in less than 20 Sampling steps. Automatic11111 has an extremely good sampler in Euler A. This should be your first step to improving your generation speeds.

### Speed up your generation through image size settings:

Stable Diffusion was trained to recreate squared images. When you change your generation speeds to something that is not squared processing times will increase greatly. If you are suffering from speed issues, consider creating only squared images. If what your computer is lacking is a powerful VRAM an alternative is using the HiRes.Fix generation to create images at your desired size. If you would like to create a 512x512 pixel image. Try generating a 128x128 pixel image and increasing it by 4 using HiRes.Fix. The ERSGAN upscaler partitions your image in tiles and processes them in smaller batches, computers with bad graphic cards can still generate fine smaller generations and then upscale them to increase speed slightly.

### Speed up your generation through Cross Attention Optimization:

**The simple Guide to optimization:**

**Step 1:** Follow the Automatic Updates guide at the end of the page.

Step 2: In AUTOMATIC1111 Web-UI, navigate to the Settings page. Select Optimization on the left panel. In the Cross attention optimization dropdown menu, select an optimization option. But, which one to select?

Knowing which to select is as simple as following the algorithm below:

Are you a Mac user? Use InvokeAI

Are you a Windows/Linux user that cares about replicability? Use xformers.

Are you a Windows/Linux user that doesn't care about replicability? Use xformers. But read the Token Merging and Negative guidance minimum guide below.

**The complicated guide to optimization:**

**Step 1:** Right click webui-user and click on edit.

**Step 2:** Replace your set COMMANDILE-ARGS line with the following:

* **If you a Linux/Mac:** *set COMMANDLINE\_ARGS= --xformers --force-enable-xformers*
* **If you have a Mac :** *You can't use this method.*

### Speed up your generation through Token Merging and Negative guidance minimum:

If you are willing to sacrifice both image precision and replicability. This is the place.

Token merging will unite tokens in your generations to increase image speed. It will begin with the tokens that are the closest in spatial definitions. But the more you are willing to sacrifice the more amount of desired detail you will compromise. This method will only show results if you use Embeddings such as EasyNegative or Badhands or a plethora of prompts.

To set it up in AUTOMATIC1111 Web-UI, navigate to the Settings page. Select a token merging ratio in the second slider. 0.2 means that 20% of the tokens will be merged. Click Apply Settings. The effectiveness of the method depends heavily on the numbers of prompts and embeddings you use.

The Negative guidance minimum option turns off the negative prompt after the initial sampling steps. Emprical testing suggests that negative conditioning can be turned off in some steps without affecting the image.

To set it up in AUTOMATIC1111 Web-UI, navigate to the Settings page. Select a Negative guidance minimum sigma in the second slider. Click Apply Settings. The effectiveness of the method depends heavily on the numbers of steps you use.

### I am desperate for speed, what can I do?

If you are willing to reboot your machine every 50-150 generations. This fix is for you. This optimization will cause no performance impact (Same image precision, quality, and replicability) and will increase the initial memory footprint. The downside is that it will reduce memory fragmentation in the long run. The first sign of memory fragmentation will be the loss of colour gradients.

**Step 1:** Right click webui-user and click on edit.

**Step 2:** Add the line below the set PYTHON= line:

* `set PYTORCH_CUDA_ALLOC_CONF=garbage_collection_threshold:0.9,max_split_size_mb:512`

## Troubleshooting

### The CUDA bug

There is one very pervasive error that you will likely suffer from if your computers has an Nvidia graphics card. Due to an interaction between pytorch and Nvidia´s CUDA Stable diffusion will wrongly assign extra RAM to Stable Diffusion. This bug causes a memory leak that builds up over time the more generations you make. Correct RAM management will help delay this error, but it will ultimately show up unless you solve it. The first signs of your machine being affected affected will be Stable Diffusion taking extra amounts of RAM compared to the past while in static and while generating. This will later build up into lower rendering times, and finally will crash your generations by showing you a message like the one below:

If you see an error message like the one above, fear not. It may be for a different cause. It could also be because you are generating a particularly large image. Only follow this tutorial if this message is appearing when rendering pictures of similar sizes you have done before. If this message appears, congratulations you are now officially addicted to generating waifu renders. This bug is almost a rite of passage. And there is a solution:

**Step 1:**

**If you are a linux user:** Install tcmalloc in your python by opening the python prompt, pasting the line below and clicking enter.

```
sudo apt-get install google-perftools

```

**If you are a Windows or mac user:** Go to your main Stable Diffusion folder and edit the file webui-user. Remove the following commands from the COMMANDLINE\_ARGS line and never use them again:

* --no-half
* --no-half-vae and
* --medvram

**Step 2:**

Edit webui-user and replace your commands with the following:

```
set COMMANDLINE_ARGS= --reinstall-torch --reinstall-xformers

```

Now open stable diffusion as usual and let it run the commands. Once finished close stable diffusion. Edit webui-user again and write the commands below:

```
set COMMANDLINE_ARGS= --xformers --force-enable-xformers --opt-sdp-attention

```

**Step 3:**

Install anaconda, is open source. It is available in the following link: https://repo.anaconda.com/archive/Anaconda3-2023.07-2-Windows-x86\_64.exe

Once installed, copy the line below in the Anaconda prompt.

```
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

```

Click enter and let it run until completion. Once you see a single line of text with only the word "done" in it, is finished. Congratulations, you have fixed your CUDA bug. Run stable diffusion as usual and keep rendering waifus.