trustformers-optim 0.1.1

Optimizers for TrustformeRS
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//! # Continual Learning Optimizers
//!
//! This module implements optimization algorithms specifically designed for
//! continual learning scenarios where models must learn new tasks while
//! retaining knowledge of previous tasks.
//!
//! ## Available Methods
//!
//! - **EWC (Elastic Weight Consolidation)**: Protects important weights from changes
//! - **PackNet**: Progressive networks with parameter allocation
//! - **L2 Regularization**: Simple regularization towards previous weights
//! - **Memory Replay**: Gradient-based memory replay optimization
//! - **Meta-Learning**: Model-Agnostic Meta-Learning for continual adaptation

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use trustformers_core::tensor::Tensor;

/// Configuration for Elastic Weight Consolidation (EWC).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EWCConfig {
    /// Base learning rate
    pub learning_rate: f32,
    /// Importance weight for Fisher information regularization
    pub lambda: f32,
    /// Method for computing Fisher information
    pub fisher_method: FisherMethod,
    /// Number of samples for Fisher information estimation
    pub fisher_samples: usize,
    /// Online vs offline EWC
    pub online: bool,
    /// Decay factor for online EWC
    pub decay_factor: f32,
}

impl Default for EWCConfig {
    fn default() -> Self {
        Self {
            learning_rate: 1e-3,
            lambda: 1000.0,
            fisher_method: FisherMethod::Empirical,
            fisher_samples: 1000,
            online: false,
            decay_factor: 0.9,
        }
    }
}

/// Methods for computing Fisher information matrix.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FisherMethod {
    /// Empirical Fisher information
    Empirical,
    /// True Fisher information (computationally expensive)
    True,
    /// Diagonal approximation
    Diagonal,
}

/// Configuration for Progressive Networks (PackNet).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PackNetConfig {
    /// Base learning rate
    pub learning_rate: f32,
    /// Sparsity level for each task
    pub sparsity_level: f32,
    /// Number of tasks
    pub num_tasks: usize,
    /// Parameter allocation strategy
    pub allocation_strategy: AllocationStrategy,
}

impl Default for PackNetConfig {
    fn default() -> Self {
        Self {
            learning_rate: 1e-3,
            sparsity_level: 0.5,
            num_tasks: 10,
            allocation_strategy: AllocationStrategy::Sequential,
        }
    }
}

/// Parameter allocation strategies for PackNet.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AllocationStrategy {
    /// Sequential allocation
    Sequential,
    /// Random allocation
    Random,
    /// Importance-based allocation
    ImportanceBased,
}

/// Configuration for L2 regularization towards previous parameters.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct L2RegularizationConfig {
    /// Base learning rate
    pub learning_rate: f32,
    /// Regularization strength
    pub reg_strength: f32,
    /// Update strategy for anchor parameters
    pub update_strategy: UpdateStrategy,
}

impl Default for L2RegularizationConfig {
    fn default() -> Self {
        Self {
            learning_rate: 1e-3,
            reg_strength: 0.1,
            update_strategy: UpdateStrategy::EMA,
        }
    }
}

/// Strategies for updating anchor parameters.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UpdateStrategy {
    /// No update (fixed anchors)
    Fixed,
    /// Exponential moving average
    EMA,
    /// Update at task boundaries
    TaskBoundary,
}

/// Configuration for Memory Replay optimization.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryReplayConfig {
    /// Base learning rate
    pub learning_rate: f32,
    /// Memory buffer size
    pub memory_size: usize,
    /// Replay frequency (every N steps)
    pub replay_frequency: usize,
    /// Replay batch size
    pub replay_batch_size: usize,
    /// Memory selection strategy
    pub selection_strategy: MemorySelectionStrategy,
}

impl Default for MemoryReplayConfig {
    fn default() -> Self {
        Self {
            learning_rate: 1e-3,
            memory_size: 1000,
            replay_frequency: 10,
            replay_batch_size: 32,
            selection_strategy: MemorySelectionStrategy::Random,
        }
    }
}

/// Memory selection strategies for replay.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MemorySelectionStrategy {
    /// Random selection
    Random,
    /// Gradient-based selection
    GradientBased,
    /// Uncertainty-based selection
    UncertaintyBased,
}

/// Elastic Weight Consolidation optimizer.
pub struct EWC {
    config: EWCConfig,
    parameters: Vec<Tensor>,
    importance_weights: Vec<Tensor>,
    anchor_parameters: Vec<Tensor>,
    current_task: usize,
    accumulated_importance: Vec<Tensor>,
}

