6 min total

LLMs

Generative AI with LLMs

Generative AI with LLMs
LLMs — Generative AI with LLMs
LLMs — Generative AI with LLMs
LLMs — Generative AI with LLMs
LLMs — Generative AI with LLMs
LLMs — Generative AI with LLMs
LLMs — Generative AI with LLMs
LLMs — Generative AI with LLMs
LLMs — Generative AI with LLMs

multi-GPU compute strategies

Why Scale Beyond a Single GPU?

  • Necessity: Model doesn't fit in single GPU memory.
  • Benefit: Even for small models, multi-GPU training speeds up training time.

Strategy 1: Data Parallelism with DDP

  • Important to understand efficient GPU utilization strategies.

🔧 DDP (Distributed Data Parallel – PyTorch)

  • Approach: Replicates the full model on each GPU.
  1. Model copied to each GPU.
  2. Data batches split and sent to each GPU.
  3. Each GPU processes data in parallel.
  4. Gradients are synchronized and averaged.
  5. Each model copy is updated identically.
  • Limitation: Model must fit in each GPU’s memory (including optimizer states and gradients).
LLMs — Generative AI with LLMs

Strategy 2: Model Sharding with FSDP

🔧 FSDP (Fully Sharded Data Parallel – PyTorch)

  • Based on ZeRO (Zero Redundancy Optimizer) by Microsoft (2019).
  • Reduces redundant memory use by sharding model states across GPUs.

🧠 Memory Components in Training

  • Weights (Parameters) – Blue
  • Gradients – Yellow
  • Optimizer States – Green
    Optimizer states take 2x the space of weights
LLMs — Generative AI with LLMs

ZeRO Stages

StageSharded ComponentMemory Reduction
1Optimizer StatesUp to 4×
2Optimizer States + GradientsUp to 8×
3All (Weights, Gradients, Optimizer States)Linear with #GPUs (e.g., 64× for 64 GPUs)

SDP vs DDP

  • DDP: Replicates full model on each GPU.
  • Shards data and model states.
  • Dynamically materializes shards during forward/backward pass.
  • Releases non-local data post-computation.
  • Option to cache data during backward pass.

Trade-offs

  • More sharding → Less memory use.
  • But ↑ GPU-to-GPU communication overhead.
  • FSDP can offload computation to CPU to save GPU memory.
  • 1 = Full replication (like DDP).
  • Max GPUs = Full sharding.
  • In-between = Hyper sharding.

Trade-offs

  • More sharding → Less memory use.
  • But ↑ GPU-to-GPU communication overhead.
  • FSDP can offload computation to CPU to save GPU memory.
  • 1 = Full replication (like DDP).
  • Max GPUs = Full sharding.
  • In-between = Hyper sharding.