BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Optimizers15 of 18 in topic

254. AdamW with linear warmup and cosine decay

HardOptimizers Pro
Asked atMeta

LLM pre-training uses AdamW plus a learning-rate schedule: ramp up linearly, then decay along a cosine curve.

Schedule for step t (1-based) with peak lr_max, warmup W, total T:
- t <= W: lr = lr_max * t / W
- otherwise: lr = lr_min + 0.5 (lr_max - lr_min)(1 + cos(pi (t - W) / (T - W)))

AdamW step: do the normal Adam update with that lr (b1=0.9, b2=0.999, eps=1e-8), and separately apply decoupled weight decay w -= lr * wd * w, using w from before this step.

Write train_adamw(grad_fn, w0, lr_max, lr_min, W, T, wd). Run T steps and return (final_w, list_of_lrs).

Functiondef train_adamw(grad_fn, w0, lr_max, lr_min, W, T, wd)
Play with it

Linear warmup, then cosine decay

The usual LLM training schedule: ramp up linearly for the warmup steps, then follow half a cosine down to the minimum.

00.512004006008001000steplearning rate
step
300
learning rate
0.89472
peak on chart
0.999996

Drag across the chart to read any step.

Example 1

Input: train_adamw(lambda w: 2 * w, [1.0], 0.1, 0.0, 1, 3, 0.0)

Output: ([0.8502], [0.1, 0.05, 0.0])

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Compute lr first.

Hint 2

Then m, v, bias-corrected step, and w_new = w - lr * (m_hat / (sqrt(v_hat) + eps) + wd * w).

Common mistakes
  • Adding wd * w into the gradient (the buggy version, which is L2 regularisation) lets Adam's scaling cancel most of the decay.
Understand the idea

Large models train with a learning-rate schedule: start small and ramp up linearly (warmup) while Adam's statistics settle, then glide down along half a cosine to lr_min. AdamW also shrinks weights directly by lr x wd x w, separate from the gradient, which keeps the decay from being cancelled by Adam's per-parameter scaling.

Worked example

lr_max = 0.001, lr_min = 0, W = 2, T = 6. t = 1: 0.0005. t = 2: 0.001. t = 3: progress (3 - 2)/(6 - 2) = 0.25, cos(pi x 0.25) = 0.707, lr = 0.5 x 0.001 x 1.707 = 0.000854. t = 4: cos(pi/2) = 0, lr = 0.0005. t = 5: 0.000146. t = 6: cos(pi) = -1, lr = 0. Decay example: w = 2, lr = 0.001, wd = 0.1 removes 0.0002.

warmup lr = lr_max t / W; after it lr = lr_min + 0.5 (lr_max - lr_min)(1 + cos(pi (t - W) / (T - W))); Adam step plus w -= lr wd w_old

LLM pre-training commonly uses exactly this setup.

Topics
Optimizers
Companies1
  • Meta the topic appears in interview reports Machine Learning interview questions, page 10 (KatKat)

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Minimise a quadratic with AdamW, bias correction and a no-decay maskHard
  • Compute Adafactor's factored second momentHard
  • Implement one step of the Adam optimizerMedium
  • Fine-tune on a memory budget: implement the Lion optimizer stepMedium
All Optimizers problems
Report
254 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np, math
​
def train_adamw(grad_fn, w0, lr_max, lr_min, W, T, wd):
# your code here
pass
​
train_adamw(_g, [0.0, 0.0], 0.1, 0.01, 3, 10, 0.1)[1]

This case uses generated data, so it runs as written. Press + to add your own case.

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS