Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Store a summary, not every number
Adam keeps two extra numbers for every weight, so its optimiser state is twice the size of the model. For a model with billions of parameters, that can be more memory than the model itself. Adafactor notices that for a weight matrix, the typical squared gradient of each entry is often roughly (something about its row) × (something about its column). So it stores just one number per row and one per column, and rebuilds the full table from those when needed.
Training and fine-tuning large models on limited GPU memory, or on accelerators with small memory, often hits the optimiser-state wall before the model-size wall. Adafactor trades a small approximation for an enormous memory saving, which can let a model fit on fewer devices or train with a larger batch.
- 1. Adafactor reduces…
- 2. For a weight matrix it stores…
- 3. Adafactor was used to train…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Row and column statistics: R ← β₂R + (1 − β₂)·rowmean(G²) and C ← β₂C + (1 − β₂)·colmean(G²).
- Reconstruction: V̂ = R Cᵀ/mean(R), a rank-1 matrix. Example: G² = [[1, 2], [3, 6]] = (1, 3)ᵀ(1, 2) is rank 1, and the reconstruction is exact.
- Memory: a 4096 × 4096 matrix needs 16 777 216 numbers for a full second moment, and only 8192 for the factored one: 2048 times less.
- Update clipping: scale the update U so that RMS(U) ≤ d (d = 1). For U = (3, 4), the RMS is √12.5 ≈ 3.54, so U is divided by 3.54.
- Relative step size: η = max(ε₂, RMS(W))·min(10⁻², 1/√t). Steps are proportional to the parameter’s own scale (the idea of Adadelta, E3-5) and decay as 1/√t after 10 000 steps.
- Momentum is optional (β₁ = 0 in the original paper) to save memory.
- 1. Factored state for a 1024 × 1024 matrix?
- 2. The reconstruction is exact when G² is…
- 3. Update clipping limits…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Adafactor in code
Exercise 1.
- The factored estimate reproduces a separable (rank-1) matrix exactly.
- For a random matrix with no row–column structure, its median relative error was 0.66.
- Factoring a 4096 × 4096 matrix cuts its state to 8192 numbers.
- Update clipping brings an update’s RMS to 1 without changing small updates.
- The relative step is 0.01 × RMS(W) early and 10⁻⁵ for a zero matrix after 10 000 steps (the ε₂ floor at work).
Practice.
- Hugging Face’s Adafactor supports
relative_step=True(no learning rate) or a fixed lr withrelative_step=False, scale_parameter=False. The latter is common for fine-tuning. - Vectors (biases, norms) are not factored; they keep a full second moment, which is cheap.
- Without momentum, training can be noisier. Adding β₁ = 0.9 restores Adam-like behaviour at the cost of one full-size buffer.
- For very large models, optimisers such as Lion (E3-16) and 8-bit Adam are other routes to lower memory.
from transformers.optimization import Adafactor
Adafactor(params, lr=1e-3, relative_step=False, scale_parameter=False, warmup_init=False)- 1. Are biases factored in Adafactor?
- 2. relative_step=True means…
- 3. Adding β₁ = 0.9 to Adafactor costs…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
The factorisation. Shazeer and Stern (2018) chose the rank-1 approximation that minimises the generalised Kullback–Leibler divergence to the non-negative matrix V. Its solution is exactly R Cᵀ/sum(R) with row and column sums (equivalently, means with the normalisation above). When V is separable, V_ij = r_i·c_j, the approximation is exact; otherwise it captures the row and column scale structure and smooths away entry-specific variation.
Stability measures.
- Without momentum and with β₂ close to 1, stale second moments can cause updates far larger than intended. Update clipping (RMS ≤ 1) bounds this.
- Adafactor also proposed a schedule β₂,ₜ = 1 − t^(−0.8), which grows toward 1, to keep v̂ fresh early.
Relative step size. Scaling the step by RMS(W) makes the update a fixed fraction of the parameter’s magnitude, the units argument of Adadelta. It removes the need to pick a learning rate per layer, but for parameters that start at zero it needs the floor ε₂.
Why a rougher estimate can train well. Exercise 2 found the factored version trained as well as or better than full per-entry normalisation, even where the approximation was rough. Averaging over a row and a column gives a less noisy denominator than a per-entry moving average, and per-entry normalisation is not always ideal: it forces every weight to move at about the same speed regardless of how far it has to go.
- 1. Adafactor's factorisation minimises…
- 2. Adafactor's β₂ schedule is…
- 3. The relative step is related to the idea of…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Does the factorisation cost accuracy?
History.
- N. Shazeer and M. Stern (2018) introduced Adafactor for training large transformer translation models.
- C. Raffel et al. (2020) trained T5 with it.
- Later memory-efficient optimisers include SM3 (Anil et al., 2019), 8-bit Adam (Dettmers et al., 2022), Lion (2023) and Sophia (2023).
Exercise 2. Multi-output linear regression with a 64 × 32 weight matrix. Either the gradient scales are separable (input features and outputs each have their own scale) or they vary entry by entry. We compare per-entry second moments (“Adam with β₁ = 0”) with Adafactor’s factored ones (also β₁ = 0), both trained for 1000 mini-batch steps with the learning rate tuned over 0.01, 0.03 and 0.1. The table gives the relative training error and the median relative error of the factored second moment against the full one.
| gradient scales | method | best η | relative error after 1000 steps | factored-estimate error |
|---|---|---|---|---|
| separable | Adam (β₁ = 0) | 0.1 | 0.0003 | 0.245 |
| separable | Adafactor (factored) | 0.03 | 0.0002 | 0.217 |
| entrywise | Adam (β₁ = 0) | 0.1 | 0.252 | 0.332 |
| entrywise | Adafactor (factored) | 0.1 | 0.144 | 0.305 |
- The approximation was better when scales were separable, as the theory says (0.22 against 0.31 median error). It is not exact because the gradients are noisy.
- With separable scales, both methods solved the problem (relative error about 0.0002–0.0003), so the factorisation cost nothing.
- With entrywise scales, Adafactor trained better (0.144 against 0.252), despite its rougher estimate. Its smoother, row-and-column denominator was an advantage here. Both methods’ best rate was the top of the grid, so both might improve with larger rates.
- Memory: the factored state was 96 numbers against 2048 here, and the saving grows with size (8192 against 16.8 million for a 4096 × 4096 matrix).
Adafactor in AI, robotics and aerospace
- Fine-tuning large models for mission-specific language or vision tasks on limited GPU memory.
- On-device and edge training, where optimiser state must fit in small memories.
- Large multi-task models across fleets of vehicles, where optimiser memory scales with parameter count.
References
- N. Shazeer, M. Stern (2018). “Adafactor: adaptive learning rates with sublinear memory cost.” Proc. ICML.
- C. Raffel et al. (2020). “Exploring the limits of transfer learning with a unified text-to-text transformer.” Journal of Machine Learning Research 21.
- R. Anil, V. Gupta, T. Koren, Y. Singer (2019). “Memory efficient adaptive optimization.” Advances in NeurIPS 32.
- T. Dettmers, M. Lewis, S. Shleifer, L. Zettlemoyer (2022). “8-bit optimizers via block-wise quantization.” Proc. ICLR.