我正在尝试在本地 GPU 上运行 Amazon Sagemaker 的 example code。我已将 Jupyter 笔记本中的代码复制到以下 Python 脚本中:
import boto3
import subprocess
import sagemaker
from sagemaker.mxnet import MXNet
from mxnet import gluon
from sagemaker import get_execution_role
import os
sagemaker_session = sagemaker.Session()
instance_type = 'local'
if subprocess.call('nvidia-smi') == 0:
# Set type to GPU if one is present
instance_type = 'local_gpu'
# role = get_execution_role()
gluon.data.vision.MNIST('./data/train', train=True)
gluon.data.vision.MNIST('./data/test', train=False)
# successfully connects and uploads data
inputs = sagemaker_session.upload_data(path='data', key_prefix='data/mnist')
hyperparameters = {
'batch_size': 100,
'epochs': 20,
'learning_rate': 0.1,
'momentum': 0.9,
'log_interval': 100
}
m = MXNet("mnist.py",
role=role,
train_instance_count=1,
train_instance_type=instance_type,
framework_version="1.1.0",
hyperparameters=hyperparameters)
# fails in Docker container
m.fit(inputs)
predictor = m.deploy(initial_instance_count=1, instance_type=instance_type)
m.delete_endpoint()
其中引用的 mnist.py 文件与 Github 上指定的完全相同。该脚本在 Docker 容器中的 m.fit
上失败,并出现以下错误:
algo-1-1DUU4_1 | Downloading s3://<S3-BUCKET>/sagemaker-mxnet-2018-10-07-00-47-10-435/source/sourcedir.tar.gz to /tmp/script.tar.gz
algo-1-1DUU4_1 | 2018-10-07 00:47:29,219 ERROR - container_support.training - uncaught exception during training: Unable to locate credentials
algo-1-1DUU4_1 | Traceback (most recent call last):
algo-1-1DUU4_1 | File "/usr/local/lib/python2.7/dist-packages/container_support/training.py", line 36, in start
algo-1-1DUU4_1 | fw.train()
algo-1-1DUU4_1 | File "/usr/local/lib/python2.7/dist-packages/mxnet_container/train.py", line 169, in train
algo-1-1DUU4_1 | mxnet_env.download_user_module()
algo-1-1DUU4_1 | File "/usr/local/lib/python2.7/dist-packages/container_support/environment.py", line 89, in download_user_module
algo-1-1DUU4_1 | cs.download_s3_resource(self.user_script_archive, tmp)
algo-1-1DUU4_1 | File "/usr/local/lib/python2.7/dist-packages/container_support/utils.py", line 37, in download_s3_resource
algo-1-1DUU4_1 | script_bucket.download_file(script_key_name, target)
algo-1-1DUU4_1 | File "/usr/local/lib/python2.7/dist-packages/boto3/s3/inject.py", line 246, in bucket_download_file
algo-1-1DUU4_1 | ExtraArgs=ExtraArgs, Callback=Callback, Config=Config)
algo-1-1DUU4_1 | File "/usr/local/lib/python2.7/dist-packages/boto3/s3/inject.py", line 172, in download_file
algo-1-1DUU4_1 | extra_args=ExtraArgs, callback=Callback)
algo-1-1DUU4_1 | File "/usr/local/lib/python2.7/dist-packages/boto3/s3/transfer.py", line 307, in download_file
algo-1-1DUU4_1 | future.result()
algo-1-1DUU4_1 | File "/usr/local/lib/python2.7/dist-packages/s3transfer/futures.py", line 73, in result
algo-1-1DUU4_1 | return self._coordinator.result()
algo-1-1DUU4_1 | File "/usr/local/lib/python2.7/dist-packages/s3transfer/futures.py", line 233, in result
algo-1-1DUU4_1 | raise self._exception
algo-1-1DUU4_1 | NoCredentialsError: Unable to locate credentials
我很困惑,我可以在容器外部对 S3 进行身份验证(以加载训练/测试数据),但在 Docker 容器内却不能。所以我猜测问题与将 AWS 凭证传递到 Docker 容器有关。这是生成的 Docker-compose 文件:
networks:
sagemaker-local:
name: sagemaker-local
services:
algo-1-1DUU4:
command: train
environment:
- AWS_REGION=us-west-2
- TRAINING_JOB_NAME=sagemaker-mxnet-2018-10-07-00-47-10-435
image: 123456789012.dkr.ecr.us-west-2.amazonaws.com/sagemaker-mxnet:1.1.0-gpu-py2
networks:
sagemaker-local:
aliases:
- algo-1-1DUU4
stdin_open: true
tty: true
volumes:
- /tmp/tmpSkaR3x/algo-1-1DUU4/input:/opt/ml/input
- /tmp/tmpSkaR3x/algo-1-1DUU4/output:/opt/ml/output
- /tmp/tmpSkaR3x/algo-1-1DUU4/output/data:/opt/ml/output/data
- /tmp/tmpSkaR3x/model:/opt/ml/model
version: '2.1'
AWS 凭证是否应该作为环境变量传入?
在阅读 Using boto3 in install local mode? 后,我将我的 sagemaker
安装升级到了,但这没有效果。我检查了在 Sagemaker session (容器外部)中获取的凭据,它们似乎是空白的,即使我有 ~/.aws/config
和 ~/.aws/凭据
文件:
{'_token': None, '_time_fetcher': <function _local_now at 0x7f4dbbe75230>, '_access_key': None, '_frozen_credentials': None, '_refresh_using': <bound method AssumeRoleCredentialFetcher.fetch_credentials of <botocore.credentials.AssumeRoleCredentialFetcher object at 0x7f4d2de48bd0>>, '_secret_key': None, '_expiry_time': None, 'method': 'assume-role', '_refresh_lock': <thread.lock object at 0x7f4d9f2aafd0>}
我是 AWS 新手,所以我不知道如何诊断有关 AWS 凭证的问题。我的 .aws/config
文件包含以下信息(带有占位符值):
[default]
output = json
region = us-west-2
role_arn = arn:aws:iam::123456789012:role/SageMakers
source_profile = sagemaker-test
[profile sagemaker-test]
output = json
region = us-west-2
其中 sagemaker-test
配置文件在 IAM 管理控制台中具有 AmazonSageMakerFullAccess
。
.aws/credentials
文件具有以下信息(由占位符值表示):
[default]
aws_access_key_id = 1234567890
aws_secret_access_key = zyxwvutsrqponmlkjihgfedcba
[sagemaker-test]
aws_access_key_id = 0987654321
aws_secret_access_key = abcdefghijklmopqrstuvwxyz
最后,这些是来自 pip freeze
的适用库的版本:
awscli==1.16.19
boto==2.48.0
boto3==1.9.18
botocore==1.12.18
docker==3.5.0
docker-compose==1.22.0
mxnet-cu91==1.1.0.post0
sagemaker==1.11.1
如果我遗漏了任何相关信息,请告诉我,并感谢您提供的任何帮助/反馈。
更新:感谢大家的帮助!在尝试您建议的一些修复时,我注意到 boto3
已过时,并将其更新为 boto3-1.9.26
和 botocore-1.12.26
)这似乎解决了这个问题。我找不到任何有关 boto3==1.9.18
问题的文档。如果有人可以帮助我了解 boto3
的问题所在,我很乐意将他们的答案标记为正确。
最佳答案
SageMaker 本地模式旨在获取 boto3 session 中可用的任何凭据,并将它们作为环境变量传递到 docker 容器中。
但是,您正在使用的 sagemaker sdk 版本(1.11.1 及更早版本)将忽略包含 token 的凭证,因为这通常表示短期凭证的有效期不会足够长。要完成的训练作业或终点是有用的。
如果您使用临时凭证,请尝试将其替换为永久凭证,或者从分配了适当实例角色的 ec2 实例(或 SageMaker 笔记本!)运行。
此外,sagemaker sdk 对凭证的处理在 v1.11.2 及更高版本中发生了变化——临时凭证将被传递到本地模式容器,但带有警告消息。因此,您可以升级到较新的版本,然后重试(pip install -U sagemaker
)。
另外,尝试升级boto3
可能会改变,所以尝试使用最新版本。
关于python - 培训中的 AWS NoCredentials,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52684987/