

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Amazon SageMaker AI で分散トレーニングを開始する
<a name="distributed-training-get-started"></a>

以下のページでは、Amazon SageMaker AI で分散トレーニングを始めるために必要な手順について説明します。既に分散トレーニングに慣れている場合は、以下のオプションからお好みの戦略やフレームワークに合わせて 1 つを選択して始めてください。分散型トレーニング全般について知りたい場合は、[分散トレーニングの概念](distributed-training.md#distributed-training-basic-concepts) を参照してください。

SageMaker AI 分散トレーニングライブラリは SageMaker トレーニング環境向けに最適化されており、分散トレーニングジョブを SageMaker AI に適応させ、トレーニングの速度とスループットを向上させるのに役立ちます。ライブラリは、データ並列とモデル並列の両方のトレーニング戦略を提供します。ソフトウェアとハードウェアのテクノロジーを組み合わせて GPU 間およびノード間の通信を改善し、SageMaker AI のトレーニング機能は、トレーニングスクリプトにわずかなコード変更を加えるだけで済む組み込みオプションによって拡張されます。 

## 始める前に
<a name="distributed-training-before-getting-started"></a>

SageMaker Training は、単一インスタンスと複数インスタンスの分散トレーニングをサポートしているため、あらゆる規模のトレーニングを大規模に実行できます。SageMaker Python SDK の [PyTorch](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/sagemaker.pytorch.html#pytorch-estimator) や [TensorFlow](https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/sagemaker.tensorflow.html#tensorflow-estimator) などのフレームワーク推定クラスを使用することをお勧めします。これらは、さまざまな分散型トレーニングオプションを備えたトレーニングジョブランチャーです。推定器オブジェクトを作成すると、オブジェクトは分散トレーニングインフラストラクチャをセットアップし、バックエンドで `CreateTrainingJob` API を実行し、現在のセッションが実行されているリージョンを検索し、 AWS 深層学習フレームワーク、分散トレーニングフレームワーク、[EFA](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/efa.html) ドライバーなど、多数のライブラリがあらかじめパッケージ化された構築済みの深層学習コンテナの 1 つをプルします。FSx ファイルシステムをトレーニングインスタンスにマウントする場合は、VPC サブネットとセキュリティグループ ID を estimator に渡す必要があります。SageMaker AI で分散型トレーニングジョブを実行する前に、基本的なインフラストラクチャの設定に関する以下の一般的なガイダンスをお読みください。

### アベイラビリティーゾーンとネットワークバックプレーン
<a name="availability-zones"></a>

複数のインスタンス (*ノード*とも呼ばれる) を使用する場合は、インスタンスを接続するネットワーク、インスタンスがトレーニングデータを読み取る方法、インスタンス間で情報を共有する方法を理解しておくことが重要です。例えば、分散データ並列トレーニングジョブを実行する場合、コンピューティングリソースの最適な使用とトレーニング速度の高速化を実現するには、`AllReduce` オペレーションを実行する計算クラスターのノード間の通信や、Amazon Simple Storage Service または Amazon FSx for Lustre 内のノードとデータストレージ間のデータ転送など、さまざまな要因が重要な役割を果たします。通信オーバーヘッドを減らすには、インスタンス、VPC サブネット、データストレージを同じ と AWS リージョン アベイラビリティーゾーンに設定してください。

### より高速なネットワークと高スループットのストレージを備えた GPU インスタンス
<a name="optimized-GPU"></a>

技術的には、どのインスタンスでも分散トレーニングに使用できます。大規模言語モデル (LLM) や拡散モデルなど、より高速なノード間通信を必要とする大規模モデルをトレーニングするためにマルチノード分散トレーニングジョブを実行する必要がある場合は、[SageMaker AI がサポートする EFA 対応 GPU インスタンス](https://aws.amazon.com/about-aws/whats-new/2021/05/amazon-sagemaker-supports-elastic-fabric-adapter-distributed-training/)を推奨します。特に、SageMaker AI で最もパフォーマンスの高い分散型トレーニングジョブを実現するには、[NVIDIA A100 GPU を搭載した P4d および P4de インスタンス](https://aws.amazon.com/ec2/instance-types/p4/)を推奨します。これらには、高スループット、低レイテンシーのローカルインスタンスストレージ、およびより高速なノード内ネットワークも搭載されています。データストレージには、トレーニングデータセットとモデルチェックポイントを高いスループットで保存できる [Amazon FSx for Lustre](https://docs.aws.amazon.com/fsx/latest/LustreGuide/what-is.html) をお勧めします。

**SageMaker AI 分散データ並列処理ライブラリ (SMDDP) を使用する**

SMDDP ライブラリは、 AWS ネットワークインフラストラクチャ`AllReduce`と Amazon SageMaker AI ML インスタンストポロジに最適化された の実装と`AllGather`集合通信オペレーションにより、ノード間の通信を改善します。[SMDDP ライブラリは PyTorch ベースの分散トレーニングパッケージ](https://docs.aws.amazon.com/sagemaker/latest/dg/data-parallel-modify-sdp-pt.html) ([PyTorch Distributed Data Parallel (DDP)](https://pytorch.org/docs/stable/notes/ddp.html)、[PyTorch Fully Sharded Data Parallelism (FSDP)](https://pytorch.org/docs/stable/fsdp.html)、[DeepSpeed](https://github.com/microsoft/DeepSpeed)、[Megatron-DeepSpeed](https://github.com/microsoft/Megatron-DeepSpeed)) のバックエンドとして使用できます。次のコード例は、2 つの `ml.p4d.24xlarge` インスタンスで分散トレーニングジョブを開始する場合の `PyTorch` 推定器の設定方法を示しています。

```
from sagemaker.pytorch import PyTorch

estimator = PyTorch(
    ...,
    instance_count=2,
    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
)
```

トレーニングスクリプトを準備し、SageMaker AI で分散データ並列トレーニングジョブを開始する方法については、「[SageMaker AI 分散データ並列処理ライブラリを使用して分散トレーニングを実行する](data-parallel.md)」を参照してください。

**SageMaker AI モデル並列処理ライブラリ (SMP) を使用する**

SageMaker AI は SMP ライブラリを提供し、シャーディングデータ並列処理、パイプライン、テンソル並列処理、オプティマイザステートシャーディングなど、さまざまな分散トレーニング技術をサポートしています。SMP ライブラリが提供するものの詳細については、[SageMaker モデル並列処理ライブラリの主要機能](model-parallel-core-features.md) を参照してください。

SageMaker AI のモデル並列処理ライブラリを使用するには、SageMaker AI フレームワーク推定器の `distribution` パラメータを設定します。サポートされているフレームワーク推定器は [PyTorch](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/sagemaker.pytorch.html#pytorch-estimator) と [TensorFlow](https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/sagemaker.tensorflow.html#tensorflow-estimator) です。次のコード例は、2 つの `ml.p4d.24xlarge` インスタンスでモデル並列処理ライブラリを使用して分散トレーニング用のフレームワーク推定器を構築する方法を示しています。

```
from sagemaker.framework import Framework

distribution={
    "smdistributed": {
        "modelparallel": {
            "enabled":True,
            "parameters": {
                ...   # enter parameter key-value pairs here
            }
        },
    },
    "mpi": {
        "enabled" : True,
        ...           # enter parameter key-value pairs here
    }
}

estimator = Framework(
    ...,
    instance_count=2,
    instance_type="ml.p4d.24xlarge",
    distribution=distribution
)
```

トレーニングスクリプトの適合、`estimator` クラスでの分散パラメータの設定、および分散トレーニングジョブを起動する方法については、「[SageMaker AI's model parallelism library](model-parallel.md)」(「*SageMaker Python SDK ドキュメント*」の「[Distributed Training APIs」も参照](https://sagemaker.readthedocs.io/en/stable/api/training/distributed.html#the-sagemaker-distributed-model-parallel-library)) を参照してください。

**オープンソースの分散型トレーニングフレームワークを使用する**

SageMaker AI は、バックエンドでの `mpirun` と `torchrun` を操作する以下のオプションもサポートしています。
+ SageMaker AI の [PyTorch 分散データパラレル (DDP)](https://pytorch.org/docs/master/generated/torch.nn.parallel.DistributedDataParallel.html) を `mpirun` バックエンドで使用するには、PyTorch 推定器に `distribution={"pytorchddp": {"enabled": True}}` を追加してください。詳細については、*SageMaker Python SDK ドキュメント*の「[Distributed PyTorch Training](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/using_pytorch.html#distributed-pytorch-training)」と [SageMaker AI PyTorch Estimator](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/sagemaker.pytorch.html#pytorch-estimator) の `distribution` の引数も参照してください。
**注記**  
このオプションは、PyTorch 1.12.0 以降で使用できます。

  ```
  from sagemaker.pytorch import PyTorch
  
  estimator = PyTorch(
      ...,
      instance_count=2,
      instance_type="ml.p4d.24xlarge",
      distribution={"pytorchddp": {"enabled": True}}  # runs mpirun in the backend
  )
  ```
+ SageMaker AI は、P3 や P4 などの GPU ベースの Amazon EC2 インスタンスでの分散トレーニング用の [PyTorch `torchrun` ランチャーと](https://pytorch.org/docs/stable/elastic/run.html)、[AWS Trainium ](https://aws.amazon.com/machine-learning/trainium/)デバイスを搭載した Trn1 をサポートしています。

  SageMaker AI の [PyTorch 分散データパラレル (DDP)](https://pytorch.org/docs/master/generated/torch.nn.parallel.DistributedDataParallel.html) を `torchrun` バックエンドで使用するには、PyTorch 推定器に `distribution={"torch_distributed": {"enabled": True}}` を追加してください。
**注記**  
このオプションは、PyTorch 1.13.0 以降で使用できます。

  次のコードスニペットは、SageMaker AI PyTorch Estimator を構築して、`torch_distributed` 分散オプションを使用して 2 つの `ml.p4d.24xlarge` インスタンスで分散トレーニングを実行する例を示しています。

  ```
  from sagemaker.pytorch import PyTorch
  
  estimator = PyTorch(
      ...,
      instance_count=2,
      instance_type="ml.p4d.24xlarge",
      distribution={"torch_distributed": {"enabled": True}}   # runs torchrun in the backend
  )
  ```

  詳細については、*SageMaker Python SDK ドキュメント*の「[Distributed PyTorch Training](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/using_pytorch.html#distributed-pytorch-training)」と [SageMaker AI PyTorch Estimator](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/sagemaker.pytorch.html#pytorch-estimator)の `distribution` の引数も参照してください。

  **Trn1 での分散トレーニングに関する注意事項**

  Trn1 インスタンスは最大 16 台の Trainium デバイスで構成され、各 Trainium デバイスは 2 つの [NeuronCores](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/general/arch/neuron-hardware/neuroncores-arch.html#neuroncores-v2-arch) で構成されます。 AWS Trainium デバイスの仕様については、*AWS Neuron ドキュメント*の [Trainium アーキテクチャ](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/general/arch/neuron-hardware/trn1-arch.html#id2)を参照してください。

  Trainium 搭載インスタンスでトレーニングを行うには、SageMaker AI PyTorch 推定クラスの `instance_type` 引数に Trn1 インスタンスコードを `ml.trn1.*` を文字列で指定するだけで済みます。使用可能な Trn1 インスタンスタイプを見つけるには、「*AWS Neuron ドキュメント*」の「[AWS Trn1 Architecture](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/general/arch/neuron-hardware/trn1-arch.html#aws-trn1-arch)」を参照してください。
**注記**  
Amazon EC2 Trn1 インスタンスでの SageMaker Training は現在、 v1.11.0 以降の AWS Deep Learning Containers for PyTorch Neuron の PyTorch フレームワークでのみ使用できます。PyTorch Neuron のサポートされているすべてのバージョンのリストについては、AWS Deep Learning Containers GitHub リポジトリの「[Neuron Containers](https://github.com/aws/deep-learning-containers/blob/master/available_images.md#neuron-containers)」を参照してください。

  SageMaker Python SDK を使用して Trn1 インスタンスでトレーニングジョブを起動すると、SageMaker AI は AWS Deep Learning [Containers が提供する Neuron](https://github.com/aws/deep-learning-containers/blob/master/available_images.md#neuron-containers) Containers から適切なコンテナを自動的に取得して実行します。Neuron Containers には、トレーニングジョブを SageMaker トレーニングプラットフォームと Amazon EC2 Trn1 インスタンスに簡単に適応させることができるように、トレーニング環境設定と依存関係があらかじめパッケージ化されています。
**注記**  
SageMaker AI を使用して Trn1 インスタンスで PyTorch トレーニングジョブを実行するには、`xla` バックエンドでプロセスグループを初期化し、[PyTorch /XLA](https://pytorch.org/xla/release/1.12/index.html) を使用するようにトレーニングスクリプトを変更する必要があります。XLA 導入プロセスをサポートするために、 AWS Neuron SDK は XLA を使用して PyTorch オペレーションを Trainium 指示に変換する PyTorch Neuron を提供します。トレーニングスクリプトを変更する方法については、「[AWS Neuron ドキュメント](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/frameworks/torch/torch-neuronx/programming-guide/training/pytorch-neuron-programming-guide.html)」の「Developer Guide for Training with PyTorch Neuron (`torch-neuronx`)」を参照してください。

  詳細については、*SageMaker Python SDK ドキュメント*の「[Distributed Training with PyTorch Neuron on Trn1 instances](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/using_pytorch.html#id24)」と [SageMaker AI PyTorch Estimator](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/sagemaker.pytorch.html#pytorch-estimator) の `distribution` の引数も参照してください。
+ SageMaker AI で MPI を使用するには、`distribution={"mpi": {"enabled": True}}` を推定器に追加してください。MPI ディストリビューションオプションは、MXNet、PyTorch、TensorFlow の各フレームワークで使用できます。
+ SageMaker AI でパラメータサーバーを使用するには、`distribution={"parameter_server": {"enabled": True}}` を推定器に追加してください。パラメータサーバーオプションは、MXNet、PyTorch、TensorFlow の各フレームワークで使用できます。
**ヒント**  
フレームワークごとの MPI およびパラメータサーバーオプションの使用方法の詳細については、「*SageMaker Python SDK ドキュメント*」への次のリンクを参照してください。  
[MXNet Distributed Training](https://sagemaker.readthedocs.io/en/stable/frameworks/mxnet/using_mxnet.html#distributed-training) と [SageMaker AI MXNet Estimator](https://sagemaker.readthedocs.io/en/stable/frameworks/mxnet/sagemaker.mxnet.html#mxnet-estimator) の `distribution` 引数
[PyTorch Distributed Training](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/using_pytorch.html#distributed-pytorch-training) と [SageMaker AI PyTorch Estimator](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/sagemaker.pytorch.html#pytorch-estimator) の `distribution` 引数
[TensorFlow Distributed Training](https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/using_tf.html#distributed-training) と [SageMaker AI TensorFlow Estimator](https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/sagemaker.tensorflow.html#tensorflow-estimator) の `distribution` 引数