optirs-core 0.3.2

OptiRS core optimization algorithms and utilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Cyclic learning rate scheduler
//
// This module provides cyclic learning rate scheduling, which cycles the learning rate
// between two boundaries with a constant frequency.

use crate::error::{OptimError, Result};
use crate::schedulers::LearningRateScheduler;
use scirs2_core::ndarray::ScalarOperand;
use scirs2_core::numeric::Float;
use std::fmt;

/// Convert a `usize` exponent into `i32`, saturating instead of wrapping.
fn exponent_i32(v: usize) -> i32 {
    i32::try_from(v).unwrap_or(i32::MAX)
}

/// Convert a `usize` counter into the scheduler's float type.
fn from_usize<A: Float>(v: usize) -> A {
    A::from(v).unwrap_or_else(A::zero)
}

/// Convert a `usize` denominator into the scheduler's float type.
///
/// Falls back to `1` so the value can never introduce a division by zero.
fn denom_from_usize<A: Float>(v: usize) -> A {
    match A::from(v) {
        Some(x) if x != A::zero() => x,
        _ => A::one(),
    }
}

/// Cyclic learning rate policy
#[derive(Debug, Clone, Copy)]
pub enum CyclicMode {
    /// Triangular mode: linear scaling between min and max
    Triangular,
    /// Triangular2 mode: linear scaling with halved amplitude each cycle
    Triangular2,
    /// Exponential range: the amplitude is scaled by `gamma^global_step`
    ExpRange(f64),
}

/// Cyclic learning rate scheduler
///
/// This scheduler cycles the learning rate between two boundaries with a constant frequency.
/// It's based on the paper "Cyclical Learning Rates for Training Neural Networks" by Leslie N. Smith.
///
/// # Modes
///
/// * [`CyclicMode::Triangular`] - constant amplitude.
/// * [`CyclicMode::Triangular2`] - the amplitude is halved after every full cycle.
/// * [`CyclicMode::ExpRange`] - the amplitude is multiplied by `gamma^global_step`. The
///   exponent is the **global** iteration counter, so the decay carries across cycles
///   (it used to be reset at every cycle boundary, which made the mode a no-op).
///
/// # Validation
///
/// The constructors never fail: a `step_size` of `0` is clamped to `1` so the schedule can
/// never divide by zero. Use [`CyclicLR::try_new`] to reject an invalid configuration
/// instead.
///
/// # Example
///
/// ```
/// use optirs_core::schedulers::{CyclicLR, CyclicMode, LearningRateScheduler};
///
/// let mut scheduler = CyclicLR::new(0.001, 0.01, 2000, CyclicMode::Triangular);
///
/// // Learning rate cycles between 0.001 and 0.01 over 2000 steps
/// for step in 0..6000 {
///     let lr = scheduler.get_learning_rate();
///     // Use lr for optimization
///     scheduler.step();
/// }
/// ```
pub struct CyclicLR<A: Float> {
    base_lr: A,
    max_lr: A,
    /// Number of iterations per half cycle (always >= 1)
    step_size: usize,
    mode: CyclicMode,
    gamma: A,
    current_step: usize,
    scale_fn: Box<dyn Fn(usize, usize, A, A) -> A + Send + Sync>,
}

impl<A: Float + std::fmt::Debug + Send + Sync> fmt::Debug for CyclicLR<A> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CyclicLR")
            .field("base_lr", &self.base_lr)
            .field("max_lr", &self.max_lr)
            .field("step_size", &self.step_size)
            .field("mode", &self.mode)
            .field("gamma", &self.gamma)
            .field("current_step", &self.current_step)
            .field("scale_fn", &"<function>")
            .finish()
    }
}

