Speed up BERT inference with Hugging Face Transformers and AWS Inferentia
notebook: sagemaker/18_inferentia_inference
The adoption of BERT and Transformers continues to grow. Transformer-based models are now not only achieving state-of-the-art performance in Natural Language Processing but also for Computer Vision, Speech, and Time-Series. 💬 🖼 🎤 ⏳
Companies are now slowly moving from the experimentation and research phase to the production phase in order to use transformer models for large-scale workloads. But by default BERT and its friends are relatively slow, big, and complex models compared to the traditional Machine Learning algorithms. Accelerating Transformers and BERT is and will become an interesting challenge to solve in the future.
AWS's take to solve this challenge was to design a custom machine learning chip designed for optimized inference workload called AWS Inferentia. AWS says that AWS Inferentia “delivers up to 80% lower cost per inference and up to 2.3X higher throughput than comparable current generation GPU-based Amazon EC2 instances.”
The real value of AWS Inferentia instances compared to GPU comes through the multiple Neuron Cores available on each device. A Neuron Core is the custom accelerator inside AWS Inferentia. Each Inferentia chip comes with 4x Neuron Cores. This enables you to either load 1 model on each core (for high throughput) or 1 model across all cores (for lower latency).
Tutorial
In this end-to-end tutorial, you will learn how to speed up BERT inference for text classification with Hugging Face Transformers, Amazon SageMaker, and AWS Inferentia.
You can find the notebook here: sagemaker/18_inferentia_inference
You will learn how to:
- 1. Convert your Hugging Face Transformer to AWS Neuron
- 2. Create a custom
inference.py
script fortext-classification
- 3. Create and upload the neuron model and inference script to Amazon S3
- 4. Deploy a Real-time Inference Endpoint on Amazon SageMaker
- 5. Run and evaluate Inference performance of BERT on Inferentia
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.
1. Convert your Hugging Face Transformer to AWS Neuron
We are going to use the AWS Neuron SDK for AWS Inferentia. The Neuron SDK includes a deep learning compiler, runtime, and tools for converting and compiling PyTorch and TensorFlow models to neuron compatible models, which can be run on EC2 Inf1 instances.
As a first step, we need to install the Neuron SDK and the 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 Neuron SDK we can load and convert our model. Neuron models are converted using torch_neuron
with its trace
method similar to torchscript
. You can find more information in our documentation.
To be able to convert our model we first need to select the model we want to use for our text classification pipeline from hf.co/models. For this example, let's go with distilbert-base-uncased-finetuned-sst-2-english but this can be easily adjusted with other BERT-like models.
At the time of writing, the AWS Neuron SDK does not support dynamic shapes, which means that the input size needs to be static for compiling and inference.
In simpler terms, this means that when the model is compiled with e.g. an input of batch size 1 and sequence length of 16, the model can only run inference on inputs with that same shape.
When using a t2.medium
instance the compilation takes around 3 minutes
inference.py
script for text-classification
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, this feature is not supported with AWS Inferentia, which means we need to provide an inference.py
script for running inference.
If you would be interested in support for zero-code deployments for Inferentia let us know on the forum.
To use the inference script, we need to create an inference.py
script. In our example, we are going to overwrite the model_fn
to load our neuron model and the predict_fn
to create a text-classification pipeline.
If you want to know more about the inference.py
script check out this example. It explains amongst other things what model_fn
and predict_fn
are.
We are using the NEURONCORE_GROUP_SIZES=1
to make sure that each HTTP worker uses 1 Neuron core to maximize throughput.
3. Create and upload the neuron model and inference script to Amazon S3
Before we can deploy our neuron model to Amazon SageMaker we need to create a model.tar.gz
archive with all our model artifacts saved into tmp/
, e.g. neuron_model.pt
and upload this to Amazon S3.
To do this we need to set up our permissions.
Next, we create our model.tar.gz
. The inference.py
script will be placed into a code/
folder.
Now we can upload our model.tar.gz
to our session S3 bucket with sagemaker
.
4. Deploy a Real-time Inference Endpoint on Amazon SageMaker
After we have uploaded our model.tar.gz
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.
5. Run and evaluate Inference performance of BERT on Inferentia
The .deploy()
returns an HuggingFacePredictor
object which can be used to request inference.
We managed to deploy our neuron compiled BERT to AWS Inferentia on Amazon SageMaker. Now, let's test its performance. As a dummy load test, we will loop and send 10,000 synchronous requests to our endpoint.
Let's inspect the performance in cloudwatch.
The average latency for our BERT model is 5-6ms
for a sequence length of 128.
Delete model and endpoint
To clean up, we can delete the model and endpoint.
Conclusion
We successfully managed to compile a vanilla Hugging Face Transformers model to an AWS Inferentia compatible Neuron Model. After that we deployed our Neuron model to Amazon SageMaker using the new Hugging Face Inference DLC. We managed to achieve 5-6ms
latency per neuron core, which is faster than CPU in terms of latency, and achieves a higher throughput than GPUs since we ran 4 models in parallel.
If you or you company are currently using a BERT-like Transformer for encoder tasks (text-classification, token-classification, question-answering etc.), and the latency meets your requirements you should switch to AWS Inferentia. This will not only save costs, but can also increase efficiency and performance for your models.
We are planning to do a more detailed case study on cost-performance of transformers in the future, so stay tuned!
Also if you want to learn more about accelerating transformers you should also check out Hugging Face optimum.
Thanks for reading! If you have any questions, feel free to contact me, through Github, or on the forum. You can also connect with me on Twitter or LinkedIn.