optirs-learned 0.3.2

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
// Configuration structures for transformer-based optimizer

use super::positional_encoding::PositionalEncodingType;
use scirs2_core::numeric::Float;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

/// Configuration for transformer-based optimizer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransformerBasedOptimizerConfig<
    T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static,
> {
    /// Model dimension (embedding size)
    pub model_dimension: usize,

    /// Number of transformer layers
    pub num_transformer_layers: usize,

    /// Number of attention heads
    pub num_attention_heads: usize,

    /// Dimension of each attention head
    pub attention_head_dimension: usize,

    /// Feed-forward network dimension
    pub feedforward_dimension: usize,

    /// Maximum sequence length
    pub sequence_length: usize,

    /// Dropout rate
    pub dropout_rate: f64,

    /// Learning rate
    pub learning_rate: T,

    /// Batch size for training
    pub batch_size: usize,

    /// Number of training epochs
    pub num_epochs: usize,

    /// Activation function type
    pub activation_function: ActivationFunction,

    /// Positional encoding type
    pub positional_encoding_type: PositionalEncodingType,

    /// Memory management configuration
    pub memory_config: MemoryConfig,

    /// Meta-learning configuration
    pub meta_learning_config: MetaLearningConfig<T>,

    /// Performance tracking configuration
    pub performance_config: PerformanceConfig,

    /// Enable gradient clipping
    pub enable_gradient_clipping: bool,

    /// Gradient clipping threshold
    pub gradient_clip_value: T,

    /// Weight decay factor
    pub weight_decay: T,

    /// Warmup steps for learning rate schedule
    pub warmup_steps: usize,

    /// Enable layer normalization
    pub enable_layer_norm: bool,

    /// Pre-norm vs post-norm
    pub use_pre_norm: bool,

    /// Enable residual connections
    pub enable_residual_connections: bool,
}

impl<T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static> Default
    for TransformerBasedOptimizerConfig<T>
{
    fn default() -> Self {
        Self {
            model_dimension: 512,
            num_transformer_layers: 6,
            num_attention_heads: 8,
            attention_head_dimension: 64,
            feedforward_dimension: 2048,
            sequence_length: 128,
            dropout_rate: 0.1,
            learning_rate: scirs2_core::numeric::NumCast::from(1e-4).unwrap_or_else(|| T::zero()),
            batch_size: 32,
            num_epochs: 100,
            activation_function: ActivationFunction::ReLU,
            positional_encoding_type: PositionalEncodingType::Sinusoidal,
            memory_config: MemoryConfig::default(),
            meta_learning_config: MetaLearningConfig::default(),
            performance_config: PerformanceConfig::default(),
            enable_gradient_clipping: true,
            gradient_clip_value: scirs2_core::numeric::NumCast::from(1.0)
                .unwrap_or_else(|| T::zero()),
            weight_decay: scirs2_core::numeric::NumCast::from(1e-5).unwrap_or_else(|| T::zero()),
            warmup_steps: 1000,
            enable_layer_norm: true,
            use_pre_norm: true,
            enable_residual_connections: true,
        }
    }
}

