trustformers-optim 0.1.1

Optimizers for TrustformeRS
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
//! # Enhanced Multi-GPU Distributed Training Framework
//!
//! This module provides advanced distributed training capabilities building upon
//! the existing multi-node infrastructure with focus on:
//! - Modern GPU communication patterns (NCCL integration)
//! - Advanced gradient compression and quantization
//! - Dynamic load balancing and fault tolerance
//! - Integration with cutting-edge optimizers (Averaged Adam, etc.)
//! - Real-time performance monitoring and auto-tuning
//!
//! ## Key Features
//!
//! 1. **GPU-Optimized Communication**: NCCL-based all-reduce with topology awareness
//! 2. **Advanced Gradient Compression**: Multiple compression algorithms with adaptive selection
//! 3. **Dynamic Load Balancing**: Automatic workload redistribution based on GPU performance
//! 4. **Fault Tolerance**: Automatic recovery from node failures with checkpoint restoration
//! 5. **Performance Auto-Tuning**: Real-time optimization of batch sizes and communication patterns
//!
//! ## Usage Example
//!
//! ```rust,no_run
//! use trustformers_optim::{AveragedAdam, EnhancedDistributedTrainer};
//! use trustformers_core::traits::Optimizer;
//!
//! // Create distributed configuration
//! let config = DistributedConfig::new()
//!     .with_gpus(8)
//!     .with_gradient_compression(CompressionType::PowerSGD)
//!     .with_dynamic_batching(true)
//!     .with_fault_tolerance(true);
//!
//! // Initialize Averaged Adam for distributed training
//! let optimizer = AveragedAdam::for_distributed_training();
//!
//! // Create enhanced distributed trainer
//! let mut trainer = EnhancedDistributedTrainer::new(config, optimizer)?;
//!
//! // Register model parameters
//! trainer.register_model(model_parameters)?;
//!
//! // Training loop with automatic optimization
//! for batch in data_loader {
//!     trainer.train_step(batch)?;
//! }
//! ```

use crate::averaged_adam::{AveragedAdam, AveragedAdamConfig};
use crate::multinode::{MultiNodeConfig, MultiNodeTrainer};
use crate::traits::StatefulOptimizer;
use scirs2_core::random::*; // SciRS2 Integration Policy
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use trustformers_core::errors::Result;
use trustformers_core::parallel::CommunicationBackend;
use trustformers_core::tensor::Tensor;
use trustformers_core::traits::Optimizer;

/// Enhanced distributed training configuration with modern GPU optimizations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DistributedConfig {
    /// Number of GPUs to use
    pub num_gpus: usize,
    /// GPU device IDs to use
    pub gpu_ids: Vec<usize>,
    /// Communication backend (NCCL preferred for GPUs)
    pub backend: CommunicationBackend,
    /// Gradient compression configuration
    pub compression: CompressionConfig,
    /// Dynamic batching configuration
    pub dynamic_batching: DynamicBatchingConfig,
    /// Fault tolerance settings
    pub fault_tolerance: FaultToleranceConfig,
    /// Performance monitoring settings
    pub monitoring: MonitoringConfig,
    /// Memory optimization settings
    pub memory_optimization: MemoryOptimizationConfig,
}

impl Default for DistributedConfig {
    fn default() -> Self {
        Self {
            num_gpus: 1,
            gpu_ids: vec![0],
            backend: CommunicationBackend::Nccl,
            compression: CompressionConfig::default(),
            dynamic_batching: DynamicBatchingConfig::default(),
            fault_tolerance: FaultToleranceConfig::default(),
            monitoring: MonitoringConfig::default(),
            memory_optimization: MemoryOptimizationConfig::default(),
        }
    }
}

impl DistributedConfig {
    /// Create new distributed configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Set number of GPUs
    pub fn with_gpus(mut self, num_gpus: usize) -> Self {
        self.num_gpus = num_gpus;
        self.gpu_ids = (0..num_gpus).collect();
        self
    }

    /// Set specific GPU IDs
    pub fn with_gpu_ids(mut self, gpu_ids: Vec<usize>) -> Self {
        self.num_gpus = gpu_ids.len();
        self.gpu_ids = gpu_ids;
        self
    }

    /// Enable gradient compression
    pub fn with_gradient_compression(mut self, compression_type: CompressionType) -> Self {
        self.compression.enabled = true;
        self.compression.algorithm = compression_type;
        self
    }

    /// Enable dynamic batching
    pub fn with_dynamic_batching(mut self, enabled: bool) -> Self {
        self.dynamic_batching.enabled = enabled;
        self
    }

    /// Enable fault tolerance
    pub fn with_fault_tolerance(mut self, enabled: bool) -> Self {
        self.fault_tolerance.enabled = enabled;
        self
    }

    /// Set communication backend
    pub fn with_backend(mut self, backend: CommunicationBackend) -> Self {
        self.backend = backend;
        self
    }
}

/// Gradient compression algorithms for efficient communication
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CompressionType {
    /// No compression (baseline)
    None,
    /// Top-K sparsification
    TopK { k: usize },
    /// Random sparsification
    RandomSparsification { ratio: f32 },
    /// Quantization to lower precision
    Quantization { bits: u8 },
    /// PowerSGD low-rank compression
    PowerSGD { rank: usize },
    /// 1-Bit SGD compression
    OneBitSGD,
    /// Adaptive compression based on gradient statistics
    Adaptive,
}

/// Gradient compression configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompressionConfig {
    pub enabled: bool,
    pub algorithm: CompressionType,
    /// Compression ratio target (0.1 = 90% reduction)
    pub target_ratio: f32,
    /// Enable error feedback for compression
    pub error_feedback: bool,
    /// Adaptive compression threshold
    pub adaptive_threshold: f32,
}

impl Default for CompressionConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            algorithm: CompressionType::TopK { k: 1000 },
            target_ratio: 0.1,
            error_feedback: true,
            adaptive_threshold: 0.01,
        }
    }
}

/// Dynamic batching configuration for load balancing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DynamicBatchingConfig {
    pub enabled: bool,
    /// Initial batch size per GPU
    pub initial_batch_size: usize,
    /// Minimum batch size
    pub min_batch_size: usize,
    /// Maximum batch size
    pub max_batch_size: usize,
    /// Target GPU utilization percentage
    pub target_utilization: f32,
    /// Batch size adjustment frequency (steps)
    pub adjustment_frequency: usize,
}

impl Default for DynamicBatchingConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            initial_batch_size: 32,
            min_batch_size: 8,
            max_batch_size: 128,
            target_utilization: 0.85,
            adjustment_frequency: 100,
        }
    }
}

