optirs-learned 0.3.1

OptiRS learned optimizers and meta-learning
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// Architecture patterns and connection topologies
//
// This module defines common architectural patterns, connection topologies,
// and optimization paths used in neural architecture search.

use std::collections::HashMap;

use super::components::*;

/// Connection topology patterns
#[derive(Debug, Clone)]
pub struct ConnectionTopology {
    /// Topology type
    pub topology_type: TopologyType,

    /// Connection density
    pub density: f64,

    /// Connection pattern
    pub pattern: ConnectionPattern,

    /// Layer-specific connections
    pub layer_connections: HashMap<usize, Vec<usize>>,

    /// Skip connection configuration
    pub skip_configuration: SkipConfiguration,
}

/// Types of connection topologies
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TopologyType {
    Sequential,
    Parallel,
    Tree,
    Graph,
    Mesh,
    Star,
    Ring,
    Custom,
}

/// Skip connection configuration
#[derive(Debug, Clone)]
pub struct SkipConfiguration {
    /// Skip connection type
    pub skip_type: SkipConnectionType,

    /// Skip intervals (every N layers)
    pub skip_intervals: Vec<usize>,

    /// Adaptive skip connections
    pub adaptive_skips: bool,

    /// Skip connection weights
    pub skip_weights: HashMap<(usize, usize), f64>,
}

/// Residual connection patterns
#[derive(Debug, Clone)]
pub struct ResidualPattern {
    /// Pattern type
    pub pattern_type: ResidualPatternType,

    /// Block size for residual connections
    pub block_size: usize,

    /// Bottleneck configuration
    pub bottleneck_config: Option<BottleneckConfig>,

    /// Normalization placement
    pub norm_placement: NormPlacement,

    /// Activation placement
    pub activation_placement: ActivationPlacement,
}

/// Types of residual patterns
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ResidualPatternType {
    Basic,          // Basic residual block
    Bottleneck,     // Bottleneck residual block
    PreActivation,  // Pre-activation residual
    DenseNet,       // DenseNet-style connections
    Highway,        // Highway networks
    ResNeXt,        // ResNeXt-style blocks
    Custom,
}

/// Bottleneck configuration
#[derive(Debug, Clone)]
pub struct BottleneckConfig {
    /// Reduction ratio
    pub reduction_ratio: f64,

    /// Intermediate dimensions
    pub intermediate_dims: Vec<usize>,

    /// Use 1x1 convolutions
    pub use_pointwise: bool,
}

/// Normalization placement in residual blocks
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum NormPlacement {
    PreActivation,
    PostActivation,
    Both,
    None,
}

/// Activation placement in residual blocks
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ActivationPlacement {
    PreNorm,
    PostNorm,
    Both,
    None,
}

/// Attention connectivity patterns
#[derive(Debug, Clone)]
pub struct AttentionConnectivity {
    /// Attention pattern
    pub attention_pattern: AttentionPatternType,

    /// Connectivity matrix
    pub connectivity_matrix: Option<Vec<Vec<bool>>>,

    /// Local attention window size
    pub local_window_size: Option<usize>,

    /// Sparse attention configuration
    pub sparse_config: Option<SparseAttentionPattern>,

    /// Cross-layer attention
    pub cross_layer_attention: bool,
}

/// Types of attention patterns
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AttentionPatternType {
    Full,         // Full attention
    Local,        // Local attention window
    Sparse,       // Sparse attention
    Blocked,      // Block-sparse attention
    Strided,      // Strided attention
    Random,       // Random sparse attention
    Learned,      // Learned attention pattern
    Hierarchical, // Hierarchical attention
}

/// Sparse attention pattern configuration
#[derive(Debug, Clone)]
pub struct SparseAttentionPattern {
    /// Sparsity ratio
    pub sparsity_ratio: f64,

    /// Block size for blocked attention
    pub block_size: Option<usize>,

    /// Stride for strided attention
    pub stride: Option<usize>,

    /// Pattern regularity
    pub pattern_regularity: PatternRegularity,
}

/// Pattern regularity for sparse attention
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PatternRegularity {
    Regular,      // Regular pattern
    Irregular,    // Irregular pattern
    Adaptive,     // Adaptive based on input
    Learned,      // Learned during training
}

/// Memory access strategies
#[derive(Debug, Clone)]
pub struct MemoryAccessStrategy {
    /// Access pattern type
    pub access_type: MemoryAccessType,

    /// Prefetch strategy
    pub prefetch_strategy: PrefetchStrategy,

    /// Cache optimization
    pub cache_optimization: CacheOptimization,

    /// Memory layout
    pub memory_layout: MemoryLayout,
}