impl EWC {
    /// Create a new EWC optimizer.
    pub fn new(config: EWCConfig, initial_parameters: Vec<Tensor>) -> Result<Self> {
        let param_count = initial_parameters.len();

        let importance_weights: Result<Vec<Tensor>> = (0..param_count)
            .map(|i| {
                Tensor::zeros(&initial_parameters[i].shape())
                    .map_err(|e| anyhow::anyhow!("{:?}", e))
            })
            .collect();

        let accumulated_importance: Result<Vec<Tensor>> = (0..param_count)
            .map(|i| {
                Tensor::zeros(&initial_parameters[i].shape())
                    .map_err(|e| anyhow::anyhow!("{:?}", e))
            })
            .collect();

        Ok(Self {
            config,
            parameters: initial_parameters.clone(),
            importance_weights: importance_weights?,
            anchor_parameters: initial_parameters.clone(),
            current_task: 0,
            accumulated_importance: accumulated_importance?,
        })
    }

    /// Compute Fisher information matrix for current task.
    pub fn compute_fisher_information(&mut self, gradients_samples: &[Vec<Tensor>]) -> Result<()> {
        let num_samples = gradients_samples.len();
        if num_samples == 0 {
            return Err(anyhow!("No gradient samples provided"));
        }

        // Reset importance weights for new task
        for importance in self.importance_weights.iter_mut() {
            *importance = Tensor::zeros(&importance.shape())?;
        }

        // Compute empirical Fisher information
        for gradient_sample in gradients_samples {
            for (i, gradient) in gradient_sample.iter().enumerate() {
                if i < self.importance_weights.len() {
                    let squared_grad = gradient.mul(gradient)?;
                    self.importance_weights[i] = self.importance_weights[i].add(&squared_grad)?;
                }
            }
        }

        // Average over samples
        for importance in self.importance_weights.iter_mut() {
            *importance = importance.div_scalar(num_samples as f32)?;
        }

        // Update accumulated importance for online EWC
        if self.config.online {
            for i in 0..self.accumulated_importance.len() {
                let decayed =
                    self.accumulated_importance[i].mul_scalar(self.config.decay_factor)?;
                self.accumulated_importance[i] = decayed.add(&self.importance_weights[i])?;
            }
        }

        Ok(())
    }

    /// Complete current task and prepare for next task.
    pub fn finish_task(&mut self) -> Result<()> {
        // Update anchor parameters to current parameters
        self.anchor_parameters = self.parameters.clone();
        self.current_task += 1;
        Ok(())
    }

    /// Perform optimization step with EWC regularization.
    pub fn step(&mut self, gradients: &[Tensor]) -> Result<()> {
        for (i, gradient) in gradients.iter().enumerate() {
            if i < self.parameters.len() {
                // Compute EWC penalty gradient
                let param_diff = self.parameters[i].sub(&self.anchor_parameters[i])?;
                let importance = if self.config.online {
                    &self.accumulated_importance[i]
                } else {
                    &self.importance_weights[i]
                };
                let ewc_grad = param_diff.mul(importance)?.mul_scalar(self.config.lambda)?;

                // Combine original gradient with EWC penalty
                let total_grad = gradient.add(&ewc_grad)?;

                // Apply update
                let update = total_grad.mul_scalar(self.config.learning_rate)?;
                self.parameters[i] = self.parameters[i].sub(&update)?;
            }
        }
        Ok(())
    }

    /// Get current parameters.
    pub fn get_parameters(&self) -> &[Tensor] {
        &self.parameters
    }

    /// Get importance weights.
    pub fn get_importance_weights(&self) -> &[Tensor] {
        &self.importance_weights
    }
}

/// Progressive Networks (PackNet) optimizer.
pub struct PackNet {
    config: PackNetConfig,
    parameters: Vec<Tensor>,
    #[allow(dead_code)]
    parameter_masks: Vec<Tensor>,
    task_allocations: HashMap<usize, Vec<Tensor>>,
    current_task: usize,
    available_capacity: Vec<f32>,
}

impl PackNet {
    /// Create a new PackNet optimizer.
    pub fn new(config: PackNetConfig, initial_parameters: Vec<Tensor>) -> Result<Self> {
        let param_count = initial_parameters.len();

        let parameter_masks: Result<Vec<Tensor>> = (0..param_count)
            .map(|i| {
                Tensor::ones(&initial_parameters[i].shape()).map_err(|e| anyhow::anyhow!("{:?}", e))
            })
            .collect();

        Ok(Self {
            config,
            parameters: initial_parameters.clone(),
            parameter_masks: parameter_masks?,
            task_allocations: HashMap::new(),
            current_task: 0,
            available_capacity: vec![1.0; param_count],
        })
    }

