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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
// Configuration structures for transformer-based optimization
//
// This module provides comprehensive configuration options for:
// - Transformer architecture settings
// - Attention mechanism configurations
// - Meta-learning parameters
// - Performance optimization settings

use scirs2_core::numeric::Float;
use std::fmt::Debug;
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Configuration for transformer-based optimizer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransformerBasedOptimizerConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Transformer architecture configuration
    pub architecture: TransformerArchConfig,
    /// Attention mechanism configuration
    pub attention: AttentionConfig<T>,
    /// Meta-learning configuration
    pub meta_learning: MetaLearningConfig<T>,
    /// Sequence processing configuration
    pub sequence: SequenceConfig,
    /// Memory management configuration
    pub memory: MemoryConfig,
    /// Performance tracking configuration
    pub performance: PerformanceConfig,
    /// Training configuration
    pub training: TrainingConfig<T>,
}

impl<T: Float + Debug + Send + Sync + 'static> Default for TransformerBasedOptimizerConfig<T> {
    fn default() -> Self {
        Self {
            architecture: TransformerArchConfig::default(),
            attention: AttentionConfig::default(),
            meta_learning: MetaLearningConfig::default(),
            sequence: SequenceConfig::default(),
            memory: MemoryConfig::default(),
            performance: PerformanceConfig::default(),
            training: TrainingConfig::default(),
        }
    }
}

/// Transformer architecture configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransformerArchConfig {
    /// Number of transformer layers
    pub num_layers: usize,
    /// Hidden dimension
    pub hidden_dim: usize,
    /// Feed-forward dimension
    pub ff_dim: usize,
    /// Number of attention heads
    pub num_heads: usize,
    /// Dropout rate
    pub dropout_rate: f64,
    /// Use pre-layer normalization
    pub pre_norm: bool,
    /// Activation function type
    pub activation: ActivationType,
    /// Maximum sequence length
    pub max_seq_length: usize,
    /// Use learnable positional encoding
    pub learnable_pe: bool,
    /// Embedding dimension
    pub embedding_dim: usize,
}

impl Default for TransformerArchConfig {
    fn default() -> Self {
        Self {
            num_layers: 6,
            hidden_dim: 512,
            ff_dim: 2048,
            num_heads: 8,
            dropout_rate: 0.1,
            pre_norm: true,
            activation: ActivationType::Gelu,
            max_seq_length: 1024,
            learnable_pe: false,
            embedding_dim: 512,
        }
    }
}

/// Activation function types
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ActivationType {
    /// ReLU activation
    Relu,
    /// GELU activation
    Gelu,
    /// Swish activation
    Swish,
    /// Mish activation
    Mish,
    /// Tanh activation
    Tanh,
    /// Sigmoid activation
    Sigmoid,
    /// LeakyReLU activation
    LeakyRelu { negative_slope: f64 },
    /// ELU activation
    Elu { alpha: f64 },
}

/// Attention mechanism configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttentionConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Attention head dimension
    pub head_dim: usize,
    /// Use scaled dot-product attention
    pub scaled: bool,
    /// Attention dropout rate
    pub attention_dropout: f64,
    /// Use relative positional encoding
    pub relative_pe: bool,
    /// Maximum relative position
    pub max_relative_position: usize,
    /// Attention bias type
    pub bias_type: AttentionBiasType,
    /// Temperature for attention softmax
    pub temperature: T,
    /// Use sparse attention
    pub sparse_attention: bool,
    /// Sparsity pattern configuration
    pub sparsity_config: Option<SparsityConfig>,
}

impl<T: Float + Debug + Send + Sync + 'static> Default for AttentionConfig<T> {
    fn default() -> Self {
        Self {
            head_dim: 64,
            scaled: true,
            attention_dropout: 0.1,
            relative_pe: false,
            max_relative_position: 32,
            bias_type: AttentionBiasType::None,
            temperature: T::one(),
            sparse_attention: false,
            sparsity_config: None,
        }
    }
}

/// Attention bias types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AttentionBiasType {
    /// No attention bias
    None,
    /// Causal (lower triangular) mask
    Causal,
    /// Custom bias matrix
    Custom,
    /// Relative position bias
    Relative,
    /// Learnable bias
    Learnable,
}

