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
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
use crate::OptimizerError;
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant};

/// Configuration for lazy parameter updates
#[derive(Debug, Clone)]
pub struct LazyUpdateConfig {
    /// Maximum delay before forcing an update (in milliseconds)
    pub max_delay_ms: u64,
    /// Minimum gradient magnitude to trigger an update
    pub gradient_threshold: f32,
    /// Maximum number of pending updates before forcing a batch update
    pub max_pending_updates: usize,
    /// Whether to use adaptive thresholds based on parameter history
    pub adaptive_threshold: bool,
    /// Update frequency based on parameter importance
    pub importance_based_updates: bool,
    /// Batch size for processing updates
    pub batch_size: usize,
}

impl Default for LazyUpdateConfig {
    fn default() -> Self {
        Self {
            max_delay_ms: 1000, // 1 second max delay
            gradient_threshold: 1e-6,
            max_pending_updates: 1000,
            adaptive_threshold: true,
            importance_based_updates: true,
            batch_size: 100,
        }
    }
}

/// Pending parameter update
#[derive(Debug, Clone)]
pub struct PendingUpdate {
    pub parameter_id: String,
    pub gradient: Vec<f32>,
    pub timestamp: Instant,
    pub priority: UpdatePriority,
    pub accumulated_magnitude: f32,
}

/// Update priority levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum UpdatePriority {
    Low = 0,
    Medium = 1,
    High = 2,
    Critical = 3,
}

/// Parameter importance tracking
#[derive(Debug, Clone)]
pub struct ParameterImportance {
    pub parameter_id: String,
    pub update_frequency: f32,
    pub average_gradient_magnitude: f32,
    pub variance: f32,
    pub last_significant_update: Instant,
    pub importance_score: f32,
}

impl ParameterImportance {
    pub fn new(parameter_id: String) -> Self {
        Self {
            parameter_id,
            update_frequency: 0.0,
            average_gradient_magnitude: 0.0,
            variance: 0.0,
            last_significant_update: Instant::now(),
            importance_score: 1.0,
        }
    }

    pub fn update(&mut self, gradient_magnitude: f32, is_significant: bool) {
        // Update running averages using exponential moving average
        let alpha = 0.1; // Learning rate for moving averages

        self.average_gradient_magnitude =
            (1.0 - alpha) * self.average_gradient_magnitude + alpha * gradient_magnitude;

        let variance_delta = (gradient_magnitude - self.average_gradient_magnitude).powi(2);
        self.variance = (1.0 - alpha) * self.variance + alpha * variance_delta;

        if is_significant {
            self.last_significant_update = Instant::now();
            self.update_frequency = (1.0 - alpha) * self.update_frequency + alpha;
        } else {
            self.update_frequency = (1.0 - alpha) * self.update_frequency;
        }

        // Calculate importance score based on multiple factors
        let recency_factor =
            1.0 / (1.0 + self.last_significant_update.elapsed().as_secs_f32() / 60.0);
        let magnitude_factor =
            self.average_gradient_magnitude / (1e-6 + self.average_gradient_magnitude);
        let frequency_factor = self.update_frequency;
        let stability_factor = 1.0 / (1.0 + self.variance);

        self.importance_score =
            recency_factor * magnitude_factor * frequency_factor * stability_factor;
    }
}

/// Lazy parameter update manager
pub struct LazyUpdateManager {
    config: LazyUpdateConfig,
    pending_updates: VecDeque<PendingUpdate>,
    parameter_importance: HashMap<String, ParameterImportance>,
    adaptive_thresholds: HashMap<String, f32>,
    last_batch_update: Instant,
    total_updates_processed: usize,
    total_updates_skipped: usize,
}

impl LazyUpdateManager {
    /// Create a new lazy update manager
    pub fn new(config: LazyUpdateConfig) -> Self {
        Self {
            config,
            pending_updates: VecDeque::new(),
            parameter_importance: HashMap::new(),
            adaptive_thresholds: HashMap::new(),
            last_batch_update: Instant::now(),
            total_updates_processed: 0,
            total_updates_skipped: 0,
        }
    }

