Skip to content

SDXL-turbo (Image Generation)

StableDiffusionXL-turbo (SDXL-turbo) is one of the fastest and highest quality diffusion models available from StabilityAI. Unlike other existing stable diffusions, SDXL-turbo can generate unprecedented quality of images in a single step. In this tutorial, we will introduce how to compile sdxl-turbo and generate images using optimum-rbln.

The tutorial consists of two main steps:

  1. How to compile the PyTorch SDXL-turbo model
  2. How to generate an image with the compiled model

Prerequisites

Before we start, please make sure you have installed the following Python packages in your system:

Note

If you want to skip the details and quickly compile and deploy the models on RBLN NPU, directly jump to the summary section. It provides the complete code with all the necessary steps to compile and deploy the model as a quick starting point for your own project.

Step 1. How to compile

Prepare and compile the model

In this step, we compile SDXL-turbo using the RBLNStableDiffusionXLPipeline class, which comes from optimum-rbln.

When you employ RBLNStableDiffusionXLPipeline, you must specify the following argument:

  • export: To compile the model, you must set export argument to True. The model will be downloaded from HuggingFace Hub, and automatically compiled by RBLN SDK. When export argument is False, you can load the precompiled model and directly run inference.
1
2
3
4
5
6
7
8
from optimum.rbln import RBLNStableDiffusionXLPipeline

model_id = "stabilityai/sdxl-turbo"
pipe = RBLNStableDiffusionXLPipeline.from_pretrained(
    model_id=model_id,
    rbln_guidance_scale=0.0,
    export=True,
)

Save the compiled model

To save the compiled model to the disk, you can use the pipe.save_pretrained() method as below:

pipe.save_pretrained("rbln-sdxl-turbo")

Step 2. How to generate an image

Load the compiled model

First, we need to load the compiled model to generate an image. Set export argument to False in the RBLNStableDiffusionXLPipeline class to load the precompiled model. As SDXL-turbo does not use guidance_scale, we disable them with rbln_guidance_scale=0.0:

1
2
3
4
5
6
from optimum.rbln import RBLNStableDiffusionXLPipeline

pipe = RBLNStableDiffusionXLPipeline.from_pretrained(
    model_id="rbln-sdxl-turbo",
    export=False,
)

Image generation

Now we are ready to generate an image. We can exploit original arguments such as num_inference_steps and guidance_scale from the HuggingFace StableDiffusionXLPipeline class. Sometimes guidance_scale affects the batch size of Unet, which is one of the components of the stable diffusion model. When performing inference, it is recommended to set the guidance_scale value to be the same as the rbln_guidance_scale value set during the compilation stage:

1
2
3
prompt = "A cinematic shot of a baby racoon wearing an intricate italian priest robe."
image = pipe(prompt, num_inference_steps=1, guidance_scale=0.0).images[0]
image.save("generated_image.png")

If the tutorial proceeded without any issues, an image similar to the one below should have been generated.

Summary

Below is the complete code for the SDXL-turbo Text-To-Image generation:

from optimum.rbln import RBLNStableDiffusionXLPipeline

model_id = "stabilityai/sdxl-turbo"
pipe = RBLNStableDiffusionXLPipeline.from_pretrained(
    model_id=model_id,
    rbln_guidance_scale=0.0,
    export=True,
)

# insert apropreate prompt which you want to generate
prompt = "A cinematic shot of a baby racoon wearing an intricate italian priest robe."
image = pipe(prompt, num_inference_steps=1, guidance_scale=0.0).images[0]
image.save("generated_image.png")