    /// Allocate parameters for a new task.
    pub fn allocate_task(&mut self, task_id: usize) -> Result<()> {
        if self.available_capacity.iter().any(|&cap| cap < self.config.sparsity_level) {
            return Err(anyhow!("Insufficient parameter capacity for new task"));
        }

        let mut task_masks = Vec::new();

        for (i, param) in self.parameters.iter().enumerate() {
            let shape = param.shape();
            let total_params = shape.iter().product::<usize>();
            let allocated_params = (total_params as f32 * self.config.sparsity_level) as usize;

            // Create allocation mask
            let mut mask_data = vec![0.0; total_params];

            match self.config.allocation_strategy {
                AllocationStrategy::Sequential => {
                    let start_idx =
                        ((1.0 - self.available_capacity[i]) * total_params as f32) as usize;
                    let end_idx = (start_idx + allocated_params).min(total_params);
                    for idx in start_idx..end_idx {
                        mask_data[idx] = 1.0;
                    }
                },
                AllocationStrategy::Random => {
                    use scirs2_core::random::*; // SciRS2 Integration Policy
                    let mut indices: Vec<usize> = (0..total_params).collect();
                    let mut rng = thread_rng();
                    indices.shuffle(rng.rng_mut());
                    for &idx in indices.iter().take(allocated_params) {
                        mask_data[idx] = 1.0;
                    }
                },
                AllocationStrategy::ImportanceBased => {
                    // Simplified importance-based allocation
                    // In practice, this would use gradient magnitudes or other importance metrics
                    for idx in 0..allocated_params.min(total_params) {
                        mask_data[idx] = 1.0;
                    }
                },
            }

            let task_mask = Tensor::new(mask_data)?;
            task_masks.push(task_mask);

            // Update available capacity
            self.available_capacity[i] -= self.config.sparsity_level;
        }

        self.task_allocations.insert(task_id, task_masks);
        self.current_task = task_id;
        Ok(())
    }

    /// Perform optimization step with parameter masking.
    pub fn step(&mut self, gradients: &[Tensor]) -> Result<()> {
        let task_masks = self
            .task_allocations
            .get(&self.current_task)
            .ok_or_else(|| anyhow!("No allocation for current task"))?;

        for (i, gradient) in gradients.iter().enumerate() {
            if i < self.parameters.len() && i < task_masks.len() {
                // Apply task-specific mask to gradient
                let masked_grad = gradient.mul(&task_masks[i])?;

                // Apply update
                let update = masked_grad.mul_scalar(self.config.learning_rate)?;
                self.parameters[i] = self.parameters[i].sub(&update)?;
            }
        }
        Ok(())
    }

    /// Get current parameters.
    pub fn get_parameters(&self) -> &[Tensor] {
        &self.parameters
    }

    /// Get available capacity for new tasks.
    pub fn get_available_capacity(&self) -> &[f32] {
        &self.available_capacity
    }
}

/// L2 Regularization optimizer for continual learning.
pub struct L2Regularization {
    config: L2RegularizationConfig,
    parameters: Vec<Tensor>,
    anchor_parameters: Vec<Tensor>,
    ema_decay: f32,
}

impl L2Regularization {
    /// Create a new L2 regularization optimizer.
    pub fn new(config: L2RegularizationConfig, initial_parameters: Vec<Tensor>) -> Self {
        Self {
            config,
            parameters: initial_parameters.clone(),
            anchor_parameters: initial_parameters,
            ema_decay: 0.999,
        }
    }

    /// Perform optimization step with L2 regularization.
    pub fn step(&mut self, gradients: &[Tensor]) -> Result<()> {
        for (i, gradient) in gradients.iter().enumerate() {
            if i < self.parameters.len() {
                // Compute L2 regularization term
                let param_diff = self.parameters[i].sub(&self.anchor_parameters[i])?;
                let reg_grad = param_diff.mul_scalar(self.config.reg_strength)?;

                // Combine gradient with regularization
                let total_grad = gradient.add(&reg_grad)?;

                // Apply update
                let update = total_grad.mul_scalar(self.config.learning_rate)?;
                self.parameters[i] = self.parameters[i].sub(&update)?;

                // Update anchor parameters based on strategy
                match self.config.update_strategy {
                    UpdateStrategy::Fixed => {
                        // Don't update anchors
                    },
                    UpdateStrategy::EMA => {
                        // Exponential moving average update
                        let anchor_update = self.parameters[i].mul_scalar(1.0 - self.ema_decay)?;
                        let anchor_keep = self.anchor_parameters[i].mul_scalar(self.ema_decay)?;
                        self.anchor_parameters[i] = anchor_update.add(&anchor_keep)?;
                    },
                    UpdateStrategy::TaskBoundary => {
                        // Will be updated when finish_task() is called
                    },
                }
            }
        }
        Ok(())
    }