/// Sparsity configuration for sparse attention
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SparsityConfig {
    /// Sparsity pattern type
    pub pattern: SparsityPattern,
    /// Block size for block-sparse attention
    pub block_size: usize,
    /// Stride for strided attention
    pub stride: usize,
    /// Number of local attention positions
    pub local_attention: usize,
    /// Number of global attention positions
    pub global_attention: usize,
}

/// Sparsity patterns for attention
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SparsityPattern {
    /// Block-sparse attention
    BlockSparse,
    /// Strided attention
    Strided,
    /// Local + global attention
    LocalGlobal,
    /// Random sparse attention
    Random { sparsity_ratio: f64 },
    /// Custom sparsity pattern
    Custom,
}

/// Meta-learning configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetaLearningConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Meta-learning strategy
    pub strategy: MetaLearningStrategy,
    /// Learning rate for meta-learning
    pub meta_learning_rate: T,
    /// Number of inner optimization steps
    pub inner_steps: usize,
    /// Meta-batch size
    pub meta_batch_size: usize,
    /// Gradient clipping threshold
    pub gradient_clip: Option<T>,
    /// Use second-order gradients
    pub second_order: bool,
    /// Adaptation configuration
    pub adaptation: AdaptationConfig<T>,
    /// Task sampling configuration
    pub task_sampling: TaskSamplingConfig,
}

impl<T: Float + Debug + Send + Sync + 'static> Default for MetaLearningConfig<T> {
    fn default() -> Self {
        Self {
            strategy: MetaLearningStrategy::Maml,
            meta_learning_rate: scirs2_core::numeric::NumCast::from(0.001).unwrap_or_else(|| T::zero()),
            inner_steps: 5,
            meta_batch_size: 16,
            gradient_clip: Some(scirs2_core::numeric::NumCast::from(1.0).unwrap_or_else(|| T::zero())),
            second_order: false,
            adaptation: AdaptationConfig::default(),
            task_sampling: TaskSamplingConfig::default(),
        }
    }
}

/// Meta-learning strategies
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MetaLearningStrategy {
    /// Model-Agnostic Meta-Learning
    Maml,
    /// Reptile algorithm
    Reptile,
    /// Prototypical networks
    Prototypical,
    /// Matching networks
    Matching,
    /// Meta-SGD
    MetaSgd,
    /// Learned optimizer
    LearnedOptimizer,
    /// Gradient-based meta-learning
    GradientBased,
    /// Memory-augmented meta-learning
    MemoryAugmented,
}

/// Adaptation configuration for meta-learning
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptationConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Number of adaptation steps
    pub num_steps: usize,
    /// Adaptation learning rate
    pub learning_rate: T,
    /// Use adaptive learning rate
    pub adaptive_lr: bool,
    /// Learning rate schedule
    pub lr_schedule: LearningRateSchedule<T>,
    /// Stop early if converged
    pub early_stopping: bool,
    /// Convergence tolerance
    pub tolerance: T,
}

impl<T: Float + Debug + Send + Sync + 'static> Default for AdaptationConfig<T> {
    fn default() -> Self {
        Self {
            num_steps: 10,
            learning_rate: scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero()),
            adaptive_lr: false,
            lr_schedule: LearningRateSchedule::Constant,
            early_stopping: false,
            tolerance: scirs2_core::numeric::NumCast::from(1e-6).unwrap_or_else(|| T::zero()),
        }
    }
}

/// Learning rate schedules
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LearningRateSchedule<T: Float + Debug + Send + Sync + 'static> {
    /// Constant learning rate
    Constant,
    /// Exponential decay
    Exponential { decay_rate: T },
    /// Cosine annealing
    Cosine { min_lr: T },
    /// Step decay
    Step { 
        step_size: usize, 
        gamma: T 
    },
    /// Polynomial decay
    Polynomial { power: T },
    /// Warm restart
    WarmRestart { 
        t_0: usize, 
        t_mult: usize 
    },
}

/// Task sampling configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskSamplingConfig {
    /// Sampling strategy
    pub strategy: TaskSamplingStrategy,
    /// Number of support examples per task
    pub num_support: usize,
    /// Number of query examples per task
    pub num_query: usize,
    /// Task difficulty curriculum
    pub curriculum: Option<CurriculumConfig>,
    /// Task augmentation
    pub augmentation: TaskAugmentationConfig,
}

