Retaining Amazon SageMaker Instance Capacity with SageMaker Managed Warm Pools
Tags: sagemaker, cloudml, aws
Published: 2024-03-04 09:39:21

## An Alternative Solution to Cloud Instance Reservation

![](https://miro.medium.com/v2/resize:fit:700/0*9Pk4gA4I9wvgkCRq)
Photo by  [Ivan Slade](https://unsplash.com/@flowinteractive?utm_source=medium&utm_medium=referral)  on  [Unsplash](https://unsplash.com/?utm_source=medium&utm_medium=referral)

In previous posts (e.g.,  [here](https://towardsdatascience.com/6-steps-to-migrating-your-machine-learning-project-to-the-cloud-6d9b6e4f18e0)  and  [here](https://towardsdatascience.com/a-simple-solution-for-managing-cloud-based-ml-training-c80a69c6939a)), we covered some of the pros and cons of training ML workloads using  [Amazon SageMaker](https://aws.amazon.com/sagemaker/). In this post we address one of its more inconvenient limitations — its lack of support (as of the time of this writing) for training on  [reserved Amazon EC2 instances](https://aws.amazon.com/ec2/pricing/reserved-instances/). This limitation has become more and more restrictive of late due to the increasing difficulty to acquire the instance types required in a reliable and timely fashion. Recent advances in the field of generative AI have led to  [unprecedented demand](https://www.google.com/search?q=nvidia+cant+keep+up+with+the+demand&rlz=1C1GCEA_enIL1069IL1074&oq=nvidia+cant+keep+up+with+the+demand&gs_lcrp=EgZjaHJvbWUyCwgAEEUYChg5GKABMgkIARAhGAoYoAEyCQgCECEYChigATIJCAMQIRgKGKAB0gEINzI4OGowajeoAgCwAgA&sourceid=chrome&ie=UTF-8)  for AI compute while challenges in the  [global supply chain](https://en.wikipedia.org/wiki/2021%E2%80%932023_global_supply_chain_crisis)  continue to linger. In this post we propose a partial mitigation to this limitation using  [SageMaker managed warm pools](https://docs.aws.amazon.com/sagemaker/latest/dg/train-warm-pools.html). By using  [SageMaker managed warm pools](https://docs.aws.amazon.com/sagemaker/latest/dg/train-warm-pools.html)  you can, under certain circumstances, retain access to provisioned instance capacity for successive training workloads. Not only will this hold on to acquired capacity for as long as you need (up to four weeks), but it can also reduce the latency between experiments.

# Training With Managed Warm Pools - Example

In the example below, we start up a PyTorch training job on a  [p5.48xlarge](https://aws.amazon.com/ec2/instance-types/p5/)  instance of type using the  [Amazon SageMaker Python SDK](https://sagemaker.readthedocs.io/en/stable/)  (version 2.208). We use the  _keep_alive_period_in_seconds_  control to configure the instance to remain  _warm_  for ten minutes.
```
from sagemaker.pytorch import PyTorch  
  
# define job  
estimator = PyTorch(  
    role='<sagemaker role>',  
    entry_point='train.py',  
    instance_type='ml.p5.48xlarge',  
    instance_count=1,  
    framework_version='2.0.1',  
    py_version='py310',  
    keep_alive_period_in_seconds=60 # keep warm for 1 minute  
)  
  
# start job  
estimator.fit()
```
As long as we start up another job with  [matching settings](https://docs.aws.amazon.com/sagemaker/latest/dg/train-warm-pools.html#train-warm-pools-matching-criteria)  within the sixty seconds allotted, the same instance will be retained for the next job. Thus, by configuring the use of SageMaker warm pools we have guaranteed instance capacity for our next workload. As an added bonus, the  _start-up time_ of the second workload will be noticeably reduced since the instance has already been provisioned.

## Limitations of Managed Warm Pools

Although this technique offers an instance capacity guarantee similar to the one provided by  [Amazon EC2 reservations](https://aws.amazon.com/ec2/pricing/reserved-instances/)  (and without the long-term commitment!!), it is important to note its significant limitations.

1.  The method relies on our ability to secure instance capacity for the first training job. Generally speaking, this is a safe assumption — sooner or later, we will succeed in securing an instance, but it is hard to know how much time and patience will be required.
2.  The method assumes that our workloads have matching settings, particularly with regards to the number and types of instances. Although AI development teams will frequently have multiple workloads with similar instance requirements, the inability to share resources between jobs with different settings (e.g., with different  [SageMaker Roles](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html)) is limiting.
3.  The method works only if the subsequent workload is started within the specified warm pool duration, with the maximum duration being one hour. Unless we want to constantly monitor our training jobs to detect when they stop, we will need to implement an automated system for submitting new jobs when our provisioned instances become available.
4.  In cases where a matching training job is not found during the warm pool durations, we still need to pay for the provisioned instance. Thus, there is a certain risk of waste associated with this method and the way it is used (e.g., the most appropriate warm pool duration setting) should be planned accordingly.
5.  The maximum period of time during which an instance can be retained in this manner is  [twenty-eight days](https://docs.aws.amazon.com/sagemaker/latest/dg/train-warm-pools.html#train-warm-pools-considerations).

Please see the  [official documentation](https://docs.aws.amazon.com/sagemaker/latest/dg/train-warm-pools.html#train-warm-pools-considerations)  for more details on how warm pooling works as well as additional  [considerations](https://docs.aws.amazon.com/sagemaker/latest/dg/train-warm-pools.html#train-warm-pools-considerations)  associated with its use.

# Reducing Cost with SageMaker Savings Plans

The method we have described for retaining control of instances is relevant for AI teams that have a consistent requirement for AI compute. This is manifested as a continuous backlog of training experiments waiting to be processed. In situations where this requirement is expected to last for an extended period of time,  [Amazon SageMaker Savings Plans](https://aws.amazon.com/savingsplans/ml-pricing/)  may provide a great opportunity for training-cost savings. SageMaker Savings Plans offer significant discounts in exchange for a commitment to pay for consistent usage. The instance types offered under this plan can vary — please refer to the documentation for the most up-do-date details. Importantly, despite some similarities to  [Amazon EC2 reservations](https://aws.amazon.com/ec2/pricing/reserved-instances/), SageMaker Savings Plans does  _not_  guarantee instance capacity. However, the method described in this post for retaining control of provisioned instance capacity can help you take the most advantage of the instances you have committed to using.

SageMaker Savings Plans is not for everyone. Make sure to fully understand all the terms of the offering before deciding whether it is the right solution for your team.

# Summary

A common approach for dealing with the difficulty of acquiring AI/ML compute resources in the cloud is to guarantee capacity by purchasing instance reservations. Unfortunately, as of the time of this writing, Amazon SageMaker does not support instance reservation. In this post, we have demonstrated how SageMaker warm pools can be used to maintain control over instance capacity for successive training workloads. We noted that for this type of solution to be effective, we require some form of mechanism for automating the detection of available warm pool instances and triggering a job with matching settings. In a future post we will propose a solution that addresses this challenge.