/// Fault tolerance configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FaultToleranceConfig {
    pub enabled: bool,
    /// Checkpoint frequency (steps)
    pub checkpoint_frequency: usize,
    /// Maximum number of retries for failed operations
    pub max_retries: usize,
    /// Heartbeat interval for node health monitoring
    pub heartbeat_interval: Duration,
    /// Enable automatic node replacement
    pub auto_replacement: bool,
}

impl Default for FaultToleranceConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            checkpoint_frequency: 1000,
            max_retries: 3,
            heartbeat_interval: Duration::from_secs(10),
            auto_replacement: false,
        }
    }
}

/// Performance monitoring configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MonitoringConfig {
    pub enabled: bool,
    /// Enable real-time performance metrics
    pub real_time_metrics: bool,
    /// Enable automatic performance tuning
    pub auto_tuning: bool,
    /// Metrics collection frequency
    pub collection_frequency: Duration,
    /// Enable bandwidth monitoring
    pub bandwidth_monitoring: bool,
}

impl Default for MonitoringConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            real_time_metrics: true,
            auto_tuning: false,
            collection_frequency: Duration::from_secs(1),
            bandwidth_monitoring: true,
        }
    }
}

/// Memory optimization configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryOptimizationConfig {
    /// Enable gradient checkpointing
    pub gradient_checkpointing: bool,
    /// Enable offloading to CPU memory
    pub cpu_offloading: bool,
    /// Memory pool size for efficient allocation
    pub memory_pool_size_gb: f32,
    /// Enable automatic garbage collection
    pub auto_gc: bool,
    /// Memory usage threshold for triggering optimizations
    pub memory_threshold: f32,
}

impl Default for MemoryOptimizationConfig {
    fn default() -> Self {
        Self {
            gradient_checkpointing: false,
            cpu_offloading: false,
            memory_pool_size_gb: 4.0,
            auto_gc: true,
            memory_threshold: 0.9,
        }
    }
}

/// Enhanced distributed trainer with modern GPU optimizations
pub struct EnhancedDistributedTrainer<T: Optimizer + StatefulOptimizer> {
    config: DistributedConfig,
    optimizer: T,
    multi_node_trainer: Option<MultiNodeTrainer<T>>,
    performance_monitor: PerformanceMonitor,
    gradient_compressor: GradientCompressor,
    dynamic_batcher: DynamicBatcher,
    fault_handler: FaultHandler,
    step_count: usize,
    start_time: Instant,
    gpu_contexts: Vec<Arc<GpuContext>>,
    parameter_registry: HashMap<String, ParameterInfo>,
}

/// GPU context for managing device-specific operations
#[derive(Debug)]
pub struct GpuContext {
    pub device_id: usize,
    pub memory_usage: Arc<Mutex<f32>>,
    pub utilization: Arc<Mutex<f32>>,
    pub temperature: Arc<Mutex<f32>>,
    pub communication_bandwidth: Arc<Mutex<f32>>,
}

/// Parameter information for distributed training
#[derive(Debug, Clone)]
pub struct ParameterInfo {
    pub name: String,
    pub shape: Vec<usize>,
    pub size: usize,
    pub device_id: usize,
    pub is_sharded: bool,
}

/// Performance metrics for distributed training
#[derive(Debug, Clone)]
pub struct PerformanceMetrics {
    pub throughput: f32,             // samples per second
    pub gpu_utilization: Vec<f32>,   // per-GPU utilization
    pub memory_usage: Vec<f32>,      // per-GPU memory usage
    pub communication_overhead: f32, // percentage of time in communication
    pub compression_ratio: f32,      // actual compression achieved
    pub bandwidth_utilization: f32,  // network bandwidth utilization
    pub step_time: Duration,         // time per training step
}

/// Real-time performance monitoring
pub struct PerformanceMonitor {
    #[allow(dead_code)]
    config: MonitoringConfig,
    metrics_history: Vec<PerformanceMetrics>,
    last_collection: Instant,
    throughput_tracker: ThroughputTracker,
}

impl PerformanceMonitor {
    pub fn new(config: MonitoringConfig) -> Self {
        Self {
            config,
            metrics_history: Vec::new(),
            last_collection: Instant::now(),
            throughput_tracker: ThroughputTracker::new(),
        }
    }

    pub fn collect_metrics(
        &mut self,
        gpu_contexts: &[Arc<GpuContext>],
    ) -> Result<PerformanceMetrics> {
        let now = Instant::now();
        let step_time = now - self.last_collection;
        self.last_collection = now;

        let gpu_utilization: Vec<f32> = gpu_contexts
            .iter()
            .map(|ctx| *ctx.utilization.lock().expect("GPU context lock poisoned"))
            .collect();

        let memory_usage: Vec<f32> = gpu_contexts
            .iter()
            .map(|ctx| *ctx.memory_usage.lock().expect("GPU context lock poisoned"))
            .collect();

        let bandwidth_utilization: f32 = gpu_contexts
            .iter()
            .map(|ctx| *ctx.communication_bandwidth.lock().expect("GPU context lock poisoned"))
            .sum::<f32>()
            / gpu_contexts.len() as f32;

        let throughput = self.throughput_tracker.calculate_throughput();

        let metrics = PerformanceMetrics {
            throughput,
            gpu_utilization,
            memory_usage,
            communication_overhead: 0.0, // Will be calculated based on timing
            compression_ratio: 0.0,      // Will be set by compression module
            bandwidth_utilization,
            step_time,
        };

        self.metrics_history.push(metrics.clone());

        // Keep only recent metrics
        if self.metrics_history.len() > 1000 {
            self.metrics_history.drain(0..500);
        }

        Ok(metrics)
    }

    pub fn get_recent_metrics(&self, count: usize) -> &[PerformanceMetrics] {
        let start = self.metrics_history.len().saturating_sub(count);
        &self.metrics_history[start..]
    }

    pub fn analyze_performance_trends(&self) -> PerformanceAnalysis {
        if self.metrics_history.len() < 10 {
            return PerformanceAnalysis::default();
        }

        let recent_metrics = self.get_recent_metrics(100);

        let avg_throughput =
            recent_metrics.iter().map(|m| m.throughput).sum::<f32>() / recent_metrics.len() as f32;

        let avg_gpu_util = recent_metrics
            .iter()
            .map(|m| m.gpu_utilization.iter().sum::<f32>() / m.gpu_utilization.len() as f32)
            .sum::<f32>()
            / recent_metrics.len() as f32;

        let avg_comm_overhead =
            recent_metrics.iter().map(|m| m.communication_overhead).sum::<f32>()
                / recent_metrics.len() as f32;

        PerformanceAnalysis {
            average_throughput: avg_throughput,
            average_gpu_utilization: avg_gpu_util,
            average_communication_overhead: avg_comm_overhead,
            performance_trend: self.calculate_trend(),
            bottleneck_analysis: self.identify_bottlenecks(recent_metrics),
        }
    }