impl Default for TaskSamplingConfig {
    fn default() -> Self {
        Self {
            strategy: TaskSamplingStrategy::Uniform,
            num_support: 10,
            num_query: 15,
            curriculum: None,
            augmentation: TaskAugmentationConfig::default(),
        }
    }
}

/// Task sampling strategies
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TaskSamplingStrategy {
    /// Uniform random sampling
    Uniform,
    /// Weighted sampling based on difficulty
    Weighted,
    /// Curriculum learning
    Curriculum,
    /// Anti-curriculum learning
    AntiCurriculum,
    /// Adaptive sampling
    Adaptive,
}

/// Curriculum learning configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CurriculumConfig {
    /// Starting difficulty level
    pub start_difficulty: f64,
    /// Ending difficulty level
    pub end_difficulty: f64,
    /// Curriculum schedule
    pub schedule: CurriculumSchedule,
    /// Difficulty metric
    pub difficulty_metric: DifficultyMetric,
}

/// Curriculum schedules
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CurriculumSchedule {
    /// Linear progression
    Linear,
    /// Exponential progression
    Exponential { rate: f64 },
    /// Step-wise progression
    Step { step_size: usize },
    /// Performance-based progression
    PerformanceBased { threshold: f64 },
}

/// Difficulty metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DifficultyMetric {
    /// Loss-based difficulty
    Loss,
    /// Gradient norm
    GradientNorm,
    /// Convergence rate
    ConvergenceRate,
    /// Number of optimization steps
    OptimizationSteps,
    /// Custom metric
    Custom(String),
}

/// Task augmentation configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskAugmentationConfig {
    /// Enable noise injection
    pub noise_injection: bool,
    /// Noise level
    pub noise_level: f64,
    /// Enable parameter perturbation
    pub parameter_perturbation: bool,
    /// Perturbation magnitude
    pub perturbation_magnitude: f64,
    /// Enable gradient augmentation
    pub gradient_augmentation: bool,
    /// Augmentation methods
    pub augmentation_methods: Vec<AugmentationMethod>,
}

impl Default for TaskAugmentationConfig {
    fn default() -> Self {
        Self {
            noise_injection: false,
            noise_level: 0.01,
            parameter_perturbation: false,
            perturbation_magnitude: 0.1,
            gradient_augmentation: false,
            augmentation_methods: vec![],
        }
    }
}

/// Augmentation methods
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AugmentationMethod {
    /// Gaussian noise
    GaussianNoise { std: f64 },
    /// Dropout
    Dropout { rate: f64 },
    /// Gradient clipping
    GradientClipping { threshold: f64 },
    /// Parameter scaling
    ParameterScaling { factor: f64 },
    /// Custom augmentation
    Custom(String),
}

/// Sequence processing configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SequenceConfig {
    /// Maximum sequence length
    pub max_length: usize,
    /// Sequence padding strategy
    pub padding_strategy: PaddingStrategy,
    /// Sequence truncation strategy
    pub truncation_strategy: TruncationStrategy,
    /// Use dynamic batching
    pub dynamic_batching: bool,
    /// Batch size
    pub batch_size: usize,
    /// Sequence augmentation
    pub augmentation: SequenceAugmentationConfig,
}

impl Default for SequenceConfig {
    fn default() -> Self {
        Self {
            max_length: 1024,
            padding_strategy: PaddingStrategy::Zero,
            truncation_strategy: TruncationStrategy::Right,
            dynamic_batching: true,
            batch_size: 32,
            augmentation: SequenceAugmentationConfig::default(),
        }
    }
}

/// Padding strategies for sequences
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PaddingStrategy {
    /// Zero padding
    Zero,
    /// Mean padding
    Mean,
    /// Constant padding
    Constant(f64),
    /// Reflection padding
    Reflection,
    /// Replication padding
    Replication,
}

/// Truncation strategies for sequences
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TruncationStrategy {
    /// Truncate from the right
    Right,
    /// Truncate from the left
    Left,
    /// Truncate from the center
    Center,
    /// Random truncation
    Random,
    /// No truncation (error if too long)
    None,
}

/// Sequence augmentation configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SequenceAugmentationConfig {
    /// Enable temporal jittering
    pub temporal_jittering: bool,
    /// Jittering strength
    pub jitter_strength: f64,
    /// Enable sequence dropout
    pub sequence_dropout: bool,
    /// Dropout rate
    pub dropout_rate: f64,
    /// Enable sequence permutation
    pub permutation: bool,
    /// Permutation probability
    pub permutation_prob: f64,
}

