How to run SDXL Turbo locally on Mac?

adversarial diffusion distillation

Introduction

This article discusses setting up the local environment on M1/M2/M3 Macs to run SDXL Turbo for real-time text-to-image and image-to-image generation.

SDXL Turbo offers incredibly fast processing speeds that allow for creative workflows and applications. By following the steps here, you can get SDXL Turbo running locally in just a few minutes.

With the correct environment set up, you'll be ready to start experimenting with lightning fast image generation using SDXL Turbo right on your own Mac!

Set Up Local SDXL Turbo Environment on Mac

To set up an environment for running SDXL Turbo locally, we will need to go through a few configuration steps:

A. Create Virtual Environment

First, create a virtual environment to install the required libraries isolated from the base system:

python -m venv env

Activate the virtual env:

. ./env/bin/activate

Install/upgrade pip and then use it to install libraries:

pip install --upgrade pip

pip install jupyter notebook

pip install diffusers transformers accelerate --upgrade

B.Launch Notebook

With the libraries in place, launch a notebook environment such as Jupyter to run the code.

You can launch Jupyter from the terminal/command line:

jupyter notebook

Or via an IDE like VSCode.

The notebook will serve as your runtime environment for executing the SDXL Turbo scripts.

Once running, you can begin writing and running code cells to try out text-to-image or image-to-image generation powered by this cutting edge AI system!

Run SDXL Turbo Code

With the environment set up and notebook running, we can now execute SDXL Turbo scripts.

A. Text to Image

Let's start with a text-to-image generation example:

from diffusers import AutoPipelineForText2Image

import torch

pipe = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float32, variant="fp32")

pipe.to("mps")

prompt = "A cinematic shot of a baby racoon wearing an intricate italian priest robe."

image = pipe(prompt=prompt, num_inference_steps=1, guidance_scale=0.0).images[0]

The key aspects are:

  • Importing AutoPipelineForText2Image
  • Setting up pipeline to leverage SDXL Turbo model
  • Passing text prompt
  • Output is image variable

In just 1 second, this generates a 1024x1024 image matching the text description!

B. Image to Image

We can also run image-to-image workflows:

from diffusers import AutoPipelineForImage2Image

from diffusers.utils import load_image

import torch

pipe = AutoPipelineForImage2Image.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float32, variant="fp32")

init_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png").resize((512, 512))

prompt = "cat wizard, gandalf, lord of the rings, detailed, fantasy, cute, adorable, Pixar, Disney, 8k"

image = pipe(prompt, image=init_image, num_inference_steps=2, strength=0.5, guidance_scale=0.0).images[0]

Here we:

  • Import AutoPipelineForImage2Image
  • Load input image
  • Pass modification text prompt
  • Run inference and get output

The model intelligently modifies the input image per the prompt in just 2 seconds!

You now have the basis for building all kinds of creative workflows and applications with SDXL Turbo!

SDXL Turbo Requirements

  • black==23.7.0
  • chardet==5.1.0
  • clip @ git+https://github.com/openai/CLIP.git
  • einops>=0.6.1
  • fairscale>=0.4.13
  • fire>=0.5.0
  • fsspec>=2023.6.0
  • invisible-watermark>=0.2.0
  • kornia==0.6.9
  • matplotlib>=3.7.2
  • natsort>=8.4.0
  • ninja>=1.11.1
  • numpy>=1.24.4
  • omegaconf>=2.3.0
  • open-clip-torch>=2.20.0
  • opencv-python==4.6.0.66
  • pandas>=2.0.3
  • pillow>=9.5.0
  • pudb>=2022.1.3
  • pytorch-lightning==2.0.1
  • pyyaml>=6.0.1
  • scipy>=1.10.1
  • streamlit>=0.73.1
  • tensorboardx==2.6
  • timm>=0.9.2
  • tokenizers==0.12.1
  • torchaudio>=2.0.2
  • torch>=2.0.1
  • torchdata==0.6.1
  • torchmetrics>=1.0.1
  • torchvision>=0.15.2
  • tqdm>=4.65.0
  • triton==2.0.0
  • transformers==4.19.1
  • wandb>=0.15.6
  • webdataset>=0.2.33
  • wheel>=0.41.0
  • xformers>=0.0.20
  • streamlit-keyup==0.2.0

Summary

In this article, we successfully covered how to set up and run SDXL Turbo locally on a Mac for ultra-fast image generation.

All within your existing Mac infrastructure without the need for cloud resources.

Reference link:

https://github.com/Stability-AI/generative-models

https://huggingface.co/stabilityai/sdxl-turbo

https://stability.ai/research/adversarial-diffusion-distillation

Copyright © 2023 SDXL Turbo Online. All rights reserved.