

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

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

SageMaker AI [`PyTorch`](https://sagemaker.readthedocs.io/en/stable/api/sagemaker_train.html)フレームワーク推定器に `distribution`引数を追加することで、分散トレーニングを開始できます。SageMaker AI 分散データ並列処理 (SMDDP) ライブラリは、PyTorch 分散トレーニングをサポートしています。

**注記**  
SMDDP は v2.11.0 以降に TensorFlow サポートを終了しました。TensorFlow を使用した分散トレーニングの場合は、代替の分散戦略を使用します。

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"
        }
    }
}
```

次のコードサンプルは、分散トレーニングオプションを備えた ModelTrainer の基本構造を示しています。

```
from sagemaker.train import ModelTrainer
from sagemaker.train.configs import SourceCode, Compute, InputData
from sagemaker.core import image_uris

# Retrieve the training image for the desired PyTorch version
training_image = image_uris.retrieve(
    framework="pytorch",
    region="{{us-west-2}}",
    version="{{2.0.1}}",
    py_version="{{py310}}",
    instance_type="{{ml.p4d.24xlarge}}",
    image_scope="training"
)

source_code = SourceCode(
    source_dir="{{subdirectory-to-your-code}}",
    entry_script="{{adapted-training-script.py}}"
)

compute = Compute(
    # 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}}"
)

pt_model_trainer = ModelTrainer(
    training_image=training_image,
    base_job_name="{{training_job_name_prefix}}",
    source_code=source_code,
    role="{{SageMakerRole}}",
    compute=compute,

    # 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_model_trainer.train(input_data_config=[
    InputData(channel_name="training", data_source="{{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/api/sagemaker_train.html)」を参照してください。

**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
  ```