Fine-tuning, RLHF & Alignment·Question 38 of 61Advanced

Explain gradient accumulation, gradient checkpointing, and ZeRO/FSDP - and how you'd combine them for a real training run.

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

Three orthogonal tools attacking different memory terms:

Gradient accumulation (attacks: activation memory from batch size): run k micro-batch forward/backwards, summing gradients, and step the optimizer once - effective batch = micro_batch × k × data_parallel_world. Numerically ~equivalent to the big batch (mind loss normalization across micro-batches, and note batch-statistics subtleties don't apply to LayerNorm-based transformers). Cost: none in memory, k× more steps per optimizer update - it trades wall-clock for memory only insofar as small micro-batches underutilise the GPU.

Full worked answer

Three orthogonal tools attacking different memory terms:

Gradient accumulation (attacks: activation memory from batch size): run k micro-batch forward/backwards, summing gradients, and step the optimizer once - effective batch = micro_batch × k × data_parallel_world. Numerically ~equivalent to the big batch (mind loss normalization across micro-batches, and note batch-statistics subtleties don't apply to LayerNorm-based transformers). Cost: none in memory, k× more steps per optimizer update - it trades wall-clock for memory only insofar as small micro-batches underutilise the GPU.

Gradient checkpointing (attacks: activation memory from depth/sequence): instead of storing every intermediate activation for backward, keep only checkpoints (typically layer inputs) and recompute the rest during the backward pass. Roughly +30% compute for an order-of-magnitude activation reduction; it's the switch that makes long-sequence fine-tuning feasible on small cards.

ZeRO / FSDP (attacks: the static 16 bytes/param): with plain DDP every GPU holds a full replica of weights, grads, and optimizer states. ZeRO shards them across data-parallel ranks - Stage 1: optimizer states; Stage 2: + gradients; Stage 3: + parameters themselves (gathered on the fly per layer, then freed). PyTorch's FSDP is the native ZeRO-3-style implementation (DeepSpeed is the other mainstream stack). Cost: communication volume rises with stage; ZeRO-3/FSDP adds all-gathers on every forward/backward. CPU/NVMe offload variants push states off-GPU at further speed cost.

Combining them - e.g., full fine-tune of a 7B on 8×80 GB: FSDP full sharding (static state ~112 GB / 8 ≈ 14 GB/GPU) + gradient checkpointing (activations under control at 4k sequence length) + micro-batch sized to fill remaining memory + accumulation up to the target effective batch (~64-128 sequences). Mention bf16 throughout, and that if it still doesn't fit, the escalation is offload → QLoRA → more GPUs. Knowing which lever maps to which memory term is the actual signal here.

What a good answer covers

  • Gradient accumulation (attacks
  • Numerically ~equivalent to the big batch (mind loss normalization across micro-batches, and note…
  • Cost: none in memory, k× more steps per optimizer update - it trades wall-clock for memory only insofar…
  • Gradient checkpointing (attacks
  • Roughly +30% compute for an order-of-magnitude activation reduction; it's the switch that makes…

Key terms: Gradient accumulation · Gradient checkpointing · ZeRO / FSDP · Combining them

Interviewer follow-ups to expect

  1. Why does ZeRO-1 add almost no communication overhead while ZeRO-3 does?

  2. When is gradient checkpointing a bad trade?

  3. What changes when you add LoRA to this setup?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep