How to Intermediate · 3 min read

How to use xformers with Stable Diffusion

Quick answer
To use xformers with Stable Diffusion, install the xformers library and enable it in your diffusion pipeline by calling pipe.enable_xformers_memory_efficient_attention(). This reduces VRAM usage and speeds up inference during image generation.

PREREQUISITES

  • Python 3.8+
  • pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117
  • pip install diffusers transformers accelerate xformers
  • NVIDIA GPU with CUDA support recommended

Setup

Install the required packages including xformers and diffusers. Ensure you have a CUDA-enabled GPU for best performance.

bash
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117
pip install diffusers transformers accelerate xformers

Step by step

Load the Stable Diffusion pipeline, enable xformers memory efficient attention, and generate an image.

python
import torch
from diffusers import StableDiffusionPipeline

# Load the pipeline with your preferred model
pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16
).to("cuda")

# Enable xformers memory efficient attention
pipe.enable_xformers_memory_efficient_attention()

# Generate an image
prompt = "A futuristic cityscape at sunset"
image = pipe(prompt).images[0]

# Save the image
image.save("output.png")
print("Image saved as output.png")
output
Image saved as output.png

Common variations

  • Use pipe.enable_attention_slicing() as an alternative for memory optimization if xformers is not compatible.
  • Run inference on CPU by removing .to("cuda") but expect slower performance.
  • Use different Stable Diffusion models by changing the model name in from_pretrained().

Troubleshooting

  • If you get an import error for xformers, ensure it is installed with the correct CUDA version matching your PyTorch installation.
  • If you see increased VRAM usage or crashes, try pipe.enable_attention_slicing() instead.
  • Verify your GPU drivers and CUDA toolkit are up to date for compatibility.

Key Takeaways

  • Install xformers and enable it via pipe.enable_xformers_memory_efficient_attention() for faster Stable Diffusion inference.
  • Use a CUDA-enabled GPU with compatible PyTorch and xformers versions for best performance.
  • If xformers causes issues, fallback to pipe.enable_attention_slicing() to reduce memory usage.
  • Changing the Stable Diffusion model in from_pretrained() is straightforward and supports xformers.
  • Keep GPU drivers and CUDA toolkit updated to avoid compatibility problems with xformers.
Verified 2026-04 · runwayml/stable-diffusion-v1-5
Verify ↗