AWS SageMaker TensorFlow Serving - 端点故障 - CloudWatch 日志参考:“NET_LOG:进入事件循环...”

Chi*_*ibe 5 nginx docker tensorflow tensorflow-serving amazon-sagemaker

这是我第一次使用 sagemaker 来服务我自己的自定义张量流模型,因此我一直在使用媒体文章来开始:

\n\n

如何为 AWS SageMaker 创建 TensorFlow Serving 容器
\n如何将 Docker 映像推送到 AWS ECS 存储库
\n如何使用 TensorFlow Serving 部署 AWS SageMaker 容器
\n如何使用 TensorFlow Serving 针对 SageMaker 端点进行预测

\n\n

我成功创建了我的服务容器,将其成功推送到 ECR,并从我的 docker 镜像创建了 sagemaker 模型。但是,当我尝试创建端点时,它开始创建,但 3-5 分钟后以失败消息结束:

\n\n
\n

“生产变体 Default 的主容器未通过\n ping 运行状况检查。请检查此端点的 CloudWatch 日志。”

\n
\n\n

失败图片

\n\n

然后我检查了我的云手表日志,看起来像这样......

\n\n

云观察日志

\n\n

...以“NET_LOG:进入事件循环...”结尾

\n\n

我尝试在 google 上搜索更多有关使用 tf-serving 部署 sagemaker 模型的日志消息,但找不到任何有用的解决方案。

\n\n

为了提供更多背景信息,在遇到这个问题之前,我遇到了另外两个问题:

\n\n
\n
    \n
  1. “FileSystemStoragePathSource 遇到文件系统访问错误:\n 找不到基本路径

    <MODEL_PATH>>/<MODEL_NAME>>/ 对于<MODEL_NAME>>”

  2. \n
  3. “在基本路径下找不到可用版本”
  4. \n
\n
\n\n

我设法使用以下链接解决了这两个问题:

\n\n

[文档] TensorFlowModel 端点需要export/Servo文件夹结构,但这没有记录

\n失败原因:生产变体 AllTraffic 的主容器未通过 ping 运行状况检查。

\n\n

还值得注意的是,我的 Tensorflow 模型是使用 TF 版本 2.0 创建的(因此我需要 docker 容器)。我仅使用 AWS CLI 来执行我的张量流服务,而不是 sagemaker SDK。

\n\n

以下是我的 shell 脚本的片段:

\n\n

nginx.config