/// Types of memory access patterns
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MemoryAccessType {
    Sequential,
    Random,
    Strided,
    Blocked,
    Hierarchical,
    ContentAddressable,
    Adaptive,
}

/// Prefetch strategies
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PrefetchStrategy {
    None,
    Conservative,
    Aggressive,
    Adaptive,
    Learned,
}

/// Cache optimization strategies
#[derive(Debug, Clone)]
pub struct CacheOptimization {
    /// Cache-friendly data layout
    pub data_layout_optimization: bool,

    /// Cache blocking strategy
    pub blocking_strategy: BlockingStrategy,

    /// Cache line utilization
    pub cache_line_optimization: bool,
}

/// Blocking strategies for cache optimization
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum BlockingStrategy {
    None,
    Basic,
    Hierarchical,
    Adaptive,
    Custom,
}

/// Memory layout patterns
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MemoryLayout {
    RowMajor,
    ColumnMajor,
    Blocked,
    Tiled,
    Custom,
}

/// Optimization path patterns
#[derive(Debug, Clone)]
pub struct OptimizationPath {
    /// Path type
    pub path_type: OptimizationPathType,

    /// Search trajectory
    pub search_trajectory: SearchTrajectory,

    /// Exploration strategy
    pub exploration_strategy: ExplorationStrategy,

    /// Convergence pattern
    pub convergence_pattern: ConvergencePattern,
}

/// Types of optimization paths
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OptimizationPathType {
    Greedy,
    Evolutionary,
    Gradient,
    Bayesian,
    Reinforcement,
    Hybrid,
    Progressive,
}

/// Search trajectory configuration
#[derive(Debug, Clone)]
pub struct SearchTrajectory {
    /// Trajectory smoothness
    pub smoothness: f64,

    /// Exploration vs exploitation balance
    pub exploration_ratio: f64,

    /// Restart strategy
    pub restart_strategy: RestartStrategy,

    /// Path memory
    pub path_memory_size: usize,
}

/// Restart strategies for optimization
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum RestartStrategy {
    None,
    Periodic,
    Adaptive,
    Performance,
    Diversity,
}

/// Exploration strategies
#[derive(Debug, Clone)]
pub struct ExplorationStrategy {
    /// Strategy type
    pub strategy_type: ExplorationStrategyType,

    /// Exploration rate schedule
    pub exploration_schedule: ExplorationSchedule,

    /// Novelty detection
    pub novelty_detection: bool,
}

/// Types of exploration strategies
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ExplorationStrategyType {
    Random,
    Systematic,
    Curiosity,
    Diversity,
    Uncertainty,
    Hybrid,
}

/// Exploration rate scheduling
#[derive(Debug, Clone)]
pub struct ExplorationSchedule {
    /// Initial exploration rate
    pub initial_rate: f64,

    /// Final exploration rate
    pub final_rate: f64,

    /// Decay type
    pub decay_type: DecayType,

    /// Decay steps
    pub decay_steps: usize,
}

/// Types of decay for exploration
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DecayType {
    Linear,
    Exponential,
    Cosine,
    Polynomial,
    Step,
    Adaptive,
}

/// Convergence patterns
#[derive(Debug, Clone)]
pub struct ConvergencePattern {
    /// Pattern type
    pub pattern_type: ConvergencePatternType,

    /// Early stopping criteria
    pub early_stopping: EarlyStopping,

    /// Convergence detection
    pub convergence_detection: ConvergenceDetection,
}

/// Types of convergence patterns
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ConvergencePatternType {
    Monotonic,
    Oscillatory,
    Plateau,
    Stochastic,
    MultiModal,
    Adaptive,
}

/// Early stopping configuration
#[derive(Debug, Clone)]
pub struct EarlyStopping {
    /// Patience (iterations without improvement)
    pub patience: usize,

    /// Minimum improvement threshold
    pub min_delta: f64,

    /// Monitor metric
    pub monitor_metric: String,

    /// Restore best weights
    pub restore_best: bool,
}

/// Convergence detection methods
#[derive(Debug, Clone)]
pub struct ConvergenceDetection {
    /// Detection method
    pub method: ConvergenceDetectionMethod,

    /// Window size for detection
    pub window_size: usize,

    /// Convergence threshold
    pub threshold: f64,
}

/// Methods for detecting convergence
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ConvergenceDetectionMethod {
    ThresholdBased,
    TrendAnalysis,
    Statistical,
    Plateau,
    Gradient,
    Hybrid,
}

// Implementation methods
impl Default for ConnectionTopology {
    fn default() -> Self {
        Self {
            topology_type: TopologyType::Sequential,
            density: 1.0,
            pattern: ConnectionPattern::Sequential,
            layer_connections: HashMap::new(),
            skip_configuration: SkipConfiguration::default(),
        }
    }
}

