torsh-optim 0.1.3

Optimization algorithms for ToRSh with PyTorch-compatible API
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
//! Natural Gradient optimizer

use crate::{optimizer::BaseOptimizer, Optimizer, OptimizerResult, OptimizerState, ParamGroup};
use parking_lot::RwLock;
use std::collections::HashMap;
use std::ops::Add;
use std::sync::Arc;
use torsh_core::error::Result;
use torsh_tensor::{creation::zeros_like, Tensor};

/// Natural Gradient optimizer
///
/// Natural gradients use the Fisher Information Matrix to precondition gradients,
/// providing better-conditioned updates especially for neural networks and probabilistic models.
///
/// Reference: "Natural Gradient Works Efficiently in Learning" (Amari, 1998)
pub struct NaturalGradient {
    base: BaseOptimizer,
    lr: f32,
    momentum: f32,
    damping: f32,
    fisher_update_freq: usize,
    use_empirical_fisher: bool,
    fisher_ema_decay: f32,
    step_count: usize,
}

impl NaturalGradient {
    /// Create a new Natural Gradient optimizer
    pub fn new(
        params: Vec<Arc<RwLock<Tensor>>>,
        lr: Option<f32>,
        momentum: Option<f32>,
        damping: Option<f32>,
        fisher_update_freq: Option<usize>,
        use_empirical_fisher: Option<bool>,
        fisher_ema_decay: Option<f32>,
    ) -> Self {
        let lr = lr.unwrap_or(0.03);
        let momentum = momentum.unwrap_or(0.9);
        let damping = damping.unwrap_or(0.001);
        let fisher_update_freq = fisher_update_freq.unwrap_or(1);
        let use_empirical_fisher = use_empirical_fisher.unwrap_or(true);
        let fisher_ema_decay = fisher_ema_decay.unwrap_or(0.95);

        let mut defaults = HashMap::new();
        defaults.insert("lr".to_string(), lr);
        defaults.insert("momentum".to_string(), momentum);
        defaults.insert("damping".to_string(), damping);

        let param_group = ParamGroup::new(params, lr);

        let base = BaseOptimizer {
            param_groups: vec![param_group],
            state: HashMap::new(),
            optimizer_type: "NaturalGradient".to_string(),
            defaults,
        };

        Self {
            base,
            lr,
            momentum,
            damping,
            fisher_update_freq,
            use_empirical_fisher,
            fisher_ema_decay,
            step_count: 0,
        }
    }

    /// Builder pattern for Natural Gradient optimizer
    pub fn builder() -> NaturalGradientBuilder {
        NaturalGradientBuilder::default()
    }

    /// Compute Fisher Information Matrix (simplified approximation)
    fn compute_fisher_matrix(&self, param: &Tensor, grad: &Tensor) -> Result<Tensor> {
        // For neural networks, we use a diagonal approximation of the Fisher matrix
        // F_ii ≈ E[g_i^2] where g_i is the gradient of log-likelihood w.r.t. parameter i

        if self.use_empirical_fisher {
            // Empirical Fisher: use squared gradients
            Ok(grad.pow(2.0)?)
        } else {
            // True Fisher would require second-order information
            // For now, we approximate with squared gradients plus some regularization
            let reg = param.mul_scalar(1e-6)?;
            let fisher_approx = grad.pow(2.0)?;
            Ok(fisher_approx.add(&reg)?)
        }
    }

    /// Apply natural gradient update
    fn apply_natural_gradient_update(
        &self,
        param: &mut Tensor,
        grad: &Tensor,
        fisher: &Tensor,
    ) -> Result<()> {
        // Natural gradient update: θ_{t+1} = θ_t - η * F^{-1} * ∇L
        // We approximate F^{-1} with element-wise division for diagonal Fisher matrix

        let damped_fisher = fisher.add_scalar(self.damping)?;
        let natural_grad = grad.div(&damped_fisher)?;
        let update = natural_grad.mul_scalar(self.lr)?;

        *param = param.sub(&update)?;
        Ok(())
    }
}

