Log inStart free
LESSON 2.33 · 6 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Gradient accumulation

Watch small microbatches add up to one full-batch step in limited memory.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 3 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 2.33 · ACCUMULATION BAYB = 8 · NEEDS 8 · GPU HOLDS 4
01
Stage 1 of 6 · Full batch
A batch of 8 examples gives one gradient, the mean of 8 example gradients. The GPU holds the activations of only 4 examples.
Explain it simply

The model is a line. Training nudges its two numbers, w and c. Each of the 8 examples gives its own gradient. The batch gradient is their average: the dashed red arrow in the buffer panel. One pass over all 8 must keep all their activations, but the memory holds only 4. So the tall block spills over in red.

Eight example cells. A memory block 8 examples tall passes the limit of 4. On the loss map, the full-batch step from θ₀ follows the gradient -1.97, 1.

examples, activationsθ = (w, c), memory limitgradients, buffer, overflowloss contours
batch of 8 examplesactivations in GPU memorygradient bufferloss map L(w, c)1234567804802468time: examples processedout of memoryGPU limit: 4 examples∂L/∂w∂L/∂cfull batch g = (−1.97, 1)empty: no pass has run yet−101234−2−10123w (slope) →c (intercept)minimumfull-batch stepθ₀batch of 8 examplesactivations in GPU memorygradient bufferloss map L(w, c)1234567804802468time: examples processedout of memoryGPU limit: 4 examples∂L/∂w∂L/∂cfull batch g = (−1.97, 1)empty: no pass has run yet−101234−2−10123w (slope) →c (intercept)minimumfull-batch stepθ₀
Toy: line fit ŷ = w·x + c · 8 examples · memory for 4 · η = 0.5
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
g=1B∑i=1B∇ℓi\sy{g}{grad}{g} = \frac{1}{\sy{B}{data}{B}} \sum_{i=1}^{B} \sy{gi}{grad}{\nabla \ell_i}
g=18(∇ℓ1+⋯+∇ℓ8)=(−1.97, 1);memory: 8>4g = \tfrac18(\nabla\ell_1 + \dots + \nabla\ell_8) = (-1.97,\ 1);\quad \text{memory: } 8 > 4

The batch gradient is the mean of the example gradients.

Common mix-ups

MYTHA step after each microbatch is the same as accumulation.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01With 8 microbatches of 1 example, where does the chain of red arrows end?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 3 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.