Stable Diffusion on WSL on Windows

First install WSL2 as suggested by the many different websites out there. I installed it like this:

wsl --install -d Ubuntu-22.04

Open the installed Ubuntu and install the necessary packages for stable diffusion itself:

sudo apt update && sudo apt -y upgrade && sudo apt -y install git-lfs python3-pip
pip install torch --extra-index-url
pip install diffusers transformers==4.26 scipy ftfy accelerate

Add CUDA-Support for the WSL (from this site):

sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda

Write yourself a small python file (e.g. as which will enable the image generation and save the images to a file with the prompt name:

import torch
from torch import autocast
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")"cuda")

prompt = "a dark environment, two warriors standing on a chess field with swords drawn"
steps = 50
width = 512
height = 512

with autocast("cuda"):
  for i in range(4):
    output = pipe(prompt, width=width, height=height, num_inference_steps=steps)
    image = output["images"][0]
    file = prompt.replace(" ", "_").replace(",", "")"{file}-{i}.png")