Notebooks
A
Amazon Web Services
Sm App Autoscaling Realtime Endpoints

Sm App Autoscaling Realtime Endpoints

deploy_and_monitordata-scienceinferencesm-app_autoscaling_realtime_endpointsamazon-sagemaker-examplesreinforcement-learningmachine-learningawsexamplesdeep-learningsagemakerjupyter-notebooktrainingmlops

Faster autoscaling on Amazon SageMaker realtime endpoints (Application Autoscaling)


This notebook's CI test result for us-west-2 is as follows. CI test results in other regions can be found at the end of the notebook.

This us-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable


In this notebook we show how the new faster autoscaling feature helps scale sagemaker inference endpoints by almost 6x faster than earlier.

We deploy Meta's Llama3-8B-Instruct model to an Amazon SageMaker realtime endpoint using Text Generation Inference (TGI) Deep Learning Container (DLC) and apply Application Autoscaling scaling policies to the endpoint.

Please select m5.2xlarge or larger instance types when running this on Amazon SageMaker Notebook Instance.
Select conda_pytorch_p310 kernel when running this notebook on Amazon SageMaker Notebook Instance.

Ensure python version for the kernel is 3.10.x (3.11 is not supported).

Prerequisites

Before using this notebook please ensure you have access to an active access token from HuggingFace and have accepted the license agreement from Meta.
  • Step 1: Create user access token in HuggingFace (HF). Refer here on how to create HF tokens.
  • Step 2: Login to HuggingFace and navigate to Meta-Llama-3-8B-Instruct* home page.
  • Step 3: Accept META LLAMA 3 COMMUNITY LICENSE AGREEMENT by following the instructions here
  • Step 4: Wait for the approval email from META (Approval may take any where b/w 1-3 hrs)

Install packages using uv, an extremely fast python package installer
Read more about uv here https://astral.sh/blog/uv

[ ]
[ ]
[ ]
[ ]
[ ]

Initiate sagemaker session

[ ]

Deploy model

Create and deploy model using Amazon SageMaker HuggingFace TGI DLC

https://sagemaker.readthedocs.io/en/stable/api/inference/model.html#sagemaker.model.Model.deploy

NOTE: Remember to copy your Hugging Face Access Token from https://hf.co/ before running the below cell.

Refer here to learn about creating HF tokens.
[ ]

Inference

Invoke and test endpoint using messages API. Refer to HF Messages API for more info.

[ ]

Baseline average latency at various concurrency levels (Optional)

By capturing average latency across various concurrency levels, we can get a fair idea on after how many concurrent request does endpoint performance would degrade significantly.

Having this information can help define values for scaling policy accordingly.

Running below cell is optional

INFO: ℹ️ Signal here is, at a given concurrency level you start to see average latency increase significantly.
At this concurrency level the endpoint gets overloaded and cannot serve requests in a timely fashion.
We use these values to set as threshold values for autoscaling.

NOTE: ⚠️ As concurrent requests to the endpoint increase you might observe ThrottlingException errors as we haven't incorporated exponential backoff and retry mechanisms.
[ ]

Apply Autoscaling policies to the endpoint

Apply Application Autoscaling Policy to endpoint

  1. Register Scalable Target
[ ]

Use the latest high-resolution Metrics to trigger auto-scaling

  • New feature introduces a new PredefinedMetricType for scaling policy configuration i.e. SageMakerVariantConcurrentRequestsPerModelHighResolution to trigger scaling actions.
  • Creating a scaling policy with this metric type will create cloudwatch alarms that track a new metric called ConcurrentRequestsPerModel.
  • These high-resolution metrics are published at sub-minute intervals (10s intervals to CW + any additional jitter + delays)
  • We should observe significant improvement in scale out times with this new metric

Steps to create Application autoscaling policy

  • Create scaling policy
    • Set PolicyType to TargetTrackingScaling
    • Set TargetValue to 5.0. i.e., Scaling triggers when endpoint receives 5 ConcurrentRequestsPerModel
    • Set PredefinedMetricType to SageMakerVariantConcurrentRequestsPerModelHighResolution
    • Set ScaleInCoolDown and ScaleOutCoolDown values to 300 seconds
[ ]

Trigger autoscaling action

LLMPerf to generate traffic to the endpoint

Refer to https://github.com/philschmid/llmperf for more details on LLMPerf.

Run the LLMPerf traffic generation script in the background using subprocess.Popen

INFO:ℹ️ Refer to utils/llmperf.py for trigger_autoscaling function implementation

Monitor Scale-Out Alarm Trigger times and scaling event times

As llmperf generates traffic to the endpoint continuously this trigger auto-scaling.

The monitor_scaling_events function does the following:

  • Calculates time taken for alarm to go into InAlarm state.
  • checks if alarm is InAlarm state. If yes, then starts the scaling timer
  • continuously monitors the DesiredInstanceCount property of the endpoint
    • waits till CurrentInstanceCount == DesiredInstanceCount and EndpointStatus is InService
  • Calculates time taken to scale out instances prints the times in a table

The below cell triggers auto scaling action and calls the monitor_scaling_events immediately on the AlarmHigh

INFO: ℹ️ Refer to utils/autoscaling.py for monitor_scaling_events function implementation
NOTE: ⚠️The AlarmHigh Alarm triggers scale out actions only after the threshold of ConcurrentRequestsPerModel >5 for 3 datapoints within 30 seconds is breached.
[ ]

Monitor if the background process (llmperf) is completed.

[ ]

Print LLMPerf results

LLMPerf writes the results to "results/" directory. summary.json file has the endpoint benchmarking data.

[ ]

Monitor Scale-in Alarm Trigger times and scaling event times

NOTE: ⚠️The AlarmLow Alarm triggers scale-in actions only after the threshold of ConcurrentRequestsPerModel < 4.5 for 90 datapoints within 15 minutes is breached.
Running the below cell with take approximately 15 minutes to complete.
[ ]

Cleanup

  • Deregister scalable target. This automatically deletes associated cloudwatch alarms.
  • Delete model
  • Delete endpoint
[ ]

Notebook CI Test Results

This notebook was tested in multiple regions. The test results are as follows, except for us-west-2 which is shown at the top of the notebook.

This us-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This us-east-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This us-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ca-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This sa-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-west-3 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-north-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-southeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-southeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-northeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-northeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-south-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable