Stable Diffusion on Google Cloud

· 395 words · 2 minute read

These instructions are for setting up a Stable Diffusion instance on Google Cloud.

I created an n1-standard-4 instance with an nVidia Tesla T4 GPU, which has 16GB of VRAM. I chose the “c0-deeplearning-common-cu110-v20220806-debian-10” image and sized it to 80GB. If I had it to do over again, I’d make a separate disk for the models and the generated output, but it’s not a big deal.

Here are the (cleaned up) commands I used.

# after GCP has completed installing nVidia drivers
sudo reboot 

# install a Python virtual-environment tool
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
conda update -n base -c defaults conda

# get the Stable Diffusion model and put it in a central
# location, because you'll probably try out different forks
# of the SD code and don't want to accidentally delete the
# model
curl https://www.googleapis.com/storage/v1/b/aai-blog-files/o/sd-v1-4.ckpt?alt=media > ~/sd-v1-4.ckpt

# get a copy of the original project
git clone https://github.com/CompVis/stable-diffusion

# let miniconda set up all the dependencies
cd stable-diffusion
conda env create -f environment.yaml
conda activate ldm

# make a symlink to the SD model within the project
mkdir -p models/ldm/stable-diffusion-v1/
ln -sf ~/sd-v1-4.ckpt models/ldm/stable-diffusion-v1/model.ckpt

Then do this each time you ssh back into the machine:

cd stable-diffusion
conda activate ldm
python scripts/txt2img.py --prompt "a photograph of an astronaut riding a horse" --plms

I’m having good luck with these commands:

$ python scripts/txt2img.py \
  --prompt "abstract painting of sunset" \
  --seed 42 --n_sample 1 --n_iter 8 \
  --ddim_steps 100 --plms --H 512 --W 832

$ python scripts/img2img.py \
  --prompt "woman standing next to jet airplane by pablo picasso" \
  --init-img imput-image.jpg --skip_grid --scale 12 \
  --seed 42 --n_sample 1 --n_iter 8 --ddim_steps 100 \
  --strength 0.8

Since GCP assigns ephemeral IP addresses by default to new instances, and you pay for static ones that you’re not using, I have a simple line in my Bash history that helps connect after the instance starts up and sets up an sshfs share to easily get at the output:

SDIP=x.x.x.x; sshfs $SDIP: ~/instance-sd ; ssh $SDIP

The other fork you should know about is maintained by basujindal. It has more features than the original code, and runs on lower-spec hardware. I haven’t found it to run any faster on GCP.

Artists who use color well to model for interesting images (“an apple in the style of yuri petrenko”)

Hints on prompt grammar and lexicon

A prompt generator