Search

Search papers and lessons

Adafactor Optimizer

Level 4 · 12 min read

Adafactor is an adaptive optimizer that stores the per-parameter gradient statistics of a matrix as one row vector and one column vector, cutting memory from n times m numbers to n plus m.

On this page

  • Colour = topic mix
  • Linear algebra
  • Discrete math
  • Probability
  • Optimizers
  • Calculus
  • Losses & regularization
  • ReadReady to readNot yet

TL;DR

Adafactor is an optimizer for training large neural networks that adapts the step size of every weight, as Adam does, but needs far less memory. For each weight matrix it stores only the row sums and column sums of the running average of squared gradients, then rebuilds the full table from those two vectors when it is needed. It also drops the momentum term by default and caps the size of each update, which keeps training stable.

Builds on: Adam Optimizer, Low-Rank Matrix, Outer Product · Leads to: the Adafactor paper

Where it's used

  • Training large language models. Models with billions of weights are mostly weight matrices. An optimizer that keeps two full-size accumulators for every weight, as Adam does, doubles the memory needed beyond the weights themselves. Adafactor was designed for this case, which arises when training large Transformer models.
  • Translation experiments at the scale of the original paper. The 2018 paper tested Adafactor on the Transformer machine translation model, measuring quality with BLEU, the standard score for translated text. The factored method matched Adam's quality in the runs that were compared, while storing much less state.
  • Settings where memory limits the batch size. Freeing memory from the optimizer state leaves room for a larger batch on the same hardware, which is often the difference between a run that fits and one that does not.

How it works

Adafactor's central idea is that the table of squared gradients for a weight matrix is well approximated by a rank-one grid, meaning every entry is one row value times one column value, so two vectors are enough to rebuild it. Here is one step on a tiny 2×22 \times 2 matrix, with the squared gradients taken as (1234)\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} for simplicity.

flowchart LR
  G[Gradient matrix G] --> S[Square each entry]
  S --> R[Keep row sums R]
  S --> C[Keep column sums C]
  R --> V[Rebuild table as R times C divided by total]
  C --> V
  V --> U[Divide the gradient by the square root, entry by entry]
  U --> K[Cap the update size, then apply it]
  1. Square each gradient entry. The squared gradients are (1234)\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}. Check: four numbers, one per weight.
  2. Add up each row to get R=(3,7)R = (3, 7), and add up each column to get C=(4,6)C = (4, 6). These are the only statistics stored for this matrix. Check: RR sums to 10 and CC sums to 10, the same total.
  3. Rebuild the full table as the outer product RC⊤R C^\top divided by the total 10. The first entry is 3×4/10=1.23 \times 4 / 10 = 1.2. Check: the rebuilt entries are 1.2,1.8,2.8,4.21.2, 1.8, 2.8, 4.2, and they still sum to 10.
  4. Compare with the true table. The true first entry was 1 and the estimate is 1.2. The true last entry was 4 and the estimate is 4.2. The estimate is close but not exact, which is the cost of storing two vectors. Check: each estimate is within 0.2 of its true value here.
  5. Divide each gradient entry by the square root of its rebuilt estimate, so every weight gets its own step size. Check: this is the same division Adam performs, with a rebuilt table instead of a stored one.
  6. Measure the size of this update as its root-mean-square, the square root of the average of its squared entries. If it is larger than a set threshold, scale the whole update down before applying it. Check: a threshold of 1 means no update may have root-mean-square size above 1.

Two more choices separate Adafactor from Adam. The first is that Momentum, the running average of the gradient itself, is off by default, which saves a second full-size accumulator. The second is that the decay of the squared-gradient average changes with the step number, so the average forgets old gradients at a steady rate.

The math

Let GtG_t be the gradient of a weight matrix X∈Rn×mX \in \mathbb{R}^{n \times m} at step tt, and let 1m\mathbf{1}_m be a column of mm ones. The squared gradients are taken entrywise.

Definition: Factored second-moment estimate

The estimate of the squared-gradient average Vt∈Rn×mV_t \in \mathbb{R}^{n \times m} is built from its row sums Rt∈RnR_t \in \mathbb{R}^{n} and column sums Ct∈RmC_t \in \mathbb{R}^{m}:

V^t=Rt Ct⊤1n⊤Rt.\hat{V}_t = \frac{R_t \, C_t^\top}{\mathbf{1}_n^\top R_t}.

Here RtCt⊤R_t C_t^\top is an outer product, which is an n×mn \times m grid of products RiCjR_i C_j. Dividing by 1n⊤Rt\mathbf{1}_n^\top R_t, the sum of all entries of VtV_t, keeps the total of the estimate equal to the total of the true table.

Step 1 in symbols: the running row and column sums

The row sums and column sums are updated with the same exponential decay that Adam uses:

Rt=β2,t Rt−1+(1−β2,t) (Gt∘2)1m,Ct=β2,t Ct−1+(1−β2,t) 1n⊤(Gt∘2),R_t = \beta_{2,t} \, R_{t-1} + (1 - \beta_{2,t}) \, (G_t^{\circ 2}) \mathbf{1}_m, \qquad C_t = \beta_{2,t} \, C_{t-1} + (1 - \beta_{2,t}) \, \mathbf{1}_n^\top (G_t^{\circ 2}),

