multi-GPU 의 표준
DistributedDataParallel (DDP) 가 PyTorch 의 multi-GPU training 방법. 정신 모델: GPU 당 한 process, 각 process 가 model 자체 copy 들고 data slice 위 자체 forward/backward 돌림. backward 후 gradient 가 process 들에 synchronize (all-reduce); 각 optimizer step 이 자기 weight copy 에 같은 averaged gradient 적용해서 lockstep 유지.
single-GPU script 에 추가할 거
- process group 초기화 (
dist.init_process_group("nccl")). - per-process device 설정 (
torch.cuda.set_device(rank)). - model 을 DDP 로 wrap (
model = DDP(model, device_ids=[rank])). - 각 process 가 다른 data slice 보게 DistributedSampler 사용.
torchrun으로 launch (modern launcher, 옛torch.distributed.launch아님).
DistributedSampler 함정
DistributedSampler 없으면 모든 process 가 full dataset iterate — 중복 작업에 compute 낭비. 매 epoch top 의 sampler 의 set_epoch(epoch) 가 mandatory; 없으면 shuffle 이 매 epoch 같고 training 이 신비롭게 plateau.
torchrun — modern launcher
torchrun --nproc_per_node=4 train.py 가 4 process spawn, 각 환경 변수 (LOCAL_RANK, WORLD_SIZE, RANK) 설정해서 너 script 가 self-identify. 옛 torch.distributed.launch 도 작동하지만 torchrun 선호.