impl<A: Float + ScalarOperand + std::fmt::Debug + Send + Sync> CyclicLR<A> {
    /// Create a new cyclic learning rate scheduler
    ///
    /// # Arguments
    ///
    /// * `base_lr` - Minimum learning rate
    /// * `max_lr` - Maximum learning rate
    /// * `step_size` - Number of training iterations per half cycle. `0` is invalid and is
    ///   clamped to `1`; use [`CyclicLR::try_new`] to reject it instead.
    /// * `mode` - Cycling mode (Triangular, Triangular2, or ExpRange)
    pub fn new(base_lr: A, max_lr: A, step_size: usize, mode: CyclicMode) -> Self {
        let step_size = step_size.max(1);
        let gamma = match mode {
            CyclicMode::ExpRange(g) => A::from(g).unwrap_or_else(A::one),
            _ => A::one(),
        };

        let scale_fn: Box<dyn Fn(usize, usize, A, A) -> A + Send + Sync> = match mode {
            CyclicMode::Triangular => Box::new(|_, _, _, _| A::one()),
            CyclicMode::Triangular2 => Box::new(|current, cycle_half, _, _| {
                let cycle_len = cycle_half.saturating_mul(2).max(1);
                let two = A::from(2).unwrap_or_else(A::one);
                A::one() / two.powi(exponent_i32(current / cycle_len))
            }),
            // The exponent is the GLOBAL step count so the decay accumulates across
            // cycles instead of restarting at every cycle boundary.
            CyclicMode::ExpRange(_) => {
                Box::new(|current, _cycle_half, gamma, _| gamma.powi(exponent_i32(current)))
            }
        };

        Self {
            base_lr,
            max_lr,
            step_size,
            mode,
            gamma,
            current_step: 0,
            scale_fn,
        }
    }

    /// Create a new cyclic learning rate scheduler, validating the configuration
    ///
    /// # Errors
    ///
    /// Returns [`OptimError::InvalidConfig`] when
    /// * `step_size == 0`,
    /// * `base_lr` or `max_lr` is not finite,
    /// * `base_lr < 0` or `max_lr < base_lr`, or
    /// * the mode is [`CyclicMode::ExpRange`] with a gamma outside `(0, 1]`.
    pub fn try_new(base_lr: A, max_lr: A, step_size: usize, mode: CyclicMode) -> Result<Self> {
        if step_size == 0 {
            return Err(OptimError::InvalidConfig(
                "CyclicLR requires step_size > 0".to_string(),
            ));
        }
        if !base_lr.is_finite() || !max_lr.is_finite() {
            return Err(OptimError::InvalidConfig(
                "CyclicLR requires finite base_lr and max_lr".to_string(),
            ));
        }
        if base_lr < A::zero() {
            return Err(OptimError::InvalidConfig(
                "CyclicLR requires base_lr >= 0".to_string(),
            ));
        }
        if max_lr < base_lr {
            return Err(OptimError::InvalidConfig(
                "CyclicLR requires max_lr >= base_lr".to_string(),
            ));
        }
        if let CyclicMode::ExpRange(g) = mode {
            if !g.is_finite() || g <= 0.0 || g > 1.0 {
                return Err(OptimError::InvalidConfig(format!(
                    "CyclicLR ExpRange requires gamma in (0, 1], got {g}"
                )));
            }
        }

        Ok(Self::new(base_lr, max_lr, step_size, mode))
    }

    /// Create a new triangular cyclic scheduler
    pub fn triangular(base_lr: A, max_lr: A, step_size: usize) -> Self {
        Self::new(base_lr, max_lr, step_size, CyclicMode::Triangular)
    }

    /// Create a new triangular2 cyclic scheduler
    pub fn triangular2(base_lr: A, max_lr: A, step_size: usize) -> Self {
        Self::new(base_lr, max_lr, step_size, CyclicMode::Triangular2)
    }

    /// Create a new exponential range cyclic scheduler
    pub fn exp_range(base_lr: A, max_lr: A, step_size: usize, gamma: f64) -> Self {
        Self::new(base_lr, max_lr, step_size, CyclicMode::ExpRange(gamma))
    }

