Dasar-Dasar DistributedDataParallel
Cara standar untuk pelatihan multi-GPU
Dasar-Dasar DistributedDataParallel adalah pelajaran Deep Learning Academy gratis di CoddyKit. Ini adalah pelajaran 2 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Deep Learning Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Deep Learning Academy mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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.
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Dasar-Dasar DistributedDataParallel” gratis?
Ya — teks lengkap “Dasar-Dasar DistributedDataParallel” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Deep Learning Academy, upgrade ke CoddyKit PRO. Kursus Deep Learning Academy mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Dasar-Dasar DistributedDataParallel”?
Cara standar untuk pelatihan multi-GPU Kamu berlatih Deep Learning Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Deep Learning Academy?
Tidak diperlukan pengalaman sebelumnya. Deep Learning Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 2 dari 4.
Berapa lama pelajaran “Dasar-Dasar DistributedDataParallel” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Deep Learning Academy ini?
Ya. Setiap pelajaran Deep Learning Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Paralelisme Data vs Model
- Dasar-Dasar DistributedDataParallel
- Norm Batch Tersinkronisasi dan State Terpecah
- Luncurkan Pekerjaan dengan torchrun