3x Faster Stable Diffusion Models

Open In Colab

This tutorial demonstrates how to use the pruna package to optimize any custom stable diffusion model. We will use the stable-diffusion-v1-4 model as an example.

1. Loading the Stable Diffusion Model

First, load your stable diffusion model.

[ ]:
import torch
from diffusers import StableDiffusionPipeline

# Define the model ID
model_id = "CompVis/stable-diffusion-v1-4"

# Load the pre-trained model
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to("cuda")

2. Initializing the Smash Config

Next, initialize the smash_config.

[ ]:
from pruna import SmashConfig

# Initialize the SmashConfig
smash_config = SmashConfig()
smash_config['compilers'] = ['diffusers2']

3. Smashing the Model

Now, smash the model. Don’t forget to replace the token by the one provided by PrunaAI.

[ ]:
from pruna import smash

# Smash the model
smashed_model = smash(
    model=pipe,
    token='<your_token>',  # replace <your_token> with your actual token
    smash_config=smash_config,
)

4. Running the Model

Finally, run the model to generate the image.

[ ]:
# Define the prompt
prompt = "a photo of an astronaut riding a horse on mars"

# Display the result
smashed_model(prompt).images[0]

Wrap Up

Congratulations! You have successfully smashed a stable diffusion model. You can now use the pruna package to optimize any custom stable diffusion model. The only parts that you should modify are step 1 and step 4 to fit your use case.