scirs2-autograd 0.3.2

Automatic differentiation module for SciRS2 (scirs2-autograd)
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
//! Configuration integration utilities for SciRS2 modules
//!
//! This module provides a unified configuration system that allows
//! consistent settings across different SciRS2 modules, with support
//! for environment variables, configuration files, and runtime updates.

use super::{IntegrationConfig, IntegrationError, MemoryStrategy, PrecisionLevel};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
#[allow(unused_imports)]
/// Global configuration manager for SciRS2 integration
pub struct ConfigManager {
    /// Current configuration
    config: GlobalConfig,
    /// Configuration sources
    sources: Vec<ConfigSource>,
    /// Configuration cache
    cache: HashMap<String, ConfigValue>,
    /// Watch for configuration changes
    watch_enabled: bool,
}

impl ConfigManager {
    /// Create new configuration manager
    pub fn new() -> Self {
        Self {
            config: GlobalConfig::default(),
            sources: Vec::new(),
            cache: HashMap::new(),
            watch_enabled: false,
        }
    }

    /// Load configuration from file
    pub fn load_from_file<P: AsRef<Path>>(&mut self, path: P) -> Result<(), IntegrationError> {
        let content = std::fs::read_to_string(path.as_ref()).map_err(|e| {
            IntegrationError::ConfigMismatch(format!("Failed to read config file: {e}"))
        })?;

        let file_config: FileConfig = toml::from_str(&content).map_err(|e| {
            IntegrationError::ConfigMismatch(format!("Failed to parse _config file: {e}"))
        })?;

        // Merge file configuration
        self.merge_file_config(file_config)?;

        // Add source
        self.sources
            .push(ConfigSource::File(path.as_ref().to_path_buf()));

        Ok(())
    }

    /// Load configuration from environment variables
    pub fn load_from_env(&mut self) -> Result<(), IntegrationError> {
        let mut env_config = HashMap::new();

        // Check for SciRS2-specific environment variables
        for (key, value) in std::env::vars() {
            if key.starts_with("SCIRS2_") {
                let config_key = key
                    .strip_prefix("SCIRS2_")
                    .expect("Operation failed")
                    .to_lowercase();
                env_config.insert(config_key, value);
            }
        }

        self.merge_env_config(env_config)?;
        self.sources.push(ConfigSource::Environment);

        Ok(())
    }

    /// Set configuration value
    pub fn set<T: Into<ConfigValue>>(&mut self, key: &str, value: T) {
        self.cache.insert(key.to_string(), value.into());
        self.apply_cached_values();
    }

    /// Get configuration value
    pub fn get(&self, key: &str) -> Option<&ConfigValue> {
        self.cache.get(key)
    }

    /// Get configuration value with default
    pub fn get_or_default<T: From<ConfigValue> + Default>(&self, key: &str) -> T {
        self.cache
            .get(key)
            .cloned()
            .map(T::from)
            .unwrap_or_default()
    }

    /// Get integration configuration
    pub fn integration_config(&self) -> &IntegrationConfig {
        &self.config.integration
    }

    /// Update integration configuration
    pub fn update_integration_config(&mut self, config: IntegrationConfig) {
        self.config.integration = config;
    }

    /// Get module-specific configuration
    pub fn module_config(&self, module_name: &str) -> Option<&ModuleConfig> {
        self.config.modules.get(module_name)
    }

    /// Set module-specific configuration
    pub fn set_module_config(&mut self, module_name: String, config: ModuleConfig) {
        self.config.modules.insert(module_name, config);
    }

    /// Enable configuration watching
    pub fn enable_watch(&mut self) -> Result<(), IntegrationError> {
        self.watch_enabled = true;
        // In practice, would set up file system watchers
        Ok(())
    }

    /// Validate configuration consistency
    pub fn validate(&self) -> Result<(), IntegrationError> {
        // Check integration config consistency
        if self.config.integration.strict_compatibility {
            // Verify all modules are compatible
            for (module_name, module_config) in &self.config.modules {
                if !module_config.enabled {
                    continue;
                }

                // Check version compatibility
                if let Some(required_version) = &module_config.required_version {
                    // Validate version compatibility
                    self.validate_module_version(module_name, required_version)?;
                }
            }
        }

        // Check for conflicting settings
        self.check_conflicting_settings()?;

        Ok(())
    }