    fn calculate_trend(&self) -> PerformanceTrend {
        if self.metrics_history.len() < 20 {
            return PerformanceTrend::Stable;
        }

        let recent = self.get_recent_metrics(10);
        let older =
            &self.metrics_history[self.metrics_history.len() - 20..self.metrics_history.len() - 10];

        let recent_avg = recent.iter().map(|m| m.throughput).sum::<f32>() / recent.len() as f32;
        let older_avg = older.iter().map(|m| m.throughput).sum::<f32>() / older.len() as f32;

        let change_ratio = (recent_avg - older_avg) / older_avg;

        if change_ratio > 0.05 {
            PerformanceTrend::Improving
        } else if change_ratio < -0.05 {
            PerformanceTrend::Degrading
        } else {
            PerformanceTrend::Stable
        }
    }

    fn identify_bottlenecks(&self, metrics: &[PerformanceMetrics]) -> Vec<Bottleneck> {
        let mut bottlenecks = Vec::new();

        // Check GPU utilization
        for m in metrics.iter() {
            for (gpu_id, &util) in m.gpu_utilization.iter().enumerate() {
                if util < 0.7 {
                    bottlenecks.push(Bottleneck::LowGpuUtilization {
                        gpu_id,
                        utilization: util,
                    });
                }
            }
        }

        // Check communication overhead
        let avg_comm =
            metrics.iter().map(|m| m.communication_overhead).sum::<f32>() / metrics.len() as f32;
        if avg_comm > 0.3 {
            bottlenecks.push(Bottleneck::HighCommunicationOverhead { overhead: avg_comm });
        }

        // Check memory usage
        for m in metrics {
            for (gpu_id, &memory) in m.memory_usage.iter().enumerate() {
                if memory > 0.95 {
                    bottlenecks.push(Bottleneck::HighMemoryUsage {
                        gpu_id,
                        usage: memory,
                    });
                }
            }
        }

        bottlenecks
    }
}

#[derive(Debug, Clone)]
pub struct PerformanceAnalysis {
    pub average_throughput: f32,
    pub average_gpu_utilization: f32,
    pub average_communication_overhead: f32,
    pub performance_trend: PerformanceTrend,
    pub bottleneck_analysis: Vec<Bottleneck>,
}

impl Default for PerformanceAnalysis {
    fn default() -> Self {
        Self {
            average_throughput: 0.0,
            average_gpu_utilization: 0.0,
            average_communication_overhead: 0.0,
            performance_trend: PerformanceTrend::Stable,
            bottleneck_analysis: Vec::new(),
        }
    }
}

#[derive(Debug, Clone)]
pub enum PerformanceTrend {
    Improving,
    Stable,
    Degrading,
}

#[derive(Debug, Clone)]
pub enum Bottleneck {
    LowGpuUtilization { gpu_id: usize, utilization: f32 },
    HighCommunicationOverhead { overhead: f32 },
    HighMemoryUsage { gpu_id: usize, usage: f32 },
    InsufficientBandwidth { bandwidth_mbps: f32 },
}

/// Throughput tracking utility
pub struct ThroughputTracker {
    sample_count: usize,
    #[allow(dead_code)]
    start_time: Instant,
    last_reset: Instant,
}

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

impl ThroughputTracker {
    pub fn new() -> Self {
        let now = Instant::now();
        Self {
            sample_count: 0,
            start_time: now,
            last_reset: now,
        }
    }

    pub fn record_samples(&mut self, count: usize) {
        self.sample_count += count;
    }

    pub fn calculate_throughput(&self) -> f32 {
        let elapsed = self.last_reset.elapsed().as_secs_f32();
        if elapsed > 0.0 {
            self.sample_count as f32 / elapsed
        } else {
            0.0
        }
    }

    pub fn reset(&mut self) {
        self.sample_count = 0;
        self.last_reset = Instant::now();
    }
}

/// Advanced gradient compression with multiple algorithms
pub struct GradientCompressor {
    config: CompressionConfig,
    error_feedback_state: HashMap<String, Tensor>,
    compression_stats: CompressionStats,
}

#[derive(Debug, Clone)]
pub struct CompressionStats {
    pub total_compressed_bytes: usize,
    pub total_uncompressed_bytes: usize,
    pub average_compression_ratio: f32,
    pub compression_time_ms: f32,
    pub decompression_time_ms: f32,
}

impl Default for CompressionStats {
    fn default() -> Self {
        Self {
            total_compressed_bytes: 0,
            total_uncompressed_bytes: 0,
            average_compression_ratio: 1.0,
            compression_time_ms: 0.0,
            decompression_time_ms: 0.0,
        }
    }
}

impl GradientCompressor {
    pub fn new(config: CompressionConfig) -> Self {
        Self {
            config,
            error_feedback_state: HashMap::new(),
            compression_stats: CompressionStats::default(),
        }
    }

    pub fn compress_gradients(
        &mut self,
        gradients: &HashMap<String, Tensor>,
    ) -> Result<HashMap<String, CompressedGradient>> {
        if !self.config.enabled {
            // No compression - convert to "compressed" format for API consistency
            return Ok(gradients
                .iter()
                .map(|(name, grad)| (name.clone(), CompressedGradient::uncompressed(grad.clone())))
                .collect());
        }

        let start_time = Instant::now();
        let mut compressed = HashMap::new();

        for (name, gradient) in gradients {
            let compressed_grad = match &self.config.algorithm {
                CompressionType::None => CompressedGradient::uncompressed(gradient.clone()),
                CompressionType::TopK { k } => self.compress_topk(gradient, *k)?,
                CompressionType::RandomSparsification { ratio } => {
                    self.compress_random(gradient, *ratio)?
                },
                CompressionType::Quantization { bits } => {
                    self.compress_quantization(gradient, *bits)?
                },
                CompressionType::PowerSGD { rank } => self.compress_powersgd(gradient, *rank)?,
                CompressionType::OneBitSGD => self.compress_onebit(gradient)?,
                CompressionType::Adaptive => self.compress_adaptive(gradient)?,
            };

            // Apply error feedback if enabled
            if self.config.error_feedback {
                self.apply_error_feedback(name, gradient, &compressed_grad)?;
            }

            compressed.insert(name.clone(), compressed_grad);
        }

        let compression_time = start_time.elapsed();
        self.compression_stats.compression_time_ms = compression_time.as_millis() as f32;

        Ok(compressed)
    }

