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:
- How to compile the PyTorch
SDXL-turbomodel - 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 setexportargument toTrue. The model will be downloaded from HuggingFace Hub, and automatically compiled by RBLN SDK. Whenexportargument isFalse, you can load the precompiled model and directly run inference.
Save the compiled model¶
To save the compiled model to the disk, you can use the pipe.save_pretrained() method as below:
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:
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:
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:
