Deploy PyTorch models with TorchServe in Azure Machine Learning online endpoints

Published Jun 21 2021 10:36 AM 2,087 Views
Microsoft

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.

The steps below reference our existing TorchServe sample here.

 

Export your model as a .mar file

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:

 

 

FROM pytorch/torchserve:latest-cpu

CMD ["torchserve","--start","--model-store","$MODEL_BASE_PATH/torchserve","--models","densenet161.mar","--ts-config","$MODEL_BASE_PATH/torchserve/config.properties"]

 

 

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 == "" ]]
then
    echo "ACR login failed, exiting"
    exit 1
fi

az acr login -n $ACR_NAME
IMAGE_TAG=${ACR_NAME}.azurecr.io/torchserve:8080
az acr build $BASE_PATH/ -f $BASE_PATH/torchserve.dockerfile -t $IMAGE_TAG -r $ACR_NAME

 

 

Test locally

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..."
curl http://localhost:8080/ping

# 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.

 

 

$schema: https://azuremlsdk2.blob.core.windows.net/latest/managedOnlineEndpoint.schema.json
name: torchserve-endpoint
type: online
auth_mode: aml_token
traffic:
  torchserve: 100

deployments:
  - name: torchserve
    model:
      name: torchserve-densenet161
      version: 1
      local_path: ./torchserve
    environment_variables:
      MODEL_BASE_PATH: /var/azureml-app/azureml-models/torchserve-densenet161/1
    environment:
      name: torchserve
      version: 1
      docker:
        image: {{acr_name}}.azurecr.io/torchserve:8080
      inference_config:
        liveness_route:
          port: 8080
          path: /ping
        readiness_route:
          port: 8080
          path: /ping
        scoring_route:
          port: 8080
          path: /predictions/densenet161
    instance_type: Standard_F2s_v2
    scale_settings:
      scale_type: manual
      instance_count: 1
      min_instances: 1
      max_instances: 2

 

 

Create endpoint

Now that you have tested locally and you have a YAML file, you can create your endpoint:

 

 

az ml endpoint create -f $BASE_PATH/$ENDPOINT_NAME.yml -n $ENDPOINT_NAME

 

 

Send a scoring request

Once your endpoint finishes deploying, you can send it unlabeled data for scoring:

 

 

# Get accessToken
echo "Getting access token..."
TOKEN=$(az ml endpoint get-credentials -n $ENDPOINT_NAME --query accessToken -o tsv)

# Get scoring url
echo "Getting scoring url..."
SCORING_URL=$(az ml endpoint show -n $ENDPOINT_NAME --query scoring_uri -o tsv)
echo "Scoring url is $SCORING_URL"

# Check scoring
echo "Uploading testing image, the scoring is..."
curl -H "Authorization: {Bearer $TOKEN}" -T kitten_small.jpg $SCORING_URL

 

 

Delete resources

Now that you have successfully created and tested your TorchServe endpoint, you can delete it.

 

 

# Delete endpoint
echo "Deleting endpoint..."
az ml endpoint delete -n $ENDPOINT_NAME --yes

# Delete model
echo "Deleting model..."
az ml model delete -n $AML_MODEL_NAME --version 1

 

 

Next steps

Read our documentation to learn more and see our other samples.

 

