步驟 2:使用 SageMaker Python SDK 啟動分散式訓練工作 - Amazon SageMaker

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

步驟 2:使用 SageMaker Python SDK 啟動分散式訓練工作

若要使用已調整的指令碼執行分散式訓練工作步驟 1:調整您的訓練腳本以使用 SMDDP 集體操作,請使用 SageMaker Python SDK 的架構或一般預估器,方法是將準備好的訓練指令碼指定為入口點指令碼和分散式訓練組態。

本頁將逐步引導您如何以兩種方式使用 SageMaker Python SDK

  • 如果您想要在中快速採用分散式訓練工作 SageMaker,請設定 SageMaker PyTorchTensorFlow架構估算器類別。架構估算器會挑選您的訓練指令碼,並自動比對預先建置 PyTorch 或 TensorFlow Deep Learning Containers (DLC) 的正確映像 URI (指定給參數的指定值)。framework_version

  • 如果您想要擴充其中一個預先建置的容器,或建立自訂容器以建立自己的 ML 環境 SageMaker,請使用 SageMaker 泛型Estimator類別,並指定 Amazon 彈性容器登錄 (Amazon ECR) 中託管的自訂 Docker 容器的映像 URI。

您的訓練資料集應存放在您啟動訓練任務的 Amazon S3 或亞馬遜 FSx for Lustre AWS 區域 中。如果您使用 Jupyter 筆記本,則應該有一個 SageMaker 筆記本實例或 SageMaker Studio 經典應用程序在同一個中運行。 AWS 區域如需有關儲存訓練資料的詳細資訊,請參閱 SageMaker Python SDK 資料輸入文件。

提示

我們建議您使用 Amazon FSx for Lustre 而不是 Amazon S3 來提高培訓效能。Amazon FSx 具有比 Amazon S3 更高的輸送量和更低的延遲。

提示

若要在啟用 EFA 的執行個體類型上正確執行分散式訓練,您應該設定 VPC 的安全性群組,以允許進出安全群組本身的所有輸入和輸出流量,以啟用執行個體之間的流量。若要了解如何設定安全群組規則,請參閱 Amazon EC2 使用者指南中的步驟 1:準備啟用 EFA 的安全群組

如需有關如何執行訓練指令碼分散式訓練工作的指示,請選擇下列其中一個主題。啟動訓練任務後,您可以使用使用 Amazon SageMaker 偵錯工具偵錯並改善模型效能或 Amazon 監控系統使用率和模型效能 CloudWatch。

當您按照下列主題中的指示進一步了解技術詳細資訊時,我們也建議您嘗試開始使用Amazon SageMaker 資料平行程式庫範例

在開發套件 SageMaker中使用框架估算器

您可以將distribution引數新增至 SageMaker 架構估算器或,PyTorch以啟動分散式訓練。TensorFlow如需詳細資訊,請從下列選項中選擇 SageMaker 分散式資料平行處理原則 (SMDDP) 程式庫支援的其中一個架構。

PyTorch

下列啟動器選項可用於啟動 PyTorch 分散式訓練。

  • pytorchddp— 此選項會執行mpirun並設定執行 PyTorch 分散式訓練所需的環境變數 SageMaker。若要使用此選項,請將下列字典傳遞給distribution參數。

    { "pytorchddp": { "enabled": True } }
  • torch_distributed— 此選項會執行torchrun並設定執行 PyTorch 分散式訓練所需的環境變數 SageMaker。若要使用此選項,請將下列字典傳遞給distribution參數。

    { "torch_distributed": { "enabled": True } }
  • smdistributed— 此選項也會執行,mpirun但會設smddprun定執行 PyTorch 分散式訓練所需的環境變數 SageMaker。

    { "smdistributed": { "dataparallel": { "enabled": True } } }

如果您選擇將 NCCL 取代AllGather為 SMDDPAllGather,則可以使用全部三個選項。選擇一個適合您使用案例的選項。

