Notebooks
A
Amazon Web Services
Sm App Autoscaling Realtime Endpoints Step Scaling

Sm App Autoscaling Realtime Endpoints Step Scaling

deploy_and_monitorsm-app_autoscaling_realtime_endpoints_step_scalingdata-scienceinferenceamazon-sagemaker-examplesreinforcement-learningmachine-learningawsexamplesdeep-learningsagemakerjupyter-notebooktrainingmlops

Faster autoscaling on Amazon SageMaker realtime endpoints (Step Scaling)


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 Step Scaling autoscaling policies to the endpoint.

Please use at least m5.2xlarge or larger instance types if running this on Amazon SageMaker Notebook Instance.

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)

Ensure python version of kernel is 3.10

[ ]

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

[ ]

Restart kernel after installing packages

[ ]
[ ]
[ ]

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)

NOTE: Running the following cell is 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.
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.
[ ]

Apply Step-Scaling autoscaling policies to endpoint

  • Step 1: Register Scalable Target
  • Step 2: Create Scale-Out Policy
  • Step 3: Create Scale-In Policy
  • Step 4: Create CloudWatch Alarms

Define and apply the step-scaling policy for scaling out.

[ ]

Create StepScaling Scale-out Policy

[ ]

Create StepScaling Scale-In Policy

[ ]

Create CloudWatch alarms (Step-Scaling)

Create CloudWatch Alarms using new ConcurrentRequestsPerModel high-resolution Metric.

[ ]

Trigger autoscaling action

Use 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 for `trigger_autoscaling` function implementation

Monitor 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 for `monitor_scaling_events` function implementation
NOTE: ⚠️Per the ScaleOut Alarm, scale-out actions only start after the threshold of ConcurrentRequestsPerModel >= 20 for 3 datapoints within 3 minutes 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 action scaling times (Optional)

NOTE: ⚠️Per the ScaleIn Alarm, scale-in actions only start after the threshold of ConcurrentRequestsPerModel <= 10 for 3 datapoints within 3 minutes is breached.
[ ]

Cleanup

  • Delete cloudwatch alarms
  • Delete scaling policies
  • Deregister scalable target
  • 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