0Pricing
Deep Learning Academy · บทเรียน

พื้นฐาน DistributedDataParallel

แนวทางมาตรฐานสำหรับการฝึกหลาย GPU

พื้นฐาน DistributedDataParallel เป็นบทเรียน Deep Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Deep Learning Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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.

คำถามที่พบบ่อย

บทเรียน “พื้นฐาน DistributedDataParallel” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “พื้นฐาน DistributedDataParallel” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Deep Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “พื้นฐาน DistributedDataParallel”

แนวทางมาตรฐานสำหรับการฝึกหลาย GPU คุณปฏิบัติ Deep Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Deep Learning Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Deep Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “พื้นฐาน DistributedDataParallel” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Deep Learning Academy นี้ได้ไหม

ได้ บทเรียน Deep Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การประมวลผลแบบขนานด้านข้อมูลกับด้านโมเดล
  2. พื้นฐาน DistributedDataParallel
  3. Batch Norm แบบซิงก์และสถานะแบ่งส่วน
  4. เปิดงานด้วย torchrun
← กลับไปที่ Deep Learning Academy