    /// Submit a gradient for potential lazy update
    pub fn submit_gradient(
        &mut self,
        parameter_id: String,
        gradient: Vec<f32>,
    ) -> LazyUpdateDecision {
        let gradient_magnitude = gradient.iter().map(|&x| x * x).sum::<f32>().sqrt();

        // Get threshold
        let threshold = self.get_threshold(&parameter_id);
        let is_significant = gradient_magnitude > threshold;

        // Get or create parameter importance tracking and update it
        {
            let importance = self
                .parameter_importance
                .entry(parameter_id.clone())
                .or_insert_with(|| ParameterImportance::new(parameter_id.clone()));
            importance.update(gradient_magnitude, is_significant);
        }

        // Now calculate priority with immutable borrow
        let priority = {
            let importance = &self.parameter_importance[&parameter_id];
            self.calculate_priority(&parameter_id, gradient_magnitude, importance)
        };

        // Check if we should update immediately
        if self.should_update_immediately(&parameter_id, gradient_magnitude, priority) {
            self.total_updates_processed += 1;
            return LazyUpdateDecision::UpdateNow;
        }

        // Add to pending updates
        let pending_update = PendingUpdate {
            parameter_id: parameter_id.clone(),
            gradient,
            timestamp: Instant::now(),
            priority,
            accumulated_magnitude: gradient_magnitude,
        };

        self.insert_pending_update(pending_update);

        // Check if we need to process batch updates
        if self.should_process_batch() {
            LazyUpdateDecision::ProcessBatch(self.get_batch_updates())
        } else {
            self.total_updates_skipped += 1;
            LazyUpdateDecision::Defer
        }
    }

    /// Get the next batch of updates to process
    pub fn get_batch_updates(&mut self) -> Vec<PendingUpdate> {
        let mut batch = Vec::new();
        let batch_size = self.config.batch_size.min(self.pending_updates.len());

        // Sort by priority and timestamp
        let mut updates: Vec<_> = self.pending_updates.drain(..).collect();
        updates.sort_by(|a, b| {
            b.priority
                .cmp(&a.priority)
                .then_with(|| a.timestamp.cmp(&b.timestamp))
        });

        // Take the highest priority updates
        for update in updates.into_iter().take(batch_size) {
            batch.push(update);
        }

        self.last_batch_update = Instant::now();
        self.total_updates_processed += batch.len();

        batch
    }

    /// Force processing of all pending updates
    pub fn flush_all_updates(&mut self) -> Vec<PendingUpdate> {
        let updates: Vec<_> = self.pending_updates.drain(..).collect();
        self.total_updates_processed += updates.len();
        updates
    }

    /// Get updates that have exceeded the maximum delay
    pub fn get_expired_updates(&mut self) -> Vec<PendingUpdate> {
        let max_delay = Duration::from_millis(self.config.max_delay_ms);
        let now = Instant::now();

        let mut expired = Vec::new();
        let mut remaining = VecDeque::new();

        while let Some(update) = self.pending_updates.pop_front() {
            if update.timestamp.elapsed() > max_delay {
                expired.push(update);
            } else {
                remaining.push_back(update);
            }
        }

        self.pending_updates = remaining;
        self.total_updates_processed += expired.len();

        expired
    }

    /// Get statistics about lazy updates
    pub fn statistics(&self) -> LazyUpdateStatistics {
        let total_parameters = self.parameter_importance.len();
        let pending_count = self.pending_updates.len();

        let average_importance = if total_parameters > 0 {
            self.parameter_importance
                .values()
                .map(|imp| imp.importance_score)
                .sum::<f32>()
                / total_parameters as f32
        } else {
            0.0
        };

        let high_priority_pending = self
            .pending_updates
            .iter()
            .filter(|update| update.priority >= UpdatePriority::High)
            .count();

        LazyUpdateStatistics {
            total_parameters,
            pending_updates: pending_count,
            high_priority_pending,
            total_processed: self.total_updates_processed,
            total_skipped: self.total_updates_skipped,
            skip_ratio: if self.total_updates_processed + self.total_updates_skipped > 0 {
                self.total_updates_skipped as f32
                    / (self.total_updates_processed + self.total_updates_skipped) as f32
            } else {
                0.0
            },
            average_importance,
        }
    }