    /// Export configuration to file
    pub fn export_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), IntegrationError> {
        let file_config = self.to_file_config();
        let content = toml::to_string_pretty(&file_config).map_err(|e| {
            IntegrationError::ConfigMismatch(format!("Failed to serialize config: {e}"))
        })?;

        std::fs::write(path.as_ref(), content).map_err(|e| {
            IntegrationError::ConfigMismatch(format!("Failed to write config file: {e}"))
        })?;

        Ok(())
    }

    /// Reset to default configuration
    pub fn reset_to_defaults(&mut self) {
        self.config = GlobalConfig::default();
        self.cache.clear();
    }

    /// Get configuration summary
    pub fn summary(&self) -> ConfigSummary {
        ConfigSummary {
            total_modules: self.config.modules.len(),
            enabled_modules: self.config.modules.values().filter(|m| m.enabled).count(),
            precision_level: self.config.integration.default_precision,
            memory_strategy: self.config.integration.memory_strategy,
            sources: self.sources.clone(),
            cache_size: self.cache.len(),
        }
    }

    // Helper methods
    fn merge_file_config(&mut self, file_config: FileConfig) -> Result<(), IntegrationError> {
        // Merge integration settings
        if let Some(integration) = file_config.integration {
            self.config.integration = self.merge_integration_config(integration)?;
        }

        // Merge module configurations
        for (name, module_config) in file_config.modules.unwrap_or_default() {
            self.config.modules.insert(name, module_config);
        }

        Ok(())
    }

    fn merge_env_config(
        &mut self,
        env_config: HashMap<String, String>,
    ) -> Result<(), IntegrationError> {
        for (key, value) in env_config {
            match key.as_str() {
                "auto_convert_tensors" => {
                    let val = value.parse::<bool>().map_err(|_| {
                        IntegrationError::ConfigMismatch(format!(
                            "Invalid boolean value for {key}: {value}"
                        ))
                    })?;
                    self.config.integration.auto_convert_tensors = val;
                }
                "strict_compatibility" => {
                    let val = value.parse::<bool>().map_err(|_| {
                        IntegrationError::ConfigMismatch(format!(
                            "Invalid boolean value for {key}: {value}"
                        ))
                    })?;
                    self.config.integration.strict_compatibility = val;
                }
                "default_precision" => {
                    self.config.integration.default_precision = match value.as_str() {
                        "float32" => PrecisionLevel::Float32,
                        "float64" => PrecisionLevel::Float64,
                        "mixed" => PrecisionLevel::Mixed,
                        _ => {
                            return Err(IntegrationError::ConfigMismatch(format!(
                                "Invalid precision level: {value}"
                            )))
                        }
                    };
                }
                "memory_strategy" => {
                    self.config.integration.memory_strategy = match value.as_str() {
                        "shared" => MemoryStrategy::Shared,
                        "copy" => MemoryStrategy::Copy,
                        "memory_mapped" => MemoryStrategy::MemoryMapped,
                        _ => {
                            return Err(IntegrationError::ConfigMismatch(format!(
                                "Invalid memory strategy: {value}"
                            )))
                        }
                    };
                }
                _ => {
                    // Store as cache value for module-specific settings
                    self.cache.insert(key, ConfigValue::String(value));
                }
            }
        }

        Ok(())
    }

    fn merge_integration_config(
        &self,
        file_integration: FileIntegrationConfig,
    ) -> Result<IntegrationConfig, IntegrationError> {
        let mut config = self.config.integration.clone();

        if let Some(val) = file_integration.auto_convert_tensors {
            config.auto_convert_tensors = val;
        }

        if let Some(val) = file_integration.strict_compatibility {
            config.strict_compatibility = val;
        }

        if let Some(precision) = file_integration.default_precision {
            config.default_precision = match precision.as_str() {
                "float32" => PrecisionLevel::Float32,
                "float64" => PrecisionLevel::Float64,
                "mixed" => PrecisionLevel::Mixed,
                _ => {
                    return Err(IntegrationError::ConfigMismatch(format!(
                        "Invalid precision level: {precision}"
                    )))
                }
            };
        }

        if let Some(strategy) = file_integration.memory_strategy {
            config.memory_strategy = match strategy.as_str() {
                "shared" => MemoryStrategy::Shared,
                "copy" => MemoryStrategy::Copy,
                "memory_mapped" => MemoryStrategy::MemoryMapped,
                _ => {
                    return Err(IntegrationError::ConfigMismatch(format!(
                        "Invalid memory strategy: {strategy}"
                    )))
                }
            };
        }

        Ok(config)
    }

    fn apply_cached_values(&mut self) {
        // Apply cached values to main configuration
        for (key, value) in &self.cache {
            match key.as_str() {
                "auto_convert_tensors" => {
                    if let ConfigValue::Bool(val) = value {
                        self.config.integration.auto_convert_tensors = *val;
                    }
                }
                "strict_compatibility" => {
                    if let ConfigValue::Bool(val) = value {
                        self.config.integration.strict_compatibility = *val;
                    }
                }
                _ => {} // Module-specific or unknown settings
            }
        }
    }

    fn validate_module_version(
        &self,
        _module_name: &str,
        _version: &str,
    ) -> Result<(), IntegrationError> {
        // Simplified _version validation
        Ok(())
    }

    fn check_conflicting_settings(&self) -> Result<(), IntegrationError> {
        // Check for conflicting configuration settings
        if self.config.integration.memory_strategy == MemoryStrategy::Shared
            && !self.config.integration.auto_convert_tensors
        {
            return Err(IntegrationError::ConfigMismatch(
                "Shared memory strategy requires auto tensor conversion".to_string(),
            ));
        }

        Ok(())
    }

    fn to_file_config(&self) -> FileConfig {
        let integration = FileIntegrationConfig {
            auto_convert_tensors: Some(self.config.integration.auto_convert_tensors),
            strict_compatibility: Some(self.config.integration.strict_compatibility),
            default_precision: Some(
                format!("{:?}", self.config.integration.default_precision).to_lowercase(),
            ),
            memory_strategy: Some(
                format!("{:?}", self.config.integration.memory_strategy).to_lowercase(),
            ),
            error_mode: Some(format!("{:?}", self.config.integration.error_mode).to_lowercase()),
        };

        FileConfig {
            integration: Some(integration),
            modules: Some(self.config.modules.clone()),
        }
    }
}

