3.3.11 · HinglishDeep Learning Frameworks

Distributed training overview

2,693 words12 min readRead in English

3.3.11 · AI-ML › Deep Learning Frameworks

Core Idea

Ise ek restaurant kitchen ki tarah socho: ek chef (GPU) ek ghante mein sirf itne hi dishes bana sakta hai. 10 chefs milke kaam karein, toh customers ko 10× faster serve karte ho—agar woh sahi se coordinate karein aur ek doosre ke raaste mein na aayein.

The Fundamental Problem

Hum Distribute Kya Kar Rahe Hain?

Jab koi model train karte hain, teen resources dominant hote hain:

  1. Computation: Forward pass () aur backward pass ()
  2. Memory: Parameters , activations, gradients, optimizer states store karna
  3. Data: Training dataset
Figure — Distributed training overview

Main Parallelization Strategies

1. Data Parallelism

Workflow:

  1. Mini-batch of size ko sub-batches mein split karo: (ek per device)
  2. Har device apne sub-batch par gradients compute karta hai:
  3. Devices ke beech gradients synchronize karo (all-reduce operation):
  4. Har device apni local copy se update karta hai:

Ye kyun kaam karta hai: Mini-batches ke gradients ka average, full batch ke gradient ke barabar hota hai (expectation ki linearity). Mathematically ye ek single device par size ke batch par training ke equivalent hai.

Ye step kyun? Synchronizing ensure karta hai ki har update ke baad sab devices ke parameters identical hain. Sync ke bina, devices diverge ho jaate aur alag-alag models produce karte.

Result: 4× faster training (ideally), lekin communication overhead actual speedup ko ~3.5× tak reduce kar deta hai.

2. Model Parallelism

Do types:

A) Tensor Parallelism (Intra-layer splitting) Individual layers ko devices mein split karo. Ek badi matrix multiplication ke liye jahan :

Device 1 compute karta hai , Device 2 compute karta hai parallel mein. Phir results concatenate karo.

Ye step kyun? Jab ek GPU ki memory mein fit nahi hota (jaise ek 50GB weight matrix), tab use split karna hi ek tarika hai model fit karne ka.

B) Pipeline Parallelism (Inter-layer splitting) Model ko vertically layers ke hisaab se split karo. Device 1 ke paas layers 1-10 hain, Device 2 ke paas layers 11-20, aur aage bhi.

Data ek assembly line ki tarah flow karta hai:

  • Batch 1 Device 1 mein enter karta hai (layers 1-10)
  • Device 1 ka output → Device 2 (layers 11-20)
  • Jab Device 2 Batch 1 process kar raha hota hai, Device 1 Batch 2 shuru kar deta hai

Challenge: Pipeline bubbles—devices idle baithte hain, pichle stage se data ka wait karte hue.

Solution: 8 GPUs mein split karo:

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

Ek forward pass mein 7 device-to-device transfers chahiye (expensive!). Lekin model run karne ka yahi ek tarika hai.

3. Hybrid Parallelism

Example: 64 GPUs (8 nodes × 8 GPUs) par ek 100B parameter model train karna:

  • Model ko 8 GPUs mein split karo (model parallelism)
  • Ise 8 baar replicate karo (nodes ke across data parallelism)
  • Total: 8 data-parallel replicas, har ek 8-way model parallelism use karta hai

Key Trade-offs

Communication Overhead

Actual speedup:

jahan gradients/activations synchronize karne mein lagta waqt hai.

Ye kyun matter karta hai: Agar , toh speedup sirf hai (50% efficiency). High-bandwidth interconnects (NVLink, InfiniBand) reduce karte hain.

Scaling Efficiency

  • Linear scaling: Efficiency = 1 (rare, sirf minimal communication ke saath)
  • Sub-linear scaling: Efficiency = 0.7-0.9 (well-optimized systems ke liye typical)
  • Super-linear scaling: Efficiency > 1 (possible jab bada batch cache mein fit ho)

Efficiency kyun degrade hoti hai: Jaise-jaise badhta hai, har device kam kaam karta hai ( shrink hota hai), lekin constant rehta hai ya badhta hai (zyada devices sync karne hain).

Common Implementation Patterns

Synchronous vs. Asynchronous Training

Pros: Mathematically single-device training ke equivalent (deterministic) Cons: Speed slowest device se limit hoti hai (stragglers performance hurt karte hain)

Pros: Stragglers ka wait nahi, better hardware utilization Cons: Stale gradients (worker use karta hai lekin update karta hai), convergence hurt ho sakti hai

Gradient Accumulation

  1. micro-batches ke upar gradients accumulate karo:
  2. forward-backward passes ke baad update karo:

Ye kyun kaam karta hai: Gradients linear hain—micro-batches ka average, full batch ke gradient ke barabar hota hai. Ye size ke batch process karne ke equivalent hai, bas slower hai.

Mistakes & Misconceptions

Reality: Communication overhead device count ke saath badhta hai. Ek certain point ke baad ("scaling wall"), devices add karne se diminishing returns milte hain ya cheezein slow bhi ho jaati hain.

Example: Ek chhote CNN ko 100 GPUs par train karna slower ho sakta hai 8 GPUs ke comparison mein kyunki gradient sync time computation ko dominate kar leta hai.