    /// Get parameter importance information
    pub fn get_parameter_importance(&self, parameter_id: &str) -> Option<&ParameterImportance> {
        self.parameter_importance.get(parameter_id)
    }

    /// Set custom threshold for a parameter
    pub fn set_parameter_threshold(&mut self, parameter_id: String, threshold: f32) {
        self.adaptive_thresholds.insert(parameter_id, threshold);
    }

    /// Clear statistics
    pub fn reset_statistics(&mut self) {
        self.total_updates_processed = 0;
        self.total_updates_skipped = 0;
    }

    // Private methods

    fn get_threshold(&self, parameter_id: &str) -> f32 {
        if let Some(&custom_threshold) = self.adaptive_thresholds.get(parameter_id) {
            return custom_threshold;
        }

        if !self.config.adaptive_threshold {
            return self.config.gradient_threshold;
        }

        if let Some(importance) = self.parameter_importance.get(parameter_id) {
            // Adaptive threshold based on parameter history
            let base_threshold = self.config.gradient_threshold;
            let variance_factor = (1.0 + importance.variance).sqrt();
            let importance_factor = 1.0 / (1.0 + importance.importance_score);

            base_threshold * variance_factor * importance_factor
        } else {
            self.config.gradient_threshold
        }
    }

    fn calculate_priority(
        &self,
        parameter_id: &str,
        gradient_magnitude: f32,
        importance: &ParameterImportance,
    ) -> UpdatePriority {
        if !self.config.importance_based_updates {
            return UpdatePriority::Medium;
        }

        let threshold = self.get_threshold(parameter_id);
        let magnitude_ratio = gradient_magnitude / threshold;

        let importance_factor = importance.importance_score;
        let time_factor = importance.last_significant_update.elapsed().as_secs_f32() / 60.0; // minutes

        let priority_score = magnitude_ratio * importance_factor * (1.0 + time_factor);

        if priority_score > 10.0 {
            UpdatePriority::Critical
        } else if priority_score > 3.0 {
            UpdatePriority::High
        } else if priority_score > 1.0 {
            UpdatePriority::Medium
        } else {
            UpdatePriority::Low
        }
    }

    fn should_update_immediately(
        &self,
        _parameter_id: &str,
        gradient_magnitude: f32,
        priority: UpdatePriority,
    ) -> bool {
        // Always update critical priority immediately
        if priority == UpdatePriority::Critical {
            return true;
        }

        // Update if gradient is significantly large (10x the normal threshold)
        if gradient_magnitude > self.config.gradient_threshold * 10.0 {
            return true;
        }

        false
    }

    fn should_process_batch(&self) -> bool {
        // Process batch if we have too many pending updates
        if self.pending_updates.len() >= self.config.max_pending_updates {
            return true;
        }

        // Process batch if we have high priority updates
        let high_priority_count = self
            .pending_updates
            .iter()
            .filter(|update| update.priority >= UpdatePriority::High)
            .count();

        if high_priority_count >= self.config.batch_size / 2 {
            return true;
        }

        // Process batch if maximum delay is reached
        let max_delay = Duration::from_millis(self.config.max_delay_ms);
        if let Some(oldest) = self.pending_updates.front() {
            if oldest.timestamp.elapsed() > max_delay {
                return true;
            }
        }

        false
    }

    fn insert_pending_update(&mut self, update: PendingUpdate) {
        // Insert in priority order
        let insert_pos = self
            .pending_updates
            .iter()
            .position(|existing| existing.priority < update.priority)
            .unwrap_or(self.pending_updates.len());

        self.pending_updates.insert(insert_pos, update);
    }
}

/// Decision for lazy update processing
#[derive(Debug)]
pub enum LazyUpdateDecision {
    /// Update the parameter immediately
    UpdateNow,
    /// Defer the update for later
    Defer,
    /// Process a batch of updates
    ProcessBatch(Vec<PendingUpdate>),
}

/// Statistics for lazy update system
#[derive(Debug, Clone)]
pub struct LazyUpdateStatistics {
    pub total_parameters: usize,
    pub pending_updates: usize,
    pub high_priority_pending: usize,
    pub total_processed: usize,
    pub total_skipped: usize,
    pub skip_ratio: f32,
    pub average_importance: f32,
}

