0Pricing
Deep Learning Academy · Leçon

Les bases de DistributedDataParallel

La méthode standard pour l’entraînement sur plusieurs GPU.

Les bases de DistributedDataParallel est une leçon Deep Learning Academy gratuite sur CoddyKit. Ceci est la leçon 2 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Deep Learning Academy, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Deep Learning Academy comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

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.

Questions Fréquemment Posées

La leçon « Les bases de DistributedDataParallel » est-elle gratuite ?

Oui — le texte complet de « Les bases de DistributedDataParallel » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Deep Learning Academy, passe à CoddyKit PRO. Le cours Deep Learning Academy comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Les bases de DistributedDataParallel » ?

La méthode standard pour l’entraînement sur plusieurs GPU. Tu pratiques Deep Learning Academy avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Deep Learning Academy ?

Aucune expérience préalable n'est requise. Deep Learning Academy sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 2 sur 4.

Combien de temps prend la leçon « Les bases de DistributedDataParallel » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Deep Learning Academy ?

Oui. Chaque leçon Deep Learning Academy inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Parallélisme des données ou du modèle
  2. Les bases de DistributedDataParallel
  3. Normalisation par lots synchronisée et état fragmenté
  4. Lancer des tâches avec torchrun
← Retour à Deep Learning Academy