3.3.11Deep Learning Frameworks

Distributed training overview

2,665 words12 min readdifficulty · medium3 backlinks

Core Idea

Think of it like a restaurant kitchen: one chef (GPU) can only make so many dishes per hour. With10 chefs working together, you serve customers10× faster—if they coordinate properly and don't get in each other's way.

The Fundamental Problem

What Are We Distributing?

When training a model, three resources dominate:

  1. Computation: Forward pass (y=f(x;θ)\mathbf{y} = f(\mathbf{x}; \theta)) and backward pass (θL\nabla_\theta \mathcal{L})
  2. Memory: Storing parameters θ\theta, activations, gradients, optimizer states
  3. Data: The training dataset D={(xi,yi)}i=1N\mathcal{D} = \{(\mathbf{x}_i, y_i)\}_{i=1}^N
Figure — Distributed training overview

Main Paralelization Strategies

1. Data Paralelism

The workflow:

  1. Split mini-batch BB of size bb into kk sub-batches: B1,B2,,BkB_1, B_2, \ldots, B_k (one per device)
  2. Each device computes gradients on its sub-batch: gi=1Bi(x,y)BiθL(f(x;θ),y)\mathbf{g}_i = \frac{1}{|B_i|} \sum_{(\mathbf{x}, y) \in B_i} \nabla_\theta \mathcal{L}(f(\mathbf{x}; \theta), y)
  3. Synchronize gradients across devices (all-reduce operation): gglobal=1ki=1kgi\mathbf{g}_{\text{global}} = \frac{1}{k} \sum_{i=1}^k \mathbf{g}_i
  4. Each device updates its local copy with gglobal\mathbf{g}_{\text{global}}: θθηgglobal\theta \leftarrow \theta - \eta \mathbf{g}_{\text{global}}

Why this works: The average of gradients across mini-batches equals the gradient of the full batch (linearity of expectation). It's mathematically equivalent to training on a batch of size b×kb \times k on a single device.

Why this step? Synchronizing ensures all devices have identical parameters after each update. Without sync, devices would diverge and produce different models.

Result: 4× faster training (ideally), but communication overhead reduces actual speedup to ~3.5×.

2. Model Paralelism

Two flavors:

A) Tensor Parallelism (Intra-layer splitting) Split individual layers across devices. For a large matrix multiplication Y=XW\mathbf{Y} = \mathbf{X} \mathbf{W} where WRd×h\mathbf{W} \in \mathbb{R}^{d \times h}:

W=[W1W2],Y=X[W1W2]=[Y1Y2]\mathbf{W} = \begin{bmatrix} \mathbf{W}_1 \\ \mathbf{W}_2 \end{bmatrix}, \quad \mathbf{Y} = \mathbf{X} \begin{bmatrix} \mathbf{W}_1 \\ \mathbf{W}_2 \end{bmatrix} = \begin{bmatrix} \mathbf{Y}_1 & \mathbf{Y}_2 \end{bmatrix}

Device 1 computes Y1=XW1\mathbf{Y}_1 = \mathbf{X} \mathbf{W}_1, Device 2 computes Y2=XW2\mathbf{Y}_2 = \mathbf{X} \mathbf{W}_2 in parallel. Then concatenate results.

Why this step? When W\mathbf{W} is too large for one GPU's memory (e.g., a 50GB weight matrix), splitting it is the only way to fit the model.

B) Pipeline Parallelism (Inter-layer splitting) Split the model vertically by layers. Device 1 has layers1-10, Device 2 has layers 11-20, etc.

Data flows like an assembly line:

  • Batch 1 enters Device 1 (layers 1-10)
  • Output of Device 1 → 2 (layers 11-20)
  • While Device 2 processes Batch 1, Device 1 starts on Batch 2

Challenge: Pipeline bubles—devices sit idle waiting for data from the previous stage.

Solution: Split across 8 GPUs:

  • GPU 0: Layers 0-11
  • GPU 1: Layers 12-23
  • ...
  • GPU 7: Layers 84-95

A forward pass requires 7 device-to-device transfers (expensive!). But it's the only way to run the model.

3. Hybrid Parallelism

Example: Training a 100B parameter model on 64 GPUs (8 nodes × 8 GPUs):

  • Split model across 8 GPUs (model parallelism)
  • Replicate this 8 times (data parallelism across nodes)
  • Total: 8 data-parallel replicas, each using 8-way model parallelism

Key Trade-offs

Communication Overhead

Actual speedup: S=TcomputeTcompute/k+TcommS = \frac{T_{\text{compute}}}{T_{\text{compute}}/k + T_{\text{comm}}}

