分散式 GPU 訓練指南 (SDK v2)

適用於Python SDK azure-ai-ml v2 (目前)

深入瞭解如何在 Azure 機器學習 中使用分散式 GPU 定型程式代碼。 本文可協助您執行現有的分散式定型程序代碼,並提供每個架構要遵循的秘訣和範例:

  • 訊息傳遞介面 (MPI)
    • Horovod
    • Open MPI 的環境變數
  • PyTorch
  • TensorFlow
  • 使用 InfiniBand 加速 GPU 訓練

必要條件

檢閱分散式 GPU 定型的基本概念,例如數據平行處理原則分散式數據平行處理原則,以及模型平行處理原則

提示

如果您不知道要使用哪種類型的平行處理原則,則應該使用 分散式數據平行處理原則的時間超過 90%。

Mpi

Azure 機器學習 提供 MPI 作業,以在每個節點中啟動指定數目的程式。 Azure 機器學習 會在幕後建構完整的 MPI 啟動命令 (mpirun)。 您無法提供自己的完整前端節點啟動器命令,例如 mpirunDeepSpeed launcher

提示

Azure 機器學習 MPI 作業所使用的基底 Docker 映射必須安裝 MPI 連結庫。 所有 Azure 機器學習 GPU 基底映像都包含 Open MPI。 當您使用自訂 Docker 映射時,您必須負責確定映像包含 MPI 連結庫。 建議使用 Open MPI,但您也可以使用不同的 MPI 實作,例如 Intel MPI。 Azure 機器學習 也為熱門架構提供策劃的環境

若要使用 MPI 執行分散式定型,請遵循下列步驟:

  1. 搭配慣用的深度學習架構和 MPI 使用 Azure 機器學習 環境。 Azure 機器學習 為熱門架構提供策展環境。 或者 ,使用慣用的深度學習架構和 MPI 建立自定義環境
  2. command使用instance_count定義 。 instance_count 如果使用者文本負責啟動每個節點的進程,則應該等於每個節點的 GPU 數目,或針對每個節點啟動進程設定為 1(預設值)。
  3. distribution使用的 command 參數來指定 的MpiDistribution設定。
from azure.ai.ml import command, MpiDistribution

job = command(
    code="./src",  # local path where the code is stored
    command="python train.py --epochs ${{inputs.epochs}}",
    inputs={"epochs": 1},
    environment="AzureML-tensorflow-2.12-cuda11@latest",
    compute="gpu-cluster",
    instance_count=2,
    distribution=MpiDistribution(process_count_per_instance=2),
    display_name="tensorflow-mnist-distributed-horovod-example"
    # experiment_name: tensorflow-mnist-distributed-horovod-example
    # description: Train a basic neural network with TensorFlow on the MNIST dataset, distributed via Horovod.
)

Horovod

當您使用 Horovod 搭配深度學習架構進行分散式定型時,請使用 MPI 作業組態。

請確定您的程式代碼遵循下列秘訣:

  • 在新增 Azure 機器學習元件之前,訓練程式代碼會使用 Horovod 正確檢測。
  • 您的 Azure 機器學習 環境包含 Horovod 和 MPI。 PyTorch 和 TensorFlow 策劃的 GPU 環境已預先設定 Horovod 及其相依性。
  • command使用您要的散發來建立 。

Horovod 範例

  • 如需執行 Horovod 範例的完整筆記本,請參閱 azureml-examples:使用 Horovod 在 MNIST 數據集上使用分散式 MPI 定型基本類神經網路。

Open MPI 的環境變數

使用 Open MPI 映像執行 MPI 作業時,您可以針對每個啟動的進程使用下列環境變數:

  1. OMPI_COMM_WORLD_RANK:進程的排名
  2. OMPI_COMM_WORLD_SIZE:世界大小
  3. AZ_BATCH_MASTER_NODE:具有埠的主要位址, MASTER_ADDR:MASTER_PORT
  4. OMPI_COMM_WORLD_LOCAL_RANK:節點上進程的本機排名
  5. OMPI_COMM_WORLD_LOCAL_SIZE:節點上的進程數目

提示

儘管名稱相同,但環境變數 OMPI_COMM_WORLD_NODE_RANK 不會對應至 NODE_RANK。 若要使用每個節點啟動器,請設定 process_count_per_node=1 並使用 OMPI_COMM_WORLD_RANK 作為 NODE_RANK

PyTorch

Azure 機器學習 支援使用 PyTorch 的原生分散式定型功能來執行分散式作業(torch.distributed)。

提示

針對數據平行處理原則, 官方 PyTorch 指引 是針對單一節點和多節點分散式定型使用透過 DataParallel 的 DistributedDataParallel (DDP)。 PyTorch 也建議在 多重處理套件上使用 DistributedDataParallel。 因此,Azure 機器學習 檔和範例著重於 DistributedDataParallel 訓練。

進程群組初始化

