Nozioni di base su DistributedDataParallel
Il percorso standard per l'addestramento su più GPU
Nozioni di base su DistributedDataParallel è una lezione Deep Learning Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Deep Learning Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Deep Learning Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Domande Frequenti
La lezione «Nozioni di base su DistributedDataParallel» è gratuita?
Sì — il testo completo di «Nozioni di base su DistributedDataParallel» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Deep Learning Academy, passa a CoddyKit PRO. Il corso Deep Learning Academy include 4 lezioni in totale.
Cosa imparerò in «Nozioni di base su DistributedDataParallel»?
Il percorso standard per l'addestramento su più GPU Eserciti Deep Learning Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Deep Learning Academy?
Non è richiesta alcuna esperienza precedente. Deep Learning Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Nozioni di base su DistributedDataParallel»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Deep Learning Academy?
Sì. Ogni lezione Deep Learning Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Parallelismo dei dati e del modello
- Nozioni di base su DistributedDataParallel
- Batch norm sincronizzata e stato suddiviso
- Avviare job con torchrun