impl Default for SkipConfiguration {
    fn default() -> Self {
        Self {
            skip_type: SkipConnectionType::None,
            skip_intervals: vec![],
            adaptive_skips: false,
            skip_weights: HashMap::new(),
        }
    }
}

impl Default for ResidualPattern {
    fn default() -> Self {
        Self {
            pattern_type: ResidualPatternType::Basic,
            block_size: 2,
            bottleneck_config: None,
            norm_placement: NormPlacement::PostActivation,
            activation_placement: ActivationPlacement::PostNorm,
        }
    }
}

impl Default for AttentionConnectivity {
    fn default() -> Self {
        Self {
            attention_pattern: AttentionPatternType::Full,
            connectivity_matrix: None,
            local_window_size: None,
            sparse_config: None,
            cross_layer_attention: false,
        }
    }
}

impl Default for MemoryAccessStrategy {
    fn default() -> Self {
        Self {
            access_type: MemoryAccessType::Sequential,
            prefetch_strategy: PrefetchStrategy::Conservative,
            cache_optimization: CacheOptimization {
                data_layout_optimization: true,
                blocking_strategy: BlockingStrategy::Basic,
                cache_line_optimization: true,
            },
            memory_layout: MemoryLayout::RowMajor,
        }
    }
}

impl Default for OptimizationPath {
    fn default() -> Self {
        Self {
            path_type: OptimizationPathType::Greedy,
            search_trajectory: SearchTrajectory {
                smoothness: 0.5,
                exploration_ratio: 0.1,
                restart_strategy: RestartStrategy::None,
                path_memory_size: 100,
            },
            exploration_strategy: ExplorationStrategy {
                strategy_type: ExplorationStrategyType::Random,
                exploration_schedule: ExplorationSchedule {
                    initial_rate: 1.0,
                    final_rate: 0.01,
                    decay_type: DecayType::Exponential,
                    decay_steps: 1000,
                },
                novelty_detection: false,
            },
            convergence_pattern: ConvergencePattern {
                pattern_type: ConvergencePatternType::Monotonic,
                early_stopping: EarlyStopping {
                    patience: 10,
                    min_delta: 0.001,
                    monitor_metric: "loss".to_string(),
                    restore_best: true,
                },
                convergence_detection: ConvergenceDetection {
                    method: ConvergenceDetectionMethod::ThresholdBased,
                    window_size: 10,
                    threshold: 0.001,
                },
            },
        }
    }
}

impl ConnectionTopology {
    /// Create a sequential topology
    pub fn sequential(num_layers: usize) -> Self {
        let mut layer_connections = HashMap::new();
        for i in 0..num_layers - 1 {
            layer_connections.insert(i, vec![i + 1]);
        }

        Self {
            topology_type: TopologyType::Sequential,
            density: 1.0 / (num_layers as f64 - 1.0),
            pattern: ConnectionPattern::Sequential,
            layer_connections,
            skip_configuration: SkipConfiguration::default(),
        }
    }

    /// Create a residual topology with skip connections
    pub fn residual(num_layers: usize, skip_interval: usize) -> Self {
        let mut topology = Self::sequential(num_layers);
        topology.topology_type = TopologyType::Custom;
        topology.pattern = ConnectionPattern::Residual;

        // Add skip connections
        for i in 0..num_layers {
            if i + skip_interval < num_layers {
                topology
                    .layer_connections
                    .entry(i)
                    .or_insert_with(Vec::new)
                    .push(i + skip_interval);
            }
        }

        topology.skip_configuration = SkipConfiguration {
            skip_type: SkipConnectionType::Residual,
            skip_intervals: vec![skip_interval],
            adaptive_skips: false,
            skip_weights: HashMap::new(),
        };

        topology
    }

    /// Calculate connectivity density
    pub fn calculate_density(&self) -> f64 {
        let total_connections: usize = self.layer_connections.values().map(|v| v.len()).sum();
        let max_connections = self.layer_connections.len() * self.layer_connections.len();
        
        if max_connections == 0 {
            0.0
        } else {
            total_connections as f64 / max_connections as f64
        }
    }

    /// Check if topology is valid
    pub fn is_valid(&self) -> bool {
        // Check for cycles (simplified check)
        for (from, connections) in &self.layer_connections {
            for &to in connections {
                if to <= *from {
                    // Backward connection detected - could be a cycle
                    continue; // Allow for some patterns like residual
                }
            }
        }
        true
    }
}

