

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 容器映像兼容性
<a name="train-remote-decorator-container"></a>

下表显示了与 @remote 装饰器兼容的 SageMaker 训练图像列表。


| Name | Python 版本 | 映像 URI - CPU | 映像 URI - GPU | 
| --- | --- | --- | --- | 
|  Data Science  |  3.7(py37)  |  仅适用于 SageMaker Studio 经典笔记本电脑。Python SDK 在用作 SageMaker Studio Classic 笔记本内核镜像时会自动选择图片 URI。  |  仅适用于 SageMaker Studio 经典笔记本电脑。Python SDK 在用作 SageMaker Studio Classic 笔记本内核镜像时会自动选择图片 URI。  | 
|  Data Science 2.0  |  3.8(py38)  |  仅适用于 SageMaker Studio 经典笔记本电脑。Python SDK 在用作 SageMaker Studio Classic 笔记本内核镜像时会自动选择图片 URI。  |  仅适用于 SageMaker Studio 经典笔记本电脑。Python SDK 在用作 SageMaker Studio Classic 笔记本内核镜像时会自动选择图片 URI。  | 
|  Data Science 3.0  |  3.10(py310)  |  仅适用于 SageMaker Studio 经典笔记本电脑。Python SDK 在用作 SageMaker Studio Classic 笔记本内核镜像时会自动选择图片 URI。  |  仅适用于 SageMaker Studio 经典笔记本电脑。Python SDK 在用作 SageMaker Studio Classic 笔记本内核镜像时会自动选择图片 URI。  | 
|  Base Python 2.0  |  3.8(py38)  |  当 Python SDK 检测到开发环境正在使用 Python 3.8 运行时系统时，它会选择此映像。否则 Python SDK 在用作 SageMaker Studio Classic 笔记本内核镜像时会自动选择  |  仅适用于 SageMaker Studio 经典笔记本电脑。Python SDK 在用作 SageMaker Studio Classic 笔记本内核镜像时会自动选择图片 URI。  | 
|  Base Python 3.0  |  3.10(py310)  |  当 Python SDK 检测到开发环境正在使用 Python 3.8 运行时系统时，它会选择此映像。否则 Python SDK 在用作 SageMaker Studio Classic 笔记本内核镜像时会自动选择  |  仅适用于 SageMaker Studio 经典笔记本电脑。在用作 Studio Classic 笔记本内核映像时，Python SDK 会自动选择映像 URI。  | 
|  DLC-TensorFlow 2.12.0 用于训练 SageMaker   |  3.10(py310)  |  763104351884.dkr.ecr.<区域>.amazonaws.com/tensorflow-training:2.12.0-cpu-py310-ubuntu20.04-sagemaker  |  763104351884.dkr.ecr.<区域>.amazonaws.com/tensorflow-training:2.12.0-gpu-py310-cu118-ubuntu20.04-sagemaker  | 
|  用于训练的 DLC-TensorFlow 2.11.0 SageMaker   |  3.9(py39)  |  763104351884.dkr.ecr.<区域>.amazonaws.com/tensorflow-training:2.11.0-cpu-py39-ubuntu20.04-sagemaker  |  763104351884.dkr.ecr.<区域>.amazonaws.com/tensorflow-training:2.11.0-gpu-py39-cu112-ubuntu20.04-sagemaker  | 
|  DLC-TensorFlow 2.10.1 用于训练 SageMaker   |  3.9(py39)  |  763104351884.dkr.ecr.<区域>.amazonaws.com/tensorflow-training:2.10.1-cpu-py39-ubuntu20.04-sagemaker  |  763104351884.dkr.ecr.<区域>.amazonaws.com/tensorflow-training:2.10.1-gpu-py39-cu112-ubuntu20.04-sagemaker  | 
|  DLC-TensorFlow 2.9.2 用于训练 SageMaker   |  3.9(py39)  |  763104351884.dkr.ecr.<区域>.amazonaws.com/tensorflow-training:2.9.2-cpu-py39-ubuntu20.04-sagemaker  |  763104351884.dkr.ecr.<区域>.amazonaws.com/tensorflow-training:2.9.2-gpu-py39-cu112-ubuntu20.04-sagemaker  | 
|  DLC-TensorFlow 2.8.3 用于训练 SageMaker   |  3.9(py39)  |  763104351884.dkr.ecr.<区域>.amazonaws.com/tensorflow-training:2.8.3-cpu-py39-ubuntu20.04-sagemaker  |  763104351884.dkr.ecr.<区域>.amazonaws.com/tensorflow-training:2.8.3-gpu-py39-cu112-ubuntu20.04-sagemaker  | 
|  DLC-PyTorch 2.0.0 用于训练 SageMaker   |  3.10(py310)  |  763104351884.dkr.ecr.<区域>.amazonaws.com/pytorch-training:2.0.0-cpu-py310-ubuntu20.04-sagemaker  |  763104351884.dkr.ecr.<区域>.amazonaws.com/pytorch-training:2.0.0-gpu-py310-cu118-ubuntu20.04-sagemaker  | 
|  DLC-PyTorch 1.13.1 用于训练 SageMaker   |  3.9(py39)  |  763104351884.dkr.ecr.<区域>.amazonaws.com/pytorch-training:1.13.1-cpu-py39-ubuntu20.04-sagemaker  |  763104351884.dkr.ecr.<区域>.amazonaws.com/pytorch-training:1.13.1-gpu-py39-cu117-ubuntu20.04-sagemaker  | 
|  DLC-PyTorch 1.12.1 用于训练 SageMaker   |  3.8(py38)  |  763104351884.dkr.ecr.<区域>.amazonaws.com/pytorch-training:1.12.1-cpu-py38-ubuntu20.04-sagemaker  |  763104351884.dkr.ecr.<区域>.amazonaws.com/pytorch-training:1.12.1-gpu-py38-cu113-ubuntu20.04-sagemaker  | 
|  DLC-PyTorch 1.11.0 用于训练 SageMaker   |  3.8(py38)  |  763104351884.dkr.ecr.<区域>.amazonaws.com/pytorch-training:1.11.0-cpu-py38-ubuntu20.04-sagemaker  |  763104351884.dkr.ecr.<区域>.amazonaws.com/pytorch-training:1.11.0-gpu-py38-cu113-ubuntu20.04-sagemaker  | 
|  DLC-MXNet 1.9.0 用于训练 SageMaker   |  3.8(py38)  |  763104351884.dkr.ecr.<区域>.amazonaws.com/mxnet-training:1.9.0-cpu-py38-ubuntu20.04-sagemaker  |  763104351884.dkr.ecr.<区域>.amazonaws.com/mxnet-training:1.9.0-gpu-py38-cu112-ubuntu20.04-sagemaker  | 

