Podstawy DistributedDataParallel
Poznać standardowy sposób trenowania na wielu GPU
Podstawy DistributedDataParallel to bezpłatna lekcja Deep Learning Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Deep Learning Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Deep Learning Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Meet DDP
DistributedDataParallel, or DDP, is PyTorch's go-to tool for multi-GPU training. It runs one process per GPU and keeps every model copy in sync.
One Process per GPU
Unlike the older DataParallel, DDP spawns a separate process for each GPU. This avoids Python's GIL and scales far more cleanly.
Rank and World Size
Each process gets a rank (its id) and shares the world size (total processes). Rank 0 is usually the one that logs and saves.
import torch.distributed as dist
rank = dist.get_rank()
world = dist.get_world_size()Init the Process Group
Before any communication you call init_process_group. The nccl backend is the fast choice for GPUs.
import torch.distributed as dist
dist.init_process_group(backend="nccl")Pin Each Process to a GPU
Use the local rank to set the device so every process owns exactly one GPU. This keeps work from piling onto a single card.
import torch
torch.cuda.set_device(local_rank)
model = model.to(local_rank)Wrap Your Model
The magic is one line: wrap your model in DDP. From then on, gradients sync automatically during the backward pass.
from torch.nn.parallel import DistributedDataParallel as DDP
model = DDP(model, device_ids=[local_rank])Gradients Sync Themselves
During backward(), DDP performs an all-reduce to average gradients across GPUs. You write normal training code and it just stays in sync. ✨
Use a DistributedSampler
So each GPU sees different data, give your DataLoader a DistributedSampler. It hands every process a non-overlapping slice of the dataset.
from torch.utils.data.distributed import DistributedSampler
sampler = DistributedSampler(dataset)Reshuffle Every Epoch
Call sampler.set_epoch(epoch) at the top of each epoch. Without it, every GPU reshuffles the same way and you lose real shuffling.
for epoch in range(epochs):
sampler.set_epoch(epoch)
train_one_epoch()Save Only on Rank 0
All copies are identical, so checkpoint from rank 0 only. Saving from every process just writes the same file many times.
if rank == 0:
torch.save(model.module.state_dict(), "ckpt.pt")Clean Up at the End
When training finishes, call destroy_process_group to release the group cleanly and avoid hanging processes.
import torch.distributed as dist
dist.destroy_process_group()Quick Check
Think about how DDP keeps copies in sync.
Recap
You set up DDP: init the process group, wrap the model, feed it a DistributedSampler, and save from rank 0. Gradients sync for free.
Często zadawane pytania
Czy lekcja „Podstawy DistributedDataParallel” jest bezpłatna?
Tak — pełny tekst „Podstawy DistributedDataParallel” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Deep Learning Academy, przejdź na CoddyKit PRO. Kurs Deep Learning Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Podstawy DistributedDataParallel”?
Poznać standardowy sposób trenowania na wielu GPU Ćwiczysz Deep Learning Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Deep Learning Academy?
Nie wymagamy żadnego doświadczenia. Deep Learning Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Podstawy DistributedDataParallel”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Deep Learning Academy?
Tak. Każda lekcja Deep Learning Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Równoległość danych a równoległość modelu
- Podstawy DistributedDataParallel
- Synchronizowany Batch Norm i shardowany stan
- Uruchamiać zadania za pomocą torchrun