기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
SageMaker Python SDK에서 PyTorch 프레임워크 추정기 사용
SageMaker AI PyTorchdistribution 인수를 추가하여 분산 훈련을 시작할 수 있습니다. 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도 실행하지만 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" } } }
다음 코드 샘플은 분산 훈련 옵션이 있는 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 라이트닝 및 라이트닝 볼트와 같은 유틸리티 라이브러리는 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 설명서의 서드파티 라이브러리 사용
SMDDP 집합 작업을 활성화하고 올바른 분산 훈련 시작 관리자 옵션을 사용하기 위한 고려 사항
-
SMDDP
AllReduce와 SMDDPAllGather는 현재 상호 호환되지 않습니다. -
SMDDP
AllReduce는 기본적으로mpirun기반 런처인smdistributed또는pytorchddp를 사용할 때 활성화되며 NCCLAllGather가 사용됩니다. -
SMDDP
AllGather는torch_distributed시작 관리자를 사용할 때 기본적으로 활성화되며AllReduce는 NCCL로 돌아갑니다. -
SMDDP
AllGather는 다음과 같이 설정된 추가 환경 변수와 함께mpirun기반 시작 관리자를 사용할 때 활성화할 수도 있습니다.export SMDATAPARALLEL_OPTIMIZE_SDP=true