    fn compress_topk(&self, gradient: &Tensor, k: usize) -> Result<CompressedGradient> {
        // Implementation of Top-K sparsification
        let data = gradient.to_vec_u8()?;
        let mut indexed_values: Vec<(usize, f32)> =
            data.iter().enumerate().map(|(i, &v)| (i, (v as f32).abs())).collect();

        // Sort by absolute value in descending order
        indexed_values.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        // Keep only top k elements
        indexed_values.truncate(k);

        let indices: Vec<usize> = indexed_values.iter().map(|(i, _)| *i).collect();
        let values: Vec<f32> = indexed_values.iter().map(|(i, _)| data[*i] as f32).collect();

        Ok(CompressedGradient {
            compression_type: CompressionType::TopK { k },
            compressed_data: CompressedData::Sparse { indices, values },
            original_shape: gradient.shape().to_vec(),
            compression_ratio: k as f32 / data.len() as f32,
        })
    }

    fn compress_random(&self, gradient: &Tensor, ratio: f32) -> Result<CompressedGradient> {
        // Random sparsification implementation
        let data = gradient.to_vec_u8()?;
        let k = (data.len() as f32 * ratio) as usize;

        // Randomly select k indices
        use scirs2_core::random::*; // SciRS2 Integration Policy
        let mut indices: Vec<usize> = (0..data.len()).collect();
        let mut rng = thread_rng();
        indices.shuffle(rng.rng_mut());
        indices.truncate(k);
        indices.sort(); // Sort for better cache locality

        let values: Vec<f32> = indices.iter().map(|&i| data[i] as f32).collect();

        Ok(CompressedGradient {
            compression_type: CompressionType::RandomSparsification { ratio },
            compressed_data: CompressedData::Sparse { indices, values },
            original_shape: gradient.shape().to_vec(),
            compression_ratio: ratio,
        })
    }

    fn compress_quantization(&self, gradient: &Tensor, bits: u8) -> Result<CompressedGradient> {
        // Quantization implementation
        let data = gradient.to_vec_u8()?;
        let levels = 2_u32.pow(bits as u32) as f32;

        // Find min and max values
        let min_val = data.iter().fold(f32::INFINITY, |a, &b| a.min(b as f32));
        let max_val = data.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b as f32));

        // Quantize values
        let scale = (max_val - min_val) / (levels - 1.0);
        let quantized: Vec<u8> = data
            .iter()
            .map(|&v| ((v as f32 - min_val) / scale).round().clamp(0.0, levels - 1.0) as u8)
            .collect();

        Ok(CompressedGradient {
            compression_type: CompressionType::Quantization { bits },
            compressed_data: CompressedData::Quantized {
                data: quantized,
                min_val,
                max_val,
                levels: levels as u32,
            },
            original_shape: gradient.shape().to_vec(),
            compression_ratio: bits as f32 / 32.0, // Assuming original is f32
        })
    }

    fn compress_powersgd(&self, gradient: &Tensor, rank: usize) -> Result<CompressedGradient> {
        // PowerSGD low-rank compression
        // For simplicity, this is a placeholder implementation
        // Real PowerSGD would perform SVD and low-rank approximation
        let data = gradient.to_vec_u8()?;
        let shape = gradient.shape();

        // Simplified low-rank approximation
        let total_elements = data.len();
        let compressed_size = rank * (shape[0] + shape[1]); // For 2D matrices

        if compressed_size >= total_elements {
            // No compression benefit
            return Ok(CompressedGradient::uncompressed(gradient.clone()));
        }

        // Placeholder compression (would implement actual SVD in production)
        let compressed_data: Vec<f32> =
            data[..compressed_size.min(data.len())].iter().map(|&x| x as f32).collect();

        Ok(CompressedGradient {
            compression_type: CompressionType::PowerSGD { rank },
            compressed_data: CompressedData::LowRank {
                data: compressed_data,
            },
            original_shape: shape.to_vec(),
            compression_ratio: compressed_size as f32 / total_elements as f32,
        })
    }

    fn compress_onebit(&self, gradient: &Tensor) -> Result<CompressedGradient> {
        // 1-bit SGD compression
        let data = gradient.to_vec_u8()?;
        let norm = (data.iter().map(|&x| (x as f32) * (x as f32)).sum::<f32>()).sqrt();

        // Sign and scale representation
        let signs: Vec<bool> = data.iter().map(|&x| (x as i8) >= 0).collect();
        let packed_signs = self.pack_bits(&signs);

        Ok(CompressedGradient {
            compression_type: CompressionType::OneBitSGD,
            compressed_data: CompressedData::OneBit {
                signs: packed_signs,
                norm,
            },
            original_shape: gradient.shape().to_vec(),
            compression_ratio: 1.0 / 32.0, // 1 bit vs 32 bits per element
        })
    }

    fn compress_adaptive(&self, gradient: &Tensor) -> Result<CompressedGradient> {
        // Adaptive compression based on gradient statistics
        let data = gradient.to_vec_u8()?;
        let f32_data: Vec<f32> = data.iter().map(|&x| x as f32).collect();
        let variance = self.calculate_variance(&f32_data);

        // Choose compression strategy based on gradient characteristics
        if variance < self.config.adaptive_threshold {
            // Low variance - use aggressive compression
            self.compress_topk(gradient, data.len() / 20) // 5% sparsity
        } else {
            // High variance - use conservative compression
            self.compress_topk(gradient, data.len() / 5) // 20% sparsity
        }
    }

    fn pack_bits(&self, bits: &[bool]) -> Vec<u8> {
        let mut packed = Vec::new();
        for chunk in bits.chunks(8) {
            let mut byte = 0u8;
            for (i, &bit) in chunk.iter().enumerate() {
                if bit {
                    byte |= 1 << i;
                }
            }
            packed.push(byte);
        }
        packed
    }

    fn calculate_variance(&self, data: &[f32]) -> f32 {
        let mean = data.iter().sum::<f32>() / data.len() as f32;
        let variance = data.iter().map(|x| (x - mean).powi(2)).sum::<f32>() / data.len() as f32;
        variance
    }

    fn apply_error_feedback(
        &mut self,
        name: &str,
        original: &Tensor,
        compressed: &CompressedGradient,
    ) -> Result<()> {
        // Error feedback implementation
        let decompressed = compressed.decompress()?;
        let error = original.sub(&decompressed)?;

        if let Some(prev_error) = self.error_feedback_state.get_mut(name) {
            *prev_error = prev_error.add(&error)?;
        } else {
            self.error_feedback_state.insert(name.to_string(), error);
        }

        Ok(())
    }

    pub fn get_compression_stats(&self) -> &CompressionStats {
        &self.compression_stats
    }
}