impl Default for ConfigManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Global configuration structure
#[derive(Debug, Clone)]
pub struct GlobalConfig {
    /// Integration configuration
    pub integration: IntegrationConfig,
    /// Module-specific configurations
    pub modules: HashMap<String, ModuleConfig>,
    /// Performance configuration
    pub performance: PerformanceConfig,
    /// Logging configuration
    pub logging: LoggingConfig,
}

impl Default for GlobalConfig {
    fn default() -> Self {
        let mut modules = HashMap::new();

        // Add default configurations for known modules
        modules.insert("scirs2-neural".to_string(), ModuleConfig::default_neural());
        modules.insert("scirs2-optim".to_string(), ModuleConfig::default_optim());
        modules.insert("scirs2-linalg".to_string(), ModuleConfig::default_linalg());

        Self {
            integration: IntegrationConfig::default(),
            modules,
            performance: PerformanceConfig::default(),
            logging: LoggingConfig::default(),
        }
    }
}

/// Configuration for individual modules
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModuleConfig {
    /// Whether the module is enabled
    pub enabled: bool,
    /// Required module version
    pub required_version: Option<String>,
    /// Module-specific settings
    pub settings: HashMap<String, ConfigValue>,
    /// Feature flags
    pub features: Vec<String>,
    /// Resource limits
    pub resource_limits: ResourceLimits,
}

impl ModuleConfig {
    /// Create new module config
    pub fn new() -> Self {
        Self {
            enabled: true,
            required_version: None,
            settings: HashMap::new(),
            features: Vec::new(),
            resource_limits: ResourceLimits::default(),
        }
    }

