Introduction

SDXL Turbo achieves state-of-the-art performance, allowing for the generation of high-quality images in a single step. It has reduced the necessary step count from 50 to a mere one. SDXL Turbo is based on a distillation technique known as Adversarial Diffusion Distillation (ADD), SDXL Turbo empowers the model to produce image outputs seamlessly in a single step. Additionally, it enables the generation of real-time text-to-image outputs with sustained high sampling fidelity.

Our Observations

We have deployed this model using A100 GPU and observed that the model took an average cold start time of 8.03sec and an average inference time of 90ms for single-step image generation.

Defining Dependencies

We are using the HuggingFace Diffusers library for the deployment.

Constructing the GitHub/GitLab Template

Now quickly construct the GitHub/GitLab template, this process is mandatory and make sure you don’t add any file named model.py

stable-video-diffusion/
├── app.py
└── config.yaml

You can also add other files to this directory.

Create the class for inference

In the app.py we will define the class and import all the required functions

  1. def initialize: In this function, you will initialize your model and define any variable that you want to use during inference.
class InferlessPythonModel:
  def initialize(self):
    vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
    self.pipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo",vae=vae, torch_dtype=torch.float16, variant="fp16",use_safetensors=True)
    self.pipeline = self.pipeline.to("cuda")
    self.pipeline.unet = torch.compile(self.pipeline.unet, mode="reduce-overhead", fullgraph=True)
  1. def infer: This function gets called for every request that you send. Here you can define all the steps that are required for the inference. You can also pass custom values for inference through the inputs parameter.
  def infer(self, inputs):
    prompt = inputs["prompt"]
    pipeline_output_image = self.pipeline(prompt=prompt, num_inference_steps=1, guidance_scale=0.0).images[0]
    buff = BytesIO()
    pipeline_output_image.save(buff, format="PNG")
    img_str = base64.b64encode(buff.getvalue())
    return {"generated_image_base64": img_str.decode('utf-8')}
  1. def finalize: This function cleans up all the allocated memory.
    def finalize(self,*args):
        self.pipeline = None

Creating the Custom Runtime

This is a mandatory step where we allow the users to upload their own custom runtime through config.yaml.

build:
  cuda_version: "12.1.1"
  system_packages:
    - "libssl-dev"
  python_packages:
    - "diffusers==0.24.0"
    - "pandas==2.1.4"
    - "torch==2.1.0"
    - "transformers==4.36.1"
    - "accelerate==0.25.0"

Deploying the model on Inferless

Inferless supports multiple ways of importing your model. For this tutorial, we will use GitHub.

Import the Model through GitHub

Click on theRepo(Custom code) and then click on the Add provider to connect to your GitHub account. Once your account integration is completed, click on your Github account and continue.

Provide the Model details

Enter the name of the model and pass the GitHub repository URL.

Now, you can define the model’s input and output parameters in JSON format. For more information, go through this page.

  • INPUT: Refer to the function def infer(self, inputs), here we mentioned inputs["prompt"], which means inputs the parameter will have a key prompt.

  • OUTPUT: The same goes here, the function mentioned above will return the results as a key-value pair return {"generated_image_base64": img_str.decode('utf-8')}.

Input JSON must include prompt a key to pass the instruction. For output JSON, it must be included generated_image_base64 to retrieve the output.

GPU Selection

On the Inferless platform, we support all the GPUs. For this tutorial, we recommend using A100 GPU. Select A100 from the drop-down menu in the GPU Type.

Using the Custom Runtime

In the Advance configuration, we have the option to select the custom runtime. First, click on the Add runtime to upload the config.yaml file, give any name and save it. Choose the runtime from the drop-down menu and then click on continue.

Review and Deploy

In this final stage, carefully review all modifications. Once you’ve examined all changes, proceed to deploy the model by clicking the Import button.

Voilà, your model is now deployed!