Stable Diffusion is one of the AI tools people have been using to generate AI art as it’s free to use and publicly available for everyone. The tool can be run online through a HuggingFace Demo or locally on a computer with a dedicated GPU. If you’ve been trying to use Stable Diffusion on your computer but are running into the “Cuda Out of Memory” error, the following post should help you fix it and get it up and running.
7 tips to fix “Cuda Out of Memory” on Stable Diffusion
The following list of fixes should help you resolve the “Cuda Out of Memory” error on Stable Diffusion.
1. Restart your system
In case you had no problem running Stable Diffusion before, it’s possible that a simple restart of your system can do the job for you as the Stable Diffusion software may have lost access to parts of your GPU. Some users (1,2) were able to quickly fix the “Cuda Out of Memory” error on their computer after a system restart.
2. Install Anaconda alongside Nvidia CUDA Toolkit
Another workaround that’s been suggested by users (1,2) to run Stable Diffusion without problems is installing and running the Anaconda prompt. For those of you unfamiliar, Anaconda is an open-source environment management system that can install and run packages for Python programs. You can install Anaconda (video tutorial), download NVIDIA’s CUDA Toolkit, and then follow the instructions from the Python GitHub repo of your choice to start using Stable Diffusion without issues.
3. Use an optimized version of Stable Diffusion
In case you’re still running into the “Cuda Out of Memory” issue, you can try using an optimized version of Stable Diffusion that you access here. If you have the original version of Stable Diffusion installed on your system, you can download the optimized version and paste its contents onto the stable-diffusion-main folder to resolve the error.
You can refer to this Reddit post for detailed instructions on how to do it.
4. Try generating images with lower resolution
The “Cuda Out of Memory” error can also pop up when you’re trying to generate images at a higher resolution. That’s because higher-quality images require a substantially larger GPU memory. Users (1,2) were able to resolve this error by lowering the image resolution which you can do by changing the height and width values inside Stable Diffusion. You can try using 512 x 512 or 256 x 256 as required resolutions or go for something lower if your GPU memory is less than 4 GB.
5. Reduce sample size to one
Like any image generator out there, Stable Diffusion also creates multiple images at the same time by default. However, multiple images can cause your GPU to run out of memory which may result in the “Cuda Out of Memory” error. To resolve this, you can reduce the sample size to 1 by entering “–n_samples 1” in your input prompt. This solution appears to have worked for many users according to this post on Reddit.
6. Check your GPU memory
To run Stable Diffusion without problems, it’s recommended that you use a GPU that has a RAM of at least 6 GB but you can also make things do using GPUs with 4 GB of RAM (refer: 1,2,3). Anything less than that will lock the Stable Diffusion program out of memory from your GPU which may mean running it directly on your CPU; that may take your generation time to at least 2 minutes per image.
If you wish to avoid getting the “Cuda Out of Memory” error, your best bet is to upgrade your graphics card to something that has a memory of at least 6 GB.
7. Edit the webui-user.bat file with optimized commands
The webui-user.bat is what Stable Diffusion uses to run commands to generate images on your computer. You can update this file with optimized commands to see if that resolves the “Cuda Out of Memory” error on your system. To get started, locate the webui-user.bat file inside the Stable Diffusion folder, right-click on it, and go to Edit > Notepad. You can then try any of the command line optimizations listed on this GitHub page to check which one works for you. You can refer to these Reddit posts (1,2,3) for detailed instructions.
That’s all you need to know to fix the “Cuda Out of Memory” error on Stable Diffusion.
Discussion