Search

Search papers and lessons

Adafactor: Adaptive Learning Rates with Sublinear Memory Cost

Noam Shazeer, Mitchell Stern · 2018

Adafactor trains large neural networks with adaptive step sizes while storing far fewer optimizer numbers than Adam, matching Adam's translation quality in the authors' Transformer experiments.

On this page

  • Colour = topic mix
  • Linear algebra
  • Discrete math
  • Probability
  • Optimizers
  • Calculus
  • Information theory
  • ReadReady to readNot yet

TL;DR

Adafactor is an optimizer for training neural networks. It scales each update by a running estimate of the squared gradients, as Adam does, but it stores that estimate for a weight matrix as one row total and one column total instead of one number per weight. It also drops momentum, clips unusually large updates, and sets step sizes relative to the size of the weights. On a machine translation task, the authors report quality comparable to Adam while using very little extra memory. Builds on: Adam Optimizer, Matrix Factorization, Kullback–Leibler Divergence · Leads to: Adafactor Optimizer, T5

Why it mattered

Adafactor became one of the standard optimizers for training large language and translation models with billions of weights. The reason is memory. Adam keeps two extra numbers per weight, so its optimizer state alone is twice the size of the weights. Adafactor's factored store removes most of that cost for the big weight matrices that dominate a Transformer, which lets the same hardware train a larger model or a larger batch. The T5 family of models (T5) was trained with Adafactor, which is the clearest sign of its practical uptake. The paper's own claim is narrower: on one benchmark, Adafactor matches the quality of Adam with momentum while storing much less.

The problem

Adaptive optimizers such as RMSProp and Adam divide each gradient by the square root of a running average of past squared gradients, one average per weight. This gives every weight its own effective step size, which helps on noisy, badly scaled problems. The cost is memory: for a weight matrix with nn rows and mm columns, the squared-gradient average has nmnm entries, as large as the matrix itself. Adam also keeps a momentum average of the gradient, so it stores two extra matrices. The paper points out that computing power for training grew much faster than memory, so this overhead became a real limit on model size.

Two further issues come up when one tries to remove that memory. Without momentum, Adam becomes unstable when the learning rate is not warmed up. The authors trace this to a second-moment average that is too slow to follow a model that is changing quickly, so the updates become too large. The paper addresses both issues and also asks for step sizes that match the scale of each parameter.

How it works

Here is the method in the order it runs, for one weight matrix XX of size n×mn \times m in a training step.

  1. Receive a gradient. Compute the gradient GtG_t of the loss with respect to XX. It is an n×mn \times m matrix of the same size as XX.
  2. Square and sum the gradient. Compute Gt2G_t^2 element by element, then add a tiny constant ϵ1\epsilon_1. Keep only two sums: the sum of each row, a vector RtR_t of length nn, and the sum of each column, a vector CtC_t of length mm. Each is a moving average over steps, with decay β^2,t\hat{\beta}_{2,t}.
  3. Rebuild an estimate of the full average. Form the rank-one matrix whose entry in row ii and column jj is Rt,iCt,j/1⊤RtR_{t,i} C_{t,j} / \mathbf{1}^{\top}R_t. The denominator is the total of all the row sums. This is the only full-size object, and it is never stored.
  4. Normalise the gradient. Divide each entry of GtG_t by the square root of the corresponding estimate from step 3. The result is UtU_t, the unscaled update.
  5. Clip the update. Measure the root-mean-square of UtU_t. If it is larger than the threshold d=1d = 1, shrink the whole update so its root-mean-square equals dd. This is update clipping.
  6. Set the step from the weights. Compute the step size as αt=max⁡(ϵ2,RMS(Xt−1))⋅ρt\alpha_t = \max(\epsilon_2, \mathrm{RMS}(X_{t-1})) \cdot \rho_t, where ρt\rho_t is the relative step size and ϵ2=10−3\epsilon_2 = 10^{-3}. Larger weights get larger steps.
  7. Update. Set Xt=Xt−1−αtU^tX_t = X_{t-1} - \alpha_t \hat{U}_t. There is no momentum term.
flowchart LR
  A[Gradient G_t] --> B[Row sums R_t and column sums C_t of G_t squared]
  B --> C[Rank-one estimate of the second moment]
  C --> D[Normalised update U_t]
  D --> E[Clip the update to RMS d]
  E --> F[Scale by the weight size and the relative step]
  F --> G[New weights X_t]