impl Optimizer for NaturalGradient {
    fn step(&mut self) -> OptimizerResult<()> {
        self.step_count += 1;
        let should_update_fisher = self.step_count % self.fisher_update_freq == 0;

        // Extract values to avoid borrowing conflicts
        let lr = self.lr;
        let momentum = self.momentum;
        let damping = self.damping;
        let fisher_ema_decay = self.fisher_ema_decay;

        for group in &mut self.base.param_groups {
            for param_arc in &group.params {
                let param = param_arc.write();

                // Check if parameter has gradients
                if !param.has_grad() {
                    continue;
                }

                let grad = param
                    .grad()
                    .expect("gradient should exist after has_grad check");
                let param_id = format!("{:p}", param_arc.as_ref());

                // Extract data early to avoid borrow conflicts
                let _param_data = param.clone();
                let grad_data = grad.clone();

                // Get or initialize optimizer state
                let needs_init = !self.base.state.contains_key(&param_id);
                let state = self
                    .base
                    .state
                    .entry(param_id.clone())
                    .or_insert_with(HashMap::new);

                if needs_init {
                    state.insert("momentum_buffer".to_string(), zeros_like(&param)?);
                    state.insert("fisher_matrix".to_string(), zeros_like(&param)?);
                    state.insert("step".to_string(), zeros_like(&param)?);
                }

                let mut momentum_buffer = state
                    .get("momentum_buffer")
                    .expect("momentum_buffer state should exist")
                    .clone();
                let mut fisher_matrix = state
                    .get("fisher_matrix")
                    .expect("fisher_matrix state should exist")
                    .clone();
                let mut step_tensor = state.get("step").expect("step state should exist").clone();

                // Increment step count
                step_tensor.add_scalar_(1.0)?;

                // Update Fisher matrix if needed
                let mut new_fisher_opt = None;
                if should_update_fisher {
                    // Temporarily drop the mutable borrow
                    drop(param);

                    // Compute Fisher matrix inline: F = g ⊗ g (outer product approximation)
                    let new_fisher = grad_data.mul_op(&grad_data)?;
                    new_fisher_opt = Some(new_fisher);
                }

                let fisher_was_updated = new_fisher_opt.is_some();
                if let Some(new_fisher) = new_fisher_opt.clone() {
                    if step_tensor.to_vec()?[0] == 1.0 {
                        // First step: initialize Fisher matrix
                        fisher_matrix = new_fisher;
                    } else {
                        // Update Fisher matrix with exponential moving average
                        fisher_matrix = fisher_matrix
                            .mul_scalar(fisher_ema_decay)?
                            .add(&new_fisher.mul_scalar(1.0 - fisher_ema_decay)?)?;
                    }
                }

                // Compute natural gradient
                let damped_fisher = fisher_matrix.add_scalar(damping)?;
                let natural_grad = grad_data.div(&damped_fisher)?;

                // Re-acquire parameter lock if it was dropped
                let mut param = if fisher_was_updated {
                    param_arc.write()
                } else {
                    // Re-acquire the lock since we dropped it earlier
                    param_arc.write()
                };

                // Apply momentum
                if momentum != 0.0 {
                    momentum_buffer = momentum_buffer.mul_scalar(momentum)?.add(&natural_grad)?;
                    let update = momentum_buffer.mul_scalar(lr)?;
                    *param = param.sub(&update)?;
                } else {
                    let update = natural_grad.mul_scalar(lr)?;
                    *param = param.sub(&update)?;
                }

                // Update state
                state.insert("momentum_buffer".to_string(), momentum_buffer);
                state.insert("fisher_matrix".to_string(), fisher_matrix);
                state.insert("step".to_string(), step_tensor);
            }
        }

        Ok(())
    }

    fn zero_grad(&mut self) {
        self.base.zero_grad();
    }

    fn get_lr(&self) -> Vec<f32> {
        self.base.get_lr()
    }

    fn set_lr(&mut self, lr: f32) {
        self.lr = lr;
        self.base.set_lr(lr);
    }

    fn add_param_group(&mut self, params: Vec<Arc<RwLock<Tensor>>>, options: HashMap<String, f32>) {
        self.base.add_param_group(params, options);
    }

    fn parameters(&self) -> Vec<Arc<RwLock<Tensor>>> {
        self.base.parameters()
    }

    fn state_dict(&self) -> OptimizerResult<OptimizerState> {
        self.base.state_dict()
    }

    fn load_state_dict(&mut self, state: OptimizerState) -> OptimizerResult<()> {
        self.base.load_state_dict(state)
    }
}

/// Builder for Natural Gradient optimizer
pub struct NaturalGradientBuilder {
    params: Vec<Arc<RwLock<Tensor>>>,
    lr: f32,
    momentum: f32,
    damping: f32,
    fisher_update_freq: usize,
    use_empirical_fisher: bool,
    fisher_ema_decay: f32,
}