Fix: Parallelism ko model size ke hisaab se match karo. Chhote models: data parallelism ke saath 1-8 GPUs. Bade models: hybrid strategies ke saath 100+ GPUs.

Difference:

  • Large batch on 1 GPU: 256 samples process karo → update
  • Data parallel on 4 GPUs (batch 64 each): Har GPU par 64 samples process karo → sync update (effective batch 256)

Math mein dono equivalent hain lekin implementation mein alag. Data parallelism communication cost add karta hai; ek GPU par large batches memory pressure add karte hain.

Nuance: Model parallelism har layer mein cross-device communication add karta hai (high latency). Data parallelism sirf backward pass ke end mein sync karta hai.

Better strategy: Jitna ho sake data parallelism use karo, model parallelism sirf tab jab zaroor ho. For example, ek 10B parameter model shayad activation checkpointing ke saath ek 40GB GPU par fit ho jaaye—model parallelism se bachte raho.

Practical Considerations

Strategy Kaise Chunein

Framework Support

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

Active Recall Practice

Recall Ek 12-Saal Ke Bacche Ko Distributed Training Samjhao

Socho tumhe homework mein 1000 math problems solve karne hain. Agar akele karo, toh 10 ghante lagte hain. Lekin agar tum aur 3 doston mein se har ek 250 problems kare, toh 2.5 ghante mein ho jaata hai—sab roughly ek saath finish karte hain.

Distributed training AI models ke liye aisa hi hai. Ek bade AI brain (jaise ChatGPT) ko ek computer par train karna bahut time leta hai. Toh hum kaam ko bahut saare computers (GPUs) mein split karte hain. Har computer alag pictures ya sentences par train karta hai, kuch seekhta hai, aur phir sab computers share karte hain jo unhone seekha. Sab ke answers combine karke, AI faster seekhta hai.

Tricky part? Computers ko answers share karne ke liye ek doosre se baat karni padti hai (jaise tum homework answers compare karte ho), aur us baat karne mein waqt lagta hai. Agar woh zyada waqt baat karne mein lagaayein, toh akele kaam karne se bhi slow ho jaata hai!

  • Model parallelism: Model ko devices mein split karo

  • Overhead: Communication cost devices ke saath badhti hai

  • Devices: Zyada hamesha better nahi (scaling wall)

  • Efficiency: Actual speedup / ideal speedup

  • Layers: Pipeline layers ke hisaab se split karta hai

  • Hybrid: Data + model parallelism combine karta hai

  • Yield: 100B+ parameter models ke liye best

  • Batch: Gradient accumulation large batches simulate karta hai

  • Ring all-reduce: Efficient gradient sync topology

  • Interconnect: NVLink/InfiniBand model parallelism ke liye critical

  • Deterministic: Synchronous SGD single-GPU se match karta hai

Connections

  • 3.1-Neural-network-basics - forward/backward passes jo parallelize ho rahe hain
  • 3.3.5-Batch-normalization - data parallelism mein batch statistics
  • 3.9-Gradient-descent-optimizers - optimizer state sharding (ZeRO)
  • 3.4.1-Transformers-overview - GPT-3 ko model parallelism kyun chahiye
  • 4.2-GPU-acceleration - hardware jo distribution enable karta hai
  • 5.1-Training-at-scale - distributed systems ki practical engineering

#flashcards/ai-ml

What is the main goal of distributed training? :: Training speed up karna aur single-device memory se bade models enable karna, computation ko multiple GPUs/TPUs mein parallelize karke.

In data parallelism, what is synchronized across devices after each mini-batch?
Gradients. Har device apne data subset par gradients compute karta hai, phir all-reduce unhe average karta hai taaki sab devices same global gradient se update karein.
What is the formula for actual speedup with k devices and communication overhead?
jahan gradient synchronization time hai.
How does model parallelism differ from data parallelism?
Model parallelism model (parameters) ko devices mein split karta hai; har device network ka ek hissa rakhta hai. Data parallelism har device par full model replicate karta hai; devices alag data process karte hain.
What is pipeline parallelism?
Model ko layers ke hisaab se devices mein split karna. Data sequentially flow karta hai: Device 1 layers 1-10 process karta hai, output Device 2 ko jaata hai layers 11-20 ke liye, aur aage bhi.
Why does scaling efficiency typically decrease as number of devices increases?
Communication overhead constant rehta hai ya badhta hai, jabki per-device computation shrink hoti hai. Aakhirkar communication dominate kar leta hai.
What is gradient accumulation used for?
Bade batch sizes simulate karne ke liye jab GPU memory limited ho. Update karne se pehle multiple micro-batches ke gradients accumulate karo, jo ek bada batch process karne ke equivalent hai.
Synchronous vs asynchronous SGD trade-off?
Synchronous: deterministic, single-device ke mathematically equivalent, lekin slowest worker se limited. Asynchronous: better utilization, straggler wait nahi, lekin stale gradients convergence hurt kar sakte hain.
When should you use model parallelism over data parallelism?
Sirf tab jab model ek single device ki memory mein fit na ho. Model parallelism high communication cost add karta hai (har layer), toh jab bhi possible ho data parallelism prefer karo.
What is hybrid parallelism?
Data aur model parallelism ko combine karna. Example: Ek 100B model ko 8 GPUs mein split karo (model parallel), phir 8 baar nodes ke across replicate karo (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