    /// Set custom scale function
    ///
    /// The closure receives `(global_step, step_size, gamma, 1)` and returns the amplitude
    /// scale factor for the current step.
    pub fn with_scale_fn<F>(mut self, scale_fn: F) -> Self
    where
        F: Fn(usize, usize, A, A) -> A + Send + Sync + 'static,
    {
        self.scale_fn = Box::new(scale_fn);
        self
    }

    /// Number of iterations per half cycle (always >= 1)
    pub fn step_size(&self) -> usize {
        self.step_size
    }

    /// Get the current cycle number
    pub fn get_cycle(&self) -> usize {
        self.current_step / self.cycle_len()
    }

    /// Full cycle length in steps (always >= 2)
    fn cycle_len(&self) -> usize {
        self.step_size.saturating_mul(2).max(1)
    }

    /// Get position within current cycle (0.0 to 1.0)
    pub fn get_cycle_position(&self) -> A {
        let cycle_len = self.cycle_len();
        let cycle_position = self.current_step % cycle_len;
        if cycle_position < self.step_size {
            // First half: increasing
            from_usize::<A>(cycle_position) / denom_from_usize::<A>(self.step_size)
        } else {
            // Second half: decreasing
            from_usize::<A>(cycle_len - cycle_position) / denom_from_usize::<A>(self.step_size)
        }
    }
}