    /// Default configuration for neural module
    pub fn default_neural() -> Self {
        let mut config = Self::new();
        config.features = vec![
            "automatic_differentiation".to_string(),
            "gradient_checkpointing".to_string(),
            "mixed_precision".to_string(),
        ];
        config
            .settings
            .insert("batch_size".to_string(), ConfigValue::Int(32));
        config
            .settings
            .insert("learning_rate".to_string(), ConfigValue::Float(0.001));
        config
    }

    /// Default configuration for optimization module
    pub fn default_optim() -> Self {
        let mut config = Self::new();
        config.features = vec![
            "adaptive_optimizers".to_string(),
            "learning_rate_scheduling".to_string(),
            "gradient_clipping".to_string(),
        ];
        config.settings.insert(
            "default_optimizer".to_string(),
            ConfigValue::String("adam".to_string()),
        );
        config
            .settings
            .insert("weight_decay".to_string(), ConfigValue::Float(1e-4));
        config
    }

    /// Default configuration for linear algebra module
    pub fn default_linalg() -> Self {
        let mut config = Self::new();
        config.features = vec![
            "blas_acceleration".to_string(),
            "gpu_support".to_string(),
            "numerical_stability".to_string(),
        ];
        config
            .settings
            .insert("use_blas".to_string(), ConfigValue::Bool(true));
        config
            .settings
            .insert("pivot_threshold".to_string(), ConfigValue::Float(1e-3));
        config
    }
}

impl Default for ModuleConfig {
    fn default() -> Self {
        Self::new()
    }
}

/// Resource limits for modules
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ResourceLimits {
    /// Maximum memory usage in bytes
    pub max_memory: Option<usize>,
    /// Maximum number of threads
    pub max_threads: Option<usize>,
    /// Maximum computation time in seconds
    pub max_compute_time: Option<f64>,
    /// GPU memory limit in bytes
    pub max_gpu_memory: Option<usize>,
}

/// Performance configuration
#[derive(Debug, Clone)]
pub struct PerformanceConfig {
    /// Enable SIMD optimizations
    pub enable_simd: bool,
    /// Number of threads for parallel operations
    pub num_threads: Option<usize>,
    /// Cache size for computations
    pub cache_size: usize,
    /// Enable GPU acceleration
    pub enable_gpu: bool,
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            enable_simd: true,
            num_threads: None,       // Use system default
            cache_size: 1024 * 1024, // 1MB
            enable_gpu: false,       // Disabled by default
        }
    }
}

/// Logging configuration
#[derive(Debug, Clone)]
pub struct LoggingConfig {
    /// Log level
    pub level: LogLevel,
    /// Enable module-specific logging
    pub module_logging: bool,
    /// Log file path
    pub log_file: Option<String>,
    /// Enable performance logging
    pub performance_logging: bool,
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: LogLevel::Info,
            module_logging: true,
            log_file: None,
            performance_logging: false,
        }
    }
}

/// Log levels
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LogLevel {
    Error,
    Warn,
    Info,
    Debug,
    Trace,
}

/// Configuration values
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConfigValue {
    Bool(bool),
    Int(i64),
    Float(f64),
    String(String),
    Array(Vec<ConfigValue>),
    Object(HashMap<String, ConfigValue>),
}

impl From<bool> for ConfigValue {
    fn from(value: bool) -> Self {
        ConfigValue::Bool(value)
    }
}

impl From<i64> for ConfigValue {
    fn from(value: i64) -> Self {
        ConfigValue::Int(value)
    }
}

impl From<f64> for ConfigValue {
    fn from(value: f64) -> Self {
        ConfigValue::Float(value)
    }
}

impl From<String> for ConfigValue {
    fn from(value: String) -> Self {
        ConfigValue::String(value)
    }
}

impl From<&str> for ConfigValue {
    fn from(value: &str) -> Self {
        ConfigValue::String(value.to_string())
    }
}

impl Default for ConfigValue {
    fn default() -> Self {
        ConfigValue::String(String::new())
    }
}

/// Configuration sources
#[derive(Debug, Clone)]
pub enum ConfigSource {
    File(std::path::PathBuf),
    Environment,
    Runtime,
    Default,
}