impl<T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static>
    TransformerBasedOptimizerConfig<T>
{
    /// Create configuration for small models
    pub fn small() -> Self {
        Self {
            model_dimension: 256,
            num_transformer_layers: 4,
            num_attention_heads: 4,
            attention_head_dimension: 64,
            feedforward_dimension: 1024,
            sequence_length: 64,
            ..Self::default()
        }
    }

    /// Create configuration for large models
    pub fn large() -> Self {
        Self {
            model_dimension: 1024,
            num_transformer_layers: 12,
            num_attention_heads: 16,
            attention_head_dimension: 64,
            feedforward_dimension: 4096,
            sequence_length: 256,
            ..Self::default()
        }
    }

    /// Create configuration optimized for training
    pub fn for_training() -> Self {
        Self {
            batch_size: 64,
            num_epochs: 200,
            learning_rate: scirs2_core::numeric::NumCast::from(2e-4).unwrap_or_else(|| T::zero()),
            warmup_steps: 2000,
            enable_gradient_clipping: true,
            weight_decay: scirs2_core::numeric::NumCast::from(1e-4).unwrap_or_else(|| T::zero()),
            ..Self::default()
        }
    }

    /// Create configuration optimized for inference
    pub fn for_inference() -> Self {
        Self {
            batch_size: 1,
            dropout_rate: 0.0,
            enable_gradient_clipping: false,
            ..Self::default()
        }
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<(), String> {
        if self.model_dimension == 0 {
            return Err("model_dimension must be greater than 0".to_string());
        }

        if self.num_transformer_layers == 0 {
            return Err("num_transformer_layers must be greater than 0".to_string());
        }

        if self.num_attention_heads == 0 {
            return Err("num_attention_heads must be greater than 0".to_string());
        }

        if !self
            .model_dimension
            .is_multiple_of(self.num_attention_heads)
        {
            return Err("model_dimension must be divisible by num_attention_heads".to_string());
        }

        if self.attention_head_dimension * self.num_attention_heads != self.model_dimension {
            return Err(
                "attention_head_dimension * num_attention_heads must equal model_dimension"
                    .to_string(),
            );
        }

        if self.sequence_length == 0 {
            return Err("sequence_length must be greater than 0".to_string());
        }

        if self.dropout_rate < 0.0 || self.dropout_rate > 1.0 {
            return Err("dropout_rate must be between 0.0 and 1.0".to_string());
        }

        if self.learning_rate <= T::zero() {
            return Err("learning_rate must be positive".to_string());
        }

        if self.batch_size == 0 {
            return Err("batch_size must be greater than 0".to_string());
        }

        self.memory_config.validate()?;
        self.meta_learning_config.validate()?;
        self.performance_config.validate()?;

        Ok(())
    }

    /// Calculate total parameters estimate
    pub fn estimate_parameter_count(&self) -> usize {
        let embedding_params = self.model_dimension * self.model_dimension; // Input embedding
        let positional_params = self.sequence_length * self.model_dimension;

        let attention_params_per_layer = 4 * self.model_dimension * self.model_dimension; // Q, K, V, O projections
        let ffn_params_per_layer = 2 * self.model_dimension * self.feedforward_dimension; // Up and down projections
        let norm_params_per_layer = 2 * self.model_dimension; // Layer norm parameters

        let layer_params =
            attention_params_per_layer + ffn_params_per_layer + norm_params_per_layer;
        let total_layer_params = layer_params * self.num_transformer_layers;

        let output_params = self.model_dimension * self.model_dimension; // Output projection

        embedding_params + positional_params + total_layer_params + output_params
    }

    /// Calculate memory requirements (in MB)
    pub fn estimate_memory_usage(&self) -> f64 {
        let param_count = self.estimate_parameter_count();
        let bytes_per_param = if std::mem::size_of::<T>() == 4 {
            4.0
        } else {
            8.0
        };

        let model_memory = param_count as f64 * bytes_per_param;
        let activation_memory = self.batch_size as f64
            * self.sequence_length as f64
            * self.model_dimension as f64
            * bytes_per_param;
        let gradient_memory = model_memory; // Assume same as model for gradients

        let total_bytes = model_memory + activation_memory + gradient_memory;
        total_bytes / (1024.0 * 1024.0) // Convert to MB
    }
}

/// Transformer architecture configuration
#[derive(Debug, Clone)]
pub struct TransformerArchConfig {
    pub model_dimension: usize,
    pub num_layers: usize,
    pub num_attention_heads: usize,
    pub feedforward_dimension: usize,
    pub dropout_rate: f64,
    pub use_pre_norm: bool,
    pub enable_residual_connections: bool,
}

impl TransformerArchConfig {
    pub fn from_optimizer_config<
        T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static,
    >(
        config: &TransformerBasedOptimizerConfig<T>,
    ) -> Self {
        Self {
            model_dimension: config.model_dimension,
            num_layers: config.num_transformer_layers,
            num_attention_heads: config.num_attention_heads,
            feedforward_dimension: config.feedforward_dimension,
            dropout_rate: config.dropout_rate,
            use_pre_norm: config.use_pre_norm,
            enable_residual_connections: config.enable_residual_connections,
        }
    }
}

/// Activation function types
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum ActivationFunction {
    ReLU,
    GELU,
    Swish,
    Tanh,
    Sigmoid,
    LeakyReLU,
}

/// Memory management configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryConfig {
    /// Maximum memory cache size
    pub max_cache_size: usize,
    /// Enable memory compression
    pub enable_compression: bool,
    /// Cache eviction strategy
    pub eviction_strategy: CacheEvictionStrategy,
    /// Memory allocation block size
    pub allocation_block_size: usize,
}

impl Default for MemoryConfig {
    fn default() -> Self {
        Self {
            max_cache_size: 1024 * 1024 * 1024, // 1GB
            enable_compression: false,
            eviction_strategy: CacheEvictionStrategy::LRU,
            allocation_block_size: 4096,
        }
    }
}

impl MemoryConfig {
    pub fn validate(&self) -> Result<(), String> {
        if self.max_cache_size == 0 {
            return Err("max_cache_size must be greater than 0".to_string());
        }

        if self.allocation_block_size == 0 {
            return Err("allocation_block_size must be greater than 0".to_string());
        }

        Ok(())
    }
}

