Migrate Pytorch Sagemaker Endpoints To TrueFoundry Platform
Truefoundry provides a straightforward migration path for Pytorch endpoints already running in Sagemaker using a custom inference script to the Truefoundry platform. We will start with an inference script and follow a workflow to implement the expected changes.
Existing Code
A Sagemaker deployment typically contains code in the form of the following file tree -
inference.py
- This is the inference handler that implements the Sagemaker functions likemodel_fn
,input_fn
,predict_fn
,output_fn
etcrequirements.txt
- This contains any additional Python packages needed by the inference handler
Apart from these, there are also -
- Model artifacts - Generated model files (e.g.
model.pth
). These may reside on your S3 buckets. - Sagemaker deployment code (e.g.
sagemaker_deploy.py
) - Code to call Sagemaker to deploy the model as an endpoint
Examples:
inference.py
sagemaker_deploy.py
Migration Workflow
Broadly speaking these are the things we shall do -
- Enclose the inference handler within a Docker container containing
torchserve
to support pytorch-based models - Upload the model artifact as a Truefoundry Artifact to make it accessible from the running container
- Launch a TrueFoundry deployment utilizing the above two pieces
Steps
-
Upload the Pytorch model artifacts to the Truefoundry Model Registry.
For reference, your model on disk may look like this
Add a upload_model.py
- Create an ML repo (docs) and provide access to that repo from the workspace we need to deploy in (docs). Change
ml_repo
arguments - Change
model_file_or_folder
to point to your model folder
The code prints a model fqn at the end which we will use in later steps.
- We will write a Python script called
main.py
that could launch thetorchserve
process at startup -
- Next, we’ll write a
Dockerfile
that can create the Truefoundry application
-
Now let’s go ahead and write a
deploy.py
script that can be used with Truefoundry to get a service deployed. Here you’ll need to change the following- Service Name - Name for the service we’ll deploy
- Entrypoint Script Name (value for
SAGEMAKER_PROGRAM
) - The code file name containingmodel_fn
,input_fn
,predict_fn
andoutput_fn
- Model Version FQN - The FQN obtained from
upload_model.py
- Deploy using
servicefoundry
- Once the deployment has gone through, it can be tested using this script -