/// Configuration summary
#[derive(Debug, Clone)]
pub struct ConfigSummary {
    pub total_modules: usize,
    pub enabled_modules: usize,
    pub precision_level: PrecisionLevel,
    pub memory_strategy: MemoryStrategy,
    pub sources: Vec<ConfigSource>,
    pub cache_size: usize,
}

/// File-based configuration format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileConfig {
    pub integration: Option<FileIntegrationConfig>,
    pub modules: Option<HashMap<String, ModuleConfig>>,
}

/// Integration configuration in file format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileIntegrationConfig {
    pub auto_convert_tensors: Option<bool>,
    pub strict_compatibility: Option<bool>,
    pub default_precision: Option<String>,
    pub memory_strategy: Option<String>,
    pub error_mode: Option<String>,
}

/// Global configuration manager instance
static GLOBAL_CONFIG_MANAGER: std::sync::OnceLock<std::sync::Mutex<ConfigManager>> =
    std::sync::OnceLock::new();

/// Initialize global configuration manager
#[allow(dead_code)]
pub fn init_config_manager() -> &'static std::sync::Mutex<ConfigManager> {
    GLOBAL_CONFIG_MANAGER.get_or_init(|| {
        let mut manager = ConfigManager::new();

        // Try to load from environment
        let _ = manager.load_from_env();

        // Try to load from default config file
        if let Ok(config_path) = std::env::var("SCIRS2_CONFIG_PATH") {
            let _ = manager.load_from_file(config_path);
        }

        std::sync::Mutex::new(manager)
    })
}

/// Get global configuration value
#[allow(dead_code)]
pub fn get_config_value(key: &str) -> Result<Option<ConfigValue>, IntegrationError> {
    let manager = init_config_manager();
    let manager_guard = manager.lock().map_err(|_| {
        IntegrationError::ConfigMismatch("Failed to acquire config lock".to_string())
    })?;
    Ok(manager_guard.get(key).cloned())
}

/// Set global configuration value
#[allow(dead_code)]
pub fn set_config_value<T: Into<ConfigValue>>(key: &str, value: T) -> Result<(), IntegrationError> {
    let manager = init_config_manager();
    let mut manager_guard = manager.lock().map_err(|_| {
        IntegrationError::ConfigMismatch("Failed to acquire config lock".to_string())
    })?;
    manager_guard.set(key, value);
    Ok(())
}

/// Get module configuration
#[allow(dead_code)]
pub fn get_module_config(modulename: &str) -> Result<Option<ModuleConfig>, IntegrationError> {
    let manager = init_config_manager();
    let manager_guard = manager.lock().map_err(|_| {
        IntegrationError::ConfigMismatch("Failed to acquire config lock".to_string())
    })?;
    Ok(manager_guard.module_config(modulename).cloned())
}

/// Update global integration configuration
#[allow(dead_code)]
pub fn update_integration_config(config: IntegrationConfig) -> Result<(), IntegrationError> {
    let manager = init_config_manager();
    let mut manager_guard = manager.lock().map_err(|_| {
        IntegrationError::ConfigMismatch("Failed to acquire _config lock".to_string())
    })?;
    manager_guard.update_integration_config(config);
    Ok(())
}

/// Load configuration from file
#[allow(dead_code)]
pub fn load_config_from_file<P: AsRef<Path>>(path: P) -> Result<(), IntegrationError> {
    let manager = init_config_manager();
    let mut manager_guard = manager.lock().map_err(|_| {
        IntegrationError::ConfigMismatch("Failed to acquire config lock".to_string())
    })?;
    manager_guard.load_from_file(path)
}

/// Export configuration to file
#[allow(dead_code)]
pub fn export_config_to_file<P: AsRef<Path>>(path: P) -> Result<(), IntegrationError> {
    let manager = init_config_manager();
    let manager_guard = manager.lock().map_err(|_| {
        IntegrationError::ConfigMismatch("Failed to acquire config lock".to_string())
    })?;
    manager_guard.export_to_file(path)
}

