0Pricing
Deep Learning Academy · Lektion

Grundlagen von DistributedDataParallel

Der Standardweg für Training auf mehreren GPUs.

Grundlagen von DistributedDataParallel ist eine kostenlose Deep Learning Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Deep Learning Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Häufig gestellte Fragen

Ist die Lektion „Grundlagen von DistributedDataParallel“ kostenlos?

Ja — der vollständige Text von „Grundlagen von DistributedDataParallel“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Deep Learning Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Grundlagen von DistributedDataParallel“?

Der Standardweg für Training auf mehreren GPUs. Du übst Deep Learning Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Deep Learning Academy zu starten?

Keine Vorkenntnisse erforderlich. Deep Learning Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Grundlagen von DistributedDataParallel“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Deep Learning Academy-Lektion Code schreiben und ausführen?

Ja. Jede Deep Learning Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Daten- vs. Modellparallelismus
  2. Grundlagen von DistributedDataParallel
  3. Synchronisierte Batch Norm und geshardeter State
  4. Jobs mit torchrun starten
← Zurück zu Deep Learning Academy