Here is a tiny instance of step 3. Take a 2×22 \times 2 matrix of averaged squared gradients V=(1234)V = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}. The row sums are 33 and 77, the column sums are 44 and 66, and the total is 1010. The rank-one estimate is (1.21.82.84.2)\begin{pmatrix} 1.2 & 1.8 \\ 2.8 & 4.2 \end{pmatrix}. Its row sums are 33 and 77 and its column sums are 44 and 66, the same as VV. The estimate keeps the margins of VV but cannot keep every entry, and that is the approximation the method accepts.

The math

Step 3: the best rank-one approximation

The paper compares two ways to measure how far a nonnegative approximation RSRS is from a nonnegative matrix VV. One is the Frobenius norm, which leads to the singular value decomposition, but its truncated factors can be negative and do not combine across time steps the way moving averages need. The other is the generalized Kullback-Leibler divergence, also called the I-divergence, which for nonnegative scalars is

d(p,q)=plog⁡pq−p+q.d(p, q) = p \log \frac{p}{q} - p + q.

It is nonnegative and equals zero only when p=qp = q. The paper minimises ∑i,jd(Vij,[RS]ij)\sum_{i,j} d(V_{ij}, [RS]_{ij}) over nonnegative R∈Rn×1R \in \mathbb{R}^{n \times 1} and S∈R1×mS \in \mathbb{R}^{1 \times m}, which is the rank-one case.

The paper's Lemma 1 gives the solution in closed form:

RS=V1m 1n⊤V1n⊤V1m.RS = \frac{V \mathbf{1}_m \, \mathbf{1}_n^{\top} V}{\mathbf{1}_n^{\top} V \mathbf{1}_m}.

Here 1ℓ\mathbf{1}_\ell is a column of ℓ\ell ones. The factor V1mV\mathbf{1}_m is the vector of row sums, and 1n⊤V\mathbf{1}_n^{\top}V is the row of column sums. The proof sets the derivatives of the loss with respect to RiR_i and SjS_j to zero, which gives Ri=∑jVij/∑jSjR_i = \sum_j V_{ij} / \sum_j S_j and Sj=∑iVij/∑iRiS_j = \sum_i V_{ij} / \sum_i R_i. The pair (αR,S/α)(\alpha R, S/\alpha) gives the same loss for any α>0\alpha \gt 0, so the paper fixes the scale by choosing RR to be the row sums. This is the canonical solution.

The formula is linear in VV. Because the row sums of a moving average equal the moving average of the row sums, the factored store can be updated with the same recursion as Adam's second moment. That is why the method is compatible with exponential smoothing.

Algorithm 2 in symbols: the factored update

The accumulators are Rt=β2Rt−1+(1−β2)(Gt2)1mR_t = \beta_2 R_{t-1} + (1 - \beta_2) (G_t^2) \mathbf{1}_m and Ct=β2Ct−1+(1−β2)1n⊤(Gt2)C_t = \beta_2 C_{t-1} + (1 - \beta_2) \mathbf{1}_n^{\top}(G_t^2). The full estimate is V^t=RtCt/(1n⊤Rt)\hat{V}_t = R_t C_t / (\mathbf{1}_n^{\top} R_t), divided by the bias correction 1−β2t1 - \beta_2^t as in Adam. Memory per matrix is n+mn + m numbers instead of nmnm. For a 1000×10001000 \times 1000 weight matrix that is 20002000 numbers instead of 1,000,0001{,}000{,}000.

Section 7 in symbols: the decay schedule

The paper writes Adam's second moment as a weighted sum of past squared gradients, v^t=∑i=1t(1−β^2,i)(∏j=i+1tβ^2,j)gi2\hat{v}_t = \sum_{i=1}^{t} (1 - \hat{\beta}_{2,i}) \left(\prod_{j=i+1}^{t} \hat{\beta}_{2,j}\right) g_i^2. If the gradients have a stable mean square, the expected estimate equals the true mean square when the weights add up to one, which holds for any schedule with β^2,1=0\hat{\beta}_{2,1} = 0. The paper proposes the schedule

β^2,t=1−t−c,c>0.\hat{\beta}_{2,t} = 1 - t^{-c}, \qquad c \gt 0.

It starts at 00 when t=1t = 1 and rises toward 11. Past gradients must also lose their weight as training goes on, which requires ∑j≥21/jc\sum_{j \ge 2} 1/j^{c} to diverge. That holds only when c≤1c \le 1. When c=1c = 1 the estimate is the plain average of all past squared gradients. The paper's recommended value is c=0.8c = 0.8, which gives β^2,t=1−t−0.8\hat{\beta}_{2,t} = 1 - t^{-0.8}.

Section 6 in symbols: update clipping

Let RMS(Ut)\mathrm{RMS}(U_t) be the root-mean-square of the entries of UtU_t, which is near 1 when the second-moment estimate is accurate. 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)}.