impl std::fmt::Display for LazyUpdateStatistics {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Lazy Update Statistics:")?;
        writeln!(f, "  Total Parameters: {}", self.total_parameters)?;
        writeln!(f, "  Pending Updates: {}", self.pending_updates)?;
        writeln!(f, "  High Priority Pending: {}", self.high_priority_pending)?;
        writeln!(f, "  Total Processed: {}", self.total_processed)?;
        writeln!(f, "  Total Skipped: {}", self.total_skipped)?;
        writeln!(f, "  Skip Ratio: {:.1}%", self.skip_ratio * 100.0)?;
        writeln!(f, "  Average Importance: {:.3}", self.average_importance)?;
        Ok(())
    }
}

/// Trait for optimizers that support lazy updates
pub trait LazyUpdateSupport {
    /// Apply a single parameter update
    fn apply_update(&mut self, parameter_id: &str, gradient: &[f32]) -> Result<(), OptimizerError>;

    /// Apply a batch of parameter updates
    fn apply_batch_updates(&mut self, updates: &[PendingUpdate]) -> Result<(), OptimizerError> {
        for update in updates {
            self.apply_update(&update.parameter_id, &update.gradient)?;
        }
        Ok(())
    }

    /// Get the current gradient for a parameter
    fn get_parameter_gradient(&self, parameter_id: &str) -> Option<Vec<f32>>;

    /// Get list of all parameter IDs
    fn get_parameter_ids(&self) -> Vec<String>;
}

/// Wrapper optimizer that adds lazy update functionality
pub struct LazyUpdateOptimizer<T> {
    inner: T,
    lazy_manager: LazyUpdateManager,
    enabled: bool,
}