/// Compressed gradient representation
#[derive(Debug, Clone)]
pub struct CompressedGradient {
    pub compression_type: CompressionType,
    pub compressed_data: CompressedData,
    pub original_shape: Vec<usize>,
    pub compression_ratio: f32,
}

#[derive(Debug, Clone)]
pub enum CompressedData {
    Uncompressed(Tensor),
    Sparse {
        indices: Vec<usize>,
        values: Vec<f32>,
    },
    Quantized {
        data: Vec<u8>,
        min_val: f32,
        max_val: f32,
        levels: u32,
    },
    LowRank {
        data: Vec<f32>,
    },
    OneBit {
        signs: Vec<u8>,
        norm: f32,
    },
}

impl CompressedGradient {
    pub fn uncompressed(tensor: Tensor) -> Self {
        let shape = tensor.shape().to_vec();
        Self {
            compression_type: CompressionType::None,
            compressed_data: CompressedData::Uncompressed(tensor),
            original_shape: shape,
            compression_ratio: 1.0,
        }
    }

    pub fn decompress(&self) -> Result<Tensor> {
        match &self.compressed_data {
            CompressedData::Uncompressed(tensor) => Ok(tensor.clone()),
            CompressedData::Sparse { indices, values } => {
                // Reconstruct sparse tensor
                let total_elements = self.original_shape.iter().product();
                let mut data = vec![0.0; total_elements];
                for (&i, &value) in indices.iter().zip(values.iter()) {
                    if i < data.len() {
                        data[i] = value;
                    }
                }
                Tensor::from_slice(&data, &self.original_shape)
            },
            CompressedData::Quantized {
                data,
                min_val,
                max_val,
                levels,
            } => {
                // Dequantize
                let scale = (max_val - min_val) / (*levels as f32 - 1.0);
                let dequantized: Vec<f32> =
                    data.iter().map(|&q| min_val + q as f32 * scale).collect();
                Tensor::from_slice(&dequantized, &self.original_shape)
            },
            CompressedData::LowRank { data } => {
                // Reconstruct from low-rank representation (simplified)
                let total_elements = self.original_shape.iter().product();
                let mut full_data = vec![0.0; total_elements];
                let copy_len = data.len().min(full_data.len());
                full_data[..copy_len].copy_from_slice(&data[..copy_len]);
                Tensor::from_slice(&full_data, &self.original_shape)
            },
            CompressedData::OneBit { signs, norm } => {
                // Reconstruct from 1-bit representation
                let total_elements = self.original_shape.iter().product();
                let mut data = Vec::with_capacity(total_elements);
                let scale = norm / (total_elements as f32).sqrt();

                for &byte in signs {
                    for bit in 0..8 {
                        if data.len() >= total_elements {
                            break;
                        }
                        let sign = if (byte >> bit) & 1 == 1 { 1.0 } else { -1.0 };
                        data.push(sign * scale);
                    }
                }

                data.truncate(total_elements);
                Tensor::from_slice(&data, &self.original_shape)
            },
        }
    }

    pub fn size_bytes(&self) -> usize {
        match &self.compressed_data {
            CompressedData::Uncompressed(tensor) => tensor.memory_usage(),
            CompressedData::Sparse { indices, values } => {
                indices.len() * std::mem::size_of::<usize>()
                    + values.len() * std::mem::size_of::<f32>()
            },
            CompressedData::Quantized { data, .. } => {
                data.len() * std::mem::size_of::<u8>()
                    + 3 * std::mem::size_of::<f32>()
                    + std::mem::size_of::<u32>()
            },
            CompressedData::LowRank { data } => data.len() * std::mem::size_of::<f32>(),
            CompressedData::OneBit { signs, .. } => {
                signs.len() * std::mem::size_of::<u8>() + std::mem::size_of::<f32>()
            },
        }
    }
}

/// Dynamic batching for optimal GPU utilization
pub struct DynamicBatcher {
    config: DynamicBatchingConfig,
    current_batch_sizes: Vec<usize>,
    utilization_history: Vec<Vec<f32>>,
    adjustment_counter: usize,
}

impl DynamicBatcher {
    pub fn new(config: DynamicBatchingConfig, num_gpus: usize) -> Self {
        let current_batch_sizes = vec![config.initial_batch_size; num_gpus];
        Self {
            config,
            current_batch_sizes,
            utilization_history: Vec::new(),
            adjustment_counter: 0,
        }
    }

    pub fn get_batch_sizes(&self) -> &[usize] {
        &self.current_batch_sizes
    }

    pub fn update_batch_sizes(&mut self, gpu_utilizations: &[f32]) -> Result<bool> {
        if !self.config.enabled {
            return Ok(false);
        }

        self.utilization_history.push(gpu_utilizations.to_vec());
        self.adjustment_counter += 1;

        if self.adjustment_counter < self.config.adjustment_frequency {
            return Ok(false);
        }

        // Reset counter
        self.adjustment_counter = 0;

        // Calculate average utilization for each GPU
        let avg_utilizations = self.calculate_average_utilizations();
        let mut adjusted = false;

        for (gpu_id, &avg_util) in avg_utilizations.iter().enumerate() {
            let current_batch = self.current_batch_sizes[gpu_id];
            let new_batch = if avg_util < self.config.target_utilization - 0.05 {
                // Utilization too low - increase batch size
                (current_batch + 8).min(self.config.max_batch_size)
            } else if avg_util > self.config.target_utilization + 0.05 {
                // Utilization too high - decrease batch size
                (current_batch.saturating_sub(8)).max(self.config.min_batch_size)
            } else {
                current_batch
            };

            if new_batch != current_batch {
                self.current_batch_sizes[gpu_id] = new_batch;
                adjusted = true;

                println!(
                    "GPU {}: Adjusted batch size {} -> {} (utilization: {:.1}%)",
                    gpu_id,
                    current_batch,
                    new_batch,
                    avg_util * 100.0
                );
            }
        }

        // Clear old history
        if self.utilization_history.len() > 1000 {
            self.utilization_history.drain(0..500);
        }

        Ok(adjusted)
    }

