Skip to content

What is gradient accumulation and how does it simulate larger batch sizes on small GPUs?

Beginner · 2 min read · Fine-Tuning & Adaptation

Open in study mode to save this question and track progress

Short answer: Gradient accumulation accumulates calculated gradients across KK micro-batches during successive forward/backward passes without updating model parameters; after KK steps, optimizer.step() updates weights once, simulating an effective batch size K×K\times larger than hardware VRAM normally fits.

Detail

Training LLMs requires large effective batch sizes (e.g. 128 or 256) for stable gradient estimates.

If a single GPU VRAM limit permits fitting a micro-batch size of only 4 sequences, gradient accumulation bridges the gap:

Effective Batch Size=Per-Device Micro-Batch Size×Gradient Accumulation Steps×Number of GPUs\text{Effective Batch Size} = \text{Per-Device Micro-Batch Size} \times \text{Gradient Accumulation Steps} \times \text{Number of GPUs}

Micro-Batch 1 (Size 4) ──► Forward + Backward ──► Accumulate Gradients (No weight update)
Micro-Batch 2 (Size 4) ──► Forward + Backward ──► Accumulate Gradients (No weight update)
Micro-Batch 3 (Size 4) ──► Forward + Backward ──► Accumulate Gradients (No weight update)
Micro-Batch 4 (Size 4) ──► Forward + Backward ──► Accumulate Gradients
                                                      │
                                                      ▼
                            `optimizer.step()` (Effective Batch Size = 16)

Example

PyTorch implementation pattern for gradient accumulation:

python
import torch

model = torch.nn.Linear(10, 2)
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3)
accumulation_steps = 4

optimizer.zero_grad()
for step, (inputs, targets) in enumerate(dataset_loader):
    outputs = model(inputs)
    loss = criterion(outputs, targets) / accumulation_steps # Scale loss
    loss.backward() # Accumulate gradients

    if (step + 1) % accumulation_steps == 0:
        optimizer.step() # Apply accumulated gradient updates
        optimizer.zero_grad() # Reset accumulated gradients

Interview tips

  • Emphasize dividing the loss by accumulation_steps before loss.backward() so accumulated gradients represent the true mathematical average across the full effective batch.
  • Discuss how gradient accumulation trades compute time for VRAM memory.

Related Concepts


⬅ Back to Fine-Tuning and Adaptation · All topics