impl Default for SequenceAugmentationConfig {
    fn default() -> Self {
        Self {
            temporal_jittering: false,
            jitter_strength: 0.1,
            sequence_dropout: false,
            dropout_rate: 0.1,
            permutation: false,
            permutation_prob: 0.1,
        }
    }
}

/// Memory management configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryConfig {
    /// Memory type
    pub memory_type: MemoryType,
    /// Memory size (number of entries)
    pub memory_size: usize,
    /// Memory update strategy
    pub update_strategy: MemoryUpdateStrategy,
    /// Memory retrieval strategy
    pub retrieval_strategy: MemoryRetrievalStrategy,
    /// Enable memory compression
    pub compression: bool,
    /// Compression algorithm
    pub compression_algorithm: CompressionAlgorithm,
}

impl Default for MemoryConfig {
    fn default() -> Self {
        Self {
            memory_type: MemoryType::Fifo,
            memory_size: 10000,
            update_strategy: MemoryUpdateStrategy::Replace,
            retrieval_strategy: MemoryRetrievalStrategy::MostRecent,
            compression: false,
            compression_algorithm: CompressionAlgorithm::None,
        }
    }
}

/// Memory types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MemoryType {
    /// First-in-first-out memory
    Fifo,
    /// Least-recently-used memory
    Lru,
    /// Priority-based memory
    Priority,
    /// Associative memory
    Associative,
    /// Episodic memory
    Episodic,
    /// Working memory
    Working,
}

/// Memory update strategies
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MemoryUpdateStrategy {
    /// Replace existing entries
    Replace,
    /// Average with existing entries
    Average,
    /// Weighted update
    Weighted { weight: f64 },
    /// Append new entries
    Append,
    /// Selective update based on importance
    Selective,
}

/// Memory retrieval strategies
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MemoryRetrievalStrategy {
    /// Most recent entries
    MostRecent,
    /// Most relevant entries
    MostRelevant,
    /// Random sampling
    Random,
    /// Similarity-based retrieval
    Similarity,
    /// Attention-based retrieval
    Attention,
}

/// Compression algorithms for memory
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CompressionAlgorithm {
    /// No compression
    None,
    /// Principal Component Analysis
    Pca { components: usize },
    /// Singular Value Decomposition
    Svd { rank: usize },
    /// Autoencoder compression
    Autoencoder { latent_dim: usize },
    /// Quantization
    Quantization { bits: usize },
}

/// Performance tracking configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceConfig {
    /// Enable performance tracking
    pub enabled: bool,
    /// Tracking interval
    pub tracking_interval: Duration,
    /// Metrics to track
    pub metrics: Vec<PerformanceMetric>,
    /// Enable profiling
    pub profiling: bool,
    /// Memory usage tracking
    pub memory_tracking: bool,
    /// Computation time tracking
    pub time_tracking: bool,
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            tracking_interval: Duration::from_secs(1),
            metrics: vec![
                PerformanceMetric::Loss,
                PerformanceMetric::Accuracy,
                PerformanceMetric::ConvergenceRate,
            ],
            profiling: false,
            memory_tracking: true,
            time_tracking: true,
        }
    }
}

/// Performance metrics to track
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PerformanceMetric {
    /// Loss value
    Loss,
    /// Accuracy
    Accuracy,
    /// Convergence rate
    ConvergenceRate,
    /// Gradient norm
    GradientNorm,
    /// Learning rate
    LearningRate,
    /// Memory usage
    MemoryUsage,
    /// Computation time
    ComputationTime,
    /// Attention weights
    AttentionWeights,
    /// Custom metric
    Custom(String),
}

/// Training configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrainingConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Learning rate
    pub learning_rate: T,
    /// Optimizer type
    pub optimizer: OptimizerType,
    /// Weight decay
    pub weight_decay: T,
    /// Gradient clipping
    pub gradient_clipping: Option<GradientClippingConfig<T>>,
    /// Learning rate schedule
    pub lr_schedule: LearningRateSchedule<T>,
    /// Warmup configuration
    pub warmup: Option<WarmupConfig>,
    /// Early stopping configuration
    pub early_stopping: Option<EarlyStoppingConfig<T>>,
}