/// Cache eviction strategies
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum CacheEvictionStrategy {
    LRU,
    LFU,
    FIFO,
    Random,
}

/// Meta-learning configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetaLearningConfig<
    T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static,
> {
    /// Meta-learning rate
    pub meta_learning_rate: T,
    /// Number of inner optimization steps
    pub inner_steps: usize,
    /// Inner learning rate
    pub inner_learning_rate: T,
    /// Enable first-order approximation
    pub first_order: bool,
    /// Number of support examples
    pub num_support: usize,
    /// Number of query examples
    pub num_query: usize,
    /// Heavy-ball momentum coefficient for the **outer** (meta) loop.
    ///
    /// `None` (the default) means plain SGD. Setting it activates the momentum
    /// branch of `MetaOptimizer::update`, which was previously unreachable
    /// because the momentum field was hardcoded to `None`.
    pub meta_momentum: Option<T>,
}

impl<T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static> Default
    for MetaLearningConfig<T>
{
    fn default() -> Self {
        Self {
            meta_learning_rate: scirs2_core::numeric::NumCast::from(1e-3)
                .unwrap_or_else(|| T::zero()),
            inner_steps: 5,
            inner_learning_rate: scirs2_core::numeric::NumCast::from(1e-2)
                .unwrap_or_else(|| T::zero()),
            first_order: false,
            num_support: 5,
            num_query: 15,
            meta_momentum: None,
        }
    }
}

impl<T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static>
    MetaLearningConfig<T>
{
    pub fn validate(&self) -> Result<(), String> {
        if self.meta_learning_rate <= T::zero() {
            return Err("meta_learning_rate must be positive".to_string());
        }

        if self.inner_learning_rate <= T::zero() {
            return Err("inner_learning_rate must be positive".to_string());
        }

        if self.inner_steps == 0 {
            return Err("inner_steps must be greater than 0".to_string());
        }

        if self.num_support == 0 {
            return Err("num_support must be greater than 0".to_string());
        }

        if self.num_query == 0 {
            return Err("num_query must be greater than 0".to_string());
        }

        Ok(())
    }
}

/// Performance tracking configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceConfig {
    /// Enable detailed performance tracking
    pub enable_detailed_tracking: bool,
    /// Performance metrics collection interval
    pub metrics_interval: usize,
    /// Maximum history size for metrics
    pub max_history_size: usize,
    /// Enable memory usage tracking
    pub track_memory_usage: bool,
    /// Enable timing analysis
    pub enable_timing_analysis: bool,
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            enable_detailed_tracking: true,
            metrics_interval: 10,
            max_history_size: 10000,
            track_memory_usage: true,
            enable_timing_analysis: true,
        }
    }
}

impl PerformanceConfig {
    pub fn validate(&self) -> Result<(), String> {
        if self.metrics_interval == 0 {
            return Err("metrics_interval must be greater than 0".to_string());
        }

        if self.max_history_size == 0 {
            return Err("max_history_size must be greater than 0".to_string());
        }

        Ok(())
    }
}

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

    #[test]
    fn test_config_default() {
        let config = TransformerBasedOptimizerConfig::<f32>::default();
        assert!(config.validate().is_ok());
        assert_eq!(config.model_dimension, 512);
        assert_eq!(config.num_transformer_layers, 6);
    }

    #[test]
    fn test_config_validation() {
        // Test invalid model dimension
        let config = TransformerBasedOptimizerConfig::<f32> {
            model_dimension: 0,
            ..Default::default()
        };
        assert!(config.validate().is_err());

        // Test mismatched attention dimensions
        let config = TransformerBasedOptimizerConfig::<f32> {
            model_dimension: 512,
            num_attention_heads: 7, // 512 is not divisible by 7
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_parameter_estimation() {
        let config = TransformerBasedOptimizerConfig::<f32>::small();
        let param_count = config.estimate_parameter_count();
        assert!(param_count > 0);

        let memory_usage = config.estimate_memory_usage();
        assert!(memory_usage > 0.0);
    }

    #[test]
    fn test_preset_configs() {
        let small_config = TransformerBasedOptimizerConfig::<f32>::small();
        assert!(small_config.validate().is_ok());
        assert_eq!(small_config.model_dimension, 256);

        let large_config = TransformerBasedOptimizerConfig::<f32>::large();
        assert!(large_config.validate().is_ok());
        assert_eq!(large_config.model_dimension, 1024);

        let training_config = TransformerBasedOptimizerConfig::<f32>::for_training();
        assert!(training_config.validate().is_ok());
        assert_eq!(training_config.batch_size, 64);
    }
}