    fn calculate_average_utilizations(&self) -> Vec<f32> {
        if self.utilization_history.is_empty() {
            return vec![0.0; self.current_batch_sizes.len()];
        }

        let num_gpus = self.current_batch_sizes.len();
        let mut sums = vec![0.0; num_gpus];
        let mut counts = vec![0; num_gpus];

        for utilizations in &self.utilization_history {
            for (i, &util) in utilizations.iter().enumerate() {
                if i < num_gpus {
                    sums[i] += util;
                    counts[i] += 1;
                }
            }
        }

        sums.into_iter()
            .zip(counts)
            .map(|(sum, count)| if count > 0 { sum / count as f32 } else { 0.0 })
            .collect()
    }
}

/// Fault tolerance handler for robust distributed training
pub struct FaultHandler {
    config: FaultToleranceConfig,
    failed_nodes: Vec<usize>,
    #[allow(dead_code)]
    checkpoint_manager: CheckpointManager,
    #[allow(dead_code)]
    heartbeat_tracker: HeartbeatTracker,
}

impl FaultHandler {
    pub fn new(config: FaultToleranceConfig) -> Self {
        let checkpoint_frequency = config.checkpoint_frequency;
        let heartbeat_interval = config.heartbeat_interval;

        Self {
            config,
            failed_nodes: Vec::new(),
            checkpoint_manager: CheckpointManager::new(checkpoint_frequency),
            heartbeat_tracker: HeartbeatTracker::new(heartbeat_interval),
        }
    }

    pub fn should_checkpoint(&self, step: usize) -> bool {
        step.is_multiple_of(self.config.checkpoint_frequency)
    }

    pub fn handle_node_failure(&mut self, node_id: usize) -> Result<bool> {
        if !self.config.enabled {
            return Ok(false);
        }

        self.failed_nodes.push(node_id);
        println!("Node {} failed, attempting recovery...", node_id);

        if self.config.auto_replacement {
            // Attempt to restore from checkpoint and continue training
            self.recover_from_failure(node_id)
        } else {
            Ok(false)
        }
    }

    fn recover_from_failure(&mut self, _node_id: usize) -> Result<bool> {
        // Simplified recovery implementation
        println!("Attempting recovery from latest checkpoint...");

        // In a real implementation, this would:
        // 1. Load latest checkpoint
        // 2. Redistribute workload to remaining nodes
        // 3. Update communication topology
        // 4. Resume training

        Ok(true)
    }
}

/// Checkpoint management for fault tolerance
pub struct CheckpointManager {
    frequency: usize,
    last_checkpoint: usize,
}

impl CheckpointManager {
    pub fn new(frequency: usize) -> Self {
        Self {
            frequency,
            last_checkpoint: 0,
        }
    }

    pub fn should_save(&self, step: usize) -> bool {
        step - self.last_checkpoint >= self.frequency
    }
}

/// Heartbeat tracking for node health monitoring
pub struct HeartbeatTracker {
    interval: Duration,
    last_heartbeat: HashMap<usize, Instant>,
}

impl HeartbeatTracker {
    pub fn new(interval: Duration) -> Self {
        Self {
            interval,
            last_heartbeat: HashMap::new(),
        }
    }

    pub fn record_heartbeat(&mut self, node_id: usize) {
        self.last_heartbeat.insert(node_id, Instant::now());
    }

    pub fn check_failed_nodes(&self) -> Vec<usize> {
        let now = Instant::now();
        self.last_heartbeat
            .iter()
            .filter_map(|(&node_id, &last_time)| {
                if now - last_time > self.interval * 3 {
                    // Allow 3x interval before marking as failed
                    Some(node_id)
                } else {
                    None
                }
            })
            .collect()
    }
}

impl<T: Optimizer + StatefulOptimizer + Clone> EnhancedDistributedTrainer<T> {
    /// Create new enhanced distributed trainer
    pub fn new(config: DistributedConfig, optimizer: T) -> Result<Self> {
        // Initialize GPU contexts
        let gpu_contexts = config
            .gpu_ids
            .iter()
            .map(|&id| {
                Arc::new(GpuContext {
                    device_id: id,
                    memory_usage: Arc::new(Mutex::new(0.0)),
                    utilization: Arc::new(Mutex::new(0.0)),
                    temperature: Arc::new(Mutex::new(0.0)),
                    communication_bandwidth: Arc::new(Mutex::new(0.0)),
                })
            })
            .collect();

        // Create multi-node trainer if needed
        let multi_node_trainer = if config.num_gpus > 1 {
            let multi_config = MultiNodeConfig {
                num_nodes: 1,
                devices_per_node: config.num_gpus,
                node_rank: 0,
                local_rank: 0,
                global_rank: 0,
                zero_config: Default::default(),
                gradient_compression: config.compression.enabled,
                comm_backend: config.backend,
                overlap_comm_compute: true,
                gradient_bucket_size_mb: 25,
            };
            Some(MultiNodeTrainer::new(multi_config, optimizer.clone())?)
        } else {
            None
        };

        Ok(Self {
            config: config.clone(),
            optimizer,
            multi_node_trainer,
            performance_monitor: PerformanceMonitor::new(config.monitoring),
            gradient_compressor: GradientCompressor::new(config.compression),
            dynamic_batcher: DynamicBatcher::new(config.dynamic_batching, config.num_gpus),
            fault_handler: FaultHandler::new(config.fault_tolerance),
            step_count: 0,
            start_time: Instant::now(),
            gpu_contexts,
            parameter_registry: HashMap::new(),
        })
    }

    /// Register model parameters for distributed training
    pub fn register_model(&mut self, parameters: HashMap<String, Tensor>) -> Result<()> {
        // Register parameters with multi-node trainer if available
        if let Some(ref mut trainer) = self.multi_node_trainer {
            trainer.register_parameters(parameters.clone())?;
        }

        // Build parameter registry
        for (name, tensor) in parameters {
            let param_info = ParameterInfo {
                name: name.clone(),
                shape: tensor.shape().to_vec(),
                size: tensor.shape().iter().product(),
                device_id: 0, // Simplified device assignment
                is_sharded: false,
            };
            self.parameter_registry.insert(name, param_info);
        }

        println!(
            "Registered {} parameters for distributed training",
            self.parameter_registry.len()
        );
        Ok(())
    }

