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
// Architecture components and basic building blocks
//
// This module defines the fundamental architectural components used in
// neural architecture search for learned optimizers.

/// Types of neural network layers
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LayerType {
    Linear,
    LSTM,
    GRU,
    Transformer,
    Convolutional1D,
    Attention,
    Recurrent,
    Highway,
    Residual,
    Dense,
    Embedding,
    Custom,
}

/// Activation function types
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ActivationType {
    ReLU,
    Tanh,
    Sigmoid,
    GELU,
    Swish,
    Mish,
    ELU,
    LeakyReLU,
    PReLU,
    Linear,
}

/// Connection patterns between layers
#[derive(Debug, Clone, Copy)]
pub enum ConnectionPattern {
    Sequential,
    Residual,
    DenseNet,
    UNet,
    Attention,
    Recurrent,
    Hybrid,
    Custom,
}

/// Types of attention mechanisms
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AttentionType {
    None,
    SelfAttention,
    MultiHeadAttention,
    CrossAttention,
    LocalAttention,
    SparseAttention,
    AdaptiveAttention,
}

/// Normalization types
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum NormalizationType {
    None,
    BatchNorm,
    LayerNorm,
    GroupNorm,
    InstanceNorm,
    AdaptiveNorm,
}

/// Optimizer-specific components
#[derive(Debug, Clone, Copy)]
pub enum OptimizerComponent {
    MomentumTracker,
    AdaptiveLearningRate,
    GradientClipping,
    NoiseInjection,
    CurvatureEstimation,
    SecondOrderInfo,
    MetaGradients,
}

/// Memory mechanism types
#[derive(Debug, Clone, Copy)]
pub enum MemoryType {
    None,
    ShortTerm,
    LongTerm,
    Episodic,
    WorkingMemory,
    ExternalMemory,
    AdaptiveMemory,
}

/// Skip connection types
#[derive(Debug, Clone, Copy)]
pub enum SkipConnectionType {
    None,
    Residual,
    Dense,
    Highway,
    Gated,
    Attention,
    Adaptive,
}

/// Memory management strategies
#[derive(Debug, Clone, Copy)]
pub enum MemoryManagementStrategy {
    Standard,
    Optimized,
    LowMemory,
}

/// Sparsity patterns
#[derive(Debug, Clone, Copy)]
pub enum SparsityPattern {
    Random,
    Local,
    Strided,
    Block,
    Learned,
}

/// Memory access patterns
#[derive(Debug, Clone, Copy)]
pub enum MemoryAccessPattern {
    Sequential,
    Random,
    Hierarchical,
    ContentAddressable,
    Adaptive,
}

impl Default for LayerType {
    fn default() -> Self {
        LayerType::Linear
    }
}

impl Default for ActivationType {
    fn default() -> Self {
        ActivationType::ReLU
    }
}

impl Default for ConnectionPattern {
    fn default() -> Self {
        ConnectionPattern::Sequential
    }
}

impl Default for AttentionType {
    fn default() -> Self {
        AttentionType::None
    }
}

impl Default for NormalizationType {
    fn default() -> Self {
        NormalizationType::None
    }
}

impl Default for OptimizerComponent {
    fn default() -> Self {
        OptimizerComponent::MomentumTracker
    }
}

impl Default for MemoryType {
    fn default() -> Self {
        MemoryType::None
    }
}

impl Default for SkipConnectionType {
    fn default() -> Self {
        SkipConnectionType::None
    }
}

impl Default for MemoryManagementStrategy {
    fn default() -> Self {
        MemoryManagementStrategy::Standard
    }
}

impl Default for SparsityPattern {
    fn default() -> Self {
        SparsityPattern::Random
    }
}

impl Default for MemoryAccessPattern {
    fn default() -> Self {
        MemoryAccessPattern::Sequential
    }
}

// Component validation and utility methods
impl LayerType {
    /// Check if the layer type supports attention mechanisms
    pub fn supports_attention(&self) -> bool {
        matches!(
            self,
            LayerType::Transformer | LayerType::Attention | LayerType::Custom
        )
    }

