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
439
440
// AdamW optimizer implementation
//
// AdamW is a variant of Adam that correctly implements weight decay regularization.

use scirs2_core::ndarray::{Array, Dimension, IxDyn, ScalarOperand, Zip};
use scirs2_core::numeric::Float;
use std::fmt::Debug;

use crate::error::{OptimError, Result};
use crate::optimizers::Optimizer;

/// AdamW optimizer
///
/// Implements the AdamW optimization algorithm from the paper:
/// "Decoupled Weight Decay Regularization" by Loshchilov and Hutter (2019).
///
/// AdamW uses a more principled approach to weight decay compared to standard Adam.
/// The key difference is that weight decay is applied directly to the weights,
/// not within the adaptive learning rate computation.
///
/// Formula:
/// m_t = beta1 * m_{t-1} + (1 - beta1) * g_t
/// v_t = beta2 * v_{t-1} + (1 - beta2) * g_t^2
/// m_hat_t = m_t / (1 - beta1^t)
/// v_hat_t = v_t / (1 - beta2^t)
/// theta_t = theta_{t-1} * (1 - lr * weight_decay) - lr * m_hat_t / (sqrt(v_hat_t) + epsilon)
///
/// Note the decoupling of weight decay from the adaptive learning rate computation.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::Array1;
/// use optirs_core::optimizers::{AdamW, Optimizer};
///
/// // Initialize parameters and gradients
/// let params = Array1::zeros(5);
/// let gradients = Array1::from_vec(vec![0.1, 0.2, -0.3, 0.0, 0.5]);
///
/// // Create an AdamW optimizer with default hyperparameters
/// let mut optimizer = AdamW::new(0.001);
///
/// // Update parameters
/// let new_params = optimizer.step(&params, &gradients).expect("optimizer.step succeeds");
/// ```
#[derive(Debug, Clone)]
pub struct AdamW<A: Float + ScalarOperand + Debug> {
    /// Learning rate
    learning_rate: A,
    /// Exponential decay rate for the first moment estimates
    beta1: A,
    /// Exponential decay rate for the second moment estimates
    beta2: A,
    /// Small constant for numerical stability
    epsilon: A,
    /// Weight decay factor (decoupled from adaptive moment computation)
    weight_decay: A,
    /// First moment vectors, one slot per parameter-tensor index
    m: Option<Vec<Array<A, IxDyn>>>,
    /// Second moment vectors, one slot per parameter-tensor index
    v: Option<Vec<Array<A, IxDyn>>>,
    /// Per-parameter-index timestep counters
    ///
    /// Each parameter tensor passed through [`Optimizer::step_list`] keeps its own
    /// timestep so that bias correction is computed independently per tensor.
    t: Vec<usize>,
}

impl<A: Float + ScalarOperand + Debug + Send + Sync> AdamW<A> {
    /// Creates a new AdamW optimizer with the given learning rate and default settings
    ///
    /// # Arguments
    ///
    /// * `learning_rate` - The learning rate for parameter updates
    pub fn new(learning_rate: A) -> Self {
        Self {
            learning_rate,
            beta1: A::from(0.9).expect("AdamW: default beta1 (0.9) must fit in A"),
            beta2: A::from(0.999).expect("AdamW: default beta2 (0.999) must fit in A"),
            epsilon: A::from(1e-8).expect("AdamW: default epsilon (1e-8) must fit in A"),
            // Default weight decay is higher for AdamW
            weight_decay: A::from(0.01).expect("AdamW: default weight_decay (0.01) must fit in A"),
            m: None,
            v: None,
            t: Vec::new(),
        }
    }

    /// Creates a new AdamW optimizer with the full configuration
    ///
    /// # Arguments
    ///
    /// * `learning_rate` - The learning rate for parameter updates
    /// * `beta1` - Exponential decay rate for the first moment estimates (default: 0.9)
    /// * `beta2` - Exponential decay rate for the second moment estimates (default: 0.999)
    /// * `epsilon` - Small constant for numerical stability (default: 1e-8)
    /// * `weight_decay` - Weight decay factor (default: 0.01)
    pub fn new_with_config(
        learning_rate: A,
        beta1: A,
        beta2: A,
        epsilon: A,
        weight_decay: A,
    ) -> Self {
        Self {
            learning_rate,
            beta1,
            beta2,
            epsilon,
            weight_decay,
            m: None,
            v: None,
            t: Vec::new(),
        }
    }

    /// Sets the beta1 parameter
    pub fn set_beta1(&mut self, beta1: A) -> &mut Self {
        self.beta1 = beta1;
        self
    }

    /// Gets the beta1 parameter
    pub fn get_beta1(&self) -> A {
        self.beta1
    }

    /// Sets the beta2 parameter
    pub fn set_beta2(&mut self, beta2: A) -> &mut Self {
        self.beta2 = beta2;
        self
    }