When RMS(Ut)≤d\mathrm{RMS}(U_t) \le d, nothing changes. When it is larger, the update is scaled so that its root-mean-square is exactly dd. The paper uses d=1d = 1.

Results

The experiments train the Transformer of Vaswani et al. on the WMT 2014 English-to-German translation task, using the Tensor2Tensor code. Scores are BLEU on the newstest2013 development set, with beam size 4 and length penalty 0.6. Models are trained for 100,000 steps, with batches of about 4,096 input tokens and 4,096 target tokens. The authors note that these batches are about 8 times smaller than those in the original Transformer paper, and that each run took less than two hours on one Google TPU v2. Because the regime is shorter and the code differs, the authors say the scores should not be compared with the original paper's numbers.

The main findings, from the paper's Table 2 and text:

  • Factored second moments matched the baseline. Adam with full second moments scored 25.6 BLEU with warmup. Adam with factored second moments and momentum off scored 25.6 with warmup as well (row J, with clipping).
  • Momentum was not needed with warmup. Adam with momentum scored 25.4 with warmup, against 25.6 without momentum (rows A and B).
  • Without warmup, Adam without momentum became unstable and scored 0.1 BLEU. Adding update clipping to the factored version raised its no-warmup score from 0.2 to 22.4 (rows C and J). Row N, which uses the decay schedule and clipping with full second moments, scored 25.9 with warmup and 22.4 without.
  • Using only row means or only column means gave different results: row means were similar to the baseline, while column means were much worse. The authors suspect that a shared embedding and output matrix, where frequent tokens have much larger gradients, is the cause.
  • Relative step sizes made Adafactor more resilient to how embedding parameters were initialised and scaled. In Table 3, Adam scored 26.4 with the standard embedding scheme and 24.2 with a naive scheme that uses no multiplier, while Adafactor scored 26.6 and 25.4 in the same two settings.
  • Plain SGD with tuned learning rates was poorer and less stable, and some settings diverged.

The limits are as important as the results. There is one model and one translation task. The paper reports that results are "comparable" to Adam, not better. The authors state that it is not clear whether relative step sizes improve stability, because the step sizes in the experiments are not directly comparable. The update-clipping result for a threshold of 2 was no better than no clipping. The memory saving is a property of the storage, not a proof of faster training.

Reading guide

Check yourself

QuestionFor a weight matrix with 512 rows and 2048 columns, how many numbers does Adam's second-moment store need, and how many does the factored store need?Show answer

Adam stores one number per weight, so 512×2048=1,048,576512 \times 2048 = 1{,}048{,}576 numbers. The factored store keeps one row total per row and one column total per column, so 512+2048=2560512 + 2048 = 2560 numbers. The ratio is about 400 to 1.

QuestionTake the matrix V=(2240)V = \begin{pmatrix} 2 & 2 \\ 4 & 0 \end{pmatrix}. What rank-one estimate does the paper's formula give, and what are its row sums?Show answer

The row sums are 44 and 44. The column sums are 66 and 22. The total is 88. Each entry of the estimate is row sum times column sum divided by total: the top-left entry is 4⋅6/8=34 \cdot 6 / 8 = 3, the top-right is 4⋅2/8=14 \cdot 2 / 8 = 1, and the bottom row is 33 and 11 by the same rule. The estimate is (3131)\begin{pmatrix} 3 & 1 \\ 3 & 1 \end{pmatrix}. Its row sums are 44 and 44, which match the row sums of VV, as the margin-preserving property requires.

QuestionWhy does the paper require c≤1c \le 1 in the schedule β^2,t=1−t−c\hat{\beta}_{2,t} = 1 - t^{-c}?Show answer

The weight of a gradient from step ii in the second-moment estimate is multiplied by a product of decay terms over all later steps. For that weight to fade to zero as training continues, the sum ∑j≥21/jc\sum_{j \ge 2} 1/j^{c} must diverge, and that requires c≤1c \le 1. If cc is larger, the sum converges, and very old gradients keep a fixed share of the estimate forever, so the estimate can go out of date again.

QuestionA team wants to use Adafactor in place of Adam to train a model with very large embedding tables that are shared with the output layer. What does the paper suggest they check, and why?Show answer

The paper found that using only column means of the second-moment estimate worked much worse than the full factored estimate in the shared embedding case, and it suspected that frequent tokens dominate the gradient magnitudes in that layer. The team should compare the factored estimate with the full one on their own model, watch training for instability, and consider relative step sizes together with the embedding initialisation and scaling, since the paper showed the choice matters.

Sources

Read the full paper

Noam Shazeer, Mitchell Stern, 2018. arXiv:1804.04235