

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# SageMaker Python SDK에서 PyTorch 프레임워크 추정기 사용
<a name="data-parallel-framework-estimator"></a>

`distribution` 인수를 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)에 추가하여 분산 훈련을 시작할 수 있습니다. 자세한 내용을 알아보려면 다음 선택 항목에서 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`도 실행하지만 SageMaker AI에서 PyTorch 분산 훈련을 실행하는 데 필요한 환경 변수를 설정하는 `smddprun`도 이용합니다.

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

NCCL `AllGather`를 SMDDP `AllGather`로 교체하도록 선택한 경우 세 가지 옵션을 모두 사용할 수 있습니다. 사용 사례에 맞는 옵션을 하나 선택합니다.

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 라이트닝 및 라이트닝 볼트와 같은 유틸리티 라이브러리는 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 설명서*의 [서드파티 라이브러리 사용](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")
```

------