torsh-distributed 0.1.2

Distributed training and inference for ToRSh
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
//! Advanced Monitoring and Performance Analytics for Distributed Training
//!
//! This module provides comprehensive monitoring capabilities for distributed training,
//! including real-time metrics collection, anomaly detection, trend analysis, and
//! automatic optimization recommendations.
//!
//! # Features
//!
//! - **Real-time Metrics**: Continuous collection of performance metrics across all ranks
//! - **Anomaly Detection**: Automatic detection of performance degradation and bottlenecks
//! - **Trend Analysis**: Historical analysis to identify performance patterns
//! - **Optimization Recommendations**: AI-powered suggestions for improving training performance
//! - **Multi-rank Coordination**: Synchronized metrics collection across distributed workers
//! - **Export Capabilities**: Integration with external monitoring tools (Prometheus, Grafana)

use crate::{ProcessGroup, TorshResult};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tracing::{debug, info, warn};

/// Maximum number of historical samples to keep per metric
const MAX_HISTORY_SIZE: usize = 1000;

/// Threshold for detecting performance anomalies (z-score)
const ANOMALY_THRESHOLD: f64 = 2.5;

/// Minimum samples required for statistical analysis
const MIN_SAMPLES_FOR_ANALYSIS: usize = 10;

/// Advanced performance metrics for distributed training
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdvancedMetrics {
    /// Timestamp when metrics were collected
    pub timestamp: Duration,

    /// Compute metrics
    pub compute: ComputeMetrics,

    /// Communication metrics
    pub communication: CommunicationMetrics,

    /// Memory metrics
    pub memory: MemoryMetrics,

    /// I/O metrics
    pub io: IoMetrics,

    /// Custom user-defined metrics
    pub custom: HashMap<String, f64>,
}

/// Compute-related performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComputeMetrics {
    /// Forward pass time (milliseconds)
    pub forward_time_ms: f64,

    /// Backward pass time (milliseconds)
    pub backward_time_ms: f64,

    /// Optimizer step time (milliseconds)
    pub optimizer_time_ms: f64,

    /// GPU utilization percentage (0-100)
    pub gpu_utilization: f64,

    /// CPU utilization percentage (0-100)
    pub cpu_utilization: f64,

    /// Tensor core utilization (0-100)
    pub tensor_core_utilization: f64,

    /// FLOPS achieved (GFLOPS)
    pub gflops: f64,
}

/// Communication-related performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommunicationMetrics {
    /// All-reduce operation time (milliseconds)
    pub all_reduce_time_ms: f64,

    /// Broadcast operation time (milliseconds)
    pub broadcast_time_ms: f64,

    /// All-gather operation time (milliseconds)
    pub all_gather_time_ms: f64,

    /// Network bandwidth utilization (MB/s)
    pub bandwidth_mbps: f64,

    /// Communication to computation ratio
    pub comm_comp_ratio: f64,

    /// Number of communication operations
    pub num_operations: u64,

    /// Average message size (bytes)
    pub avg_message_size: usize,
}

/// Memory-related performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryMetrics {
    /// GPU memory used (MB)
    pub gpu_memory_used_mb: f64,

    /// GPU memory total (MB)
    pub gpu_memory_total_mb: f64,

    /// CPU memory used (MB)
    pub cpu_memory_used_mb: f64,

    /// Memory bandwidth utilization (GB/s)
    pub memory_bandwidth_gbps: f64,

    /// Number of memory allocations
    pub num_allocations: u64,

    /// Peak memory usage (MB)
    pub peak_memory_mb: f64,
}

/// I/O-related performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IoMetrics {
    /// Data loading time (milliseconds)
    pub data_load_time_ms: f64,

    /// Disk read throughput (MB/s)
    pub disk_read_mbps: f64,

    /// Disk write throughput (MB/s)
    pub disk_write_mbps: f64,

    /// Data preprocessing time (milliseconds)
    pub preprocessing_time_ms: f64,
}

/// Performance anomaly detected in the system
#[derive(Debug, Clone)]
pub struct PerformanceAnomaly {
    /// Type of anomaly detected
    pub anomaly_type: AnomalyType,