**注意**  
要使用 Dee AWS p Learning Containers (DLC) 图像在本地运行作业，请使用 [DLC](https://github.com/aws/deep-learning-containers/blob/master/available_images.md) 文档 URIs 中的图像。DLC 映像不支持依赖项的 `auto_capture` 值。  
[在 SageMaker Studio 中使用 SageMaker AI 分布](https://github.com/aws/sagemaker-distribution#amazon-sagemaker-studio)的作业以名`sagemaker-user`为的非 root 用户身份在容器中运行。此用户需要完全权限才能访问 `/opt/ml` 和 `/tmp`。通过将 `sudo chmod -R 777 /opt/ml /tmp` 添加到 `pre_execution_commands` 列表来授予此权限，如以下代码片段所示：  

```
@remote(pre_execution_commands=["sudo chmod -R 777 /opt/ml /tmp"])
def func():
    pass
```

您还可以使用自定义映像运行 Remote 函数。为了与 Remote 函数兼容，应使用 Python 版本 3.7.x-3.10.x 构建自定义映像。以下是一个最小 Dockerfile 示例，说明了如何将 Docker 映像用于 Python 3.10。

```
FROM python:3.10

#... Rest of the Dockerfile
```

要在映像中创建 `conda` 环境并使用它来运行作业，请将环境变量设置 `SAGEMAKER_JOB_CONDA_ENV` 设置为 `conda` 环境名称。如果您的映像设置了 `SAGEMAKER_JOB_CONDA_ENV` 值，则 Remote 函数无法在训练作业运行期间创建新的 conda 环境。请参阅以下 Dockerfile 示例，该示例将 `conda` 环境用于 Python 版本 3.10。

```
FROM continuumio/miniconda3:4.12.0  

ENV SHELL=/bin/bash \
    CONDA_DIR=/opt/conda \
    SAGEMAKER_JOB_CONDA_ENV=sagemaker-job-env

RUN conda create -n $SAGEMAKER_JOB_CONDA_ENV \
   && conda install -n $SAGEMAKER_JOB_CONDA_ENV python=3.10 -y \
   && conda clean --all -f -y \
```

要让 SageMaker AI 使用 [mamba](https://mamba.readthedocs.io/en/latest/user_guide/mamba.html) 在容器镜像中管理 Python 虚拟环境，请安装 miniforge 的 [mamba 工具包](https://github.com/conda-forge/miniforge)。要使用 mamba，请将以下代码示例添加到 Dockerfile 中。然后， SageMaker AI 将在运行时检测`mamba`可用性并使用它来代替`conda`。

```
#Mamba Installation
RUN curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-Linux-x86_64.sh" \
    && bash Mambaforge-Linux-x86_64.sh -b -p "/opt/conda"  \
    && /opt/conda/bin/conda init bash
```

在使用 Remote 函数时，在 Amazon S3 存储桶上使用自定义 conda 通道会与 mamba 不兼容。如果您选择使用 mamba，请确保您未在 Amazon S3 上使用自定义 conda 通道。有关更多信息，请参阅**使用 Amazon S3 的自定义 conda 存储库**下的**先决条件**部分。

以下是一个完整的 Dockerfile 示例，该实例说明了如何创建兼容的 Docker 映像。

```
FROM python:3.10

RUN apt-get update -y \
    # Needed for awscli to work
    # See: https://github.com/aws/aws-cli/issues/1957#issuecomment-687455928
    && apt-get install -y groff unzip curl \
    && pip install --upgrade \
        'boto3>1.0<2' \
        'awscli>1.0<2' \
        'ipykernel>6.0.0<7.0.0' \
#Use ipykernel with --sys-prefix flag, so that the absolute path to 
    #/usr/local/share/jupyter/kernels/python3/kernel.json python is used
    # in kernelspec.json file
    && python -m ipykernel install --sys-prefix

#Install Mamba
RUN curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-Linux-x86_64.sh" \
    && bash Mambaforge-Linux-x86_64.sh -b -p "/opt/conda"  \
    && /opt/conda/bin/conda init bash

#cleanup
RUN apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf ${HOME}/.cache/pip \
    && rm Mambaforge-Linux-x86_64.sh

ENV SHELL=/bin/bash \
    PATH=$PATH:/opt/conda/bin
```

 运行前面的 Dockerfile 示例生成的镜像也可以用[作 SageMaker Studio Classic 内核](https://docs.aws.amazon.com/sagemaker/latest/dg/studio-byoi.html)镜像。