Running local txt2img with stable diffusion

Original forum link
https://forum.qubes-os.org/t/23318
Original poster
renehoj
Created at
2023-12-30 17:00:21
Last wiki edit
2023-12-30 17:40:11
Revisions
1 revision
Posts count
4
Likes count
12
Tags
generative-ai

This guide explains how to install stable-diffusion-webui (AUTOMATIC1111) on Qubes OS 4.2.0. It’s meant to be used with an nvidia GPU, but it can run on the CPU. The GPU I use is the nvidia 4060, I’m not sure if it will work with cards using the legacy driver, I’m also not sure how to make it work with other GPU types.

I have tested SD with and without using a GPU on 3 systems running Qubes OS, and it does work only using the CPU. So if you just want to try SD before you go out and buy a $1000 GPU, you can run it on the CPU, but it is very slow compared to running it on a GPU.

Image generation time (20 steps, 512x512): i7-8650U (Old laptop CPU): ~6 min i9-13900K (Desktop CPU): ~1m 45s nvidia 4060 (GPU) with i9-13900K : >5s

If you don’t have a GPU, you can skip to installing stable-diffusion-webui

GPU passthrough

Follow this guide, it explains how to do passthrough: https://neowutran.ovh/qubes/articles/gaming_windows_hvm.html I’ll only give a summary of how you configure GPU passthrough, there are already multiple guides going into detail about passthrough.

You are also only going to need CUDA support, which makes passthrough slightly easier.

  1. Find your device ID with lspci.
  2. Hide the device ID from dom0, by adding rd.qubes.hide_pci=ID to grub. Generate grub and reboot, grub2-mkconfig -o /boot/grub2/grub.cfg
  3. Check if the device is hidden, sudo lspci -vvn, kernel driver should be pciback.
  4. Use the patch_stubdom.sh script to patch qemu-stubdom-linux-rootfs

If you are having issues with passthrough, search the forum.

Installing the CUDA driver

wget https://developer.download.nvidia.com/compute/cuda/repos/debian12/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo add-apt-repository contrib
sudo apt-get update
sudo apt-get -y install nvidia-kernel-open-dkms
sudo apt-get -y install cuda-drivers

Installing stable-diffusion-webui

Make a qube with 16 GB memory, and 25 GB disk space. If you are using a GPU it needs to be standalone with the kernel supplied by qube, if you used the patch script the name needs to start with gpu_, you also need to install the CUDA driver and pass the GPU.

If you are not using a standalone qube, python3.11-venv need to be installed in the template

sudo apt install python3.11-venv
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
cd stable-diffusion-webui
python3 -m venv venv/
./webui.sh
If you don’t have a GPU, you need to edit webui-user.sh and add --use-cpu all --no-half --no-half-vae --skip-torch-cuda-test to the COMMANDLINE_ARGS

Let the installation complete and there should be a web server running on localhost:7860, it’s installed with the SD v1.5 image model, and should be ready for use.

Stable Diffusion running in Qubes OS (ThinkPad T480) t480-txt2img|690x388