/// Get configuration summary
#[allow(dead_code)]
pub fn get_config_summary() -> Result<ConfigSummary, IntegrationError> {
    let manager = init_config_manager();
    let manager_guard = manager.lock().map_err(|_| {
        IntegrationError::ConfigMismatch("Failed to acquire config lock".to_string())
    })?;
    Ok(manager_guard.summary())
}

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

    #[test]
    fn test_config_manager_creation() {
        let manager = ConfigManager::new();
        assert_eq!(manager.config.modules.len(), 3); // neural, optim, linalg
    }

    #[test]
    fn test_config_value_types() {
        let bool_val = ConfigValue::Bool(true);
        let int_val = ConfigValue::Int(42);
        let float_val = ConfigValue::Float(std::f64::consts::PI);
        let string_val = ConfigValue::String("test".to_string());

        assert!(matches!(bool_val, ConfigValue::Bool(true)));
        assert!(matches!(int_val, ConfigValue::Int(42)));
        assert!(
            matches!(float_val, ConfigValue::Float(f) if (f - std::f64::consts::PI).abs() < 1e-10)
        );
        assert!(matches!(string_val, ConfigValue::String(ref s) if s == "test"));
    }

    #[test]
    fn test_module_config() {
        let neural_config = ModuleConfig::default_neural();
        assert!(neural_config.enabled);
        assert!(neural_config
            .features
            .contains(&"automatic_differentiation".to_string()));
        assert!(neural_config.settings.contains_key("batch_size"));
    }

    #[test]
    fn test_config_value_conversions() {
        let bool_val: ConfigValue = true.into();
        let int_val: ConfigValue = 42i64.into();
        let float_val: ConfigValue = std::f64::consts::PI.into();
        let string_val: ConfigValue = "test".into();

        assert!(matches!(bool_val, ConfigValue::Bool(true)));
        assert!(matches!(int_val, ConfigValue::Int(42)));
        assert!(
            matches!(float_val, ConfigValue::Float(f) if (f - std::f64::consts::PI).abs() < 1e-10)
        );
        assert!(matches!(string_val, ConfigValue::String(ref s) if s == "test"));
    }

    #[test]
    fn test_resource_limits() {
        let limits = ResourceLimits {
            max_memory: Some(1024 * 1024 * 1024), // 1GB
            max_threads: Some(8),
            max_compute_time: Some(60.0),            // 60 seconds
            max_gpu_memory: Some(512 * 1024 * 1024), // 512MB
        };

        assert_eq!(limits.max_memory, Some(1024 * 1024 * 1024));
        assert_eq!(limits.max_threads, Some(8));
        assert_eq!(limits.max_compute_time, Some(60.0));
        assert_eq!(limits.max_gpu_memory, Some(512 * 1024 * 1024));
    }

    #[test]
    fn test_global_config_default() {
        let config = GlobalConfig::default();
        assert!(config.modules.contains_key("scirs2-neural"));
        assert!(config.modules.contains_key("scirs2-optim"));
        assert!(config.modules.contains_key("scirs2-linalg"));
        assert!(config.performance.enable_simd);
    }

    #[test]
    fn test_config_manager_set_get() {
        let mut manager = ConfigManager::new();

        manager.set("test_key", ConfigValue::String("test_value".to_string()));
        let retrieved = manager.get("test_key");

        assert!(retrieved.is_some());
        if let Some(ConfigValue::String(val)) = retrieved {
            assert_eq!(val, "test_value");
        } else {
            panic!("Expected string value");
        }
    }

    #[test]
    fn test_env_config_merge() {
        let mut manager = ConfigManager::new();
        let mut env_vars = HashMap::new();
        env_vars.insert("auto_convert_tensors".to_string(), "false".to_string());
        env_vars.insert("strict_compatibility".to_string(), "true".to_string());

        manager
            .merge_env_config(env_vars)
            .expect("Operation failed");

        assert!(!manager.config.integration.auto_convert_tensors);
        assert!(manager.config.integration.strict_compatibility);
    }

    #[test]
    fn test_config_validation() {
        let manager = ConfigManager::new();

        // Default config should be valid
        assert!(manager.validate().is_ok());
    }
}