如果您選擇以 SMDDP 取代 NCCL AllReduceAllReduce,您應該選擇mpirun以下其中一個選項:或。smdistributed pytorchddp您還可以添加其他 MPI 選項,如下所示。

{ "pytorchddp": { "enabled": True, "custom_mpi_options": "-verbose -x NCCL_DEBUG=VERSION" } }
{ "smdistributed": { "dataparallel": { "enabled": True, "custom_mpi_options": "-verbose -x NCCL_DEBUG=VERSION" } } }

下列程式碼範例顯示具有分散式訓練選項之 PyTorch 估算器的基本結構。

from sagemaker.pytorch import PyTorch pt_estimator = PyTorch( base_job_name="training_job_name_prefix", source_dir="subdirectory-to-your-code", entry_point="adapted-training-script.py", role="SageMakerRole", py_version="py310", framework_version="2.0.1",     # For running a multi-node distributed training job, specify a value greater than 1     # Example: 2,3,4,..8 instance_count=2,     # Instance types supported by the SageMaker data parallel library: # ml.p4d.24xlarge, ml.p4de.24xlarge instance_type="ml.p4d.24xlarge", # Activate distributed training with SMDDP distribution={ "pytorchddp": { "enabled": True } } # mpirun, activates SMDDP AllReduce OR AllGather # distribution={ "torch_distributed": { "enabled": True } } # torchrun, activates SMDDP AllGather # distribution={ "smdistributed": { "dataparallel": { "enabled": True } } } # mpirun, activates SMDDP AllReduce OR AllGather ) pt_estimator.fit("s3://bucket/path/to/training/data")
注意

PyTorch 閃電及其公用程式庫 (例如閃電螺栓) 未預先安裝在 SageMaker PyTorch DLC 中。建立下列 requirements.txt 檔案並儲存在存放訓練指令碼的來源目錄中。

# requirements.txt pytorch-lightning lightning-bolts

例如,tree-structured 目錄看起來應該如下所示。

├── pytorch_training_launcher_jupyter_notebook.ipynb └── sub-folder-for-your-code ├── adapted-training-script.py └── requirements.txt

如需有關指定要放置requirements.txt檔案的來源目錄以及訓練指令碼和任務提交的詳細資訊,請參閱 Amazon SageMaker Python SDK 文件中的使用第三方程式庫

啟動 SMDDP 集體作業和使用正確的分散式訓練啟動器選項的考量
  • SMDDP AllReduce 和 SMDDP AllGather 目前並不相互兼容。

  • 使用smdistributed或(mpirunAllReduce於啟動器)時pytorchddp,默認情況下會激活 SMDDP,並使用 N AllGather CCL。

  • 使用torch_distributed啟動器時AllGather,默認情況下會激活 SMDDP,並返回AllReduce到 NCCL。

  • 在使用mpirun基於啟動器的情況下,如下所示,還AllGather可以激活 SMDDP。

    export SMDATAPARALLEL_OPTIMIZE_SDP=true
TensorFlow
重要

SMDDP 程式庫已停止支援, TensorFlow 且在版 TensorFlow 本 2.11.0 之後的 DLC 中不再提供。若要尋找已安裝 SM TensorFlow DDP 程式庫的先前 DLC,請參閱。TensorFlow (已廢除)

from sagemaker.tensorflow import TensorFlow tf_estimator = TensorFlow( base_job_name = "training_job_name_prefix", entry_point="adapted-training-script.py", role="SageMakerRole", framework_version="2.11.0", py_version="py38",     # For running a multi-node distributed training job, specify a value greater than 1 # Example: 2,3,4,..8 instance_count=2,     # Instance types supported by the SageMaker data parallel library: # ml.p4d.24xlargeml.p3dn.24xlarge, and ml.p3.16xlarge instance_type="ml.p3.16xlarge",     # Training using the SageMaker data parallel distributed training strategy distribution={ "smdistributed": { "dataparallel": { "enabled": True } } } ) tf_estimator.fit("s3://bucket/path/to/training/data")

使用 SageMaker 通用估算器擴展預構建的容器

您可以自訂 SageMaker 預先建置的容器或擴充容器,以處理預先建置 SageMaker Docker 映像不支援的演算法或模型的任何其他功能需求。有關如何擴展預構建容器的範例,請參閱擴展預構建的容器

若要擴充預先建置的容器或調整您自己的容器以使用程式庫,您必須使用 支援的架構 中列出的其中一個映像。

注意

從 TensorFlow 2.4.1 和 PyTorch 1.8.1 開始, SageMaker 架構 DLC 支援啟用 EFA 的執行個體類型。我們建議您使用包含 TensorFlow 2.4.1 或更新版本和 PyTorch 1.8.1 或更新版本的 DLC 影像。

例如,如果您使用 PyTorch,Docker 檔案應包含類似下列內容的FROM陳述式:

# SageMaker PyTorch image FROM 763104351884.dkr.ecr.<aws-region>.amazonaws.com/pytorch-training:<image-tag> ENV PATH="/opt/ml/code:${PATH}" # this environment variable is used by the SageMaker PyTorch container to determine our user code directory. ENV SAGEMAKER_SUBMIT_DIRECTORY /opt/ml/code # /opt/ml and all subdirectories are utilized by SageMaker, use the /code subdirectory to store your user code. COPY train.py /opt/ml/code/train.py # Defines cifar10.py as script entrypoint ENV SAGEMAKER_PROGRAM train.py

您可以進一步自訂您自己的 Docker 容器,以 SageMaker 使用SageMaker 訓練工具組和 SageMaker 分散式資料 parallel 程式庫的二進位檔案來使用。如需進一步了解,請參閱下一節中的指示。

使用 SageMaker 分佈式數據 parallel 庫創建自己的 Docker 容器

要構建自己的 Docker 容器進行培訓並使用 SageMaker 數據 parallel 庫,您必須在 Dockerfile 中包含 SageMaker 分佈式 parallel 庫的正確依賴項和二進制文件。本節提供有關如何 SageMaker使用數據 parallel 庫中的分佈式培訓創建具有最小依賴關係集的完整 Dockerfile 的說明。

注意

此自訂 Docker 選項將 SageMaker 資料 parallel 程式庫做為二進位檔案庫,僅適用於 PyTorch。

使用 SageMaker 培訓工具包和數據 parallel 庫創建 Docker 文件
  1. NVIDIA CUDA 的 Docker 映像開始。使用包含 CUDA 執行階段和開發工具 (標頭和程式庫) 的 cuDNN 開發人員版本,從原始程式碼進行建置。PyTorch

    FROM nvidia/cuda:11.3.1-cudnn8-devel-ubuntu20.04
    提示

    官方的 AWS 深度學習容器 (DLC) 映像檔是根據 NVIDIA CUDA 基礎映像檔所建立。如果您想要使用預先建置的 DLC 映像檔做為參考,同時遵循其餘指示,請參閱 PyTorch Docker 檔案適用的AWS Deep Learning Contain ers。

  2. 新增下列引數以指定套件 PyTorch 和其他套件的版本。此外,請指出資 SageMaker 料 parallel 程式庫和其他軟體的 Amazon S3 儲存貯體路徑以使用 AWS 資源,例如 Amazon S3 外掛程式。

    若要使用下列程式碼範例所提供的協力廠商程式庫以外的其他版本,我們建議您查看AWS 深度學習容器的官方 Dockerfiles, PyTorch以尋找經過測試、相容且適合您應用程式的版本。

    若要尋找SMDATAPARALLEL_BINARY引數的 URL,請參閱中的查閱表格支援的架構

    ARG PYTORCH_VERSION=1.10.2 ARG PYTHON_SHORT_VERSION=3.8 ARG EFA_VERSION=1.14.1 ARG SMDATAPARALLEL_BINARY=https://smdataparallel.s3.amazonaws.com/binary/pytorch/${PYTORCH_VERSION}/cu113/2022-02-18/smdistributed_dataparallel-1.4.0-cp38-cp38-linux_x86_64.whl ARG PT_S3_WHL_GPU=https://aws-s3-plugin.s3.us-west-2.amazonaws.com/binaries/0.0.1/1c3e69e/awsio-0.0.1-cp38-cp38-manylinux1_x86_64.whl ARG CONDA_PREFIX="/opt/conda" ARG BRANCH_OFI=1.1.3-aws
  3. 設定下列環境變數,以正確建置 SageMaker 訓練元件並執行資料 parallel 程式庫。您可以在後續步驟中將這些變數用於元件。

    # Set ENV variables required to build PyTorch ENV TORCH_CUDA_ARCH_LIST="7.0+PTX 8.0" ENV TORCH_NVCC_FLAGS="-Xfatbin -compress-all" ENV NCCL_VERSION=2.10.3 # Add OpenMPI to the path. ENV PATH /opt/amazon/openmpi/bin:$PATH # Add Conda to path ENV PATH $CONDA_PREFIX/bin:$PATH # Set this enviroment variable for SageMaker to launch SMDDP correctly. ENV SAGEMAKER_TRAINING_MODULE=sagemaker_pytorch_container.training:main # Add enviroment variable for processes to be able to call fork() ENV RDMAV_FORK_SAFE=1 # Indicate the container type ENV DLC_CONTAINER_TYPE=training # Add EFA and SMDDP to LD library path ENV LD_LIBRARY_PATH="/opt/conda/lib/python${PYTHON_SHORT_VERSION}/site-packages/smdistributed/dataparallel/lib:$LD_LIBRARY_PATH" ENV LD_LIBRARY_PATH=/opt/amazon/efa/lib/:$LD_LIBRARY_PATH
  4. 在後續步驟中安裝或更新 curlwgetgit,以下載並建置套件。

    RUN --mount=type=cache,id=apt-final,target=/var/cache/apt \ apt-get update && apt-get install -y --no-install-recommends \ curl \ wget \ git \ && rm -rf /var/lib/apt/lists/*
  5. 為 Amazon EC2 網路通訊安裝 Elastic Fabric Adapter (EFA) 軟體。

    RUN DEBIAN_FRONTEND=noninteractive apt-get update RUN mkdir /tmp/efa \ && cd /tmp/efa \ && curl --silent -O https://efa-installer.amazonaws.com/aws-efa-installer-${EFA_VERSION}.tar.gz \ && tar -xf aws-efa-installer-${EFA_VERSION}.tar.gz \ && cd aws-efa-installer \ && ./efa_installer.sh -y --skip-kmod -g \ && rm -rf /tmp/efa
  6. 安裝 Conda 以處理套件管理。

    RUN curl -fsSL -v -o ~/miniconda.sh -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && \ chmod +x ~/miniconda.sh && \ ~/miniconda.sh -b -p $CONDA_PREFIX && \ rm ~/miniconda.sh && \ $CONDA_PREFIX/bin/conda install -y python=${PYTHON_SHORT_VERSION} conda-build pyyaml numpy ipython && \ $CONDA_PREFIX/bin/conda clean -ya
  7. 獲取,構建和安裝 PyTorch 及其依賴項。AWS 我們PyTorch 從源代碼構建,因為我們需要控制 NCCL 版本,以確保與 OFI NCCL 插件的兼容性。

    1. 按照PyTorch 官方 dockerfile 中的步驟,安裝構建依賴項並設置 ccache 以加快重新編譯速度。

      RUN DEBIAN_FRONTEND=noninteractive \ apt-get install -y --no-install-recommends \ build-essential \ ca-certificates \ ccache \ cmake \ git \ libjpeg-dev \ libpng-dev \ && rm -rf /var/lib/apt/lists/* # Setup ccache RUN /usr/sbin/update-ccache-symlinks RUN mkdir /opt/ccache && ccache --set-config=cache_dir=/opt/ccache
    2. 安裝PyTorch的常見和 Linux 依賴關係

      # Common dependencies for PyTorch RUN conda install astunparse numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing_extensions future six requests dataclasses # Linux specific dependency for PyTorch RUN conda install -c pytorch magma-cuda113
    3. 克隆存PyTorch GitHub儲庫

      RUN --mount=type=cache,target=/opt/ccache \ cd / \ && git clone --recursive https://github.com/pytorch/pytorch -b v${PYTORCH_VERSION}
    4. 安裝並建置特定的 NCCL 版本。若要這麼做,請以 NVIDIA 儲存庫中 PyTorch的特定 NCCL 版本取代預設 NCCL 資料夾 (/pytorch/third_party/nccl) 中的內容。NCCL 版本已在本指南的步驟 3 中設定。

      RUN cd /pytorch/third_party/nccl \ && rm -rf nccl \ && git clone https://github.com/NVIDIA/nccl.git -b v${NCCL_VERSION}-1 \ && cd nccl \ && make -j64 src.build CUDA_HOME=/usr/local/cuda NVCC_GENCODE="-gencode=arch=compute_70,code=sm_70 -gencode=arch=compute_80,code=sm_80" \ && make pkg.txz.build \ && tar -xvf build/pkg/txz/nccl_*.txz -C $CONDA_PREFIX --strip-components=1
    5. 構建和安裝 PyTorch。此過程通常需要稍微超過 1 小時才能完成。它是使用上一個步驟中所下載的 NCCL 版本。

      RUN cd /pytorch \ && CMAKE_PREFIX_PATH="$(dirname $(which conda))/../" \ python setup.py install \ && rm -rf /pytorch
  8. 建置並安裝 AWS OFI NCCL 外掛程式。這會啟用 SageMaker 資料 parallel 程式庫的 libFabric 支援。

    RUN DEBIAN_FRONTEND=noninteractive apt-get update \ && apt-get install -y --no-install-recommends \ autoconf \ automake \ libtool RUN mkdir /tmp/efa-ofi-nccl \ && cd /tmp/efa-ofi-nccl \ && git clone https://github.com/aws/aws-ofi-nccl.git -b v${BRANCH_OFI} \ && cd aws-ofi-nccl \ && ./autogen.sh \ && ./configure --with-libfabric=/opt/amazon/efa \ --with-mpi=/opt/amazon/openmpi \ --with-cuda=/usr/local/cuda \ --with-nccl=$CONDA_PREFIX \ && make \ && make install \ && rm -rf /tmp/efa-ofi-nccl
  9. 構建和安裝TorchVision

    RUN pip install --no-cache-dir -U \ packaging \ mpi4py==3.0.3 RUN cd /tmp \ && git clone https://github.com/pytorch/vision.git -b v0.9.1 \ && cd vision \ && BUILD_VERSION="0.9.1+cu111" python setup.py install \ && cd /tmp \ && rm -rf vision
  10. 安裝及設定 OpenSSH。MPI 需要 OpenSSH 才能在容器之間進行通訊。允許 OpenSSH 不必要求確認就會與容器通話。

    RUN apt-get update \ && apt-get install -y --allow-downgrades --allow-change-held-packages --no-install-recommends \ && apt-get install -y --no-install-recommends openssh-client openssh-server \ && mkdir -p /var/run/sshd \ && cat /etc/ssh/ssh_config | grep -v StrictHostKeyChecking > /etc/ssh/ssh_config.new \ && echo " StrictHostKeyChecking no" >> /etc/ssh/ssh_config.new \ && mv /etc/ssh/ssh_config.new /etc/ssh/ssh_config \ && rm -rf /var/lib/apt/lists/* # Configure OpenSSH so that nodes can communicate with each other RUN mkdir -p /var/run/sshd && \ sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd RUN rm -rf /root/.ssh/ && \ mkdir -p /root/.ssh/ && \ ssh-keygen -q -t rsa -N '' -f /root/.ssh/id_rsa && \ cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys \ && printf "Host *\n StrictHostKeyChecking no\n" >> /root/.ssh/config
  11. 安裝 PT S3 外掛程式,以有效率地存取 Amazon S3 中的資料集。

    RUN pip install --no-cache-dir -U ${PT_S3_WHL_GPU} RUN mkdir -p /etc/pki/tls/certs && cp /etc/ssl/certs/ca-certificates.crt /etc/pki/tls/certs/ca-bundle.crt
  12. 安裝 libboost 程式庫。此軟件包是用於聯網 SageMaker 數據 parallel 庫的異步 IO 功能。

    WORKDIR / RUN wget https://sourceforge.net/projects/boost/files/boost/1.73.0/boost_1_73_0.tar.gz/download -O boost_1_73_0.tar.gz \ && tar -xzf boost_1_73_0.tar.gz \ && cd boost_1_73_0 \ && ./bootstrap.sh \ && ./b2 threading=multi --prefix=${CONDA_PREFIX} -j 64 cxxflags=-fPIC cflags=-fPIC install || true \ && cd .. \ && rm -rf boost_1_73_0.tar.gz \ && rm -rf boost_1_73_0 \ && cd ${CONDA_PREFIX}/include/boost
  13. 安裝以下 SageMaker 工具進行 PyTorch 訓練。

    WORKDIR /root RUN pip install --no-cache-dir -U \ smclarify \ "sagemaker>=2,<3" \ sagemaker-experiments==0.* \ sagemaker-pytorch-training
  14. 最後,安裝 SageMaker 數據 parallel 二進製文件和剩餘的依賴關係。

    RUN --mount=type=cache,id=apt-final,target=/var/cache/apt \ apt-get update && apt-get install -y --no-install-recommends \ jq \ libhwloc-dev \ libnuma1 \ libnuma-dev \ libssl1.1 \ libtool \ hwloc \ && rm -rf /var/lib/apt/lists/* RUN SMDATAPARALLEL_PT=1 pip install --no-cache-dir ${SMDATAPARALLEL_BINARY}
  15. 完成 Docker 檔案建立之後,請參閱調整您自己的訓練容器以了解如何建置 Docker 容器、在 Amazon ECR 中託管,以及使用 Python SDK 執行訓練任務。 SageMaker

以下範例程式碼顯示了一個完整的 Dockerfile 於合併所有先前的程式碼區塊的情況。

# This file creates a docker image with minimum dependencies to run SageMaker data parallel training FROM nvidia/cuda:11.3.1-cudnn8-devel-ubuntu20.04 # Set appropiate versions and location for components ARG PYTORCH_VERSION=1.10.2 ARG PYTHON_SHORT_VERSION=3.8 ARG EFA_VERSION=1.14.1 ARG SMDATAPARALLEL_BINARY=https://smdataparallel.s3.amazonaws.com/binary/pytorch/${PYTORCH_VERSION}/cu113/2022-02-18/smdistributed_dataparallel-1.4.0-cp38-cp38-linux_x86_64.whl ARG PT_S3_WHL_GPU=https://aws-s3-plugin.s3.us-west-2.amazonaws.com/binaries/0.0.1/1c3e69e/awsio-0.0.1-cp38-cp38-manylinux1_x86_64.whl ARG CONDA_PREFIX="/opt/conda" ARG BRANCH_OFI=1.1.3-aws # Set ENV variables required to build PyTorch ENV TORCH_CUDA_ARCH_LIST="3.7 5.0 7.0+PTX 8.0" ENV TORCH_NVCC_FLAGS="-Xfatbin -compress-all" ENV NCCL_VERSION=2.10.3 # Add OpenMPI to the path. ENV PATH /opt/amazon/openmpi/bin:$PATH # Add Conda to path ENV PATH $CONDA_PREFIX/bin:$PATH # Set this enviroment variable for SageMaker to launch SMDDP correctly. ENV SAGEMAKER_TRAINING_MODULE=sagemaker_pytorch_container.training:main # Add enviroment variable for processes to be able to call fork() ENV RDMAV_FORK_SAFE=1 # Indicate the container type ENV DLC_CONTAINER_TYPE=training # Add EFA and SMDDP to LD library path ENV LD_LIBRARY_PATH="/opt/conda/lib/python${PYTHON_SHORT_VERSION}/site-packages/smdistributed/dataparallel/lib:$LD_LIBRARY_PATH" ENV LD_LIBRARY_PATH=/opt/amazon/efa/lib/:$LD_LIBRARY_PATH # Install basic dependencies to download and build other dependencies RUN --mount=type=cache,id=apt-final,target=/var/cache/apt \ apt-get update && apt-get install -y --no-install-recommends \ curl \ wget \ git \ && rm -rf /var/lib/apt/lists/* # Install EFA. # This is required for SMDDP backend communication RUN DEBIAN_FRONTEND=noninteractive apt-get update RUN mkdir /tmp/efa \ && cd /tmp/efa \ && curl --silent -O https://efa-installer.amazonaws.com/aws-efa-installer-${EFA_VERSION}.tar.gz \ && tar -xf aws-efa-installer-${EFA_VERSION}.tar.gz \ && cd aws-efa-installer \ && ./efa_installer.sh -y --skip-kmod -g \ && rm -rf /tmp/efa # Install Conda RUN curl -fsSL -v -o ~/miniconda.sh -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && \ chmod +x ~/miniconda.sh && \ ~/miniconda.sh -b -p $CONDA_PREFIX && \ rm ~/miniconda.sh && \ $CONDA_PREFIX/bin/conda install -y python=${PYTHON_SHORT_VERSION} conda-build pyyaml numpy ipython && \ $CONDA_PREFIX/bin/conda clean -ya # Install PyTorch. # Start with dependencies listed in official PyTorch dockerfile # https://github.com/pytorch/pytorch/blob/master/Dockerfile RUN DEBIAN_FRONTEND=noninteractive \ apt-get install -y --no-install-recommends \ build-essential \ ca-certificates \ ccache \ cmake \ git \ libjpeg-dev \ libpng-dev && \ rm -rf /var/lib/apt/lists/* # Setup ccache RUN /usr/sbin/update-ccache-symlinks RUN mkdir /opt/ccache && ccache --set-config=cache_dir=/opt/ccache # Common dependencies for PyTorch RUN conda install astunparse numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing_extensions future six requests dataclasses # Linux specific dependency for PyTorch RUN conda install -c pytorch magma-cuda113 # Clone PyTorch RUN --mount=type=cache,target=/opt/ccache \ cd / \ && git clone --recursive https://github.com/pytorch/pytorch -b v${PYTORCH_VERSION} # Note that we need to use the same NCCL version for PyTorch and OFI plugin. # To enforce that, install NCCL from source before building PT and OFI plugin. # Install NCCL. # Required for building OFI plugin (OFI requires NCCL's header files and library) RUN cd /pytorch/third_party/nccl \ && rm -rf nccl \ && git clone https://github.com/NVIDIA/nccl.git -b v${NCCL_VERSION}-1 \ && cd nccl \ && make -j64 src.build CUDA_HOME=/usr/local/cuda NVCC_GENCODE="-gencode=arch=compute_70,code=sm_70 -gencode=arch=compute_80,code=sm_80" \ && make pkg.txz.build \ && tar -xvf build/pkg/txz/nccl_*.txz -C $CONDA_PREFIX --strip-components=1 # Build and install PyTorch. RUN cd /pytorch \ && CMAKE_PREFIX_PATH="$(dirname $(which conda))/../" \ python setup.py install \ && rm -rf /pytorch RUN ccache -C # Build and install OFI plugin. \ # It is required to use libfabric. RUN DEBIAN_FRONTEND=noninteractive apt-get update \ && apt-get install -y --no-install-recommends \ autoconf \ automake \ libtool RUN mkdir /tmp/efa-ofi-nccl \ && cd /tmp/efa-ofi-nccl \ && git clone https://github.com/aws/aws-ofi-nccl.git -b v${BRANCH_OFI} \ && cd aws-ofi-nccl \ && ./autogen.sh \ && ./configure --with-libfabric=/opt/amazon/efa \ --with-mpi=/opt/amazon/openmpi \ --with-cuda=/usr/local/cuda \ --with-nccl=$CONDA_PREFIX \ && make \ && make install \ && rm -rf /tmp/efa-ofi-nccl # Build and install Torchvision RUN pip install --no-cache-dir -U \ packaging \ mpi4py==3.0.3 RUN cd /tmp \ && git clone https://github.com/pytorch/vision.git -b v0.9.1 \ && cd vision \ && BUILD_VERSION="0.9.1+cu111" python setup.py install \ && cd /tmp \ && rm -rf vision # Install OpenSSH. # Required for MPI to communicate between containers, allow OpenSSH to talk to containers without asking for confirmation RUN apt-get update \ && apt-get install -y --allow-downgrades --allow-change-held-packages --no-install-recommends \ && apt-get install -y --no-install-recommends openssh-client openssh-server \ && mkdir -p /var/run/sshd \ && cat /etc/ssh/ssh_config | grep -v StrictHostKeyChecking > /etc/ssh/ssh_config.new \ && echo " StrictHostKeyChecking no" >> /etc/ssh/ssh_config.new \ && mv /etc/ssh/ssh_config.new /etc/ssh/ssh_config \ && rm -rf /var/lib/apt/lists/* # Configure OpenSSH so that nodes can communicate with each other RUN mkdir -p /var/run/sshd && \ sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd RUN rm -rf /root/.ssh/ && \ mkdir -p /root/.ssh/ && \ ssh-keygen -q -t rsa -N '' -f /root/.ssh/id_rsa && \ cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys \ && printf "Host *\n StrictHostKeyChecking no\n" >> /root/.ssh/config # Install PT S3 plugin. # Required to efficiently access datasets in Amazon S3 RUN pip install --no-cache-dir -U ${PT_S3_WHL_GPU} RUN mkdir -p /etc/pki/tls/certs && cp /etc/ssl/certs/ca-certificates.crt /etc/pki/tls/certs/ca-bundle.crt # Install libboost from source. # This package is needed for smdataparallel functionality (for networking asynchronous IO). WORKDIR / RUN wget https://sourceforge.net/projects/boost/files/boost/1.73.0/boost_1_73_0.tar.gz/download -O boost_1_73_0.tar.gz \ && tar -xzf boost_1_73_0.tar.gz \ && cd boost_1_73_0 \ && ./bootstrap.sh \ && ./b2 threading=multi --prefix=${CONDA_PREFIX} -j 64 cxxflags=-fPIC cflags=-fPIC install || true \ && cd .. \ && rm -rf boost_1_73_0.tar.gz \ && rm -rf boost_1_73_0 \ && cd ${CONDA_PREFIX}/include/boost # Install SageMaker PyTorch training. WORKDIR /root RUN pip install --no-cache-dir -U \ smclarify \ "sagemaker>=2,<3" \ sagemaker-experiments==0.* \ sagemaker-pytorch-training # Install SageMaker data parallel binary (SMDDP) # Start with dependencies RUN --mount=type=cache,id=apt-final,target=/var/cache/apt \ apt-get update && apt-get install -y --no-install-recommends \ jq \ libhwloc-dev \ libnuma1 \ libnuma-dev \ libssl1.1 \ libtool \ hwloc \ && rm -rf /var/lib/apt/lists/* # Install SMDDP RUN SMDATAPARALLEL_PT=1 pip install --no-cache-dir ${SMDATAPARALLEL_BINARY}
提示

如需有關在中建立自訂 Docker 檔案以進行訓練的更多一般資訊 SageMaker,請參閱使用您自己的訓練演算法。

提示

如果您想要延伸自訂 Docker 檔案以合併 SageMaker 模型 parallel 資料庫,請參閱。使用 SageMaker 分散式模型平行程式庫建立您自己的 Docker 容器