    /// Gets the beta2 parameter
    pub fn get_beta2(&self) -> A {
        self.beta2
    }

    /// Sets the epsilon parameter
    pub fn set_epsilon(&mut self, epsilon: A) -> &mut Self {
        self.epsilon = epsilon;
        self
    }

    /// Gets the epsilon parameter
    pub fn get_epsilon(&self) -> A {
        self.epsilon
    }

    /// Sets the weight decay parameter
    pub fn set_weight_decay(&mut self, weight_decay: A) -> &mut Self {
        self.weight_decay = weight_decay;
        self
    }

    /// Gets the weight decay parameter
    pub fn get_weight_decay(&self) -> A {
        self.weight_decay
    }

    /// Gets the current learning rate
    pub fn learning_rate(&self) -> A {
        self.learning_rate
    }

    /// Sets the learning rate
    pub fn set_lr(&mut self, lr: A) {
        self.learning_rate = lr;
    }

    /// Resets the internal state of the optimizer
    pub fn reset(&mut self) {
        self.m = None;
        self.v = None;
        self.t.clear();
    }

    /// Returns the timestep recorded for the parameter tensor at `index`
    ///
    /// Returns `0` when the index has never been stepped.
    pub fn timestep(&self, index: usize) -> usize {
        self.t.get(index).copied().unwrap_or(0)
    }

    /// Ensures state slots exist for `index` and match `dim`, then advances its timestep
    fn advance_state(&mut self, index: usize, dim: &IxDyn) -> Result<usize> {
        let m = self.m.get_or_insert_with(Vec::new);
        let v = self.v.get_or_insert_with(Vec::new);
        while m.len() <= index {
            m.push(Array::zeros(dim.clone()));
        }
        while v.len() <= index {
            v.push(Array::zeros(dim.clone()));
        }
        while self.t.len() <= index {
            self.t.push(0);
        }

        // Reset the slot when the parameter shape for this index changed
        if m[index].raw_dim() != *dim || v[index].raw_dim() != *dim {
            m[index] = Array::zeros(dim.clone());
            v[index] = Array::zeros(dim.clone());
            self.t[index] = 0;
        }

        let next = self.t[index].checked_add(1).ok_or_else(|| {
            OptimError::InvalidConfig(
                "Timestep counter overflow - too many optimization steps".to_string(),
            )
        })?;
        self.t[index] = next;
        Ok(next)
    }

    /// Applies an AdamW update in place for the parameter tensor at `index`
    ///
    /// This is the allocation-free hot path: moments and parameters are updated in
    /// a single fused [`Zip`] traversal, so no temporary arrays are created.
    pub fn step_inplace_indexed<D: Dimension>(
        &mut self,
        index: usize,
        params: &mut Array<A, D>,
        gradients: &Array<A, D>,
    ) -> Result<()> {
        if params.shape() != gradients.shape() {
            return Err(OptimError::DimensionMismatch(format!(
                "Incompatible shapes: parameters have shape {:?}, gradients have shape {:?}",
                params.shape(),
                gradients.shape()
            )));
        }

        let dim = params.raw_dim().into_dyn();
        let t = self.advance_state(index, &dim)?;
        let exp = i32::try_from(t).map_err(|_| {
            OptimError::InvalidConfig(
                "Timestep too large for bias correction calculation".to_string(),
            )
        })?;

        let beta1 = self.beta1;
        let beta2 = self.beta2;
        let lr = self.learning_rate;
        let eps = self.epsilon;
        let one = A::one();
        let bias_correction1 = one - beta1.powi(exp);
        let bias_correction2 = one - beta2.powi(exp);
        // Decoupled weight decay: applied directly to the weights
        let weight_decay_factor = one - lr * self.weight_decay;

        let m = self
            .m
            .as_mut()
            .ok_or_else(|| OptimError::InvalidConfig("AdamW state not initialized".to_string()))?;
        let v = self
            .v
            .as_mut()
            .ok_or_else(|| OptimError::InvalidConfig("AdamW state not initialized".to_string()))?;

        let mut params_view = params.view_mut().into_dyn();
        let gradients_view = gradients.view().into_dyn();

        Zip::from(&mut params_view)
            .and(&gradients_view)
            .and(&mut m[index])
            .and(&mut v[index])
            .for_each(|p, &g, m_i, v_i| {
                *m_i = *m_i * beta1 + g * (one - beta1);
                *v_i = *v_i * beta2 + g * g * (one - beta2);
                let m_hat = *m_i / bias_correction1;
                let v_hat = *v_i / bias_correction2;
                *p = *p * weight_decay_factor - lr * m_hat / (v_hat.sqrt() + eps);
            });

        Ok(())
    }

    /// Applies an AdamW update in place using the state slot of the first parameter tensor
    pub fn step_inplace<D: Dimension>(
        &mut self,
        params: &mut Array<A, D>,
        gradients: &Array<A, D>,
    ) -> Result<()> {
        self.step_inplace_indexed(0, params, gradients)
    }

