

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

# SageMaker Python SDK で PyTorch フレームワーク推定器を使用する
<a name="data-parallel-framework-estimator"></a>

分散トレーニングを開始するには、SageMaker AI フレームワーク推定器 ([https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/sagemaker.pytorch.html#sagemaker.pytorch.estimator.PyTorch](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/sagemaker.pytorch.html#sagemaker.pytorch.estimator.PyTorch) または [https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/sagemaker.tensorflow.html#tensorflow-estimator](https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/sagemaker.tensorflow.html#tensorflow-estimator)) に `distribution` 引数を追加します。詳細については、次の選択肢から、SageMaker AI 分散データ並列処理 (SMDDP) ライブラリでサポートされているフレームワークのいずれかを選択してください。

------
#### [ PyTorch ]

PyTorch 分散トレーニングを開始するには、次のランチャーオプションを使用できます。
+ `pytorchddp` – このオプションは、`mpirun` を実行し、SageMaker AI で PyTorch 分散トレーニングを実行するために必要な環境変数を設定します。このオプションを使用するには、次のディクショナリを `distribution` パラメータに渡してください。

  ```
  { "pytorchddp": { "enabled": True } }
  ```
+ `torch_distributed` – このオプションは、`torchrun` を実行し、SageMaker AI で PyTorch 分散トレーニングを実行するために必要な環境変数を設定します。このオプションを使用するには、次のディクショナリを `distribution` パラメータに渡してください。

  ```
  { "torch_distributed": { "enabled": True } }
  ```
+ `smdistributed` – このオプションも `mpirun` を実行しますが、`smddprun` を使用して、SageMaker AI で PyTorch 分散トレーニングを実行するために必要な環境変数を設定します。

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

NCCL の `AllGather` を SMDDP の `AllGather` に置き換えることにした場合は、3 つのオプションすべてを使用できます。ユースケースに合ったオプションを選択してください。

NCCL の `AllReduce` を SMDDP の `AllReduce` に置き換えることにした場合は、`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 AI 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 Lightning と Lightning Bolts などのユーティリティライブラリは、SageMaker AI PyTorch DLC にはプリインストールされていません。次の `requirements.txt` ファイルを作成し、トレーニングスクリプトを保存するソースディレクトリに保存します。  

```
# requirements.txt
pytorch-lightning
lightning-bolts
```
例えば、ツリー構造のディレクトリは次のようになります。  

```
├── pytorch_training_launcher_jupyter_notebook.ipynb
└── sub-folder-for-your-code
    ├──  adapted-training-script.py
    └──  requirements.txt
```
トレーニングスクリプトやジョブ送信と一緒に `requirements.txt` ファイルを配置するソースディレクトリを指定する方法の詳細については、Amazon SageMaker AI Python SDK ドキュメントの「[Using third-party libraries](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/using_pytorch.html#id12)」を参照してください。

**SMDDP 集合演算を有効にし、適切な分散トレーニングランチャーオプションを使用するための考慮事項**
+ SMDDP の `AllReduce` と SMDDP の `AllGather` は、現時点では相互互換性がありません。
+ SMDDP の `AllReduce` は、`mpirun` ベースのランチャーである `smdistributed` または `pytorchddp` を使用する場合はデフォルトで有効になり、NCCL の `AllGather` が使用されます。
+ SMDDP の `AllGather` は `torch_distributed` ランチャーの使用時にデフォルトで有効になり、`AllReduce` は NCCL にフォールバックされます。
+ SMDDP の `AllGather` は、`mpirun` ベースのランチャーを使用する場合も、次のように追加の環境変数を設定することで有効化できます。

  ```
  export SMDATAPARALLEL_OPTIMIZE_SDP=true
  ```

------
#### [ TensorFlow ]

**重要**  
SMDDP ライブラリは TensorFlow のサポートを終了し、v2.11.0 より新しい TensorFlow の DLC では使用できなくなりました。SMDDP ライブラリがインストールされている以前の TensorFlow DLC を調べるには、「[TensorFlow (非推奨)](distributed-data-parallel-support.md#distributed-data-parallel-supported-frameworks-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 AI data parallel library: 
    # ml.p4d.24xlarge, ml.p3dn.24xlarge, and ml.p3.16xlarge
    instance_type="ml.p3.16xlarge",

    # Training using the SageMaker AI data parallel distributed training strategy
    distribution={ "smdistributed": { "dataparallel": { "enabled": True } } }
)

tf_estimator.fit("s3://bucket/path/to/training/data")
```

------