下列文本提取自本地原版 PDF。图表和公式的准确布局请对照 PDF。
4.3 AdamW
Modern language models are typically trained with more sophisticated optimizers, instead of SGD. Most
optimizers used recently are derivatives of the Adam optimizer [D. P. Kingma et al., 2015]. We will use
AdamW [I. Loshchilov et al., 2019], which is in wide use in recent work. AdamW proposes a modification
to Adam that improves regularization by adding weight decay (at each iteration, we pull the parameters
towards 0), in a way that is decoupled from the gradient update. We will implement AdamW as described
in algorithm 2 of I. Loshchilov et al. [23].
AdamW is stateful: for each parameter, it keeps track of a running estimate of its first and second
moments. Thus, AdamW uses additional memory in exchange for improved stability and convergence.
Besides the learning rate 𝛼, AdamW has a pair of hyperparameters (𝛽1 , 𝛽2 ) that control the updates to
the moment estimates, and a weight decay rate 𝜆. Typical applications set (𝛽1 , 𝛽2 ) to (0.9, 0.999), but
large language models like LLaMA [H. Touvron et al., 2023] and GPT-3 [T. B. Brown et al., 2020] are
often trained with (0.9, 0.95). The algorithm can be written as follows, where 𝜀 is a small value (e.g.,
10−8 ) used to improve numerical stability in case we get extremely small values in 𝑣:
Algorithm 1: AdamW Optimizer
1 init(𝜃) ▷ Initialize learnable parameters
2 𝑚←0 ▷ Initial value of the first moment vector; same shape as 𝜃
3 𝑣←0 ▷ Initial value of the second moment vector; same shape as 𝜃
4 for 𝑡 = 1, …, 𝑇 do
5 Sample batch of data 𝐵𝑡
6 𝑔 ← ∇𝜃 ℓ(𝜃; 𝐵𝑡 ) ▷ Compute the gradient of the loss
√1−𝛽𝑡
7 𝛼𝑡 ← 𝛼 1−𝛽𝑡 2 ▷ Compute adjusted 𝛼 for iteration 𝑡
1
8 𝜃 ← 𝜃 − 𝛼𝜆𝜃 ▷ Apply weight decay
9 𝑚 ← 𝛽1 𝑚 + (1 − 𝛽1 )𝑔 ▷ Update the first moment estimate
10 𝑣 ← 𝛽2 𝑣 + (1 − 𝛽2 )𝑔2 ▷ Update the second moment estimate
𝑚
11 𝜃 ← 𝜃 − 𝛼𝑡 √𝑣+𝜀 ▷ Apply moment-adjusted weight updates
12 end for
Algorithm 1: AdamW optimizer pseudocode.
Note that 𝑡 starts at 1. You will now implement this optimizer.
Problem (adamw): Implement AdamW (2 points)
Deliverable: Implement the AdamW optimizer as a subclass of torch.optim.Optimizer. Your
class should take the learning rate 𝛼 in __init__, as well as the 𝛽, 𝜀 and 𝜆 hyperparameters. To
help you keep state, the base Optimizer class gives you a dictionary self.state, which maps
nn.Parameter objects to a dictionary that stores any information you need for that parameter (for
AdamW, this would be the moment estimates). Implement [adapters.get_adamw_cls] and make
sure it passes uv run pytest -k test_adamw.
Problem (adamw_accounting): Resource accounting for training with AdamW (2 points)
Let us compute how much memory and compute running AdamW requires. Assume we are using
float32 for every tensor.
(a) How much peak memory does running AdamW require? Decompose your answer based on the
memory usage of the parameters, activations, gradients, and optimizer state. Express your
answer in terms of the batch_size and the model hyperparameters (vocab_size,
context_length, num_layers, d_model, num_heads). Assume 𝑑ff = 83 × 𝑑model .
For simplicity, when calculating memory usage of activations, consider only the following
components:
• Transformer block
‣ RMSNorm(s)
‣ Multi-head self-attention sublayer: 𝑄𝐾𝑉 projections, 𝑄𝐾 ⊤ matrix multiply, softmax,
weighted sum of values, output projection.
‣ Position-wise feed-forward (SwiGLU): 𝑊1 , 𝑊2 , SiLU on the gate branch, element-wise
product, 𝑊3
• final RMSNorm
• output embedding
• cross-entropy on logits
Deliverable: An algebraic expression for each of parameters, activations, gradients, and
optimizer state, as well as the total.
(b) Instantiate your answer for a GPT-2 XL-shaped model to get an expression that only
depends on the batch_size. What is the maximum batch size you can use and still fit within
80GB memory?
Deliverable: An expression that looks like 𝑎 ⋅ batch_size + 𝑏 for numerical values 𝑎, 𝑏, and a
number representing the maximum batch size.
(c) How many FLOPs does running one step of AdamW take?
Deliverable: An algebraic expression, with a brief justification.
(d) Model FLOPs utilization (MFU) is defined as the ratio of observed throughput (tokens per
second) relative to the hardware’s theoretical peak FLOP throughput
[A. Chowdhery et al., 2022]. An NVIDIA H100 GPU has a theoretical peak of 495 teraFLOP/
s for “float32” (actually TensorFloat-32, which in reality is “bfloat19”) operations. Assuming
you are able to get 50% MFU, how long would it take to train a GPT-2 XL for 400K steps
and a batch size of 1024 on a single H100? Following J. Kaplan et al. [25] and
J. Hoffmann et al. [26], assume that the backward pass has twice the FLOPs of the forward
pass.
Deliverable: The number of hours training would take, with a brief justification.
本章讨论
在 GitHub 查看记录 ↗