impl<T> LazyUpdateOptimizer<T>
where
    T: LazyUpdateSupport,
{
    /// Create a new lazy update optimizer wrapper
    pub fn new(inner: T, config: LazyUpdateConfig) -> Self {
        Self {
            inner,
            lazy_manager: LazyUpdateManager::new(config),
            enabled: true,
        }
    }

    /// Enable or disable lazy updates
    pub fn set_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;

        if !enabled {
            // Process all pending updates immediately
            let pending = self.lazy_manager.flush_all_updates();
            let _ = self.inner.apply_batch_updates(&pending);
        }
    }

    /// Get the inner optimizer
    pub fn inner(&self) -> &T {
        &self.inner
    }

    /// Get the inner optimizer mutably
    pub fn inner_mut(&mut self) -> &mut T {
        &mut self.inner
    }

    /// Get the lazy update manager
    pub fn lazy_manager(&self) -> &LazyUpdateManager {
        &self.lazy_manager
    }

    /// Get the lazy update manager mutably
    pub fn lazy_manager_mut(&mut self) -> &mut LazyUpdateManager {
        &mut self.lazy_manager
    }

    /// Submit gradients for lazy processing
    pub fn submit_gradients(
        &mut self,
        gradients: HashMap<String, Vec<f32>>,
    ) -> Result<(), OptimizerError> {
        if !self.enabled {
            // Apply all gradients immediately
            for (param_id, gradient) in gradients {
                self.inner.apply_update(&param_id, &gradient)?;
            }
            return Ok(());
        }

        // Process each gradient through the lazy update system
        for (param_id, gradient) in gradients {
            match self.lazy_manager.submit_gradient(param_id, gradient) {
                LazyUpdateDecision::UpdateNow => {
                    // This shouldn't happen in practice since we already processed the gradient
                }
                LazyUpdateDecision::Defer => {
                    // Nothing to do, update is deferred
                }
                LazyUpdateDecision::ProcessBatch(updates) => {
                    self.inner.apply_batch_updates(&updates)?;
                }
            }
        }

        // Check for expired updates
        let expired = self.lazy_manager.get_expired_updates();
        if !expired.is_empty() {
            self.inner.apply_batch_updates(&expired)?;
        }

        Ok(())
    }

    /// Force processing of all pending updates
    pub fn flush_pending_updates(&mut self) -> Result<(), OptimizerError> {
        let pending = self.lazy_manager.flush_all_updates();
        self.inner.apply_batch_updates(&pending)
    }

    /// Get statistics about lazy updates
    pub fn statistics(&self) -> LazyUpdateStatistics {
        self.lazy_manager.statistics()
    }
}

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

    #[derive(Debug)]
    struct MockOptimizer {
        parameters: HashMap<String, Vec<f32>>,
        gradients: HashMap<String, Vec<f32>>,
        update_count: usize,
    }

    impl MockOptimizer {
        fn new() -> Self {
            let mut parameters = HashMap::new();
            parameters.insert("layer1.weight".to_string(), vec![1.0, 2.0, 3.0]);
            parameters.insert("layer1.bias".to_string(), vec![0.1, 0.2]);

            Self {
                parameters,
                gradients: HashMap::new(),
                update_count: 0,
            }
        }
    }

    impl LazyUpdateSupport for MockOptimizer {
        fn apply_update(
            &mut self,
            parameter_id: &str,
            gradient: &[f32],
        ) -> Result<(), OptimizerError> {
            self.gradients
                .insert(parameter_id.to_string(), gradient.to_vec());
            self.update_count += 1;
            Ok(())
        }

        fn get_parameter_gradient(&self, parameter_id: &str) -> Option<Vec<f32>> {
            self.gradients.get(parameter_id).cloned()
        }

        fn get_parameter_ids(&self) -> Vec<String> {
            self.parameters.keys().cloned().collect()
        }
    }

    #[test]
    fn test_lazy_update_manager() {
        let config = LazyUpdateConfig {
            gradient_threshold: 0.1,
            max_pending_updates: 5,
            ..Default::default()
        };

        let mut manager = LazyUpdateManager::new(config);

        // Submit a small gradient (should be deferred)
        let decision = manager.submit_gradient("param1".to_string(), vec![0.01, 0.02]);
        assert!(matches!(decision, LazyUpdateDecision::Defer));

        // Submit a large gradient (should trigger immediate update)
        let decision = manager.submit_gradient("param2".to_string(), vec![1.0, 2.0]);
        assert!(matches!(decision, LazyUpdateDecision::UpdateNow));

        // Submit multiple small gradients to trigger batch processing
        for i in 0..6 {
            let decision = manager.submit_gradient(format!("param{}", i), vec![0.05, 0.06]);
            if i == 3 {
                // After adding param3, we have 5 pending updates (max_pending_updates),
                // so this should trigger batch processing
                assert!(matches!(decision, LazyUpdateDecision::ProcessBatch(_)));
            } else if i < 3 {
                // Before reaching the batch threshold, updates should be deferred
                assert!(matches!(decision, LazyUpdateDecision::Defer));
            }
            // After i=3, we start accumulating again, so i=4 and i=5 should defer
        }
    }

    #[test]
    fn test_lazy_update_optimizer() {
        let config = LazyUpdateConfig {
            gradient_threshold: 0.1,
            max_pending_updates: 3,
            ..Default::default()
        };

        let optimizer = MockOptimizer::new();
        let mut lazy_optimizer = LazyUpdateOptimizer::new(optimizer, config);

        // Submit gradients
        let mut gradients = HashMap::new();
        gradients.insert("layer1.weight".to_string(), vec![0.01, 0.02, 0.03]);
        gradients.insert("layer1.bias".to_string(), vec![0.05, 0.06]);

        lazy_optimizer.submit_gradients(gradients).unwrap();

        // Check that not all updates were applied immediately
        assert!(lazy_optimizer.inner().update_count < 2);

        // Flush pending updates
        lazy_optimizer.flush_pending_updates().unwrap();

        // Now all updates should be applied
        assert!(lazy_optimizer.inner().update_count >= 2);
    }

    #[test]
    fn test_parameter_importance() {
        let mut importance = ParameterImportance::new("test_param".to_string());

        // Update with various gradient magnitudes
        importance.update(0.5, true);
        assert!(importance.importance_score > 0.0);

        importance.update(0.1, false);
        importance.update(0.8, true);

        // Importance score should reflect the update pattern
        assert!(importance.average_gradient_magnitude > 0.0);
        assert!(importance.update_frequency > 0.0);
    }
}