사용자 지정 컨테이너를 사용하여 모델을 온라인 엔드포인트에 배포

적용 대상:Azure CLI ml 확장 v2(현재)Python SDK azure-ai-ml v2(현재)

Azure Machine Learning의 온라인 엔드포인트에 모델을 배포하기 위해 사용자 지정 컨테이너를 사용하는 방법을 알아봅니다.

사용자 지정 컨테이너 배포는 Azure Machine Learning에서 사용되는 기본 Python Flask 서버 이외의 웹 서버를 사용할 수 있습니다. 이러한 배포의 사용자는 Azure Machine Learning의 기본 제공 모니터링, 크기 조정, 경고 및 인증을 계속 활용할 수 있습니다.

다음 표에는 TensorFlow 서비스 제공, TorchServe, Triton Inference Server, Plumber R 패키지, Azure Machine Learning Inference Minimal 이미지와 같은 사용자 지정 컨테이너를 사용하는 다양한 배포 예가 나열되어 있습니다.

예시 스크립트(CLI) 설명
minimal/multimodel deploy-custom-container-minimal-multimodel Azure Machine Learning Inference Minimal 이미지를 확장하여 여러 모델을 단일 배포에 배포합니다.
minimal/single-model deploy-custom-container-minimal-single-model Azure Machine Learning Inference Minimal 이미지를 확장하여 단일 모델을 배포합니다.
mlflow/multideployment-scikit deploy-custom-container-mlflow-multideployment-scikit Azure Machine Learning Inference Minimal 이미지를 사용하여 단일 엔드포인트 뒤에 있는 두 개의 별도 배포에 서로 다른 Python 요구 사항이 있는 두 개의 MLFlow 모델을 배포합니다.
r/multimodel-plumber deploy-custom-container-r-multimodel-plumber Plumber R 패키지를 사용하여 세 개의 회귀 모델을 하나의 엔드포인트에 배포
tfserving/half-plus-two deploy-custom-container-tfserving-half-plus-two 표준 모델 등록 프로세스를 사용하여 TensorFlow 서비스 제공 사용자 지정 컨테이너를 사용하여 간단한 Half Plus Two 모델을 배포합니다.
tfserving/half-plus-two-integrated deploy-custom-container-tfserving-half-plus-two-integrated 모델이 이미지에 통합된 TensorFlow 서비스 제공 사용자 지정 컨테이너를 사용하여 간단한 Half Plus Two 모델을 배포합니다.
torchserve/densenet deploy-custom-container-torchserve-densenet TorchServe 사용자 지정 컨테이너를 사용하여 단일 모델을 배포합니다.
torchserve/huggingface-textgen deploy-custom-container-torchserve-huggingface-textgen Hugging Face 모델을 온라인 엔드포인트에 배포하고 Hugging Face Transformers TorchServe 예를 따릅니다.
triton/single-model deploy-custom-container-triton-single-model 사용자 지정 컨테이너를 사용하여 Triton 모델 배포

이 문서에서는 TF(TensorFlow) 서비스 제공을 사용하여 TensorFlow 모델을 제공하는 데 중점을 둡니다.

Warning

Microsoft는 사용자 지정 이미지로 인해 발생하는 문제를 해결하는 데 도움을 주지 못할 수 없습니다. 문제가 발생하는 경우 기본 이미지 또는 Microsoft에서 제공하는 이미지 중 하나를 사용하여 문제가 이미지와 관련된 것인지 확인하라는 메시지가 표시될 수 있습니다.

필수 조건

