Tutorial: Host a PyTorch Model for Inference on an Amazon EC2 Instance

In this tutorial, I will walk you through the steps involved in hosting a PyTorch model on Amazon Web Services’ EC2 backed by an EFS file system. This article is a part of the series on making AWS Lambda functions stateful with Amazon EFS (Part 1, Part 2).
Assuming you followed the steps mentioned in the previous tutorial, you should have an EC2 instance with a mounted EFS file system. We will now configure it to run a Flask server that exposes a PyTorch inference API.
Our goal is to install all the dependencies of PyTorch and test the inference on EC2 before running it in AWS Lambda.
Preparing the EC2 Instance
We will start by creating two directories that will hold the PyTorch modules and the pre-trained ResNet model respectively.
1 2 |
sudo mkdir -p /mnt/efs/fs1/ml/lib sudo mkdir -p /mnt/efs/fs1/ml/model |
Let’s start by taking ownership of the EFS file system. This will enable us to install everything that’s needed for the tutorial.
1 |
sudo chown -R ec2-user:ec2-user /mnt/efs/fs1 |
It’s time to install Python 3.8 and Git client on Amazon Linux 2 instance.
1 2 |
sudo amazon-linux-extras install -y python3.8 sudo yum install -y git |
Let’s add a link to Python3.8. This will make it easy to deal with different versions of Python installed in the machine.
1 |
sudo ln -s /usr/bin/python3.8 /usr/bin/python3 |
We will then install pip
utility.
1 2 |
curl -O https://bootstrap.pypa.io/get-pip.py python3 get-pip.py --user |
Installing PyTorch and Hosting the Inference API
Start by cloning the GitHub repository that has the model and the inference code.
1 |
git clone https://github.com/janakiramm/serverless_inference.git |
Navigate to the ec2
directory and run the following command to install the Python modules including PyTorch and Flask.
1 |
cd serverless_inference/ec2/ |
1 |
pip3 install -t /mnt/efs/fs1/ml/lib -r requirements.txt |
The above command installs the Python modules in the lib
directory of the EFS file system. This is the most important step where we populate the directory with all the dependencies that AWS Lambda will need.
According to the official documentation of pip, the -t or --target
switch installs packages into a specific directory. We will leverage this to ensure that the modules are installed in one of the EFS directories instead of the default location. You can see the installed modules in the /mnt/efs/fs1/ml/lib
directory.
Running the Inference API
Now that we have the basic environment configured, we are almost ready to host the PyTorch inference API.
First, let’s tell the Python runtime where to find the PyTorch and Flask modules. This can be achieved by setting the PYTHONPATH environment variable.
1 |
export PYTHONPATH=”/mnt/efs/fs1/ml/lib/” |
The trained model and label file are also passed through an environment variable. They are currently available in the ./model
directory.
Let’s copy them to the /mnt/efs/fs1/ml/model/
directory created in the EFS file system.
1 |
cp -R model/* /mnt/efs/fs1/ml/model/ |
Before running the inference service, we need to set the MODEL_DIR
environment variable with the location of the model and label file.
1 |
export MODEL_DIR=”/mnt/efs/fs1/ml/model/” |
We are now ready to launch the inference service.
1 |
python3 infer.py |
The above command launches the Flask server listening on the default port, 5000.
Since the security group associated with the EC2 instance has port 5000 open, we should be able to hit the endpoint.
Classifying an Image with the Inference API
Since the inference API expects a URL of the image, upload an image to a hosting service and send the URL as a parameter to the service. You can find a sample dog image that I uploaded at https://i.postimg.cc/v8pmjrwf/dog.jpg
Let’s send the same URL to the inference API.
1 |
curl -X POST "http://15.206.211.244:5000/predict?url=https://i.postimg.cc/v8pmjrwf/dog.jpg" |
The response from the API confirms that the inference service is working.
Giving Back the Ownership of EFS to POSIX User ID
Now that we are done with the configuration and testing, it’s time to relinquish the ownership of the EFS root. We do that by making the POSIX user the owner of the file system.
Make sure you run the below command before terminating the EC2 instance.
1 |
sudo chown -R 1001:1001 /mnt/efs/fs1 |
This tutorial demonstrated how to use an EFS file system to host Python modules and a trained model to run the inference API. With the file system fully populated with everything that’s needed for the inference service, we are ready to run it in AWS Lambda.
In the next part of the tutorial, we will port the inference API to Lambda to turn that into a serverless API. Check back tomorrow.
Janakiram MSV’s Webinar series, “Machine Intelligence and Modern Infrastructure (MI2)” offers informative and insightful sessions covering cutting-edge technologies. Sign up for the upcoming MI2 webinar at http://mi2.live.
Amazon Web Services is a sponsor of The New Stack.
Feature image via Pixabay.