%3CLINGO-SUB%20id%3D%22lingo-sub-2466459%22%20slang%3D%22en-US%22%3EDeploy%20PyTorch%20models%20with%20TorchServe%20in%20Azure%20Machine%20Learning%20online%20endpoints%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2466459%22%20slang%3D%22en-US%22%3E%3CP%3EWith%20our%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-ai%2Fcustom-containers-in-azure-machine-learning-managed-online%2Fba-p%2F2460558%22%20target%3D%22_self%22%3Erecent%20announcement%3C%2FA%3E%20of%20support%20for%20custom%20containers%20in%20Azure%20Machine%20Learning%20comes%20support%20for%20a%20wide%20variety%20of%20machine%20learning%20frameworks%20and%20servers%20including%20TensorFlow%20Serving%2C%20R%2C%20and%20ML.NET.%20In%20this%20blog%20post%2C%20we'll%20show%20you%20how%20to%20deploy%20a%20PyTorch%20model%20using%20%3CA%20href%3D%22https%3A%2F%2Fpytorch.org%2Fserve%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3ETorchServe.%3C%2FA%3E%3C%2FP%3E%0A%3CP%3EThe%20steps%20below%20reference%20our%20existing%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazureml-examples%2Fblob%2Fmain%2Fcli%2Fdeploy-torchserve.sh%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3ETorchServe%20sample%20here%3C%2FA%3E.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--437351370%22%20id%3D%22toc-hId--437351341%22%3EExport%20your%20model%20as%20a%20.mar%20file%3C%2FH2%3E%0A%3CP%3ETo%20use%20TorchServe%2C%20you%20first%20need%20to%20export%20your%20model%20in%20the%20%22Model%20Archive%20Repository%22%20(.mar)%20format.%20Follow%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fpytorch%2Fserve%2Ftree%2Fmaster%2Fmodel-archiver%23creating-a-model-archive%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EPyTorch%20quickstart%3C%2FA%3E%20to%20learn%20how%20to%20do%20this%20for%20your%20PyTorch%20model.%3C%2FP%3E%0A%3CP%3ESave%20your%20.mar%20file%20in%20a%20directory%20called%20%22torchserve.%22%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-2050161463%22%20id%3D%22toc-hId-2050161492%22%3EConstruct%20a%20Dockerfile%3C%2FH2%3E%0A%3CP%3EIn%20the%20existing%20sample%2C%20we%20have%20a%20two-line%20Dockerfile%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3EFROM%20pytorch%2Ftorchserve%3Alatest-cpu%0A%0ACMD%20%5B%22torchserve%22%2C%22--start%22%2C%22--model-store%22%2C%22%24MODEL_BASE_PATH%2Ftorchserve%22%2C%22--models%22%2C%22densenet161.mar%22%2C%22--ts-config%22%2C%22%24MODEL_BASE_PATH%2Ftorchserve%2Fconfig.properties%22%5D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EModify%20this%20Dockerfile%20to%20pass%20the%20name%20of%20your%20exported%20model%20from%20the%20previous%20step%20for%20the%20%22--models%22%20argument.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-242707000%22%20id%3D%22toc-hId-242707029%22%3EBuild%20an%20image%3C%2FH2%3E%0A%3CP%3ENow%2C%20build%20a%20Docker%20image%20from%20the%20Dockerfile%20in%20the%20previous%20step%2C%20and%20store%20this%20image%20in%20the%20Azure%20Container%20Registry%20associated%20with%20your%20workspace%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3EWORKSPACE%3D%24(az%20config%20get%20--query%20%22defaults%5B%3Fname%20%3D%3D%20'workspace'%5D.value%22%20-o%20tsv)%0AACR_NAME%3D%24(az%20ml%20workspace%20show%20-w%20%24WORKSPACE%20--query%20container_registry%20-o%20tsv%20%7C%20cut%20-d'%2F'%20-f9-)%0A%0Aif%20%5B%5B%20%24ACR_NAME%20%3D%3D%20%22%22%20%5D%5D%0Athen%0A%20%20%20%20echo%20%22ACR%20login%20failed%2C%20exiting%22%0A%20%20%20%20exit%201%0Afi%0A%0Aaz%20acr%20login%20-n%20%24ACR_NAME%0AIMAGE_TAG%3D%24%7BACR_NAME%7D.azurecr.io%2Ftorchserve%3A8080%0Aaz%20acr%20build%20%24BASE_PATH%2F%20-f%20%24BASE_PATH%2Ftorchserve.dockerfile%20-t%20%24IMAGE_TAG%20-r%20%24ACR_NAME%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1564747463%22%20id%3D%22toc-hId--1564747434%22%3ETest%20locally%3C%2FH2%3E%0A%3CP%3EEnsure%20that%20you%20can%20serve%20your%20model%20by%20doing%20a%20local%20test.%20You%20will%20need%20to%20have%20Docker%20installed%20for%20this%20to%20work.%20Below%2C%20we%20show%20you%20how%20to%20run%20the%20image%2C%20download%20some%20sample%20data%2C%20and%20send%20a%20test%20liveness%20and%20scoring%20request.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3E%23%20Run%20image%20locally%20for%20testing%0Adocker%20run%20--rm%20-d%20-p%208080%3A8080%20--name%20torchserve-test%20%5C%0A%20%20%20%20-e%20MODEL_BASE_PATH%3D%24MODEL_BASE_PATH%20%5C%0A%20%20%20%20-v%20%24PWD%2F%24BASE_PATH%2Ftorchserve%3A%24MODEL_BASE_PATH%2Ftorchserve%20%24IMAGE_TAG%0A%0A%23%20Check%20Torchserve%20health%0Aecho%20%22Checking%20Torchserve%20health...%22%0Acurl%20http%3A%2F%2Flocalhost%3A8080%2Fping%0A%0A%23%20Download%20test%20image%0Aecho%20%22Downloading%20test%20image...%22%0Awget%20https%3A%2F%2Faka.ms%2Ftorchserve-test-image%20-O%20kitten_small.jpg%0A%0A%23%20Check%20scoring%20locally%0Aecho%20%22Uploading%20testing%20image%2C%20the%20scoring%20is...%22%0Acurl%20http%3A%2F%2Flocalhost%3A8080%2Fpredictions%2Fdensenet161%20-T%20kitten_small.jpg%0A%0Adocker%20stop%20torchserve-test%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-922765370%22%20id%3D%22toc-hId-922765399%22%3ECreate%20endpoint%20YAML%3C%2FH2%3E%0A%3CP%3ECreate%20a%20YAML%20file%20that%20specifies%20the%20properties%20of%20the%20managed%20online%20endpoint%20you%20would%20like%20to%20create.%20In%20the%20example%20below%2C%20we%20specify%20the%20location%20of%20the%20model%20we%20will%20use%20as%20well%20as%20the%20Azure%20Virtual%20Machine%20size%20to%20use%20when%20deploying.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-yaml%22%3E%3CCODE%3E%24schema%3A%20https%3A%2F%2Fazuremlsdk2.blob.core.windows.net%2Flatest%2FmanagedOnlineEndpoint.schema.json%0Aname%3A%20torchserve-endpoint%0Atype%3A%20online%0Aauth_mode%3A%20aml_token%0Atraffic%3A%0A%20%20torchserve%3A%20100%0A%0Adeployments%3A%0A%20%20-%20name%3A%20torchserve%0A%20%20%20%20model%3A%0A%20%20%20%20%20%20name%3A%20torchserve-densenet161%0A%20%20%20%20%20%20version%3A%201%0A%20%20%20%20%20%20local_path%3A%20.%2Ftorchserve%0A%20%20%20%20environment_variables%3A%0A%20%20%20%20%20%20MODEL_BASE_PATH%3A%20%2Fvar%2Fazureml-app%2Fazureml-models%2Ftorchserve-densenet161%2F1%0A%20%20%20%20environment%3A%0A%20%20%20%20%20%20name%3A%20torchserve%0A%20%20%20%20%20%20version%3A%201%0A%20%20%20%20%20%20docker%3A%0A%20%20%20%20%20%20%20%20image%3A%20%7B%7Bacr_name%7D%7D.azurecr.io%2Ftorchserve%3A8080%0A%20%20%20%20%20%20inference_config%3A%0A%20%20%20%20%20%20%20%20liveness_route%3A%0A%20%20%20%20%20%20%20%20%20%20port%3A%208080%0A%20%20%20%20%20%20%20%20%20%20path%3A%20%2Fping%0A%20%20%20%20%20%20%20%20readiness_route%3A%0A%20%20%20%20%20%20%20%20%20%20port%3A%208080%0A%20%20%20%20%20%20%20%20%20%20path%3A%20%2Fping%0A%20%20%20%20%20%20%20%20scoring_route%3A%0A%20%20%20%20%20%20%20%20%20%20port%3A%208080%0A%20%20%20%20%20%20%20%20%20%20path%3A%20%2Fpredictions%2Fdensenet161%0A%20%20%20%20instance_type%3A%20Standard_F2s_v2%0A%20%20%20%20scale_settings%3A%0A%20%20%20%20%20%20scale_type%3A%20manual%0A%20%20%20%20%20%20instance_count%3A%201%0A%20%20%20%20%20%20min_instances%3A%201%0A%20%20%20%20%20%20max_instances%3A%202%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--884689093%22%20id%3D%22toc-hId--884689064%22%3ECreate%20endpoint%3C%2FH2%3E%0A%3CP%3ENow%20that%20you%20have%20tested%20locally%20and%20you%20have%20a%20YAML%20file%2C%20you%20can%20create%20your%20endpoint%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Eaz%20ml%20endpoint%20create%20-f%20%24BASE_PATH%2F%24ENDPOINT_NAME.yml%20-n%20%24ENDPOINT_NAME%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1602823740%22%20id%3D%22toc-hId-1602823769%22%3ESend%20a%20scoring%20request%3C%2FH2%3E%0A%3CP%3EOnce%20your%20endpoint%20finishes%20deploying%2C%20you%20can%20send%20it%20unlabeled%20data%20for%20scoring%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3E%23%20Get%20accessToken%0Aecho%20%22Getting%20access%20token...%22%0ATOKEN%3D%24(az%20ml%20endpoint%20get-credentials%20-n%20%24ENDPOINT_NAME%20--query%20accessToken%20-o%20tsv)%0A%0A%23%20Get%20scoring%20url%0Aecho%20%22Getting%20scoring%20url...%22%0ASCORING_URL%3D%24(az%20ml%20endpoint%20show%20-n%20%24ENDPOINT_NAME%20--query%20scoring_uri%20-o%20tsv)%0Aecho%20%22Scoring%20url%20is%20%24SCORING_URL%22%0A%0A%23%20Check%20scoring%0Aecho%20%22Uploading%20testing%20image%2C%20the%20scoring%20is...%22%0Acurl%20-H%20%22Authorization%3A%20%7BBearer%20%24TOKEN%7D%22%20-T%20kitten_small.jpg%20%24SCORING_URL%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--204630723%22%20id%3D%22toc-hId--204630694%22%3EDelete%20resources%3C%2FH2%3E%0A%3CP%3ENow%20that%20you%20have%20successfully%20created%20and%20tested%20your%20TorchServe%20endpoint%2C%20you%20can%20delete%20it.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3E%23%20Delete%20endpoint%0Aecho%20%22Deleting%20endpoint...%22%0Aaz%20ml%20endpoint%20delete%20-n%20%24ENDPOINT_NAME%20--yes%0A%0A%23%20Delete%20model%0Aecho%20%22Deleting%20model...%22%0Aaz%20ml%20model%20delete%20-n%20%24AML_MODEL_NAME%20--version%201%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2466459%22%20slang%3D%22en-US%22%3E%3CP%3ELearn%20how%20to%20deploy%20a%20TorchServe%20model%20as%20a%20managed%20online%20endpoint%20in%20Azure%20Machine%20Learning%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2466459%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20Machine%20Learning%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMLOps%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Jun 21 2021 10:46 AM
Updated by:
www.000webhost.com