이 문서의 단계를 수행하기 전에 다음과 같은 필수 구성 요소가 있는지 확인합니다.

  • Azure Machine Learning 작업 영역 리소스가 없으면 빠른 시작: 작업 영역 리소스 만들기 문서의 단계에서 리소스를 만듭니다.

  • Azure CLI 및 ml 확장 또는 Azure Machine Learning Python SDK v2:

    • Azure CLI 및 확장을 설치하려면 CLI(v2) 설치, 설정 및 사용을 참조하세요.

      Important

      이 문서의 CLI 예제에서는 Bash(또는 호환) 셸을 사용한다고 가정합니다. 예를 들어 Linux 시스템 또는 Linux용 Windows 하위 시스템에서 이러한 예제를 사용합니다.

    • Python SDK v2를 설치하려면 다음 명령을 사용합니다.

      pip install azure-ai-ml azure-identity
      

      기존 SDK 설치를 최신 버전으로 업데이트하려면 다음 명령을 사용합니다.

      pip install --upgrade azure-ai-ml azure-identity
      

      자세한 내용은 Azure Machine Learning용 Python SDK v2 설치를 참조하세요.

  • 사용자 또는 사용하는 서비스 주체는 작업 영역을 포함하는 Azure 리소스 그룹에 대한 Contributor 액세스 권한이 있어야 합니다. 빠른 시작 문서를 사용하여 작업 영역을 구성한 경우 이러한 리소스 그룹이 있을 것입니다.

  • 로컬에서 배포하려면 Docker 엔진이 로컬로 실행되고 있어야 합니다. 이 단계를 적극 권장합니다. 문제를 디버그하는 데 도움이 됩니다.

원본 코드 다운로드

이 자습서를 따르려면 아래 소스 코드를 다운로드합니다.

git clone https://github.com/Azure/azureml-examples --depth 1
cd azureml-examples/cli

환경 변수 초기화

환경 변수 정의:

BASE_PATH=endpoints/online/custom-container/tfserving/half-plus-two
AML_MODEL_NAME=tfserving-mounted
MODEL_NAME=half_plus_two
MODEL_BASE_PATH=/var/azureml-app/azureml-models/$AML_MODEL_NAME/1

TensorFlow 모델 다운로드

입력을 2로 나누고 결과에 2를 추가하는 모델을 다운로드하고 압축을 풉니다.

wget https://aka.ms/half_plus_two-model -O $BASE_PATH/half_plus_two.tar.gz
tar -xvf $BASE_PATH/half_plus_two.tar.gz -C $BASE_PATH

TF 서비스 제공 이미지를 로컬로 실행하여 작동하는지 테스트

docker를 사용하여 테스트를 위해 이미지를 로컬로 실행합니다.

docker run --rm -d -v $PWD/$BASE_PATH:$MODEL_BASE_PATH -p 8501:8501 \
 -e MODEL_BASE_PATH=$MODEL_BASE_PATH -e MODEL_NAME=$MODEL_NAME \
 --name="tfserving-test" docker.io/tensorflow/serving:latest
sleep 10

이미지에 라이브 및 점수 매기기 요청을 보낼 수 있는지 확인

먼저 컨테이너가 "활성"인지 확인합니다. 즉, 컨테이너 내부의 프로세스가 계속 실행되고 있습니다. 200(정상) 응답이 표시됩니다.

curl -v http://localhost:8501/v1/models/$MODEL_NAME

그런 다음, 레이블이 지정되지 않은 데이터에 대한 예측을 얻을 수 있는지 확인합니다.

curl --header "Content-Type: application/json" \
  --request POST \
  --data @$BASE_PATH/sample_request.json \
  http://localhost:8501/v1/models/$MODEL_NAME:predict

이미지 중지

이제 로컬에서 테스트했으므로 이미지를 중지합니다.

docker stop tfserving-test

Azure에 온라인 엔드포인트 배포

다음으로, Azure에 온라인 엔드포인트를 배포합니다.

엔드포인트 및 배포에 대한 YAML 파일 만들기

YAML을 사용하여 클라우드 배포를 구성할 수 있습니다. 이 예제에 대한 샘플 YAML을 살펴봅니다.

tfserving-endpoint.yml

$schema: https://azuremlsdk2.blob.core.windows.net/latest/managedOnlineEndpoint.schema.json
name: tfserving-endpoint
auth_mode: aml_token

tfserving-deployment.yml

$schema: https://azuremlschemas.azureedge.net/latest/managedOnlineDeployment.schema.json
name: tfserving-deployment
endpoint_name: tfserving-endpoint
model:
  name: tfserving-mounted
  version: {{MODEL_VERSION}}
  path: ./half_plus_two
environment_variables:
  MODEL_BASE_PATH: /var/azureml-app/azureml-models/tfserving-mounted/{{MODEL_VERSION}}
  MODEL_NAME: half_plus_two