impl Default for NaturalGradientBuilder {
    fn default() -> Self {
        Self {
            params: Vec::new(),
            lr: 0.03,
            momentum: 0.9,
            damping: 0.001,
            fisher_update_freq: 1,
            use_empirical_fisher: true,
            fisher_ema_decay: 0.95,
        }
    }
}

impl NaturalGradientBuilder {
    pub fn params(mut self, params: Vec<Arc<RwLock<Tensor>>>) -> Self {
        self.params = params;
        self
    }

    pub fn lr(mut self, lr: f32) -> Self {
        self.lr = lr;
        self
    }

    pub fn momentum(mut self, momentum: f32) -> Self {
        self.momentum = momentum;
        self
    }

    pub fn damping(mut self, damping: f32) -> Self {
        self.damping = damping;
        self
    }

    pub fn fisher_update_freq(mut self, freq: usize) -> Self {
        self.fisher_update_freq = freq;
        self
    }

    pub fn use_empirical_fisher(mut self, use_empirical: bool) -> Self {
        self.use_empirical_fisher = use_empirical;
        self
    }

    pub fn fisher_ema_decay(mut self, decay: f32) -> Self {
        self.fisher_ema_decay = decay;
        self
    }

    pub fn build(self) -> NaturalGradient {
        NaturalGradient::new(
            self.params,
            Some(self.lr),
            Some(self.momentum),
            Some(self.damping),
            Some(self.fisher_update_freq),
            Some(self.use_empirical_fisher),
            Some(self.fisher_ema_decay),
        )
    }
}

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

    #[test]
    fn test_natural_gradient_creation() {
        let param = Arc::new(RwLock::new(randn::<f32>(&[10, 10]).unwrap()));
        let params = vec![param];

        let optimizer = NaturalGradient::new(
            params,
            Some(0.01),
            Some(0.9),
            Some(0.001),
            Some(1),
            Some(true),
            Some(0.95),
        );

        assert_eq!(optimizer.lr, 0.01);
        assert_eq!(optimizer.momentum, 0.9);
        assert_eq!(optimizer.damping, 0.001);
    }

    #[test]
    fn test_natural_gradient_builder() {
        let param = Arc::new(RwLock::new(randn::<f32>(&[5, 5]).unwrap()));
        let params = vec![param];

        let optimizer = NaturalGradient::builder()
            .params(params)
            .lr(0.02)
            .momentum(0.95)
            .damping(0.01)
            .fisher_update_freq(5)
            .use_empirical_fisher(false)
            .fisher_ema_decay(0.9)
            .build();

        assert_eq!(optimizer.lr, 0.02);
        assert_eq!(optimizer.momentum, 0.95);
        assert_eq!(optimizer.damping, 0.01);
        assert_eq!(optimizer.fisher_update_freq, 5);
        assert!(!optimizer.use_empirical_fisher);
        assert_eq!(optimizer.fisher_ema_decay, 0.9);
    }

    #[test]
    fn test_natural_gradient_step() -> OptimizerResult<()> {
        let param = Arc::new(RwLock::new(randn::<f32>(&[3, 3]).unwrap()));
        let initial_param = param.read().clone();

        // Set up gradient
        {
            let mut p = param.write();
            let grad = randn::<f32>(&[3, 3]).unwrap();
            p.set_grad(Some(grad));
        }

        let params = vec![param.clone()];
        let mut optimizer = NaturalGradient::new(params, Some(0.01), None, None, None, None, None);

        // Perform optimization step
        optimizer.step().unwrap();

        // Check that parameter changed
        let final_param = param.read().clone();
        let diff = initial_param.sub(&final_param).unwrap();
        let norm = diff.norm()?.to_vec()?[0];
        assert!(
            norm > 0.0,
            "Parameter should change after optimization step"
        );
        Ok(())
    }

    #[test]
    #[allow(dead_code)]
    fn test_fisher_matrix_computation() -> OptimizerResult<()> {
        let param = randn::<f32>(&[2, 2]).unwrap();
        let grad = randn::<f32>(&[2, 2]).unwrap();

        let optimizer =
            NaturalGradient::new(vec![], Some(0.01), None, None, None, Some(true), None);

        let fisher = optimizer.compute_fisher_matrix(&param, &grad).unwrap();

        // Fisher matrix should be positive (squared gradients)
        let fisher_vec = fisher.to_vec()?;
        for &val in &fisher_vec {
            assert!(val >= 0.0, "Fisher matrix elements should be non-negative");
        }

        Ok(())
    }
}