    /// Finish current task (update anchors for TaskBoundary strategy).
    pub fn finish_task(&mut self) -> Result<()> {
        if matches!(self.config.update_strategy, UpdateStrategy::TaskBoundary) {
            self.anchor_parameters = self.parameters.clone();
        }
        Ok(())
    }

    /// Get current parameters.
    pub fn get_parameters(&self) -> &[Tensor] {
        &self.parameters
    }
}

/// Memory replay optimizer.
pub struct MemoryReplay {
    config: MemoryReplayConfig,
    parameters: Vec<Tensor>,
    memory_buffer: Vec<Vec<Tensor>>, // Stored gradients
    step_count: usize,
}

impl MemoryReplay {
    /// Create a new memory replay optimizer.
    pub fn new(config: MemoryReplayConfig, initial_parameters: Vec<Tensor>) -> Self {
        Self {
            config,
            parameters: initial_parameters,
            memory_buffer: Vec::new(),
            step_count: 0,
        }
    }

    /// Add gradient to memory buffer.
    pub fn store_gradient(&mut self, gradients: &[Tensor]) -> Result<()> {
        if self.memory_buffer.len() >= self.config.memory_size {
            // Remove oldest or least important gradient
            match self.config.selection_strategy {
                MemorySelectionStrategy::Random => {
                    use scirs2_core::random::*; // SciRS2 Integration Policy
                    let idx = thread_rng().random_range(0..self.memory_buffer.len());
                    self.memory_buffer.remove(idx);
                },
                _ => {
                    self.memory_buffer.remove(0); // FIFO for simplicity
                },
            }
        }

        self.memory_buffer.push(gradients.to_vec());
        Ok(())
    }

    /// Perform optimization step with memory replay.
    pub fn step(&mut self, gradients: &[Tensor]) -> Result<()> {
        // Regular gradient update
        for (i, gradient) in gradients.iter().enumerate() {
            if i < self.parameters.len() {
                let update = gradient.mul_scalar(self.config.learning_rate)?;
                self.parameters[i] = self.parameters[i].sub(&update)?;
            }
        }

        // Store current gradient
        self.store_gradient(gradients)?;

        // Replay from memory
        if self.step_count.is_multiple_of(self.config.replay_frequency)
            && !self.memory_buffer.is_empty()
        {
            self.replay_step()?;
        }

        self.step_count += 1;
        Ok(())
    }

    fn replay_step(&mut self) -> Result<()> {
        let batch_size = self.config.replay_batch_size.min(self.memory_buffer.len());

        // Select random batch from memory
        use scirs2_core::random::*; // SciRS2 Integration Policy
        let mut indices: Vec<usize> = (0..self.memory_buffer.len()).collect();
        let mut rng = thread_rng();
        indices.shuffle(rng.rng_mut());

        for &idx in indices.iter().take(batch_size) {
            let replay_gradients = &self.memory_buffer[idx];

            // Apply replay gradient with reduced learning rate
            let replay_lr = self.config.learning_rate * 0.5;
            for (i, gradient) in replay_gradients.iter().enumerate() {
                if i < self.parameters.len() {
                    let update = gradient.mul_scalar(replay_lr)?;
                    self.parameters[i] = self.parameters[i].sub(&update)?;
                }
            }
        }

        Ok(())
    }

    /// Get current parameters.
    pub fn get_parameters(&self) -> &[Tensor] {
        &self.parameters
    }

    /// Get memory buffer size.
    pub fn memory_size(&self) -> usize {
        self.memory_buffer.len()
    }
}

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

    #[test]
    fn test_ewc_config() {
        let config = EWCConfig::default();
        assert_eq!(config.learning_rate, 1e-3);
        assert_eq!(config.lambda, 1000.0);
        assert!(!config.online);
    }

    #[test]
    fn test_packnet_config() {
        let config = PackNetConfig::default();
        assert_eq!(config.sparsity_level, 0.5);
        assert_eq!(config.num_tasks, 10);
    }

    #[test]
    fn test_l2_regularization_config() {
        let config = L2RegularizationConfig::default();
        assert_eq!(config.reg_strength, 0.1);
        assert!(matches!(config.update_strategy, UpdateStrategy::EMA));
    }

    #[test]
    fn test_memory_replay_config() {
        let config = MemoryReplayConfig::default();
        assert_eq!(config.memory_size, 1000);
        assert_eq!(config.replay_frequency, 10);
        assert!(matches!(
            config.selection_strategy,
            MemorySelectionStrategy::Random
        ));
    }

    #[test]
    fn test_fisher_methods() {
        assert!(matches!(FisherMethod::Empirical, FisherMethod::Empirical));
        assert!(matches!(FisherMethod::True, FisherMethod::True));
        assert!(matches!(FisherMethod::Diagonal, FisherMethod::Diagonal));
    }
}