    /// Severity level (0-10)
    pub severity: u8,

    /// Metric that triggered the anomaly
    pub metric_name: String,

    /// Current value of the metric
    pub current_value: f64,

    /// Expected value based on historical data
    pub expected_value: f64,

    /// Deviation from expected (z-score)
    pub deviation: f64,

    /// Timestamp when anomaly was detected (seconds since monitoring started)
    pub timestamp_secs: f64,

    /// Description of the anomaly
    pub description: String,
}

/// Types of performance anomalies
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AnomalyType {
    /// Sudden spike in execution time
    PerformanceSpike,

    /// Gradual performance degradation
    PerformanceDegradation,

    /// Memory leak detected
    MemoryLeak,

    /// Communication bottleneck
    CommunicationBottleneck,

    /// GPU underutilization
    GpuUnderutilization,

    /// I/O bottleneck
    IoBottleneck,

    /// Imbalanced load across ranks
    LoadImbalance,
}

/// Optimization recommendation for improving performance
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationRecommendation {
    /// Category of the recommendation
    pub category: RecommendationCategory,

    /// Priority level (1-10, higher is more important)
    pub priority: u8,

    /// Title of the recommendation
    pub title: String,

    /// Detailed description
    pub description: String,

    /// Expected performance improvement (percentage)
    pub expected_improvement: f64,

    /// Implementation difficulty (1-5)
    pub difficulty: u8,

    /// Code example or configuration change
    pub code_example: Option<String>,
}

/// Categories of optimization recommendations
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RecommendationCategory {
    /// Batch size optimization
    BatchSize,

    /// Gradient accumulation tuning
    GradientAccumulation,

    /// Communication optimization
    Communication,

    /// Memory management
    Memory,

    /// Data loading optimization
    DataLoading,

    /// Model architecture
    Architecture,

    /// Mixed precision training
    MixedPrecision,
}

/// Statistical summary of a metric over time
#[derive(Debug, Clone)]
struct MetricStatistics {
    /// Mean value
    mean: f64,

    /// Standard deviation
    std_dev: f64,

    /// Minimum value
    #[allow(dead_code)]
    min: f64,

    /// Maximum value
    #[allow(dead_code)]
    max: f64,

    /// Median value
    #[allow(dead_code)]
    median: f64,

    /// 95th percentile
    #[allow(dead_code)]
    p95: f64,

    /// 99th percentile
    #[allow(dead_code)]
    p99: f64,
}

/// Advanced monitoring system for distributed training
pub struct AdvancedMonitor {
    /// Process group for distributed coordination
    process_group: Arc<ProcessGroup>,

    /// Historical metrics per rank
    metrics_history: Arc<RwLock<HashMap<u32, VecDeque<AdvancedMetrics>>>>,

    /// Detected anomalies
    anomalies: Arc<RwLock<Vec<PerformanceAnomaly>>>,

    /// Generated recommendations
    recommendations: Arc<RwLock<Vec<OptimizationRecommendation>>>,

    /// Start time for relative timestamps
    start_time: Instant,

    /// Whether monitoring is enabled
    enabled: Arc<RwLock<bool>>,

    /// Custom metric thresholds
    custom_thresholds: Arc<RwLock<HashMap<String, f64>>>,
}

