跳至内容

如何解决 SageMaker AI 处理作业中的算法错误?

3 分钟阅读
0

我想解决我在运行 Amazon SageMaker AI 处理作业时收到的算法错误。

简短描述

当您运行 SageMaker AI 处理作业时,由于以下原因,您可能会收到“失败原因,AlgorithmError:,退出代码: 1"错误消息:

  • 存在 Python 依赖问题。
  • 出现 Python 运行时错误,且您收到非零退出代码。
  • 您正在使用为不同 CPU 架构构建的 Docker 容器。

解决方法

**注意:**如果您在运行 AWS 命令行界面 (AWS CLI) 命令时收到错误,请参阅 AWS CLI 错误故障排除。此外,请确保您使用的是最新版本的 AWS CLI

确定错误原因

要确定错误原因,请使用 Amazon CloudWatch Logs 查看堆栈跟踪日志。堆栈跟踪日志显示了导致代码失败的原因以及处理脚本中失败发生的时间。

确定错误原因后,从终端手动运行容器。如果您没有安装 Docker,请使用 SageMaker AI Notebook 实例来运行容器。确保使用适当的实例类型。

将容器拉取到本地环境

如果您使用 SageMaker AI 预构建的容器(例如 Scikit-learn),请登录注册表并拉取容器。要进行登录,请运行 get-login-password AWS CLI 命令:

aws ecr get-login-password --region $REGION | docker login -u AWS --password-stdin 683313688378.dkr.ecr.${REGION}.amazonaws.com

**注意:**请将 region 替换为您的 AWS 区域。

要拉取容器,请运行以下命令:

docker pull 683313688378.dkr.ecr.${REGION}.amazonaws.com/sagemaker-scikit-learn:1.2-1-cpu-py3

**注意:**如果您使用 SageMaker AI 算法(例如 Random Cut Forest),则无法将容器拉取到本地环境。对于这些容器,请联系 AWS Support

在容器中创建 Bash Shell

要对代码进行故障排除,请在本地环境中运行容器。创建一个名为 code 的文件夹,然后将您的代码添加到该文件夹。将包含 code 文件夹的目录作为卷挂载到容器中。然后,将目录输出装载到相同的位置,如以下示例中所示:

docker run \
    -v ./code:/opt/ml/processing/input/code \
    -v ./output:/opt/ml/processing/output \
    --entrypoint '/bin/bash' \
    683313688378.dkr.ecr.us-east-1.amazonaws.com/sagemaker-scikit-learn:1.2-1-cpu-py3

上述命令会在您的容器中创建一个 Bash Shell。在“对算法错误进行故障排除”部分中,更新 code 文件夹中的代码,然后使用 Bash Shell 运行处理脚本。

对算法错误进行故障排除

Python 依赖问题

如果您未指定 Python 软件包版本,则 pip 会安装可能导致算法错误的最新软件包。要解决此问题,请确定您的软件包版本,然后在处理脚本中安装软件包。

如果您使用预构建的 SageMaker 容器,请找到源代码的存储库。检索您在容器中使用的 Python 软件包和版本。或者,在 Bash Shell 中运行以下命令,以查看自定义容器的完整软件包列表:

pip freeze | grep '==' > /opt/ml/processing/input/code/requirements.txt

确定软件包版本后,在与 processing.py 脚本相同的目录中创建 requirements.txt 文件。然后,将所需的软件包添加到 requirements.txt 文件中。

使用 SageMaker AI Scikit-learn 存储库的 requirements.txt 文件示例:

# Scikit-learn 1.2-1 packages
boto3==1.28.57
botocore>=1.31.57,<1.32.0
cryptography
Flask==1.1.1
itsdangerous==2.0.1
gunicorn==20.0.4
model-archiver==1.0.3
multi-model-server==1.1.1
pandas==1.1.3
protobuf==3.20.2
psutil==5.7.2
python-dateutil==2.8.1
retrying==1.3.3
sagemaker-containers==2.8.6.post2
sagemaker-inference==1.2.0
sagemaker-training==4.8.0
scikit-learn==1.2.1
scipy==1.8.0
urllib3==1.26.17
six==1.15.0
jinja2==3.0.3
MarkupSafe==2.1.1
numpy==1.24.1
gevent==23.9.1
Werkzeug==2.0.3
setuptools
wheel
certifi

