6.1.9 · HinglishScaling & Efficient Architectures

FSDP and sharded training

2,270 words10 min readRead in English

6.1.9 · AI-ML › Scaling & Efficient Architectures

FSDP Actually Kya Karta Hai

Fully Sharded Data Parallel ek memory optimization strategy hai jo teen key components ko shard (partition) karta hai data-parallel workers mein:

  1. Model parameters ()
  2. Gradients ()
  3. Optimizer states (momentum, variance for Adam)

Traditional data parallelism har device par teeno ki full copies rakhta hai. FSDP sirf rakhta hai har ek ka, devices mein se har ek par.

FSDP Kaise Kaam Karta Hai: Teen-Phase Protocol

Phase 1: Forward Pass

Har layer ki computation se pehle:

  1. Device ke paas shard hai layer ke liye
  2. All-gather operation: saare devices se collect karo
  3. Device memory mein temporarily poora reconstruct karo
  4. Layer ka forward pass compute karo:
  5. Borrowed shards discard karo, sirf rakho

Yeh step kyun? Har layer ko apne saare parameters chahiye hote hain activations sahi se compute karne ke liye. Hum matrix multiply split nahi kar sakte agar hamare paas sirf aadha ho.

Phase 2: Backward Pass

Har layer ke liye reverse mein:

  1. ko dobara all-gather karo (gradient computation ke liye zaroori)
  2. Local gradients compute karo:
  3. Gradients ko reduce-scatter karo: har device ko sirf milta hai (apne shard ka gradient)
  4. Poora phir se discard karo

Reduce-scatter kyun? Poora gradient compute karne ke baad, hum devices ke contributions ko sum karte hain (reduce) aur chunks distribute karte hain (scatter), taaki har device sirf apna shard update kare.

Phase 3: Optimizer Step

Har device independently apna shard update karta hai:

Optimizer states bhi sharded hain, isliye har device sirf store karta hai momentum/variance buffers ka.

Communication Primitives

FSDP do collective operations par rely karta hai:

ZeRO ke Saath Comparison

FSDP, PyTorch ka implementation hai ZeRO (Zero Redundancy Optimizer) ka, jo Microsoft DeepSpeed se aaya hai:

  • ZeRO Stage 1: Sirf optimizer states shard karo → memory reduction
  • ZeRO Stage 2: Optimizer states + gradients shard karo → memory reduction
  • ZeRO Stage 3: Optimizer states + gradients + parameters shard karo → memory reduction (FSDP = ZeRO-3)

Teen stages kyun? Har stage zyada communication overhead introduce karta hai. ZeRO-1 moderate models ke liye faster hai; ZeRO-3 sirf tab zaroori hai jab models single-GPU memory se bade hon.

Hybrid Sharding Strategies

Modern frameworks hybrid parallelism allow karte hain:

FSDP + Pipeline Parallelism:

  • Model ko stages mein partition karo (layers 1-12 GPU group A par, layers 13-24 GPU group B par)
  • Har stage ke andar FSDP se shard karo
  • Benefit: All-gather scope ko intra-stage devices tak reduce karo

FSDP + Tensor Parallelism:

  • Bade layers (jaise 12,288 × 12,288 weight matrix) ko ek node ke andar GPUs mein split karo
  • Nodes ke across FSDP se parameters shard karo
  • Benefit: Computation ke liye tensor parallelism, memory ke liye FSDP

Effective parallelism ka formula:

Implementation Details

Wrapping granularity: FSDP individual layers, modules, ya poore model ko wrap kar sakta hai. Finer wrapping temporary memory kam karta hai (chhote all-gather buffers) lekin communication frequency badh jaati hai.

Typical strategy:

model = TransformerModel(...)
for layer in model.transformer_layers:
    layer = FSDP(layer)  # Wrap each transformer block
model = FSDP(model)  # Outer wrap for top-level

Mixed precision: FSDP shards fp32 mein store karta hai (optimizer ke liye) lekin fp16 mein communicate karta hai (bandwidth ke liye). All-gather, send karne se pehle fp32 → fp16 convert karta hai.