impl AdvancedMonitor {
    /// Create a new advanced monitoring system
    pub fn new(process_group: Arc<ProcessGroup>) -> Self {
        Self {
            process_group,
            metrics_history: Arc::new(RwLock::new(HashMap::new())),
            anomalies: Arc::new(RwLock::new(Vec::new())),
            recommendations: Arc::new(RwLock::new(Vec::new())),
            start_time: Instant::now(),
            enabled: Arc::new(RwLock::new(true)),
            custom_thresholds: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Record metrics for the current rank
    pub fn record_metrics(&self, metrics: AdvancedMetrics) -> TorshResult<()> {
        if !*self.enabled.read() {
            return Ok(());
        }

        let rank = self.process_group.rank();
        let mut history = self.metrics_history.write();

        let rank_history = history.entry(rank).or_default();
        rank_history.push_back(metrics.clone());

        // Keep only the most recent samples
        if rank_history.len() > MAX_HISTORY_SIZE {
            rank_history.pop_front();
        }

        // Perform anomaly detection
        self.detect_anomalies(&metrics, rank_history)?;

        debug!(
            "Recorded metrics for rank {}: compute={:.2}ms, comm={:.2}ms",
            rank,
            metrics.compute.forward_time_ms + metrics.compute.backward_time_ms,
            metrics.communication.all_reduce_time_ms
        );

        Ok(())
    }

    /// Detect performance anomalies in the metrics
    fn detect_anomalies(
        &self,
        current: &AdvancedMetrics,
        history: &VecDeque<AdvancedMetrics>,
    ) -> TorshResult<()> {
        if history.len() < MIN_SAMPLES_FOR_ANALYSIS {
            return Ok(());
        }

        // Analyze forward pass time
        self.check_metric_anomaly(
            "forward_time_ms",
            current.compute.forward_time_ms,
            history.iter().map(|m| m.compute.forward_time_ms).collect(),
            AnomalyType::PerformanceSpike,
        )?;

        // Analyze communication time
        self.check_metric_anomaly(
            "all_reduce_time_ms",
            current.communication.all_reduce_time_ms,
            history
                .iter()
                .map(|m| m.communication.all_reduce_time_ms)
                .collect(),
            AnomalyType::CommunicationBottleneck,
        )?;

        // Analyze memory usage
        self.check_metric_anomaly(
            "gpu_memory_used_mb",
            current.memory.gpu_memory_used_mb,
            history
                .iter()
                .map(|m| m.memory.gpu_memory_used_mb)
                .collect(),
            AnomalyType::MemoryLeak,
        )?;

        // Check GPU utilization
        if current.compute.gpu_utilization < 50.0 {
            self.report_anomaly(PerformanceAnomaly {
                anomaly_type: AnomalyType::GpuUnderutilization,
                severity: 7,
                metric_name: "gpu_utilization".to_string(),
                current_value: current.compute.gpu_utilization,
                expected_value: 80.0,
                deviation: (80.0 - current.compute.gpu_utilization) / 15.0,
                timestamp_secs: self.start_time.elapsed().as_secs_f64(),
                description: format!(
                    "GPU utilization is low ({:.1}%), suggesting compute inefficiency",
                    current.compute.gpu_utilization
                ),
            });
        }

        Ok(())
    }

    /// Check if a metric value is anomalous
    fn check_metric_anomaly(
        &self,
        metric_name: &str,
        current_value: f64,
        history: Vec<f64>,
        anomaly_type: AnomalyType,
    ) -> TorshResult<()> {
        let stats = Self::calculate_statistics(&history);

        if stats.std_dev == 0.0 {
            return Ok(());
        }

        let z_score = (current_value - stats.mean) / stats.std_dev;

        if z_score.abs() > ANOMALY_THRESHOLD {
            let severity = ((z_score.abs() - ANOMALY_THRESHOLD) * 2.0).clamp(5.0, 10.0) as u8;

            self.report_anomaly(PerformanceAnomaly {
                anomaly_type,
                severity,
                metric_name: metric_name.to_string(),
                current_value,
                expected_value: stats.mean,
                deviation: z_score,
                timestamp_secs: self.start_time.elapsed().as_secs_f64(),
                description: format!(
                    "{} is {:.1}σ from normal: current={:.2}, expected={:.2}±{:.2}",
                    metric_name, z_score, current_value, stats.mean, stats.std_dev
                ),
            });
        }

        Ok(())
    }

    /// Calculate statistical summary of values
    fn calculate_statistics(values: &[f64]) -> MetricStatistics {
        if values.is_empty() {
            return MetricStatistics {
                mean: 0.0,
                std_dev: 0.0,
                min: 0.0,
                max: 0.0,
                median: 0.0,
                p95: 0.0,
                p99: 0.0,
            };
        }

        let n = values.len() as f64;
        let mean = values.iter().sum::<f64>() / n;

        let variance = values.iter().map(|v| (v - mean).powi(2)).sum::<f64>() / n;
        let std_dev = variance.sqrt();

        let mut sorted = values.to_vec();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let min = sorted[0];
        let max = sorted[sorted.len() - 1];

        // Calculate median properly
        let median = if sorted.len() % 2 == 0 {
            let mid = sorted.len() / 2;
            (sorted[mid - 1] + sorted[mid]) / 2.0
        } else {
            sorted[sorted.len() / 2]
        };

        let p95 = sorted[(sorted.len() as f64 * 0.95).min((sorted.len() - 1) as f64) as usize];
        let p99 = sorted[(sorted.len() as f64 * 0.99).min((sorted.len() - 1) as f64) as usize];

        MetricStatistics {
            mean,
            std_dev,
            min,
            max,
            median,
            p95,
            p99,
        }
    }

    /// Report a detected anomaly
    fn report_anomaly(&self, anomaly: PerformanceAnomaly) {
        warn!(
            "🚨 Performance anomaly detected: {} (severity: {})",
            anomaly.description, anomaly.severity
        );

        self.anomalies.write().push(anomaly);
    }

    /// Generate optimization recommendations based on collected metrics
    pub fn generate_recommendations(&self) -> TorshResult<Vec<OptimizationRecommendation>> {
        let history = self.metrics_history.read();
        let mut recommendations = Vec::new();

        // Analyze metrics from all ranks
        for (_rank, metrics) in history.iter() {
            if metrics.is_empty() {
                continue;
            }

            let start_idx = metrics.len().saturating_sub(10);
            let recent: Vec<&AdvancedMetrics> = metrics.iter().skip(start_idx).collect();

            // Check communication overhead
            let avg_comm_ratio: f64 = recent
                .iter()
                .map(|m| m.communication.comm_comp_ratio)
                .sum::<f64>()
                / recent.len() as f64;

            if avg_comm_ratio > 0.3 {
                recommendations.push(OptimizationRecommendation {
                    category: RecommendationCategory::Communication,
                    priority: 9,
                    title: "High Communication Overhead Detected".to_string(),
                    description: format!(
                        "Communication takes {:.1}% of total time. Consider gradient accumulation or larger batch sizes.",
                        avg_comm_ratio * 100.0
                    ),
                    expected_improvement: 20.0,
                    difficulty: 2,
                    code_example: Some("# Increase gradient accumulation steps\naccumulation_steps = 4".to_string()),
                });
            }

            // Check GPU utilization
            let avg_gpu_util: f64 = recent
                .iter()
                .map(|m| m.compute.gpu_utilization)
                .sum::<f64>()
                / recent.len() as f64;

            if avg_gpu_util < 60.0 {
                recommendations.push(OptimizationRecommendation {
                    category: RecommendationCategory::BatchSize,
                    priority: 8,
                    title: "Low GPU Utilization".to_string(),
                    description: format!(
                        "GPU utilization is only {:.1}%. Consider increasing batch size to improve hardware utilization.",
                        avg_gpu_util
                    ),
                    expected_improvement: 30.0,
                    difficulty: 1,
                    code_example: Some("# Increase batch size\nbatch_size = current_batch_size * 2".to_string()),
                });
            }

            // Check memory usage
            let avg_memory_pct: f64 = recent
                .iter()
                .map(|m| (m.memory.gpu_memory_used_mb / m.memory.gpu_memory_total_mb) * 100.0)
                .sum::<f64>()
                / recent.len() as f64;

            if avg_memory_pct > 90.0 {
                recommendations.push(OptimizationRecommendation {
                    category: RecommendationCategory::Memory,
                    priority: 10,
                    title: "Near Memory Limit".to_string(),
                    description: "GPU memory usage is above 90%. Risk of OOM errors.".to_string(),
                    expected_improvement: 0.0,
                    difficulty: 3,
                    code_example: Some(
                        "# Enable gradient checkpointing\nmodel.gradient_checkpointing_enable()"
                            .to_string(),
                    ),
                });
            } else if avg_memory_pct < 50.0 {
                recommendations.push(OptimizationRecommendation {
                    category: RecommendationCategory::BatchSize,
                    priority: 6,
                    title: "Underutilized GPU Memory".to_string(),
                    description: format!(
                        "Only using {:.1}% of GPU memory. Can increase batch size for better performance.",
                        avg_memory_pct
                    ),
                    expected_improvement: 15.0,
                    difficulty: 1,
                    code_example: Some("# Increase batch size to use more memory\nbatch_size *= 1.5".to_string()),
                });
            }
        }

        // Sort by priority (descending)
        recommendations.sort_by(|a, b| b.priority.cmp(&a.priority));

        // Store recommendations
        *self.recommendations.write() = recommendations.clone();

        info!(
            "Generated {} optimization recommendations",
            recommendations.len()
        );

        Ok(recommendations)
    }

    /// Get all detected anomalies
    pub fn get_anomalies(&self) -> Vec<PerformanceAnomaly> {
        self.anomalies.read().clone()
    }

    /// Get recent anomalies (last N)
    pub fn get_recent_anomalies(&self, count: usize) -> Vec<PerformanceAnomaly> {
        let anomalies = self.anomalies.read();
        anomalies.iter().rev().take(count).cloned().collect()
    }

    /// Clear all anomalies
    pub fn clear_anomalies(&self) {
        self.anomalies.write().clear();
    }

    /// Get current optimization recommendations
    pub fn get_recommendations(&self) -> Vec<OptimizationRecommendation> {
        self.recommendations.read().clone()
    }

    /// Get metrics history for a specific rank
    pub fn get_rank_history(&self, rank: u32) -> Option<Vec<AdvancedMetrics>> {
        self.metrics_history
            .read()
            .get(&rank)
            .map(|h| h.iter().cloned().collect())
    }

    /// Get latest metrics for all ranks
    pub async fn get_latest_metrics(&self) -> TorshResult<HashMap<u32, AdvancedMetrics>> {
        let history = self.metrics_history.read();
        let mut latest_metrics = HashMap::new();

        for (rank, metrics) in history.iter() {
            if let Some(latest) = metrics.back() {
                latest_metrics.insert(*rank, latest.clone());
            }
        }

        Ok(latest_metrics)
    }

    /// Get aggregated metrics across all ranks
    pub fn get_aggregated_metrics(&self) -> Option<AdvancedMetrics> {
        let history = self.metrics_history.read();

        if history.is_empty() {
            return None;
        }

        // Get the most recent metrics from each rank
        let recent_metrics: Vec<&AdvancedMetrics> =
            history.values().filter_map(|h| h.back()).collect();

        if recent_metrics.is_empty() {
            return None;
        }

        let count = recent_metrics.len() as f64;

        // Calculate averages
        Some(AdvancedMetrics {
            timestamp: self.start_time.elapsed(),
            compute: ComputeMetrics {
                forward_time_ms: recent_metrics
                    .iter()
                    .map(|m| m.compute.forward_time_ms)
                    .sum::<f64>()
                    / count,
                backward_time_ms: recent_metrics
                    .iter()
                    .map(|m| m.compute.backward_time_ms)
                    .sum::<f64>()
                    / count,
                optimizer_time_ms: recent_metrics
                    .iter()
                    .map(|m| m.compute.optimizer_time_ms)
                    .sum::<f64>()
                    / count,
                gpu_utilization: recent_metrics
                    .iter()
                    .map(|m| m.compute.gpu_utilization)
                    .sum::<f64>()
                    / count,
                cpu_utilization: recent_metrics
                    .iter()
                    .map(|m| m.compute.cpu_utilization)
                    .sum::<f64>()
                    / count,
                tensor_core_utilization: recent_metrics
                    .iter()
                    .map(|m| m.compute.tensor_core_utilization)
                    .sum::<f64>()
                    / count,
                gflops: recent_metrics.iter().map(|m| m.compute.gflops).sum::<f64>() / count,
            },
            communication: CommunicationMetrics {
                all_reduce_time_ms: recent_metrics
                    .iter()
                    .map(|m| m.communication.all_reduce_time_ms)
                    .sum::<f64>()
                    / count,
                broadcast_time_ms: recent_metrics
                    .iter()
                    .map(|m| m.communication.broadcast_time_ms)
                    .sum::<f64>()
                    / count,
                all_gather_time_ms: recent_metrics
                    .iter()
                    .map(|m| m.communication.all_gather_time_ms)
                    .sum::<f64>()
                    / count,
                bandwidth_mbps: recent_metrics
                    .iter()
                    .map(|m| m.communication.bandwidth_mbps)
                    .sum::<f64>()
                    / count,
                comm_comp_ratio: recent_metrics
                    .iter()
                    .map(|m| m.communication.comm_comp_ratio)
                    .sum::<f64>()
                    / count,
                num_operations: (recent_metrics
                    .iter()
                    .map(|m| m.communication.num_operations)
                    .sum::<u64>() as f64
                    / count) as u64,
                avg_message_size: (recent_metrics
                    .iter()
                    .map(|m| m.communication.avg_message_size)
                    .sum::<usize>() as f64
                    / count) as usize,
            },
            memory: MemoryMetrics {
                gpu_memory_used_mb: recent_metrics
                    .iter()
                    .map(|m| m.memory.gpu_memory_used_mb)
                    .sum::<f64>()
                    / count,
                gpu_memory_total_mb: recent_metrics
                    .iter()
                    .map(|m| m.memory.gpu_memory_total_mb)
                    .sum::<f64>()
                    / count,
                cpu_memory_used_mb: recent_metrics
                    .iter()
                    .map(|m| m.memory.cpu_memory_used_mb)
                    .sum::<f64>()
                    / count,
                memory_bandwidth_gbps: recent_metrics
                    .iter()
                    .map(|m| m.memory.memory_bandwidth_gbps)
                    .sum::<f64>()
                    / count,
                num_allocations: (recent_metrics
                    .iter()
                    .map(|m| m.memory.num_allocations)
                    .sum::<u64>() as f64
                    / count) as u64,
                peak_memory_mb: recent_metrics
                    .iter()
                    .map(|m| m.memory.peak_memory_mb)
                    .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
                    .unwrap_or(0.0),
            },
            io: IoMetrics {
                data_load_time_ms: recent_metrics
                    .iter()
                    .map(|m| m.io.data_load_time_ms)
                    .sum::<f64>()
                    / count,
                disk_read_mbps: recent_metrics
                    .iter()
                    .map(|m| m.io.disk_read_mbps)
                    .sum::<f64>()
                    / count,
                disk_write_mbps: recent_metrics
                    .iter()
                    .map(|m| m.io.disk_write_mbps)
                    .sum::<f64>()
                    / count,
                preprocessing_time_ms: recent_metrics
                    .iter()
                    .map(|m| m.io.preprocessing_time_ms)
                    .sum::<f64>()
                    / count,
            },
            custom: HashMap::new(),
        })
    }

    /// Enable or disable monitoring
    pub fn set_enabled(&self, enabled: bool) {
        *self.enabled.write() = enabled;
        info!(
            "Advanced monitoring {}",
            if enabled { "enabled" } else { "disabled" }
        );
    }

    /// Check if monitoring is enabled
    pub fn is_enabled(&self) -> bool {
        *self.enabled.read()
    }

    /// Set custom threshold for anomaly detection
    pub fn set_threshold(&self, metric_name: String, threshold: f64) {
        self.custom_thresholds
            .write()
            .insert(metric_name, threshold);
    }

    /// Generate a comprehensive performance report
    pub fn generate_report(&self) -> String {
        let mut report = String::new();

        report.push_str("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
        report.push_str("   📊 ADVANCED PERFORMANCE MONITORING REPORT\n");
        report.push_str("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n");

        // Aggregated metrics
        if let Some(metrics) = self.get_aggregated_metrics() {
            report.push_str("📈 Aggregated Metrics (All Ranks):\n");
            report.push_str(&format!(
                "   Compute: fwd={:.2}ms, bwd={:.2}ms, opt={:.2}ms\n",
                metrics.compute.forward_time_ms,
                metrics.compute.backward_time_ms,
                metrics.compute.optimizer_time_ms
            ));
            report.push_str(&format!(
                "   GPU Utilization: {:.1}%\n",
                metrics.compute.gpu_utilization
            ));
            report.push_str(&format!(
                "   Communication: {:.2}ms ({:.1}% of total)\n",
                metrics.communication.all_reduce_time_ms,
                metrics.communication.comm_comp_ratio * 100.0
            ));
            report.push_str(&format!(
                "   Memory: {:.0}/{:.0} MB ({:.1}%)\n\n",
                metrics.memory.gpu_memory_used_mb,
                metrics.memory.gpu_memory_total_mb,
                (metrics.memory.gpu_memory_used_mb / metrics.memory.gpu_memory_total_mb) * 100.0
            ));
        }

        // Recent anomalies
        let anomalies = self.get_recent_anomalies(5);
        if !anomalies.is_empty() {
            report.push_str("🚨 Recent Anomalies:\n");
            for anomaly in anomalies {
                report.push_str(&format!(
                    "   [{:?}] {} (severity: {})\n",
                    anomaly.anomaly_type, anomaly.description, anomaly.severity
                ));
            }
            report.push('\n');
        }

        // Recommendations
        let recommendations = self.get_recommendations();
        if !recommendations.is_empty() {
            report.push_str("💡 Top Optimization Recommendations:\n");
            for (i, rec) in recommendations.iter().take(5).enumerate() {
                report.push_str(&format!(
                    "   {}. [Priority {}] {}\n",
                    i + 1,
                    rec.priority,
                    rec.title
                ));
                report.push_str(&format!("      {}\n", rec.description));
                if rec.expected_improvement > 0.0 {
                    report.push_str(&format!(
                        "      Expected improvement: {:.1}%\n",
                        rec.expected_improvement
                    ));
                }
            }
        }

        report.push_str("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

        report
    }
}

impl Default for ComputeMetrics {
    fn default() -> Self {
        Self {
            forward_time_ms: 0.0,
            backward_time_ms: 0.0,
            optimizer_time_ms: 0.0,
            gpu_utilization: 0.0,
            cpu_utilization: 0.0,
            tensor_core_utilization: 0.0,
            gflops: 0.0,
        }
    }
}

impl Default for CommunicationMetrics {
    fn default() -> Self {
        Self {
            all_reduce_time_ms: 0.0,
            broadcast_time_ms: 0.0,
            all_gather_time_ms: 0.0,
            bandwidth_mbps: 0.0,
            comm_comp_ratio: 0.0,
            num_operations: 0,
            avg_message_size: 0,
        }
    }
}

impl Default for MemoryMetrics {
    fn default() -> Self {
        Self {
            gpu_memory_used_mb: 0.0,
            gpu_memory_total_mb: 16384.0, // 16GB default
            cpu_memory_used_mb: 0.0,
            memory_bandwidth_gbps: 0.0,
            num_allocations: 0,
            peak_memory_mb: 0.0,
        }
    }
}

impl Default for IoMetrics {
    fn default() -> Self {
        Self {
            data_load_time_ms: 0.0,
            disk_read_mbps: 0.0,
            disk_write_mbps: 0.0,
            preprocessing_time_ms: 0.0,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{init_process_group, BackendType};

    #[tokio::test]
    async fn test_advanced_monitor_creation() {
        let pg = init_process_group(BackendType::Gloo, 0, 1, "127.0.0.1", 29500)
            .await
            .unwrap();

        let monitor = AdvancedMonitor::new(Arc::new(pg));
        assert!(monitor.is_enabled());
        assert_eq!(monitor.get_anomalies().len(), 0);
    }

    #[tokio::test]
    async fn test_metrics_recording() {
        let pg = init_process_group(BackendType::Gloo, 0, 1, "127.0.0.1", 29500)
            .await
            .unwrap();

        let monitor = AdvancedMonitor::new(Arc::new(pg));

        let metrics = AdvancedMetrics {
            timestamp: Duration::from_secs(1),
            compute: ComputeMetrics {
                forward_time_ms: 10.0,
                backward_time_ms: 15.0,
                gpu_utilization: 85.0,
                ..Default::default()
            },
            communication: CommunicationMetrics::default(),
            memory: MemoryMetrics::default(),
            io: IoMetrics::default(),
            custom: HashMap::new(),
        };

        assert!(monitor.record_metrics(metrics).is_ok());

        let history = monitor.get_rank_history(0);
        assert!(history.is_some());
        assert_eq!(history.unwrap().len(), 1);
    }

    #[tokio::test]
    async fn test_anomaly_detection() {
        let pg = init_process_group(BackendType::Gloo, 0, 1, "127.0.0.1", 29500)
            .await
            .unwrap();

        let monitor = AdvancedMonitor::new(Arc::new(pg));

        // Record normal metrics
        for i in 0..15 {
            let metrics = AdvancedMetrics {
                timestamp: Duration::from_secs(i),
                compute: ComputeMetrics {
                    forward_time_ms: 10.0,
                    gpu_utilization: 80.0,
                    ..Default::default()
                },
                communication: CommunicationMetrics::default(),
                memory: MemoryMetrics::default(),
                io: IoMetrics::default(),
                custom: HashMap::new(),
            };
            monitor.record_metrics(metrics).unwrap();
        }

        // Record anomalous metric
        let anomalous_metrics = AdvancedMetrics {
            timestamp: Duration::from_secs(16),
            compute: ComputeMetrics {
                forward_time_ms: 100.0, // 10x slower!
                gpu_utilization: 80.0,
                ..Default::default()
            },
            communication: CommunicationMetrics::default(),
            memory: MemoryMetrics::default(),
            io: IoMetrics::default(),
            custom: HashMap::new(),
        };
        monitor.record_metrics(anomalous_metrics).unwrap();

        // Should have detected anomaly
        let anomalies = monitor.get_anomalies();
        assert!(!anomalies.is_empty());
    }

    #[tokio::test]
    async fn test_recommendation_generation() {
        let pg = init_process_group(BackendType::Gloo, 0, 1, "127.0.0.1", 29500)
            .await
            .unwrap();

        let monitor = AdvancedMonitor::new(Arc::new(pg));

        // Record metrics with low GPU utilization
        for i in 0..20 {
            let metrics = AdvancedMetrics {
                timestamp: Duration::from_secs(i),
                compute: ComputeMetrics {
                    forward_time_ms: 10.0,
                    gpu_utilization: 40.0, // Low utilization
                    ..Default::default()
                },
                communication: CommunicationMetrics {
                    comm_comp_ratio: 0.4, // High comm ratio
                    ..Default::default()
                },
                memory: MemoryMetrics::default(),
                io: IoMetrics::default(),
                custom: HashMap::new(),
            };
            monitor.record_metrics(metrics).unwrap();
        }

        let recommendations = monitor.generate_recommendations().unwrap();
        assert!(!recommendations.is_empty());

        // Should recommend batch size increase due to low GPU utilization
        assert!(recommendations
            .iter()
            .any(|r| r.category == RecommendationCategory::BatchSize));
    }

    #[tokio::test]
    async fn test_aggregated_metrics() {
        let pg = init_process_group(BackendType::Gloo, 0, 2, "127.0.0.1", 29500)
            .await
            .unwrap();

        let monitor = AdvancedMonitor::new(Arc::new(pg));

        // Simulate metrics from multiple ranks
        let mut history = monitor.metrics_history.write();
        for rank in 0..2 {
            let mut rank_history = VecDeque::new();
            rank_history.push_back(AdvancedMetrics {
                timestamp: Duration::from_secs(1),
                compute: ComputeMetrics {
                    forward_time_ms: 10.0 + rank as f64,
                    gpu_utilization: 80.0,
                    ..Default::default()
                },
                communication: CommunicationMetrics::default(),
                memory: MemoryMetrics::default(),
                io: IoMetrics::default(),
                custom: HashMap::new(),
            });
            history.insert(rank, rank_history);
        }
        drop(history);

        let aggregated = monitor.get_aggregated_metrics();
        assert!(aggregated.is_some());

        let metrics = aggregated.unwrap();
        // Average of 10.0 and 11.0
        assert!((metrics.compute.forward_time_ms - 10.5).abs() < 0.1);
    }

    #[test]
    fn test_statistics_calculation() {
        let values = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        let stats = AdvancedMonitor::calculate_statistics(&values);

        assert!((stats.mean - 5.5).abs() < 0.1);
        // Median of 10 values is average of 5th and 6th values: (5.0 + 6.0) / 2 = 5.5
        assert!((stats.median - 5.5).abs() < 0.1);
        assert_eq!(stats.min, 1.0);
        assert_eq!(stats.max, 10.0);
    }
}