任何分散式定型的骨幹都是以一組彼此瞭解且可以使用後端彼此通訊的程序為基礎。 針對 PyTorch,進程群組是藉由在所有分散式進程中呼叫torch.distributed.init_process_group來共同形成進程群組來建立。

torch.distributed.init_process_group(backend='nccl', init_method='env://', ...)

最常使用的通訊後端是 mpincclgloo。 針對 GPU 型定型, nccl 建議獲得最佳效能,並應盡可能使用。

init_method 說明每個進程如何探索彼此、如何使用通訊後端初始化和驗證進程群組。 根據預設,如果未 init_method 指定 ,PyTorch 會使用環境變數初始化方法 (env://)。 init_method是訓練程式代碼中用來在 Azure 機器學習 上執行分散式 PyTorch 的建議初始化方法。 PyTorch 會尋找下列環境變數以進行初始化:

  • MASTER_ADDR:裝載排名為 0 之進程的機器 IP 位址
  • MASTER_PORT:裝載排名為 0 之進程的電腦上免費埠
  • WORLD_SIZE:進程總數。 應該等於用於分散式定型的裝置總數 (GPU)
  • RANK:目前進程的 (global) 排名。 可能的值為 0 到 (世界大小 - 1)

如需進程群組初始化的詳細資訊,請參閱 PyTorch 檔

許多應用程式也需要下列環境變數:

  • LOCAL_RANK:節點內進程的本機(相對)排名。 可能的值為 0 到 (節點上的進程 # - 1)。 這項資訊很有用,因為許多作業,例如數據準備,通常應該在每個節點上執行一次,通常是在 local_rank = 0 上。
  • NODE_RANK:多節點定型的節點排名。 可能的值為 0 到 (節點總數 - 1)。

您不需要使用啟動器公用程式,例如 torch.distributed.launch。 若要執行分散式 PyTorch 作業:

  1. 指定定型腳本和自變數。
  2. 建立 ,command並在 參數中distribution將 型別指定為 PyTorchprocess_count_per_instance 。 對應 process_count_per_instance 至您要為作業執行的進程總數。 process_count_per_instance 通常應該等於 # of GPUs per node。 如果未process_count_per_instance指定,Azure 機器學習 預設會在每個節點啟動一個進程。

Azure 機器學習 會在每個節點上設定 MASTER_ADDRMASTER_PORTWORLD_SIZENODE_RANK 環境變數,並設定進程層級RANKLOCAL_RANK環境變數。

from azure.ai.ml import command
from azure.ai.ml.entities import Data
from azure.ai.ml import Input
from azure.ai.ml import Output
from azure.ai.ml.constants import AssetTypes

# === Note on path ===
# can be can be a local path or a cloud path. AzureML supports https://`, `abfss://`, `wasbs://` and `azureml://` URIs.
# Local paths are automatically uploaded to the default datastore in the cloud.
# More details on supported paths: https://docs.microsoft.com/azure/machine-learning/how-to-read-write-data-v2#supported-paths

inputs = {
    "cifar": Input(
        type=AssetTypes.URI_FOLDER, path=returned_job.outputs.cifar.path
    ),  # path="azureml:azureml_stoic_cartoon_wgb3lgvgky_output_data_cifar:1"), #path="azureml://datastores/workspaceblobstore/paths/azureml/stoic_cartoon_wgb3lgvgky/cifar/"),
    "epoch": 10,
    "batchsize": 64,
    "workers": 2,
    "lr": 0.01,
    "momen": 0.9,
    "prtfreq": 200,
    "output": "./outputs",
}

from azure.ai.ml.entities import ResourceConfiguration

job = command(
    code="./src",  # local path where the code is stored
    command="python train.py --data-dir ${{inputs.cifar}} --epochs ${{inputs.epoch}} --batch-size ${{inputs.batchsize}} --workers ${{inputs.workers}} --learning-rate ${{inputs.lr}} --momentum ${{inputs.momen}} --print-freq ${{inputs.prtfreq}} --model-dir ${{inputs.output}}",
    inputs=inputs,
    environment="azureml:AzureML-acpt-pytorch-2.2-cuda12.1@latest",
    instance_count=2,  # In this, only 2 node cluster was created.
    distribution={
        "type": "PyTorch",
        # set process count to the number of gpus per node
        # NC6s_v3 has only 1 GPU
        "process_count_per_instance": 1,
    },
)
job.resources = ResourceConfiguration(
    instance_type="Standard_NC6s_v3", instance_count=2
)  # Serverless compute resources

Pytorch 範例

DeepSpeed

Azure 機器學習 支援 DeepSpeed 作為一流的公民,以近乎線性延展性的方式執行分散式作業:

  • 增加模型大小
  • 增加 GPU 數目

您可以使用 Pytorch 散發或 MPI 來啟用 DeepSpeed,以執行分散式定型。 Azure 機器學習 支援 DeepSpeed 啟動器來啟動分散式定型,以及自動調整以取得最佳ds設定。

您可以針對 現成可用的環境使用策劃的環境 ,並搭配最新的技術,包括 DeepSpeed、ORT、MSSCCL 和 Pytorch,以用於您的 DeepSpeed 訓練作業。

DeepSpeed 範例

  • 如需DeepSpeed定型和自動調整範例,請參閱 這些資料夾

TensorFlow

如果您在定型程式代碼中使用原生分散式 TensorFlow,例如 TensorFlow 2.x 的 tf.distribute.Strategy API,您可以使用參數或 TensorFlowDistribution 物件,透過 distribution Azure 機器學習 啟動分散式作業。

# create the command
job = command(
    code="./src",  # local path where the code is stored
    command="python main.py --epochs ${{inputs.epochs}} --model-dir ${{inputs.model_dir}}",
    inputs={"epochs": 1, "model_dir": "outputs/keras-model"},
    environment="AzureML-tensorflow-2.12-cuda11@latest",
    compute="cpu-cluster",
    instance_count=2,
    # distribution = {"type": "mpi", "process_count_per_instance": 1},
    # distribution={
    #     "type": "tensorflow",
    #     "parameter_server_count": 1,  # for legacy TensorFlow 1.x
    #     "worker_count": 2,
    #     "added_property": 7,
    # },
    # distribution = {
    #        "type": "pytorch",
    #        "process_count_per_instance": 4,
    #        "additional_prop": {"nested_prop": 3},
    #    },
    display_name="tensorflow-mnist-distributed-example"
    # experiment_name: tensorflow-mnist-distributed-example
    # description: Train a basic neural network with TensorFlow on the MNIST dataset, distributed via TensorFlow.
)

# can also set the distribution in a separate step and using the typed objects instead of a dict
job.distribution = TensorFlowDistribution(worker_count=2)

如果您的定型腳本使用參數伺服器策略進行分散式定型,例如舊版 TensorFlow 1.x,您也需要在 的 參數commanddistribution指定要在作業中使用的參數伺服器數目。 在上述的 中,例如 "parameter_server_count" : 1"worker_count": 2

TF_CONFIG

在 TensorFlow 中 TF_CONFIG ,需要環境變數才能在多部計算機上定型。 對於 TensorFlow 作業,Azure 機器學習 會在執行定型腳本之前,先針對每個背景工作角色設定及TF_CONFIG設定適當的變數。

如果您需要:os.environ['TF_CONFIG'],您可以從定型腳本存取TF_CONFIG

主要背景工作節點上設定的範例 TF_CONFIG

TF_CONFIG='{
    "cluster": {
        "worker": ["host0:2222", "host1:2222"]
    },
    "task": {"type": "worker", "index": 0},
    "environment": "cloud"
}'

TensorFlow 範例

使用 InfiniBand 加速分散式 GPU 定型

隨著模型定型的 VM 數目增加,定型該模型所需的時間應該會減少。 在理想情況下,時間的減少應該與定型 VM 數目成線性比例。 例如,如果在一個 VM 上定型模型需要 100 秒,則最好在兩部 VM 上將相同的模型定型 50 秒。 在四部 VM 上定型模型應該需要 25 秒,依此等。

InfiniBand 可以是達到此線性調整的重要因素。 InfiniBand 可在叢集中的節點之間啟用低延遲的 GPU 對 GPU 通訊。 InfiniBand 需要特殊硬體才能運作。 某些 Azure VM 系列,特別是 NC、ND 和 H 系列,現在具有支援 SR-IOV 和 InfiniBand 的 RDMA VM。 這些 VM 會透過低延遲和高頻寬的 InfiniBand 網路進行通訊,這比乙太網路型連線更有效能。 適用於 InfiniBand 的 SR-IOV 可針對任何 MPI 連結庫啟用近乎裸機的效能(許多分散式訓練架構和工具都使用 MPI,包括 NVIDIA 的 NCCL 軟體。這些 SKU 旨在滿足需要大量計算的 GPU 加速機器學習工作負載。 如需詳細資訊,請參閱使用 SR-IOV 加速 Azure 機器學習 中的分散式定型。

一般而言,名稱中有 “r” 的 VM SKU 包含必要的 InfiniBand 硬體,而沒有 “r” 的 SKU 通常不會。 (“r” 是 RDMA 的參考,代表 遠端直接記憶體存取。例如,VM SKU Standard_NC24rs_v3 已啟用 InfiniBand,但 SKU Standard_NC24s_v3 並未啟用。 除了 InfiniBand 功能之外,這兩個 SKU 之間的規格大致相同。 兩者都有 24 個核心、448 GB RAM、4 個相同 SKU 的 GPU 等等。 深入瞭解已啟用 RDMA 和 InfiniBand 的機器 SKU

警告

舊版機器 SKU Standard_NC24r 已啟用 RDMA,但不包含 InfiniBand 所需的 SR-IOV 硬體。

如果您建立其中一個 AmlCompute 支援 InfiniBand 功能的 RDMA 叢集,則 OS 映射隨附 Mellanox OFED 驅動程式,以啟用 InfiniBand 預安裝並預先設定。

下一步