Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/main/sagemaker/13_deploy_and_autoscaling_transformers/sagemaker-notebook.ipynb
Views: 2542
Going Production: Auto-scale Hugging Face Transformer Endpoints with Amazon SageMaker
Welcome to this getting started guide, we will use the new Hugging Face Inference DLCs and Amazon SageMaker Python SDK to deploy a transformer model for real-time inference. In this example we are going to deploy a trained Hugging Face Transformer model on to SageMaker for inference.
Deploy one of the 15 000+ Hugging Face Transformers to Amazon SageMaker for Inference
To deploy a model directly from the Hub to SageMaker we need to define 2 environment variables when creating the HuggingFaceModel
. We need to define:
HF_MODEL_ID
: defines the model id, which will be automatically loaded from huggingface.co/models when creating or SageMaker Endpoint. The 🤗 Hub provides +10 000 models all available through this environment variable.HF_TASK
: defines the task for the used 🤗 Transformers pipeline. A full list of tasks can be find here.
Architecture
The Hugging Face Inference Toolkit for SageMaker is an open-source library for serving Hugging Face transformer models on SageMaker. It utilizes the SageMaker Inference Toolkit for starting up the model server, which is responsible for handling inference requests. The SageMaker Inference Toolkit uses Multi Model Server (MMS) for serving ML models. It bootstraps MMS with a configuration and settings that make it compatible with SageMaker and allow you to adjust important performance parameters, such as the number of workers per model, depending on the needs of your scenario.
Deploying a model using SageMaker hosting services is a three-step process:
Create a model in SageMaker —By creating a model, you tell SageMaker where it can find the model components.
Create an endpoint configuration for an HTTPS endpoint —You specify the name of one or more models in production variants and the ML compute instances that you want SageMaker to launch to host each production variant.
Create an HTTPS endpoint —Provide the endpoint configuration to SageMaker. The service launches the ML compute instances and deploys the model or models as specified in the configuration
Model Monitoring
Auto Scaling your Model
Amazon SageMaker is a fully managed service that provides every developer and data scientist with the ability to quickly build, train, and deploy machine learning (ML) models at scale.
Autoscaling is an out-of-the-box feature that monitors your workloads and dynamically adjusts the capacity to maintain steady and predictable performance at the possible lowest cost.
The following diagram is a sample architecture that showcases how a model is served as a endpoint with autoscaling enabled.
Reference Blog post Configuring autoscaling inference endpoints in Amazon SageMaker
Configure Autoscaling for our Endpoint
You can define minimum, desired, and maximum number of instances per endpoint and, based on the autoscaling configurations, instances are managed dynamically. The following diagram illustrates this architecture.
AWS offers many different ways to auto-scale your endpoints. One of them Simple-Scaling, where you scale the instance capacity based on CPUUtilization
of the instances or SageMakerVariantInvocationsPerInstance
.
In this example we are going to use SageMakerVariantInvocationsPerInstance
to auto-scale our Endpoint
Create Scaling Policy with configuration details, e.g. TargetValue
when the instance should be scaled.
stress test the endpoint with threaded requests
Monitor the InvocationsPerInstance
in cloudwatch
check the endpoint instance_count number