impl ResidualPattern {
    /// Create a basic residual pattern
    pub fn basic(block_size: usize) -> Self {
        Self {
            pattern_type: ResidualPatternType::Basic,
            block_size,
            bottleneck_config: None,
            norm_placement: NormPlacement::PostActivation,
            activation_placement: ActivationPlacement::PostNorm,
        }
    }

    /// Create a bottleneck residual pattern
    pub fn bottleneck(block_size: usize, reduction_ratio: f64) -> Self {
        Self {
            pattern_type: ResidualPatternType::Bottleneck,
            block_size,
            bottleneck_config: Some(BottleneckConfig {
                reduction_ratio,
                intermediate_dims: vec![],
                use_pointwise: true,
            }),
            norm_placement: NormPlacement::PostActivation,
            activation_placement: ActivationPlacement::PostNorm,
        }
    }

    /// Get computational overhead
    pub fn computational_overhead(&self) -> f64 {
        match self.pattern_type {
            ResidualPatternType::Basic => 1.1,
            ResidualPatternType::Bottleneck => 0.8,
            ResidualPatternType::PreActivation => 1.05,
            ResidualPatternType::DenseNet => 1.5,
            ResidualPatternType::Highway => 1.3,
            ResidualPatternType::ResNeXt => 1.2,
            ResidualPatternType::Custom => 1.0,
        }
    }
}

impl AttentionConnectivity {
    /// Create full attention connectivity
    pub fn full_attention(num_heads: usize) -> Self {
        Self {
            attention_pattern: AttentionPatternType::Full,
            connectivity_matrix: None,
            local_window_size: None,
            sparse_config: None,
            cross_layer_attention: false,
        }
    }

    /// Create local attention connectivity
    pub fn local_attention(window_size: usize) -> Self {
        Self {
            attention_pattern: AttentionPatternType::Local,
            connectivity_matrix: None,
            local_window_size: Some(window_size),
            sparse_config: None,
            cross_layer_attention: false,
        }
    }

    /// Create sparse attention connectivity
    pub fn sparse_attention(sparsity_ratio: f64) -> Self {
        Self {
            attention_pattern: AttentionPatternType::Sparse,
            connectivity_matrix: None,
            local_window_size: None,
            sparse_config: Some(SparseAttentionPattern {
                sparsity_ratio,
                block_size: None,
                stride: None,
                pattern_regularity: PatternRegularity::Regular,
            }),
            cross_layer_attention: false,
        }
    }

    /// Calculate attention complexity
    pub fn attention_complexity(&self, sequence_length: usize) -> f64 {
        match self.attention_pattern {
            AttentionPatternType::Full => (sequence_length * sequence_length) as f64,
            AttentionPatternType::Local => {
                let window = self.local_window_size.unwrap_or(8);
                (sequence_length * window) as f64
            }
            AttentionPatternType::Sparse => {
                let sparsity = self
                    .sparse_config
                    .as_ref()
                    .map(|c| c.sparsity_ratio)
                    .unwrap_or(0.1);
                (sequence_length * sequence_length) as f64 * sparsity
            }
            _ => (sequence_length * sequence_length) as f64 * 0.5,
        }
    }
}

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

    #[test]
    fn test_sequential_topology() {
        let topology = ConnectionTopology::sequential(5);
        assert_eq!(topology.topology_type, TopologyType::Sequential);
        assert_eq!(topology.layer_connections.len(), 4);
    }

    #[test]
    fn test_residual_topology() {
        let topology = ConnectionTopology::residual(6, 2);
        assert_eq!(topology.pattern, ConnectionPattern::Residual);
        assert_eq!(topology.skip_configuration.skip_intervals, vec![2]);
    }

    #[test]
    fn test_density_calculation() {
        let topology = ConnectionTopology::sequential(4);
        let density = topology.calculate_density();
        assert!(density > 0.0 && density <= 1.0);
    }

    #[test]
    fn test_residual_pattern_overhead() {
        let basic = ResidualPattern::basic(2);
        let bottleneck = ResidualPattern::bottleneck(2, 0.25);
        
        assert!(basic.computational_overhead() > 1.0);
        assert!(bottleneck.computational_overhead() < basic.computational_overhead());
    }

    #[test]
    fn test_attention_complexity() {
        let full = AttentionConnectivity::full_attention(8);
        let local = AttentionConnectivity::local_attention(16);
        let sparse = AttentionConnectivity::sparse_attention(0.1);

        let seq_len = 128;
        assert!(full.attention_complexity(seq_len) > local.attention_complexity(seq_len));
        assert!(sparse.attention_complexity(seq_len) < full.attention_complexity(seq_len));
    }

    #[test]
    fn test_topology_validity() {
        let topology = ConnectionTopology::sequential(3);
        assert!(topology.is_valid());
    }
}