impl<T: Float + Debug + Send + Sync + 'static> Default for TrainingConfig<T> {
    fn default() -> Self {
        Self {
            learning_rate: scirs2_core::numeric::NumCast::from(0.001).unwrap_or_else(|| T::zero()),
            optimizer: OptimizerType::Adam,
            weight_decay: scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero()),
            gradient_clipping: Some(GradientClippingConfig::default()),
            lr_schedule: LearningRateSchedule::Constant,
            warmup: None,
            early_stopping: None,
        }
    }
}

/// Optimizer types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OptimizerType {
    /// Adam optimizer
    Adam,
    /// AdamW optimizer
    AdamW,
    /// SGD optimizer
    Sgd,
    /// RMSprop optimizer
    RmsProp,
    /// Adagrad optimizer
    Adagrad,
    /// Custom optimizer
    Custom(String),
}

/// Gradient clipping configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GradientClippingConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Clipping method
    pub method: ClippingMethod<T>,
    /// Clipping threshold
    pub threshold: T,
    /// Adaptive clipping
    pub adaptive: bool,
}

impl<T: Float + Debug + Send + Sync + 'static> Default for GradientClippingConfig<T> {
    fn default() -> Self {
        Self {
            method: ClippingMethod::Norm,
            threshold: scirs2_core::numeric::NumCast::from(1.0).unwrap_or_else(|| T::zero()),
            adaptive: false,
        }
    }
}

/// Gradient clipping methods
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ClippingMethod<T: Float + Debug + Send + Sync + 'static> {
    /// Clip by norm
    Norm,
    /// Clip by value
    Value,
    /// Clip by global norm
    GlobalNorm,
    /// Adaptive clipping
    Adaptive { percentile: T },
}

/// Warmup configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WarmupConfig {
    /// Number of warmup steps
    pub steps: usize,
    /// Warmup type
    pub warmup_type: WarmupType,
}

/// Warmup types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum WarmupType {
    /// Linear warmup
    Linear,
    /// Cosine warmup
    Cosine,
    /// Exponential warmup
    Exponential,
}

/// Early stopping configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EarlyStoppingConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Patience (number of epochs to wait)
    pub patience: usize,
    /// Minimum delta for improvement
    pub min_delta: T,
    /// Metric to monitor
    pub monitor: String,
    /// Mode (minimize or maximize)
    pub mode: MonitorMode,
}

/// Monitoring modes for early stopping
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MonitorMode {
    /// Minimize the metric
    Min,
    /// Maximize the metric
    Max,
}

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

    #[test]
    fn test_default_config() {
        let config = TransformerBasedOptimizerConfig::<f64>::default();
        assert_eq!(config.architecture.num_layers, 6);
        assert_eq!(config.architecture.hidden_dim, 512);
        assert_eq!(config.attention.head_dim, 64);
    }

    #[test]
    fn test_activation_types() {
        let relu = ActivationType::Relu;
        let gelu = ActivationType::Gelu;
        let leaky_relu = ActivationType::LeakyRelu { negative_slope: 0.01 };
        
        assert_eq!(relu, ActivationType::Relu);
        assert_eq!(gelu, ActivationType::Gelu);
        
        if let ActivationType::LeakyRelu { negative_slope } = leaky_relu {
            assert_eq!(negative_slope, 0.01);
        }
    }

    #[test]
    fn test_meta_learning_config() {
        let config = MetaLearningConfig::<f32>::default();
        assert_eq!(config.inner_steps, 5);
        assert_eq!(config.meta_batch_size, 16);
        assert!(matches!(config.strategy, MetaLearningStrategy::Maml));
    }

    #[test]
    fn test_memory_config() {
        let config = MemoryConfig::default();
        assert_eq!(config.memory_size, 10000);
        assert!(matches!(config.memory_type, MemoryType::Fifo));
        assert!(!config.compression);
    }

    #[test]
    fn test_sequence_config() {
        let config = SequenceConfig::default();
        assert_eq!(config.max_length, 1024);
        assert_eq!(config.batch_size, 32);
        assert!(config.dynamic_batching);
    }

    #[test]
    fn test_performance_config() {
        let config = PerformanceConfig::default();
        assert!(config.enabled);
        assert!(config.memory_tracking);
        assert!(config.time_tracking);
        assert_eq!(config.metrics.len(), 3);
    }
}