Deploy Stable Diffusion XL on AWS inferentia2 with Amazon SageMaker
In this end-to-end tutorial, you will learn how to deploy and speed up Stable Diffusion XL inference using AWS Inferentia2 and optimum-neuron on Amazon SageMaker. Optimum Neuron is the interface between the Hugging Face Transformers & Diffusers library and AWS Accelerators including AWS Trainium and AWS Inferentia2.
You will learn how to:
- Convert Stable Diffusion XL to AWS Neuron (Inferentia2) with
optimum-neuron
- Create a custom
inference.py
script for Stable Diffusion - Upload the neuron model and inference script to Amazon S3
- Deploy a Real-time Inference Endpoint on Amazon SageMaker
- Generate images using the deployed model
Quick intro: AWS Inferentia 2
AWS inferentia (Inf2) are purpose-built EC2 for deep learning (DL) inference workloads. Inferentia 2 is the successor of AWS Inferentia, which promises to deliver up to 4x higher throughput and up to 10x lower latency.
instance size | accelerators | Neuron Cores | accelerator memory | vCPU | CPU Memory | on-demand price ($/h) |
---|---|---|---|---|---|---|
inf2.xlarge | 1 | 2 | 32 | 4 | 16 | 0.76 |
inf2.8xlarge | 1 | 2 | 32 | 32 | 128 | 1.97 |
inf2.24xlarge | 6 | 12 | 192 | 96 | 384 | 6.49 |
inf2.48xlarge | 12 | 24 | 384 | 192 | 768 | 12.98 |
Additionally, inferentia 2 will support the writing of custom operators in c++ and new datatypes, including FP8
(cFP8).
Let's get started! š
If you are going to use Sagemaker in a local environment (not SageMaker Studio or Notebook Instances). You need access to an IAM Role with the required permissions for Sagemaker. You can findĀ hereĀ more about it.
optimum-neuron
1. Convert Stable Diffusion to AWS Neuron (Inferentia2) with We are going to use the optimum-neuron to compile/convert our model to neuronx. Optimum Neuron provides a set of tools enabling easy model loading, training and inference on single- and multi-Accelerator settings for different downstream tasks.
As a first step, we need to install the optimum-neuron
and other required packages.
Tip: If you are using Amazon SageMaker Notebook Instances or Studio you can go with the conda_python3
conda kernel.
After we have installed the optimum-neuron
we can convert load and convert our model.
We are going to use the stabilityai/stable-diffusion-xl-base-1.0 model. Stable Diffusion XL (SDXL) from Stability AI is the newset text-to-image generation model, which can create photorealistic images with detailed imagery and composition compared to previous SD models, including SD 2.1.
At the time of writing, the AWS Inferentia2 does not support dynamic shapes for inference, which means that the we need to specify our image size in advanced for compiling and inference.
In simpler terms, this means we need to define the input shapes for our prompt (sequence length), batch size, height and width of the image.
We precompiled the model with the following parameters and pushed it to the Hugging Face Hub:
height
: 1024width
: 1024num_images_per_prompt
: 1batch_size
: 1neuron
: 2.15.0
Note: If you want to compile your own model or a different Stable Diffusion XL checkpoint you need to use ~120GB of memory and the compilation can take ~45 minutes. We used an inf2.8xlarge
ec2 instance with the Hugging Face Neuron Deep Learning AMI to compile the model.
inference.py
script for Stable Diffusion
2. Create a custom The Hugging Face Inference Toolkit supports zero-code deployments on top of theĀ pipelineĀ featureĀ from š¤ Transformers. This allows users to deploy Hugging Face transformers without an inference script [Example].
Currently is this feature not supported with AWS Inferentia2, which means we need to provide an inference.py
for running inference. But optimum-neuron
has integrated support for the š¤ Diffusers pipeline feature. That way we can use the optimum-neuron
to create a pipeline for our model.
If you want to know more about the inference.py
Ā script check out this example. It explains amongst other things what the model_fn
and predict_fn
are.
We are using the NEURON_RT_NUM_CORES=2
to make sure that each HTTP worker uses 2 Neuron core to maximize throughput.
3. Upload the neuron model and inference script to Amazon S3
Before we can deploy our neuron model to Amazon SageMaker we need to upload it all our model artifacts to Amazon S3.
Note: Currently inf2
instances are only available in the us-east-2
& us-east-1
region [REF]. Therefore we need to force the region to us-east-2.
Lets create our SageMaker session and upload our model to Amazon S3.
We create our model.tar.gz
with our `inference.py`` script
Next, we upload our model.tar.gz
to Amazon S3 using our session bucket and sagemaker
sdk.
4. Deploy a Real-time Inference Endpoint on Amazon SageMaker
After we have uploaded ourĀ model artifactsĀ to Amazon S3 can we create a customĀ HuggingfaceModel
. This class will be used to create and deploy our real-time inference endpoint on Amazon SageMaker.
The inf2.xlarge
instance type is the smallest instance type with AWS Inferentia2 support. It comes with 1 Inferentia2 chip with 2 Neuron Cores. This means we can use 2 Neuron Cores to minimize latency for our image generation.
5. Generate images using the deployed model
The .deploy()
returns an HuggingFacePredictor
object which can be used to request inference. Our endpoint expects a json
with at least inputs
key. The inputs
key is the input prompt for the model, which will be used to generate the image. Additionally, we can provide inference parameters, e.g. num_inference_steps
.
The predictor.predict()
function returns a json
with the generated_images
key. The generated_images
key contains the 1
generated image as a base64
encoded string. To decode our response we added a small helper function decode_base64_to_image
which takes the base64
encoded string and returns a PIL.Image
object and display_image
displays them.
Now, lets generate some images. As example A dog trying catch a flying pizza in style of comic book, at a street corner.
. Generating an image with 25 steps takes around ~6 seconds, except for the first request which can take 45-60s.
note: If the request times out, just rerun again. Only the first request takes a long time.
Delete model and endpoint
To clean up, we can delete the model and endpoint.
Conclusion
In this post, we deployed Stable Diffusion XL on a single inf2.xlarge
instance costing $0.99/hour on Amazon SageMaker using Optimum Neuron.
We achieved ~6s per image generation leading to ~10 images per minute or ~600 images per hour. This would translate to ~0.0016$ per image if utilized well.
For startups and companies looking into GPU alternative Inferentia2 is a great option for not only efficient and fast but also cost-effective inference.
Thanks for reading! If you have any questions, feel free to contact me on Twitter or LinkedIn.