impl<A: Float + ScalarOperand + std::fmt::Debug + Send + Sync> LearningRateScheduler<A>
    for CyclicLR<A>
{
    fn get_learning_rate(&self) -> A {
        let position = self.get_cycle_position();
        let scale = (self.scale_fn)(self.current_step, self.step_size, self.gamma, A::one());

        let amplitude = (self.max_lr - self.base_lr) * scale;
        self.base_lr + amplitude * position
    }

    fn step(&mut self) -> A {
        self.current_step = self.current_step.saturating_add(1);
        self.get_learning_rate()
    }

    fn reset(&mut self) {
        self.current_step = 0;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;

    #[test]
    fn test_triangular_cyclic() {
        let base_lr = 0.001;
        let max_lr = 0.01;
        let step_size = 100;

        let mut scheduler = CyclicLR::triangular(base_lr, max_lr, step_size);

        // At start, should be base_lr
        assert_relative_eq!(scheduler.get_learning_rate(), base_lr, epsilon = 1e-6);

        // At half cycle, should be max_lr
        for _ in 0..step_size {
            scheduler.step();
        }
        assert_relative_eq!(scheduler.get_learning_rate(), max_lr, epsilon = 1e-6);

        // At full cycle, should be back to base_lr
        for _ in 0..step_size {
            scheduler.step();
        }
        assert_relative_eq!(scheduler.get_learning_rate(), base_lr, epsilon = 1e-6);
    }

    #[test]
    fn test_triangular2_cyclic() {
        let base_lr = 0.001;
        let max_lr = 0.01;
        let step_size = 100;

        let mut scheduler = CyclicLR::triangular2(base_lr, max_lr, step_size);

        // First cycle
        for _ in 0..step_size {
            scheduler.step();
        }
        let first_max = scheduler.get_learning_rate();
        assert_relative_eq!(first_max, max_lr, epsilon = 1e-6);

        // Move to second cycle max
        for _ in 0..(2 * step_size) {
            scheduler.step();
        }
        let second_max = scheduler.get_learning_rate();

        // Second cycle should have half amplitude
        assert_relative_eq!(
            second_max,
            base_lr + (max_lr - base_lr) / 2.0,
            epsilon = 1e-6
        );
    }

    #[test]
    fn test_exp_range_cyclic() {
        let base_lr = 0.001;
        let max_lr = 0.01;
        let step_size = 100;
        let gamma = 0.99995;

        let mut scheduler = CyclicLR::exp_range(base_lr, max_lr, step_size, gamma);

        // Test that learning rate decreases exponentially within each cycle
        let lr_start = scheduler.get_learning_rate();

        for _ in 0..10 {
            scheduler.step();
        }

        let lr_10_steps = scheduler.get_learning_rate();

        // Learning rate should increase in first half of cycle
        assert!(lr_10_steps > lr_start);

        // But the increase should be modulated by gamma
        assert!(lr_10_steps < base_lr + (max_lr - base_lr) * 0.1);
    }

    #[test]
    fn test_exp_range_decay_is_global_not_per_cycle() {
        let base_lr = 0.001;
        let max_lr = 0.01;
        let step_size = 5;
        let gamma = 0.9;

        let mut scheduler = CyclicLR::exp_range(base_lr, max_lr, step_size, gamma);

        for _ in 0..step_size {
            scheduler.step();
        }
        let first_peak = scheduler.get_learning_rate();

        for _ in 0..(2 * step_size) {
            scheduler.step();
        }
        let second_peak = scheduler.get_learning_rate();

        assert!(second_peak < first_peak);
        assert_relative_eq!(
            first_peak,
            base_lr + (max_lr - base_lr) * gamma.powi(step_size as i32),
            epsilon = 1e-12
        );
        assert_relative_eq!(
            second_peak,
            base_lr + (max_lr - base_lr) * gamma.powi(3 * step_size as i32),
            epsilon = 1e-12
        );
    }

    #[test]
    fn test_zero_step_size_is_clamped() {
        for mut scheduler in [
            CyclicLR::triangular(0.001f64, 0.01, 0),
            CyclicLR::triangular2(0.001f64, 0.01, 0),
            CyclicLR::exp_range(0.001f64, 0.01, 0, 0.99),
        ] {
            assert_eq!(scheduler.step_size(), 1);
            assert!(scheduler.get_learning_rate().is_finite());
            for _ in 0..10 {
                assert!(scheduler.step().is_finite());
            }
        }
    }

    #[test]
    fn test_try_new_validates() {
        assert!(CyclicLR::try_new(0.001f64, 0.01, 0, CyclicMode::Triangular).is_err());
        assert!(CyclicLR::try_new(0.01f64, 0.001, 10, CyclicMode::Triangular).is_err());
        assert!(CyclicLR::try_new(-0.1f64, 0.01, 10, CyclicMode::Triangular).is_err());
        assert!(CyclicLR::try_new(0.001f64, 0.01, 10, CyclicMode::ExpRange(0.0)).is_err());
        assert!(CyclicLR::try_new(0.001f64, 0.01, 10, CyclicMode::ExpRange(1.5)).is_err());
        assert!(CyclicLR::try_new(0.001f64, 0.01, 10, CyclicMode::ExpRange(0.99)).is_ok());
        assert!(CyclicLR::try_new(0.001f64, 0.01, 10, CyclicMode::Triangular).is_ok());
    }

    #[test]
    fn test_cycle_counting() {
        let mut scheduler = CyclicLR::triangular(0.001, 0.01, 100);

        assert_eq!(scheduler.get_cycle(), 0);

        // Complete one cycle
        for _ in 0..200 {
            scheduler.step();
        }
        assert_eq!(scheduler.get_cycle(), 1);

        // Half way through second cycle
        for _ in 0..100 {
            scheduler.step();
        }
        assert_eq!(scheduler.get_cycle(), 1);
    }

    #[test]
    fn test_reset() {
        let mut scheduler = CyclicLR::triangular(0.001, 0.01, 100);

        // Move forward
        for _ in 0..50 {
            scheduler.step();
        }

        let lr_before_reset = scheduler.get_learning_rate();
        assert!(lr_before_reset > 0.001);

        // Reset
        scheduler.reset();
        assert_relative_eq!(scheduler.get_learning_rate(), 0.001, epsilon = 1e-6);
        assert_eq!(scheduler.current_step, 0);
    }
}