Introduction
PyTorch 1.6.0 버전 이후 torch.distributed 는 3가지 메인 concept으로 분류가 가능
- Distributed Data-Parallel Training(DDP)
sigle-program multiple-data training에서 주로 사용.
모든 process에 model이 복제되고 모든 복제된 모델에는 다른 data sample set이 들어감.
gradient communication을 통해 model의 synchr를 맞추고 gradient 계산을 overlap 하여 train speed를 높힘.
- RPC-Based Distributed Training(RPC)
DDP에 적합하지 않은 training을 포함하여 general한 training structure를 지원.
예를 들면 ditributed pipeline, parameter server 등.
- Collective Communication(c10d)
group 내의 process 간 tensor의 전송을 가능하게 하는 library.
collective communication API와 P2P communication API를 제공.
DDP와 RCP 모두 c10d를 기반으로 만들어짐.
DDP와 RCP에서 충분히 많은 시나리오에 대한 기능을 제공하여 c10d를 사용할 일이 적음.
Data Parallel Training
Pytorch에서 제공하는 Data Parallel Training에 대한 Option에 따라, 개발자의 공통된 가이드는 다음과 같다:
- data와 model이 모두 하나의 gpu에 올라가고 training speed가 문제되지 않으면 single-device 사용
- 최소한의 코드 변경과, 여러 GPU를 이용한 속도 향상을 위해서는 DataParallel을 사용
- 코드를 더 작성하는 대신, 속도를 더욱 향상시키고 싶은경우 DistributedDataParallel 사용
- application이 machine 간의 scale이 필요한 경우, DistributedDataParallel과 추가 code 작성
- ditributed training 중 error(e.g., out-of-memory)가 예상되거나 resource가 dynamic하게 바뀌는 경우 torch.distributed.elastic 이용.
torch.nn.DataParallel
code의 한줄만 변경되면 적용이 가능하다. 대신 best performace를 제공하지는 않는다. DataParallel은 모든 forward pass 마다 model을 복제하고, single-process multi-thread parallelism은 GIL connection에 의해 성능 저하가 발생한다.
♢ GIL이란 Global Interpreter Lock의 약자로 python에서 multi-thread 사용시 interpreter가 한 thread만 하나의 바이트코드를 실행할 수 있도록 Lock는 것.
torch.nn.Parallel.DistributedDataParallel
DistributedDataParallel은 init_process_group이라는 step을 거쳐야한다. DDP는 multi-process parallelism을 사용하므로 GIL connection이 없다. 또한 model이 DDP construction 시간에 broadcast되어 training 속도가 향상된다.
다음은 DDP에 대한 document list이다.
- DDP notes. starter example과 design, implementation에 대한 간단한 설명. 초보자에게 추천
- Getting Started with Ditributed Data Parallel. DDP Training하는 방법에 대한 설명
- Launching and configuring distributed data parallel applications. DDP launching script를 어떻게 사용하는 알려줌
- Shard Optimizer States With ZeroRedundancyOptimizer. optimizer memory footprint를 줄이고 ZeroRedundancyOptimizer를 하는 방법 설명.
- Distributed Training with Uneven Inputs Using the Join Context Manager. 고르지 않은 데이터 input에 대한 distributed training을 하는 방법 설명.
torch.distributed.elastic
DDP는 OOM과 같은 error를 처리할 수 없다. elastic은 falut tolerance를 더하고 machine들의 dynamic pool을 이용할 수 있도록 한다.
RPC-Based Distributed Training
torch.distributed.rpc는 일반적인 distributed training scenarios에 대한 지원을 목적으로한다.
torch.distributed.rpc는 다음 4가지의 핵심 존재:
- remote worker에서의 running 지원
- RRef는 remote object에 대한 lifetime을 관리
- Distributed Autograd는 machine boundaries를 넘어 autograd egine을 확장
- Distributed Optimizer는 모든 worker를 통해 parameter update 진행
아래는 RPC Tutorials에 대한 문서 list이다.
- Getting Started with Distributed RPC Framework. RPC와 RRef에 대한 RL의 예시를 제공하고, ditributed autograd와 distributed optimizer에 대한 RNN의 예시를 제공
- Implementing a Parameter Server Using Distributed RPC Framework. asynchronous parameter server training application에 대한 예시
- Distributed Pipeline Parallelism Using RPC. single machin pipeline 예제를 RPC를 이용하여 distributed environment로 확장한 예제
- Implementing Batch RPC Processing Using Asynchronous Executions. @rpc.functions.async_execution decorator를 이용한 RPC batch processing 예제.
- Combining Distributed DataParallel with Distributed RPC Framework. RPC와 DDP를 결합하여 훈련하는 예제.
참고 문서