With our recent announcement of support for custom containers in Azure Machine Learning comes support for a wide variety of machine learning frameworks and servers including TensorFlow Serving, R, and ML.NET. In this blog post, we'll show you how to deploy a PyTorch model using TorchServe.
To use TorchServe, you first need to export your model in the "Model Archive Repository" (.mar) format. Follow the PyTorch quickstart to learn how to do this for your PyTorch model.
Save your .mar file in a directory called "torchserve."
Construct a Dockerfile
In the existing sample, we have a two-line Dockerfile:
Modify this Dockerfile to pass the name of your exported model from the previous step for the "--models" argument.
Build an image
Now, build a Docker image from the Dockerfile in the previous step, and store this image in the Azure Container Registry associated with your workspace:
WORKSPACE=$(az config get --query "defaults[?name == 'workspace'].value" -o tsv)
ACR_NAME=$(az ml workspace show -w $WORKSPACE --query container_registry -o tsv | cut -d'/' -f9-)
if [[ $ACR_NAME == "" ]]
echo "ACR login failed, exiting"
az acr login -n $ACR_NAME
az acr build $BASE_PATH/ -f $BASE_PATH/torchserve.dockerfile -t $IMAGE_TAG -r $ACR_NAME
Ensure that you can serve your model by doing a local test. You will need to have Docker installed for this to work. Below, we show you how to run the image, download some sample data, and send a test liveness and scoring request.
# Run image locally for testing
docker run --rm -d -p 8080:8080 --name torchserve-test \
-e MODEL_BASE_PATH=$MODEL_BASE_PATH \
-v $PWD/$BASE_PATH/torchserve:$MODEL_BASE_PATH/torchserve $IMAGE_TAG
# Check Torchserve health
echo "Checking Torchserve health..."
# Download test image
echo "Downloading test image..."
wget https://aka.ms/torchserve-test-image -O kitten_small.jpg
# Check scoring locally
echo "Uploading testing image, the scoring is..."
curl http://localhost:8080/predictions/densenet161 -T kitten_small.jpg
docker stop torchserve-test
Create endpoint YAML
Create a YAML file that specifies the properties of the managed online endpoint you would like to create. In the example below, we specify the location of the model we will use as well as the Azure Virtual Machine size to use when deploying.