How To Fix Stable Diffusion Runtime Error CUDA Out Of Memory

Stable Diffusion is a Sophisticated AI tool for creating images via text.

Anyone can generate their favorite art or illustration by giving a detailed description of the required image.

It is not a lite tool; you need a sufficient system requirement to run the Stable Diffusion without any error.

If there is an issue with the system requirement, you will get the following error message that appears suddenly from nowhere “RuntimeError: CUDA out of Memory“.

The error occurs due to a memory problem, preventing the Stable Diffusion from functioning correctly.

This article will show simple steps to fix Stable Diffusion Runtime Errors.

What Causes The Stable Diffusion Runtime Error?

The Stable Diffusion runtime error occurs because the allocated Memory for Stable Diffusion may be exhausted, and you need to take necessary action to fix the error.

Please note that Stable Diffusion requires at least four gigabytes (GB) of visual random access memory (VRAM) to function correctly.

Also, one recommendation is a 3xxx series NVIDIA GPU with 6 GB of VRAM. 

Each command should have a certain quantity of Memory to upload the data. If the uploaded data exceeds the allocated Memory, the Stable Diffusion throws the run time error.

The possible reasons are

  • The uploaded project exceeds the memory limit
  • You might have processed multiple quantities of files at a time
  • Your Memory is full and cannot load further request

Stable Diffusion Runtime Error CUDA Out Of Memory

Stable Diffusion acts like a server; if you run it locally on your PC, it is common to get such errors often.

First, go through the mistake and follow any fixes specified in the error message.

So first, follow the directions in the error message and see if the error is fixed.

If the direction leads to another error message, follow the troubleshooting below.

1. Restart Your Pc

If you are getting the Runtime error for the first time. Restarting your system will help you to complete your project without error.

It is a simple but effective method.

If you get the runtime error very often, then you must follow the steps below.

2. Allocate Memory In Webui-user.bat File

You have your torch installed preferably inside the same environment.

  • Find and locate Webui-User. bat file. 
  • Open and Add the following command in the Webui-user.bat file to allocate the Memory for Stable Diffusion.

Set PYTORCH_CUDA_ALLOC_CONF=garbage_collection_threshold:0.6,max_split_size_mb:128

  • Relaunch Stable Diffusion, and you can run the project without error.

3. Close Other Application In Your PC

Closing unwanted applications will free up memory space and help to run the Tool effectively.

So close to any other application opened other than the Stable Diffusion.

Close the browser if it is active since it consumes more Memory.

4. Running Multiple Projects At A Time

You need to process the image individually instead of giving the bulk request.

Because some image files require a small amount of Memory, on the other hand, some requests need high memory space.

Running multiple projects simultaneously might conflict with the Memory and throw the runtime error.

5. Reduce The Resolution Of Output Image File

If the error persists, then reduce the resolution setting.

It is an effective way to use the Memory to its maximum potential.

The default image size is 512×512, which may be why you have this issue.

 Restrict the resolution of your image to 256 x 256 by setting the size -W 256 -H 256 in the command line.

6. Reduce The Batch Size Of Your Project

If your project is more significant, divide the data into smaller batches.

Processing data in smaller batches will avoid memory overload and help you to complete your project without running out of memory error.

7. Lower The Precision 

You can lower the Precision by using the alternative framework.

If you are using Pytorch, then try using Float16. 

You can also switch to a alternative more memory-efficient framework, if you aware any.

8. Delete Cache Memory In Your System

Deleting cache memory will help you to free up the memory space.

  • Open the Run command box by pressing Windows+R.
  • Type %LocalappData% and click OK.
  • Find and locate the Environmental variable folder and delete it.
  • Type %Temp% In the run command box and Delete all the files.
  • Empty recycle bin and restart your PC. 

Now you can see the error is fixed and run the file without issues.

9. Remove Safety Checks In Filter Setting

Try to Replace scripts/txt2img.py with this:

https://github.com/JustinGuese/stable-diffusor-docker-text2image/blob/master/txt2img.py

It will free up memory space up to 2GB RAM.

10. Call The Optimized Python File

You can use the following command while running the text to an image file.

python optimizedSD/optimized_txt2img.py –prompt “{Text Command}” –n_iter 5 –n_samples 1 –H 512 –W 512 –precision full

Also, installing the “basujindal optimizedSD” in Stable Diffusion will fix the error.

You can download the file here.

If nothing above works, you must increase GPU and RAM Memory for Stable Diffusion or you need to replace the GPU or RAM memory in your PC.