where TcommT_{\text{comm}} is time spent synchronizing gradients/activations.

Why this matters: If TcommTcompute/kT_{\text{comm}} \approx T_{\text{compute}}/k, speedup is only 0.5k0.5k (50% efficiency). High-bandwidth interconnects (NVLink, InfiniBand) reduce TcommT_{\text{comm}}.

Scaling Efficiency

  • Linear scaling: Efficiency =1 (rare, only with minimal communication)
  • Sub-linear scaling: Efficiency = 0.7-0.9 (typical for well-optimized systems)
  • Super-linear scaling: Efficiency > 1 (possible when larger batch fits in cache)

Why efficiency degrades: As kk increases, each device does less work (Tcompute/kT_{\text{compute}}/k shrinks), but TcommT_{\text{comm}} stays constant or grows (more devices to sync).

Common Implementation Patterns

Synchronous vs. Asynchronous Training

Pros: Mathematically equivalent to single-device training (deterministic) Cons: Speed limited by the slowest device (stragglers hurt performance)

Pros: No waiting for stragglers, better hardware utilization Cons: Stale gradients (worker uses θt\theta_t but updates θt+5\theta_{t+5}), can hurt convergence

Gradient Accumulation

  1. Accumulate gradients over k=B/bk = B/b micro-batches: gaccum=1ki=1kgi\mathbf{g}_{\text{accum}} = \frac{1}{k} \sum_{i=1}^k \mathbf{g}_i
  2. Update after kk forward-backward passes: θθηgaccum\theta \leftarrow \theta - \eta \mathbf{g}_{\text{accum}}

Why this works: Gradients are linear—averaging over micro-batches equals the gradient of the full batch. It's equivalent to processing a batch of size BB, just slower.

Mistakes & Misconceptions

The reality: Communication overhead grows with device count. Beyond a certain point (the "scaling wall"), adding devices gives diminishing returns or even slows things down.

Example: Training a small CNN on 100 GPUs might be slower than on 8 GPUs because gradient sync time dominates computation.

Fix: Match paralelism to model size. Small models: 1-8 GPUs with data parallelism. Large models: 100+ GPUs with hybrid strategies.

The difference:

  • Large batch on1 GPU: Process 256 samples → update
  • Data parallel on 4 GPUs (batch 64 each): Process 64 samples per GPU → sync update (effective batch 256)

They're equivalent in math but different in implementation. Data parallelism adds communication cost; large batches one GPU add memory pressure.

The nuance: Model parallelism adds cross-device communication every layer (high latency). Data parallelism only syncs at the end of backward pass.

Better strategy: Use data parallelism as much as possible, model parallelism only when necessary. For example, a 10B parameter model might fit on a 40GB GPU with activation checkpointing—avoid model parallelism.

Practical Considerations

Choosing a Strategy

Framework Support

  • PyTorch: DistributedDataParallel (data), Tensor Parallelism (via Megatron-LM), PipeDream (pipeline)
  • TensorFlow: tf.distribute.MirroredStrategy (data), tf.distribute.TPUStrategy (data on TPUs)
  • JAX: pmap (data), xmap (arbitrary sharding)
  • DepSpeed: ZeRO stages (optimizer sharding), pipeline paralelism
  • Horovod: MPI-based data parallelism (framework-agnostic)

Active Recall Practice

Recall Explain Distributed Training to a 12-Year-Old

Imagine you need to solve1000 math problems for homework. If you do it alone, it takes 10 hours. But if you and3 friends each do 250 problems, you're done in 2.5 hours—everyone finishes around the same time.

Distributed training is like that for AI models. Training a huge AI brain (like ChatGPT) one computer takes forever. So we split the work across many computers (GPUs). Each computer trains on different pictures or sentences, learns something, and then all computers share what they learned. By combining everyone's answers, the AI learns faster.

The tricky part? The computers need to talk to each other to share answers (like you comparing homework answers), and that talking takes time. If they spend too much time talking, it's slower than just working alone!

  • Model parallelism: Split model across devices

  • Overhead: Communication cost grows with devices

  • Devices: More isn't always better (scaling wall)

  • Efficiency: Actual speedup / ideal speedup

  • Layers: Pipeline splits by layers

  • Hybrid: Combines data + model parallelism

  • Yield: Best for100B+ parameter models

  • Batch: Gradient accumulation simulates large batches

  • Ring all-reduce: Efficient gradient sync topology

  • Interconnect: NVLink/InfiniBand critical for model parallelism

  • Deterministic: Synchronous SGD matches single-GPU

