← All courses ← Course home

Lesson 06 of 12 · Phase 3 Training

Distributed training simulation

DDP, FSDP, and ZeRO are memory and communication stories. Simulate them before you rent a cluster.

Agent brief (llms.md)

Distributed training is how you fit a replica that no longer fits on one GPU, or how you shorten wall time. The words are DDP, FSDP, and ZeRO. The paper title to know for sharded optimiser state is ZeRO. You will simulate the memory story before you pay for a box of H100s.

Intuition, not a vendor tour

You do not need a 64-GPU job to understand this. Write a simulator that accounts bytes.


from dataclasses import dataclass

@dataclass
class Plan:
    params: int
    dtype_bytes: int
    ranks: int
    adam: bool = True

def ddp_bytes(plan: Plan) -> dict:
    model = plan.params * plan.dtype_bytes
    grads = model
    optim = plan.params * 8 * 2 if plan.adam else 0  # two fp32 moments, rough
    return {"per_rank": model + grads + optim, "mode": "ddp"}

def zero3_bytes(plan: Plan) -> dict:
    model = plan.params * plan.dtype_bytes / plan.ranks
    grads = model
    optim = (plan.params * 8 * 2 / plan.ranks) if plan.adam else 0
    return {"per_rank": model + grads + optim, "mode": "zero3"}

This is a sketch. Real frameworks add buckets, gather buffers, and activation memory. Activations often dominate before parameters do. Write that sentence in your notes so you do not blame ZeRO for an activation OOM.

Assignment

Opinion checkpoint

Write this down. If your distributed plan has no communication story and no activation term, you are decorating a slide. Simulate bytes. Then rent GPUs.

Core project 4: a DDP/FSDP/ZeRO intuition simulator.

Next: Supervised fine-tuning.

Key concepts

  • DDP replicates the model and all-reduces gradients.
  • ZeRO/FSDP shard state so memory per rank falls and communication rises.
  • Adam moments are often larger than the fp16 weights.
  • Activation memory can dwarf parameter memory.

Takeaways

  • Ship a byte simulator before you book a cluster.
  • Name ZeRO as a paper title, not as a mood.
  • Write one failure mode you will actually run into.