    /// Check if the layer type is recurrent
    pub fn is_recurrent(&self) -> bool {
        matches!(self, LayerType::LSTM | LayerType::GRU | LayerType::Recurrent)
    }

    /// Get the typical parameter count multiplier for this layer type
    pub fn parameter_multiplier(&self) -> f64 {
        match self {
            LayerType::Linear => 1.0,
            LayerType::LSTM => 4.0,
            LayerType::GRU => 3.0,
            LayerType::Transformer => 4.0,
            LayerType::Convolutional1D => 0.5,
            LayerType::Attention => 3.0,
            LayerType::Recurrent => 3.0,
            LayerType::Highway => 2.0,
            LayerType::Residual => 1.2,
            LayerType::Dense => 1.0,
            LayerType::Embedding => 0.3,
            LayerType::Custom => 1.0,
        }
    }
}

impl ActivationType {
    /// Check if the activation function is differentiable
    pub fn is_differentiable(&self) -> bool {
        !matches!(self, ActivationType::Linear)
    }

    /// Get the computational cost factor
    pub fn computational_cost(&self) -> f64 {
        match self {
            ActivationType::ReLU => 0.1,
            ActivationType::Tanh => 0.8,
            ActivationType::Sigmoid => 0.7,
            ActivationType::GELU => 1.0,
            ActivationType::Swish => 0.9,
            ActivationType::Mish => 1.1,
            ActivationType::ELU => 0.6,
            ActivationType::LeakyReLU => 0.2,
            ActivationType::PReLU => 0.3,
            ActivationType::Linear => 0.0,
        }
    }

    /// Check if the activation requires learnable parameters
    pub fn has_learnable_parameters(&self) -> bool {
        matches!(self, ActivationType::PReLU)
    }
}

impl ConnectionPattern {
    /// Check if the pattern requires special connectivity handling
    pub fn requires_special_connectivity(&self) -> bool {
        matches!(
            self,
            ConnectionPattern::Residual
                | ConnectionPattern::DenseNet
                | ConnectionPattern::UNet
                | ConnectionPattern::Attention
                | ConnectionPattern::Hybrid
                | ConnectionPattern::Custom
        )
    }

    /// Get the memory overhead factor for this connection pattern
    pub fn memory_overhead(&self) -> f64 {
        match self {
            ConnectionPattern::Sequential => 1.0,
            ConnectionPattern::Residual => 1.1,
            ConnectionPattern::DenseNet => 1.5,
            ConnectionPattern::UNet => 1.3,
            ConnectionPattern::Attention => 1.4,
            ConnectionPattern::Recurrent => 1.2,
            ConnectionPattern::Hybrid => 1.6,
            ConnectionPattern::Custom => 1.0,
        }
    }
}

impl AttentionType {
    /// Get the computational complexity factor
    pub fn complexity_factor(&self) -> f64 {
        match self {
            AttentionType::None => 0.0,
            AttentionType::SelfAttention => 1.0,
            AttentionType::MultiHeadAttention => 1.5,
            AttentionType::CrossAttention => 1.2,
            AttentionType::LocalAttention => 0.8,
            AttentionType::SparseAttention => 0.6,
            AttentionType::AdaptiveAttention => 1.8,
        }
    }

    /// Check if the attention type supports multi-head operation
    pub fn supports_multi_head(&self) -> bool {
        matches!(
            self,
            AttentionType::MultiHeadAttention | AttentionType::AdaptiveAttention
        )
    }
}

impl OptimizerComponent {
    /// Get the computational overhead for this component
    pub fn computational_overhead(&self) -> f64 {
        match self {
            OptimizerComponent::MomentumTracker => 0.1,
            OptimizerComponent::AdaptiveLearningRate => 0.2,
            OptimizerComponent::GradientClipping => 0.05,
            OptimizerComponent::NoiseInjection => 0.08,
            OptimizerComponent::CurvatureEstimation => 0.5,
            OptimizerComponent::SecondOrderInfo => 0.8,
            OptimizerComponent::MetaGradients => 0.3,
        }
    }