Connections

  • 3.1-Neural-network-basics - forward/backward passes being parallelized
  • 3.3.5-Batch-normalization - batch statistics in data parallelism
  • 3.9-Gradient-descent-optimizers - optimizer state sharding (ZeRO)
  • 3.4.1-Transformers-overview - why GPT-3 needs model parallelism
  • 4.2-GPU-acceleration - hardware that enables distribution
  • 5.1-Training-at-scale - practical engineering of distributed systems

#flashcards/ai-ml

What is the main goal of distributed training? :: Speed up training and enable models larger than single-device memory by paralelizing computation across multiple GPUs/TPUs.

In data paralelism, what is synchronized across devices after each mini-batch?
Gradients. Each device computes gradients on its data subset, then all-reduce averages them so all devices update with the same global gradient.
What is the formula for actual speedup with k devices and communication overhead?
S=TcomputeTcompute/k+TcommS = \frac{T_{\text{compute}}}{T_{\text{compute}}/k + T_{\text{comm}}} where TcommT_{\text{comm}} is gradient synchronization time.
How does model parallelism differ from data parallelism?
Model parallelism splits the model (parameters) across devices; each device holds part of the network. Data parallelism replicates the full model on each device; devices process different data.
What is pipeline parallelism?
Splitting the model by layers across devices. Data flows sequentially: Device 1 processes layers 1-10, outputs to Device 2 for layers 11-20, etc.
Why does scaling efficiency typically decrease as number of devices increases?
Communication overhead TcommT_{\text{comm}} stays constant or grows, while per-device computation Tcompute/kT_{\text{compute}}/k shrinks. Eventually communication dominates.
What is gradient accumulation used for?
Simulating large batch sizes when GPU memory is limited. Accumulate gradients over multiple micro-batches before updating, equivalent to processing a larger batch.
Synchronous vs asynchronous SGD trade-off?
Synchronous: deterministic, mathematically equivalent to single-device, but limited by slowest worker. Asynchronous: better utilization, no stragler waiting, but stale gradients can hurt convergence.
When should you use model parallelism over data parallelism?
Only when the model doesn't fit on a single device's memory. Model parallelism adds high communication cost (every layer), so prefer data parallelism whenever possible.
What is hybrid parallelism?
Combining data and model parallelism. Example: Split a 100B model across 8 GPUs (model parallel), then replicate 8 times across nodes (data parallel).

Concept Map

aims to

distributes

strategy

each device holds

splits batch

computes

averaged via

ensures

keeps devices consistent

Distributed Training

Speed, Bigger Models, Bigger Batches

Dominant Resources

Computation

Memory

Data

Data Parallelism

Full Model Copy per Device

Split Batch into Sub-batches

Local Gradients

All-Reduce Sync

Identical Param Update

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Distributed training ka matlab haiek bade model ko train karne ke liye bahut sare GPUs ya computers ka use karna. Socho, agar tumhe ek bahut bada neural network train karna hai—jaise GPT-3 jo 175 billion parameters ka hai—toh ek GPU mein wo fit hi nahi hoga! Aur agar fit bhi ho jaye, toh training mein mahino lag jayenge. Isliye hum kaam ko divide karte hain multiple devices mein.

Teen main tarike hain: Data paralelism mein har GPU ke pas model kiek copy hoti hai, lekin har GPU alag-alag data pe train karta hai. Jaise 4 doston ne homework divide kar liya—sab same questions solve kar rahe hain, bas alag-alag pages pe. Phir sabke answers combine karke final solution banate hain (isse all-reduce kehte hain). Model parallelism mein model ko hi tod dete hain—ek GPU ke paas pehle 25 layers, dusre ke paas next 25, aur aise chalte hain. Ye tab use hota hai jab model itna bada hai ki ek GPU mein fit hi nahi hota. Pipeline parallelism bhi model ko split karta hai, lekin assembly line ki tarah—data pehle GPU se dusre GPU mein flow hota hai.

Sabse badi challenge hai communication overhead. Jab GPUs ko apne gradients ya activations share karne padte hain, toh usme time lagta hai. Agar ye communication ka time zyada ho gaya, toh speedup kam ho jata hai. Isliye high-speed connections jaise NVLink ya InfiniBand bahut important hain. Real-world mein, agar 8 GPUs use karo toh ideal mein 8x speedup milna chahiye, lekin practically6-7x hi milta hai communication delays ki wajah se. Phir bhi, ye technique hi hai jo aj ke large AI models ko possible banati hai!

Go deeper — visual, from zero

Test yourself — Deep Learning Frameworks

Connections