

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

# Amazon SageMaker AI で異種クラスターを使用したトレーニングジョブを設定する
<a name="train-heterogeneous-cluster-configure"></a>

このセクションでは、複数のインスタンスタイプで構成される異種クラスターを使用してトレーニングジョブを実行する方法について説明します。

開始する前に、次の点に注意してください。
+ すべてのインスタンスグループは同じ Docker イメージとトレーニングスクリプトを共有します。そのため、トレーニングスクリプトは、そのスクリプトがどのインスタンスグループに属しているかを検出し、それに応じて実行をフォークするように変更する必要があります。
+ 異種クラスター機能は SageMaker AI ローカルモードとは互換性がありません。
+ 異種クラスタートレーニングジョブの Amazon CloudWatch ログストリームは、インスタンスグループごとにグループ化されていません。どのノードがどのグループに属しているかをログから把握する必要があります。

**Topics**
+ [オプション 1: SageMaker Python SDK を使用する](#train-heterogeneous-cluster-configure-pysdk)
+ [オプション 2: 詳細な設定が可能な SageMaker API を使用する](#train-heterogeneous-cluster-configure-api)

## オプション 1: SageMaker Python SDK を使用する
<a name="train-heterogeneous-cluster-configure-pysdk"></a>

指示に従って、SageMaker Python SDK を使用して異種クラスターのインスタンスグループを設定します。

1. 異種クラスターのインスタンスグループをトレーニングジョブ用に設定するには、`sagemaker.instance_group.InstanceGroup` クラスを使用します。各インスタンスグループのカスタム名、インスタンスタイプ、インスタンス数を指定できます。詳細については、*SageMaker AI Python SDK ドキュメント*の「[sagemaker.instance\$1group.InstanceGroup](https://sagemaker.readthedocs.io/en/stable/api/utility/instance_group.html)」を参照してください。
**注記**  
利用可能なインスタンスタイプと、異種クラスターで構成できるインスタンスグループの最大数の詳細については、「[InstanceGroup](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_InstanceGroup.html)」の API リファレンスをご覧ください。

   以下のコード例は、次の図のように `instance_group_1` という名前の 2 つの `ml.c5.18xlarge` CPU 専用インスタンスと、`instance_group_2` という名前の 1 つの `ml.p3dn.24xlarge` GPU インスタンスで構成される 2 つのインスタンスグループを設定する方法を示しています。  
![\[SageMaker トレーニングジョブでデータを割り当てる方法の概念的な例。\]](http://docs.aws.amazon.com/ja_jp/sagemaker/latest/dg/images/HCTraining.png)

   前の図は、データ前処理などの事前トレーニングプロセスを CPU インスタンスグループに割り当て、前処理されたデータを GPU インスタンスグループにストリーミングする方法の概念的な例を示しています。

   ```
   from sagemaker.instance_group import InstanceGroup
   
   instance_group_1 = InstanceGroup(
       "instance_group_1", "ml.c5.18xlarge", 2
   )
   instance_group_2 = InstanceGroup(
       "instance_group_2", "ml.p3dn.24xlarge", 1
   )
   ```

1. インスタンスグループオブジェクトを使用してトレーニング入力チャンネルを設定し、「[sagemaker.inputs.TrainingInput](https://sagemaker.readthedocs.io/en/stable/api/utility/inputs.html)」クラスの `instance_group_names` 引数を使用してインスタンスグループをチャンネルに割り当てます。`instance_group_names` 引数には、インスタンスグループ名の文字列のリストを使用できます。

   以下の例では、2 つのトレーニング入力チャンネルを設定し、前のステップの例で作成したインスタンスグループを割り当てる方法を示します。インスタンスグループの `s3_data` 引数に Amazon S3 バケットパスを指定することで、利用目的に応じたデータを処理を行うこともできます。

   ```
   from sagemaker.inputs import TrainingInput
   
   training_input_channel_1 = TrainingInput(
       s3_data_type='S3Prefix', # Available Options: S3Prefix | ManifestFile | AugmentedManifestFile
       s3_data='s3://your-training-data-storage/folder1',
       distribution='FullyReplicated', # Available Options: FullyReplicated | ShardedByS3Key 
       input_mode='File', # Available Options: File | Pipe | FastFile
       instance_groups=["instance_group_1"]
   )
   
   training_input_channel_2 = TrainingInput(
       s3_data_type='S3Prefix',
       s3_data='s3://your-training-data-storage/folder2',
       distribution='FullyReplicated',
       input_mode='File',
       instance_groups=["instance_group_2"]
   )
   ```

   `TrainingInput` の引数の詳細については、以下のリンクを参照してください。
   + 「SageMaker Python SDK ドキュメント」の「[sagemaker.inputs.TrainingInput](https://sagemaker.readthedocs.io/en/stable/api/utility/inputs.html)」クラス
   + 「*SageMaker AI API リファレンス*」の「[S3DataSource](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_S3DataSource.html)」API

1. 以下のコード例に示すように、`instance_groups` 引数を使用で SageMaker AI 推定器を設定します。`instance_groups` 引数には `InstanceGroup` オブジェクトのリストを使用できます。
**注記**  
異種クラスター機能は、SageMaker AI の [PyTorch](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/sagemaker.pytorch.html) および [TensorFlow](https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/sagemaker.tensorflow.html#tensorflow-estimator) フレームワーク推定器クラスを通じて利用できます。サポートされているフレームワークは PyTorch v1.10 以降と TensorFlow v2.6 以降です。使用可能なフレームワークコンテナ、フレームワークバージョン、Python バージョンの完全なリストについては、 AWS Deep Learning Container GitHub リポジトリの[SageMaker AI Framework Containers](https://github.com/aws/deep-learning-containers/blob/master/available_images.md#sagemaker-framework-containers-sm-support-only)」を参照してください。

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

   ```
   from sagemaker.pytorch import PyTorch
   
   estimator = PyTorch(
       ...
       entry_point='my-training-script.py',
       framework_version='x.y.z',    # 1.10.0 or later
       py_version='pyxy',            
       job_name='my-training-job-with-heterogeneous-cluster',
       instance_groups=[instance_group_1, instance_group_2]
   )
   ```

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

   ```
   from sagemaker.tensorflow import TensorFlow
   
   estimator = TensorFlow(
       ...
       entry_point='my-training-script.py',
       framework_version='x.y.z', # 2.6.0 or later
       py_version='pyxy',
       job_name='my-training-job-with-heterogeneous-cluster',
       instance_groups=[instance_group_1, instance_group_2]
   )
   ```

------
**注記**  
SageMaker AI 推定器クラスの `instance_type` と `instance_count` 引数のペアと `instance_groups` 引数は相互に排他的です。同種クラスタートレーニングには、`instance_type` と `instance_count` 引数のペアを使用します。異種クラスタートレーニングには、`instance_groups` を使用します。
**注記**  
使用可能なフレームワークコンテナ、フレームワークバージョン、Python バージョンの完全なリストについては、 AWS Deep Learning Container GitHub リポジトリの[SageMaker AI Framework Containers](https://github.com/aws/deep-learning-containers/blob/master/available_images.md#sagemaker-framework-containers-sm-support-only)」を参照してください。

1. インスタンスグループで設定したトレーニング入力チャネルで `estimator.fit` メソッドを設定し、トレーニングジョブを開始します。

   ```
   estimator.fit(
       inputs={
           'training': training_input_channel_1, 
           'dummy-input-channel': training_input_channel_2
       }
   )
   ```

## オプション 2: 詳細な設定が可能な SageMaker API を使用する
<a name="train-heterogeneous-cluster-configure-api"></a>

 AWS Command Line Interface または を使用して AWS SDK for Python (Boto3) いて、異種クラスターでトレーニングジョブリクエストを送信するために低レベルの SageMaker APIs を使用する場合は、次の API リファレンスを参照してください。
+ [CreateTrainingJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html)
+ [ResourceConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ResourceConfig.html)
+ [InstanceGroup](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_InstanceGroup.html)
+ [S3DataSource](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_S3DataSource.html)