    /// Get the memory overhead for this component
    pub fn memory_overhead(&self) -> f64 {
        match self {
            OptimizerComponent::MomentumTracker => 1.0,
            OptimizerComponent::AdaptiveLearningRate => 0.5,
            OptimizerComponent::GradientClipping => 0.1,
            OptimizerComponent::NoiseInjection => 0.0,
            OptimizerComponent::CurvatureEstimation => 2.0,
            OptimizerComponent::SecondOrderInfo => 3.0,
            OptimizerComponent::MetaGradients => 1.5,
        }
    }
}

impl MemoryType {
    /// Get the memory capacity multiplier
    pub fn capacity_multiplier(&self) -> f64 {
        match self {
            MemoryType::None => 0.0,
            MemoryType::ShortTerm => 0.1,
            MemoryType::LongTerm => 1.0,
            MemoryType::Episodic => 0.5,
            MemoryType::WorkingMemory => 0.3,
            MemoryType::ExternalMemory => 2.0,
            MemoryType::AdaptiveMemory => 0.8,
        }
    }

    /// Check if the memory type requires persistence
    pub fn requires_persistence(&self) -> bool {
        matches!(
            self,
            MemoryType::LongTerm | MemoryType::Episodic | MemoryType::ExternalMemory
        )
    }
}

// Compatibility checking functions
pub fn are_components_compatible(
    layer_type: LayerType,
    activation: ActivationType,
    attention: AttentionType,
) -> bool {
    // Check if layer type supports the attention mechanism
    if attention != AttentionType::None && !layer_type.supports_attention() {
        return false;
    }

    // Check activation compatibility with layer type
    match layer_type {
        LayerType::LSTM | LayerType::GRU => {
            // Recurrent layers work better with saturating activations
            matches!(
                activation,
                ActivationType::Tanh | ActivationType::Sigmoid | ActivationType::GELU
            )
        }
        LayerType::Transformer => {
            // Transformers typically use GELU or ReLU
            matches!(
                activation,
                ActivationType::GELU | ActivationType::ReLU | ActivationType::Swish
            )
        }
        _ => true, // Most combinations are valid
    }
}

pub fn estimate_component_cost(
    layer_type: LayerType,
    activation: ActivationType,
    attention: AttentionType,
    memory_type: MemoryType,
) -> f64 {
    let layer_cost = layer_type.parameter_multiplier();
    let activation_cost = activation.computational_cost();
    let attention_cost = attention.complexity_factor();
    let memory_cost = memory_type.capacity_multiplier();

    layer_cost + activation_cost + attention_cost + memory_cost
}

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

    #[test]
    fn test_layer_type_properties() {
        assert!(LayerType::Transformer.supports_attention());
        assert!(!LayerType::Linear.supports_attention());
        assert!(LayerType::LSTM.is_recurrent());
        assert!(!LayerType::Linear.is_recurrent());
    }

    #[test]
    fn test_activation_properties() {
        assert!(ActivationType::ReLU.is_differentiable());
        assert!(!ActivationType::Linear.is_differentiable());
        assert!(ActivationType::PReLU.has_learnable_parameters());
        assert!(!ActivationType::ReLU.has_learnable_parameters());
    }

    #[test]
    fn test_component_compatibility() {
        assert!(are_components_compatible(
            LayerType::Transformer,
            ActivationType::GELU,
            AttentionType::MultiHeadAttention
        ));
        
        assert!(!are_components_compatible(
            LayerType::Linear,
            ActivationType::ReLU,
            AttentionType::SelfAttention
        ));
    }

    #[test]
    fn test_cost_estimation() {
        let cost = estimate_component_cost(
            LayerType::LSTM,
            ActivationType::Tanh,
            AttentionType::None,
            MemoryType::ShortTerm
        );
        assert!(cost > 0.0);
    }
}