\n\n
events {\n    # determines how many requests can simultaneously be served\n    # https://www.digitalocean.com/community/tutorials/how-to-optimize-nginx-configuration\n    # for more information\n    worker_connections 2048;\n}\n\nhttp {\n  server {\n    # configures the server to listen to the port 8080\n    # Amazon SageMaker sends inference requests to port 8080.\n    # For more information: https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-inference-code.html#your-algorithms-inference-code-container-response\n    listen 8080 deferred;\n\n    # redirects requests from SageMaker to TF Serving\n    location /invocations {\n      proxy_pass http://localhost:8501/v1/models/pornilarity_model:predict;\n    }\n\n    # Used by SageMaker to confirm if server is alive.\n    # https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-inference-code.html#your-algorithms-inference-algo-ping-requests\n    location /ping {\n      return 200 "OK";\n    }\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

Dockerfile

\n\n
\n# RUN pip install sagemaker-containers\n\n# Installing NGINX, used to reverse proxy the predictions from SageMaker to TF Serving\nRUN apt-get update && apt-get install -y --no-install-recommends nginx git\n\n# Copy our model folder to the container \n# NB: Tensorflow serving requires you manually assign version numbering to models e.g. model_path/1/\n# see below links: \n\n# /sf/ask/3188144991/\n# https://github.com/aws/sagemaker-python-sdk/issues/599\nCOPY pornilarity_model /opt/ml/model/export/Servo/1/\n\n# Copy NGINX configuration to the container\nCOPY nginx.conf /opt/ml/code/nginx.conf\n\n# Copies the hosting code inside the container\n# COPY serve.py /opt/ml/code/serve.py\n\n# Defines serve.py as script entrypoint\n# ENV SAGEMAKER_PROGRAM serve.py\n\n# starts NGINX and TF serving pointing to our model\nENTRYPOINT service nginx start | tensorflow_model_server --rest_api_port=8501 \\\n --model_name=pornilarity_model \\\n --model_base_path=/opt/ml/model/export/Servo/\n
Run Code Online (Sandbox Code Playgroud)\n\n

构建并推送

\n\n
%%sh\n\n# The name of our algorithm\necr_repo=sagemaker-tf-serving\ndocker_image=sagemaker-tf-serving\n\ncd container\n\n# chmod a+x container/serve.py\n\naccount=$(aws sts get-caller-identity --query Account --output text)\n\n# Get the region defined in the current configuration (default to us-west-2 if none defined)\nregion=$(aws configure get region)\nregion=${region:-eu-west-2}\n\nfullname="${account}.dkr.ecr.${region}.amazonaws.com/${ecr_repo}:latest"\n\n# If the repository doesn\'t exist in ECR, create it.\n\naws ecr describe-repositories --repository-names "${ecr_repo}" > /dev/null 2>&1\n\nif [ $? -ne 0 ]\nthen\n    aws ecr create-repository --repository-name "${ecr_repo}" > /dev/null\nfi\n\n# Get the login command from ECR and execute it directly\n$(aws ecr get-login --region ${region} --no-include-email)\n\n# Build the docker image locally with the image name and then push it to ECR\n# with the full name.\n\ndocker build -t ${docker_image} .\n# docker tag ${docker_image} ${fullname}\ndocker tag ${docker_image}:latest ${fullname}\n\ndocker push ${fullname}\n
Run Code Online (Sandbox Code Playgroud)\n\n

创建 SageMaker 模型

\n\n
#!/usr/bin/env bash\n\nCONTAINER_NAME="Pornilarity-Container"\nMODEL_NAME=pornilarity-model-v1\n\n# the role named created with\n# https://gist.github.com/mvsusp/599311cb9f4ee1091065f8206c026962\nROLE_NAME=AmazonSageMaker-ExecutionRole-20191202T133391\n\n# the name of the image created with\n# https://gist.github.com/mvsusp/07610f9cfecbec13fb2b7c77a2e843c4\nECS_IMAGE_NAME=sagemaker-tf-serving\n# the role arn of the role\nEXECUTION_ROLE_ARN=$(aws iam get-role --role-name ${ROLE_NAME} | jq -r .Role.Arn)\n\n# the ECS image URI\nECS_IMAGE_URI=$(aws ecr describe-repositories --repository-name ${ECS_IMAGE_NAME} |\\\njq -r .repositories[0].repositoryUri)\n\n# defines the SageMaker model primary container image as the ECS image\nPRIMARY_CONTAINER="ContainerHostname=${CONTAINER_NAME},Image=${ECS_IMAGE_URI}"\n\n# Createing the model\naws sagemaker create-model --model-name ${MODEL_NAME} \\\n--primary-container=${PRIMARY_CONTAINER}  --execution-role-arn ${EXECUTION_ROLE_ARN}\n
Run Code Online (Sandbox Code Playgroud)\n\n

端点配置

\n\n
#!/usr/bin/env bash\n\nMODEL_NAME=pornilarity-model-v1\n\nENDPOINT_CONFIG_NAME=pornilarity-model-v1-config\n\nENDPOINT_NAME=pornilarity-v1-endpoint\n\nPRODUCTION_VARIANTS="VariantName=Default,ModelName=${MODEL_NAME},"\\\n"InitialInstanceCount=1,InstanceType=ml.c5.large"\n\naws sagemaker create-endpoint-config --endpoint-config-name ${ENDPOINT_CONFIG_NAME} \\\n--production-variants ${PRODUCTION_VARIANTS}\n\naws sagemaker create-endpoint --endpoint-name ${ENDPOINT_NAME} \\\n--endpoint-config-name ${ENDPOINT_CONFIG_NAME}\n
Run Code Online (Sandbox Code Playgroud)\n\n

Docker 容器文件夹结构

\n\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 container\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Dockerfile\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 nginx.conf\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pornilarity_model\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 assets\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 saved_model.pb\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 variables\n\xe2\x94\x82   \xe2\x94\x82       \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 variables.data-00000-of-00002\n\xe2\x94\x82   \xe2\x94\x82       \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 variables.data-00001-of-00002\n\xe2\x94\x82   \xe2\x94\x82       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 variables.index\n
Run Code Online (Sandbox Code Playgroud)\n\n

任何指导将不胜感激!

\n

zhr*_*ist 0

您的网络服务器在路径上

<公共地址>/ping

必须暴露活性响​​应,如果一切都在运行,则返回 200。现在它丢失了,Sagemaker 不将其视为有效的推理容器。就是这么简单:)

Web 服务器上已经有容器为您执行此操作,在您的情况下,Tensorfolow 服务容器可在以下位置公开获得:

763104351884.dkr.ecr.us-east-1.amazonaws.com/tensorflow-inference:2.4.1-cpu-py37-ubuntu18.04
Run Code Online (Sandbox Code Playgroud)

这是 us-east 和 CPU 类型推理的示例。您可以像在示例 docker 中一样添加模型 /opt/ml/model/。

对于所有可用的容器,请查看AWS 的深度学习容器