步驟 2:使用開發套件啟動訓練 Job SageMaker - Amazon SageMaker

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

步驟 2:使用開發套件啟動訓練 Job SageMaker

SageMaker Python SDK 支援使用 ML 架構 (例如 TensorFlow 和) 模型進行受管理的訓練 PyTorch。若要使用其中一個架構啟動訓練工作,您可以定義估算器、 SageMaker TensorFlow 估算器或一 SageMaker 般 SageMaker PyTorch 計器,以使用修改過的訓練指令碼和模型平行程度組態。

使用 SageMaker TensorFlow 和 PyTorch 估算器

TensorFlow 和 PyTorch 估算器類別包含distribution參數,您可以使用這個參數來指定使用分散式訓練架構的組態參數。 SageMaker 模型 parallel 程式庫內部使用 MPI 來處理混合式資料和模型平行處理原則,因此您必須搭配程式庫使用 MPI 選項。

以下 TensorFlow PyTorch 或估計器範本顯示如何配置distribution參數,以便將 SageMaker 模型 parallel 庫與 MPI 搭配使用。

Using the SageMaker TensorFlow estimator
import sagemaker from sagemaker.tensorflow import TensorFlow smp_options = { "enabled":True, # Required "parameters": { "partitions": 2, # Required "microbatches": 4, "placement_strategy": "spread", "pipeline": "interleaved", "optimize": "speed", "horovod": True, # Use this for hybrid model and data parallelism } } mpi_options = { "enabled" : True, # Required "processes_per_host" : 8, # Required # "custom_mpi_options" : "--mca btl_vader_single_copy_mechanism none" } smd_mp_estimator = TensorFlow( entry_point="your_training_script.py", # Specify your train script source_dir="location_to_your_script", role=sagemaker.get_execution_role(), instance_count=1, instance_type='ml.p3.16xlarge', framework_version='2.6.3', py_version='py38', distribution={ "smdistributed": {"modelparallel": smp_options}, "mpi": mpi_options }, base_job_name="SMD-MP-demo", ) smd_mp_estimator.fit('s3://my_bucket/my_training_data/')
Using the SageMaker PyTorch estimator
import sagemaker from sagemaker.pytorch import PyTorch smp_options = { "enabled":True, "parameters": { # Required "pipeline_parallel_degree": 2, # Required "microbatches": 4, "placement_strategy": "spread", "pipeline": "interleaved", "optimize": "speed", "ddp": True, } } mpi_options = { "enabled" : True, # Required "processes_per_host" : 8, # Required # "custom_mpi_options" : "--mca btl_vader_single_copy_mechanism none" } smd_mp_estimator = PyTorch( entry_point="your_training_script.py", # Specify your train script source_dir="location_to_your_script", role=sagemaker.get_execution_role(), instance_count=1, instance_type='ml.p3.16xlarge', framework_version='1.13.1', py_version='py38', distribution={ "smdistributed": {"modelparallel": smp_options}, "mpi": mpi_options }, base_job_name="SMD-MP-demo", ) smd_mp_estimator.fit('s3://my_bucket/my_training_data/')

若要啟用程式庫,您需要透過 SageMaker 估算器建構distribution函式的引數將組態字典傳遞至"smdistributed"和索引"mpi"鍵。

SageMaker 模型平行處理原則的組態參數
  • 若為 "smdistributed" 金鑰,利用 "modelparallel" 金鑰來傳遞字典及下列內部字典。

    注意

    系統不支援在單一訓練任務中使用 "modelparallel""dataparallel"

  • 若為 "mpi" 金鑰,請傳遞包含以下內容的字典:

    • "enabled" - 必要。設定 True 以透過 MPI 啟動分散式訓練任務。

    • "processes_per_host" - 必要。指定應在每台主機上啟動 MPI 的處理數目。在中 SageMaker,主機是單一的 Amazon EC2 ML 執行個體。 SageMaker Python SDK 會維護跨模型和資料平行處理原則的處理序與 GPU 之間的 one-to-one 對應。這 SageMaker 表示每個處理序會在單一個獨立的 GPU 上排程,而且 GPU 不包含多個處理序。如果您正在使用 PyTorch,則必須通過將每個進程限制為其自己的設備torch.cuda.set_device(smp.local_rank())。如需進一步了解,請參閱自動拆分 PyTorch

      重要

      process_per_host 不得超過每個執行個體的 GPU 數目,且通常會等於每個執行個體的 GPU 數目。

    • "custom_mpi_options" (選用) — 使用此金鑰以傳遞您可能需要的任何自訂 MPI 選項。如果您未傳遞任何 MPI 自訂選項至金鑰,MPI 選項預設會設為下列標記。

      --mca btl_vader_single_copy_mechanism none
      注意

      您不需要將此預設標記明確指定給金鑰。如果您明確指定,您的分散式模型平行訓練任務可能會失敗,並出現下列錯誤:

      The following MCA parameter has been listed multiple times on the command line: MCA param: btl_vader_single_copy_mechanism MCA parameters can only be listed once on a command line to ensure there is no ambiguity as to its value. Please correct the situation and try again.
      提示

      如果您使用支援 EFA 的執行個體類型 (例如 ml.p4d.24xlargeml.p3dn.24xlarge ) 啟動訓練任務,請使用下列標記來獲得最佳效能:

      -x FI_EFA_USE_DEVICE_RDMA=1 -x FI_PROVIDER=efa -x RDMAV_FORK_SAFE=1

若要使用估算器和 SageMaker 模型並行設定的訓練指令碼來啟動訓練工作,請執行函數estimator.fit()

請使用下列資源,進一步瞭解如何在 SageMaker Python SDK 中使用模型平行處理原則功能:

擴展包含分佈式模型並行庫 SageMaker的預構建 Docker 容器

若要擴充預先建置的容器並使用 SageMaker模型平行程式庫,您必須針 PyTorch 對或使用其中一個可用的 AWS Deep Learning Containers (DLC) 映像檔。 TensorFlow SageMaker 模型平行程式庫包含在含 CUDA TensorFlow () 的 (2.3.0 及更新版本) 和 PyTorch (1.6.0 及更新版本) DLC 影像中。cuxyz如需 DLC 映像檔的完整清單,請參閱 Deep Learning Containers GitHub 儲存庫中的可用AWS Deep Learning Contain ers 映像

提示

我們建議您使用包含 TensorFlow或最新版本的映像檔 PyTorch 來存取最多 up-to-date 版本的 SageMaker 模型平行程式庫。

例如,您的 Dockerfile 應包含類似下列的 FROM 陳述式:

# Use the SageMaker DLC image URI for TensorFlow or PyTorch FROM aws-dlc-account-id.dkr.ecr.aws-region.amazonaws.com/framework-training:{framework-version-tag} # Add your dependencies here RUN ... ENV PATH="/opt/ml/code:${PATH}" # this environment variable is used by the SageMaker container to determine our user code directory. ENV SAGEMAKER_SUBMIT_DIRECTORY /opt/ml/code

此外,當您定義 PyTorch TensorFlow 或估算器時,您必須entry_point為訓練指令碼指定。這應與 Dockerfile 中 ENV SAGEMAKER_SUBMIT_DIRECTORY 識別的路徑相同。

提示

您必須將此 Docker 容器推送到 Amazon Elastic Container Registry (Amazon ECR),並使用圖像 URI(image_uri)定義培訓 SageMaker 估算器。如需詳細資訊,請參閱 延伸預先建置的容器。

完成託管 Docker 容器並擷取容器的映像 URI 之後,建立 SageMaker PyTorch估算器物件,如下所示。此範例假設您已定義 smp_optionsmpi_options

smd_mp_estimator = Estimator( entry_point="your_training_script.py", role=sagemaker.get_execution_role(), instance_type='ml.p3.16xlarge', sagemaker_session=sagemaker_session, image_uri='your_aws_account_id.dkr.ecr.region.amazonaws.com/name:tag' instance_count=1, distribution={ "smdistributed": smp_options, "mpi": mpi_options }, base_job_name="SMD-MP-demo", ) smd_mp_estimator.fit('s3://my_bucket/my_training_data/')

使用 SageMaker 分散式模型平行程式庫建立您自己的 Docker 容器

要構建自己的 Docker 容器進行培訓並使用 SageMaker 模型 parallel 庫,您必須在 Dockerfile 中包含正確的依賴關係和 SageMaker分佈式 parallel 庫的二進製文件。本節提供您在自己的 Docker 容器中正確準備 SageMaker 訓練環境和模型 parallel 程式庫所必須包含的最小程式碼區塊集。

注意

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

使用 SageMaker 培訓工具包和模型 parallel 庫創建 Docker 文件
  1. 從其中一個 NVIDIA CUDA 基礎映像開始著手。

    FROM <cuda-cudnn-base-image>
    提示

    官方的 AWS 深度學習容器 (DLC) 映像檔是根據 NVIDIA CUDA 基礎映像檔所建立。我們建議您查看AWS 深度學習容器的官方 Dockerfiles,以找 PyTorch到您需要安裝的庫版本以及如何配置它們。官方 Dockerfiles 已完成、基準測試,並由深度學習容器服務團隊管理。 SageMaker 在提供的鏈接中,選擇您使用的 PyTorch版本,選擇 CUDA(cuxyz)文件夾,然後選擇以或結尾的 Docker 文件。.gpu .sagemaker.gpu

  2. 若要設定分散式訓練環境,您需要安裝適用於通訊和網路裝置的軟體,例如 Elastic Fabric Adapter (EFA)NVIDIA 集合通訊程式庫 (NCCL)Open MPI。根據您選擇的 PyTorch 和 CUDA 版本,您必須安裝相容版本的程式庫。

    重要

    由於 SageMaker 模型 parallel 程式庫需要後續步驟中的 SageMaker 資料 parallel 程式庫,因此我們強烈建議您遵循中的指示,使用 SageMaker 分佈式數據 parallel 庫創建自己的 Docker 容器為分散式 SageMaker 訓練正確設定訓練環境。

    如需有關使用 NCCL 和 Open MPI 設定 EFA 的詳細資訊,請參閱開始使用 EFA 和 MPI 以及開始使用 EFA 和 NCCL

  3. 新增下列引數以指定的 SageMaker 分散式訓練套件的 URL PyTorch。 SageMaker 模型 parallel 程式庫需要 SageMaker 資料 parallel 程式庫才能使用跨節點遠端直接記憶體存取 (RDMA)。

    ARG SMD_MODEL_PARALLEL_URL=https://sagemaker-distributed-model-parallel.s3.us-west-2.amazonaws.com/pytorch-1.10.0/build-artifacts/2022-02-21-19-26/smdistributed_modelparallel-1.7.0-cp38-cp38-linux_x86_64.whl ARG SMDATAPARALLEL_BINARY=https://smdataparallel.s3.amazonaws.com/binary/pytorch/1.10.2/cu113/2022-02-18/smdistributed_dataparallel-1.4.0-cp38-cp38-linux_x86_64.whl
  4. 安裝 SageMaker 模型 parallel 程式庫所需的相依性。

    1. 安裝 METIS 程式庫。

      ARG METIS=metis-5.1.0 RUN rm /etc/apt/sources.list.d/* \ && wget -nv http://glaros.dtc.umn.edu/gkhome/fetch/sw/metis/${METIS}.tar.gz \ && gunzip -f ${METIS}.tar.gz \ && tar -xvf ${METIS}.tar \ && cd ${METIS} \ && apt-get update \ && make config shared=1 \ && make install \ && cd .. \ && rm -rf ${METIS}.tar* \ && rm -rf ${METIS} \ && rm -rf /var/lib/apt/lists/* \ && apt-get clean
    2. 安裝 RAPIDS 記憶體管理員程式庫。系統須使用 CMake 3.14 或更高版本。

      ARG RMM_VERSION=0.15.0 RUN wget -nv https://github.com/rapidsai/rmm/archive/v${RMM_VERSION}.tar.gz \ && tar -xvf v${RMM_VERSION}.tar.gz \ && cd rmm-${RMM_VERSION} \ && INSTALL_PREFIX=/usr/local ./build.sh librmm \ && cd .. \ && rm -rf v${RMM_VERSION}.tar* \ && rm -rf rmm-${RMM_VERSION}
  5. 安裝 SageMaker 模型 parallel 庫。

    RUN pip install --no-cache-dir -U ${SMD_MODEL_PARALLEL_URL}
  6. 安裝 SageMaker 數據 parallel 庫。

    RUN SMDATAPARALLEL_PT=1 pip install --no-cache-dir ${SMDATAPARALLEL_BINARY}
  7. 安裝 sagemaker-training 工具組。此工具組包含建立與 SageMaker 訓練平台和 SageMaker Python SDK 相容之容器所需的一般功能。

    RUN pip install sagemaker-training
  8. 完成建立 Dockerfile 之後,請參閱調整您自己的訓練容器,了解如何建置 Docker 容器並將其託管在 Amazon ECR 中。

提示

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