Skip to content

What is learning rate scheduling (cosine decay) during LLM fine-tuning?

Beginner · 2 min read · Fine-Tuning & Adaptation

Open in study mode to save this question and track progress

Short answer: Learning rate scheduling dynamically adjusts the optimizer's learning rate (η\eta) during training; cosine decay warms up the learning rate linearly for initial steps before gradually decaying it along a cosine curve to a small minimum learning rate, stabilizing LLM convergence.

Detail

Constant learning rates cause training instability: too high a learning rate early on causes loss divergence, while too high a learning rate late in training prevents the model from settling into optimal loss minima.

Learning Rate (η)
  ▲
  │     /─── Cosine Decay Curve ───\
  │    /                            \
  │   / (Linear Warmup)              \___ (Min LR)
  └───────────────────────────────────────────────► Training Steps

The Cosine Decay Formula with Warmup

For step tt, warmup steps TwarmupT_{warmup}, and total steps TtotalT_{total}:

ηt=ηmin+12(ηmax−ηmin)(1+cos⁡(t−TwarmupTtotal−Twarmupπ))\eta_t = \eta_{min} + \frac{1}{2}(\eta_{max} - \eta_{min}) \left(1 + \cos\left(\frac{t - T_{warmup}}{T_{total} - T_{warmup}} \pi\right)\right)

Why Cosine Decay is Preferred

  • Warmup Phase: Prevents large initial gradients from destabilizing pre-trained weight matrices.
  • Smooth Decay: Cosine curvature decays slower than exponential decay mid-training, maintaining optimization velocity before gently dampening updates near convergence.

Example

PyTorch Cosine Annealing scheduler configuration pattern:

python
import torch
from torch.optim.lr_scheduler import CosineAnnealingLR

optimizer = torch.optim.AdamW([torch.nn.Parameter(torch.randn(10))], lr=2e-5)
scheduler = CosineAnnealingLR(optimizer, T_max=1000, eta_min=2e-6)

# Simulate training loop
for step in range(5):
    optimizer.step()
    scheduler.step()
    print(f"Step {step+1} Learning Rate: {scheduler.get_last_lr()[0]:.2e}")

Interview tips

  • Emphasize typical LLM fine-tuning learning rates: 1e-4−3e-41\text{e-}4 - 3\text{e-}4 for LoRA fine-tuning vs 1e-5−2e-51\text{e-}5 - 2\text{e-}5 for full parameter SFT.
  • Discuss warmup ratios (typically 3%−5%3\% - 5\% of total training steps).

Related Concepts


⬅ Back to Fine-Tuning and Adaptation · All topics