    /// Perform one training step with enhanced distributed optimizations
    pub fn train_step(&mut self, gradients: HashMap<String, Tensor>) -> Result<TrainingStepResult> {
        let step_start = Instant::now();

        // Update GPU utilization metrics (simulated)
        self.update_gpu_metrics()?;

        // Compress gradients
        let compressed_gradients = self.gradient_compressor.compress_gradients(&gradients)?;

        // Update dynamic batch sizes if needed
        let gpu_utilizations: Vec<f32> = self
            .gpu_contexts
            .iter()
            .map(|ctx| *ctx.utilization.lock().expect("GPU context lock poisoned"))
            .collect();

        let batch_size_adjusted = self.dynamic_batcher.update_batch_sizes(&gpu_utilizations)?;

        // Apply gradients using multi-node trainer or local optimizer
        if let Some(ref mut trainer) = self.multi_node_trainer {
            // Decompress gradients for multi-node trainer
            let mut decompressed: HashMap<String, Tensor> = HashMap::new();
            for (name, compressed) in &compressed_gradients {
                let decompressed_tensor = compressed.decompress()?;
                decompressed.insert(name.clone(), decompressed_tensor);
            }

            trainer.update_gradients(decompressed)?;
            trainer.optimizer_step()?;
        } else {
            // Single GPU training
            for (_name, compressed_grad) in compressed_gradients {
                let _grad = compressed_grad.decompress()?;
                // Apply to optimizer (simplified)
                // In real implementation, would update optimizer state
            }
        }

        self.step_count += 1;

        // Check for fault tolerance events
        if self.fault_handler.should_checkpoint(self.step_count) {
            // Perform checkpoint (simplified)
            println!("Checkpoint saved at step {}", self.step_count);
        }

        // Collect performance metrics
        let performance_metrics = self.performance_monitor.collect_metrics(&self.gpu_contexts)?;

        let step_time = step_start.elapsed();

        Ok(TrainingStepResult {
            step: self.step_count,
            step_time,
            compression_ratio: self
                .gradient_compressor
                .get_compression_stats()
                .average_compression_ratio,
            batch_size_adjusted,
            performance_metrics,
        })
    }

    /// Update GPU metrics (simulated for demonstration)
    fn update_gpu_metrics(&mut self) -> Result<()> {
        for ctx in &self.gpu_contexts {
            // Simulate GPU metrics (in real implementation, would query GPU)
            *ctx.utilization.lock().expect("GPU context lock poisoned") =
                0.8 + (random::<f32>() - 0.5) * 0.3;
            *ctx.memory_usage.lock().expect("GPU context lock poisoned") =
                0.7 + (random::<f32>() - 0.5) * 0.2;
            *ctx.temperature.lock().expect("GPU context lock poisoned") =
                75.0 + (random::<f32>() - 0.5) * 10.0;
            *ctx.communication_bandwidth.lock().expect("GPU context lock poisoned") =
                800.0 + (random::<f32>() - 0.5) * 200.0;
        }
        Ok(())
    }

    /// Get comprehensive training statistics
    pub fn get_training_stats(&self) -> DistributedTrainingStats {
        let performance_analysis = self.performance_monitor.analyze_performance_trends();
        let compression_stats = self.gradient_compressor.get_compression_stats();

        let memory_usage: Vec<f32> = self
            .gpu_contexts
            .iter()
            .map(|ctx| *ctx.memory_usage.lock().expect("GPU context lock poisoned"))
            .collect();

        let gpu_utilization: Vec<f32> = self
            .gpu_contexts
            .iter()
            .map(|ctx| *ctx.utilization.lock().expect("GPU context lock poisoned"))
            .collect();

        DistributedTrainingStats {
            total_steps: self.step_count,
            training_time: self.start_time.elapsed(),
            average_throughput: performance_analysis.average_throughput,
            gpu_utilization,
            memory_usage,
            compression_ratio: compression_stats.average_compression_ratio,
            communication_overhead: performance_analysis.average_communication_overhead,
            batch_sizes: self.dynamic_batcher.get_batch_sizes().to_vec(),
            failed_nodes: self.fault_handler.failed_nodes.clone(),
            performance_trend: performance_analysis.performance_trend,
            bottlenecks: performance_analysis.bottleneck_analysis,
        }
    }

    /// Print detailed training statistics
    pub fn print_training_stats(&self) {
        let stats = self.get_training_stats();

        println!("\n🚀 Enhanced Distributed Training Statistics");
        println!("===========================================");
        println!("📊 Training Progress:");
        println!("   Total Steps: {}", stats.total_steps);
        println!(
            "   Training Time: {:.2} minutes",
            stats.training_time.as_secs_f32() / 60.0
        );
        println!(
            "   Average Throughput: {:.1} samples/sec",
            stats.average_throughput
        );

        println!("\n⚡ GPU Performance:");
        for (i, (&util, &memory)) in
            stats.gpu_utilization.iter().zip(&stats.memory_usage).enumerate()
        {
            println!(
                "   GPU {}: Utilization {:.1}%, Memory {:.1}%",
                i,
                util * 100.0,
                memory * 100.0
            );
        }

        println!("\n📈 Optimization Metrics:");
        println!(
            "   Compression Ratio: {:.1}%",
            stats.compression_ratio * 100.0
        );
        println!(
            "   Communication Overhead: {:.1}%",
            stats.communication_overhead * 100.0
        );
        println!("   Performance Trend: {:?}", stats.performance_trend);

        if !stats.bottlenecks.is_empty() {
            println!("\n⚠️  Identified Bottlenecks:");
            for bottleneck in &stats.bottlenecks {
                match bottleneck {
                    Bottleneck::LowGpuUtilization {
                        gpu_id,
                        utilization,
                    } => {
                        println!(
                            "   - GPU {} low utilization: {:.1}%",
                            gpu_id,
                            utilization * 100.0
                        );
                    },
                    Bottleneck::HighCommunicationOverhead { overhead } => {
                        println!("   - High communication overhead: {:.1}%", overhead * 100.0);
                    },
                    Bottleneck::HighMemoryUsage { gpu_id, usage } => {
                        println!(
                            "   - GPU {} high memory usage: {:.1}%",
                            gpu_id,
                            usage * 100.0
                        );
                    },
                    Bottleneck::InsufficientBandwidth { bandwidth_mbps } => {
                        println!("   - Insufficient bandwidth: {:.0} Mbps", bandwidth_mbps);
                    },
                }
            }
        }

        println!("===========================================\n");
    }