where Gt∘2G_t^{\circ 2} is the matrix of squared gradients. Storing RtR_t and CtC_t takes n+mn + m numbers, while storing the full table takes nmnm.

Step 2 in symbols: rebuilding the table

Apply the definition to get V^t\hat{V}_t. In the worked walkthrough, R=(3,7)R = (3, 7) and C=(4,6)C = (4, 6) with total 1⊤R=10\mathbf{1}^\top R = 10, so

V^=110(37)(46)=(1.21.82.84.2).\hat{V} = \frac{1}{10}\begin{pmatrix} 3 \\ 7 \end{pmatrix}\begin{pmatrix} 4 & 6 \end{pmatrix} = \begin{pmatrix} 1.2 & 1.8 \\ 2.8 & 4.2 \end{pmatrix}.

Step 3 in symbols: the decay schedule

The paper uses a decay that depends on the step number,

β2,t=1−t−0.8,\beta_{2,t} = 1 - t^{-0.8},

which is 0 at t=1t = 1, so the first estimate is just the first squared gradient, and which approaches 1 slowly as tt grows. The paper calls the value β2,t\beta_{2,t} the second-moment decay.

Step 4 in symbols: the update and its clipping

Let Ut=Gt/V^tU_t = G_t / \sqrt{\hat{V}_t} be the unscaled update, entry by entry. Its root-mean-square size is

RMS(Ut)=Mean(Ut∘2).\mathrm{RMS}(U_t) = \sqrt{\mathrm{Mean}\left(U_t^{\circ 2}\right)}.

With a threshold dd, the clipped update is

U^t=Utmax⁡(1, RMS(Ut)/d),\hat{U}_t = \frac{U_t}{\max\left(1,\ \mathrm{RMS}(U_t)/d\right)},

and the parameter moves by αtU^t\alpha_t \hat{U}_t, where αt\alpha_t is the learning rate. The clipping only shrinks an update that is too large, and it leaves small updates unchanged.

Worked examples

Example
Example 1: a bias vector, where nothing is lost

A bias vector has m=1m = 1 column, so its squared gradients form a single column (4,1,0)⊤(4, 1, 0)^\top.

  1. Row sums: R=(4,1,0)R = (4, 1, 0). Column sum: C=(5)C = (5). The total is 1⊤R=5\mathbf{1}^\top R = 5.
  2. Rebuild: V^i=Ri⋅5/5=Ri\hat{V}_i = R_i \cdot 5 / 5 = R_i, so V^=(4,1,0)\hat{V} = (4, 1, 0).
  3. Compare: the rebuilt table equals the true table exactly.
  4. Interpretation: when a parameter has one column, the rank-one rebuild is exact. The factored form costs nothing in accuracy for vectors, which is why Adafactor treats them with the ordinary per-entry rule.
Example
Example 2: the standard $2 \times 2$ case

Use the squared gradients (1234)\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} from the walkthrough.

  1. Row sums: R=(1+2,3+4)=(3,7)R = (1 + 2, 3 + 4) = (3, 7).
  2. Column sums: C=(1+3,2+4)=(4,6)C = (1 + 3, 2 + 4) = (4, 6).
  3. Total: 3+7=103 + 7 = 10, which also equals 4+64 + 6.
  4. Rebuild the (1,1)(1,1) entry: R1C1/10=3×4/10=1.2R_1 C_1 / 10 = 3 \times 4 / 10 = 1.2. The true value is 11.
  5. Rebuild the (2,2)(2,2) entry: R2C2/10=7×6/10=4.2R_2 C_2 / 10 = 7 \times 6 / 10 = 4.2. The true value is 44.
  6. Rebuild the other two: (1,2)(1,2) gives 3×6/10=1.83 \times 6 / 10 = 1.8 against a true 22, and (2,1)(2,1) gives 7×4/10=2.87 \times 4 / 10 = 2.8 against a true 33.
  7. Check: the rebuilt entries sum to 1.2+1.8+2.8+4.2=101.2 + 1.8 + 2.8 + 4.2 = 10, which matches the true total.
  8. Interpretation: each rebuilt entry is off by a fraction of the true value. At 2×22 \times 2 the memory saving is zero, since n+m=nm=4n + m = nm = 4, so the example shows the approximation, not the saving.
Example
Example 3: a realistic matrix, with the memory saving

