use std::f32::consts::PI;
use super::Optimizer;
pub trait LrScheduler {
fn lr_at(&self, step: usize) -> f32;
fn apply(&self, optimizer: &mut dyn Optimizer, step: usize) -> f32 {
let lr = self.lr_at(step);
optimizer.set_lr(lr);
lr
}
}
#[derive(Debug, Clone, Copy)]
pub struct ConstantLr {
pub lr: f32,
}
impl LrScheduler for ConstantLr {
fn lr_at(&self, _step: usize) -> f32 {
self.lr
}
}
#[derive(Debug, Clone, Copy)]
pub struct StepLr {
pub base_lr: f32,
pub step_size: usize,
pub gamma: f32,
}
impl LrScheduler for StepLr {
fn lr_at(&self, step: usize) -> f32 {
self.base_lr * self.gamma.powi((step / self.step_size.max(1)) as i32)
}
}
#[derive(Debug, Clone, Copy)]
pub struct CosineAnnealingLr {
pub base_lr: f32,
pub min_lr: f32,
pub total_steps: usize,
}
impl LrScheduler for CosineAnnealingLr {
fn lr_at(&self, step: usize) -> f32 {
cosine(self.base_lr, self.min_lr, step, self.total_steps)
}
}
#[derive(Debug, Clone, Copy)]
pub struct WarmupCosineLr {
pub peak_lr: f32,
pub min_lr: f32,
pub warmup_steps: usize,
pub total_steps: usize,
}
impl WarmupCosineLr {
pub fn new(peak_lr: f32, min_lr: f32, warmup_steps: usize, total_steps: usize) -> Self {
Self {
peak_lr,
min_lr,
warmup_steps,
total_steps,
}
}
}
impl LrScheduler for WarmupCosineLr {
fn lr_at(&self, step: usize) -> f32 {
if step < self.warmup_steps {
return self.peak_lr * (step + 1) as f32 / self.warmup_steps as f32;
}
let decay_steps = self.total_steps.saturating_sub(self.warmup_steps);
cosine(
self.peak_lr,
self.min_lr,
step - self.warmup_steps,
decay_steps,
)
}
}
#[derive(Debug, Clone, Copy)]
pub struct WarmupLinearLr {
pub peak_lr: f32,
pub end_lr: f32,
pub warmup_steps: usize,
pub total_steps: usize,
}
impl LrScheduler for WarmupLinearLr {
fn lr_at(&self, step: usize) -> f32 {
if step < self.warmup_steps {
return self.peak_lr * (step + 1) as f32 / self.warmup_steps as f32;
}
let decay_steps = self.total_steps.saturating_sub(self.warmup_steps).max(1);
let progress = ((step - self.warmup_steps) as f32 / decay_steps as f32).min(1.0);
self.peak_lr + (self.end_lr - self.peak_lr) * progress
}
}
fn cosine(start: f32, end: f32, step: usize, steps: usize) -> f32 {
let progress = if steps == 0 {
1.0
} else {
(step as f32 / steps as f32).min(1.0)
};
end + (start - end) * 0.5 * (1.0 + (PI * progress).cos())
}
#[cfg(test)]
mod tests {
use super::*;
fn close(a: f32, b: f32) -> bool {
(a - b).abs() < 1e-7
}
#[test]
fn warmup_cosine_shape() {
let s = WarmupCosineLr::new(1.0, 0.1, 10, 110);
assert!(close(s.lr_at(0), 0.1));
assert!(close(s.lr_at(9), 1.0));
assert!(close(s.lr_at(10), 1.0));
assert!(close(s.lr_at(60), 0.55));
assert!(close(s.lr_at(110), 0.1));
assert!(close(s.lr_at(500), 0.1));
let decreasing = (10..110).all(|t| s.lr_at(t + 1) <= s.lr_at(t));
assert!(decreasing);
}
#[test]
fn step_and_linear_schedules() {
let s = StepLr {
base_lr: 1.0,
step_size: 10,
gamma: 0.5,
};
assert!(close(s.lr_at(9), 1.0) && close(s.lr_at(10), 0.5) && close(s.lr_at(25), 0.25));
let l = WarmupLinearLr {
peak_lr: 1.0,
end_lr: 0.0,
warmup_steps: 0,
total_steps: 4,
};
assert!(close(l.lr_at(2), 0.5) && close(l.lr_at(9), 0.0));
let c = CosineAnnealingLr {
base_lr: 1.0,
min_lr: 0.0,
total_steps: 0,
};
assert!(close(c.lr_at(0), 0.0));
}
}