environment:
  #name: tfserving
  #version: 1
  image: docker.io/tensorflow/serving:latest
  inference_config:
    liveness_route:
      port: 8501
      path: /v1/models/half_plus_two
    readiness_route:
      port: 8501
      path: /v1/models/half_plus_two
    scoring_route:
      port: 8501
      path: /v1/models/half_plus_two:predict
instance_type: Standard_DS3_v2
instance_count: 1

이 YAML/Python 매개 변수에서 주의해야 할 몇 가지 중요한 개념이 있습니다.

준비 경로와 활동성 경로

HTTP 서버는 livenessreadiness 모두에 대한 경로를 지정합니다. 활동성 경로는 서버가 실행 중인지 여부를 확인하는 데 사용됩니다. 준비 경로는 서버가 작업을 수행할 준비가 되었는지 여부를 확인하는 데 사용됩니다. 기계 학습 유추에서 서버는 모델을 로드하기 전에 활동성 요청에 200 정상으로 응답할 수 있습니다. 서버는 모델이 메모리에 로드된 후에만 준비 요청에 200 정상으로 응답할 수 있습니다.

활동성 및 준비 상태 프로브에 대한 자세한 내용은 Kubernetes 설명서를 검토하세요.

TF 서비스 제공은 활동성 경로만 정의하므로 이 배포는 활동성 및 준비 모두에 동일한 경로를 사용합니다.

탑재된 모델 찾기

온라인 엔드포인트로 모델을 배포하는 경우 Azure Machine Learning은 모델을 엔드포인트로 탑재합니다. 모델 탑재를 사용하면 새 Docker 이미지를 만들지 않고도 새 버전의 모델을 배포할 수 있습니다. 기본적으로 foo 및 버전 1이라는 이름으로 등록된 모델은 배포된 컨테이너 내의 다음 경로에 있습니다. /var/azureml-app/azureml-models/foo/1

예를 들어 로컬 머신에 /azureml-examples/cli/endpoints/online/custom-container의 디렉터리 구조가 있는 경우 모델의 이름은 half_plus_two입니다.

Diagram showing a tree view of the local directory structure.

tfserving-deployment.yml은 다음을 포함합니다.

model:
    name: tfserving-mounted
    version: 1
    path: ./half_plus_two

그러면 배포의 /var/azureml-app/azureml-models/tfserving-deployment/1 아래에 모델이 배치됩니다.

Diagram showing a tree view of the deployment directory structure.

필요에 따라 model_mount_path를 구성할 수 있습니다. 모델을 탑재하는 경로를 변경할 수 있습니다.

Important

model_mount_path는 Linux(컨테이너 이미지의 OS)에서 유효한 절대 경로여야 합니다.

예를 들어 tfserving-deployment.ymlmodel_mount_path 매개 변수가 있을 수 있습니다.

name: tfserving-deployment
endpoint_name: tfserving-endpoint
model:
  name: tfserving-mounted
  version: 1
  path: ./half_plus_two
model_mount_path: /var/tfserving-model-mount
.....

그러면 배포의 /var/tfserving-model-mount/tfserving-deployment/1에 모델이 배치됩니다. 더 이상 azureml-app/azureml-models 아래가 아니라 지정한 탑재 경로 아래에 있습니다.

Diagram showing a tree view of the deployment directory structure when using mount_model_path.

엔드포인트 및 배포 만들기

이제 YAML이 어떻게 생성되었는지 이해했으므로 엔드포인트를 만듭니다.

az ml online-endpoint create --name tfserving-endpoint -f endpoints/online/custom-container/tfserving-endpoint.yml

배포를 만드는 데 몇 분 정도 걸릴 수 있습니다.

az ml online-deployment create --name tfserving-deployment -f endpoints/online/custom-container/tfserving-deployment.yml --all-traffic

엔드포인트 호출

배포가 완료되면 배포된 엔드포인트에 점수 매기기 요청을 할 수 있는지 확인합니다.

RESPONSE=$(az ml online-endpoint invoke -n $ENDPOINT_NAME --request-file $BASE_PATH/sample_request.json)

엔드포인트 삭제

이제 엔드포인트를 통해 점수를 성공적으로 매겼기 때문에 삭제할 수 있습니다.

az ml online-endpoint delete --name tfserving-endpoint

다음 단계