CPU offloading: Kuch FSDP implementations inactive shards ko CPU RAM mein offload karne ki allow karte hain, GPU memory ke liye PCIe bandwidth trade karte hue. Severe latency penalties ki wajah se rarely use hota hai.

Recall Ek 12-Saal Ke Bachche Ko Samjhao

Socho tum aur 7 doston ek bada LEGO castle bana rahe ho (AI model). Castle itna bada hai ki ek insaan saare pieces ek saath nahi pakad sakta.

Normal tarika (data parallel): Aap mein se har ek ke paas saare pieces ki ek complete copy hai. Aap sabhi ek hi castle 8 baar banate ho. Yeh bahut zyada jagah waste karta hai!

FSDP tarika: Aap pieces ko 8 boxes mein divide karte ho. Tum box 1 rakho, tumhara dost box 2 rakhta hai, aur aage bhi aisa hi. Jab tumhe tower banana hota hai (jiske liye saari boxes ke pieces chahiye), sab log apni boxes circle mein jaldi se pass karte hain taaki tum zaroori pieces le sako. Tower banane ke baad, extra boxes waapas de do.

Yeh kyun help karta hai? Tumhe permanently sirf 1/8 pieces store karne ki zaroorat hai. Passing-around mein thoda time lagta hai, lekin yeh 8× shelf space khareedne se kaafi better hai. Bahut bade AI models ke liye (jaise ChatGPT ke bade bhai), yeh "pieces share karna" wala trick hi unhe banane ka ek maatra tarika hai, bina impossibly expensive computers khareed ke.

Connections

  • Data Paralelism — FSDP, data parallelism ko sharding ke saath extend karta hai
  • Model Parallelism — FSDP orthogonal hai; dono combine kar sakte hain
  • Mixed Precision Training — FSDP fp16 communication, fp32 computation leverage karta hai
  • Gradient Accumulation — Per-step communication kam karne ke liye microbatches par accumulate karo
  • Activation Checkpointing — Activation memory ke liye FSDP ka essential complement
  • ZeRO Optimizer — FSDP, DeepSpeed ka ZeRO-3 implement karta hai
  • Communication Collectives — All-gather, reduce-scatter FSDP ke primitives hain
  • Pipeline Parallelism — Hybrid: pipeline + FSDP within stages

#flashcards/ai-ml

What are the three components FSDP shards across devices? :: Model parameters, gradients, aur optimizer states (momentum, variance).

What collective operation does FSDP use to reconstruct full parameters forward pass?
All-gather (har device apna shard saare doosron ko broadcast karta hai).
What is the memory reduction factor for ZeRO-3/FSDP with N devices?
Persistent memory ke liye approximately reduction (parameters + gradients + optimizer states).
Why does FSDP discard borrowed parameter shards after each layer?
Peak memory usage minimize karne ke liye—full parameters rakhne se sharding ka faayda khatam ho jaata.
What is the difference between ZeRO-2 and ZeRO-3?
ZeRO-2 optimizer states + gradients shard karta hai. ZeRO-3 additionally model parameters bhi shard karta hai.
When should you NOT use FSDP?
Jab model standard data parallelism ke saath GPU memory mein fit ho jaaye, kyunki FSDP ka communication overhead throughput kam karta hai.
What memory component does FSDP not shard?
Activations (woh per-device rehte hain aur batch size ke saath scale karte hain).
What is the communication cost of all-gather for a shard of size S on N devices?
per device (har device doosron se N-1 shards receive karta hai).
What technique complements FSDP to handle activation memory?
Activation checkpointing (store karne ki jagah backward pass mein activations recompute karo).
In hybrid FSDP + pipeline parallelism, what does FSDP shard?
Har pipeline stage ke andar parameters/gradients/optimizer states (stages ke across nahi).

Concept Map

too costly for large models

uses

shards

enables

kept as 1 over N per device

needed for

forward uses

backward uses

produces shard gradient

feeds

discard borrowed shards

Data Parallel replicates full model

FSDP Fully Sharded Data Parallel

Sharding partition tensor across N devices

Shards params grads optimizer states

All-gather reconstruct full layer

Forward pass compute activations

Reduce-scatter distribute gradient chunks

Backward pass compute gradients

Optimizer step update local shard

Memory per device 24P over N