How does gradient accumulation let you simulate a larger batch size under a memory limit?

Quick Overview

Asks how gradient accumulation trains with a large effective batch when only a small batch fits in GPU memory. Tests loss scaling across micro-batches, which memory depends on batch size, and where accumulation differs from a true large batch in practice.

How does gradient accumulation let you simulate a larger batch size under a memory limit?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

How does gradient accumulation let you simulate a larger batch size under a memory limit? ```hint Which memory grows Split training memory into the parts that scale with batch size and the parts that do not, and see which part accumulation keeps small. ``` ```hint Matching the large batch Check what happens to `.grad` when `backward()` runs twice without zeroing, and what you must do to the loss so the result equals the large-batch gradient. ``` ### What a Strong Answer Covers - The mechanics: several micro-batches, summed gradients, one optimizer step per group - How to scale the loss so the accumulated gradient equals the mean over the large batch - Which memory depends on batch size and why accumulation keeps it low - Where the result differs from a true large batch, plus practical details such as normalization layers, distributed training and learning-rate schedules ### Follow-up Questions - How would you scale the loss when micro-batches contain different numbers of tokens? - Why is accumulation not equivalent to a large batch for a contrastive loss with in-batch negatives? - Where does gradient clipping go in an accumulation loop, and what changes with mixed precision? - If the large batch fits in memory directly, is there any reason to keep using accumulation?

Overview: Asks how gradient accumulation trains with a large effective batch when only a small batch fits in GPU memory. Tests loss scaling across micro-batches, which memory depends on batch size, and where accumulation differs from a true large batch in practice.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

How does gradient accumulation let you simulate a larger batch size under a memory limit?

What a Strong Answer Covers Guidance

  • The mechanics: several micro-batches, summed gradients, one optimizer step per group
  • How to scale the loss so the accumulated gradient equals the mean over the large batch
  • Which memory depends on batch size and why accumulation keeps it low
  • Where the result differs from a true large batch, plus practical details such as normalization layers, distributed training and learning-rate schedules

Follow-up Questions Guidance

  • How would you scale the loss when micro-batches contain different numbers of tokens?
  • Why is accumulation not equivalent to a large batch for a contrastive loss with in-batch negatives?
  • Where does gradient clipping go in an accumulation loop, and what changes with mixed precision?
  • If the large batch fits in memory directly, is there any reason to keep using accumulation?