# Your packages and their versions
sagemaker==2.232.1
boto3==1.34.142
botocore>=1.34.142,<1.35.0

要安装所需的软件包,请在您的 processing.py 脚本中运行 pip 命令:

import sys
import subprocess


def install(requirements: str) -> None:
    subprocess.check_call([sys.executable, "-q", "-m", "pip", "install", "-r", requirements])


def install_requirements() -> None:
    install("/opt/ml/processing/input/code/requirements.txt")


def main() -> None:
    import sagemaker
    import pandas
    example_code


if __name__ == "__main__":
    install_requirements()
    main()

**注意:**请将 example_code 替换为您的代码。确保现有软件包不会更新。

要测试您是否向 processing.py 脚本添加了正确的软件包版本,请在 Bash Shell 中运行以下命令:

cd /opt/ml/processing/input/code
python3 process.py

如果脚本失败,请继续更新软件包版本,直到脚本成功。

对 Python 运行时问题进行故障排除

要对 Python 运行时问题进行故障排除,请修改 code 文件夹中的代码以更新 processing.py 脚本。要测试您的 processing.py 脚本,请在 Bash Shell 中运行以下命令:

cd /opt/ml/processing/input/code
python3 processing.py

修改您的代码,直到解决堆栈跟踪日志中的所有问题并收到“0”退出代码。

对 CPU 架构问题进行故障排除

您必须维护为特定 CPU 构建的容器版本。拉取现有映像,然后创建一个仅包含您的容器映像 (FROM IMAGE_URI) 的新 Dockerfile。然后,将容器推送到您现有的 Amazon Elastic Container Registry (Amazon ECR) 中。

以下示例使用了为 x86_64 构建的映像,并针对 ARM 架构重新构建了该映像:

#!/usr/bin/env bash
algorithm_name=ALGORITHM_NAME
sagemaker_account=SAGEMAKER_ACCOUNT_NUMBER
your_account=$(aws sts get-caller-identity --query Account --output text)
region=$(aws configure get region)
fullname="${your_account}.dkr.ecr.${region}.amazonaws.com/${algorithm_name}:graviton-latest"

aws ecr get-login-password --region ${region} | docker login -u AWS --password-stdin ${sagemaker_account}.dkr.ecr.${region}.amazonaws.com
docker build -t ${algorithm_name} --platform=linux/arm64 .
docker tag ${algorithm_name} ${fullname}


aws ecr describe-repositories --repository-names "${algorithm_name}" > /dev/null 2>&1

if [ $? -ne 0 ]
then
    aws ecr create-repository --repository-name "${algorithm_name}" > /dev/null
fi


aws ecr get-login-password --region ${region} | docker login --username AWS --password-stdin ${fullname}
docker push ${fullname}

**注意:**请将 ALGORITHM_NAMESAGEMAKER_ACCOUNT_NUMBER 替换为您的值。

测试您的处理作业

要测试更新后的处理作业,请运行以下命令:

import boto3
import sagemaker
from sagemaker import get_execution_role
from sagemaker.sklearn.processing import SKLearnProcessor
from sagemaker.processing import ProcessingInput, ProcessingOutput


role = get_execution_role()

local_mode = True

if local_mode:
    sklearn_processor = SKLearnProcessor(
        framework_version="1.2-1", role=role, instance_type="local", instance_count=1
    )
else:
    sklearn_processor = SKLearnProcessor(
        framework_version="1.2-1", role=role, instance_type="ml.m5.xlarge", instance_count=1
    )

sklearn_processor.run(code='code/processing.py',
                      inputs=[ProcessingInput(
                          source='./code/',
                          destination='/opt/ml/processing/input/code/')],
                      outputs=[ProcessingOutput(
                          output_name='output',
                          source='/opt/ml/processing/output/')]
                     )

**注意:**请将 instance_type 替换为 locallocal-gpu,以将作业作为测试运行。

AWS 官方已更新 10 个月前