Take a single weight matrix of a language model of size 4096×40964096 \times 4096, the size of an attention projection in a large model.

  1. Full second-moment table: nm=4096×4096=16,777,216nm = 4096 \times 4096 = 16{,}777{,}216 numbers.
  2. Factored form: n+m=4096+4096=8,192n + m = 4096 + 4096 = 8{,}192 numbers.
  3. Ratio: 16,777,216/8,192=204816{,}777{,}216 / 8{,}192 = 2048. The factored form stores about 2000 times fewer numbers for this matrix.
  4. Quality effect: in the paper's Transformer runs, the factored second moment gave results similar to Adam's, which the paper reports as similar across the tested cases.
  5. Update clipping: in the paper's Table 2, the unclipped baseline (row A) reached BLEU 0.1 without warm-up. Row J, which uses the factored estimate and clips at threshold 1.0, reached BLEU 22.4 in the same setting. With warm-up, both rows reached BLEU 25.6.
  6. Interpretation: the memory saving is large for big matrices, and clipping is what restored stable training when the step size was not warmed up.

Try it: a matrix of size 2048×20482048 \times 2048 has how many numbers in its factored form, and how many in its full table? Answer: the factored form stores 2048+2048=40962048 + 2048 = 4096 numbers, and the full table stores 2048×2048=4,194,3042048 \times 2048 = 4{,}194{,}304.

Common mistakes and tradeoffs

It is tempting to think Adafactor is a cheap approximation that gives worse training. In fact the paper's comparisons on the Transformer showed results similar to Adam with the full table, across the cases it tested. The loss of accuracy in the rebuilt table did not translate into a visible loss in those experiments.

It is tempting to think a single-vector summary works as well. In fact the paper tried using only row means or only column means in place of the full rebuild. The row-mean version was close to baseline, but the column-mean version was much worse, which the paper attributes partly to a weight matrix shared between input embeddings and output probabilities. The outer product of both sums is what makes the rebuild reliable.

It is tempting to think no momentum means no memory of past gradients. In fact the squared-gradient average still remembers past steps; only the average of the gradient itself, which is Momentum, is removed. Momentum can be added back if wanted, at the cost of a second full-size accumulator.

It is tempting to think update clipping is the same as gradient clipping. In fact gradient clipping caps the gradient before the per-parameter scaling, while update clipping caps the update after scaling. An adaptive method can produce a large update from a small gradient, so only capping the update keeps the step bounded.

Use Adafactor when optimizer memory is the bottleneck, especially for large weight matrices. Be cautious when the model's parameters have very uneven gradient sizes across rows and columns, since the rebuild assumes the rank-one structure holds well enough. Alternatives include Adam with the full table when memory allows, and simpler methods such as RMSProp when per-parameter adaptation is needed without the matrix structure.

History

Adam, published in 2014 and presented at ICLR in 2015, made per-parameter adaptive step sizes the default for deep learning, but it stores two full-size accumulators for every weight. As models grew into the billions of parameters, that state became a real cost in memory.

Shazeer and collaborators first mentioned a preliminary version of the factored idea in 2017, in an appendix to a paper on very large neural networks. Shazeer and Stern wrote the full method up in 2018 as Adafactor. The paper also cites a related 2014 technique by Gupta and colleagues, which saved memory by averaging the accumulators of an adaptive method across embedding vectors.

The method caught on because it solved a concrete problem at scale: the optimizer state for large models was too large to keep in accelerator memory alongside the weights, and Adafactor removed most of it without a large loss in training quality.

Recap

  • Adam stores two full-size accumulators per weight, which becomes costly for large models.
  • For a weight matrix, Adafactor stores only the row sums and column sums of the squared-gradient average, n+mn + m numbers instead of nmnm.
  • It rebuilds the table as the outer product of those sums divided by their total, then divides the gradient by the square root of that estimate.
  • It turns off momentum by default, lets the squared-gradient decay depend on the step, and caps the size of each update.
  • Read next: the Adafactor paper, which introduces the method and its experiments.

Check yourself

QuestionIn one sentence, what does Adafactor store for a weight matrix instead of a full table of squared-gradient averages?Show answer

It stores the row sums and the column sums of the running squared-gradient average, which together have n+mn + m numbers, and it rebuilds an estimate of the full table from them when needed.

QuestionFor squared gradients (2648)\begin{pmatrix} 2 & 6 \\ 4 & 8 \end{pmatrix}, what is the rebuilt estimate of the entry in row 1, column 2?Show answer

Row 1 sum is 2+6=82 + 6 = 8. Column 2 sum is 6+8=146 + 8 = 14. The total is 2+6+4+8=202 + 6 + 4 + 8 = 20. The estimate is 8×14/20=5.68 \times 14 / 20 = 5.6, against a true value of 6.

QuestionWhy does Adafactor use update clipping instead of only clipping the gradient?Show answer

Adaptive methods divide the gradient by a per-parameter scale, so a small gradient can still produce a large update when that scale is small. Capping the gradient does not bound the update, so the paper caps the size of the update itself, which keeps each step bounded even after scaling.

QuestionA matrix has 1000 rows and 3000 columns. How many numbers does the factored form store, and what fraction of the full table is that?Show answer

The factored form stores 1000+3000=40001000 + 3000 = 4000 numbers. The full table has 1000×3000=3,000,0001000 \times 3000 = 3{,}000{,}000 numbers, so the factored form is 4000/3,000,000≈0.00134000 / 3{,}000{,}000 \approx 0.0013 of the full table, which is the same as one 750th of its size.

Sources