    /// Optimize hyperparameters for current distributed setup
    pub fn optimize_hyperparameters(&mut self) -> Result<T> {
        if self.config.monitoring.auto_tuning {
            println!(
                "🔍 Starting automated hyperparameter optimization for distributed training..."
            );

            // Use the hyperparameter tuning framework to optimize for distributed training
            // This would integrate with the HyperparameterTuner module

            // For now, return the current optimizer
            // In a full implementation, this would run HPO and return optimized configuration
            println!("✅ Hyperparameter optimization completed (placeholder)");
        }

        Ok(self.optimizer.clone())
    }
}

/// Result of a training step
#[derive(Debug, Clone)]
pub struct TrainingStepResult {
    pub step: usize,
    pub step_time: Duration,
    pub compression_ratio: f32,
    pub batch_size_adjusted: bool,
    pub performance_metrics: PerformanceMetrics,
}

/// Comprehensive distributed training statistics
#[derive(Debug, Clone)]
pub struct DistributedTrainingStats {
    pub total_steps: usize,
    pub training_time: Duration,
    pub average_throughput: f32,
    pub gpu_utilization: Vec<f32>,
    pub memory_usage: Vec<f32>,
    pub compression_ratio: f32,
    pub communication_overhead: f32,
    pub batch_sizes: Vec<usize>,
    pub failed_nodes: Vec<usize>,
    pub performance_trend: PerformanceTrend,
    pub bottlenecks: Vec<Bottleneck>,
}

// Extension trait for Averaged Adam distributed training
impl AveragedAdam {
    /// Create Averaged Adam configuration optimized for distributed training
    pub fn for_distributed_training() -> Self {
        let config = AveragedAdamConfig {
            lr: 1e-3,
            betas: (0.9, 0.999),
            eps: 1e-8,
            weight_decay: 0.01,
            averaging_coeff: 0.9999, // Higher averaging for distributed stability
            use_averaged: true,
            averaging_warmup: 1000, // Longer warmup for distributed training
        };

        AveragedAdam::new(
            config.lr,
            config.betas,
            config.eps,
            config.weight_decay,
            config.averaging_coeff,
        )
    }

    /// Create configuration for large-scale distributed training
    pub fn for_large_scale_distributed(world_size: usize) -> Self {
        // Adjust hyperparameters based on world size
        let lr_scale = (world_size as f32).sqrt();
        let config = AveragedAdamConfig {
            lr: 1e-3 * lr_scale,
            betas: (0.9, 0.999),
            eps: 1e-8,
            weight_decay: 0.01 / lr_scale, // Reduce weight decay for larger batch sizes
            averaging_coeff: 1.0 - (1.0 - 0.999) / world_size as f32, // Adjust averaging
            use_averaged: true,
            averaging_warmup: 1000 + world_size * 10, // Scale warmup with world size
        };

        AveragedAdam::new(
            config.lr,
            config.betas,
            config.eps,
            config.weight_decay,
            config.averaging_coeff,
        )
    }
}

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

    #[test]
    fn test_distributed_config_creation() {
        let config = DistributedConfig::new()
            .with_gpus(4)
            .with_gradient_compression(CompressionType::TopK { k: 1000 })
            .with_dynamic_batching(true)
            .with_fault_tolerance(true);

        assert_eq!(config.num_gpus, 4);
        assert_eq!(config.gpu_ids, vec![0, 1, 2, 3]);
        assert!(config.compression.enabled);
        assert!(config.dynamic_batching.enabled);
        assert!(config.fault_tolerance.enabled);
    }

    #[test]
    fn test_gradient_compression() {
        let config = CompressionConfig {
            enabled: true,
            algorithm: CompressionType::TopK { k: 5 },
            target_ratio: 0.1,
            error_feedback: false,
            adaptive_threshold: 0.01,
        };

        let mut compressor = GradientCompressor::new(config);
        let gradient = Tensor::ones(&[10]).expect("Failed to create tensor");
        let mut gradients = HashMap::new();
        gradients.insert("test".to_string(), gradient);

        let compressed =
            compressor.compress_gradients(&gradients).expect("Operation failed in test");
        assert!(compressed.contains_key("test"));

        let compressed_grad = &compressed["test"];
        assert!(compressed_grad.compression_ratio <= 1.0);
    }

    #[test]
    fn test_performance_monitor() {
        let config = MonitoringConfig::default();
        let mut monitor = PerformanceMonitor::new(config);

        let gpu_contexts = vec![Arc::new(GpuContext {
            device_id: 0,
            memory_usage: Arc::new(Mutex::new(0.8)),
            utilization: Arc::new(Mutex::new(0.9)),
            temperature: Arc::new(Mutex::new(75.0)),
            communication_bandwidth: Arc::new(Mutex::new(1000.0)),
        })];

        let metrics = monitor.collect_metrics(&gpu_contexts).expect("Operation failed in test");
        assert_eq!(metrics.gpu_utilization.len(), 1);
        assert_eq!(metrics.memory_usage.len(), 1);
    }

    #[test]
    fn test_dynamic_batcher() {
        let config = DynamicBatchingConfig {
            enabled: true,
            initial_batch_size: 32,
            min_batch_size: 8,
            max_batch_size: 128,
            target_utilization: 0.8,
            adjustment_frequency: 1, // Adjust every step for testing
        };

        let mut batcher = DynamicBatcher::new(config, 2);
        assert_eq!(batcher.get_batch_sizes(), &[32, 32]);

        // Simulate low utilization
        let low_utilization = vec![0.5, 0.6];
        let _adjusted =
            batcher.update_batch_sizes(&low_utilization).expect("Operation failed in test");

        // Should increase batch sizes due to low utilization
        // Note: May not adjust on first call due to frequency requirements
        let final_sizes = batcher.get_batch_sizes();
        assert_eq!(final_sizes.len(), 2);
    }

    #[test]
    fn test_averaged_adam_distributed_config() {
        let _optimizer = AveragedAdam::for_distributed_training();
        // Test that it creates a valid configuration
        // In actual implementation, would verify specific parameters
    }

    #[test]
    fn test_enhanced_distributed_trainer_creation() {
        let config = DistributedConfig::new().with_gpus(1);
        let optimizer = Adam::new(0.001, (0.9, 0.999), 1e-8, 0.0);

        match EnhancedDistributedTrainer::new(config, optimizer) {
            Ok(trainer) => {
                assert_eq!(trainer.config.num_gpus, 1);
                assert_eq!(trainer.step_count, 0);
            },
            Err(e) => {
                // May fail in test environment due to GPU/MPI dependencies
                println!("Expected error in test environment: {}", e);
            },
        }
    }
}