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 matrix, with the squared gradients taken as 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]
- Square each gradient entry. The squared gradients are . Check: four numbers, one per weight.
- Add up each row to get , and add up each column to get . These are the only statistics stored for this matrix. Check: sums to 10 and sums to 10, the same total.
- Rebuild the full table as the outer product divided by the total 10. The first entry is . Check: the rebuilt entries are , and they still sum to 10.
- 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.
- 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.
- 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 be the gradient of a weight matrix at step , and let be a column of ones. The squared gradients are taken entrywise.
The estimate of the squared-gradient average is built from its row sums and column sums :
Here is an outer product, which is an grid of products . Dividing by , the sum of all entries of , 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:
where is the matrix of squared gradients. Storing and takes numbers, while storing the full table takes .
Step 2 in symbols: rebuilding the table
Apply the definition to get . In the worked walkthrough, and with total , so
Step 3 in symbols: the decay schedule
The paper uses a decay that depends on the step number,
which is 0 at , so the first estimate is just the first squared gradient, and which approaches 1 slowly as grows. The paper calls the value the second-moment decay.
Step 4 in symbols: the update and its clipping
Let be the unscaled update, entry by entry. Its root-mean-square size is
With a threshold , the clipped update is
and the parameter moves by , where is the learning rate. The clipping only shrinks an update that is too large, and it leaves small updates unchanged.
Worked examples
A bias vector has column, so its squared gradients form a single column .
- Row sums: . Column sum: . The total is .
- Rebuild: , so .
- Compare: the rebuilt table equals the true table exactly.
- 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.
Use the squared gradients from the walkthrough.
- Row sums: .
- Column sums: .
- Total: , which also equals .
- Rebuild the entry: . The true value is .
- Rebuild the entry: . The true value is .
- Rebuild the other two: gives against a true , and gives against a true .
- Check: the rebuilt entries sum to , which matches the true total.
- Interpretation: each rebuilt entry is off by a fraction of the true value. At the memory saving is zero, since , so the example shows the approximation, not the saving.
Take a single weight matrix of a language model of size , the size of an attention projection in a large model.
- Full second-moment table: numbers.
- Factored form: numbers.
- Ratio: . The factored form stores about 2000 times fewer numbers for this matrix.
- 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.
- 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.
- 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 has how many numbers in its factored form, and how many in its full table? Answer: the factored form stores numbers, and the full table stores .
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, numbers instead of .
- 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 numbers, and it rebuilds an estimate of the full table from them when needed.
QuestionFor squared gradients , what is the rebuilt estimate of the entry in row 1, column 2?Show answer
Row 1 sum is . Column 2 sum is . The total is . The estimate is , 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 numbers. The full table has numbers, so the factored form is of the full table, which is the same as one 750th of its size.