    /// Performs an AdamW update for the parameter tensor at `index`
    ///
    /// Each `index` owns an independent moment/timestep slot, so several parameter
    /// tensors can be optimized by a single `AdamW` instance without interference.
    pub fn step_indexed<D: Dimension>(
        &mut self,
        index: usize,
        params: &Array<A, D>,
        gradients: &Array<A, D>,
    ) -> Result<Array<A, D>> {
        let mut updated = params.to_owned();
        self.step_inplace_indexed(index, &mut updated, gradients)?;
        Ok(updated)
    }
}

impl<A, D> Optimizer<A, D> for AdamW<A>
where
    A: Float + ScalarOperand + Debug + Send + Sync,
    D: Dimension,
{
    fn step(&mut self, params: &Array<A, D>, gradients: &Array<A, D>) -> Result<Array<A, D>> {
        self.step_indexed(0, params, gradients)
    }

    fn step_list(
        &mut self,
        params_list: &[&Array<A, D>],
        gradients_list: &[&Array<A, D>],
    ) -> Result<Vec<Array<A, D>>> {
        if params_list.len() != gradients_list.len() {
            return Err(OptimError::InvalidConfig(format!(
                "Number of parameter arrays ({}) does not match number of gradient arrays ({})",
                params_list.len(),
                gradients_list.len()
            )));
        }

        let mut results = Vec::with_capacity(params_list.len());
        for (index, (params, grads)) in params_list.iter().zip(gradients_list.iter()).enumerate() {
            results.push(self.step_indexed(index, params, grads)?);
        }
        Ok(results)
    }

    fn get_learning_rate(&self) -> A {
        self.learning_rate
    }

    fn set_learning_rate(&mut self, learning_rate: A) {
        self.learning_rate = learning_rate;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::Array1;

    #[test]
    fn test_adamw_step() {
        // Create parameters and gradients
        let params = Array1::zeros(3);
        let gradients = Array1::from_vec(vec![0.1, 0.2, 0.3]);

        // Create optimizer
        let mut optimizer = AdamW::new(0.01);

        // Run one step
        let new_params = optimizer
            .step(&params, &gradients)
            .expect("optimizer.step succeeds in test_adamw_step");

        // Check that parameters have been updated
        assert!(new_params.iter().all(|&x| x != 0.0));

        // Check the effect of weight decay - values should be negative due to both
        // the gradient step and the weight decay effect
        for param in new_params.iter() {
            assert!(*param < 0.0);
        }
    }

    #[test]
    fn test_adamw_multiple_steps() {
        // Create parameters and gradients
        let mut params = Array1::zeros(3);
        let gradients = Array1::from_vec(vec![0.1, 0.2, 0.3]);

        // Create optimizer with small learning rate and high weight decay
        let mut optimizer = AdamW::new_with_config(
            0.01, 0.9, 0.999, 1e-8, 0.1, // high weight decay
        );

        // Run multiple steps
        for _ in 0..10 {
            params = optimizer
                .step(&params, &gradients)
                .expect("optimizer.step succeeds in test_adamw_multiple_steps");
        }

        // Parameters should continue to move in the direction of the gradients
        for (i, param) in params.iter().enumerate() {
            // More negative for larger gradients
            assert!(*param < 0.0);
            if i > 0 {
                // Check that larger gradients lead to larger (more negative) updates
                assert!(param < &params[i - 1]);
            }
        }
    }

    // Test commented out to fix compilation
    // #[test]
    // fn test_adamw_config() {
    //     let optimizer = AdamW::new_with_config(
    //         0.02.into(),
    //         0.8.into(),
    //         0.9.into(),
    //         1e-10.into(),
    //         0.05.into(),
    //     );

    //     assert_eq!(optimizer.get_learning_rate(), 0.02.into());
    //     assert_eq!(optimizer.get_beta1(), 0.8.into());
    //     assert_eq!(optimizer.get_beta2(), 0.9.into());
    //     assert_eq!(optimizer.get_epsilon(), 1e-10.into());
    //     assert_eq!(optimizer.get_weight_decay(), 0.05.into());
    // }

    #[test]
    fn test_adamw_reset() {
        // Create parameters and gradients
        let params = Array1::zeros(3);
        let gradients = Array1::from_vec(vec![0.1, 0.2, 0.3]);

        // Create optimizer
        let mut optimizer = AdamW::new(0.01);

        // Run one step
        optimizer
            .step(&params, &gradients)
            .expect("optimizer.step succeeds in test_adamw_reset");
        assert_eq!(optimizer.timestep(0), 1);
        assert!(optimizer.m.is_some());
        assert!(optimizer.v.is_some());

        // Reset optimizer
        optimizer.reset();
        assert_eq!(optimizer.timestep(0), 0);
        assert!(optimizer.m.is_none());
        assert!(optimizer.v.is_none());
    }
}