Help us improve the AWS re:Post Knowledge Center by sharing your feedback in a brief survey. Your input can influence how we create and update our content to better support your AWS journey.
如何對 SageMaker AI 處理作業中的演算法錯誤進行疑難排解?
我想對在執行 Amazon SageMaker AI 處理作業時收到的演算法錯誤進行疑難排解。
簡短描述
執行 SageMaker AI 處理作業時,您可能會收到 "Failure reason AlgorithmError: , exit code: 1" (「失敗原因 AlgorithmError:,結束代碼: 1」) 錯誤訊息,原因如下:
- 存在 Python 相依性問題。
- 發生 Python 執行時期錯誤,並且您收到非零結束代碼。
- 您正在使用為不同 CPU 架構建置的 Docker 容器。
解決方法
**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤訊息,請參閱對 AWS CLI 錯誤進行疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本。
確定錯誤的原因
若要確定錯誤的原因,請使用 Amazon CloudWatch Logs 查看堆疊追蹤日誌。堆疊追蹤日誌會顯示導致程式碼失敗的原因,以及處理指令碼中發生失敗的時間。
確定錯誤原因後,請手動從終端執行容器。如果您沒有安裝 Docker,請使用 SageMaker AI 筆記本執行個體來執行容器。請務必使用適當的執行個體類型。
將容器提取到本機環境
如果您使用 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。在 Troubleshoot algorithm errors (對演算法錯誤進行疑難排解) 區段中,更新 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_NAME 和 SAGEMAKER_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 替換為local 或 local-gpu,以測試方式執行該作業。
- 語言
- 中文 (繁體)

相關內容
- 已提問 2 年前
- 已提問 2 年前
- 已提問 1 年前